merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / framework / configuration / ConfigurationClassifier.cxx
blob0645f5f47bfce69b1e9388456dc6544dece064b6
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationClassifier.cxx,v $
10 * $Revision: 1.4 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
33 #include "ConfigurationClassifier.hxx"
35 #include "framework/FrameworkHelper.hxx"
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::drawing::framework;
40 using ::rtl::OUString;
42 #undef VERBOSE
43 //#define VERBOSE 2
46 namespace sd { namespace framework {
48 ConfigurationClassifier::ConfigurationClassifier (
49 const Reference<XConfiguration>& rxConfiguration1,
50 const Reference<XConfiguration>& rxConfiguration2)
51 : mxConfiguration1(rxConfiguration1),
52 mxConfiguration2(rxConfiguration2),
53 maC1minusC2(),
54 maC2minusC1(),
55 maC1andC2()
62 bool ConfigurationClassifier::Partition (void)
64 maC1minusC2.clear();
65 maC2minusC1.clear();
66 maC1andC2.clear();
68 PartitionResources(
69 mxConfiguration1->getResources(NULL, OUString(), AnchorBindingMode_DIRECT),
70 mxConfiguration2->getResources(NULL, OUString(), AnchorBindingMode_DIRECT));
72 return !maC1minusC2.empty() || !maC2minusC1.empty();
78 const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC1minusC2 (void) const
80 return maC1minusC2;
86 const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC2minusC1 (void) const
88 return maC2minusC1;
94 const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC1andC2 (void) const
96 return maC1andC2;
102 void ConfigurationClassifier::PartitionResources (
103 const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS1,
104 const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS2)
106 ResourceIdVector aC1minusC2;
107 ResourceIdVector aC2minusC1;
108 ResourceIdVector aC1andC2;
110 // Classify the resources in the configurations that are not bound to
111 // other resources.
112 ClassifyResources(
113 rS1,
114 rS2,
115 aC1minusC2,
116 aC2minusC1,
117 aC1andC2);
119 #if defined VERBOSE && VERBOSE >= 2
120 OSL_TRACE("copying resource ids to C1-C2\r");
121 #endif
122 CopyResources(aC1minusC2, mxConfiguration1, maC1minusC2);
123 #if defined VERBOSE && VERBOSE >= 2
124 OSL_TRACE("copying resource ids to C2-C1\r");
125 #endif
126 CopyResources(aC2minusC1, mxConfiguration2, maC2minusC1);
128 // Process the unique resources that belong to both configurations.
129 ResourceIdVector::const_iterator iResource;
130 for (iResource=aC1andC2.begin(); iResource!=aC1andC2.end(); ++iResource)
132 maC1andC2.push_back(*iResource);
133 PartitionResources(
134 mxConfiguration1->getResources(*iResource, OUString(), AnchorBindingMode_DIRECT),
135 mxConfiguration2->getResources(*iResource, OUString(), AnchorBindingMode_DIRECT));
142 void ConfigurationClassifier::ClassifyResources (
143 const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS1,
144 const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS2,
145 ResourceIdVector& rS1minusS2,
146 ResourceIdVector& rS2minusS1,
147 ResourceIdVector& rS1andS2)
149 // Get arrays from the sequences for faster iteration.
150 const Reference<XResourceId>* aA1 = rS1.getConstArray();
151 const Reference<XResourceId>* aA2 = rS2.getConstArray();
152 sal_Int32 nL1 (rS1.getLength());
153 sal_Int32 nL2 (rS2.getLength());
155 // Find all elements in rS1 and place them in rS1minusS2 or rS1andS2
156 // depending on whether they are in rS2 or not.
157 for (sal_Int32 i=0; i<nL1; ++i)
159 bool bFound (false);
160 for (sal_Int32 j=0; j<nL2 && !bFound; ++j)
161 if (aA1[i]->getResourceURL().equals(aA2[j]->getResourceURL()))
162 bFound = true;
164 if (bFound)
165 rS1andS2.push_back(aA1[i]);
166 else
167 rS1minusS2.push_back(aA1[i]);
170 // Find all elements in rS2 that are not in rS1. The elements that are
171 // in both rS1 and rS2 have been handled above and are therefore ignored
172 // here.
173 for (sal_Int32 j=0; j<nL2; ++j)
175 bool bFound (false);
176 for (sal_Int32 i=0; i<nL1 && !bFound; ++i)
177 if (aA2[j]->getResourceURL().equals(aA1[i]->getResourceURL()))
178 bFound = true;
180 if ( ! bFound)
181 rS2minusS1.push_back(aA2[j]);
188 void ConfigurationClassifier::CopyResources (
189 const ResourceIdVector& rSource,
190 const Reference<XConfiguration>& rxConfiguration,
191 ResourceIdVector& rTarget)
193 // Copy all resources bound to the ones in aC1minusC2Unique to rC1minusC2.
194 ResourceIdVector::const_iterator iResource (rSource.begin());
195 ResourceIdVector::const_iterator iEnd(rSource.end());
196 for ( ; iResource!=iEnd; ++iResource)
198 const Sequence<Reference<XResourceId> > aBoundResources (
199 rxConfiguration->getResources(
200 *iResource,
201 OUString(),
202 AnchorBindingMode_INDIRECT));
203 const sal_Int32 nL (aBoundResources.getLength());
205 rTarget.reserve(rTarget.size() + 1 + nL);
206 rTarget.push_back(*iResource);
208 #if defined VERBOSE && VERBOSE >= 2
209 OSL_TRACE(" copying %s\r",
210 OUStringToOString(FrameworkHelper::ResourceIdToString(*iResource),
211 RTL_TEXTENCODING_UTF8).getStr());
212 #endif
214 const Reference<XResourceId>* aA = aBoundResources.getConstArray();
215 for (sal_Int32 i=0; i<nL; ++i)
217 rTarget.push_back(aA[i]);
218 #if defined VERBOSE && VERBOSE >= 2
219 OSL_TRACE(" copying %s\r",
220 OUStringToOString(FrameworkHelper::ResourceIdToString(aA[i]),
221 RTL_TEXTENCODING_UTF8).getStr());
222 #endif
230 void ConfigurationClassifier::TraceResourceIdVector (
231 const sal_Char* pMessage,
232 const ResourceIdVector& rResources) const
235 OSL_TRACE(pMessage);
236 ResourceIdVector::const_iterator iResource;
237 for (iResource=rResources.begin(); iResource!=rResources.end(); ++iResource)
239 OUString sResource (FrameworkHelper::ResourceIdToString(*iResource));
240 OSL_TRACE(" %s\r",
241 OUStringToOString(sResource, RTL_TEXTENCODING_UTF8).getStr());
248 } } // end of namespace sd::framework