Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "framework/Configuration.hxx"
31 : :
32 : : #include "framework/FrameworkHelper.hxx"
33 : : #include <comphelper/stl_types.hxx>
34 : :
35 : :
36 : :
37 : : using namespace ::com::sun::star;
38 : : using namespace ::com::sun::star::uno;
39 : : using namespace ::com::sun::star::drawing::framework;
40 : : using ::sd::framework::FrameworkHelper;
41 : : using ::rtl::OUString;
42 : :
43 : : namespace {
44 : : /** Use the XResourceId::compareTo() method to implement a compare operator
45 : : for STL containers.
46 : : */
47 : : class XResourceIdLess
48 : : : public ::std::binary_function <Reference<XResourceId>, Reference<XResourceId>, bool>
49 : : {
50 : : public:
51 : 12535 : bool operator () (const Reference<XResourceId>& rId1, const Reference<XResourceId>& rId2) const
52 : : {
53 : 12535 : return rId1->compareTo(rId2) == -1;
54 : : }
55 : : };
56 : :
57 : : } // end of anonymous namespace
58 : :
59 : :
60 : :
61 : :
62 : : namespace sd { namespace framework {
63 : :
64 : :
65 : 15246 : class Configuration::ResourceContainer
66 : : : public ::std::set<Reference<XResourceId>, XResourceIdLess>
67 : : {
68 : : public:
69 : 390 : ResourceContainer (void) {}
70 : : };
71 : :
72 : :
73 : :
74 : :
75 : : //----- Service ---------------------------------------------------------------
76 : :
77 : 0 : Reference<XInterface> SAL_CALL Configuration_createInstance (
78 : : const Reference<XComponentContext>& rxContext)
79 : : {
80 : : (void)rxContext;
81 [ # # ][ # # ]: 0 : return Reference<XInterface>(static_cast<XWeak*>(new Configuration(NULL,false)));
82 : : }
83 : :
84 : :
85 : :
86 : :
87 : 22 : OUString Configuration_getImplementationName (void) throw(RuntimeException)
88 : : {
89 : 22 : return OUString("com.sun.star.comp.Draw.framework.configuration.Configuration");
90 : : }
91 : :
92 : :
93 : :
94 : :
95 : 0 : Sequence<rtl::OUString> SAL_CALL Configuration_getSupportedServiceNames (void)
96 : : throw (RuntimeException)
97 : : {
98 [ # # ][ # # ]: 0 : static const OUString sServiceName("com.sun.star.drawing.framework.Configuration");
99 : 0 : return Sequence<rtl::OUString>(&sServiceName, 1);
100 : : }
101 : :
102 : :
103 : :
104 : :
105 : : //===== Configuration =========================================================
106 : :
107 : 390 : Configuration::Configuration (
108 : : const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
109 : : bool bBroadcastRequestEvents)
110 : : : ConfigurationInterfaceBase(MutexOwner::maMutex),
111 : 0 : mpResourceContainer(new ResourceContainer()),
112 : : mxBroadcaster(rxBroadcaster),
113 [ + - ][ + - ]: 390 : mbBroadcastRequestEvents(bBroadcastRequestEvents)
114 : : {
115 : 390 : }
116 : :
117 : :
118 : :
119 : 7428 : Configuration::Configuration (
120 : : const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
121 : : bool bBroadcastRequestEvents,
122 : : const ResourceContainer& rResourceContainer)
123 : : : ConfigurationInterfaceBase(MutexOwner::maMutex),
124 [ + - ]: 7428 : mpResourceContainer(new ResourceContainer(rResourceContainer)),
125 : : mxBroadcaster(rxBroadcaster),
126 [ + - ]: 14856 : mbBroadcastRequestEvents(bBroadcastRequestEvents)
127 : : {
128 : 7428 : }
129 : :
130 : :
131 : :
132 : :
133 [ + - ][ + - ]: 7818 : Configuration::~Configuration (void)
134 : : {
135 [ - + ]: 15636 : }
136 : :
137 : :
138 : :
139 : :
140 : 7818 : void SAL_CALL Configuration::disposing (void)
141 : : {
142 [ + - ]: 7818 : ::osl::MutexGuard aGuard (maMutex);
143 : 7818 : mpResourceContainer->clear();
144 [ + - ][ + - ]: 7818 : mxBroadcaster = NULL;
145 : 7818 : }
146 : :
147 : :
148 : :
149 : :
150 : : //----- XConfiguration --------------------------------------------------------
151 : :
152 : 1591 : void SAL_CALL Configuration::addResource (const Reference<XResourceId>& rxResourceId)
153 : : throw (RuntimeException)
154 : : {
155 : 1591 : ThrowIfDisposed();
156 : :
157 [ + - ][ + - ]: 1591 : if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
[ - + ][ + - ]
[ - + # # ]
[ + - ]
158 [ # # ]: 0 : throw ::com::sun::star::lang::IllegalArgumentException();
159 : :
160 [ + - ][ + + ]: 1591 : if (mpResourceContainer->find(rxResourceId) == mpResourceContainer->end())
161 : : {
162 : : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": Configuration::addResource() " <<
163 : : OUStringToOString(
164 : : FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
165 : 1396 : mpResourceContainer->insert(rxResourceId);
166 : 1396 : PostEvent(rxResourceId, true);
167 : : }
168 : 1591 : }
169 : :
170 : :
171 : :
172 : :
173 : 1766 : void SAL_CALL Configuration::removeResource (const Reference<XResourceId>& rxResourceId)
174 : : throw (RuntimeException)
175 : : {
176 [ + - ]: 1766 : ThrowIfDisposed();
177 : :
178 [ + - ][ + - ]: 1766 : if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
[ + - ][ - + ]
[ + - ]
[ - + # # ]
179 [ # # ]: 0 : throw ::com::sun::star::lang::IllegalArgumentException();
180 : :
181 [ + - ]: 1766 : ResourceContainer::iterator iResource (mpResourceContainer->find(rxResourceId));
182 [ + + ]: 1766 : if (iResource != mpResourceContainer->end())
183 : : {
184 : : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": Configuration::removeResource() " <<
185 : : OUStringToOString(
186 : : FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
187 [ + - ]: 1396 : PostEvent(rxResourceId,false);
188 [ + - ]: 1396 : mpResourceContainer->erase(iResource);
189 : : }
190 : 1766 : }
191 : :
192 : :
193 : :
194 : :
195 : 15650 : Sequence<Reference<XResourceId> > SAL_CALL Configuration::getResources (
196 : : const Reference<XResourceId>& rxAnchorId,
197 : : const ::rtl::OUString& rsResourceURLPrefix,
198 : : AnchorBindingMode eMode)
199 : : throw (::com::sun::star::uno::RuntimeException)
200 : : {
201 [ + - ]: 15650 : ::osl::MutexGuard aGuard (maMutex);
202 [ + - ]: 15650 : ThrowIfDisposed();
203 : :
204 : 15650 : bool bFilterResources (!rsResourceURLPrefix.isEmpty());
205 : :
206 : : // Collect the matching resources in a vector.
207 [ + - ]: 15650 : ::std::vector<Reference<XResourceId> > aResources;
208 : 15650 : ResourceContainer::const_iterator iResource;
209 [ + + ]: 167320 : for (iResource=mpResourceContainer->begin();
210 : 83660 : iResource!=mpResourceContainer->end();
211 : : ++iResource)
212 : : {
213 [ + - ][ + - ]: 68010 : if ( ! (*iResource)->isBoundTo(rxAnchorId,eMode))
[ + + ]
214 : 47566 : continue;
215 : :
216 : :
217 [ + + ]: 20444 : 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 [ - + ][ # # ]: 9536 : if (eMode != AnchorBindingMode_DIRECT
[ - + ]
223 [ # # ][ # # ]: 0 : && ! (*iResource)->isBoundTo(rxAnchorId, AnchorBindingMode_DIRECT))
224 : : {
225 : 0 : continue;
226 : : }
227 : :
228 : : // Make sure that the resource URL matches the given prefix.
229 [ + - ][ + - ]: 9536 : if ( ! (*iResource)->getResourceURL().match(rsResourceURLPrefix))
[ + + ]
230 : : {
231 : 1791 : continue;
232 : : }
233 : : }
234 : :
235 [ + - ]: 18653 : aResources.push_back(*iResource);
236 : : }
237 : :
238 : : // Copy the resources from the vector into a new sequence.
239 [ + - ]: 15650 : Sequence<Reference<XResourceId> > aResult (aResources.size());
240 [ + + ]: 34303 : for (sal_uInt32 nIndex=0; nIndex<aResources.size(); ++nIndex)
241 [ + - ][ + - ]: 18653 : aResult[nIndex] = aResources[nIndex];
242 : :
243 [ + - ]: 15650 : return aResult;
244 : : }
245 : :
246 : :
247 : :
248 : :
249 : 0 : sal_Bool SAL_CALL Configuration::hasResource (const Reference<XResourceId>& rxResourceId)
250 : : throw (RuntimeException)
251 : : {
252 [ # # ]: 0 : ::osl::MutexGuard aGuard (maMutex);
253 [ # # ]: 0 : ThrowIfDisposed();
254 : :
255 : 0 : return rxResourceId.is()
256 [ # # ][ # # ]: 0 : && mpResourceContainer->find(rxResourceId) != mpResourceContainer->end();
[ # # ][ # # ]
[ # # ]
[ # # # # ]
[ # # ]
257 : : }
258 : :
259 : :
260 : :
261 : :
262 : : //----- XCloneable ------------------------------------------------------------
263 : :
264 : 7428 : Reference<util::XCloneable> SAL_CALL Configuration::createClone (void)
265 : : throw (RuntimeException)
266 : : {
267 [ + - ]: 7428 : ::osl::MutexGuard aGuard (maMutex);
268 [ + - ]: 7428 : ThrowIfDisposed();
269 : :
270 : : Configuration* pConfiguration = new Configuration(
271 : : mxBroadcaster,
272 : : mbBroadcastRequestEvents,
273 [ + - ]: 7428 : *mpResourceContainer);
274 : :
275 [ + - ][ + - ]: 7428 : return Reference<util::XCloneable>(pConfiguration);
[ + - ]
276 : : }
277 : :
278 : :
279 : :
280 : :
281 : : //----- XNamed ----------------------------------------------------------------
282 : :
283 : 0 : OUString SAL_CALL Configuration::getName (void)
284 : : throw (RuntimeException)
285 : : {
286 [ # # ]: 0 : ::osl::MutexGuard aGuard (maMutex);
287 : 0 : OUString aString;
288 : :
289 [ # # ][ # # ]: 0 : if (rBHelper.bDisposed || rBHelper.bInDispose)
290 : 0 : aString += "DISPOSED ";
291 : 0 : aString += "Configuration[";
292 : :
293 : 0 : ResourceContainer::const_iterator iResource;
294 [ # # ]: 0 : for (iResource=mpResourceContainer->begin();
295 : 0 : iResource!=mpResourceContainer->end();
296 : : ++iResource)
297 : : {
298 [ # # ]: 0 : if (iResource != mpResourceContainer->begin())
299 : 0 : aString += ", ";
300 [ # # ]: 0 : aString += FrameworkHelper::ResourceIdToString(*iResource);
301 : : }
302 : 0 : aString += "]";
303 : :
304 [ # # ]: 0 : return aString;
305 : : }
306 : :
307 : :
308 : :
309 : :
310 : 0 : void SAL_CALL Configuration::setName (const OUString& rsName)
311 : : throw (RuntimeException)
312 : : {
313 : : (void)rsName; // rsName is ignored.
314 : 0 : }
315 : :
316 : :
317 : :
318 : :
319 : :
320 : : // ----------------------------------------------------------------------------
321 : :
322 : 2792 : void Configuration::PostEvent (
323 : : const Reference<XResourceId>& rxResourceId,
324 : : const bool bActivation)
325 : : {
326 : : OSL_ASSERT(rxResourceId.is());
327 : :
328 [ + + ]: 2792 : if (mxBroadcaster.is())
329 : : {
330 [ + - ]: 1240 : ConfigurationChangeEvent aEvent;
331 [ + - ]: 1240 : aEvent.ResourceId = rxResourceId;
332 [ + + ]: 1240 : if (bActivation)
333 [ + - ]: 620 : if (mbBroadcastRequestEvents)
334 : 620 : aEvent.Type = FrameworkHelper::msResourceActivationRequestEvent;
335 : : else
336 : 0 : aEvent.Type = FrameworkHelper::msResourceActivationEvent;
337 : : else
338 [ + - ]: 620 : if (mbBroadcastRequestEvents)
339 : 620 : aEvent.Type = FrameworkHelper::msResourceDeactivationRequestEvent;
340 : : else
341 : 0 : aEvent.Type = FrameworkHelper::msResourceDeactivationEvent;
342 [ + - ]: 1240 : aEvent.Configuration = this;
343 : :
344 [ + - ][ + - ]: 1240 : mxBroadcaster->notifyEvent(aEvent);
[ + - ]
345 : : }
346 : 2792 : }
347 : :
348 : :
349 : :
350 : :
351 : 26435 : void Configuration::ThrowIfDisposed (void) const
352 : : throw (::com::sun::star::lang::DisposedException)
353 : : {
354 [ + - ][ - + ]: 26435 : if (rBHelper.bDisposed || rBHelper.bInDispose)
355 : : {
356 : : throw lang::DisposedException ("Configuration object has already been disposed",
357 [ # # ][ # # ]: 0 : const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
358 : : }
359 : 26435 : }
360 : :
361 : :
362 : :
363 : :
364 : : //=============================================================================
365 : :
366 : 542 : bool AreConfigurationsEquivalent (
367 : : const Reference<XConfiguration>& rxConfiguration1,
368 : : const Reference<XConfiguration>& rxConfiguration2)
369 : : {
370 [ - + ]: 542 : if (rxConfiguration1.is() != rxConfiguration2.is())
371 : 0 : return false;
372 [ - + ][ # # ]: 542 : if ( ! rxConfiguration1.is() && ! rxConfiguration2.is())
[ - + ]
373 : 0 : return true;
374 : :
375 : : // Get the lists of resources from the two given configurations.
376 : : const Sequence<Reference<XResourceId> > aResources1(
377 [ + - ]: 542 : rxConfiguration1->getResources(
378 [ + - ][ + - ]: 542 : NULL, OUString(), AnchorBindingMode_INDIRECT));
379 : : const Sequence<Reference<XResourceId> > aResources2(
380 [ + - ]: 542 : rxConfiguration2->getResources(
381 [ + - ][ + - ]: 542 : NULL, OUString(), AnchorBindingMode_INDIRECT));
382 : :
383 : : // When the number of resources differ then the configurations can not
384 : : // be equivalent.
385 : 542 : const sal_Int32 nCount (aResources1.getLength());
386 : 542 : const sal_Int32 nCount2 (aResources2.getLength());
387 [ + + ]: 542 : if (nCount != nCount2)
388 : 161 : return false;
389 : :
390 : : // Comparison of the two lists of resource ids relies on their
391 : : // ordering.
392 [ + + ]: 1557 : for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
393 : : {
394 : 1176 : const Reference<XResourceId> xResource1 (aResources1[nIndex]);
395 : 1176 : const Reference<XResourceId> xResource2 (aResources2[nIndex]);
396 [ + - ][ + - ]: 1176 : if (xResource1.is() && xResource2.is())
[ + - ]
397 : : {
398 [ + - ][ + - ]: 1176 : if (xResource1->compareTo(xResource2) != 0)
[ - + ]
399 : 0 : return false;
400 : : }
401 [ # # ]: 0 : else if (xResource1.is() != xResource2.is())
402 : : {
403 : 1176 : return false;
404 : : }
405 [ - + ][ + - ]: 1176 : }
406 : :
407 [ + - ][ + - ]: 542 : return true;
408 : : }
409 : :
410 : : } } // end of namespace sd::framework
411 : :
412 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|