bump product version to 7.2.5.1
[LibreOffice.git] / configmgr / source / configurationprovider.cxx
blobf9936b6ba1a3ed8a44d1e51d68d079f3a98f0751
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 <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"
55 #include "lock.hxx"
56 #include "defaultprovider.hxx"
57 #include "rootaccess.hxx"
59 namespace configmgr::configuration_provider {
61 namespace {
63 constexpr OUStringLiteral accessServiceName =
64 u"com.sun.star.configuration.ConfigurationAccess";
65 constexpr OUStringLiteral updateAccessServiceName =
66 u"com.sun.star.configuration.ConfigurationUpdateAccess";
68 void badNodePath() {
69 throw css::uno::Exception(
70 ("com.sun.star.configuration.ConfigurationProvider expects a single,"
71 " non-empty, string nodepath argument"),
72 nullptr);
75 typedef
76 cppu::WeakComponentImplHelper<
77 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
78 css::util::XRefreshable, css::util::XFlushable,
79 css::lang::XLocalizable >
80 ServiceBase;
82 class Service:
83 private cppu::BaseMutex, public ServiceBase
85 public:
86 explicit Service(
87 const css::uno::Reference< css::uno::XComponentContext >& context):
88 ServiceBase(m_aMutex), context_(context), default_(true),
89 lock_( lock() )
91 assert(context.is());
94 Service(
95 const css::uno::Reference< css::uno::XComponentContext >& context,
96 OUString const & locale):
97 ServiceBase(m_aMutex), context_(context), locale_(locale),
98 default_(false),
99 lock_( lock() )
101 assert(context.is());
104 private:
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
114 return default_
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
125 return default_
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_;
164 OUString locale_;
165 bool default_;
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)
181 OUString nodepath;
182 OUString locale;
183 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
184 css::beans::NamedValue v1;
185 css::beans::PropertyValue v2;
186 OUString name;
187 css::uno::Any value;
188 if (Arguments[i] >>= v1) {
189 name = v1.Name;
190 value = v1.Value;
191 } else if (Arguments[i] >>= v2) {
192 name = v2.Name;
193 value = v2.Value;
194 } else if (Arguments.getLength() == 1 && (Arguments[i] >>= nodepath)) {
195 // For backwards compatibility, allow a single string argument that
196 // denotes nodepath.
197 if (nodepath.isEmpty()) {
198 badNodePath();
200 break;
201 } else {
202 throw css::uno::Exception(
203 ("com.sun.star.configuration.ConfigurationProvider expects"
204 " NamedValue or PropertyValue arguments"),
205 nullptr);
207 // For backwards compatibility, allow "nodepath" and "Locale" in any
208 // case:
209 if (name.equalsIgnoreAsciiCase("nodepath")) {
210 if (!nodepath.isEmpty() || !(value >>= nodepath) ||
211 nodepath.isEmpty())
213 badNodePath();
215 } else if (name.equalsIgnoreAsciiCase("locale")) {
216 if (!locale.isEmpty() || !(value >>= locale) ||
217 locale.isEmpty())
219 throw css::uno::Exception(
220 ("com.sun.star.configuration.ConfigurationProvider expects"
221 " at most one, non-empty, string Locale argument"),
222 nullptr);
226 if (nodepath.isEmpty()) {
227 badNodePath();
229 // For backwards compatibility, allow a nodepath that misses the leading
230 // slash:
231 if (nodepath[0] != '/') {
232 nodepath = "/" + nodepath;
234 if (locale.isEmpty()) {
235 //TODO: should the Access use the dynamically changing locale_ instead?
236 locale = locale_;
237 if (locale.isEmpty()) {
238 locale = "en-US";
241 bool update;
242 if (ServiceSpecifier == accessServiceName) {
243 update = false;
244 } else if (ServiceSpecifier == updateAccessServiceName) {
245 update = true;
246 } else {
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_);
254 rtl::Reference root(
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() {
272 //TODO
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);
330 return loc;
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);
348 p->acquire();
349 return p.get();
350 } else {
351 OUString locale;
352 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
353 css::beans::NamedValue v1;
354 css::beans::PropertyValue v2;
355 OUString name;
356 css::uno::Any value;
357 if (Arguments[i] >>= v1) {
358 name = v1.Name;
359 value = v1.Value;
360 } else if (Arguments[i] >>= v2) {
361 name = v2.Name;
362 value = v2.Value;
363 } else {
364 throw css::uno::Exception(
365 ("com.sun.star.configuration.ConfigurationProvider factory"
366 " expects NamedValue or PropertyValue arguments"),
367 nullptr);
369 // For backwards compatibility, allow "Locale" and (ignored)
370 // "EnableAsync" in any case:
371 if (name.equalsIgnoreAsciiCase("locale")) {
372 if (!locale.isEmpty() || !(value >>= locale) ||
373 locale.isEmpty())
375 throw css::uno::Exception(
376 ("com.sun.star.configuration.ConfigurationProvider"
377 " factory expects at most one, non-empty, string"
378 " Locale argument"),
379 nullptr);
381 } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
382 throw css::uno::Exception(
383 ("com.sun.star.configuration.ConfigurationProvider factory:"
384 " unknown argument " + name),
385 nullptr);
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: */