bump product version to 5.0.4.1
[LibreOffice.git] / sd / source / ui / framework / configuration / ConfigurationControllerResourceManager.cxx
blob5e0e92864b15a2bd5a1aea26b188ebc630bbe64a
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 "ConfigurationControllerResourceManager.hxx"
21 #include "ConfigurationControllerBroadcaster.hxx"
22 #include "ResourceFactoryManager.hxx"
23 #include "framework/FrameworkHelper.hxx"
24 #include <com/sun/star/lang/DisposedException.hpp>
25 #include <tools/diagnose_ex.h>
26 #include <algorithm>
27 #include <boost/bind.hpp>
29 using namespace ::com::sun::star;
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::drawing::framework;
33 namespace sd { namespace framework {
35 //===== ConfigurationControllerResourceManager ================================
37 ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
38 const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer,
39 const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster)
40 : maResourceMap(ResourceComparator()),
41 mpResourceFactoryContainer(rpResourceFactoryContainer),
42 mpBroadcaster(rpBroadcaster)
46 ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager()
50 ConfigurationControllerResourceManager::ResourceDescriptor
51 ConfigurationControllerResourceManager::GetResource (
52 const Reference<XResourceId>& rxResourceId)
54 ::osl::MutexGuard aGuard (maMutex);
55 ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
56 if (iResource != maResourceMap.end())
57 return iResource->second;
58 else
59 return ResourceDescriptor();
62 void ConfigurationControllerResourceManager::ActivateResources (
63 const ::std::vector<Reference<XResourceId> >& rResources,
64 const Reference<XConfiguration>& rxConfiguration)
66 ::osl::MutexGuard aGuard (maMutex);
67 // Iterate in normal order over the resources that are to be
68 // activated so that resources on which others depend are activated
69 // before the depending resources are activated.
70 ::std::for_each(
71 rResources.begin(),
72 rResources.end(),
73 ::boost::bind(&ConfigurationControllerResourceManager::ActivateResource,
74 this, _1, rxConfiguration));
77 void ConfigurationControllerResourceManager::DeactivateResources (
78 const ::std::vector<Reference<XResourceId> >& rResources,
79 const Reference<XConfiguration>& rxConfiguration)
81 ::osl::MutexGuard aGuard (maMutex);
82 // Iterate in reverse order over the resources that are to be
83 // deactivated so that resources on which others depend are deactivated
84 // only when the depending resources have already been deactivated.
85 ::std::for_each(
86 rResources.rbegin(),
87 rResources.rend(),
88 ::boost::bind(&ConfigurationControllerResourceManager::DeactivateResource,
89 this, _1, rxConfiguration));
92 /* In this method we do following steps.
93 1. Get the factory with which the resource will be created.
94 2. Create the resource.
95 3. Add the resource to the URL->Object map of the configuration
96 controller.
97 4. Add the resource id to the current configuration.
98 5. Notify listeners.
100 void ConfigurationControllerResourceManager::ActivateResource (
101 const Reference<XResourceId>& rxResourceId,
102 const Reference<XConfiguration>& rxConfiguration)
104 if ( ! rxResourceId.is())
106 OSL_ASSERT(rxResourceId.is());
107 return;
110 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": activating resource " << OUStringToOString(
111 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
113 // 1. Get the factory.
114 const OUString sResourceURL (rxResourceId->getResourceURL());
115 Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
116 if ( ! xFactory.is())
118 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": no factory found for " <<
119 OUStringToOString(sResourceURL, RTL_TEXTENCODING_UTF8).getStr());
120 return;
125 // 2. Create the resource.
126 Reference<XResource> xResource;
129 xResource = xFactory->createResource(rxResourceId);
131 catch (lang::DisposedException&)
133 // The factory is disposed and can be removed from the list
134 // of registered factories.
135 mpResourceFactoryContainer->RemoveFactoryForReference(xFactory);
137 catch (Exception& e)
139 (void)e;
142 if (xResource.is())
144 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully created");
145 // 3. Add resource to URL->Object map.
146 AddResource(xResource, xFactory);
148 // 4. Add resource id to current configuration.
149 rxConfiguration->addResource(rxResourceId);
151 // 5. Notify the new resource to listeners of the ConfigurationController.
152 mpBroadcaster->NotifyListeners(
153 FrameworkHelper::msResourceActivationEvent,
154 rxResourceId,
155 xResource);
157 else
159 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": resource creation failed");
162 catch (RuntimeException&)
164 DBG_UNHANDLED_EXCEPTION();
168 /* In this method we do following steps.
169 1. Remove the resource from the URL->Object map of the configuration
170 controller.
171 2. Notify listeners that deactivation has started.
172 3. Remove the resource id from the current configuration.
173 4. Release the resource.
174 5. Notify listeners about that deactivation is completed.
176 void ConfigurationControllerResourceManager::DeactivateResource (
177 const Reference<XResourceId>& rxResourceId,
178 const Reference<XConfiguration>& rxConfiguration)
180 if ( ! rxResourceId.is())
181 return;
183 #if OSL_DEBUG_LEVEL >= 1
184 bool bSuccess (false);
185 #endif
188 // 1. Remove resource from URL->Object map.
189 ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
191 if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
193 // 2. Notifiy listeners that the resource is being deactivated.
194 mpBroadcaster->NotifyListeners(
195 FrameworkHelper::msResourceDeactivationEvent,
196 rxResourceId,
197 aDescriptor.mxResource);
199 // 3. Remove resource id from current configuration.
200 rxConfiguration->removeResource(rxResourceId);
202 // 4. Release the resource.
205 aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
207 catch (const lang::DisposedException& rException)
209 if ( ! rException.Context.is()
210 || rException.Context == aDescriptor.mxResourceFactory)
212 // The factory is disposed and can be removed from the
213 // list of registered factories.
214 mpResourceFactoryContainer->RemoveFactoryForReference(
215 aDescriptor.mxResourceFactory);
219 #if OSL_DEBUG_LEVEL >= 1
220 bSuccess = true;
221 #endif
224 catch (RuntimeException&)
226 DBG_UNHANDLED_EXCEPTION();
229 // 5. Notifiy listeners that the resource is being deactivated.
230 mpBroadcaster->NotifyListeners(
231 FrameworkHelper::msResourceDeactivationEndEvent,
232 rxResourceId,
233 NULL);
235 #if OSL_DEBUG_LEVEL >= 1
236 if (bSuccess)
237 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": successfully deactivated " << OUStringToOString(
238 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
239 else
240 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": activating resource " << OUStringToOString(
241 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr()
242 << " failed");
243 #endif
246 void ConfigurationControllerResourceManager::AddResource (
247 const Reference<XResource>& rxResource,
248 const Reference<XResourceFactory>& rxFactory)
250 if ( ! rxResource.is())
252 OSL_ASSERT(rxResource.is());
253 return;
256 // Add the resource to the resource container.
257 ResourceDescriptor aDescriptor;
258 aDescriptor.mxResource = rxResource;
259 aDescriptor.mxResourceFactory = rxFactory;
260 maResourceMap[rxResource->getResourceId()] = aDescriptor;
262 #if OSL_DEBUG_LEVEL >= 2
263 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationControllerResourceManager::AddResource(): added " <<
264 OUStringToOString(
265 FrameworkHelper::ResourceIdToString(rxResource->getResourceId()),
266 RTL_TEXTENCODING_UTF8).getStr() << " -> " << rxResource.get());
267 #endif
270 ConfigurationControllerResourceManager::ResourceDescriptor
271 ConfigurationControllerResourceManager::RemoveResource (
272 const Reference<XResourceId>& rxResourceId)
274 ResourceDescriptor aDescriptor;
276 ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
277 if (iResource != maResourceMap.end())
279 #if OSL_DEBUG_LEVEL >= 2
280 SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ConfigurationControllerResourceManager::RemoveResource(): removing " <<
281 OUStringToOString(
282 FrameworkHelper::ResourceIdToString(rxResourceId),
283 RTL_TEXTENCODING_UTF8).getStr() <<
284 " -> " << &iResource);
285 #endif
287 aDescriptor = iResource->second;
288 maResourceMap.erase(rxResourceId);
291 return aDescriptor;
294 //===== ConfigurationControllerResourceManager::ResourceComparator ============
296 bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
297 const Reference<XResourceId>& rxId1,
298 const Reference<XResourceId>& rxId2) const
300 if (rxId1.is() && rxId2.is())
301 return rxId1->compareTo(rxId2)<0;
302 else if (rxId1.is())
303 return true;
304 else
305 return false;
308 } } // end of namespace sd::framework
310 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */