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 .
21 #include "services/modulemanager.hxx"
22 #include "services/frame.hxx"
24 #include <threadhelp/readguard.hxx>
25 #include <threadhelp/writeguard.hxx>
28 #include <com/sun/star/frame/XFrame.hpp>
29 #include <com/sun/star/frame/XController.hpp>
30 #include <com/sun/star/frame/XModel.hpp>
31 #include <com/sun/star/frame/XModule.hpp>
32 #include <comphelper/configurationhelper.hxx>
33 #include <comphelper/sequenceashashmap.hxx>
34 #include <comphelper/sequenceasvector.hxx>
35 #include <comphelper/enumhelper.hxx>
37 #include <rtl/logfile.hxx>
42 static const char CFGPATH_FACTORIES
[] = "/org.openoffice.Setup/Office/Factories";
43 static const char MODULEPROP_IDENTIFIER
[] = "ooSetupFactoryModuleIdentifier";
45 OUString
ModuleManager::impl_getStaticImplementationName() {
46 return IMPLEMENTATIONNAME_MODULEMANAGER
;
49 css::uno::Reference
< css::lang::XSingleServiceFactory
>
50 ModuleManager::impl_createFactory(
51 css::uno::Reference
< css::lang::XMultiServiceFactory
> const & manager
)
53 return cppu::createSingleFactory(
54 manager
, impl_getStaticImplementationName(), &impl_createInstance
,
55 impl_getSupportedServiceNames());
58 css::uno::Sequence
< OUString
>
59 ModuleManager::impl_getSupportedServiceNames() {
60 css::uno::Sequence
< OUString
> s(1);
61 s
[0] = "com.sun.star.frame.ModuleManager";
65 css::uno::Reference
< css::uno::XInterface
> ModuleManager::impl_createInstance(
66 css::uno::Reference
< css::lang::XMultiServiceFactory
> const & manager
)
68 return static_cast< cppu::OWeakObject
* >(new ModuleManager(manager
));
71 ModuleManager::ModuleManager(const css::uno::Reference
< css::lang::XMultiServiceFactory
>& xSMGR
)
77 ModuleManager::~ModuleManager()
83 OUString
ModuleManager::getImplementationName()
84 throw (css::uno::RuntimeException
)
86 return impl_getStaticImplementationName();
89 sal_Bool
ModuleManager::supportsService(OUString
const & ServiceName
)
90 throw (css::uno::RuntimeException
)
92 css::uno::Sequence
< OUString
> s(getSupportedServiceNames());
93 for (sal_Int32 i
= 0; i
!= s
.getLength(); ++i
) {
94 if (s
[i
] == ServiceName
) {
101 css::uno::Sequence
< OUString
> ModuleManager::getSupportedServiceNames()
102 throw (css::uno::RuntimeException
)
104 return impl_getSupportedServiceNames();
107 OUString SAL_CALL
ModuleManager::identify(const css::uno::Reference
< css::uno::XInterface
>& xModule
)
108 throw(css::lang::IllegalArgumentException
,
109 css::frame::UnknownModuleException
,
110 css::uno::RuntimeException
)
113 css::uno::Reference
< css::frame::XFrame
> xFrame (xModule
, css::uno::UNO_QUERY
);
114 css::uno::Reference
< css::awt::XWindow
> xWindow (xModule
, css::uno::UNO_QUERY
);
115 css::uno::Reference
< css::frame::XController
> xController(xModule
, css::uno::UNO_QUERY
);
116 css::uno::Reference
< css::frame::XModel
> xModel (xModule
, css::uno::UNO_QUERY
);
121 (!xController
.is()) &&
125 throw css::lang::IllegalArgumentException(
126 OUString("Given module is not a frame nor a window, controller or model."),
127 static_cast< ::cppu::OWeakObject
* >(this),
133 xController
= xFrame
->getController();
134 xWindow
= xFrame
->getComponentWindow();
136 if (xController
.is())
137 xModel
= xController
->getModel();
139 // modules are implemented by the deepest component in hierarchy ...
140 // Means: model -> controller -> window
141 // No fallbacks to higher components are allowed !
142 // Note : A frame provides access to module components only ... but it's not a module by himself.
146 sModule
= implts_identify(xModel
);
147 else if (xController
.is())
148 sModule
= implts_identify(xController
);
149 else if (xWindow
.is())
150 sModule
= implts_identify(xWindow
);
152 if (sModule
.isEmpty())
153 throw css::frame::UnknownModuleException(
154 OUString("Cant find suitable module for the given component."),
155 static_cast< ::cppu::OWeakObject
* >(this));
160 void SAL_CALL
ModuleManager::replaceByName(const OUString
& sName
,
161 const css::uno::Any
& aValue
)
162 throw (css::lang::IllegalArgumentException
,
163 css::container::NoSuchElementException
,
164 css::lang::WrappedTargetException
,
165 css::uno::RuntimeException
)
167 ::comphelper::SequenceAsHashMap
lProps(aValue
);
170 throw css::lang::IllegalArgumentException(
171 OUString("No properties given to replace part of module."),
172 static_cast< cppu::OWeakObject
* >(this),
176 // SAFE -> ----------------------------------
177 ReadGuard
aReadLock(m_aLock
);
178 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
180 // <- SAFE ----------------------------------
182 // get access to the element
183 // Note: Dont use impl_getConfig() method here. Because it creates a readonly access only, further
184 // it cache it as a member of this module manager instance. If we change some props there ... but dont
185 // flush changes (because an error occurred) we will read them later. If we use a different config access
186 // we can close it without a flush ... and our read data wont be affected .-)
187 css::uno::Reference
< css::uno::XInterface
> xCfg
= ::comphelper::ConfigurationHelper::openConfig(
188 comphelper::getComponentContext(xSMGR
),
189 OUString(CFGPATH_FACTORIES
),
190 ::comphelper::ConfigurationHelper::E_STANDARD
);
191 css::uno::Reference
< css::container::XNameAccess
> xModules (xCfg
, css::uno::UNO_QUERY_THROW
);
192 css::uno::Reference
< css::container::XNameReplace
> xModule
;
194 xModules
->getByName(sName
) >>= xModule
;
197 throw css::uno::RuntimeException(
198 OUString("Was not able to get write access to the requested module entry inside configuration."),
199 static_cast< cppu::OWeakObject
* >(this));
202 ::comphelper::SequenceAsHashMap::const_iterator pProp
;
203 for ( pProp
= lProps
.begin();
204 pProp
!= lProps
.end() ;
207 const OUString
& sPropName
= pProp
->first
;
208 const css::uno::Any
& aPropValue
= pProp
->second
;
210 // let "NoSuchElementException" out ! We support the same API ...
211 // and without a flush() at the end all changed data before will be ignored !
212 xModule
->replaceByName(sPropName
, aPropValue
);
215 ::comphelper::ConfigurationHelper::flush(xCfg
);
218 css::uno::Any SAL_CALL
ModuleManager::getByName(const OUString
& sName
)
219 throw(css::container::NoSuchElementException
,
220 css::lang::WrappedTargetException
,
221 css::uno::RuntimeException
)
223 // get access to the element
224 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
225 css::uno::Reference
< css::container::XNameAccess
> xModule
;
226 xCFG
->getByName(sName
) >>= xModule
;
229 throw css::uno::RuntimeException(
230 OUString("Was not able to get write access to the requested module entry inside configuration."),
231 static_cast< cppu::OWeakObject
* >(this));
234 // convert it to seq< PropertyValue >
235 const css::uno::Sequence
< OUString
> lPropNames
= xModule
->getElementNames();
236 ::comphelper::SequenceAsHashMap lProps
;
237 sal_Int32 c
= lPropNames
.getLength();
240 lProps
[OUString(MODULEPROP_IDENTIFIER
)] <<= sName
;
243 const OUString
& sPropName
= lPropNames
[i
];
244 lProps
[sPropName
] = xModule
->getByName(sPropName
);
247 return css::uno::makeAny(lProps
.getAsConstPropertyValueList());
250 css::uno::Sequence
< OUString
> SAL_CALL
ModuleManager::getElementNames()
251 throw(css::uno::RuntimeException
)
253 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
254 return xCFG
->getElementNames();
257 sal_Bool SAL_CALL
ModuleManager::hasByName(const OUString
& sName
)
258 throw(css::uno::RuntimeException
)
260 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
261 return xCFG
->hasByName(sName
);
264 css::uno::Type SAL_CALL
ModuleManager::getElementType()
265 throw(css::uno::RuntimeException
)
267 return ::getCppuType((const css::uno::Sequence
< css::beans::PropertyValue
>*)0);
270 sal_Bool SAL_CALL
ModuleManager::hasElements()
271 throw(css::uno::RuntimeException
)
273 css::uno::Reference
< css::container::XNameAccess
> xCFG
= implts_getConfig();
274 return xCFG
->hasElements();
277 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
ModuleManager::createSubSetEnumerationByQuery(const OUString
&)
278 throw(css::uno::RuntimeException
)
280 return css::uno::Reference
< css::container::XEnumeration
>();
283 css::uno::Reference
< css::container::XEnumeration
> SAL_CALL
ModuleManager::createSubSetEnumerationByProperties(const css::uno::Sequence
< css::beans::NamedValue
>& lProperties
)
284 throw(css::uno::RuntimeException
)
286 ::comphelper::SequenceAsHashMap
lSearchProps (lProperties
);
287 css::uno::Sequence
< OUString
> lModules
= getElementNames();
288 sal_Int32 c
= lModules
.getLength();
290 ::comphelper::SequenceAsVector
< css::uno::Any
> lResult
;
296 const OUString
& sModule
= lModules
[i
];
297 ::comphelper::SequenceAsHashMap lModuleProps
= getByName(sModule
);
299 if (lModuleProps
.match(lSearchProps
))
300 lResult
.push_back(css::uno::makeAny(lModuleProps
.getAsConstPropertyValueList()));
302 catch(const css::uno::Exception
&)
307 ::comphelper::OAnyEnumeration
* pEnum
= new ::comphelper::OAnyEnumeration(lResult
.getAsConstList());
308 css::uno::Reference
< css::container::XEnumeration
> xEnum(static_cast< css::container::XEnumeration
* >(pEnum
), css::uno::UNO_QUERY_THROW
);
312 css::uno::Reference
< css::container::XNameAccess
> ModuleManager::implts_getConfig()
313 throw(css::uno::RuntimeException
)
315 // SAFE -> ----------------------------------
316 ReadGuard
aReadLock(m_aLock
);
319 css::uno::Reference
< css::lang::XMultiServiceFactory
> xSMGR
= m_xSMGR
;
321 // <- SAFE ----------------------------------
323 css::uno::Reference
< css::uno::XInterface
> xCfg
;
326 xCfg
= ::comphelper::ConfigurationHelper::openConfig(
327 comphelper::getComponentContext(xSMGR
),
328 OUString(CFGPATH_FACTORIES
),
329 ::comphelper::ConfigurationHelper::E_READONLY
);
331 catch(const css::uno::RuntimeException
&)
335 catch(const css::uno::Exception
&)
340 // SAFE -> ----------------------------------
341 WriteGuard
aWriteLock(m_aLock
);
342 m_xCFG
= css::uno::Reference
< css::container::XNameAccess
>(xCfg
, css::uno::UNO_QUERY_THROW
);
344 // <- SAFE ----------------------------------
347 OUString
ModuleManager::implts_identify(const css::uno::Reference
< css::uno::XInterface
>& xComponent
)
349 // Search for an optional (!) interface XModule first.
350 // Its used to overrule an existing service name. Used e.g. by our database form designer
351 // which uses a writer module internaly.
352 css::uno::Reference
< css::frame::XModule
> xModule(xComponent
, css::uno::UNO_QUERY
);
354 return xModule
->getIdentifier();
356 // detect modules in a generic way ...
357 // comparing service names with configured entries ...
358 css::uno::Reference
< css::lang::XServiceInfo
> xInfo(xComponent
, css::uno::UNO_QUERY
);
362 const css::uno::Sequence
< OUString
> lKnownModules
= getElementNames();
363 const OUString
* pKnownModules
= lKnownModules
.getConstArray();
364 sal_Int32 c
= lKnownModules
.getLength();
369 if (xInfo
->supportsService(pKnownModules
[i
]))
370 return pKnownModules
[i
];
376 } // namespace framework
378 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */