LibreOffice Module configmgr (master) 1
configurationprovider.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <cassert>
23
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>
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>
50
52#include <utility>
53
54#include "components.hxx"
56#include "lock.hxx"
57#include "defaultprovider.hxx"
58#include "rootaccess.hxx"
59
61
62namespace {
63
64constexpr OUStringLiteral accessServiceName =
65 u"com.sun.star.configuration.ConfigurationAccess";
66constexpr OUStringLiteral updateAccessServiceName =
67 u"com.sun.star.configuration.ConfigurationUpdateAccess";
68
69void badNodePath() {
70 throw css::uno::Exception(
71 ("com.sun.star.configuration.ConfigurationProvider expects a single,"
72 " non-empty, string nodepath argument"),
73 nullptr);
74}
75
76typedef
77 cppu::WeakComponentImplHelper<
78 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
79 css::util::XRefreshable, css::util::XFlushable,
80 css::lang::XLocalizable >
81 ServiceBase;
82
83class Service:
84 private cppu::BaseMutex, public ServiceBase
85{
86public:
87 explicit Service(
88 const css::uno::Reference< css::uno::XComponentContext >& context):
89 ServiceBase(m_aMutex), context_(context), default_(true),
90 lock_( lock() )
91 {
92 assert(context.is());
93 }
94
95 Service(
96 const css::uno::Reference< css::uno::XComponentContext >& context,
97 OUString locale):
98 ServiceBase(m_aMutex), context_(context), locale_(std::move(locale)),
99 default_(false),
100 lock_( lock() )
101 {
102 assert(context.is());
103 }
104
105private:
106 Service(const Service&) = delete;
107 Service& operator=(const Service&) = delete;
108
109 virtual ~Service() override {}
110
111 virtual void SAL_CALL disposing() override { flushModifications(); }
112
113 virtual OUString SAL_CALL getImplementationName() override
114 {
115 return default_
117 : "com.sun.star.comp.configuration.ConfigurationProvider";
118 }
119
120 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
121 { return cppu::supportsService(this, ServiceName); }
122
123 virtual css::uno::Sequence< OUString > SAL_CALL
124 getSupportedServiceNames() override
125 {
126 return default_
128 : css::uno::Sequence<OUString> { "com.sun.star.configuration.ConfigurationProvider" };
129 }
130
131 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
132 OUString const & aServiceSpecifier) override;
133
134 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
135 createInstanceWithArguments(
136 OUString const & ServiceSpecifier,
137 css::uno::Sequence< css::uno::Any > const & Arguments) override;
138
139 virtual css::uno::Sequence< OUString > SAL_CALL
140 getAvailableServiceNames() override;
141
142 virtual void SAL_CALL refresh() override;
143
144 virtual void SAL_CALL addRefreshListener(
145 css::uno::Reference< css::util::XRefreshListener > const & l) override;
146
147 virtual void SAL_CALL removeRefreshListener(
148 css::uno::Reference< css::util::XRefreshListener > const & l) override;
149
150 virtual void SAL_CALL flush() override;
151
152 virtual void SAL_CALL addFlushListener(
153 css::uno::Reference< css::util::XFlushListener > const & l) override;
154
155 virtual void SAL_CALL removeFlushListener(
156 css::uno::Reference< css::util::XFlushListener > const & l) override;
157
158 virtual void SAL_CALL setLocale(css::lang::Locale const & eLocale) override;
159
160 virtual css::lang::Locale SAL_CALL getLocale() override;
161
162 void flushModifications() const;
163
164 css::uno::Reference< css::uno::XComponentContext > context_;
165 OUString locale_;
167 std::shared_ptr<osl::Mutex> lock_;
168};
169
170css::uno::Reference< css::uno::XInterface > Service::createInstance(
171 OUString const & aServiceSpecifier)
172{
173 return createInstanceWithArguments(
174 aServiceSpecifier, css::uno::Sequence< css::uno::Any >());
175}
176
177css::uno::Reference< css::uno::XInterface >
178Service::createInstanceWithArguments(
179 OUString const & ServiceSpecifier,
180 css::uno::Sequence< css::uno::Any > const & Arguments)
181{
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();
200 }
201 break;
202 } else {
203 throw css::uno::Exception(
204 ("com.sun.star.configuration.ConfigurationProvider expects"
205 " NamedValue or PropertyValue arguments"),
206 nullptr);
207 }
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())
213 {
214 badNodePath();
215 }
216 } else if (name.equalsIgnoreAsciiCase("locale")) {
217 if (!locale.isEmpty() || !(value >>= locale) ||
218 locale.isEmpty())
219 {
220 throw css::uno::Exception(
221 ("com.sun.star.configuration.ConfigurationProvider expects"
222 " at most one, non-empty, string Locale argument"),
223 nullptr);
224 }
225 }
226 }
227 if (nodepath.isEmpty()) {
228 badNodePath();
229 }
230 // For backwards compatibility, allow a nodepath that misses the leading
231 // slash:
232 if (nodepath[0] != '/') {
233 nodepath = "/" + nodepath;
234 }
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";
240 }
241 }
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 static_cast< cppu::OWeakObject * >(this));
252 }
253 osl::MutexGuard guard(*lock_);
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 static_cast< cppu::OWeakObject * >(this));
262 }
263 components.addRootAccess(root);
264 return static_cast< cppu::OWeakObject * >(root.get());
265}
266
267css::uno::Sequence< OUString > Service::getAvailableServiceNames()
268{
269 return { accessServiceName, updateAccessServiceName };
270}
271
272void Service::refresh() {
273 //TODO
274 cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
276 if (cont != nullptr) {
277 css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
278 cont->notifyEach(&css::util::XRefreshListener::refreshed, ev);
279 }
280}
281
282void Service::addRefreshListener(
283 css::uno::Reference< css::util::XRefreshListener > const & l)
284{
285 rBHelper.addListener(
287}
288
289void Service::removeRefreshListener(
290 css::uno::Reference< css::util::XRefreshListener > const & l)
291{
292 rBHelper.removeListener(
294}
295
296void Service::flush() {
297 flushModifications();
298 cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
300 if (cont != nullptr) {
301 css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
302 cont->notifyEach(&css::util::XFlushListener::flushed, ev);
303 }
304}
305
306void Service::addFlushListener(
307 css::uno::Reference< css::util::XFlushListener > const & l)
308{
309 rBHelper.addListener(cppu::UnoType< css::util::XFlushListener >::get(), l);
310}
311
312void Service::removeFlushListener(
313 css::uno::Reference< css::util::XFlushListener > const & l)
314{
315 rBHelper.removeListener(
317}
318
319void Service::setLocale(css::lang::Locale const & eLocale)
320{
321 osl::MutexGuard guard(*lock_);
322 locale_ = LanguageTag::convertToBcp47( eLocale, false);
323}
324
325css::lang::Locale Service::getLocale() {
326 osl::MutexGuard guard(*lock_);
327 css::lang::Locale loc;
328 if (! locale_.isEmpty()) {
330 }
331 return loc;
332}
333
334void Service::flushModifications() const {
335 Components * components;
336 {
337 osl::MutexGuard guard(*lock_);
338 components = &Components::getSingleton(context_);
339 }
340 components->flushModifications();
341}
342
343extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
344com_sun_star_comp_configuration_ConfigurationProvider_get_implementation(
345 css::uno::XComponentContext* Context, css::uno::Sequence<css::uno::Any> const& Arguments)
346{
347 if (!Arguments.hasElements()) {
348 auto p = css::configuration::theDefaultProvider::get(Context);
349 p->acquire();
350 return p.get();
351 } else {
352 OUString locale;
353 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
354 css::beans::NamedValue v1;
355 css::beans::PropertyValue v2;
356 OUString name;
357 css::uno::Any value;
358 if (Arguments[i] >>= v1) {
359 name = v1.Name;
360 value = v1.Value;
361 } else if (Arguments[i] >>= v2) {
362 name = v2.Name;
363 value = v2.Value;
364 } else {
365 throw css::uno::Exception(
366 ("com.sun.star.configuration.ConfigurationProvider factory"
367 " expects NamedValue or PropertyValue arguments"),
368 nullptr);
369 }
370 // For backwards compatibility, allow "Locale" and (ignored)
371 // "EnableAsync" in any case:
372 if (name.equalsIgnoreAsciiCase("locale")) {
373 if (!locale.isEmpty() || !(value >>= locale) ||
374 locale.isEmpty())
375 {
376 throw css::uno::Exception(
377 ("com.sun.star.configuration.ConfigurationProvider"
378 " factory expects at most one, non-empty, string"
379 " Locale argument"),
380 nullptr);
381 }
382 } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
383 throw css::uno::Exception(
384 ("com.sun.star.configuration.ConfigurationProvider factory:"
385 " unknown argument " + name),
386 nullptr);
387 }
388 }
389 return cppu::acquire(new Service(Context, locale));
390 }
391}
392
393}
394
395css::uno::Reference< css::uno::XInterface > createDefault(
396 css::uno::Reference< css::uno::XComponentContext > const & context)
397{
398 return static_cast< cppu::OWeakObject * >(new Service(context));
399}
400
401}
402
403/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
static OUString convertToBcp47(LanguageType nLangID)
static Components & getSingleton(css::uno::Reference< css::uno::XComponentContext > const &context)
Definition: components.cxx:199
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
Any value
OUString name
Definition: components.cxx:85
OUString locale_
bool default_
css::uno::Reference< css::uno::XComponentContext > context_
std::shared_ptr< osl::Mutex > lock_
float u
std::mutex m_aMutex
void * p
const LanguageTag & getLocale()
void setLocale(const LanguageTag &languageTag)
css::uno::Reference< css::uno::XInterface > createDefault(css::uno::Reference< css::uno::XComponentContext > const &context)
css::uno::Sequence< OUString > getSupportedServiceNames()
std::shared_ptr< osl::Mutex > const & lock()
Definition: lock.cxx:28
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
unsigned char sal_Bool
bool update()
std::vector< uno::Reference< sheet::XSpreadsheetDocument > > Components