update dev300-m57
[ooovba.git] / framework / source / services / modulemanager.cxx
blob1ad7cfb47ea45ae7200f3a40f408026a74bbc4af
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: modulemanager.cxx,v $
10 * $Revision: 1.10 $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_framework.hxx"
34 #include "services/modulemanager.hxx"
35 #include "services/frame.hxx"
37 //_______________________________________________
38 // own includes
39 #include <threadhelp/readguard.hxx>
40 #include <threadhelp/writeguard.hxx>
41 #include <services.h>
43 //_______________________________________________
44 // interface includes
45 #include <com/sun/star/frame/XFrame.hpp>
46 #include <com/sun/star/frame/XController.hpp>
47 #include <com/sun/star/frame/XModel.hpp>
48 #include <com/sun/star/frame/XModule.hpp>
49 #include <comphelper/configurationhelper.hxx>
50 #include <comphelper/sequenceashashmap.hxx>
51 #include <comphelper/sequenceasvector.hxx>
52 #include <comphelper/enumhelper.hxx>
54 //_______________________________________________
55 // other includes
57 namespace framework
60 static const ::rtl::OUString CFGPATH_FACTORIES = ::rtl::OUString::createFromAscii("/org.openoffice.Setup/Office/Factories");
61 static const ::rtl::OUString MODULEPROP_IDENTIFIER = ::rtl::OUString::createFromAscii("ooSetupFactoryModuleIdentifier" );
63 /*-----------------------------------------------
64 04.12.2003 09:32
65 -----------------------------------------------*/
66 DEFINE_XINTERFACE_7(ModuleManager ,
67 OWeakObject ,
68 DIRECT_INTERFACE(css::lang::XTypeProvider ),
69 DIRECT_INTERFACE(css::lang::XServiceInfo ),
70 DIRECT_INTERFACE(css::container::XNameReplace ),
71 DIRECT_INTERFACE(css::container::XNameAccess ),
72 DIRECT_INTERFACE(css::container::XElementAccess ),
73 DIRECT_INTERFACE(css::container::XContainerQuery),
74 DIRECT_INTERFACE(css::frame::XModuleManager ))
76 /*-----------------------------------------------
77 04.12.2003 09:32
78 -----------------------------------------------*/
79 DEFINE_XTYPEPROVIDER_7(ModuleManager ,
80 css::lang::XTypeProvider ,
81 css::lang::XServiceInfo ,
82 css::container::XNameReplace ,
83 css::container::XNameAccess ,
84 css::container::XElementAccess ,
85 css::container::XContainerQuery,
86 css::frame::XModuleManager )
88 /*-----------------------------------------------
89 04.12.2003 09:35
90 -----------------------------------------------*/
91 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(ModuleManager ,
92 ::cppu::OWeakObject ,
93 SERVICENAME_MODULEMANAGER ,
94 IMPLEMENTATIONNAME_MODULEMANAGER)
96 /*-----------------------------------------------
97 04.12.2003 09:35
98 -----------------------------------------------*/
99 DEFINE_INIT_SERVICE(
100 ModuleManager,
102 /*Attention
103 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
104 to create a new instance of this class by our own supported service factory.
105 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
110 /*-----------------------------------------------
111 04.12.2003 09:30
112 -----------------------------------------------*/
113 ModuleManager::ModuleManager(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
114 : ThreadHelpBase( )
115 , m_xSMGR (xSMGR)
119 /*-----------------------------------------------
120 10.12.2003 11:59
121 -----------------------------------------------*/
122 ModuleManager::~ModuleManager()
124 if (m_xCFG.is())
125 m_xCFG.clear();
128 /*-----------------------------------------------
129 10.12.2003 11:02
130 -----------------------------------------------*/
131 ::rtl::OUString SAL_CALL ModuleManager::identify(const css::uno::Reference< css::uno::XInterface >& xModule)
132 throw(css::lang::IllegalArgumentException,
133 css::frame::UnknownModuleException,
134 css::uno::RuntimeException )
136 // valid parameter?
137 css::uno::Reference< css::frame::XFrame > xFrame (xModule, css::uno::UNO_QUERY);
138 css::uno::Reference< css::awt::XWindow > xWindow (xModule, css::uno::UNO_QUERY);
139 css::uno::Reference< css::frame::XController > xController(xModule, css::uno::UNO_QUERY);
140 css::uno::Reference< css::frame::XModel > xModel (xModule, css::uno::UNO_QUERY);
142 if (
143 (!xFrame.is() ) &&
144 (!xWindow.is() ) &&
145 (!xController.is()) &&
146 (!xModel.is() )
149 throw css::lang::IllegalArgumentException(
150 ::rtl::OUString::createFromAscii("Given module is not a frame nor a window, controller or model."),
151 static_cast< ::cppu::OWeakObject* >(this),
155 if (xFrame.is())
157 xController = xFrame->getController();
158 xWindow = xFrame->getComponentWindow();
160 if (xController.is())
161 xModel = xController->getModel();
163 // modules are implemented by the deepest component in hierarchy ...
164 // Means: model -> controller -> window
165 // No fallbacks to higher components are allowed !
166 // Note : A frame provides access to module components only ... but it's not a module by himself.
168 ::rtl::OUString sModule;
169 if (xModel.is())
170 sModule = implts_identify(xModel);
171 else
172 if (xController.is())
173 sModule = implts_identify(xController);
174 else
175 if (xWindow.is())
176 sModule = implts_identify(xWindow);
178 if (sModule.getLength() < 1)
179 throw css::frame::UnknownModuleException(
180 ::rtl::OUString::createFromAscii("Cant find suitable module for the given component."),
181 static_cast< ::cppu::OWeakObject* >(this));
183 return sModule;
186 /*-----------------------------------------------
187 08.03.2007 09:55
188 -----------------------------------------------*/
189 void SAL_CALL ModuleManager::replaceByName(const ::rtl::OUString& sName ,
190 const css::uno::Any& aValue)
191 throw (css::lang::IllegalArgumentException ,
192 css::container::NoSuchElementException,
193 css::lang::WrappedTargetException ,
194 css::uno::RuntimeException )
196 ::comphelper::SequenceAsHashMap lProps(aValue);
197 if (lProps.size() < 1)
199 throw css::lang::IllegalArgumentException(
200 ::rtl::OUString::createFromAscii("No properties given to replace part of module."),
201 static_cast< css::container::XNameAccess* >(this),
205 // SAFE -> ----------------------------------
206 ReadGuard aReadLock(m_aLock);
207 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
208 aReadLock.unlock();
209 // <- SAFE ----------------------------------
211 // get access to the element
212 // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
213 // it cache it as a member of this module manager instance. If we change some props there ... but dont
214 // flush changes (because an error occured) we will read them later. If we use a different config access
215 // we can close it without a flush ... and our read data wont be affected .-)
216 css::uno::Reference< css::uno::XInterface > xCfg = ::comphelper::ConfigurationHelper::openConfig(
217 xSMGR,
218 CFGPATH_FACTORIES,
219 ::comphelper::ConfigurationHelper::E_STANDARD);
220 css::uno::Reference< css::container::XNameAccess > xModules (xCfg, css::uno::UNO_QUERY_THROW);
221 css::uno::Reference< css::container::XNameReplace > xModule ;
223 xModules->getByName(sName) >>= xModule;
224 if (!xModule.is())
226 throw css::uno::RuntimeException(
227 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
228 static_cast< css::container::XNameAccess* >(this));
231 ::comphelper::SequenceAsHashMap::const_iterator pProp;
232 for ( pProp = lProps.begin();
233 pProp != lProps.end() ;
234 ++pProp )
236 const ::rtl::OUString& sPropName = pProp->first;
237 const css::uno::Any& aPropValue = pProp->second;
239 // let "NoSuchElementException" out ! We support the same API ...
240 // and without a flush() at the end all changed data before will be ignored !
241 xModule->replaceByName(sPropName, aPropValue);
244 ::comphelper::ConfigurationHelper::flush(xCfg);
247 /*-----------------------------------------------
248 10.12.2003 12:05
249 -----------------------------------------------*/
250 css::uno::Any SAL_CALL ModuleManager::getByName(const ::rtl::OUString& sName)
251 throw(css::container::NoSuchElementException,
252 css::lang::WrappedTargetException ,
253 css::uno::RuntimeException )
255 // get access to the element
256 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
257 css::uno::Any aElement = xCFG->getByName(sName);
259 css::uno::Reference< css::container::XNameAccess > xModule;
260 aElement >>= xModule;
261 if (!xModule.is())
263 throw css::uno::RuntimeException(
264 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
265 static_cast< css::container::XNameAccess* >(this));
268 // convert it to seq< PropertyValue >
269 const css::uno::Sequence< ::rtl::OUString > lPropNames = xModule->getElementNames();
270 ::comphelper::SequenceAsHashMap lProps ;
271 sal_Int32 c = lPropNames.getLength();
272 sal_Int32 i = 0;
274 lProps[MODULEPROP_IDENTIFIER] <<= sName;
275 for (i=0; i<c; ++i)
277 const ::rtl::OUString& sPropName = lPropNames[i];
278 lProps[sPropName] = xModule->getByName(sPropName);
281 return css::uno::makeAny(lProps.getAsConstPropertyValueList());
284 /*-----------------------------------------------
285 10.12.2003 11:58
286 -----------------------------------------------*/
287 css::uno::Sequence< ::rtl::OUString > SAL_CALL ModuleManager::getElementNames()
288 throw(css::uno::RuntimeException)
290 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
291 return xCFG->getElementNames();
294 /*-----------------------------------------------
295 10.12.2003 11:57
296 -----------------------------------------------*/
297 sal_Bool SAL_CALL ModuleManager::hasByName(const ::rtl::OUString& sName)
298 throw(css::uno::RuntimeException)
300 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
301 return xCFG->hasByName(sName);
304 /*-----------------------------------------------
305 10.12.2003 11:35
306 -----------------------------------------------*/
307 css::uno::Type SAL_CALL ModuleManager::getElementType()
308 throw(css::uno::RuntimeException)
310 return ::getCppuType((const css::uno::Sequence< css::beans::PropertyValue >*)0);
313 /*-----------------------------------------------
314 10.12.2003 11:56
315 -----------------------------------------------*/
316 sal_Bool SAL_CALL ModuleManager::hasElements()
317 throw(css::uno::RuntimeException)
319 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
320 return xCFG->hasElements();
323 /*-----------------------------------------------
324 07.03.2007 12:55
325 -----------------------------------------------*/
326 css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByQuery(const ::rtl::OUString&)
327 throw(css::uno::RuntimeException)
329 return css::uno::Reference< css::container::XEnumeration >();
332 /*-----------------------------------------------
333 07.03.2007 12:55
334 -----------------------------------------------*/
335 css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
336 throw(css::uno::RuntimeException)
338 ::comphelper::SequenceAsHashMap lSearchProps (lProperties);
339 css::uno::Sequence< ::rtl::OUString > lModules = getElementNames();
340 sal_Int32 c = lModules.getLength();
341 sal_Int32 i = 0;
342 ::comphelper::SequenceAsVector< css::uno::Any > lResult ;
344 for (i=0; i<c; ++i)
348 const ::rtl::OUString& sModule = lModules[i];
349 ::comphelper::SequenceAsHashMap lModuleProps = getByName(sModule);
351 if (lModuleProps.match(lSearchProps))
352 lResult.push_back(css::uno::makeAny(lModuleProps.getAsConstPropertyValueList()));
354 catch(const css::uno::Exception&)
358 ::comphelper::OAnyEnumeration* pEnum = new ::comphelper::OAnyEnumeration(lResult.getAsConstList());
359 css::uno::Reference< css::container::XEnumeration > xEnum(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY_THROW);
360 return xEnum;
363 /*-----------------------------------------------
364 14.12.2003 09:45
365 -----------------------------------------------*/
366 css::uno::Reference< css::container::XNameAccess > ModuleManager::implts_getConfig()
367 throw(css::uno::RuntimeException)
369 // SAFE -> ----------------------------------
370 ReadGuard aReadLock(m_aLock);
371 if (m_xCFG.is())
372 return m_xCFG;
373 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
374 aReadLock.unlock();
375 // <- SAFE ----------------------------------
377 css::uno::Reference< css::uno::XInterface > xCfg;
380 xCfg = ::comphelper::ConfigurationHelper::openConfig(
381 xSMGR,
382 CFGPATH_FACTORIES,
383 ::comphelper::ConfigurationHelper::E_READONLY);
385 catch(const css::uno::RuntimeException& exRun)
386 { throw exRun; }
387 catch(const css::uno::Exception&)
388 { xCfg.clear(); }
390 // SAFE -> ----------------------------------
391 WriteGuard aWriteLock(m_aLock);
392 m_xCFG = css::uno::Reference< css::container::XNameAccess >(xCfg, css::uno::UNO_QUERY_THROW);
393 return m_xCFG;
394 // <- SAFE ----------------------------------
397 /*-----------------------------------------------
398 30.01.2004 07:54
399 -----------------------------------------------*/
400 ::rtl::OUString ModuleManager::implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent)
402 // Search for an optional (!) interface XModule first.
403 // Its used to overrule an existing service name. Used e.g. by our database form designer
404 // which uses a writer module internaly.
405 css::uno::Reference< css::frame::XModule > xModule(xComponent, css::uno::UNO_QUERY);
406 if (xModule.is())
407 return xModule->getIdentifier();
409 // detect modules in a generic way ...
410 // comparing service names with configured entries ...
411 css::uno::Reference< css::lang::XServiceInfo > xInfo(xComponent, css::uno::UNO_QUERY);
412 if (!xInfo.is())
413 return ::rtl::OUString();
415 const css::uno::Sequence< ::rtl::OUString > lKnownModules = getElementNames();
416 const ::rtl::OUString* pKnownModules = lKnownModules.getConstArray();
417 sal_Int32 c = lKnownModules.getLength();
418 sal_Int32 i = 0;
420 for (i=0; i<c; ++i)
422 if (xInfo->supportsService(pKnownModules[i]))
423 return pKnownModules[i];
426 return ::rtl::OUString();
429 } // namespace framework