Version 7.6.3.2-android, tag libreoffice-7.6.3.2-android
[LibreOffice.git] / sd / source / ui / framework / configuration / ConfigurationControllerResourceManager.cxx
blobcc04b9099a759d5a51b473fa544d94c21f09b7b3
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 <com/sun/star/drawing/framework/XConfiguration.hpp>
26 #include <com/sun/star/drawing/framework/XResourceFactory.hpp>
27 #include <comphelper/diagnose_ex.hxx>
28 #include <sal/log.hxx>
29 #include <algorithm>
30 #include <utility>
32 using namespace ::com::sun::star;
33 using namespace ::com::sun::star::uno;
34 using namespace ::com::sun::star::drawing::framework;
36 namespace sd::framework {
38 //===== ConfigurationControllerResourceManager ================================
40 ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
41 std::shared_ptr<ResourceFactoryManager> pResourceFactoryContainer,
42 std::shared_ptr<ConfigurationControllerBroadcaster> pBroadcaster)
43 : maResourceMap(ResourceComparator()),
44 mpResourceFactoryContainer(std::move(pResourceFactoryContainer)),
45 mpBroadcaster(std::move(pBroadcaster))
49 ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager()
53 ConfigurationControllerResourceManager::ResourceDescriptor
54 ConfigurationControllerResourceManager::GetResource (
55 const Reference<XResourceId>& rxResourceId)
57 ::osl::MutexGuard aGuard (maMutex);
58 ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
59 if (iResource != maResourceMap.end())
60 return iResource->second;
61 else
62 return ResourceDescriptor();
65 void ConfigurationControllerResourceManager::ActivateResources (
66 const ::std::vector<Reference<XResourceId> >& rResources,
67 const Reference<XConfiguration>& rxConfiguration)
69 ::osl::MutexGuard aGuard (maMutex);
70 // Iterate in normal order over the resources that are to be
71 // activated so that resources on which others depend are activated
72 // before the depending resources are activated.
73 for (const Reference<XResourceId>& xResource : rResources)
74 ActivateResource(xResource, 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 [&] (Reference<XResourceId> const& xResource) {
89 return DeactivateResource(xResource, rxConfiguration);
90 } );
93 /* In this method we do following steps.
94 1. Get the factory with which the resource will be created.
95 2. Create the resource.
96 3. Add the resource to the URL->Object map of the configuration
97 controller.
98 4. Add the resource id to the current configuration.
99 5. Notify listeners.
101 void ConfigurationControllerResourceManager::ActivateResource (
102 const Reference<XResourceId>& rxResourceId,
103 const Reference<XConfiguration>& rxConfiguration)
105 if ( ! rxResourceId.is())
107 OSL_ASSERT(rxResourceId.is());
108 return;
111 SAL_INFO("sd.fwk", __func__ << ": activating resource " <<
112 FrameworkHelper::ResourceIdToString(rxResourceId));
114 // 1. Get the factory.
115 const OUString sResourceURL (rxResourceId->getResourceURL());
116 Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
117 if ( ! xFactory.is())
119 SAL_INFO("sd.fwk", __func__ << ": no factory found for " << sResourceURL);
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&) {}
139 if (xResource.is())
141 SAL_INFO("sd.fwk", __func__ << ": successfully created");
142 // 3. Add resource to URL->Object map.
143 AddResource(xResource, xFactory);
145 // 4. Add resource id to current configuration.
146 rxConfiguration->addResource(rxResourceId);
148 // 5. Notify the new resource to listeners of the ConfigurationController.
149 mpBroadcaster->NotifyListeners(
150 FrameworkHelper::msResourceActivationEvent,
151 rxResourceId,
152 xResource);
154 else
156 SAL_INFO("sd.fwk", __func__ << ": resource creation failed");
159 catch (RuntimeException&)
161 DBG_UNHANDLED_EXCEPTION("sd");
165 /* In this method we do following steps.
166 1. Remove the resource from the URL->Object map of the configuration
167 controller.
168 2. Notify listeners that deactivation has started.
169 3. Remove the resource id from the current configuration.
170 4. Release the resource.
171 5. Notify listeners about that deactivation is completed.
173 void ConfigurationControllerResourceManager::DeactivateResource (
174 const Reference<XResourceId>& rxResourceId,
175 const Reference<XConfiguration>& rxConfiguration)
177 if ( ! rxResourceId.is())
178 return;
180 #if OSL_DEBUG_LEVEL >= 1
181 bool bSuccess (false);
182 #endif
185 // 1. Remove resource from URL->Object map.
186 ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
188 if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
190 // 2. Notify listeners that the resource is being deactivated.
191 mpBroadcaster->NotifyListeners(
192 FrameworkHelper::msResourceDeactivationEvent,
193 rxResourceId,
194 aDescriptor.mxResource);
196 // 3. Remove resource id from current configuration.
197 rxConfiguration->removeResource(rxResourceId);
199 // 4. Release the resource.
202 aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
204 catch (const lang::DisposedException& rException)
206 if ( ! rException.Context.is()
207 || rException.Context == aDescriptor.mxResourceFactory)
209 // The factory is disposed and can be removed from the
210 // list of registered factories.
211 mpResourceFactoryContainer->RemoveFactoryForReference(
212 aDescriptor.mxResourceFactory);
216 #if OSL_DEBUG_LEVEL >= 1
217 bSuccess = true;
218 #endif
221 catch (RuntimeException&)
223 DBG_UNHANDLED_EXCEPTION("sd");
226 // 5. Notify listeners that the resource is being deactivated.
227 mpBroadcaster->NotifyListeners(
228 FrameworkHelper::msResourceDeactivationEndEvent,
229 rxResourceId,
230 nullptr);
232 #if OSL_DEBUG_LEVEL >= 1
233 if (bSuccess)
234 SAL_INFO("sd.fwk", __func__ << ": successfully deactivated " <<
235 FrameworkHelper::ResourceIdToString(rxResourceId));
236 else
237 SAL_INFO("sd.fwk", __func__ << ": activating resource " <<
238 FrameworkHelper::ResourceIdToString(rxResourceId)
239 << " failed");
240 #endif
243 void ConfigurationControllerResourceManager::AddResource (
244 const Reference<XResource>& rxResource,
245 const Reference<XResourceFactory>& rxFactory)
247 if ( ! rxResource.is())
249 OSL_ASSERT(rxResource.is());
250 return;
253 // Add the resource to the resource container.
254 ResourceDescriptor aDescriptor;
255 aDescriptor.mxResource = rxResource;
256 aDescriptor.mxResourceFactory = rxFactory;
257 maResourceMap[rxResource->getResourceId()] = aDescriptor;
259 #if OSL_DEBUG_LEVEL >= 2
260 SAL_INFO("sd.fwk", __func__ << ": ConfigurationControllerResourceManager::AddResource(): added " <<
261 FrameworkHelper::ResourceIdToString(rxResource->getResourceId()) <<
262 " -> " << rxResource.get());
263 #endif
266 ConfigurationControllerResourceManager::ResourceDescriptor
267 ConfigurationControllerResourceManager::RemoveResource (
268 const Reference<XResourceId>& rxResourceId)
270 ResourceDescriptor aDescriptor;
272 ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
273 if (iResource != maResourceMap.end())
275 #if OSL_DEBUG_LEVEL >= 2
276 SAL_INFO("sd.fwk", __func__ << ": ConfigurationControllerResourceManager::RemoveResource(): removing " <<
277 FrameworkHelper::ResourceIdToString(rxResourceId) <<
278 " -> " << &iResource);
279 #endif
281 aDescriptor = iResource->second;
282 maResourceMap.erase(rxResourceId);
285 return aDescriptor;
288 //===== ConfigurationControllerResourceManager::ResourceComparator ============
290 bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
291 const Reference<XResourceId>& rxId1,
292 const Reference<XResourceId>& rxId2) const
294 if (rxId1.is() && rxId2.is())
295 return rxId1->compareTo(rxId2)<0;
296 else if (rxId1.is())
297 return true;
298 else
299 return false;
302 } // end of namespace sd::framework
304 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */