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 .
20 #include <sal/config.h>
24 #include <com/sun/star/beans/NamedValue.hpp>
25 #include <com/sun/star/beans/PropertyValue.hpp>
26 #include <com/sun/star/configuration/theDefaultProvider.hpp>
27 #include <com/sun/star/lang/EventObject.hpp>
28 #include <com/sun/star/lang/Locale.hpp>
29 #include <com/sun/star/lang/XLocalizable.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/uno/Any.hxx>
33 #include <com/sun/star/uno/Exception.hpp>
34 #include <com/sun/star/uno/Reference.hxx>
35 #include <com/sun/star/uno/Sequence.hxx>
36 #include <com/sun/star/uno/XInterface.hpp>
37 #include <com/sun/star/util/XFlushListener.hpp>
38 #include <com/sun/star/util/XFlushable.hpp>
39 #include <com/sun/star/util/XRefreshListener.hpp>
40 #include <com/sun/star/util/XRefreshable.hpp>
41 #include <cppu/unotype.hxx>
42 #include <cppuhelper/basemutex.hxx>
43 #include <cppuhelper/compbase.hxx>
44 #include <cppuhelper/supportsservice.hxx>
45 #include <cppuhelper/weak.hxx>
46 #include <osl/mutex.hxx>
47 #include <sal/types.h>
48 #include <rtl/ref.hxx>
49 #include <rtl/ustring.hxx>
51 #include <i18nlangtag/languagetag.hxx>
53 #include "components.hxx"
54 #include "configurationprovider.hxx"
56 #include "defaultprovider.hxx"
57 #include "rootaccess.hxx"
59 namespace configmgr::configuration_provider
{
63 constexpr OUStringLiteral accessServiceName
=
64 u
"com.sun.star.configuration.ConfigurationAccess";
65 constexpr OUStringLiteral updateAccessServiceName
=
66 u
"com.sun.star.configuration.ConfigurationUpdateAccess";
69 throw css::uno::Exception(
70 ("com.sun.star.configuration.ConfigurationProvider expects a single,"
71 " non-empty, string nodepath argument"),
76 cppu::WeakComponentImplHelper
<
77 css::lang::XServiceInfo
, css::lang::XMultiServiceFactory
,
78 css::util::XRefreshable
, css::util::XFlushable
,
79 css::lang::XLocalizable
>
83 private cppu::BaseMutex
, public ServiceBase
87 const css::uno::Reference
< css::uno::XComponentContext
>& context
):
88 ServiceBase(m_aMutex
), context_(context
), default_(true),
95 const css::uno::Reference
< css::uno::XComponentContext
>& context
,
96 OUString
const & locale
):
97 ServiceBase(m_aMutex
), context_(context
), locale_(locale
),
101 assert(context
.is());
105 Service(const Service
&) = delete;
106 Service
& operator=(const Service
&) = delete;
108 virtual ~Service() override
{}
110 virtual void SAL_CALL
disposing() override
{ flushModifications(); }
112 virtual OUString SAL_CALL
getImplementationName() override
115 ? default_provider::getImplementationName()
116 : "com.sun.star.comp.configuration.ConfigurationProvider";
119 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
120 { return cppu::supportsService(this, ServiceName
); }
122 virtual css::uno::Sequence
< OUString
> SAL_CALL
123 getSupportedServiceNames() override
126 ? default_provider::getSupportedServiceNames()
127 : css::uno::Sequence
<OUString
> { "com.sun.star.configuration.ConfigurationProvider" };
130 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
createInstance(
131 OUString
const & aServiceSpecifier
) override
;
133 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
134 createInstanceWithArguments(
135 OUString
const & ServiceSpecifier
,
136 css::uno::Sequence
< css::uno::Any
> const & Arguments
) override
;
138 virtual css::uno::Sequence
< OUString
> SAL_CALL
139 getAvailableServiceNames() override
;
141 virtual void SAL_CALL
refresh() override
;
143 virtual void SAL_CALL
addRefreshListener(
144 css::uno::Reference
< css::util::XRefreshListener
> const & l
) override
;
146 virtual void SAL_CALL
removeRefreshListener(
147 css::uno::Reference
< css::util::XRefreshListener
> const & l
) override
;
149 virtual void SAL_CALL
flush() override
;
151 virtual void SAL_CALL
addFlushListener(
152 css::uno::Reference
< css::util::XFlushListener
> const & l
) override
;
154 virtual void SAL_CALL
removeFlushListener(
155 css::uno::Reference
< css::util::XFlushListener
> const & l
) override
;
157 virtual void SAL_CALL
setLocale(css::lang::Locale
const & eLocale
) override
;
159 virtual css::lang::Locale SAL_CALL
getLocale() override
;
161 void flushModifications() const;
163 css::uno::Reference
< css::uno::XComponentContext
> context_
;
166 std::shared_ptr
<osl::Mutex
> lock_
;
169 css::uno::Reference
< css::uno::XInterface
> Service::createInstance(
170 OUString
const & aServiceSpecifier
)
172 return createInstanceWithArguments(
173 aServiceSpecifier
, css::uno::Sequence
< css::uno::Any
>());
176 css::uno::Reference
< css::uno::XInterface
>
177 Service::createInstanceWithArguments(
178 OUString
const & ServiceSpecifier
,
179 css::uno::Sequence
< css::uno::Any
> const & Arguments
)
183 for (sal_Int32 i
= 0; i
< Arguments
.getLength(); ++i
) {
184 css::beans::NamedValue v1
;
185 css::beans::PropertyValue v2
;
188 if (Arguments
[i
] >>= v1
) {
191 } else if (Arguments
[i
] >>= v2
) {
194 } else if (Arguments
.getLength() == 1 && (Arguments
[i
] >>= nodepath
)) {
195 // For backwards compatibility, allow a single string argument that
197 if (nodepath
.isEmpty()) {
202 throw css::uno::Exception(
203 ("com.sun.star.configuration.ConfigurationProvider expects"
204 " NamedValue or PropertyValue arguments"),
207 // For backwards compatibility, allow "nodepath" and "Locale" in any
209 if (name
.equalsIgnoreAsciiCase("nodepath")) {
210 if (!nodepath
.isEmpty() || !(value
>>= nodepath
) ||
215 } else if (name
.equalsIgnoreAsciiCase("locale")) {
216 if (!locale
.isEmpty() || !(value
>>= locale
) ||
219 throw css::uno::Exception(
220 ("com.sun.star.configuration.ConfigurationProvider expects"
221 " at most one, non-empty, string Locale argument"),
226 if (nodepath
.isEmpty()) {
229 // For backwards compatibility, allow a nodepath that misses the leading
231 if (nodepath
[0] != '/') {
232 nodepath
= "/" + nodepath
;
234 if (locale
.isEmpty()) {
235 //TODO: should the Access use the dynamically changing locale_ instead?
237 if (locale
.isEmpty()) {
242 if (ServiceSpecifier
== accessServiceName
) {
244 } else if (ServiceSpecifier
== updateAccessServiceName
) {
247 throw css::uno::Exception(
248 ("com.sun.star.configuration.ConfigurationProvider does not support"
249 " service " + ServiceSpecifier
),
250 static_cast< cppu::OWeakObject
* >(this));
252 osl::MutexGuard
guard(*lock_
);
253 Components
& components
= Components::getSingleton(context_
);
255 new RootAccess(components
, nodepath
, locale
, update
));
256 if (root
->isValue()) {
257 throw css::uno::Exception(
258 ("com.sun.star.configuration.ConfigurationProvider: there is a leaf"
259 " value at nodepath " + nodepath
),
260 static_cast< cppu::OWeakObject
* >(this));
262 components
.addRootAccess(root
);
263 return static_cast< cppu::OWeakObject
* >(root
.get());
266 css::uno::Sequence
< OUString
> Service::getAvailableServiceNames()
268 return { accessServiceName
, updateAccessServiceName
};
271 void Service::refresh() {
273 cppu::OInterfaceContainerHelper
* cont
= rBHelper
.getContainer(
274 cppu::UnoType
< css::util::XRefreshListener
>::get());
275 if (cont
!= nullptr) {
276 css::lang::EventObject
ev(static_cast< cppu::OWeakObject
* >(this));
277 cont
->notifyEach(&css::util::XRefreshListener::refreshed
, ev
);
281 void Service::addRefreshListener(
282 css::uno::Reference
< css::util::XRefreshListener
> const & l
)
284 rBHelper
.addListener(
285 cppu::UnoType
< css::util::XRefreshListener
>::get(), l
);
288 void Service::removeRefreshListener(
289 css::uno::Reference
< css::util::XRefreshListener
> const & l
)
291 rBHelper
.removeListener(
292 cppu::UnoType
< css::util::XRefreshListener
>::get(), l
);
295 void Service::flush() {
296 flushModifications();
297 cppu::OInterfaceContainerHelper
* cont
= rBHelper
.getContainer(
298 cppu::UnoType
< css::util::XFlushListener
>::get());
299 if (cont
!= nullptr) {
300 css::lang::EventObject
ev(static_cast< cppu::OWeakObject
* >(this));
301 cont
->notifyEach(&css::util::XFlushListener::flushed
, ev
);
305 void Service::addFlushListener(
306 css::uno::Reference
< css::util::XFlushListener
> const & l
)
308 rBHelper
.addListener(cppu::UnoType
< css::util::XFlushListener
>::get(), l
);
311 void Service::removeFlushListener(
312 css::uno::Reference
< css::util::XFlushListener
> const & l
)
314 rBHelper
.removeListener(
315 cppu::UnoType
< css::util::XFlushListener
>::get(), l
);
318 void Service::setLocale(css::lang::Locale
const & eLocale
)
320 osl::MutexGuard
guard(*lock_
);
321 locale_
= LanguageTag::convertToBcp47( eLocale
, false);
324 css::lang::Locale
Service::getLocale() {
325 osl::MutexGuard
guard(*lock_
);
326 css::lang::Locale loc
;
327 if (! locale_
.isEmpty()) {
328 loc
= LanguageTag::convertToLocale( locale_
, false);
333 void Service::flushModifications() const {
334 Components
* components
;
336 osl::MutexGuard
guard(*lock_
);
337 components
= &Components::getSingleton(context_
);
339 components
->flushModifications();
342 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
343 com_sun_star_comp_configuration_ConfigurationProvider_get_implementation(
344 css::uno::XComponentContext
* Context
, css::uno::Sequence
<css::uno::Any
> const& Arguments
)
346 if (!Arguments
.hasElements()) {
347 auto p
= css::configuration::theDefaultProvider::get(Context
);
352 for (sal_Int32 i
= 0; i
< Arguments
.getLength(); ++i
) {
353 css::beans::NamedValue v1
;
354 css::beans::PropertyValue v2
;
357 if (Arguments
[i
] >>= v1
) {
360 } else if (Arguments
[i
] >>= v2
) {
364 throw css::uno::Exception(
365 ("com.sun.star.configuration.ConfigurationProvider factory"
366 " expects NamedValue or PropertyValue arguments"),
369 // For backwards compatibility, allow "Locale" and (ignored)
370 // "EnableAsync" in any case:
371 if (name
.equalsIgnoreAsciiCase("locale")) {
372 if (!locale
.isEmpty() || !(value
>>= locale
) ||
375 throw css::uno::Exception(
376 ("com.sun.star.configuration.ConfigurationProvider"
377 " factory expects at most one, non-empty, string"
381 } else if (!name
.equalsIgnoreAsciiCase("enableasync")) {
382 throw css::uno::Exception(
383 ("com.sun.star.configuration.ConfigurationProvider factory:"
384 " unknown argument " + name
),
388 return cppu::acquire(new Service(Context
, locale
));
394 css::uno::Reference
< css::uno::XInterface
> createDefault(
395 css::uno::Reference
< css::uno::XComponentContext
> const & context
)
397 return static_cast< cppu::OWeakObject
* >(new Service(context
));
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */