1*5b190011SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*5b190011SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*5b190011SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*5b190011SAndrew Rist  * distributed with this work for additional information
6*5b190011SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*5b190011SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*5b190011SAndrew Rist  * "License"); you may not use this file except in compliance
9*5b190011SAndrew Rist  * with the License.  You may obtain a copy of the License at
10*5b190011SAndrew Rist  *
11*5b190011SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*5b190011SAndrew Rist  *
13*5b190011SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*5b190011SAndrew Rist  * software distributed under the License is distributed on an
15*5b190011SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*5b190011SAndrew Rist  * KIND, either express or implied.  See the License for the
17*5b190011SAndrew Rist  * specific language governing permissions and limitations
18*5b190011SAndrew Rist  * under the License.
19*5b190011SAndrew Rist  *
20*5b190011SAndrew Rist  *************************************************************/
21*5b190011SAndrew Rist 
22*5b190011SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir #include "precompiled_sd.hxx"
25cdf0e10cSrcweir 
26cdf0e10cSrcweir #include "ConfigurationClassifier.hxx"
27cdf0e10cSrcweir 
28cdf0e10cSrcweir #include "framework/FrameworkHelper.hxx"
29cdf0e10cSrcweir 
30cdf0e10cSrcweir using namespace ::com::sun::star;
31cdf0e10cSrcweir using namespace ::com::sun::star::uno;
32cdf0e10cSrcweir using namespace ::com::sun::star::drawing::framework;
33cdf0e10cSrcweir using ::rtl::OUString;
34cdf0e10cSrcweir 
35cdf0e10cSrcweir #undef VERBOSE
36cdf0e10cSrcweir //#define VERBOSE 2
37cdf0e10cSrcweir 
38cdf0e10cSrcweir 
39cdf0e10cSrcweir namespace sd { namespace framework {
40cdf0e10cSrcweir 
ConfigurationClassifier(const Reference<XConfiguration> & rxConfiguration1,const Reference<XConfiguration> & rxConfiguration2)41cdf0e10cSrcweir ConfigurationClassifier::ConfigurationClassifier (
42cdf0e10cSrcweir     const Reference<XConfiguration>& rxConfiguration1,
43cdf0e10cSrcweir     const Reference<XConfiguration>& rxConfiguration2)
44cdf0e10cSrcweir     : mxConfiguration1(rxConfiguration1),
45cdf0e10cSrcweir       mxConfiguration2(rxConfiguration2),
46cdf0e10cSrcweir       maC1minusC2(),
47cdf0e10cSrcweir       maC2minusC1(),
48cdf0e10cSrcweir       maC1andC2()
49cdf0e10cSrcweir {
50cdf0e10cSrcweir }
51cdf0e10cSrcweir 
52cdf0e10cSrcweir 
53cdf0e10cSrcweir 
54cdf0e10cSrcweir 
Partition(void)55cdf0e10cSrcweir bool ConfigurationClassifier::Partition (void)
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     maC1minusC2.clear();
58cdf0e10cSrcweir     maC2minusC1.clear();
59cdf0e10cSrcweir     maC1andC2.clear();
60cdf0e10cSrcweir 
61cdf0e10cSrcweir     PartitionResources(
62cdf0e10cSrcweir         mxConfiguration1->getResources(NULL, OUString(), AnchorBindingMode_DIRECT),
63cdf0e10cSrcweir         mxConfiguration2->getResources(NULL, OUString(), AnchorBindingMode_DIRECT));
64cdf0e10cSrcweir 
65cdf0e10cSrcweir     return !maC1minusC2.empty() || !maC2minusC1.empty();
66cdf0e10cSrcweir }
67cdf0e10cSrcweir 
68cdf0e10cSrcweir 
69cdf0e10cSrcweir 
70cdf0e10cSrcweir 
GetC1minusC2(void) const71cdf0e10cSrcweir const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC1minusC2 (void) const
72cdf0e10cSrcweir {
73cdf0e10cSrcweir     return maC1minusC2;
74cdf0e10cSrcweir }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir 
77cdf0e10cSrcweir 
78cdf0e10cSrcweir 
GetC2minusC1(void) const79cdf0e10cSrcweir const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC2minusC1 (void) const
80cdf0e10cSrcweir {
81cdf0e10cSrcweir     return maC2minusC1;
82cdf0e10cSrcweir }
83cdf0e10cSrcweir 
84cdf0e10cSrcweir 
85cdf0e10cSrcweir 
GetC1andC2(void) const86cdf0e10cSrcweir const ConfigurationClassifier::ResourceIdVector& ConfigurationClassifier::GetC1andC2 (void) const
87cdf0e10cSrcweir {
88cdf0e10cSrcweir     return maC1andC2;
89cdf0e10cSrcweir }
90cdf0e10cSrcweir 
91cdf0e10cSrcweir 
PartitionResources(const::com::sun::star::uno::Sequence<Reference<XResourceId>> & rS1,const::com::sun::star::uno::Sequence<Reference<XResourceId>> & rS2)92cdf0e10cSrcweir void ConfigurationClassifier::PartitionResources (
93cdf0e10cSrcweir     const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS1,
94cdf0e10cSrcweir     const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS2)
95cdf0e10cSrcweir {
96cdf0e10cSrcweir     ResourceIdVector aC1minusC2;
97cdf0e10cSrcweir     ResourceIdVector aC2minusC1;
98cdf0e10cSrcweir     ResourceIdVector aC1andC2;
99cdf0e10cSrcweir 
100cdf0e10cSrcweir     // Classify the resources in the configurations that are not bound to
101cdf0e10cSrcweir     // other resources.
102cdf0e10cSrcweir     ClassifyResources(
103cdf0e10cSrcweir         rS1,
104cdf0e10cSrcweir         rS2,
105cdf0e10cSrcweir         aC1minusC2,
106cdf0e10cSrcweir         aC2minusC1,
107cdf0e10cSrcweir         aC1andC2);
108cdf0e10cSrcweir 
109cdf0e10cSrcweir #if defined VERBOSE && VERBOSE >= 2
110cdf0e10cSrcweir     OSL_TRACE("copying resource ids to C1-C2\r");
111cdf0e10cSrcweir #endif
112cdf0e10cSrcweir     CopyResources(aC1minusC2, mxConfiguration1, maC1minusC2);
113cdf0e10cSrcweir #if defined VERBOSE && VERBOSE >= 2
114cdf0e10cSrcweir     OSL_TRACE("copying resource ids to C2-C1\r");
115cdf0e10cSrcweir #endif
116cdf0e10cSrcweir     CopyResources(aC2minusC1, mxConfiguration2, maC2minusC1);
117cdf0e10cSrcweir 
118cdf0e10cSrcweir     // Process the unique resources that belong to both configurations.
119cdf0e10cSrcweir     ResourceIdVector::const_iterator iResource;
120cdf0e10cSrcweir     for (iResource=aC1andC2.begin(); iResource!=aC1andC2.end(); ++iResource)
121cdf0e10cSrcweir     {
122cdf0e10cSrcweir         maC1andC2.push_back(*iResource);
123cdf0e10cSrcweir         PartitionResources(
124cdf0e10cSrcweir             mxConfiguration1->getResources(*iResource, OUString(), AnchorBindingMode_DIRECT),
125cdf0e10cSrcweir             mxConfiguration2->getResources(*iResource, OUString(), AnchorBindingMode_DIRECT));
126cdf0e10cSrcweir     }
127cdf0e10cSrcweir }
128cdf0e10cSrcweir 
129cdf0e10cSrcweir 
130cdf0e10cSrcweir 
131cdf0e10cSrcweir 
ClassifyResources(const::com::sun::star::uno::Sequence<Reference<XResourceId>> & rS1,const::com::sun::star::uno::Sequence<Reference<XResourceId>> & rS2,ResourceIdVector & rS1minusS2,ResourceIdVector & rS2minusS1,ResourceIdVector & rS1andS2)132cdf0e10cSrcweir void ConfigurationClassifier::ClassifyResources (
133cdf0e10cSrcweir     const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS1,
134cdf0e10cSrcweir     const ::com::sun::star::uno::Sequence<Reference<XResourceId> >& rS2,
135cdf0e10cSrcweir     ResourceIdVector& rS1minusS2,
136cdf0e10cSrcweir     ResourceIdVector& rS2minusS1,
137cdf0e10cSrcweir     ResourceIdVector& rS1andS2)
138cdf0e10cSrcweir {
139cdf0e10cSrcweir     // Get arrays from the sequences for faster iteration.
140cdf0e10cSrcweir     const Reference<XResourceId>* aA1 = rS1.getConstArray();
141cdf0e10cSrcweir     const Reference<XResourceId>* aA2 = rS2.getConstArray();
142cdf0e10cSrcweir     sal_Int32 nL1 (rS1.getLength());
143cdf0e10cSrcweir     sal_Int32 nL2 (rS2.getLength());
144cdf0e10cSrcweir 
145cdf0e10cSrcweir     // Find all elements in rS1 and place them in rS1minusS2 or rS1andS2
146cdf0e10cSrcweir     // depending on whether they are in rS2 or not.
147cdf0e10cSrcweir     for (sal_Int32 i=0; i<nL1; ++i)
148cdf0e10cSrcweir     {
149cdf0e10cSrcweir         bool bFound (false);
150cdf0e10cSrcweir         for (sal_Int32 j=0; j<nL2 && !bFound; ++j)
151cdf0e10cSrcweir             if (aA1[i]->getResourceURL().equals(aA2[j]->getResourceURL()))
152cdf0e10cSrcweir                 bFound = true;
153cdf0e10cSrcweir 
154cdf0e10cSrcweir         if (bFound)
155cdf0e10cSrcweir             rS1andS2.push_back(aA1[i]);
156cdf0e10cSrcweir         else
157cdf0e10cSrcweir             rS1minusS2.push_back(aA1[i]);
158cdf0e10cSrcweir     }
159cdf0e10cSrcweir 
160cdf0e10cSrcweir     // Find all elements in rS2 that are not in rS1.  The elements that are
161cdf0e10cSrcweir     // in both rS1 and rS2 have been handled above and are therefore ignored
162cdf0e10cSrcweir     // here.
163cdf0e10cSrcweir     for (sal_Int32 j=0; j<nL2; ++j)
164cdf0e10cSrcweir     {
165cdf0e10cSrcweir         bool bFound (false);
166cdf0e10cSrcweir         for (sal_Int32 i=0; i<nL1 && !bFound; ++i)
167cdf0e10cSrcweir             if (aA2[j]->getResourceURL().equals(aA1[i]->getResourceURL()))
168cdf0e10cSrcweir                 bFound = true;
169cdf0e10cSrcweir 
170cdf0e10cSrcweir         if ( ! bFound)
171cdf0e10cSrcweir             rS2minusS1.push_back(aA2[j]);
172cdf0e10cSrcweir     }
173cdf0e10cSrcweir }
174cdf0e10cSrcweir 
175cdf0e10cSrcweir 
176cdf0e10cSrcweir 
177cdf0e10cSrcweir 
CopyResources(const ResourceIdVector & rSource,const Reference<XConfiguration> & rxConfiguration,ResourceIdVector & rTarget)178cdf0e10cSrcweir void ConfigurationClassifier::CopyResources (
179cdf0e10cSrcweir     const ResourceIdVector& rSource,
180cdf0e10cSrcweir     const Reference<XConfiguration>& rxConfiguration,
181cdf0e10cSrcweir     ResourceIdVector& rTarget)
182cdf0e10cSrcweir {
183cdf0e10cSrcweir     // Copy all resources bound to the ones in aC1minusC2Unique to rC1minusC2.
184cdf0e10cSrcweir     ResourceIdVector::const_iterator iResource (rSource.begin());
185cdf0e10cSrcweir     ResourceIdVector::const_iterator iEnd(rSource.end());
186cdf0e10cSrcweir     for ( ; iResource!=iEnd; ++iResource)
187cdf0e10cSrcweir     {
188cdf0e10cSrcweir         const Sequence<Reference<XResourceId> > aBoundResources (
189cdf0e10cSrcweir             rxConfiguration->getResources(
190cdf0e10cSrcweir                 *iResource,
191cdf0e10cSrcweir                 OUString(),
192cdf0e10cSrcweir                 AnchorBindingMode_INDIRECT));
193cdf0e10cSrcweir         const sal_Int32 nL (aBoundResources.getLength());
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         rTarget.reserve(rTarget.size() + 1 + nL);
196cdf0e10cSrcweir         rTarget.push_back(*iResource);
197cdf0e10cSrcweir 
198cdf0e10cSrcweir #if defined VERBOSE && VERBOSE >= 2
199cdf0e10cSrcweir         OSL_TRACE("    copying %s\r",
200cdf0e10cSrcweir             OUStringToOString(FrameworkHelper::ResourceIdToString(*iResource),
201cdf0e10cSrcweir                 RTL_TEXTENCODING_UTF8).getStr());
202cdf0e10cSrcweir #endif
203cdf0e10cSrcweir 
204cdf0e10cSrcweir         const Reference<XResourceId>* aA = aBoundResources.getConstArray();
205cdf0e10cSrcweir         for (sal_Int32 i=0; i<nL; ++i)
206cdf0e10cSrcweir         {
207cdf0e10cSrcweir             rTarget.push_back(aA[i]);
208cdf0e10cSrcweir #if defined VERBOSE && VERBOSE >= 2
209cdf0e10cSrcweir             OSL_TRACE("    copying %s\r",
210cdf0e10cSrcweir                 OUStringToOString(FrameworkHelper::ResourceIdToString(aA[i]),
211cdf0e10cSrcweir                     RTL_TEXTENCODING_UTF8).getStr());
212cdf0e10cSrcweir #endif
213cdf0e10cSrcweir         }
214cdf0e10cSrcweir     }
215cdf0e10cSrcweir }
216cdf0e10cSrcweir 
217cdf0e10cSrcweir 
TraceResourceIdVector(const sal_Char * pMessage,const ResourceIdVector & rResources) const218cdf0e10cSrcweir void ConfigurationClassifier::TraceResourceIdVector (
219cdf0e10cSrcweir     const sal_Char* pMessage,
220cdf0e10cSrcweir     const ResourceIdVector& rResources) const
221cdf0e10cSrcweir {
222cdf0e10cSrcweir 
223cdf0e10cSrcweir     OSL_TRACE(pMessage);
224cdf0e10cSrcweir     ResourceIdVector::const_iterator iResource;
225cdf0e10cSrcweir     for (iResource=rResources.begin(); iResource!=rResources.end(); ++iResource)
226cdf0e10cSrcweir     {
227cdf0e10cSrcweir         OUString sResource (FrameworkHelper::ResourceIdToString(*iResource));
228cdf0e10cSrcweir         OSL_TRACE("    %s\r",
229cdf0e10cSrcweir             OUStringToOString(sResource, RTL_TEXTENCODING_UTF8).getStr());
230cdf0e10cSrcweir     }
231cdf0e10cSrcweir }
232cdf0e10cSrcweir 
233cdf0e10cSrcweir 
234cdf0e10cSrcweir } } // end of namespace sd::framework
235