1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/ModuleController.hxx"
22 #include "tools/ConfigurationAccess.hxx"
23 #include <comphelper/processfactory.hxx>
24 #include <boost/bind.hpp>
25 #include <unordered_map>
27 #include <tools/diagnose_ex.h>
31 using namespace ::com::sun::star
;
32 using namespace ::com::sun::star::uno
;
33 using namespace ::com::sun::star::drawing::framework
;
34 using ::sd::tools::ConfigurationAccess
;
36 namespace sd
{ namespace framework
{
38 static const sal_uInt32
snFactoryPropertyCount (2);
39 static const sal_uInt32
snStartupPropertyCount (1);
41 class ModuleController::ResourceToFactoryMap
42 : public std::unordered_map
<
48 ResourceToFactoryMap() {}
51 class ModuleController::LoadedFactoryContainer
52 : public std::unordered_map
<
54 WeakReference
<XInterface
>,
58 LoadedFactoryContainer() {}
61 //===== ModuleController ======================================================
62 Reference
<XModuleController
> ModuleController::CreateInstance (
63 const Reference
<XComponentContext
>& rxContext
)
65 return new ModuleController(rxContext
);
68 ModuleController::ModuleController (const Reference
<XComponentContext
>& rxContext
)
69 throw (std::exception
)
70 : ModuleControllerInterfaceBase(MutexOwner::maMutex
),
72 mpResourceToFactoryMap(new ResourceToFactoryMap()),
73 mpLoadedFactories(new LoadedFactoryContainer())
76 LoadFactories(rxContext
);
79 ModuleController::~ModuleController() throw()
83 void SAL_CALL
ModuleController::disposing()
85 // Break the cyclic reference back to DrawController object
86 mpLoadedFactories
.reset();
87 mpResourceToFactoryMap
.reset();
91 void ModuleController::LoadFactories (const Reference
<XComponentContext
>& rxContext
)
95 ConfigurationAccess
aConfiguration (
97 "/org.openoffice.Office.Impress/",
98 ConfigurationAccess::READ_ONLY
);
99 Reference
<container::XNameAccess
> xFactories (
100 aConfiguration
.GetConfigurationNode("MultiPaneGUI/Framework/ResourceFactories"),
102 ::std::vector
<OUString
> aProperties (snFactoryPropertyCount
);
103 aProperties
[0] = "ServiceName";
104 aProperties
[1] = "ResourceList";
105 ConfigurationAccess::ForAll(
108 ::boost::bind(&ModuleController::ProcessFactory
, this, _2
));
112 DBG_UNHANDLED_EXCEPTION();
116 void ModuleController::ProcessFactory (const ::std::vector
<Any
>& rValues
)
118 OSL_ASSERT(rValues
.size() == snFactoryPropertyCount
);
120 // Get the service name of the factory.
121 OUString sServiceName
;
122 rValues
[0] >>= sServiceName
;
124 // Get all resource URLs that are created by the factory.
125 Reference
<container::XNameAccess
> xResources (rValues
[1], UNO_QUERY
);
126 ::std::vector
<OUString
> aURLs
;
127 tools::ConfigurationAccess::FillList(
132 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": ModuleController::adding factory " <<
133 OUStringToOString(sServiceName
, RTL_TEXTENCODING_UTF8
).getStr());
135 // Add the resource URLs to the map.
136 ::std::vector
<OUString
>::const_iterator iResource
;
137 for (iResource
=aURLs
.begin(); iResource
!=aURLs
.end(); ++iResource
)
139 (*mpResourceToFactoryMap
)[*iResource
] = sServiceName
;
140 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": " <<
141 OUStringToOString(*iResource
, RTL_TEXTENCODING_UTF8
).getStr());
145 void ModuleController::InstantiateStartupServices()
149 tools::ConfigurationAccess
aConfiguration (
150 "/org.openoffice.Office.Impress/",
151 tools::ConfigurationAccess::READ_ONLY
);
152 Reference
<container::XNameAccess
> xFactories (
153 aConfiguration
.GetConfigurationNode("MultiPaneGUI/Framework/StartupServices"),
155 ::std::vector
<OUString
> aProperties (snStartupPropertyCount
);
156 aProperties
[0] = "ServiceName";
157 tools::ConfigurationAccess::ForAll(
160 ::boost::bind(&ModuleController::ProcessStartupService
, this, _2
));
164 OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
168 void ModuleController::ProcessStartupService (const ::std::vector
<Any
>& rValues
)
170 OSL_ASSERT(rValues
.size() == snStartupPropertyCount
);
174 // Get the service name of the startup service.
175 OUString sServiceName
;
176 rValues
[0] >>= sServiceName
;
178 // Instantiate service.
179 Reference
<uno::XComponentContext
> xContext
=
180 ::comphelper::getProcessComponentContext();
182 // Create the startup service.
183 Sequence
<Any
> aArguments(1);
184 aArguments
[0] <<= mxController
;
185 // Note that when the new object will be destroyed at the end of
186 // this scope when it does not register itself anywhere.
187 // Typically it will add itself as ConfigurationChangeListener
188 // at the configuration controller.
189 xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(sServiceName
, aArguments
, xContext
);
191 SAL_INFO("sd.fwk", OSL_THIS_FUNC
<< ": ModuleController::created startup service " <<
192 OUStringToOString(sServiceName
, RTL_TEXTENCODING_UTF8
).getStr());
196 OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
200 //----- XModuleController -----------------------------------------------------
202 void SAL_CALL
ModuleController::requestResource (const OUString
& rsResourceURL
)
203 throw (RuntimeException
, std::exception
)
205 ResourceToFactoryMap::const_iterator
iFactory (mpResourceToFactoryMap
->find(rsResourceURL
));
206 if (iFactory
!= mpResourceToFactoryMap
->end())
208 // Check that the factory has already been loaded and not been
209 // destroyed in the meantime.
210 Reference
<XInterface
> xFactory
;
211 LoadedFactoryContainer::const_iterator
iLoadedFactory (
212 mpLoadedFactories
->find(iFactory
->second
));
213 if (iLoadedFactory
!= mpLoadedFactories
->end())
214 xFactory
= Reference
<XInterface
>(iLoadedFactory
->second
, UNO_QUERY
);
215 if ( ! xFactory
.is())
217 // Create a new instance of the factory.
218 Reference
<uno::XComponentContext
> xContext
=
219 ::comphelper::getProcessComponentContext();
221 // Create the factory service.
222 Sequence
<Any
> aArguments(1);
223 aArguments
[0] <<= mxController
;
224 OSL_TRACE("creating resource %s",
225 OUStringToOString(iFactory
->second
, RTL_TEXTENCODING_ASCII_US
).getStr());
228 xFactory
= xContext
->getServiceManager()->createInstanceWithArgumentsAndContext(
233 catch (const Exception
&)
235 OSL_TRACE("caught exception while creating factory.");
238 // Remember that this factory has been instanced.
239 (*mpLoadedFactories
)[iFactory
->second
] = xFactory
;
244 //----- XInitialization -------------------------------------------------------
246 void SAL_CALL
ModuleController::initialize (const Sequence
<Any
>& aArguments
)
247 throw (Exception
, RuntimeException
, std::exception
)
249 if (aArguments
.getLength() > 0)
253 // Get the XController from the first argument.
254 mxController
= Reference
<frame::XController
>(aArguments
[0], UNO_QUERY_THROW
);
256 InstantiateStartupServices();
258 catch (RuntimeException
&)
263 } } // end of namespace sd::framework
266 extern "C" SAL_DLLPUBLIC_EXPORT ::com::sun::star::uno::XInterface
* SAL_CALL
267 com_sun_star_comp_Draw_framework_module_ModuleController_get_implementation(
268 ::com::sun::star::uno::XComponentContext
* context
,
269 ::com::sun::star::uno::Sequence
<css::uno::Any
> const &)
271 css::uno::Reference
< css::uno::XInterface
> xModCont ( sd::framework::ModuleController::CreateInstance(context
) );
273 return xModCont
.get();
277 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */