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