Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / configmgr / source / configurationprovider.cxx
blob4f0b5329155252771215324ffbb9e6e6d87f05fb
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 <vector>
25 #include "boost/noncopyable.hpp"
26 #include "com/sun/star/beans/NamedValue.hpp"
27 #include "com/sun/star/beans/PropertyValue.hpp"
28 #include "com/sun/star/configuration/theDefaultProvider.hpp"
29 #include "com/sun/star/lang/EventObject.hpp"
30 #include "com/sun/star/lang/Locale.hpp"
31 #include "com/sun/star/lang/XLocalizable.hpp"
32 #include "com/sun/star/lang/XMultiServiceFactory.hpp"
33 #include "com/sun/star/lang/XServiceInfo.hpp"
34 #include "com/sun/star/lang/XSingleComponentFactory.hpp"
35 #include "com/sun/star/uno/Any.hxx"
36 #include "com/sun/star/uno/Exception.hpp"
37 #include "com/sun/star/uno/Reference.hxx"
38 #include "com/sun/star/uno/RuntimeException.hpp"
39 #include "com/sun/star/uno/Sequence.hxx"
40 #include "com/sun/star/uno/XComponentContext.hpp"
41 #include "com/sun/star/uno/XInterface.hpp"
42 #include "com/sun/star/util/XFlushListener.hpp"
43 #include "com/sun/star/util/XFlushable.hpp"
44 #include "com/sun/star/util/XRefreshListener.hpp"
45 #include "com/sun/star/util/XRefreshable.hpp"
46 #include "comphelper/locale.hxx"
47 #include "cppu/unotype.hxx"
48 #include "cppuhelper/compbase5.hxx"
49 #include "cppuhelper/factory.hxx"
50 #include "cppuhelper/implbase2.hxx"
51 #include "cppuhelper/interfacecontainer.hxx"
52 #include "cppuhelper/weak.hxx"
53 #include "osl/mutex.hxx"
54 #include "sal/types.h"
55 #include "rtl/ref.hxx"
56 #include "rtl/unload.h"
57 #include "rtl/ustring.h"
58 #include "rtl/ustring.hxx"
60 #include "components.hxx"
61 #include "configurationprovider.hxx"
62 #include "lock.hxx"
63 #include "rootaccess.hxx"
65 namespace configmgr { namespace configuration_provider {
67 namespace {
69 char const accessServiceName[] =
70 "com.sun.star.configuration.ConfigurationAccess";
71 char const updateAccessServiceName[] =
72 "com.sun.star.configuration.ConfigurationUpdateAccess";
74 void badNodePath() {
75 throw css::uno::Exception(
76 rtl::OUString(
77 RTL_CONSTASCII_USTRINGPARAM(
78 "com.sun.star.configuration.ConfigurationProvider expects a"
79 " single, non-empty, string nodepath argument")),
80 0);
83 typedef
84 cppu::WeakComponentImplHelper5<
85 css::lang::XServiceInfo, css::lang::XMultiServiceFactory,
86 css::util::XRefreshable, css::util::XFlushable,
87 css::lang::XLocalizable >
88 ServiceBase;
90 class Service:
91 private osl::Mutex, public ServiceBase, private boost::noncopyable
93 public:
94 Service(
95 css::uno::Reference< css::uno::XComponentContext > const context,
96 rtl::OUString const & locale):
97 ServiceBase(*static_cast< osl::Mutex * >(this)), context_(context),
98 locale_(locale)
100 lock_ = lock();
101 assert(context.is());
104 private:
105 virtual ~Service() {}
107 virtual void SAL_CALL disposing() { flushModifications(); }
109 virtual rtl::OUString SAL_CALL getImplementationName()
110 throw (css::uno::RuntimeException)
111 { return configuration_provider::getImplementationName(); }
113 virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
114 throw (css::uno::RuntimeException)
115 { return ServiceName == getSupportedServiceNames()[0]; } //TODO
117 virtual css::uno::Sequence< rtl::OUString > SAL_CALL
118 getSupportedServiceNames() throw (css::uno::RuntimeException)
119 { return configuration_provider::getSupportedServiceNames(); }
121 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL createInstance(
122 rtl::OUString const & aServiceSpecifier)
123 throw (css::uno::Exception, css::uno::RuntimeException);
125 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
126 createInstanceWithArguments(
127 rtl::OUString const & ServiceSpecifier,
128 css::uno::Sequence< css::uno::Any > const & Arguments)
129 throw (css::uno::Exception, css::uno::RuntimeException);
131 virtual css::uno::Sequence< rtl::OUString > SAL_CALL
132 getAvailableServiceNames() throw (css::uno::RuntimeException);
134 virtual void SAL_CALL refresh() throw (css::uno::RuntimeException);
136 virtual void SAL_CALL addRefreshListener(
137 css::uno::Reference< css::util::XRefreshListener > const & l)
138 throw (css::uno::RuntimeException);
140 virtual void SAL_CALL removeRefreshListener(
141 css::uno::Reference< css::util::XRefreshListener > const & l)
142 throw (css::uno::RuntimeException);
144 virtual void SAL_CALL flush() throw (css::uno::RuntimeException);
146 virtual void SAL_CALL addFlushListener(
147 css::uno::Reference< css::util::XFlushListener > const & l)
148 throw (css::uno::RuntimeException);
150 virtual void SAL_CALL removeFlushListener(
151 css::uno::Reference< css::util::XFlushListener > const & l)
152 throw (css::uno::RuntimeException);
154 virtual void SAL_CALL setLocale(css::lang::Locale const & eLocale)
155 throw (css::uno::RuntimeException);
157 virtual css::lang::Locale SAL_CALL getLocale()
158 throw (css::uno::RuntimeException);
160 void flushModifications() const;
162 css::uno::Reference< css::uno::XComponentContext > context_;
163 rtl::OUString locale_;
164 boost::shared_ptr<osl::Mutex> lock_;
167 css::uno::Reference< css::uno::XInterface > Service::createInstance(
168 rtl::OUString const & aServiceSpecifier)
169 throw (css::uno::Exception, css::uno::RuntimeException)
171 return createInstanceWithArguments(
172 aServiceSpecifier, css::uno::Sequence< css::uno::Any >());
175 css::uno::Reference< css::uno::XInterface >
176 Service::createInstanceWithArguments(
177 rtl::OUString const & ServiceSpecifier,
178 css::uno::Sequence< css::uno::Any > const & Arguments)
179 throw (css::uno::Exception, css::uno::RuntimeException)
181 rtl::OUString nodepath;
182 rtl::OUString locale;
183 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
184 css::beans::NamedValue v1;
185 css::beans::PropertyValue v2;
186 rtl::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 rtl::OUString(
204 RTL_CONSTASCII_USTRINGPARAM(
205 "com.sun.star.configuration.ConfigurationProvider"
206 " expects NamedValue or PropertyValue arguments")),
209 // For backwards compatibility, allow "nodepath" and "Locale" in any
210 // case:
211 if (name.equalsIgnoreAsciiCaseAsciiL(
212 RTL_CONSTASCII_STRINGPARAM("nodepath")))
214 if (!nodepath.isEmpty() || !(value >>= nodepath) ||
215 nodepath.isEmpty())
217 badNodePath();
219 } else if (name.equalsIgnoreAsciiCaseAsciiL(
220 RTL_CONSTASCII_STRINGPARAM("locale")))
222 if (!locale.isEmpty() || !(value >>= locale) ||
223 locale.isEmpty())
225 throw css::uno::Exception(
226 rtl::OUString(
227 RTL_CONSTASCII_USTRINGPARAM(
228 "com.sun.star.configuration.ConfigurationProvider"
229 " expects at most one, non-empty, string Locale"
230 " argument")),
235 if (nodepath.isEmpty()) {
236 badNodePath();
238 // For backwards compatibility, allow a nodepath that misses the leading
239 // slash:
240 if (nodepath[0] != '/') {
241 nodepath = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("/")) + nodepath;
243 if (locale.isEmpty()) {
244 //TODO: should the Access use the dynamically changing locale_ instead?
245 locale = locale_;
246 if (locale.isEmpty()) {
247 locale = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM("en-US"));
250 bool update;
251 if ( ServiceSpecifier == accessServiceName )
253 update = false;
254 } else if ( ServiceSpecifier == updateAccessServiceName )
256 update = true;
257 } else {
258 throw css::uno::Exception(
259 (rtl::OUString(
260 RTL_CONSTASCII_USTRINGPARAM(
261 "com.sun.star.configuration.ConfigurationProvider does not"
262 " support service ")) +
263 ServiceSpecifier),
264 static_cast< cppu::OWeakObject * >(this));
266 osl::MutexGuard guard(*lock_);
267 Components & components = Components::getSingleton(context_);
268 rtl::Reference< RootAccess > root(
269 new RootAccess(components, nodepath, locale, update));
270 if (root->isValue()) {
271 throw css::uno::Exception(
272 (rtl::OUString(
273 RTL_CONSTASCII_USTRINGPARAM(
274 "com.sun.star.configuration.ConfigurationProvider: there is"
275 " a leaf value at nodepath ")) +
276 nodepath),
277 static_cast< cppu::OWeakObject * >(this));
279 components.addRootAccess(root);
280 return static_cast< cppu::OWeakObject * >(root.get());
283 css::uno::Sequence< rtl::OUString > Service::getAvailableServiceNames()
284 throw (css::uno::RuntimeException)
286 css::uno::Sequence< rtl::OUString > names(2);
287 names[0] = rtl::OUString(RTL_CONSTASCII_USTRINGPARAM(accessServiceName));
288 names[1] = rtl::OUString(
289 RTL_CONSTASCII_USTRINGPARAM(updateAccessServiceName));
290 return names;
293 void Service::refresh() throw (css::uno::RuntimeException) {
294 //TODO
295 cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
296 cppu::UnoType< css::util::XRefreshListener >::get());
297 if (cont != 0) {
298 css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
299 cont->notifyEach(&css::util::XRefreshListener::refreshed, ev);
303 void Service::addRefreshListener(
304 css::uno::Reference< css::util::XRefreshListener > const & l)
305 throw (css::uno::RuntimeException)
307 rBHelper.addListener(
308 cppu::UnoType< css::util::XRefreshListener >::get(), l);
311 void Service::removeRefreshListener(
312 css::uno::Reference< css::util::XRefreshListener > const & l)
313 throw (css::uno::RuntimeException)
315 rBHelper.removeListener(
316 cppu::UnoType< css::util::XRefreshListener >::get(), l);
319 void Service::flush() throw (css::uno::RuntimeException) {
320 flushModifications();
321 cppu::OInterfaceContainerHelper * cont = rBHelper.getContainer(
322 cppu::UnoType< css::util::XFlushListener >::get());
323 if (cont != 0) {
324 css::lang::EventObject ev(static_cast< cppu::OWeakObject * >(this));
325 cont->notifyEach(&css::util::XFlushListener::flushed, ev);
329 void Service::addFlushListener(
330 css::uno::Reference< css::util::XFlushListener > const & l)
331 throw (css::uno::RuntimeException)
333 rBHelper.addListener(cppu::UnoType< css::util::XFlushListener >::get(), l);
336 void Service::removeFlushListener(
337 css::uno::Reference< css::util::XFlushListener > const & l)
338 throw (css::uno::RuntimeException)
340 rBHelper.removeListener(
341 cppu::UnoType< css::util::XFlushListener >::get(), l);
344 void Service::setLocale(css::lang::Locale const & eLocale)
345 throw (css::uno::RuntimeException)
347 osl::MutexGuard guard(*lock_);
348 locale_ = comphelper::Locale(
349 eLocale.Language, eLocale.Country, eLocale.Variant).toISO();
352 css::lang::Locale Service::getLocale() throw (css::uno::RuntimeException) {
353 osl::MutexGuard guard(*lock_);
354 css::lang::Locale loc;
355 if ( locale_ == "*" ) {
356 loc.Language = locale_;
357 } else if (! locale_.isEmpty()) {
358 try {
359 comphelper::Locale l(locale_);
360 loc.Language = l.getLanguage();
361 loc.Country = l.getCountry();
362 loc.Variant = l.getVariant();
363 } catch (comphelper::Locale::MalFormedLocaleException & e) {
364 throw css::uno::RuntimeException(
365 (rtl::OUString(
366 RTL_CONSTASCII_USTRINGPARAM("MalformedLocaleException: ")) +
367 e.Message),
368 static_cast< cppu::OWeakObject * >(this));
371 return loc;
374 void Service::flushModifications() const {
375 Components * components;
377 osl::MutexGuard guard(*lock_);
378 components = &Components::getSingleton(context_);
380 components->flushModifications();
383 class Factory:
384 public cppu::WeakImplHelper2<
385 css::lang::XSingleComponentFactory, css::lang::XServiceInfo >,
386 private boost::noncopyable
388 public:
389 Factory() {}
391 private:
392 virtual ~Factory() {}
394 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
395 createInstanceWithContext(
396 css::uno::Reference< css::uno::XComponentContext > const & Context)
397 throw (css::uno::Exception, css::uno::RuntimeException);
399 virtual css::uno::Reference< css::uno::XInterface > SAL_CALL
400 createInstanceWithArgumentsAndContext(
401 css::uno::Sequence< css::uno::Any > const & Arguments,
402 css::uno::Reference< css::uno::XComponentContext > const & Context)
403 throw (css::uno::Exception, css::uno::RuntimeException);
405 virtual rtl::OUString SAL_CALL getImplementationName()
406 throw (css::uno::RuntimeException)
407 { return configuration_provider::getImplementationName(); }
409 virtual sal_Bool SAL_CALL supportsService(rtl::OUString const & ServiceName)
410 throw (css::uno::RuntimeException)
411 { return ServiceName == getSupportedServiceNames()[0]; } //TODO
413 virtual css::uno::Sequence< rtl::OUString > SAL_CALL
414 getSupportedServiceNames() throw (css::uno::RuntimeException)
415 { return configuration_provider::getSupportedServiceNames(); }
418 css::uno::Reference< css::uno::XInterface > Factory::createInstanceWithContext(
419 css::uno::Reference< css::uno::XComponentContext > const & Context)
420 throw (css::uno::Exception, css::uno::RuntimeException)
422 return createInstanceWithArgumentsAndContext(
423 css::uno::Sequence< css::uno::Any >(), Context);
426 css::uno::Reference< css::uno::XInterface >
427 Factory::createInstanceWithArgumentsAndContext(
428 css::uno::Sequence< css::uno::Any > const & Arguments,
429 css::uno::Reference< css::uno::XComponentContext > const & Context)
430 throw (css::uno::Exception, css::uno::RuntimeException)
432 if (Arguments.getLength() == 0) {
433 return css::configuration::theDefaultProvider::get(Context);
434 } else {
435 rtl::OUString locale;
436 for (sal_Int32 i = 0; i < Arguments.getLength(); ++i) {
437 css::beans::NamedValue v1;
438 css::beans::PropertyValue v2;
439 rtl::OUString name;
440 css::uno::Any value;
441 if (Arguments[i] >>= v1) {
442 name = v1.Name;
443 value = v1.Value;
444 } else if (Arguments[i] >>= v2) {
445 name = v2.Name;
446 value = v2.Value;
447 } else {
448 throw css::uno::Exception(
449 rtl::OUString(
450 RTL_CONSTASCII_USTRINGPARAM(
451 "com.sun.star.configuration.ConfigurationProvider"
452 " factory expects NamedValue or PropertyValue"
453 " arguments")),
456 // For backwards compatibility, allow "Locale" and (ignored)
457 // "EnableAsync" in any case:
458 if (name.equalsIgnoreAsciiCaseAsciiL(
459 RTL_CONSTASCII_STRINGPARAM("locale")))
461 if (!locale.isEmpty() || !(value >>= locale) ||
462 locale.isEmpty())
464 throw css::uno::Exception(
465 rtl::OUString(
466 RTL_CONSTASCII_USTRINGPARAM(
467 "com.sun.star.configuration."
468 "ConfigurationProvider factory expects at most"
469 " one, non-empty, string Locale argument")),
472 } else if (!name.equalsIgnoreAsciiCaseAsciiL(
473 RTL_CONSTASCII_STRINGPARAM("enableasync")))
475 throw css::uno::Exception(
476 rtl::OUString(
477 RTL_CONSTASCII_USTRINGPARAM(
478 "com.sun.star.configuration.ConfigurationProvider"
479 " factory: unknown argument ")) + name,
483 return static_cast< cppu::OWeakObject * >(new Service(Context, locale));
489 css::uno::Reference< css::uno::XInterface > createDefault(
490 css::uno::Reference< css::uno::XComponentContext > const & context)
492 return static_cast< cppu::OWeakObject * >(
493 new Service(context, rtl::OUString()));
496 rtl::OUString getImplementationName() {
497 return rtl::OUString(
498 RTL_CONSTASCII_USTRINGPARAM(
499 "com.sun.star.comp.configuration.ConfigurationProvider"));
502 css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
503 rtl::OUString name(
504 RTL_CONSTASCII_USTRINGPARAM(
505 "com.sun.star.configuration.ConfigurationProvider"));
506 return css::uno::Sequence< rtl::OUString >(&name, 1);
509 css::uno::Reference< css::lang::XSingleComponentFactory >
510 createFactory(
511 SAL_UNUSED_PARAMETER cppu::ComponentFactoryFunc,
512 SAL_UNUSED_PARAMETER rtl::OUString const &,
513 SAL_UNUSED_PARAMETER css::uno::Sequence< rtl::OUString > const &,
514 SAL_UNUSED_PARAMETER rtl_ModuleCount *)
515 SAL_THROW(())
517 return new Factory;
522 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */