merge the formfield patch from ooo-build
[ooovba.git] / sd / source / ui / framework / configuration / ConfigurationControllerResourceManager.cxx
blob0fc28de8e6837d30857f5e5b22de7ceb77b5dc34
1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationControllerResourceManager.cxx,v $
11 * $Revision: 1.4 $
13 * This file is part of OpenOffice.org.
15 * OpenOffice.org is free software: you can redistribute it and/or modify
16 * it under the terms of the GNU Lesser General Public License version 3
17 * only, as published by the Free Software Foundation.
19 * OpenOffice.org is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License version 3 for more details
23 * (a copy is included in the LICENSE file that accompanied this code).
25 * You should have received a copy of the GNU Lesser General Public License
26 * version 3 along with OpenOffice.org. If not, see
27 * <http://www.openoffice.org/license.html>
28 * for a copy of the LGPLv3 License.
30 ************************************************************************/
32 #include "precompiled_sd.hxx"
34 #include "ConfigurationControllerResourceManager.hxx"
35 #include "ConfigurationControllerBroadcaster.hxx"
36 #include "ResourceFactoryManager.hxx"
37 #include "framework/FrameworkHelper.hxx"
38 #include <com/sun/star/lang/DisposedException.hpp>
39 #include <algorithm>
40 #include <boost/bind.hpp>
42 using namespace ::com::sun::star;
43 using namespace ::com::sun::star::uno;
44 using namespace ::com::sun::star::drawing::framework;
45 using ::rtl::OUString;
47 #undef VERBOSE
48 //#define VERBOSE 1
50 namespace sd { namespace framework {
52 //===== ConfigurationControllerResourceManager ================================
54 ConfigurationControllerResourceManager::ConfigurationControllerResourceManager (
55 const ::boost::shared_ptr<ResourceFactoryManager>& rpResourceFactoryContainer,
56 const ::boost::shared_ptr<ConfigurationControllerBroadcaster>& rpBroadcaster)
57 : maResourceMap(ResourceComparator()),
58 mpResourceFactoryContainer(rpResourceFactoryContainer),
59 mpBroadcaster(rpBroadcaster)
66 ConfigurationControllerResourceManager::~ConfigurationControllerResourceManager (void)
73 ConfigurationControllerResourceManager::ResourceDescriptor
74 ConfigurationControllerResourceManager::GetResource (
75 const Reference<XResourceId>& rxResourceId)
77 ::osl::MutexGuard aGuard (maMutex);
78 ResourceMap::const_iterator iResource (maResourceMap.find(rxResourceId));
79 if (iResource != maResourceMap.end())
80 return iResource->second;
81 else
82 return ResourceDescriptor();
88 void ConfigurationControllerResourceManager::ActivateResources (
89 const ::std::vector<Reference<XResourceId> >& rResources,
90 const Reference<XConfiguration>& rxConfiguration)
92 ::osl::MutexGuard aGuard (maMutex);
93 // Iterate in normal order over the resources that are to be
94 // activated so that resources on which others depend are activated
95 // beforet the depending resources are activated.
96 ::std::for_each(
97 rResources.begin(),
98 rResources.end(),
99 ::boost::bind(&ConfigurationControllerResourceManager::ActivateResource,
100 this, _1, rxConfiguration));
106 void ConfigurationControllerResourceManager::DeactivateResources (
107 const ::std::vector<Reference<XResourceId> >& rResources,
108 const Reference<XConfiguration>& rxConfiguration)
110 ::osl::MutexGuard aGuard (maMutex);
111 // Iterate in reverese order over the resources that are to be
112 // deactivated so that resources on which others depend are deactivated
113 // only when the depending resources have already been deactivated.
114 ::std::for_each(
115 rResources.rbegin(),
116 rResources.rend(),
117 ::boost::bind(&ConfigurationControllerResourceManager::DeactivateResource,
118 this, _1, rxConfiguration));
124 /* In this method we do following steps.
125 1. Get the factory with which the resource will be created.
126 2. Create the resource.
127 3. Add the resource to the URL->Object map of the configuration
128 controller.
129 4. Add the resource id to the current configuration.
130 5. Notify listeners.
132 void ConfigurationControllerResourceManager::ActivateResource (
133 const Reference<XResourceId>& rxResourceId,
134 const Reference<XConfiguration>& rxConfiguration)
136 if ( ! rxResourceId.is())
138 OSL_ASSERT(rxResourceId.is());
139 return;
142 #if defined VERBOSE && VERBOSE>=1
143 OSL_TRACE("activating resource %s\n", OUStringToOString(
144 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
145 #endif
147 // 1. Get the factory.
148 const OUString sResourceURL (rxResourceId->getResourceURL());
149 Reference<XResourceFactory> xFactory (mpResourceFactoryContainer->GetFactory(sResourceURL));
150 if ( ! xFactory.is())
152 #if defined VERBOSE && VERBOSE>=1
153 OSL_TRACE(" no factory found fo %s\n",
154 OUStringToOString(sResourceURL, RTL_TEXTENCODING_UTF8).getStr());
155 #endif
156 return;
161 // 2. Create the resource.
162 Reference<XResource> xResource;
165 xResource = xFactory->createResource(rxResourceId);
167 catch (lang::DisposedException&)
169 // The factory is disposed and can be removed from the list
170 // of registered factories.
171 mpResourceFactoryContainer->RemoveFactoryForReference(xFactory);
173 catch(Exception&)
175 OSL_ENSURE(false, "caught exception while activating resource");
178 if (xResource.is())
180 #if defined VERBOSE && VERBOSE>=1
181 OSL_TRACE(" successfully created\n");
182 #endif
183 // 3. Add resource to URL->Object map.
184 AddResource(xResource, xFactory);
186 // 4. Add resource id to current configuration.
187 rxConfiguration->addResource(rxResourceId);
189 // 5. Notify the new resource to listeners of the ConfigurationController.
190 mpBroadcaster->NotifyListeners(
191 FrameworkHelper::msResourceActivationEvent,
192 rxResourceId,
193 xResource);
195 else
197 #if defined VERBOSE && VERBOSE>=1
198 OSL_TRACE(" resource creation failed\n");
199 #endif
202 catch (RuntimeException&)
204 OSL_ASSERT(false);
211 /* In this method we do following steps.
212 1. Remove the resource from the URL->Object map of the configuration
213 controller.
214 2. Notify listeners.
215 3. Remove the resource id from the current configuration.
216 4. Release the resource.
218 void ConfigurationControllerResourceManager::DeactivateResource (
219 const Reference<XResourceId>& rxResourceId,
220 const Reference<XConfiguration>& rxConfiguration)
222 if ( ! rxResourceId.is())
223 return;
225 bool bSuccess (false);
228 // 1. Remove resource from URL->Object map.
229 ResourceDescriptor aDescriptor (RemoveResource(rxResourceId));
231 if (aDescriptor.mxResource.is() && aDescriptor.mxResourceFactory.is())
233 // 2. Notifiy listeners that the resource is being deactivated.
234 mpBroadcaster->NotifyListeners(
235 FrameworkHelper::msResourceDeactivationEvent,
236 rxResourceId,
237 aDescriptor.mxResource);
239 // 3. Remove resource id from current configuration.
240 rxConfiguration->removeResource(rxResourceId);
242 // 4. Release the resource.
245 aDescriptor.mxResourceFactory->releaseResource(aDescriptor.mxResource);
247 catch (lang::DisposedException& rException)
249 if ( ! rException.Context.is()
250 || rException.Context == aDescriptor.mxResourceFactory)
252 // The factory is disposed and can be removed from the
253 // list of registered factories.
254 mpResourceFactoryContainer->RemoveFactoryForReference(
255 aDescriptor.mxResourceFactory);
259 bSuccess = true;
262 catch (RuntimeException&)
264 OSL_ASSERT(false);
267 #if defined VERBOSE && VERBOSE>=1
268 if (bSuccess)
269 OSL_TRACE("successfully deactivated %s\n", OUStringToOString(
270 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
271 else
272 OSL_TRACE("activating resource %s failed\n", OUStringToOString(
273 FrameworkHelper::ResourceIdToString(rxResourceId), RTL_TEXTENCODING_UTF8).getStr());
274 #endif
280 void ConfigurationControllerResourceManager::AddResource (
281 const Reference<XResource>& rxResource,
282 const Reference<XResourceFactory>& rxFactory)
284 if ( ! rxResource.is())
286 OSL_ASSERT(rxResource.is());
287 return;
290 // Add the resource to the resource container.
291 ResourceDescriptor aDescriptor;
292 aDescriptor.mxResource = rxResource;
293 aDescriptor.mxResourceFactory = rxFactory;
294 maResourceMap[rxResource->getResourceId()] = aDescriptor;
296 #if defined VERBOSE && VERBOSE>=2
297 OSL_TRACE("ConfigurationControllerResourceManager::AddResource(): added %s -> %x\n",
298 OUStringToOString(
299 FrameworkHelper::ResourceIdToString(rxResource->getResourceId()),
300 RTL_TEXTENCODING_UTF8).getStr(),
301 rxResource.get());
302 #endif
308 ConfigurationControllerResourceManager::ResourceDescriptor
309 ConfigurationControllerResourceManager::RemoveResource (
310 const Reference<XResourceId>& rxResourceId)
312 ResourceDescriptor aDescriptor;
314 ResourceMap::iterator iResource (maResourceMap.find(rxResourceId));
315 if (iResource != maResourceMap.end())
317 #if defined VERBOSE && VERBOSE>=2
318 OSL_TRACE("ConfigurationControllerResourceManager::RemoveResource(): removing %s -> %x\n",
319 OUStringToOString(
320 FrameworkHelper::ResourceIdToString(rxResourceId),
321 RTL_TEXTENCODING_UTF8).getStr(),
322 *iResource);
323 #endif
325 aDescriptor = iResource->second;
326 maResourceMap.erase(rxResourceId);
329 return aDescriptor;
335 //===== ConfigurationControllerResourceManager::ResourceComparator ============
337 bool ConfigurationControllerResourceManager::ResourceComparator::operator() (
338 const Reference<XResourceId>& rxId1,
339 const Reference<XResourceId>& rxId2) const
341 if (rxId1.is() && rxId2.is())
342 return rxId1->compareTo(rxId2)<0;
343 else if (rxId1.is())
344 return true;
345 else if (rxId2.is())
346 return false;
347 else
348 return false;
354 } } // end of namespace sd::framework