merge the formfield patch from ooo-build
[ooovba.git] / framework / source / services / modulemanager.cxx
blob2bb57c30adc5ae1ba192963aa17a97e45f19a189
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
56 #include <rtl/logfile.hxx>
58 namespace framework
61 static const ::rtl::OUString CFGPATH_FACTORIES = ::rtl::OUString::createFromAscii("/org.openoffice.Setup/Office/Factories");
62 static const ::rtl::OUString MODULEPROP_IDENTIFIER = ::rtl::OUString::createFromAscii("ooSetupFactoryModuleIdentifier" );
64 /*-----------------------------------------------
65 04.12.2003 09:32
66 -----------------------------------------------*/
67 DEFINE_XINTERFACE_7(ModuleManager ,
68 OWeakObject ,
69 DIRECT_INTERFACE(css::lang::XTypeProvider ),
70 DIRECT_INTERFACE(css::lang::XServiceInfo ),
71 DIRECT_INTERFACE(css::container::XNameReplace ),
72 DIRECT_INTERFACE(css::container::XNameAccess ),
73 DIRECT_INTERFACE(css::container::XElementAccess ),
74 DIRECT_INTERFACE(css::container::XContainerQuery),
75 DIRECT_INTERFACE(css::frame::XModuleManager ))
77 /*-----------------------------------------------
78 04.12.2003 09:32
79 -----------------------------------------------*/
80 DEFINE_XTYPEPROVIDER_7(ModuleManager ,
81 css::lang::XTypeProvider ,
82 css::lang::XServiceInfo ,
83 css::container::XNameReplace ,
84 css::container::XNameAccess ,
85 css::container::XElementAccess ,
86 css::container::XContainerQuery,
87 css::frame::XModuleManager )
89 /*-----------------------------------------------
90 04.12.2003 09:35
91 -----------------------------------------------*/
92 DEFINE_XSERVICEINFO_ONEINSTANCESERVICE(ModuleManager ,
93 ::cppu::OWeakObject ,
94 SERVICENAME_MODULEMANAGER ,
95 IMPLEMENTATIONNAME_MODULEMANAGER)
97 /*-----------------------------------------------
98 04.12.2003 09:35
99 -----------------------------------------------*/
100 DEFINE_INIT_SERVICE(
101 ModuleManager,
103 /*Attention
104 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
105 to create a new instance of this class by our own supported service factory.
106 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations!
111 /*-----------------------------------------------
112 04.12.2003 09:30
113 -----------------------------------------------*/
114 ModuleManager::ModuleManager(const css::uno::Reference< css::lang::XMultiServiceFactory >& xSMGR)
115 : ThreadHelpBase( )
116 , m_xSMGR (xSMGR)
120 /*-----------------------------------------------
121 10.12.2003 11:59
122 -----------------------------------------------*/
123 ModuleManager::~ModuleManager()
125 if (m_xCFG.is())
126 m_xCFG.clear();
129 /*-----------------------------------------------
130 10.12.2003 11:02
131 -----------------------------------------------*/
132 ::rtl::OUString SAL_CALL ModuleManager::identify(const css::uno::Reference< css::uno::XInterface >& xModule)
133 throw(css::lang::IllegalArgumentException,
134 css::frame::UnknownModuleException,
135 css::uno::RuntimeException )
137 // valid parameter?
138 css::uno::Reference< css::frame::XFrame > xFrame (xModule, css::uno::UNO_QUERY);
139 css::uno::Reference< css::awt::XWindow > xWindow (xModule, css::uno::UNO_QUERY);
140 css::uno::Reference< css::frame::XController > xController(xModule, css::uno::UNO_QUERY);
141 css::uno::Reference< css::frame::XModel > xModel (xModule, css::uno::UNO_QUERY);
143 if (
144 (!xFrame.is() ) &&
145 (!xWindow.is() ) &&
146 (!xController.is()) &&
147 (!xModel.is() )
150 throw css::lang::IllegalArgumentException(
151 ::rtl::OUString::createFromAscii("Given module is not a frame nor a window, controller or model."),
152 static_cast< ::cppu::OWeakObject* >(this),
156 if (xFrame.is())
158 xController = xFrame->getController();
159 xWindow = xFrame->getComponentWindow();
161 if (xController.is())
162 xModel = xController->getModel();
164 // modules are implemented by the deepest component in hierarchy ...
165 // Means: model -> controller -> window
166 // No fallbacks to higher components are allowed !
167 // Note : A frame provides access to module components only ... but it's not a module by himself.
169 ::rtl::OUString sModule;
170 if (xModel.is())
171 sModule = implts_identify(xModel);
172 else
173 if (xController.is())
174 sModule = implts_identify(xController);
175 else
176 if (xWindow.is())
177 sModule = implts_identify(xWindow);
179 if (sModule.getLength() < 1)
180 throw css::frame::UnknownModuleException(
181 ::rtl::OUString::createFromAscii("Cant find suitable module for the given component."),
182 static_cast< ::cppu::OWeakObject* >(this));
184 return sModule;
187 /*-----------------------------------------------
188 08.03.2007 09:55
189 -----------------------------------------------*/
190 void SAL_CALL ModuleManager::replaceByName(const ::rtl::OUString& sName ,
191 const css::uno::Any& aValue)
192 throw (css::lang::IllegalArgumentException ,
193 css::container::NoSuchElementException,
194 css::lang::WrappedTargetException ,
195 css::uno::RuntimeException )
197 ::comphelper::SequenceAsHashMap lProps(aValue);
198 if (lProps.empty() )
200 throw css::lang::IllegalArgumentException(
201 ::rtl::OUString::createFromAscii("No properties given to replace part of module."),
202 static_cast< css::container::XNameAccess* >(this),
206 // SAFE -> ----------------------------------
207 ReadGuard aReadLock(m_aLock);
208 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
209 aReadLock.unlock();
210 // <- SAFE ----------------------------------
212 // get access to the element
213 // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
214 // it cache it as a member of this module manager instance. If we change some props there ... but dont
215 // flush changes (because an error occured) we will read them later. If we use a different config access
216 // we can close it without a flush ... and our read data wont be affected .-)
217 css::uno::Reference< css::uno::XInterface > xCfg = ::comphelper::ConfigurationHelper::openConfig(
218 xSMGR,
219 CFGPATH_FACTORIES,
220 ::comphelper::ConfigurationHelper::E_STANDARD);
221 css::uno::Reference< css::container::XNameAccess > xModules (xCfg, css::uno::UNO_QUERY_THROW);
222 css::uno::Reference< css::container::XNameReplace > xModule ;
224 xModules->getByName(sName) >>= xModule;
225 if (!xModule.is())
227 throw css::uno::RuntimeException(
228 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
229 static_cast< css::container::XNameAccess* >(this));
232 ::comphelper::SequenceAsHashMap::const_iterator pProp;
233 for ( pProp = lProps.begin();
234 pProp != lProps.end() ;
235 ++pProp )
237 const ::rtl::OUString& sPropName = pProp->first;
238 const css::uno::Any& aPropValue = pProp->second;
240 // let "NoSuchElementException" out ! We support the same API ...
241 // and without a flush() at the end all changed data before will be ignored !
242 xModule->replaceByName(sPropName, aPropValue);
245 ::comphelper::ConfigurationHelper::flush(xCfg);
248 /*-----------------------------------------------
249 10.12.2003 12:05
250 -----------------------------------------------*/
251 css::uno::Any SAL_CALL ModuleManager::getByName(const ::rtl::OUString& sName)
252 throw(css::container::NoSuchElementException,
253 css::lang::WrappedTargetException ,
254 css::uno::RuntimeException )
256 // get access to the element
257 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
258 css::uno::Reference< css::container::XNameAccess > xModule;
259 xCFG->getByName(sName) >>= xModule;
260 if (!xModule.is())
262 throw css::uno::RuntimeException(
263 ::rtl::OUString::createFromAscii("Was not able to get write access to the requested module entry inside configuration."),
264 static_cast< css::container::XNameAccess* >(this));
267 // convert it to seq< PropertyValue >
268 const css::uno::Sequence< ::rtl::OUString > lPropNames = xModule->getElementNames();
269 ::comphelper::SequenceAsHashMap lProps ;
270 sal_Int32 c = lPropNames.getLength();
271 sal_Int32 i = 0;
273 lProps[MODULEPROP_IDENTIFIER] <<= sName;
274 for (i=0; i<c; ++i)
276 const ::rtl::OUString& sPropName = lPropNames[i];
277 lProps[sPropName] = xModule->getByName(sPropName);
280 return css::uno::makeAny(lProps.getAsConstPropertyValueList());
283 /*-----------------------------------------------
284 10.12.2003 11:58
285 -----------------------------------------------*/
286 css::uno::Sequence< ::rtl::OUString > SAL_CALL ModuleManager::getElementNames()
287 throw(css::uno::RuntimeException)
289 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
290 return xCFG->getElementNames();
293 /*-----------------------------------------------
294 10.12.2003 11:57
295 -----------------------------------------------*/
296 sal_Bool SAL_CALL ModuleManager::hasByName(const ::rtl::OUString& sName)
297 throw(css::uno::RuntimeException)
299 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
300 return xCFG->hasByName(sName);
303 /*-----------------------------------------------
304 10.12.2003 11:35
305 -----------------------------------------------*/
306 css::uno::Type SAL_CALL ModuleManager::getElementType()
307 throw(css::uno::RuntimeException)
309 return ::getCppuType((const css::uno::Sequence< css::beans::PropertyValue >*)0);
312 /*-----------------------------------------------
313 10.12.2003 11:56
314 -----------------------------------------------*/
315 sal_Bool SAL_CALL ModuleManager::hasElements()
316 throw(css::uno::RuntimeException)
318 css::uno::Reference< css::container::XNameAccess > xCFG = implts_getConfig();
319 return xCFG->hasElements();
322 /*-----------------------------------------------
323 07.03.2007 12:55
324 -----------------------------------------------*/
325 css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByQuery(const ::rtl::OUString&)
326 throw(css::uno::RuntimeException)
328 return css::uno::Reference< css::container::XEnumeration >();
331 /*-----------------------------------------------
332 07.03.2007 12:55
333 -----------------------------------------------*/
334 css::uno::Reference< css::container::XEnumeration > SAL_CALL ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence< css::beans::NamedValue >& lProperties)
335 throw(css::uno::RuntimeException)
337 ::comphelper::SequenceAsHashMap lSearchProps (lProperties);
338 css::uno::Sequence< ::rtl::OUString > lModules = getElementNames();
339 sal_Int32 c = lModules.getLength();
340 sal_Int32 i = 0;
341 ::comphelper::SequenceAsVector< css::uno::Any > lResult ;
343 for (i=0; i<c; ++i)
347 const ::rtl::OUString& sModule = lModules[i];
348 ::comphelper::SequenceAsHashMap lModuleProps = getByName(sModule);
350 if (lModuleProps.match(lSearchProps))
351 lResult.push_back(css::uno::makeAny(lModuleProps.getAsConstPropertyValueList()));
353 catch(const css::uno::Exception&)
357 ::comphelper::OAnyEnumeration* pEnum = new ::comphelper::OAnyEnumeration(lResult.getAsConstList());
358 css::uno::Reference< css::container::XEnumeration > xEnum(static_cast< css::container::XEnumeration* >(pEnum), css::uno::UNO_QUERY_THROW);
359 return xEnum;
362 /*-----------------------------------------------
363 14.12.2003 09:45
364 -----------------------------------------------*/
365 css::uno::Reference< css::container::XNameAccess > ModuleManager::implts_getConfig()
366 throw(css::uno::RuntimeException)
368 // SAFE -> ----------------------------------
369 ReadGuard aReadLock(m_aLock);
370 if (m_xCFG.is())
371 return m_xCFG;
372 css::uno::Reference< css::lang::XMultiServiceFactory > xSMGR = m_xSMGR;
373 aReadLock.unlock();
374 // <- SAFE ----------------------------------
376 css::uno::Reference< css::uno::XInterface > xCfg;
379 xCfg = ::comphelper::ConfigurationHelper::openConfig(
380 xSMGR,
381 CFGPATH_FACTORIES,
382 ::comphelper::ConfigurationHelper::E_READONLY);
384 catch(const css::uno::RuntimeException& exRun)
385 { throw exRun; }
386 catch(const css::uno::Exception&)
387 { xCfg.clear(); }
389 // SAFE -> ----------------------------------
390 WriteGuard aWriteLock(m_aLock);
391 m_xCFG = css::uno::Reference< css::container::XNameAccess >(xCfg, css::uno::UNO_QUERY_THROW);
392 return m_xCFG;
393 // <- SAFE ----------------------------------
396 /*-----------------------------------------------
397 30.01.2004 07:54
398 -----------------------------------------------*/
399 ::rtl::OUString ModuleManager::implts_identify(const css::uno::Reference< css::uno::XInterface >& xComponent)
401 // Search for an optional (!) interface XModule first.
402 // Its used to overrule an existing service name. Used e.g. by our database form designer
403 // which uses a writer module internaly.
404 css::uno::Reference< css::frame::XModule > xModule(xComponent, css::uno::UNO_QUERY);
405 if (xModule.is())
406 return xModule->getIdentifier();
408 // detect modules in a generic way ...
409 // comparing service names with configured entries ...
410 css::uno::Reference< css::lang::XServiceInfo > xInfo(xComponent, css::uno::UNO_QUERY);
411 if (!xInfo.is())
412 return ::rtl::OUString();
414 const css::uno::Sequence< ::rtl::OUString > lKnownModules = getElementNames();
415 const ::rtl::OUString* pKnownModules = lKnownModules.getConstArray();
416 sal_Int32 c = lKnownModules.getLength();
417 sal_Int32 i = 0;
419 for (i=0; i<c; ++i)
421 if (xInfo->supportsService(pKnownModules[i]))
422 return pKnownModules[i];
425 return ::rtl::OUString();
428 } // namespace framework