bump product version to 4.1.6.2
[LibreOffice.git] / sd / source / ui / framework / configuration / ConfigurationControllerResourceManager.cxx
blob16a96f2ecca0517dd614cd5edaae9a5141ec1743
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 .
21 #include "ConfigurationControllerResourceManager.hxx"
22 #include "ConfigurationControllerBroadcaster.hxx"
23 #include "ResourceFactoryManager.hxx"
24 #include "framework/FrameworkHelper.hxx"
25 #include <com/sun/star/lang/DisposedException.hpp>
26 #include <tools/diagnose_ex.h>
27 #include <algorithm>
28 #include <boost/bind.hpp>
30 using namespace ::com::sun::star;
31 using namespace ::com::sun::star::uno;
32 using namespace ::com::sun::star::drawing::framework;
34 namespace sd { namespace framework {
36 //===== ConfigurationControllerResourceManager ================================
38 ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
39 const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer,
40 const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster)
41 : maResourceMap(ResourceComparator()),
42 mpResourceFactoryContainer(rpResourceFactoryContainer),
43 mpBroadcaster(rpBroadcaster)
50 ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager (void)
57 ConfigurationControllerResourceManager::ResourceDescriptor
58 ConfigurationControllerResourceManager::GetResource (
59 const Reference<XResourceId>& rxResourceId)
61 ::osl::MutexGuard aGuard (maMutex);
62 ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
63 if (iResource != maResourceMap.end())
64 return iResource->second;
65 else
66 return ResourceDescriptor();
72 void ConfigurationControllerResourceManager::ActivateResources (
73 const ::std::vector<Reference<XResourceId> >& rResources,
74 const Reference<XConfiguration>& rxConfiguration)
76 ::osl::MutexGuard aGuard (maMutex);
77 // Iterate in normal order over the resources that are to be
78 // activated so that resources on which others depend are activated
79 // beforet the depending resources are activated.
80 ::std::for_each(
81 rResources.begin(),
82 rResources.end(),
83 ::boost::bind(&ConfigurationControllerResourceManager::ActivateResource,
84 this, _1, rxConfiguration));
90 void ConfigurationControllerResourceManager::DeactivateResources (
91 const ::std::vector<Reference<XResourceId> >& rResources,
92 const Reference<XConfiguration>& rxConfiguration)
94 ::osl::MutexGuard aGuard (maMutex);
95 // Iterate in reverese order over the resources that are to be
96 // deactivated so that resources on which others depend are deactivated
97 // only when the depending resources have already been deactivated.
98 ::std::for_each(
99 rResources.rbegin(),
100 rResources.rend(),
101 ::boost::bind(&ConfigurationControllerResourceManager::DeactivateResource,
102 this, _1, rxConfiguration));
108 /* In this method we do following steps.
109 1. Get the factory with which the resource will be created.
110 2. Create the resource.
111 3. Add the resource to the URL->Object map of the configuration
112 controller.
113 4. Add the resource id to the current configuration.
114 5. Notify listeners.
116 void ConfigurationControllerResourceManager::ActivateResource (
117 const Reference<XResourceId>& rxResourceId,
118 const Reference<XConfiguration>& rxConfiguration)
120 if ( ! rxResourceId.is())
122 OSL_ASSERT(rxResourceId.is());
123 return;
126 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": activating resource " << OUStringToOString(
127 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
129 // 1. Get the factory.
130 const OUString sResourceURL (rxResourceId->getResourceURL());
131 Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
132 if ( ! xFactory.is())
134 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": no factory found for " <<
135 OUStringToOString(sResourceURL, RTL_TEXTENCODING_UTF8).getStr());
136 return;
141 // 2. Create the resource.
142 Reference<XResource> xResource;
145 xResource = xFactory->createResource(rxResourceId);
147 catch (lang::DisposedException&)
149 // The factory is disposed and can be removed from the list
150 // of registered factories.
151 mpResourceFactoryContainer->RemoveFactoryForReference(xFactory);
153 catch (Exception& e)
155 (void)e;
158 if (xResource.is())
160 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully created");
161 // 3. Add resource to URL->Object map.
162 AddResource(xResource, xFactory);
164 // 4. Add resource id to current configuration.
165 rxConfiguration->addResource(rxResourceId);
167 // 5. Notify the new resource to listeners of the ConfigurationController.
168 mpBroadcaster->NotifyListeners(
169 FrameworkHelper::msResourceActivationEvent,
170 rxResourceId,
171 xResource);
173 else
175 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": resource creation failed");
178 catch (RuntimeException&)
180 DBG_UNHANDLED_EXCEPTION();
187 /* In this method we do following steps.
188 1. Remove the resource from the URL->Object map of the configuration
189 controller.
190 2. Notify listeners that deactivation has started.
191 3. Remove the resource id from the current configuration.
192 4. Release the resource.
193 5. Notify listeners about that deactivation is completed.
195 void ConfigurationControllerResourceManager::DeactivateResource (
196 const Reference<XResourceId>& rxResourceId,
197 const Reference<XConfiguration>& rxConfiguration)
199 if ( ! rxResourceId.is())
200 return;
202 #if OSL_DEBUG_LEVEL >= 1
203 bool bSuccess (false);
204 #endif
207 // 1. Remove resource from URL->Object map.
208 ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
210 if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
212 // 2. Notifiy listeners that the resource is being deactivated.
213 mpBroadcaster->NotifyListeners(
214 FrameworkHelper::msResourceDeactivationEvent,
215 rxResourceId,
216 aDescriptor.mxResource);
218 // 3. Remove resource id from current configuration.
219 rxConfiguration->removeResource(rxResourceId);
221 // 4. Release the resource.
224 aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
226 catch (const lang::DisposedException& rException)
228 if ( ! rException.Context.is()
229 || rException.Context == aDescriptor.mxResourceFactory)
231 // The factory is disposed and can be removed from the
232 // list of registered factories.
233 mpResourceFactoryContainer->RemoveFactoryForReference(
234 aDescriptor.mxResourceFactory);
238 #if OSL_DEBUG_LEVEL >= 1
239 bSuccess = true;
240 #endif
243 catch (RuntimeException&)
245 DBG_UNHANDLED_EXCEPTION();
248 // 5. Notifiy listeners that the resource is being deactivated.
249 mpBroadcaster->NotifyListeners(
250 FrameworkHelper::msResourceDeactivationEndEvent,
251 rxResourceId,
252 NULL);
254 #if OSL_DEBUG_LEVEL >= 1
255 if (bSuccess)
256 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully deactivated " << OUStringToOString(
257 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
258 else
259 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": activating resource " << OUStringToOString(
260 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()
261 << " failed");
262 #endif
268 void ConfigurationControllerResourceManager::AddResource (
269 const Reference<XResource>& rxResource,
270 const Reference<XResourceFactory>& rxFactory)
272 if ( ! rxResource.is())
274 OSL_ASSERT(rxResource.is());
275 return;
278 // Add the resource to the resource container.
279 ResourceDescriptor aDescriptor;
280 aDescriptor.mxResource = rxResource;
281 aDescriptor.mxResourceFactory = rxFactory;
282 maResourceMap[rxResource->getResourceId()] = aDescriptor;
284 #if OSL_DEBUG_LEVEL >= 2
285 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationControllerResourceManager::AddResource(): added " <<
286 OUStringToOString(
287 FrameworkHelper::ResourceIdToString(rxResource->getResourceId()),
288 RTL_TEXTENCODING_UTF8).getStr() << " -> " << rxResource.get());
289 #endif
295 ConfigurationControllerResourceManager::ResourceDescriptor
296 ConfigurationControllerResourceManager::RemoveResource (
297 const Reference<XResourceId>& rxResourceId)
299 ResourceDescriptor aDescriptor;
301 ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
302 if (iResource != maResourceMap.end())
304 #if OSL_DEBUG_LEVEL >= 2
305 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationControllerResourceManager::RemoveResource(): removing " <<
306 OUStringToOString(
307 FrameworkHelper::ResourceIdToString(rxResourceId),
308 RTL_TEXTENCODING_UTF8).getStr() <<
309 " -> " << &iResource);
310 #endif
312 aDescriptor = iResource->second;
313 maResourceMap.erase(rxResourceId);
316 return aDescriptor;
322 //===== ConfigurationControllerResourceManager::ResourceComparator ============
324 bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
325 const Reference<XResourceId>& rxId1,
326 const Reference<XResourceId>& rxId2) const
328 if (rxId1.is() && rxId2.is())
329 return rxId1->compareTo(rxId2)<0;
330 else if (rxId1.is())
331 return true;
332 else
333 return false;
339 } } // end of namespace sd::framework
341 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */