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