Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / configmgr / source / configurationprovider.cxx
blobaa9db3a394f0d3b480e4dda69edd1583be3e7b00
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>
23 #include <memory>
25 #include <com/sun/star/beans/NamedValue.hpp>
26 #include <com/sun/star/beans/PropertyValue.hpp>
27 #include <com/sun/star/configuration/theDefaultProvider.hpp>
28 #include <com/sun/star/lang/EventObject.hpp>
29 #include <com/sun/star/lang/Locale.hpp>
30 #include <com/sun/star/lang/XLocalizable.hpp>
31 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
32 #include <com/sun/star/lang/XServiceInfo.hpp>
33 #include <com/sun/star/lang/XSingleComponentFactory.hpp>
34 #include <com/sun/star/uno/Any.hxx>
35 #include <com/sun/star/uno/Exception.hpp>
36 #include <com/sun/star/uno/Reference.hxx>
37 #include <com/sun/star/uno/Sequence.hxx>
38 #include <com/sun/star/uno/XInterface.hpp>
39 #include <com/sun/star/util/XFlushListener.hpp>
40 #include <com/sun/star/util/XFlushable.hpp>
41 #include <com/sun/star/util/XRefreshListener.hpp>
42 #include <com/sun/star/util/XRefreshable.hpp>
43 #include <cppu/unotype.hxx>
44 #include <cppuhelper/basemutex.hxx>
45 #include <cppuhelper/compbase.hxx>
46 #include <cppuhelper/factory.hxx>
47 #include <cppuhelper/implbase.hxx>
48 #include <cppuhelper/supportsservice.hxx>
49 #include <cppuhelper/weak.hxx>
50 #include <osl/mutex.hxx>
51 #include <sal/types.h>
52 #include <rtl/ref.hxx>
53 #include <rtl/ustring.hxx>
55 #include <i18nlangtag/languagetag.hxx>
57 #include "components.hxx"
58 #include "configurationprovider.hxx"
59 #include "lock.hxx"
60 #include "defaultprovider.hxx"
61 #include "rootaccess.hxx"
63 namespace configmgr { namespace configuration_provider {
65 namespace {
67 char const accessServiceName[] =
68 "com.sun.star.configuration.ConfigurationAccess";
69 char const updateAccessServiceName[] =
70 "com.sun.star.configuration.ConfigurationUpdateAccess";
72 void badNodePath() {
73 throw css::uno::Exception(
74 ("com.sun.star.configuration.ConfigurationProvider expects a single,"
75 " non-empty, string nodepath argument"),
76 nullptr);
79 typedef
80 cppu::WeakComponentImplHelper<
81 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
82 css::util::XRefreshable, css::util::XFlushable,
83 css::lang::XLocalizable >
84 ServiceBase;
86 class Service:
87 private cppu::BaseMutex, public ServiceBase
89 public:
90 explicit Service(
91 const css::uno::Reference< css::uno::XComponentContext >& context):
92 ServiceBase(m_aMutex), context_(context), default_(true),
93 lock_( lock() )
95 assert(context.is());
98 Service(
99 const css::uno::Reference< css::uno::XComponentContext >& context,
100 OUString const & locale):
101 ServiceBase(m_aMutex), context_(context), locale_(locale),
102 default_(false),
103 lock_( lock() )
105 assert(context.is());
108 private:
109 Service(const Service&) = delete;
110 Service& operator=(const Service&) = delete;
112 virtual ~Service() override {}
114 virtual void SAL_CALL disposing() override { flushModifications(); }
116 virtual OUString SAL_CALL getImplementationName() override
118 return default_
119 ? default_provider::getImplementationName()
120 : configuration_provider::getImplementationName();
123 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
124 { return cppu::supportsService(this, ServiceName); }
126 virtual css::uno::Sequence< OUString > SAL_CALL
127 getSupportedServiceNames() override
129 return default_
130 ? default_provider::getSupportedServiceNames()
131 : configuration_provider::getSupportedServiceNames();
134 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
135 OUString const & aServiceSpecifier) override;
137 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
138 createInstanceWithArguments(
139 OUString const & ServiceSpecifier,
140 css::uno::Sequence< css::uno::Any > const & Arguments) override;
142 virtual css::uno::Sequence< OUString > SAL_CALL
143 getAvailableServiceNames() override;
145 virtual void SAL_CALL refresh() override;
147 virtual void SAL_CALL addRefreshListener(
148 css::uno::Reference< css::util::XRefreshListener > const & l) override;
150 virtual void SAL_CALL removeRefreshListener(
151 css::uno::Reference< css::util::XRefreshListener > const & l) override;
153 virtual void SAL_CALL flush() override;
155 virtual void SAL_CALL addFlushListener(
156 css::uno::Reference< css::util::XFlushListener > const & l) override;
158 virtual void SAL_CALL removeFlushListener(
159 css::uno::Reference< css::util::XFlushListener > const & l) override;
161 virtual void SAL_CALL setLocale(css::lang::Locale const & eLocale) override;
163 virtual css::lang::Locale SAL_CALL getLocale() override;
165 void flushModifications() const;
167 css::uno::Reference< css::uno::XComponentContext > context_;
168 OUString locale_;
169 bool default_;
170 std::shared_ptr<osl::Mutex> lock_;
173 css::uno::Reference< css::uno::XInterface > Service::createInstance(
174 OUString const & aServiceSpecifier)
176 return createInstanceWithArguments(
177 aServiceSpecifier, css::uno::Sequence< css::uno::Any >());
180 css::uno::Reference< css::uno::XInterface >
181 Service::createInstanceWithArguments(
182 OUString const & ServiceSpecifier,
183 css::uno::Sequence< css::uno::Any > const & Arguments)
185 OUString nodepath;
186 OUString locale;
187 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
188 css::beans::NamedValue v1;
189 css::beans::PropertyValue v2;
190 OUString name;
191 css::uno::Any value;
192 if (Arguments[i] >>= v1) {
193 name = v1.Name;
194 value = v1.Value;
195 } else if (Arguments[i] >>= v2) {
196 name = v2.Name;
197 value = v2.Value;
198 } else if (Arguments.getLength() == 1 && (Arguments[i] >>= nodepath)) {
199 // For backwards compatibility, allow a single string argument that
200 // denotes nodepath.
201 if (nodepath.isEmpty()) {
202 badNodePath();
204 break;
205 } else {
206 throw css::uno::Exception(
207 ("com.sun.star.configuration.ConfigurationProvider expects"
208 " NamedValue or PropertyValue arguments"),
209 nullptr);
211 // For backwards compatibility, allow "nodepath" and "Locale" in any
212 // case:
213 if (name.equalsIgnoreAsciiCase("nodepath")) {
214 if (!nodepath.isEmpty() || !(value >>= nodepath) ||
215 nodepath.isEmpty())
217 badNodePath();
219 } else if (name.equalsIgnoreAsciiCase("locale")) {
220 if (!locale.isEmpty() || !(value >>= locale) ||
221 locale.isEmpty())
223 throw css::uno::Exception(
224 ("com.sun.star.configuration.ConfigurationProvider expects"
225 " at most one, non-empty, string Locale argument"),
226 nullptr);
230 if (nodepath.isEmpty()) {
231 badNodePath();
233 // For backwards compatibility, allow a nodepath that misses the leading
234 // slash:
235 if (nodepath[0] != '/') {
236 nodepath = "/" + nodepath;
238 if (locale.isEmpty()) {
239 //TODO: should the Access use the dynamically changing locale_ instead?
240 locale = locale_;
241 if (locale.isEmpty()) {
242 locale = "en-US";
245 bool update;
246 if (ServiceSpecifier == accessServiceName) {
247 update = false;
248 } else if (ServiceSpecifier == updateAccessServiceName) {
249 update = true;
250 } else {
251 throw css::uno::Exception(
252 ("com.sun.star.configuration.ConfigurationProvider does not support"
253 " service " + ServiceSpecifier),
254 static_cast< cppu::OWeakObject * >(this));
256 osl::MutexGuard guard(*lock_);
257 Components & components = Components::getSingleton(context_);
258 rtl::Reference root(
259 new RootAccess(components, nodepath, locale, update));
260 if (root->isValue()) {
261 throw css::uno::Exception(
262 ("com.sun.star.configuration.ConfigurationProvider: there is a leaf"
263 " value at nodepath " + nodepath),
264 static_cast< cppu::OWeakObject * >(this));
266 components.addRootAccess(root);
267 return static_cast< cppu::OWeakObject * >(root.get());
270 css::uno::Sequence< OUString > Service::getAvailableServiceNames()
272 css::uno::Sequence< OUString > names(2);
273 names[0] = accessServiceName;
274 names[1] = updateAccessServiceName;
275 return names;
278 void Service::refresh() {
279 //TODO
280 cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
281 cppu::UnoType< css::util::XRefreshListener >::get());
282 if (cont != nullptr) {
283 css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
284 cont->notifyEach(&css::util::XRefreshListener::refreshed, ev);
288 void Service::addRefreshListener(
289 css::uno::Reference< css::util::XRefreshListener > const & l)
291 rBHelper.addListener(
292 cppu::UnoType< css::util::XRefreshListener >::get(), l);
295 void Service::removeRefreshListener(
296 css::uno::Reference< css::util::XRefreshListener > const & l)
298 rBHelper.removeListener(
299 cppu::UnoType< css::util::XRefreshListener >::get(), l);
302 void Service::flush() {
303 flushModifications();
304 cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
305 cppu::UnoType< css::util::XFlushListener >::get());
306 if (cont != nullptr) {
307 css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
308 cont->notifyEach(&css::util::XFlushListener::flushed, ev);
312 void Service::addFlushListener(
313 css::uno::Reference< css::util::XFlushListener > const & l)
315 rBHelper.addListener(cppu::UnoType< css::util::XFlushListener >::get(), l);
318 void Service::removeFlushListener(
319 css::uno::Reference< css::util::XFlushListener > const & l)
321 rBHelper.removeListener(
322 cppu::UnoType< css::util::XFlushListener >::get(), l);
325 void Service::setLocale(css::lang::Locale const & eLocale)
327 osl::MutexGuard guard(*lock_);
328 locale_ = LanguageTag::convertToBcp47( eLocale, false);
331 css::lang::Locale Service::getLocale() {
332 osl::MutexGuard guard(*lock_);
333 css::lang::Locale loc;
334 if (! locale_.isEmpty()) {
335 loc = LanguageTag::convertToLocale( locale_, false);
337 return loc;
340 void Service::flushModifications() const {
341 Components * components;
343 osl::MutexGuard guard(*lock_);
344 components = &Components::getSingleton(context_);
346 components->flushModifications();
349 class Factory:
350 public cppu::WeakImplHelper<
351 css::lang::XSingleComponentFactory, css::lang::XServiceInfo >
353 public:
354 Factory() {}
356 private:
357 Factory(const Factory&) = delete;
358 Factory& operator=(const Factory&) = delete;
360 virtual ~Factory() override {}
362 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
363 createInstanceWithContext(
364 css::uno::Reference< css::uno::XComponentContext > const & Context) override;
366 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
367 createInstanceWithArgumentsAndContext(
368 css::uno::Sequence< css::uno::Any > const & Arguments,
369 css::uno::Reference< css::uno::XComponentContext > const & Context) override;
371 virtual OUString SAL_CALL getImplementationName() override
372 { return configuration_provider::getImplementationName(); }
374 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
375 { return cppu::supportsService(this, ServiceName); }
377 virtual css::uno::Sequence< OUString > SAL_CALL
378 getSupportedServiceNames() override
379 { return configuration_provider::getSupportedServiceNames(); }
382 css::uno::Reference< css::uno::XInterface > Factory::createInstanceWithContext(
383 css::uno::Reference< css::uno::XComponentContext > const & Context)
385 return createInstanceWithArgumentsAndContext(
386 css::uno::Sequence< css::uno::Any >(), Context);
389 css::uno::Reference< css::uno::XInterface >
390 Factory::createInstanceWithArgumentsAndContext(
391 css::uno::Sequence< css::uno::Any > const & Arguments,
392 css::uno::Reference< css::uno::XComponentContext > const & Context)
394 if (!Arguments.hasElements()) {
395 return css::configuration::theDefaultProvider::get(Context);
396 } else {
397 OUString locale;
398 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
399 css::beans::NamedValue v1;
400 css::beans::PropertyValue v2;
401 OUString name;
402 css::uno::Any value;
403 if (Arguments[i] >>= v1) {
404 name = v1.Name;
405 value = v1.Value;
406 } else if (Arguments[i] >>= v2) {
407 name = v2.Name;
408 value = v2.Value;
409 } else {
410 throw css::uno::Exception(
411 ("com.sun.star.configuration.ConfigurationProvider factory"
412 " expects NamedValue or PropertyValue arguments"),
413 nullptr);
415 // For backwards compatibility, allow "Locale" and (ignored)
416 // "EnableAsync" in any case:
417 if (name.equalsIgnoreAsciiCase("locale")) {
418 if (!locale.isEmpty() || !(value >>= locale) ||
419 locale.isEmpty())
421 throw css::uno::Exception(
422 ("com.sun.star.configuration.ConfigurationProvider"
423 " factory expects at most one, non-empty, string"
424 " Locale argument"),
425 nullptr);
427 } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
428 throw css::uno::Exception(
429 ("com.sun.star.configuration.ConfigurationProvider factory:"
430 " unknown argument " + name),
431 nullptr);
434 return static_cast< cppu::OWeakObject * >(new Service(Context, locale));
440 css::uno::Reference< css::uno::XInterface > createDefault(
441 css::uno::Reference< css::uno::XComponentContext > const & context)
443 return static_cast< cppu::OWeakObject * >(new Service(context));
446 OUString getImplementationName() {
447 return "com.sun.star.comp.configuration.ConfigurationProvider";
450 css::uno::Sequence< OUString > getSupportedServiceNames() {
451 return css::uno::Sequence< OUString > { "com.sun.star.configuration.ConfigurationProvider" };
454 css::uno::Reference< css::lang::XSingleComponentFactory >
455 createFactory(
456 SAL_UNUSED_PARAMETER cppu::ComponentFactoryFunc,
457 SAL_UNUSED_PARAMETER OUString const &,
458 SAL_UNUSED_PARAMETER css::uno::Sequence< OUString > const &,
459 SAL_UNUSED_PARAMETER rtl_ModuleCount *)
461 return new Factory;
466 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */