Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / configmgr / source / configurationregistry.cxx
blobf27e3c7b88cc10ecf77bef0015a912d89441a908
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>
24 #include <com/sun/star/beans/NamedValue.hpp>
25 #include <com/sun/star/container/NoSuchElementException.hpp>
26 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
27 #include <com/sun/star/container/XNamed.hpp>
28 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
29 #include <com/sun/star/lang/XServiceInfo.hpp>
30 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
31 #include <com/sun/star/registry/InvalidRegistryException.hpp>
32 #include <com/sun/star/registry/InvalidValueException.hpp>
33 #include <com/sun/star/registry/RegistryKeyType.hpp>
34 #include <com/sun/star/registry/RegistryValueType.hpp>
35 #include <com/sun/star/registry/XRegistryKey.hpp>
36 #include <com/sun/star/registry/XSimpleRegistry.hpp>
37 #include <com/sun/star/uno/Any.hxx>
38 #include <com/sun/star/uno/DeploymentException.hpp>
39 #include <com/sun/star/uno/Exception.hpp>
40 #include <com/sun/star/uno/Reference.hxx>
41 #include <com/sun/star/uno/RuntimeException.hpp>
42 #include <com/sun/star/uno/Sequence.hxx>
43 #include <com/sun/star/uno/Type.hxx>
44 #include <com/sun/star/uno/TypeClass.hpp>
45 #include <com/sun/star/uno/XComponentContext.hpp>
46 #include <com/sun/star/uno/XInterface.hpp>
47 #include <com/sun/star/util/XFlushable.hpp>
48 #include <cppu/unotype.hxx>
49 #include <cppuhelper/exc_hlp.hxx>
50 #include <cppuhelper/implbase.hxx>
51 #include <cppuhelper/supportsservice.hxx>
52 #include <cppuhelper/weak.hxx>
53 #include <osl/mutex.hxx>
54 #include <rtl/ustring.hxx>
55 #include <sal/types.h>
57 #include "configurationregistry.hxx"
59 namespace com { namespace sun { namespace star { namespace util {
60 class XFlushListener;
61 } } } }
63 namespace configmgr { namespace configuration_registry {
65 namespace {
67 class Service:
68 public cppu::WeakImplHelper<
69 css::lang::XServiceInfo, css::registry::XSimpleRegistry,
70 css::util::XFlushable >
72 public:
73 explicit Service(css::uno::Reference< css::uno::XComponentContext > const & context);
75 private:
76 Service(const Service&) = delete;
77 Service& operator=(const Service&) = delete;
79 virtual ~Service() override {}
81 virtual OUString SAL_CALL getImplementationName() override
82 { return configuration_registry::getImplementationName(); }
84 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
85 { return cppu::supportsService(this, ServiceName); }
87 virtual css::uno::Sequence< OUString > SAL_CALL
88 getSupportedServiceNames() override
89 { return configuration_registry::getSupportedServiceNames(); }
91 virtual OUString SAL_CALL getURL() override;
93 virtual void SAL_CALL open(
94 OUString const & rURL, sal_Bool bReadOnly, sal_Bool) override;
96 virtual sal_Bool SAL_CALL isValid() override;
98 virtual void SAL_CALL close() override;
100 virtual void SAL_CALL destroy() override;
102 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL
103 getRootKey() override;
105 virtual sal_Bool SAL_CALL isReadOnly() override;
107 virtual void SAL_CALL mergeKey(OUString const &, OUString const &) override;
109 virtual void SAL_CALL flush() override;
111 virtual void SAL_CALL addFlushListener(
112 css::uno::Reference< css::util::XFlushListener > const &) override;
114 virtual void SAL_CALL removeFlushListener(
115 css::uno::Reference< css::util::XFlushListener > const &) override;
117 void checkValid();
119 void checkValid_RuntimeException();
121 void doClose();
123 css::uno::Reference< css::lang::XMultiServiceFactory > provider_;
124 osl::Mutex mutex_;
125 css::uno::Reference< css::uno::XInterface > access_;
126 OUString url_;
127 bool readOnly_;
129 friend class RegistryKey;
132 class RegistryKey:
133 public cppu::WeakImplHelper< css::registry::XRegistryKey >
135 public:
136 RegistryKey(Service & service, css::uno::Any const & value):
137 service_(service), value_(value) {}
139 private:
140 RegistryKey(const RegistryKey&) = delete;
141 RegistryKey& operator=(const RegistryKey&) = delete;
143 virtual ~RegistryKey() override {}
145 virtual OUString SAL_CALL getKeyName() override;
147 virtual sal_Bool SAL_CALL isReadOnly() override;
149 virtual sal_Bool SAL_CALL isValid() override;
151 virtual css::registry::RegistryKeyType SAL_CALL getKeyType(
152 OUString const &) override;
154 virtual css::registry::RegistryValueType SAL_CALL getValueType() override;
156 virtual sal_Int32 SAL_CALL getLongValue() override;
158 virtual void SAL_CALL setLongValue(sal_Int32) override;
160 virtual css::uno::Sequence< sal_Int32 > SAL_CALL getLongListValue() override;
162 virtual void SAL_CALL setLongListValue(
163 css::uno::Sequence< sal_Int32 > const &) override;
165 virtual OUString SAL_CALL getAsciiValue() override;
167 virtual void SAL_CALL setAsciiValue(OUString const &) override;
169 virtual css::uno::Sequence< OUString > SAL_CALL getAsciiListValue() override;
171 virtual void SAL_CALL setAsciiListValue(
172 css::uno::Sequence< OUString > const &) override;
174 virtual OUString SAL_CALL getStringValue() override;
176 virtual void SAL_CALL setStringValue(OUString const &) override;
178 virtual css::uno::Sequence< OUString > SAL_CALL getStringListValue() override;
180 virtual void SAL_CALL setStringListValue(
181 css::uno::Sequence< OUString > const &) override;
183 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBinaryValue() override;
185 virtual void SAL_CALL setBinaryValue(css::uno::Sequence< sal_Int8 > const &) override;
187 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL openKey(
188 OUString const & aKeyName) override;
190 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL
191 createKey(OUString const &) override;
193 virtual void SAL_CALL closeKey() override;
195 virtual void SAL_CALL deleteKey(OUString const &) override;
197 virtual
198 css::uno::Sequence< css::uno::Reference< css::registry::XRegistryKey > >
199 SAL_CALL openKeys() override;
201 virtual css::uno::Sequence< OUString > SAL_CALL getKeyNames() override;
203 virtual sal_Bool SAL_CALL createLink(
204 OUString const &, OUString const &) override;
206 virtual void SAL_CALL deleteLink(OUString const &) override;
208 virtual OUString SAL_CALL getLinkTarget(OUString const &) override;
210 virtual OUString SAL_CALL getResolvedName(
211 OUString const & aKeyName) override;
213 Service & service_;
214 css::uno::Any value_;
217 Service::Service(
218 css::uno::Reference< css::uno::XComponentContext > const & context)
219 : readOnly_(false)
221 assert(context.is());
222 try {
223 provider_.set(
224 context->getServiceManager()->createInstanceWithContext(
225 "com.sun.star.configuration.DefaultProvider", context),
226 css::uno::UNO_QUERY_THROW);
227 } catch (css::uno::RuntimeException &) {
228 throw;
229 } catch (css::uno::Exception & e) {
230 throw css::uno::DeploymentException(
231 ("component context fails to supply service"
232 " com.sun.star.configuration.DefaultProvider of type"
233 " com.sun.star.lang.XMultiServiceFactory: " + e.Message),
234 context);
238 OUString Service::getURL() {
239 osl::MutexGuard g(mutex_);
240 checkValid_RuntimeException();
241 return url_;
244 void Service::open(OUString const & rURL, sal_Bool bReadOnly, sal_Bool)
246 //TODO: bCreate
247 osl::MutexGuard g(mutex_);
248 if (access_.is()) {
249 doClose();
251 css::uno::Sequence< css::uno::Any > args(1);
252 args[0] <<= css::beans::NamedValue("nodepath", css::uno::Any(rURL));
253 try {
254 access_ = provider_->createInstanceWithArguments(
255 (bReadOnly
256 ? OUString("com.sun.star.configuration.ConfigurationAccess")
257 : OUString(
258 "com.sun.star.configuration.ConfigurationUpdateAccess")),
259 args);
260 } catch (css::uno::RuntimeException &) {
261 throw;
262 } catch (css::uno::Exception & e) {
263 css::uno::Any anyEx = cppu::getCaughtException();
264 throw css::lang::WrappedTargetRuntimeException(
265 "com.sun.star.configuration.ConfigurationRegistry: open failed: " +
266 e.Message,
267 static_cast< cppu::OWeakObject * >(this), anyEx );
269 url_ = rURL;
270 readOnly_ = bReadOnly;
273 sal_Bool Service::isValid() {
274 osl::MutexGuard g(mutex_);
275 return access_.is();
278 void Service::close()
280 osl::MutexGuard g(mutex_);
281 checkValid();
282 doClose();
285 void Service::destroy()
287 throw css::uno::RuntimeException(
288 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
289 static_cast< cppu::OWeakObject * >(this));
292 css::uno::Reference< css::registry::XRegistryKey > Service::getRootKey()
294 osl::MutexGuard g(mutex_);
295 checkValid();
296 return new RegistryKey(*this, css::uno::Any(access_));
299 sal_Bool Service::isReadOnly() {
300 osl::MutexGuard g(mutex_);
301 checkValid_RuntimeException();
302 return readOnly_;
305 void Service::mergeKey(OUString const &, OUString const &)
307 throw css::uno::RuntimeException(
308 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
309 static_cast< cppu::OWeakObject * >(this));
312 void Service::flush()
314 throw css::uno::RuntimeException(
315 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
316 static_cast< cppu::OWeakObject * >(this));
319 void Service::addFlushListener(
320 css::uno::Reference< css::util::XFlushListener > const &)
322 throw css::uno::RuntimeException(
323 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
324 static_cast< cppu::OWeakObject * >(this));
327 void Service::removeFlushListener(
328 css::uno::Reference< css::util::XFlushListener > const &)
330 throw css::uno::RuntimeException(
331 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
332 static_cast< cppu::OWeakObject * >(this));
335 void Service::checkValid() {
336 if (!access_.is()) {
337 throw css::registry::InvalidRegistryException(
338 "com.sun.star.configuration.ConfigurationRegistry: not valid",
339 static_cast< cppu::OWeakObject * >(this));
343 void Service::checkValid_RuntimeException() {
344 if (!access_.is()) {
345 throw css::uno::RuntimeException(
346 "com.sun.star.configuration.ConfigurationRegistry: not valid",
347 static_cast< cppu::OWeakObject * >(this));
351 void Service::doClose() {
352 access_.clear();
355 OUString RegistryKey::getKeyName() {
356 osl::MutexGuard g(service_.mutex_);
357 service_.checkValid_RuntimeException();
358 css::uno::Reference< css::container::XNamed > named;
359 if (value_ >>= named) {
360 return named->getName();
362 throw css::uno::RuntimeException(
363 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
364 static_cast< cppu::OWeakObject * >(this));
367 sal_Bool RegistryKey::isReadOnly()
369 osl::MutexGuard g(service_.mutex_);
370 service_.checkValid_RuntimeException();
371 return service_.readOnly_; //TODO: read-only sub-nodes in update access?
374 sal_Bool RegistryKey::isValid() {
375 return service_.isValid();
378 css::registry::RegistryKeyType RegistryKey::getKeyType(OUString const &)
380 osl::MutexGuard g(service_.mutex_);
381 service_.checkValid();
382 return css::registry::RegistryKeyType_KEY;
385 css::registry::RegistryValueType RegistryKey::getValueType()
387 osl::MutexGuard g(service_.mutex_);
388 service_.checkValid();
389 css::uno::Type t(value_.getValueType());
390 switch (t.getTypeClass()) {
391 case css::uno::TypeClass_LONG:
392 return css::registry::RegistryValueType_LONG;
393 case css::uno::TypeClass_STRING:
394 return css::registry::RegistryValueType_STRING;
395 case css::uno::TypeClass_SEQUENCE:
396 if (t == cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()) {
397 return css::registry::RegistryValueType_BINARY;
398 } else if (t == cppu::UnoType< css::uno::Sequence< sal_Int32 > >::get())
400 return css::registry::RegistryValueType_LONGLIST;
401 } else if (t ==
402 cppu::UnoType< css::uno::Sequence< OUString > >::get())
404 return css::registry::RegistryValueType_STRINGLIST;
406 [[fallthrough]];
407 default:
408 return css::registry::RegistryValueType_NOT_DEFINED;
412 sal_Int32 RegistryKey::getLongValue()
414 osl::MutexGuard g(service_.mutex_);
415 service_.checkValid();
416 sal_Int32 v = 0;
417 if (value_ >>= v) {
418 return v;
420 throw css::registry::InvalidValueException(
421 "com.sun.star.configuration.ConfigurationRegistry",
422 static_cast< cppu::OWeakObject * >(this));
425 void RegistryKey::setLongValue(sal_Int32)
427 throw css::uno::RuntimeException(
428 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
429 static_cast< cppu::OWeakObject * >(this));
432 css::uno::Sequence< sal_Int32 > RegistryKey::getLongListValue()
434 osl::MutexGuard g(service_.mutex_);
435 service_.checkValid();
436 css::uno::Sequence< sal_Int32 > v;
437 if (value_ >>= v) {
438 return v;
440 throw css::registry::InvalidValueException(
441 "com.sun.star.configuration.ConfigurationRegistry",
442 static_cast< cppu::OWeakObject * >(this));
445 void RegistryKey::setLongListValue(css::uno::Sequence< sal_Int32 > const &)
447 throw css::uno::RuntimeException(
448 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
449 static_cast< cppu::OWeakObject * >(this));
452 OUString RegistryKey::getAsciiValue()
454 osl::MutexGuard g(service_.mutex_);
455 service_.checkValid();
456 OUString v;
457 if (value_ >>= v) {
458 return v;
460 throw css::registry::InvalidValueException(
461 "com.sun.star.configuration.ConfigurationRegistry",
462 static_cast< cppu::OWeakObject * >(this));
465 void RegistryKey::setAsciiValue(OUString const &)
467 throw css::uno::RuntimeException(
468 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
469 static_cast< cppu::OWeakObject * >(this));
472 css::uno::Sequence< OUString > RegistryKey::getAsciiListValue()
474 osl::MutexGuard g(service_.mutex_);
475 service_.checkValid();
476 css::uno::Sequence< OUString > v;
477 if (value_ >>= v) {
478 return v;
480 throw css::registry::InvalidValueException(
481 "com.sun.star.configuration.ConfigurationRegistry",
482 static_cast< cppu::OWeakObject * >(this));
485 void RegistryKey::setAsciiListValue(css::uno::Sequence< OUString > const &)
487 throw css::uno::RuntimeException(
488 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
489 static_cast< cppu::OWeakObject * >(this));
492 OUString RegistryKey::getStringValue()
494 osl::MutexGuard g(service_.mutex_);
495 service_.checkValid();
496 OUString v;
497 if (value_ >>= v) {
498 return v;
500 throw css::registry::InvalidValueException(
501 "com.sun.star.configuration.ConfigurationRegistry",
502 static_cast< cppu::OWeakObject * >(this));
505 void RegistryKey::setStringValue(OUString const &)
507 throw css::uno::RuntimeException(
508 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
509 static_cast< cppu::OWeakObject * >(this));
512 css::uno::Sequence< OUString > RegistryKey::getStringListValue()
514 osl::MutexGuard g(service_.mutex_);
515 service_.checkValid();
516 css::uno::Sequence< OUString > v;
517 if (value_ >>= v) {
518 return v;
520 throw css::registry::InvalidValueException(
521 "com.sun.star.configuration.ConfigurationRegistry",
522 static_cast< cppu::OWeakObject * >(this));
525 void RegistryKey::setStringListValue(
526 css::uno::Sequence< OUString > const &)
528 throw css::uno::RuntimeException(
529 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
530 static_cast< cppu::OWeakObject * >(this));
533 css::uno::Sequence< sal_Int8 > RegistryKey::getBinaryValue()
535 osl::MutexGuard g(service_.mutex_);
536 service_.checkValid();
537 css::uno::Sequence< sal_Int8 > v;
538 if (value_ >>= v) {
539 return v;
541 throw css::registry::InvalidValueException(
542 "com.sun.star.configuration.ConfigurationRegistry",
543 static_cast< cppu::OWeakObject * >(this));
546 void RegistryKey::setBinaryValue(css::uno::Sequence< sal_Int8 > const &)
548 throw css::uno::RuntimeException(
549 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
550 static_cast< cppu::OWeakObject * >(this));
553 css::uno::Reference< css::registry::XRegistryKey > RegistryKey::openKey(
554 OUString const & aKeyName)
556 osl::MutexGuard g(service_.mutex_);
557 service_.checkValid_RuntimeException();
558 css::uno::Reference< css::container::XHierarchicalNameAccess > access;
559 if (value_ >>= access) {
560 try {
561 return new RegistryKey(
562 service_, access->getByHierarchicalName(aKeyName));
563 } catch (css::container::NoSuchElementException &) {}
565 return css::uno::Reference< css::registry::XRegistryKey >();
568 css::uno::Reference< css::registry::XRegistryKey > RegistryKey::createKey(
569 OUString const &)
571 throw css::uno::RuntimeException(
572 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
573 static_cast< cppu::OWeakObject * >(this));
576 void RegistryKey::closeKey()
578 osl::MutexGuard g(service_.mutex_);
579 service_.checkValid_RuntimeException();
582 void RegistryKey::deleteKey(OUString const &)
584 throw css::uno::RuntimeException(
585 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
586 static_cast< cppu::OWeakObject * >(this));
589 css::uno::Sequence< css::uno::Reference< css::registry::XRegistryKey > >
590 RegistryKey::openKeys()
592 throw css::uno::RuntimeException(
593 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
594 static_cast< cppu::OWeakObject * >(this));
597 css::uno::Sequence< OUString > RegistryKey::getKeyNames()
599 throw css::uno::RuntimeException(
600 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
601 static_cast< cppu::OWeakObject * >(this));
604 sal_Bool RegistryKey::createLink(OUString const &, OUString const &)
606 osl::MutexGuard g(service_.mutex_);
607 service_.checkValid_RuntimeException();
608 return false;
611 void RegistryKey::deleteLink(OUString const &)
613 osl::MutexGuard g(service_.mutex_);
614 service_.checkValid_RuntimeException();
617 OUString RegistryKey::getLinkTarget(OUString const &)
619 osl::MutexGuard g(service_.mutex_);
620 service_.checkValid_RuntimeException();
621 return OUString();
624 OUString RegistryKey::getResolvedName(OUString const & aKeyName)
626 osl::MutexGuard g(service_.mutex_);
627 service_.checkValid_RuntimeException();
628 return aKeyName;
633 css::uno::Reference< css::uno::XInterface > create(
634 css::uno::Reference< css::uno::XComponentContext > const & context)
636 return static_cast< cppu::OWeakObject * >(new Service(context));
639 OUString getImplementationName() {
640 return "com.sun.star.comp.configuration.ConfigurationRegistry";
643 css::uno::Sequence< OUString > getSupportedServiceNames() {
644 return css::uno::Sequence< OUString > { "com.sun.star.configuration.ConfigurationRegistry" };
649 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */