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>
42 #include <cppuhelper/basemutex.hxx>
43 #include <cppuhelper/compbase.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 
53 #include "components.hxx"
55 #include "lock.hxx"
56 #include "defaultprovider.hxx"
57 #include "rootaccess.hxx"
58 
60 
61 namespace {
62 
63 constexpr OUStringLiteral accessServiceName =
64  u"com.sun.star.configuration.ConfigurationAccess";
65 constexpr OUStringLiteral updateAccessServiceName =
66  u"com.sun.star.configuration.ConfigurationUpdateAccess";
67 
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);
73 }
74 
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;
81 
82 class Service:
83  private cppu::BaseMutex, public ServiceBase
84 {
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() )
90  {
91  assert(context.is());
92  }
93 
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() )
100  {
101  assert(context.is());
102  }
103 
104 private:
105  Service(const Service&) = delete;
106  Service& operator=(const Service&) = delete;
107 
108  virtual ~Service() override {}
109 
110  virtual void SAL_CALL disposing() override { flushModifications(); }
111 
112  virtual OUString SAL_CALL getImplementationName() override
113  {
114  return default_
116  : "com.sun.star.comp.configuration.ConfigurationProvider";
117  }
118 
119  virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
120  { return cppu::supportsService(this, ServiceName); }
121 
122  virtual css::uno::Sequence< OUString > SAL_CALL
123  getSupportedServiceNames() override
124  {
125  return default_
127  : css::uno::Sequence<OUString> { "com.sun.star.configuration.ConfigurationProvider" };
128  }
129 
130  virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
131  OUString const & aServiceSpecifier) override;
132 
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;
137 
138  virtual css::uno::Sequence< OUString > SAL_CALL
139  getAvailableServiceNames() override;
140 
141  virtual void SAL_CALL refresh() override;
142 
143  virtual void SAL_CALL addRefreshListener(
144  css::uno::Reference< css::util::XRefreshListener > const & l) override;
145 
146  virtual void SAL_CALL removeRefreshListener(
147  css::uno::Reference< css::util::XRefreshListener > const & l) override;
148 
149  virtual void SAL_CALL flush() override;
150 
151  virtual void SAL_CALL addFlushListener(
152  css::uno::Reference< css::util::XFlushListener > const & l) override;
153 
154  virtual void SAL_CALL removeFlushListener(
155  css::uno::Reference< css::util::XFlushListener > const & l) override;
156 
157  virtual void SAL_CALL setLocale(css::lang::Locale const & eLocale) override;
158 
159  virtual css::lang::Locale SAL_CALL getLocale() override;
160 
161  void flushModifications() const;
162 
163  css::uno::Reference< css::uno::XComponentContext > context_;
164  OUString locale_;
165  bool default_;
166  std::shared_ptr<osl::Mutex> lock_;
167 };
168 
169 css::uno::Reference< css::uno::XInterface > Service::createInstance(
170  OUString const & aServiceSpecifier)
171 {
172  return createInstanceWithArguments(
173  aServiceSpecifier, css::uno::Sequence< css::uno::Any >());
174 }
175 
176 css::uno::Reference< css::uno::XInterface >
177 Service::createInstanceWithArguments(
178  OUString const & ServiceSpecifier,
179  css::uno::Sequence< css::uno::Any > const & Arguments)
180 {
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();
199  }
200  break;
201  } else {
202  throw css::uno::Exception(
203  ("com.sun.star.configuration.ConfigurationProvider expects"
204  " NamedValue or PropertyValue arguments"),
205  nullptr);
206  }
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())
212  {
213  badNodePath();
214  }
215  } else if (name.equalsIgnoreAsciiCase("locale")) {
216  if (!locale.isEmpty() || !(value >>= locale) ||
217  locale.isEmpty())
218  {
219  throw css::uno::Exception(
220  ("com.sun.star.configuration.ConfigurationProvider expects"
221  " at most one, non-empty, string Locale argument"),
222  nullptr);
223  }
224  }
225  }
226  if (nodepath.isEmpty()) {
227  badNodePath();
228  }
229  // For backwards compatibility, allow a nodepath that misses the leading
230  // slash:
231  if (nodepath[0] != '/') {
232  nodepath = "/" + nodepath;
233  }
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";
239  }
240  }
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));
251  }
252  osl::MutexGuard guard(*lock_);
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));
261  }
262  components.addRootAccess(root);
263  return static_cast< cppu::OWeakObject * >(root.get());
264 }
265 
266 css::uno::Sequence< OUString > Service::getAvailableServiceNames()
267 {
268  return { accessServiceName, updateAccessServiceName };
269 }
270 
271 void Service::refresh() {
272  //TODO
273  cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
275  if (cont != nullptr) {
276  css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
277  cont->notifyEach(&css::util::XRefreshListener::refreshed, ev);
278  }
279 }
280 
281 void Service::addRefreshListener(
282  css::uno::Reference< css::util::XRefreshListener > const & l)
283 {
284  rBHelper.addListener(
286 }
287 
288 void Service::removeRefreshListener(
289  css::uno::Reference< css::util::XRefreshListener > const & l)
290 {
291  rBHelper.removeListener(
293 }
294 
295 void Service::flush() {
296  flushModifications();
297  cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
299  if (cont != nullptr) {
300  css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
301  cont->notifyEach(&css::util::XFlushListener::flushed, ev);
302  }
303 }
304 
305 void Service::addFlushListener(
306  css::uno::Reference< css::util::XFlushListener > const & l)
307 {
308  rBHelper.addListener(cppu::UnoType< css::util::XFlushListener >::get(), l);
309 }
310 
311 void Service::removeFlushListener(
312  css::uno::Reference< css::util::XFlushListener > const & l)
313 {
314  rBHelper.removeListener(
316 }
317 
318 void Service::setLocale(css::lang::Locale const & eLocale)
319 {
320  osl::MutexGuard guard(*lock_);
321  locale_ = LanguageTag::convertToBcp47( eLocale, false);
322 }
323 
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);
329  }
330  return loc;
331 }
332 
333 void Service::flushModifications() const {
334  Components * components;
335  {
336  osl::MutexGuard guard(*lock_);
337  components = &Components::getSingleton(context_);
338  }
339  components->flushModifications();
340 }
341 
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)
345 {
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);
368  }
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())
374  {
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);
380  }
381  } else if (!name.equalsIgnoreAsciiCase("enableasync")) {
382  throw css::uno::Exception(
383  ("com.sun.star.configuration.ConfigurationProvider factory:"
384  " unknown argument " + name),
385  nullptr);
386  }
387  }
388  return cppu::acquire(new Service(Context, locale));
389  }
390 }
391 
392 }
393 
394 css::uno::Reference< css::uno::XInterface > createDefault(
395  css::uno::Reference< css::uno::XComponentContext > const & context)
396 {
397  return static_cast< cppu::OWeakObject * >(new Service(context));
398 }
399 
400 }
401 
402 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
OUString locale_
osl::Mutex m_aMutex
bool update()
static OUString convertToBcp47(LanguageType nLangID)
css::uno::Sequence< OUString > getSupportedServiceNames()
css::uno::Reference< css::uno::XComponentContext > context_
bool default_
const BorderLinePrimitive2D *pCandidateB assert(pCandidateA)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
int i
HRESULT createInstance(REFIID iid, Ifc **ppIfc)
float u
unsigned char sal_Bool
css::uno::Reference< css::uno::XInterface > createDefault(css::uno::Reference< css::uno::XComponentContext > const &context)
const LanguageTag & getLocale()
std::vector< uno::Reference< sheet::XSpreadsheetDocument > > Components
void * p
Any value
void notifyEach(void(SAL_CALL ListenerT::*NotificationMethod)(const EventT &), const EventT &Event)
OUString name
Definition: components.cxx:83
std::shared_ptr< osl::Mutex > lock_
std::shared_ptr< osl::Mutex > const & lock()
Definition: lock.cxx:28
static Components & getSingleton(css::uno::Reference< css::uno::XComponentContext > const &context)
Definition: components.cxx:197
static css::lang::Locale convertToLocale(LanguageType nLangID, bool bResolveSystem=true)
void setLocale(const LanguageTag &languageTag)