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 <comphelper/compbase.hxx>
43 #include <cppuhelper/supportsservice.hxx>
44 #include <cppuhelper/weak.hxx>
45 #include <osl/mutex.hxx>
46 #include <sal/types.h>
47 #include <rtl/ref.hxx>
48 #include <rtl/ustring.hxx>
50 #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 OUString accessServiceName
=
64 u
"com.sun.star.configuration.ConfigurationAccess"_ustr
;
65 constexpr OUString updateAccessServiceName
=
66 u
"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr
;
69 throw css::uno::Exception(
70 (u
"com.sun.star.configuration.ConfigurationProvider expects a single,"
71 " non-empty, string nodepath argument"_ustr
),
76 comphelper::WeakComponentImplHelper
<
77 css::lang::XServiceInfo
, css::lang::XMultiServiceFactory
,
78 css::util::XRefreshable
, css::util::XFlushable
,
79 css::lang::XLocalizable
>
82 class Service
: public ServiceBase
86 const css::uno::Reference
< css::uno::XComponentContext
>& context
):
87 context_(context
), default_(true),
94 const css::uno::Reference
< css::uno::XComponentContext
>& context
,
96 context_(context
), locale_(std::move(locale
)),
100 assert(context
.is());
104 Service(const Service
&) = delete;
105 Service
& operator=(const Service
&) = delete;
107 virtual ~Service() override
{}
109 virtual void disposing(std::unique_lock
<std::mutex
>& rGuard
) override
;
111 virtual OUString SAL_CALL
getImplementationName() override
114 ? default_provider::getImplementationName()
115 : u
"com.sun.star.comp.configuration.ConfigurationProvider"_ustr
;
118 virtual sal_Bool SAL_CALL
supportsService(OUString
const & ServiceName
) override
119 { return cppu::supportsService(this, ServiceName
); }
121 virtual css::uno::Sequence
< OUString
> SAL_CALL
122 getSupportedServiceNames() override
125 ? default_provider::getSupportedServiceNames()
126 : css::uno::Sequence
<OUString
> { u
"com.sun.star.configuration.ConfigurationProvider"_ustr
};
129 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
createInstance(
130 OUString
const & aServiceSpecifier
) override
;
132 virtual css::uno::Reference
< css::uno::XInterface
> SAL_CALL
133 createInstanceWithArguments(
134 OUString
const & ServiceSpecifier
,
135 css::uno::Sequence
< css::uno::Any
> const & Arguments
) override
;
137 virtual css::uno::Sequence
< OUString
> SAL_CALL
138 getAvailableServiceNames() override
;
140 virtual void SAL_CALL
refresh() override
;
142 virtual void SAL_CALL
addRefreshListener(
143 css::uno::Reference
< css::util::XRefreshListener
> const & l
) override
;
145 virtual void SAL_CALL
removeRefreshListener(
146 css::uno::Reference
< css::util::XRefreshListener
> const & l
) override
;
148 virtual void SAL_CALL
flush() override
;
150 virtual void SAL_CALL
addFlushListener(
151 css::uno::Reference
< css::util::XFlushListener
> const & l
) override
;
153 virtual void SAL_CALL
removeFlushListener(
154 css::uno::Reference
< css::util::XFlushListener
> const & l
) override
;
156 virtual void SAL_CALL
setLocale(css::lang::Locale
const & eLocale
) override
;
158 virtual css::lang::Locale SAL_CALL
getLocale() override
;
160 void flushModifications() const;
162 css::uno::Reference
< css::uno::XComponentContext
> context_
;
165 std::shared_ptr
<osl::Mutex
> lock_
;
166 comphelper::OInterfaceContainerHelper4
<css::util::XRefreshListener
> maRefreshListeners
;
167 comphelper::OInterfaceContainerHelper4
<css::util::XFlushListener
> maFlushListeners
;
170 css::uno::Reference
< css::uno::XInterface
> Service::createInstance(
171 OUString
const & aServiceSpecifier
)
173 return createInstanceWithArguments(
174 aServiceSpecifier
, css::uno::Sequence
< css::uno::Any
>());
177 css::uno::Reference
< css::uno::XInterface
>
178 Service::createInstanceWithArguments(
179 OUString
const & ServiceSpecifier
,
180 css::uno::Sequence
< css::uno::Any
> const & Arguments
)
184 for (sal_Int32 i
= 0; i
< Arguments
.getLength(); ++i
) {
185 css::beans::NamedValue v1
;
186 css::beans::PropertyValue v2
;
189 if (Arguments
[i
] >>= v1
) {
192 } else if (Arguments
[i
] >>= v2
) {
195 } else if (Arguments
.getLength() == 1 && (Arguments
[i
] >>= nodepath
)) {
196 // For backwards compatibility, allow a single string argument that
198 if (nodepath
.isEmpty()) {
203 throw css::uno::Exception(
204 (u
"com.sun.star.configuration.ConfigurationProvider expects"
205 " NamedValue or PropertyValue arguments"_ustr
),
208 // For backwards compatibility, allow "nodepath" and "Locale" in any
210 if (name
.equalsIgnoreAsciiCase("nodepath")) {
211 if (!nodepath
.isEmpty() || !(value
>>= nodepath
) ||
216 } else if (name
.equalsIgnoreAsciiCase("locale")) {
217 if (!locale
.isEmpty() || !(value
>>= locale
) ||
220 throw css::uno::Exception(
221 (u
"com.sun.star.configuration.ConfigurationProvider expects"
222 " at most one, non-empty, string Locale argument"_ustr
),
227 if (nodepath
.isEmpty()) {
230 // For backwards compatibility, allow a nodepath that misses the leading
232 if (nodepath
[0] != '/') {
233 nodepath
= "/" + nodepath
;
235 if (locale
.isEmpty()) {
236 //TODO: should the Access use the dynamically changing locale_ instead?
238 if (locale
.isEmpty()) {
243 if (ServiceSpecifier
== accessServiceName
) {
245 } else if (ServiceSpecifier
== updateAccessServiceName
) {
248 throw css::uno::Exception(
249 ("com.sun.star.configuration.ConfigurationProvider does not support"
250 " service " + ServiceSpecifier
),
253 osl::MutexGuard
guard(*lock_
);
254 Components
& components
= Components::getSingleton(context_
);
256 new RootAccess(components
, nodepath
, locale
, update
));
257 if (root
->isValue()) {
258 throw css::uno::Exception(
259 ("com.sun.star.configuration.ConfigurationProvider: there is a leaf"
260 " value at nodepath " + nodepath
),
263 components
.addRootAccess(root
);
264 return root
->getXWeak();
267 css::uno::Sequence
< OUString
> Service::getAvailableServiceNames()
269 return { accessServiceName
, updateAccessServiceName
};
272 void Service::refresh() {
274 std::unique_lock
g(m_aMutex
);
275 if (maRefreshListeners
.getLength(g
)) {
276 css::lang::EventObject
ev(getXWeak());
277 maRefreshListeners
.notifyEach(g
, &css::util::XRefreshListener::refreshed
, ev
);
281 void Service::addRefreshListener(
282 css::uno::Reference
< css::util::XRefreshListener
> const & l
)
284 std::unique_lock
g(m_aMutex
);
285 maRefreshListeners
.addInterface(g
, l
);
288 void Service::removeRefreshListener(
289 css::uno::Reference
< css::util::XRefreshListener
> const & l
)
291 std::unique_lock
g(m_aMutex
);
292 maRefreshListeners
.removeInterface(g
, l
);
295 void Service::flush() {
296 flushModifications();
297 std::unique_lock
g(m_aMutex
);
298 if (maFlushListeners
.getLength(g
)) {
299 css::lang::EventObject
ev(getXWeak());
300 maFlushListeners
.notifyEach(g
, &css::util::XFlushListener::flushed
, ev
);
304 void Service::addFlushListener(
305 css::uno::Reference
< css::util::XFlushListener
> const & l
)
307 std::unique_lock
g(m_aMutex
);
308 maFlushListeners
.addInterface(g
, l
);
311 void Service::removeFlushListener(
312 css::uno::Reference
< css::util::XFlushListener
> const & l
)
314 std::unique_lock
g(m_aMutex
);
315 maFlushListeners
.removeInterface(g
, 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::disposing(std::unique_lock
<std::mutex
>& rGuard
) {
334 rGuard
.unlock(); // just in case we call back into Service during dispose()
335 flushModifications();
339 void Service::flushModifications() const {
340 Components
* components
;
342 osl::MutexGuard
guard(*lock_
);
343 components
= &Components::getSingleton(context_
);
345 components
->flushModifications();
348 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
349 com_sun_star_comp_configuration_ConfigurationProvider_get_implementation(
350 css::uno::XComponentContext
* Context
, css::uno::Sequence
<css::uno::Any
> const& Arguments
)
352 if (!Arguments
.hasElements()) {
353 auto p
= css::configuration::theDefaultProvider::get(Context
);
358 for (sal_Int32 i
= 0; i
< Arguments
.getLength(); ++i
) {
359 css::beans::NamedValue v1
;
360 css::beans::PropertyValue v2
;
363 if (Arguments
[i
] >>= v1
) {
366 } else if (Arguments
[i
] >>= v2
) {
370 throw css::uno::Exception(
371 (u
"com.sun.star.configuration.ConfigurationProvider factory"
372 " expects NamedValue or PropertyValue arguments"_ustr
),
375 // For backwards compatibility, allow "Locale" and (ignored)
376 // "EnableAsync" in any case:
377 if (name
.equalsIgnoreAsciiCase("locale")) {
378 if (!locale
.isEmpty() || !(value
>>= locale
) ||
381 throw css::uno::Exception(
382 (u
"com.sun.star.configuration.ConfigurationProvider"
383 " factory expects at most one, non-empty, string"
384 " Locale argument"_ustr
),
387 } else if (!name
.equalsIgnoreAsciiCase("enableasync")) {
388 throw css::uno::Exception(
389 ("com.sun.star.configuration.ConfigurationProvider factory:"
390 " unknown argument " + name
),
394 return cppu::acquire(new Service(Context
, locale
));
400 css::uno::Reference
< css::uno::XInterface
> createDefault(
401 css::uno::Reference
< css::uno::XComponentContext
> const & context
)
403 return getXWeak(new Service(context
));
408 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */