bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / framework / configuration / Configuration.cxx
blobf64bcd5f9346c6dff33039316a3bcf61c6190556
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "framework/Configuration.hxx"
22 #include "framework/FrameworkHelper.hxx"
24 #include <facreg.hxx>
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;
31 namespace {
32 /** Use the XResourceId::compareTo() method to implement a compare operator
33 for STL containers.
35 class XResourceIdLess
36 : public ::std::binary_function <Reference<XResourceId>, Reference<XResourceId>, bool>
38 public:
39 bool operator () (const Reference<XResourceId>& rId1, const Reference<XResourceId>& rId2) const
41 return rId1->compareTo(rId2) == -1;
45 } // end of anonymous namespace
47 namespace sd { namespace framework {
49 class Configuration::ResourceContainer
50 : public ::std::set<Reference<XResourceId>, XResourceIdLess>
52 public:
53 ResourceContainer() {}
56 //===== Configuration =========================================================
58 Configuration::Configuration (
59 const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
60 bool bBroadcastRequestEvents)
61 : ConfigurationInterfaceBase(MutexOwner::maMutex),
62 mpResourceContainer(new ResourceContainer()),
63 mxBroadcaster(rxBroadcaster),
64 mbBroadcastRequestEvents(bBroadcastRequestEvents)
68 Configuration::Configuration (
69 const Reference<XConfigurationControllerBroadcaster>& rxBroadcaster,
70 bool bBroadcastRequestEvents,
71 const ResourceContainer& rResourceContainer)
72 : ConfigurationInterfaceBase(MutexOwner::maMutex),
73 mpResourceContainer(new ResourceContainer(rResourceContainer)),
74 mxBroadcaster(rxBroadcaster),
75 mbBroadcastRequestEvents(bBroadcastRequestEvents)
79 Configuration::~Configuration()
83 void SAL_CALL Configuration::disposing()
85 ::osl::MutexGuard aGuard (maMutex);
86 mpResourceContainer->clear();
87 mxBroadcaster = NULL;
90 //----- XConfiguration --------------------------------------------------------
92 void SAL_CALL Configuration::addResource (const Reference<XResourceId>& rxResourceId)
93 throw (RuntimeException, std::exception)
95 ThrowIfDisposed();
97 if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
98 throw ::com::sun::star::lang::IllegalArgumentException();
100 if (mpResourceContainer->find(rxResourceId) == mpResourceContainer->end())
102 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": Configuration::addResource() " <<
103 OUStringToOString(
104 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
105 mpResourceContainer->insert(rxResourceId);
106 PostEvent(rxResourceId, true);
110 void SAL_CALL Configuration::removeResource (const Reference<XResourceId>& rxResourceId)
111 throw (RuntimeException, std::exception)
113 ThrowIfDisposed();
115 if ( ! rxResourceId.is() || rxResourceId->getResourceURL().isEmpty())
116 throw ::com::sun::star::lang::IllegalArgumentException();
118 ResourceContainer::iterator iResource (mpResourceContainer->find(rxResourceId));
119 if (iResource != mpResourceContainer->end())
121 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": Configuration::removeResource() " <<
122 OUStringToOString(
123 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
124 PostEvent(rxResourceId,false);
125 mpResourceContainer->erase(iResource);
129 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)
135 ::osl::MutexGuard aGuard (maMutex);
136 ThrowIfDisposed();
138 bool bFilterResources (!rsResourceURLPrefix.isEmpty());
140 // Collect the matching resources in a vector.
141 ::std::vector<Reference<XResourceId> > aResources;
142 ResourceContainer::const_iterator iResource;
143 for (iResource=mpResourceContainer->begin();
144 iResource!=mpResourceContainer->end();
145 ++iResource)
147 if ( ! (*iResource)->isBoundTo(rxAnchorId,eMode))
148 continue;
150 if (bFilterResources)
152 // Apply the given resource prefix as filter.
154 // Make sure that the resource is bound directly to the anchor.
155 if (eMode != AnchorBindingMode_DIRECT
156 && ! (*iResource)->isBoundTo(rxAnchorId, AnchorBindingMode_DIRECT))
158 continue;
161 // Make sure that the resource URL matches the given prefix.
162 if ( ! (*iResource)->getResourceURL().match(rsResourceURLPrefix))
164 continue;
168 aResources.push_back(*iResource);
171 // Copy the resources from the vector into a new sequence.
172 Sequence<Reference<XResourceId> > aResult (aResources.size());
173 for (sal_uInt32 nIndex=0; nIndex<aResources.size(); ++nIndex)
174 aResult[nIndex] = aResources[nIndex];
176 return aResult;
179 sal_Bool SAL_CALL Configuration::hasResource (const Reference<XResourceId>& rxResourceId)
180 throw (RuntimeException, std::exception)
182 ::osl::MutexGuard aGuard (maMutex);
183 ThrowIfDisposed();
185 return rxResourceId.is()
186 && mpResourceContainer->find(rxResourceId) != mpResourceContainer->end();
189 //----- XCloneable ------------------------------------------------------------
191 Reference<util::XCloneable> SAL_CALL Configuration::createClone()
192 throw (RuntimeException, std::exception)
194 ::osl::MutexGuard aGuard (maMutex);
195 ThrowIfDisposed();
197 Configuration* pConfiguration = new Configuration(
198 mxBroadcaster,
199 mbBroadcastRequestEvents,
200 *mpResourceContainer);
202 return Reference<util::XCloneable>(pConfiguration);
205 //----- XNamed ----------------------------------------------------------------
207 OUString SAL_CALL Configuration::getName()
208 throw (RuntimeException, std::exception)
210 ::osl::MutexGuard aGuard (maMutex);
211 OUString aString;
213 if (rBHelper.bDisposed || rBHelper.bInDispose)
214 aString += "DISPOSED ";
215 aString += "Configuration[";
217 ResourceContainer::const_iterator iResource;
218 for (iResource=mpResourceContainer->begin();
219 iResource!=mpResourceContainer->end();
220 ++iResource)
222 if (iResource != mpResourceContainer->begin())
223 aString += ", ";
224 aString += FrameworkHelper::ResourceIdToString(*iResource);
226 aString += "]";
228 return aString;
231 void SAL_CALL Configuration::setName (const OUString& rsName)
232 throw (RuntimeException, std::exception)
234 (void)rsName; // rsName is ignored.
237 OUString Configuration::getImplementationName()
238 throw (css::uno::RuntimeException, std::exception)
240 return OUString(
241 "com.sun.star.comp.Draw.framework.configuration.Configuration");
244 sal_Bool Configuration::supportsService(OUString const & ServiceName)
245 throw (css::uno::RuntimeException, std::exception)
247 return cppu::supportsService(this, ServiceName);
250 css::uno::Sequence<OUString> Configuration::getSupportedServiceNames()
251 throw (css::uno::RuntimeException, std::exception)
253 return css::uno::Sequence<OUString>{
254 "com.sun.star.drawing.framework.Configuration"};
257 void Configuration::PostEvent (
258 const Reference<XResourceId>& rxResourceId,
259 const bool bActivation)
261 OSL_ASSERT(rxResourceId.is());
263 if (mxBroadcaster.is())
265 ConfigurationChangeEvent aEvent;
266 aEvent.ResourceId = rxResourceId;
267 if (bActivation)
268 if (mbBroadcastRequestEvents)
269 aEvent.Type = FrameworkHelper::msResourceActivationRequestEvent;
270 else
271 aEvent.Type = FrameworkHelper::msResourceActivationEvent;
272 else
273 if (mbBroadcastRequestEvents)
274 aEvent.Type = FrameworkHelper::msResourceDeactivationRequestEvent;
275 else
276 aEvent.Type = FrameworkHelper::msResourceDeactivationEvent;
277 aEvent.Configuration = this;
279 mxBroadcaster->notifyEvent(aEvent);
283 void Configuration::ThrowIfDisposed() const
284 throw (::com::sun::star::lang::DisposedException)
286 if (rBHelper.bDisposed || rBHelper.bInDispose)
288 throw lang::DisposedException ("Configuration object has already been disposed",
289 const_cast<uno::XWeak*>(static_cast<const uno::XWeak*>(this)));
293 bool AreConfigurationsEquivalent (
294 const Reference<XConfiguration>& rxConfiguration1,
295 const Reference<XConfiguration>& rxConfiguration2)
297 if (rxConfiguration1.is() != rxConfiguration2.is())
298 return false;
299 if ( ! rxConfiguration1.is() && ! rxConfiguration2.is())
300 return true;
302 // Get the lists of resources from the two given configurations.
303 const Sequence<Reference<XResourceId> > aResources1(
304 rxConfiguration1->getResources(
305 NULL, OUString(), AnchorBindingMode_INDIRECT));
306 const Sequence<Reference<XResourceId> > aResources2(
307 rxConfiguration2->getResources(
308 NULL, OUString(), AnchorBindingMode_INDIRECT));
310 // When the number of resources differ then the configurations can not
311 // be equivalent.
312 const sal_Int32 nCount (aResources1.getLength());
313 const sal_Int32 nCount2 (aResources2.getLength());
314 if (nCount != nCount2)
315 return false;
317 // Comparison of the two lists of resource ids relies on their
318 // ordering.
319 for (sal_Int32 nIndex=0; nIndex<nCount; ++nIndex)
321 const Reference<XResourceId> xResource1 (aResources1[nIndex]);
322 const Reference<XResourceId> xResource2 (aResources2[nIndex]);
323 if (xResource1.is() && xResource2.is())
325 if (xResource1->compareTo(xResource2) != 0)
326 return false;
328 else if (xResource1.is() != xResource2.is())
330 return false;
334 return true;
337 } } // end of namespace sd::framework
340 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface* SAL_CALL
341 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 &)
345 return cppu::acquire(new sd::framework::Configuration(NULL, false));
350 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */