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