Branch data Line data Source code
1 : : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : : /*************************************************************************
3 : : *
4 : : * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 : : *
6 : : * Copyright 2000, 2010 Oracle and/or its affiliates.
7 : : *
8 : : * OpenOffice.org - a multi-platform office productivity suite
9 : : *
10 : : * This file is part of OpenOffice.org.
11 : : *
12 : : * OpenOffice.org is free software: you can redistribute it and/or modify
13 : : * it under the terms of the GNU Lesser General Public License version 3
14 : : * only, as published by the Free Software Foundation.
15 : : *
16 : : * OpenOffice.org is distributed in the hope that it will be useful,
17 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
18 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 : : * GNU Lesser General Public License version 3 for more details
20 : : * (a copy is included in the LICENSE file that accompanied this code).
21 : : *
22 : : * You should have received a copy of the GNU Lesser General Public License
23 : : * version 3 along with OpenOffice.org. If not, see
24 : : * <http://www.openoffice.org/license.html>
25 : : * for a copy of the LGPLv3 License.
26 : : *
27 : : ************************************************************************/
28 : :
29 : :
30 : : #include "framework/ModuleController.hxx"
31 : :
32 : : #include "tools/ConfigurationAccess.hxx"
33 : : #include <comphelper/processfactory.hxx>
34 : : #include <comphelper/stl_types.hxx>
35 : : #include <boost/bind.hpp>
36 : : #include <boost/unordered_map.hpp>
37 : :
38 : : #include <tools/diagnose_ex.h>
39 : :
40 : : using namespace ::com::sun::star;
41 : : using namespace ::com::sun::star::uno;
42 : : using namespace ::com::sun::star::drawing::framework;
43 : : using ::rtl::OUString;
44 : : using ::sd::tools::ConfigurationAccess;
45 : :
46 : : namespace sd { namespace framework {
47 : :
48 : : static const sal_uInt32 snFactoryPropertyCount (2);
49 : : static const sal_uInt32 snStartupPropertyCount (1);
50 : :
51 : :
52 : :
53 : :
54 : 130 : class ModuleController::ResourceToFactoryMap
55 : : : public ::boost::unordered_map<
56 : : rtl::OUString,
57 : : rtl::OUString,
58 : : ::rtl::OUStringHash,
59 : : ::comphelper::UStringEqual>
60 : : {
61 : : public:
62 [ + - ]: 130 : ResourceToFactoryMap (void) {}
63 : : };
64 : :
65 : :
66 : 130 : class ModuleController::LoadedFactoryContainer
67 : : : public ::boost::unordered_map<
68 : : rtl::OUString,
69 : : WeakReference<XInterface>,
70 : : ::rtl::OUStringHash,
71 : : ::comphelper::UStringEqual>
72 : : {
73 : : public:
74 [ + - ]: 130 : LoadedFactoryContainer (void) {}
75 : : };
76 : :
77 : :
78 : :
79 : :
80 : :
81 : 130 : Reference<XInterface> SAL_CALL ModuleController_createInstance (
82 : : const Reference<XComponentContext>& rxContext)
83 : : {
84 [ + - ]: 130 : return Reference<XInterface>(ModuleController::CreateInstance(rxContext), UNO_QUERY);
85 : : }
86 : :
87 : :
88 : :
89 : :
90 : 38 : ::rtl::OUString ModuleController_getImplementationName (void) throw(RuntimeException)
91 : : {
92 : 38 : return ::rtl::OUString("com.sun.star.comp.Draw.framework.module.ModuleController");
93 : : }
94 : :
95 : :
96 : :
97 : :
98 : 16 : Sequence<rtl::OUString> SAL_CALL ModuleController_getSupportedServiceNames (void)
99 : : throw (RuntimeException)
100 : : {
101 [ + - ][ + - ]: 16 : static const ::rtl::OUString sServiceName("com.sun.star.drawing.framework.ModuleController");
102 : 16 : return Sequence<rtl::OUString>(&sServiceName, 1);
103 : : }
104 : :
105 : :
106 : :
107 : :
108 : : //===== ModuleController ======================================================
109 : :
110 : 130 : Reference<XModuleController> ModuleController::CreateInstance (
111 : : const Reference<XComponentContext>& rxContext)
112 : : {
113 [ + - ]: 130 : return new ModuleController(rxContext);
114 : : }
115 : :
116 : :
117 : :
118 : :
119 : 130 : ModuleController::ModuleController (const Reference<XComponentContext>& rxContext) throw()
120 : : : ModuleControllerInterfaceBase(MutexOwner::maMutex),
121 : : mxController(),
122 : 0 : mpResourceToFactoryMap(new ResourceToFactoryMap()),
123 [ + - ][ + - ]: 130 : mpLoadedFactories(new LoadedFactoryContainer())
[ + - ][ + - ]
124 : : {
125 : : (void)rxContext;
126 [ + - ]: 130 : LoadFactories(rxContext);
127 : 130 : }
128 : :
129 : :
130 : :
131 : :
132 [ + - ][ + - ]: 130 : ModuleController::~ModuleController (void) throw()
[ + - ]
133 : : {
134 [ - + ]: 260 : }
135 : :
136 : :
137 : :
138 : :
139 : 130 : void SAL_CALL ModuleController::disposing (void)
140 : : {
141 : : // Break the cyclic reference back to DrawController object
142 : 130 : mpLoadedFactories.reset();
143 : 130 : mpResourceToFactoryMap.reset();
144 : 130 : mxController.clear();
145 : 130 : }
146 : :
147 : :
148 : :
149 : :
150 : 130 : void ModuleController::LoadFactories (const Reference<XComponentContext>& rxContext)
151 : : {
152 : : try
153 : : {
154 : : ConfigurationAccess aConfiguration (
155 : : rxContext,
156 : : "/org.openoffice.Office.Impress/",
157 [ + - ]: 130 : ConfigurationAccess::READ_ONLY);
158 : : Reference<container::XNameAccess> xFactories (
159 : : aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/ResourceFactories"),
160 [ + - ][ + - ]: 130 : UNO_QUERY);
161 [ + - ]: 130 : ::std::vector<rtl::OUString> aProperties (snFactoryPropertyCount);
162 : 130 : aProperties[0] = "ServiceName";
163 : 130 : aProperties[1] = "ResourceList";
164 : : ConfigurationAccess::ForAll(
165 : : xFactories,
166 : : aProperties,
167 [ + - ][ + - ]: 130 : ::boost::bind(&ModuleController::ProcessFactory, this, _2));
[ + - ][ + - ]
[ # # ][ + - ]
168 : : }
169 : 0 : catch (Exception&)
170 : : {
171 : : DBG_UNHANDLED_EXCEPTION();
172 : : }
173 : 130 : }
174 : :
175 : :
176 : :
177 : :
178 : 520 : void ModuleController::ProcessFactory (const ::std::vector<Any>& rValues)
179 : : {
180 : : OSL_ASSERT(rValues.size() == snFactoryPropertyCount);
181 : :
182 : : // Get the service name of the factory.
183 : 520 : rtl::OUString sServiceName;
184 : 520 : rValues[0] >>= sServiceName;
185 : :
186 : : // Get all resource URLs that are created by the factory.
187 [ + - ]: 520 : Reference<container::XNameAccess> xResources (rValues[1], UNO_QUERY);
188 [ + - ]: 520 : ::std::vector<rtl::OUString> aURLs;
189 : : tools::ConfigurationAccess::FillList(
190 : : xResources,
191 : : "URL",
192 [ + - ]: 520 : aURLs);
193 : :
194 : : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::adding factory " <<
195 : : OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
196 : :
197 : : // Add the resource URLs to the map.
198 : 520 : ::std::vector<rtl::OUString>::const_iterator iResource;
199 [ + - ][ + + ]: 2860 : for (iResource=aURLs.begin(); iResource!=aURLs.end(); ++iResource)
[ + - ]
200 : : {
201 [ + - ]: 2340 : (*mpResourceToFactoryMap)[*iResource] = sServiceName;
202 : : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": " <<
203 : : OUStringToOString(*iResource, RTL_TEXTENCODING_UTF8).getStr());
204 : 520 : }
205 : 520 : }
206 : :
207 : :
208 : :
209 : :
210 : 130 : void ModuleController::InstantiateStartupServices (void)
211 : : {
212 : : try
213 : : {
214 : : tools::ConfigurationAccess aConfiguration (
215 : : "/org.openoffice.Office.Impress/",
216 [ + - ]: 130 : tools::ConfigurationAccess::READ_ONLY);
217 : : Reference<container::XNameAccess> xFactories (
218 : : aConfiguration.GetConfigurationNode("MultiPaneGUI/Framework/StartupServices"),
219 [ + - ][ + - ]: 130 : UNO_QUERY);
220 [ + - ]: 130 : ::std::vector<rtl::OUString> aProperties (snStartupPropertyCount);
221 : 130 : aProperties[0] = "ServiceName";
222 : : tools::ConfigurationAccess::ForAll(
223 : : xFactories,
224 : : aProperties,
225 [ + - ][ + - ]: 130 : ::boost::bind(&ModuleController::ProcessStartupService, this, _2));
[ + - ][ + - ]
[ # # ][ + - ]
226 : : }
227 : 0 : catch (Exception&)
228 : : {
229 : : OSL_TRACE("ERROR in ModuleController::InstantiateStartupServices");
230 : : }
231 : 130 : }
232 : :
233 : :
234 : :
235 : :
236 : 130 : void ModuleController::ProcessStartupService (const ::std::vector<Any>& rValues)
237 : : {
238 : : OSL_ASSERT(rValues.size() == snStartupPropertyCount);
239 : :
240 : : try
241 : : {
242 : : // Get the service name of the startup service.
243 : 130 : rtl::OUString sServiceName;
244 : 130 : rValues[0] >>= sServiceName;
245 : :
246 : : // Instantiate service.
247 : : Reference<lang::XMultiServiceFactory> xGlobalFactory (
248 [ + - ][ + - ]: 130 : ::comphelper::getProcessServiceFactory(), UNO_QUERY);
249 [ + - ]: 130 : if (xGlobalFactory.is())
250 : : {
251 : : // Create the startup service.
252 [ + - ]: 130 : Sequence<Any> aArguments(1);
253 [ + - ][ + - ]: 130 : aArguments[0] <<= mxController;
254 : : // Note that when the new object will be destroyed at the end of
255 : : // this scope when it does not register itself anywhere.
256 : : // Typically it will add itself as ConfigurationChangeListener
257 : : // at the configuration controller.
258 [ + - ][ + - ]: 130 : xGlobalFactory->createInstanceWithArguments(sServiceName, aArguments);
259 : :
260 : : SAL_INFO("sd.fwk", OSL_THIS_FUNC << ": ModuleController::created startup service " <<
261 [ + - ]: 130 : OUStringToOString(sServiceName, RTL_TEXTENCODING_UTF8).getStr());
262 [ # # ]: 130 : }
263 : : }
264 : 0 : catch (Exception&)
265 : : {
266 : : OSL_TRACE("ERROR in ModuleController::ProcessStartupServices");
267 : : }
268 : 130 : }
269 : :
270 : :
271 : :
272 : :
273 : : //----- XModuleController -----------------------------------------------------
274 : :
275 : 302 : void SAL_CALL ModuleController::requestResource (const OUString& rsResourceURL)
276 : : throw (RuntimeException)
277 : : {
278 [ + - ]: 302 : ResourceToFactoryMap::const_iterator iFactory (mpResourceToFactoryMap->find(rsResourceURL));
279 [ + - ][ + - ]: 302 : if (iFactory != mpResourceToFactoryMap->end())
280 : : {
281 : : // Check that the factory has already been loaded and not been
282 : : // destroyed in the meantime.
283 : 302 : Reference<XInterface> xFactory;
284 : : LoadedFactoryContainer::const_iterator iLoadedFactory (
285 [ + - ][ + - ]: 302 : mpLoadedFactories->find(iFactory->second));
286 [ - + ][ + - ]: 302 : if (iLoadedFactory != mpLoadedFactories->end())
287 [ # # ][ # # ]: 0 : xFactory = Reference<XInterface>(iLoadedFactory->second, UNO_QUERY);
[ # # ][ # # ]
288 [ + - ]: 302 : if ( ! xFactory.is())
289 : : {
290 : : // Create a new instance of the factory.
291 : : Reference<lang::XMultiServiceFactory> xGlobalFactory (
292 [ + - ][ + - ]: 302 : ::comphelper::getProcessServiceFactory(), UNO_QUERY);
293 [ + - ]: 302 : if (xGlobalFactory.is())
294 : : {
295 : : // Create the factory service.
296 [ + - ]: 302 : Sequence<Any> aArguments(1);
297 [ + - ][ + - ]: 302 : aArguments[0] <<= mxController;
298 [ + - ]: 302 : xFactory = xGlobalFactory->createInstanceWithArguments(
299 [ + - ]: 302 : iFactory->second,
300 [ + - ][ + - ]: 302 : aArguments);
301 : :
302 : : // Remember that this factory has been instanced.
303 [ + - ][ + - ]: 302 : (*mpLoadedFactories)[iFactory->second] = xFactory;
[ + - ][ + - ]
304 : 302 : }
305 : 302 : }
306 : : }
307 : 302 : }
308 : :
309 : :
310 : :
311 : :
312 : : //----- XInitialization -------------------------------------------------------
313 : :
314 : 130 : void SAL_CALL ModuleController::initialize (const Sequence<Any>& aArguments)
315 : : throw (Exception, RuntimeException)
316 : : {
317 [ + - ]: 130 : if (aArguments.getLength() > 0)
318 : : {
319 : : try
320 : : {
321 : : // Get the XController from the first argument.
322 [ + - ][ + - ]: 130 : mxController = Reference<frame::XController>(aArguments[0], UNO_QUERY_THROW);
[ # # ]
323 : :
324 [ + - ]: 130 : InstantiateStartupServices();
325 : : }
326 : 0 : catch (RuntimeException&)
327 : : {}
328 : : }
329 : 130 : }
330 : :
331 : :
332 [ + - ][ + - ]: 75 : } } // end of namespace sd::framework
333 : :
334 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|