1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 #include "precompiled_sd.hxx"
25
26 #include "framework/Configuration.hxx"
27
28 #include "framework/FrameworkHelper.hxx"
29 #include <comphelper/stl_types.hxx>
30
31
32
33 using namespace ::com::sun::star;
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::drawing::framework;
36 using ::sd::framework::FrameworkHelper;
37 using ::rtl::OUString;
38
39 #undef VERBOSE
40
41 namespace {
42 /** Use the XResourceId::compareTo() method to implement a compare operator
43 for STL containers.
44 */
45 class XResourceIdLess
46 : public ::std::binary_function <Reference<XResourceId>, Reference<XResourceId>, bool>
47 {
48 public:
operator ()(const Reference<XResourceId> & rId1,const Reference<XResourceId> & rId2) const49 bool operator () (const Reference<XResourceId>& rId1, const Reference<XResourceId>& rId2) const
50 {
51 return rId1->compareTo(rId2) == -1;
52 }
53 };
54
55 } // end of anonymous namespace
56
57
58
59
60 namespace sd { namespace framework {
61
62
63 class Configuration::ResourceContainer
64 : public ::std::set<Reference<XResourceId>, XResourceIdLess>
65 {
66 public:
ResourceContainer(void)67 ResourceContainer (void) {}
68 };
69
70
71
72
73 //----- Service ---------------------------------------------------------------
74
Configuration_createInstance(const Reference<XComponentContext> & rxContext)75 Reference<XInterface> SAL_CALL Configuration_createInstance (
76 const Reference<XComponentContext>& rxContext)
77 {
78 (void)rxContext;
79 return Reference<XInterface>(static_cast<XWeak*>(new Configuration(NULL,false)));
80 }
81
82
83
84
Configuration_getImplementationName(void)85 OUString Configuration_getImplementationName (void)
86 {
87 return OUString(RTL_CONSTASCII_USTRINGPARAM(
88 "com.sun.star.comp.Draw.framework.configuration.Configuration"));
89 }
90
91
92
93
Configuration_getSupportedServiceNames(void)94 Sequence<rtl::OUString> SAL_CALL Configuration_getSupportedServiceNames (void)
95 {
96 static const OUString sServiceName(OUString::createFromAscii(
97 "com.sun.star.drawing.framework.Configuration"));
98 return Sequence<rtl::OUString>(&sServiceName, 1);
99 }
100
101
102
103
104 //===== Configuration =========================================================
105
Configuration(const Reference<XConfigurationControllerBroadcaster> & rxBroadcaster,bool bBroadcastRequestEvents)106 Configuration::Configuration (
107 const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
108 bool bBroadcastRequestEvents)
109 : ConfigurationInterfaceBase(MutexOwner::maMutex),
110 mpResourceContainer(new ResourceContainer()),
111 mxBroadcaster(rxBroadcaster),
112 mbBroadcastRequestEvents(bBroadcastRequestEvents)
113 {
114 }
115
116
117
Configuration(const Reference<XConfigurationControllerBroadcaster> & rxBroadcaster,bool bBroadcastRequestEvents,const ResourceContainer & rResourceContainer)118 Configuration::Configuration (
119 const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
120 bool bBroadcastRequestEvents,
121 const ResourceContainer& rResourceContainer)
122 : ConfigurationInterfaceBase(MutexOwner::maMutex),
123 mpResourceContainer(new ResourceContainer(rResourceContainer)),
124 mxBroadcaster(rxBroadcaster),
125 mbBroadcastRequestEvents(bBroadcastRequestEvents)
126 {
127 }
128
129
130
131
~Configuration(void)132 Configuration::~Configuration (void)
133 {
134 }
135
136
137
138
disposing(void)139 void SAL_CALL Configuration::disposing (void)
140 {
141 ::osl::MutexGuard aGuard (maMutex);
142 mpResourceContainer->clear();
143 mxBroadcaster = NULL;
144 }
145
146
147
148
149 //----- XConfiguration --------------------------------------------------------
150
addResource(const Reference<XResourceId> & rxResourceId)151 void SAL_CALL Configuration::addResource (const Reference<XResourceId>& rxResourceId)
152 {
153 ThrowIfDisposed();
154
155 if ( ! rxResourceId.is() || rxResourceId->getResourceURL().getLength()==0)
156 throw ::com::sun::star::lang::IllegalArgumentException();
157
158 if (mpResourceContainer->find(rxResourceId) == mpResourceContainer->end())
159 {
160 #ifdef VERBOSE
161 OSL_TRACE("Configuration::addResource() %s",
162 OUStringToOString(
163 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
164 #endif
165 mpResourceContainer->insert(rxResourceId);
166 PostEvent(rxResourceId, true);
167 }
168 }
169
170
171
172
removeResource(const Reference<XResourceId> & rxResourceId)173 void SAL_CALL Configuration::removeResource (const Reference<XResourceId>& rxResourceId)
174 {
175 ThrowIfDisposed();
176
177 if ( ! rxResourceId.is() || rxResourceId->getResourceURL().getLength()==0)
178 throw ::com::sun::star::lang::IllegalArgumentException();
179
180 ResourceContainer::iterator iResource (mpResourceContainer->find(rxResourceId));
181 if (iResource != mpResourceContainer->end())
182 {
183 #ifdef VERBOSE
184 OSL_TRACE("Configuration::removeResource() %s",
185 OUStringToOString(
186 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
187 #endif
188 PostEvent(rxResourceId,false);
189 mpResourceContainer->erase(iResource);
190 }
191 }
192
193
194
195
getResources(const Reference<XResourceId> & rxAnchorId,const::rtl::OUString & rsResourceURLPrefix,AnchorBindingMode eMode)196 Sequence<Reference<XResourceId> > SAL_CALL Configuration::getResources (
197 const Reference<XResourceId>& rxAnchorId,
198 const ::rtl::OUString& rsResourceURLPrefix,
199 AnchorBindingMode eMode)
200 {
201 ::osl::MutexGuard aGuard (maMutex);
202 ThrowIfDisposed();
203
204 bool bFilterResources (rsResourceURLPrefix.getLength() > 0);
205
206 // Collect the matching resources in a vector.
207 ::std::vector<Reference<XResourceId> > aResources;
208 ResourceContainer::const_iterator iResource;
209 for (iResource=mpResourceContainer->begin();
210 iResource!=mpResourceContainer->end();
211 ++iResource)
212 {
213 if ( ! (*iResource)->isBoundTo(rxAnchorId,eMode))
214 continue;
215
216
217 if (bFilterResources)
218 {
219 // Apply the given resource prefix as filter.
220
221 // Make sure that the resource is bound directly to the anchor.
222 if (eMode != AnchorBindingMode_DIRECT
223 && ! (*iResource)->isBoundTo(rxAnchorId, AnchorBindingMode_DIRECT))
224 {
225 continue;
226 }
227
228 // Make sure that the resource URL matches the given prefix.
229 if ( ! (*iResource)->getResourceURL().match(rsResourceURLPrefix))
230 {
231 continue;
232 }
233 }
234
235 aResources.push_back(*iResource);
236 }
237
238 // Copy the resources from the vector into a new sequence.
239 Sequence<Reference<XResourceId> > aResult (aResources.size());
240 for (sal_uInt32 nIndex=0; nIndex<aResources.size(); ++nIndex)
241 aResult[nIndex] = aResources[nIndex];
242
243 return aResult;
244 }
245
246
247
248
hasResource(const Reference<XResourceId> & rxResourceId)249 sal_Bool SAL_CALL Configuration::hasResource (const Reference<XResourceId>& rxResourceId)
250 {
251 ::osl::MutexGuard aGuard (maMutex);
252 ThrowIfDisposed();
253
254 return rxResourceId.is()
255 && mpResourceContainer->find(rxResourceId) != mpResourceContainer->end();
256 }
257
258
259
260
261 //----- XCloneable ------------------------------------------------------------
262
createClone(void)263 Reference<util::XCloneable> SAL_CALL Configuration::createClone (void)
264 {
265 ::osl::MutexGuard aGuard (maMutex);
266 ThrowIfDisposed();
267
268 Configuration* pConfiguration = new Configuration(
269 mxBroadcaster,
270 mbBroadcastRequestEvents,
271 *mpResourceContainer);
272
273 return Reference<util::XCloneable>(pConfiguration);
274 }
275
276
277
278
279 //----- XNamed ----------------------------------------------------------------
280
getName(void)281 OUString SAL_CALL Configuration::getName (void)
282 {
283 ::osl::MutexGuard aGuard (maMutex);
284 OUString aString;
285
286 if (rBHelper.bDisposed || rBHelper.bInDispose)
287 aString += OUString::createFromAscii("DISPOSED ");
288 aString += OUString::createFromAscii("Configuration[");
289
290 ResourceContainer::const_iterator iResource;
291 for (iResource=mpResourceContainer->begin();
292 iResource!=mpResourceContainer->end();
293 ++iResource)
294 {
295 if (iResource != mpResourceContainer->begin())
296 aString += OUString::createFromAscii(", ");
297 aString += FrameworkHelper::ResourceIdToString(*iResource);
298 }
299 aString += OUString::createFromAscii("]");
300
301 return aString;
302 }
303
304
305
306
setName(const OUString & rsName)307 void SAL_CALL Configuration::setName (const OUString& rsName)
308 {
309 (void)rsName; // rsName is ignored.
310 }
311
312
313
314
315
316 // ----------------------------------------------------------------------------
317
PostEvent(const Reference<XResourceId> & rxResourceId,const bool bActivation)318 void Configuration::PostEvent (
319 const Reference<XResourceId>& rxResourceId,
320 const bool bActivation)
321 {
322 OSL_ASSERT(rxResourceId.is());
323
324 if (mxBroadcaster.is())
325 {
326 ConfigurationChangeEvent aEvent;
327 aEvent.ResourceId = rxResourceId;
328 if (bActivation)
329 if (mbBroadcastRequestEvents)
330 aEvent.Type = FrameworkHelper::msResourceActivationRequestEvent;
331 else
332 aEvent.Type = FrameworkHelper::msResourceActivationEvent;
333 else
334 if (mbBroadcastRequestEvents)
335 aEvent.Type = FrameworkHelper::msResourceDeactivationRequestEvent;
336 else
337 aEvent.Type = FrameworkHelper::msResourceDeactivationEvent;
338 aEvent.Configuration = this;
339
340 mxBroadcaster->notifyEvent(aEvent);
341 }
342 }
343
344
345
346
ThrowIfDisposed(void) const347 void Configuration::ThrowIfDisposed (void) const
348 {
349 if (rBHelper.bDisposed || rBHelper.bInDispose)
350 {
351 throw lang::DisposedException (
352 OUString(RTL_CONSTASCII_USTRINGPARAM(
353 "Configuration object has already been disposed")),
354 const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
355 }
356 }
357
358
359
360
361 //=============================================================================
362
AreConfigurationsEquivalent(const Reference<XConfiguration> & rxConfiguration1,const Reference<XConfiguration> & rxConfiguration2)363 bool AreConfigurationsEquivalent (
364 const Reference<XConfiguration>& rxConfiguration1,
365 const Reference<XConfiguration>& rxConfiguration2)
366 {
367 if (rxConfiguration1.is() != rxConfiguration2.is())
368 return false;
369 if ( ! rxConfiguration1.is() && ! rxConfiguration2.is())
370 return true;
371
372 // Get the lists of resources from the two given configurations.
373 const Sequence<Reference<XResourceId> > aResources1(
374 rxConfiguration1->getResources(
375 NULL, OUString(), AnchorBindingMode_INDIRECT));
376 const Sequence<Reference<XResourceId> > aResources2(
377 rxConfiguration2->getResources(
378 NULL, OUString(), AnchorBindingMode_INDIRECT));
379
380 // When the number of resources differ then the configurations can not
381 // be equivalent.
382 const sal_Int32 nCount (aResources1.getLength());
383 const sal_Int32 nCount2 (aResources2.getLength());
384 if (nCount != nCount2)
385 return false;
386
387 // Comparison of the two lists of resource ids relies on their
388 // ordering.
389 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
390 {
391 const Reference<XResourceId> xResource1 (aResources1[nIndex]);
392 const Reference<XResourceId> xResource2 (aResources2[nIndex]);
393 if (xResource1.is() && xResource2.is())
394 {
395 if (xResource1->compareTo(xResource2) != 0)
396 return false;
397 }
398 else if (xResource1.is() != xResource2.is())
399 {
400 return false;
401 }
402 }
403
404 return true;
405 }
406
407 } } // end of namespace sd::framework
408