CppunitTest_sc_tiledrendering2: move to tiledrendering folder
[LibreOffice.git] / configmgr / source / configurationprovider.cxx
blob02c3c78d430325d14c2f1a9071e49567379019d6
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
22 #include <cassert>
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>
51 #include <utility>
53 #include "components.hxx"
54 #include "configurationprovider.hxx"
55 #include "lock.hxx"
56 #include "defaultprovider.hxx"
57 #include "rootaccess.hxx"
59 namespace configmgr::configuration_provider {
61 namespace {
63 constexpr OUString accessServiceName =
64 u"com.sun.star.configuration.ConfigurationAccess"_ustr;
65 constexpr OUString updateAccessServiceName =
66 u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr;
68 void badNodePath() {
69 throw css::uno::Exception(
70 (u"com.sun.star.configuration.ConfigurationProvider expects a single,"
71 " non-empty, string nodepath argument"_ustr),
72 nullptr);
75 typedef
76 comphelper::WeakComponentImplHelper<
77 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
78 css::util::XRefreshable, css::util::XFlushable,
79 css::lang::XLocalizable >
80 ServiceBase;
82 class Service : public ServiceBase
84 public:
85 explicit Service(
86 const css::uno::Reference< css::uno::XComponentContext >& context):
87 context_(context), default_(true),
88 lock_( lock() )
90 assert(context.is());
93 Service(
94 const css::uno::Reference< css::uno::XComponentContext >& context,
95 OUString locale):
96 context_(context), locale_(std::move(locale)),
97 default_(false),
98 lock_( lock() )
100 assert(context.is());
103 private:
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
113 return default_
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
124 return default_
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_;
163 OUString locale_;
164 bool default_;
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)
182 OUString nodepath;
183 OUString locale;
184 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
185 css::beans::NamedValue v1;
186 css::beans::PropertyValue v2;
187 OUString name;
188 css::uno::Any value;
189 if (Arguments[i] >>= v1) {
190 name = v1.Name;
191 value = v1.Value;
192 } else if (Arguments[i] >>= v2) {
193 name = v2.Name;
194 value = v2.Value;
195 } else if (Arguments.getLength() == 1 && (Arguments[i] >>= nodepath)) {
196 // For backwards compatibility, allow a single string argument that
197 // denotes nodepath.
198 if (nodepath.isEmpty()) {
199 badNodePath();
201 break;
202 } else {
203 throw css::uno::Exception(
204 (u"com.sun.star.configuration.ConfigurationProvider expects"
205 " NamedValue or PropertyValue arguments"_ustr),
206 nullptr);
208 // For backwards compatibility, allow "nodepath" and "Locale" in any
209 // case:
210 if (name.equalsIgnoreAsciiCase("nodepath")) {
211 if (!nodepath.isEmpty() || !(value >>= nodepath) ||
212 nodepath.isEmpty())
214 badNodePath();
216 } else if (name.equalsIgnoreAsciiCase("locale")) {
217 if (!locale.isEmpty() || !(value >>= locale) ||
218 locale.isEmpty())
220 throw css::uno::Exception(
221 (u"com.sun.star.configuration.ConfigurationProvider expects"
222 " at most one, non-empty, string Locale argument"_ustr),
223 nullptr);
227 if (nodepath.isEmpty()) {
228 badNodePath();
230 // For backwards compatibility, allow a nodepath that misses the leading
231 // slash:
232 if (nodepath[0] != '/') {
233 nodepath = "/" + nodepath;
235 if (locale.isEmpty()) {
236 //TODO: should the Access use the dynamically changing locale_ instead?
237 locale = locale_;
238 if (locale.isEmpty()) {
239 locale = "en-US";
242 bool update;
243 if (ServiceSpecifier == accessServiceName) {
244 update = false;
245 } else if (ServiceSpecifier == updateAccessServiceName) {
246 update = true;
247 } else {
248 throw css::uno::Exception(
249 ("com.sun.star.configuration.ConfigurationProvider does not support"
250 " service " + ServiceSpecifier),
251 getXWeak());
253 osl::MutexGuard guard(*lock_);
254 Components & components = Components::getSingleton(context_);
255 rtl::Reference root(
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),
261 getXWeak());
263 components.addRootAccess(root);
264 return root->getXWeak();
267 css::uno::Sequence< OUString > Service::getAvailableServiceNames()
269 return { accessServiceName, updateAccessServiceName };
272 void Service::refresh() {
273 //TODO
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);
330 return loc;
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();
336 rGuard.lock();
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);
354 p->acquire();
355 return p.get();
356 } else {
357 OUString locale;
358 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
359 css::beans::NamedValue v1;
360 css::beans::PropertyValue v2;
361 OUString name;
362 css::uno::Any value;
363 if (Arguments[i] >>= v1) {
364 name = v1.Name;
365 value = v1.Value;
366 } else if (Arguments[i] >>= v2) {
367 name = v2.Name;
368 value = v2.Value;
369 } else {
370 throw css::uno::Exception(
371 (u"com.sun.star.configuration.ConfigurationProvider factory"
372 " expects NamedValue or PropertyValue arguments"_ustr),
373 nullptr);
375 // For backwards compatibility, allow "Locale" and (ignored)
376 // "EnableAsync" in any case:
377 if (name.equalsIgnoreAsciiCase("locale")) {
378 if (!locale.isEmpty() || !(value >>= locale) ||
379 locale.isEmpty())
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),
385 nullptr);
387 } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
388 throw css::uno::Exception(
389 ("com.sun.star.configuration.ConfigurationProvider factory:"
390 " unknown argument " + name),
391 nullptr);
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: */