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>
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>
49
51#include <utility>
52
53#include "components.hxx"
55#include "lock.hxx"
56#include "defaultprovider.hxx"
57#include "rootaccess.hxx"
58
60
61namespace {
62
63constexpr OUStringLiteral accessServiceName =
64 u"com.sun.star.configuration.ConfigurationAccess";
65constexpr OUStringLiteral updateAccessServiceName =
66 u"com.sun.star.configuration.ConfigurationUpdateAccess";
67
68void badNodePath() {
69 throw css::uno::Exception(
70 ("com.sun.star.configuration.ConfigurationProvider expects a single,"
71 " non-empty, string nodepath argument"),
72 nullptr);
73}
74
75typedef
77 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
78 css::util::XRefreshable, css::util::XFlushable,
79 css::lang::XLocalizable >
80 ServiceBase;
81
82class Service : public ServiceBase
83{
84public:
85 explicit Service(
86 const css::uno::Reference< css::uno::XComponentContext >& context):
87 context_(context), default_(true),
88 lock_( lock() )
89 {
90 assert(context.is());
91 }
92
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() )
99 {
100 assert(context.is());
101 }
102
103private:
104 Service(const Service&) = delete;
105 Service& operator=(const Service&) = delete;
106
107 virtual ~Service() override {}
108
109 virtual void disposing(std::unique_lock<std::mutex>& rGuard) override;
110
111 virtual OUString SAL_CALL getImplementationName() override
112 {
113 return default_
115 : "com.sun.star.comp.configuration.ConfigurationProvider";
116 }
117
118 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
119 { return cppu::supportsService(this, ServiceName); }
120
121 virtual css::uno::Sequence< OUString > SAL_CALL
122 getSupportedServiceNames() override
123 {
124 return default_
126 : css::uno::Sequence<OUString> { "com.sun.star.configuration.ConfigurationProvider" };
127 }
128
129 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
130 OUString const & aServiceSpecifier) override;
131
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;
136
137 virtual css::uno::Sequence< OUString > SAL_CALL
138 getAvailableServiceNames() override;
139
140 virtual void SAL_CALL refresh() override;
141
142 virtual void SAL_CALL addRefreshListener(
143 css::uno::Reference< css::util::XRefreshListener > const & l) override;
144
145 virtual void SAL_CALL removeRefreshListener(
146 css::uno::Reference< css::util::XRefreshListener > const & l) override;
147
148 virtual void SAL_CALL flush() override;
149
150 virtual void SAL_CALL addFlushListener(
151 css::uno::Reference< css::util::XFlushListener > const & l) override;
152
153 virtual void SAL_CALL removeFlushListener(
154 css::uno::Reference< css::util::XFlushListener > const & l) override;
155
156 virtual void SAL_CALL setLocale(css::lang::Locale const & eLocale) override;
157
158 virtual css::lang::Locale SAL_CALL getLocale() override;
159
160 void flushModifications() const;
161
162 css::uno::Reference< css::uno::XComponentContext > context_;
163 OUString locale_;
165 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 getXWeak());
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 getXWeak());
262 }
263 components.addRootAccess(root);
264 return root->getXWeak();
265}
266
267css::uno::Sequence< OUString > Service::getAvailableServiceNames()
268{
269 return { accessServiceName, updateAccessServiceName };
270}
271
272void Service::refresh() {
273 //TODO
274 std::unique_lock g(m_aMutex);
276 css::lang::EventObject ev(getXWeak());
277 maRefreshListeners.notifyEach(g, &css::util::XRefreshListener::refreshed, ev);
278 }
279}
280
281void Service::addRefreshListener(
282 css::uno::Reference< css::util::XRefreshListener > const & l)
283{
284 std::unique_lock g(m_aMutex);
286}
287
288void Service::removeRefreshListener(
289 css::uno::Reference< css::util::XRefreshListener > const & l)
290{
291 std::unique_lock g(m_aMutex);
293}
294
295void Service::flush() {
296 flushModifications();
297 std::unique_lock g(m_aMutex);
299 css::lang::EventObject ev(getXWeak());
300 maFlushListeners.notifyEach(g, &css::util::XFlushListener::flushed, ev);
301 }
302}
303
304void Service::addFlushListener(
305 css::uno::Reference< css::util::XFlushListener > const & l)
306{
307 std::unique_lock g(m_aMutex);
309}
310
311void Service::removeFlushListener(
312 css::uno::Reference< css::util::XFlushListener > const & l)
313{
314 std::unique_lock g(m_aMutex);
316}
317
318void Service::setLocale(css::lang::Locale const & eLocale)
319{
320 osl::MutexGuard guard(*lock_);
321 locale_ = LanguageTag::convertToBcp47( eLocale, false);
322}
323
324css::lang::Locale Service::getLocale() {
325 osl::MutexGuard guard(*lock_);
326 css::lang::Locale loc;
327 if (! locale_.isEmpty()) {
329 }
330 return loc;
331}
332
333void 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();
337}
338
339void Service::flushModifications() const {
340 Components * components;
341 {
342 osl::MutexGuard guard(*lock_);
343 components = &Components::getSingleton(context_);
344 }
345 components->flushModifications();
346}
347
348extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
349com_sun_star_comp_configuration_ConfigurationProvider_get_implementation(
350 css::uno::XComponentContext* Context, css::uno::Sequence<css::uno::Any> const& Arguments)
351{
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 ("com.sun.star.configuration.ConfigurationProvider factory"
372 " expects NamedValue or PropertyValue arguments"),
373 nullptr);
374 }
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())
380 {
381 throw css::uno::Exception(
382 ("com.sun.star.configuration.ConfigurationProvider"
383 " factory expects at most one, non-empty, string"
384 " Locale argument"),
385 nullptr);
386 }
387 } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
388 throw css::uno::Exception(
389 ("com.sun.star.configuration.ConfigurationProvider factory:"
390 " unknown argument " + name),
391 nullptr);
392 }
393 }
394 return cppu::acquire(new Service(Context, locale));
395 }
396}
397
398}
399
400css::uno::Reference< css::uno::XInterface > createDefault(
401 css::uno::Reference< css::uno::XComponentContext > const & context)
402{
403 return getXWeak(new Service(context));
404}
405
406}
407
408/* 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)
sal_Int32 addInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
void notifyEach(std::unique_lock< std::mutex > &rGuard, void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event) const
sal_Int32 getLength(std::unique_lock< std::mutex > &rGuard) const
sal_Int32 removeInterface(std::unique_lock< std::mutex > &rGuard, const css::uno::Reference< ListenerT > &rxIFace)
static Components & getSingleton(css::uno::Reference< css::uno::XComponentContext > const &context)
Definition: components.cxx:199
Any value
OUString name
Definition: components.cxx:85
OUString locale_
bool default_
css::uno::Reference< css::uno::XComponentContext > context_
comphelper::OInterfaceContainerHelper4< css::util::XRefreshListener > maRefreshListeners
comphelper::OInterfaceContainerHelper4< css::util::XFlushListener > maFlushListeners
std::shared_ptr< osl::Mutex > lock_
float u
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