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