Bump version to 6.0-36
[LibreOffice.git] / configmgr / source / configurationregistry.cxx
blob65a24c6903fae4b99d6ac15ed88b96644acd43c8
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/beans/Property.hpp>
26 #include <com/sun/star/beans/XProperty.hpp>
27 #include <com/sun/star/container/NoSuchElementException.hpp>
28 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
29 #include <com/sun/star/container/XNamed.hpp>
30 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
31 #include <com/sun/star/lang/XServiceInfo.hpp>
32 #include <com/sun/star/registry/InvalidRegistryException.hpp>
33 #include <com/sun/star/registry/InvalidValueException.hpp>
34 #include <com/sun/star/registry/MergeConflictException.hpp>
35 #include <com/sun/star/registry/RegistryKeyType.hpp>
36 #include <com/sun/star/registry/RegistryValueType.hpp>
37 #include <com/sun/star/registry/XRegistryKey.hpp>
38 #include <com/sun/star/registry/XSimpleRegistry.hpp>
39 #include <com/sun/star/uno/Any.hxx>
40 #include <com/sun/star/uno/DeploymentException.hpp>
41 #include <com/sun/star/uno/Exception.hpp>
42 #include <com/sun/star/uno/Reference.hxx>
43 #include <com/sun/star/uno/RuntimeException.hpp>
44 #include <com/sun/star/uno/Sequence.hxx>
45 #include <com/sun/star/uno/Type.hxx>
46 #include <com/sun/star/uno/TypeClass.hpp>
47 #include <com/sun/star/uno/XComponentContext.hpp>
48 #include <com/sun/star/uno/XInterface.hpp>
49 #include <com/sun/star/util/XFlushable.hpp>
50 #include <cppu/unotype.hxx>
51 #include <cppuhelper/implbase.hxx>
52 #include <cppuhelper/supportsservice.hxx>
53 #include <cppuhelper/weak.hxx>
54 #include <osl/mutex.hxx>
55 #include <rtl/ustring.h>
56 #include <rtl/ustring.hxx>
57 #include <sal/types.h>
59 #include "configurationregistry.hxx"
61 namespace com { namespace sun { namespace star { namespace util {
62 class XFlushListener;
63 } } } }
65 namespace configmgr { namespace configuration_registry {
67 namespace {
69 class Service:
70 public cppu::WeakImplHelper<
71 css::lang::XServiceInfo, css::registry::XSimpleRegistry,
72 css::util::XFlushable >
74 public:
75 explicit Service(css::uno::Reference< css::uno::XComponentContext > const & context);
77 private:
78 Service(const Service&) = delete;
79 Service& operator=(const Service&) = delete;
81 virtual ~Service() override {}
83 virtual OUString SAL_CALL getImplementationName() override
84 { return configuration_registry::getImplementationName(); }
86 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
87 { return cppu::supportsService(this, ServiceName); }
89 virtual css::uno::Sequence< OUString > SAL_CALL
90 getSupportedServiceNames() override
91 { return configuration_registry::getSupportedServiceNames(); }
93 virtual OUString SAL_CALL getURL() override;
95 virtual void SAL_CALL open(
96 OUString const & rURL, sal_Bool bReadOnly, sal_Bool) override;
98 virtual sal_Bool SAL_CALL isValid() override;
100 virtual void SAL_CALL close() override;
102 virtual void SAL_CALL destroy() override;
104 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL
105 getRootKey() override;
107 virtual sal_Bool SAL_CALL isReadOnly() override;
109 virtual void SAL_CALL mergeKey(OUString const &, OUString const &) override;
111 virtual void SAL_CALL flush() override;
113 virtual void SAL_CALL addFlushListener(
114 css::uno::Reference< css::util::XFlushListener > const &) override;
116 virtual void SAL_CALL removeFlushListener(
117 css::uno::Reference< css::util::XFlushListener > const &) override;
119 void checkValid();
121 void checkValid_RuntimeException();
123 void doClose();
125 css::uno::Reference< css::lang::XMultiServiceFactory > provider_;
126 osl::Mutex mutex_;
127 css::uno::Reference< css::uno::XInterface > access_;
128 OUString url_;
129 bool readOnly_;
131 friend class RegistryKey;
134 class RegistryKey:
135 public cppu::WeakImplHelper< css::registry::XRegistryKey >
137 public:
138 RegistryKey(Service & service, css::uno::Any const & value):
139 service_(service), value_(value) {}
141 private:
142 RegistryKey(const RegistryKey&) = delete;
143 RegistryKey& operator=(const RegistryKey&) = delete;
145 virtual ~RegistryKey() override {}
147 virtual OUString SAL_CALL getKeyName() override;
149 virtual sal_Bool SAL_CALL isReadOnly() override;
151 virtual sal_Bool SAL_CALL isValid() override;
153 virtual css::registry::RegistryKeyType SAL_CALL getKeyType(
154 OUString const &) override;
156 virtual css::registry::RegistryValueType SAL_CALL getValueType() override;
158 virtual sal_Int32 SAL_CALL getLongValue() override;
160 virtual void SAL_CALL setLongValue(sal_Int32) override;
162 virtual css::uno::Sequence< sal_Int32 > SAL_CALL getLongListValue() override;
164 virtual void SAL_CALL setLongListValue(
165 css::uno::Sequence< sal_Int32 > const &) override;
167 virtual OUString SAL_CALL getAsciiValue() override;
169 virtual void SAL_CALL setAsciiValue(OUString const &) override;
171 virtual css::uno::Sequence< OUString > SAL_CALL getAsciiListValue() override;
173 virtual void SAL_CALL setAsciiListValue(
174 css::uno::Sequence< OUString > const &) override;
176 virtual OUString SAL_CALL getStringValue() override;
178 virtual void SAL_CALL setStringValue(OUString const &) override;
180 virtual css::uno::Sequence< OUString > SAL_CALL getStringListValue() override;
182 virtual void SAL_CALL setStringListValue(
183 css::uno::Sequence< OUString > const &) override;
185 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBinaryValue() override;
187 virtual void SAL_CALL setBinaryValue(css::uno::Sequence< sal_Int8 > const &) override;
189 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL openKey(
190 OUString const & aKeyName) override;
192 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL
193 createKey(OUString const &) override;
195 virtual void SAL_CALL closeKey() override;
197 virtual void SAL_CALL deleteKey(OUString const &) override;
199 virtual
200 css::uno::Sequence< css::uno::Reference< css::registry::XRegistryKey > >
201 SAL_CALL openKeys() override;
203 virtual css::uno::Sequence< OUString > SAL_CALL getKeyNames() override;
205 virtual sal_Bool SAL_CALL createLink(
206 OUString const &, OUString const &) override;
208 virtual void SAL_CALL deleteLink(OUString const &) override;
210 virtual OUString SAL_CALL getLinkTarget(OUString const &) override;
212 virtual OUString SAL_CALL getResolvedName(
213 OUString const & aKeyName) override;
215 Service & service_;
216 css::uno::Any value_;
219 Service::Service(
220 css::uno::Reference< css::uno::XComponentContext > const & context)
221 : readOnly_(false)
223 assert(context.is());
224 try {
225 provider_.set(
226 context->getServiceManager()->createInstanceWithContext(
227 "com.sun.star.configuration.DefaultProvider", context),
228 css::uno::UNO_QUERY_THROW);
229 } catch (css::uno::RuntimeException &) {
230 throw;
231 } catch (css::uno::Exception & e) {
232 throw css::uno::DeploymentException(
233 ("component context fails to supply service"
234 " com.sun.star.configuration.DefaultProvider of type"
235 " com.sun.star.lang.XMultiServiceFactory: " + e.Message),
236 context);
240 OUString Service::getURL() {
241 osl::MutexGuard g(mutex_);
242 checkValid_RuntimeException();
243 return url_;
246 void Service::open(OUString const & rURL, sal_Bool bReadOnly, sal_Bool)
248 //TODO: bCreate
249 osl::MutexGuard g(mutex_);
250 if (access_.is()) {
251 doClose();
253 css::uno::Sequence< css::uno::Any > args(1);
254 args[0] <<= css::beans::NamedValue("nodepath", css::uno::Any(rURL));
255 try {
256 access_ = provider_->createInstanceWithArguments(
257 (bReadOnly
258 ? OUString("com.sun.star.configuration.ConfigurationAccess")
259 : OUString(
260 "com.sun.star.configuration.ConfigurationUpdateAccess")),
261 args);
262 } catch (css::uno::RuntimeException &) {
263 throw;
264 } catch (css::uno::Exception & e) {
265 throw css::uno::RuntimeException(
266 ("com.sun.star.configuration.ConfigurationRegistry: open failed: " +
267 e.Message),
268 static_cast< cppu::OWeakObject * >(this));
270 url_ = rURL;
271 readOnly_ = bReadOnly;
274 sal_Bool Service::isValid() {
275 osl::MutexGuard g(mutex_);
276 return access_.is();
279 void Service::close()
281 osl::MutexGuard g(mutex_);
282 checkValid();
283 doClose();
286 void Service::destroy()
288 throw css::uno::RuntimeException(
289 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
290 static_cast< cppu::OWeakObject * >(this));
293 css::uno::Reference< css::registry::XRegistryKey > Service::getRootKey()
295 osl::MutexGuard g(mutex_);
296 checkValid();
297 return new RegistryKey(*this, css::uno::Any(access_));
300 sal_Bool Service::isReadOnly() {
301 osl::MutexGuard g(mutex_);
302 checkValid_RuntimeException();
303 return readOnly_;
306 void Service::mergeKey(OUString const &, OUString const &)
308 throw css::uno::RuntimeException(
309 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
310 static_cast< cppu::OWeakObject * >(this));
313 void Service::flush()
315 throw css::uno::RuntimeException(
316 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
317 static_cast< cppu::OWeakObject * >(this));
320 void Service::addFlushListener(
321 css::uno::Reference< css::util::XFlushListener > const &)
323 throw css::uno::RuntimeException(
324 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
325 static_cast< cppu::OWeakObject * >(this));
328 void Service::removeFlushListener(
329 css::uno::Reference< css::util::XFlushListener > const &)
331 throw css::uno::RuntimeException(
332 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
333 static_cast< cppu::OWeakObject * >(this));
336 void Service::checkValid() {
337 if (!access_.is()) {
338 throw css::registry::InvalidRegistryException(
339 "com.sun.star.configuration.ConfigurationRegistry: not valid",
340 static_cast< cppu::OWeakObject * >(this));
344 void Service::checkValid_RuntimeException() {
345 if (!access_.is()) {
346 throw css::uno::RuntimeException(
347 "com.sun.star.configuration.ConfigurationRegistry: not valid",
348 static_cast< cppu::OWeakObject * >(this));
352 void Service::doClose() {
353 access_.clear();
356 OUString RegistryKey::getKeyName() {
357 osl::MutexGuard g(service_.mutex_);
358 service_.checkValid_RuntimeException();
359 css::uno::Reference< css::container::XNamed > named;
360 if (value_ >>= named) {
361 return named->getName();
363 throw css::uno::RuntimeException(
364 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
365 static_cast< cppu::OWeakObject * >(this));
368 sal_Bool RegistryKey::isReadOnly()
370 osl::MutexGuard g(service_.mutex_);
371 service_.checkValid_RuntimeException();
372 return service_.readOnly_; //TODO: read-only sub-nodes in update access?
375 sal_Bool RegistryKey::isValid() {
376 return service_.isValid();
379 css::registry::RegistryKeyType RegistryKey::getKeyType(OUString const &)
381 osl::MutexGuard g(service_.mutex_);
382 service_.checkValid();
383 return css::registry::RegistryKeyType_KEY;
386 css::registry::RegistryValueType RegistryKey::getValueType()
388 osl::MutexGuard g(service_.mutex_);
389 service_.checkValid();
390 css::uno::Type t(value_.getValueType());
391 switch (t.getTypeClass()) {
392 case css::uno::TypeClass_LONG:
393 return css::registry::RegistryValueType_LONG;
394 case css::uno::TypeClass_STRING:
395 return css::registry::RegistryValueType_STRING;
396 case css::uno::TypeClass_SEQUENCE:
397 if (t == cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()) {
398 return css::registry::RegistryValueType_BINARY;
399 } else if (t == cppu::UnoType< css::uno::Sequence< sal_Int32 > >::get())
401 return css::registry::RegistryValueType_LONGLIST;
402 } else if (t ==
403 cppu::UnoType< css::uno::Sequence< OUString > >::get())
405 return css::registry::RegistryValueType_STRINGLIST;
407 SAL_FALLTHROUGH;
408 default:
409 return css::registry::RegistryValueType_NOT_DEFINED;
413 sal_Int32 RegistryKey::getLongValue()
415 osl::MutexGuard g(service_.mutex_);
416 service_.checkValid();
417 sal_Int32 v = 0;
418 if (value_ >>= v) {
419 return v;
421 throw css::registry::InvalidValueException(
422 "com.sun.star.configuration.ConfigurationRegistry",
423 static_cast< cppu::OWeakObject * >(this));
426 void RegistryKey::setLongValue(sal_Int32)
428 throw css::uno::RuntimeException(
429 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
430 static_cast< cppu::OWeakObject * >(this));
433 css::uno::Sequence< sal_Int32 > RegistryKey::getLongListValue()
435 osl::MutexGuard g(service_.mutex_);
436 service_.checkValid();
437 css::uno::Sequence< sal_Int32 > v;
438 if (value_ >>= v) {
439 return v;
441 throw css::registry::InvalidValueException(
442 "com.sun.star.configuration.ConfigurationRegistry",
443 static_cast< cppu::OWeakObject * >(this));
446 void RegistryKey::setLongListValue(css::uno::Sequence< sal_Int32 > const &)
448 throw css::uno::RuntimeException(
449 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
450 static_cast< cppu::OWeakObject * >(this));
453 OUString RegistryKey::getAsciiValue()
455 osl::MutexGuard g(service_.mutex_);
456 service_.checkValid();
457 OUString v;
458 if (value_ >>= v) {
459 return v;
461 throw css::registry::InvalidValueException(
462 "com.sun.star.configuration.ConfigurationRegistry",
463 static_cast< cppu::OWeakObject * >(this));
466 void RegistryKey::setAsciiValue(OUString const &)
468 throw css::uno::RuntimeException(
469 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
470 static_cast< cppu::OWeakObject * >(this));
473 css::uno::Sequence< OUString > RegistryKey::getAsciiListValue()
475 osl::MutexGuard g(service_.mutex_);
476 service_.checkValid();
477 css::uno::Sequence< OUString > v;
478 if (value_ >>= v) {
479 return v;
481 throw css::registry::InvalidValueException(
482 "com.sun.star.configuration.ConfigurationRegistry",
483 static_cast< cppu::OWeakObject * >(this));
486 void RegistryKey::setAsciiListValue(css::uno::Sequence< OUString > const &)
488 throw css::uno::RuntimeException(
489 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
490 static_cast< cppu::OWeakObject * >(this));
493 OUString RegistryKey::getStringValue()
495 osl::MutexGuard g(service_.mutex_);
496 service_.checkValid();
497 OUString v;
498 if (value_ >>= v) {
499 return v;
501 throw css::registry::InvalidValueException(
502 "com.sun.star.configuration.ConfigurationRegistry",
503 static_cast< cppu::OWeakObject * >(this));
506 void RegistryKey::setStringValue(OUString const &)
508 throw css::uno::RuntimeException(
509 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
510 static_cast< cppu::OWeakObject * >(this));
513 css::uno::Sequence< OUString > RegistryKey::getStringListValue()
515 osl::MutexGuard g(service_.mutex_);
516 service_.checkValid();
517 css::uno::Sequence< OUString > v;
518 if (value_ >>= v) {
519 return v;
521 throw css::registry::InvalidValueException(
522 "com.sun.star.configuration.ConfigurationRegistry",
523 static_cast< cppu::OWeakObject * >(this));
526 void RegistryKey::setStringListValue(
527 css::uno::Sequence< OUString > const &)
529 throw css::uno::RuntimeException(
530 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
531 static_cast< cppu::OWeakObject * >(this));
534 css::uno::Sequence< sal_Int8 > RegistryKey::getBinaryValue()
536 osl::MutexGuard g(service_.mutex_);
537 service_.checkValid();
538 css::uno::Sequence< sal_Int8 > v;
539 if (value_ >>= v) {
540 return v;
542 throw css::registry::InvalidValueException(
543 "com.sun.star.configuration.ConfigurationRegistry",
544 static_cast< cppu::OWeakObject * >(this));
547 void RegistryKey::setBinaryValue(css::uno::Sequence< sal_Int8 > const &)
549 throw css::uno::RuntimeException(
550 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
551 static_cast< cppu::OWeakObject * >(this));
554 css::uno::Reference< css::registry::XRegistryKey > RegistryKey::openKey(
555 OUString const & aKeyName)
557 osl::MutexGuard g(service_.mutex_);
558 service_.checkValid_RuntimeException();
559 css::uno::Reference< css::container::XHierarchicalNameAccess > access;
560 if (value_ >>= access) {
561 try {
562 return new RegistryKey(
563 service_, access->getByHierarchicalName(aKeyName));
564 } catch (css::container::NoSuchElementException &) {}
566 return css::uno::Reference< css::registry::XRegistryKey >();
569 css::uno::Reference< css::registry::XRegistryKey > RegistryKey::createKey(
570 OUString const &)
572 throw css::uno::RuntimeException(
573 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
574 static_cast< cppu::OWeakObject * >(this));
577 void RegistryKey::closeKey()
579 osl::MutexGuard g(service_.mutex_);
580 service_.checkValid_RuntimeException();
583 void RegistryKey::deleteKey(OUString const &)
585 throw css::uno::RuntimeException(
586 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
587 static_cast< cppu::OWeakObject * >(this));
590 css::uno::Sequence< css::uno::Reference< css::registry::XRegistryKey > >
591 RegistryKey::openKeys()
593 throw css::uno::RuntimeException(
594 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
595 static_cast< cppu::OWeakObject * >(this));
598 css::uno::Sequence< OUString > RegistryKey::getKeyNames()
600 throw css::uno::RuntimeException(
601 "com.sun.star.configuration.ConfigurationRegistry: not implemented",
602 static_cast< cppu::OWeakObject * >(this));
605 sal_Bool RegistryKey::createLink(OUString const &, OUString const &)
607 osl::MutexGuard g(service_.mutex_);
608 service_.checkValid_RuntimeException();
609 return false;
612 void RegistryKey::deleteLink(OUString const &)
614 osl::MutexGuard g(service_.mutex_);
615 service_.checkValid_RuntimeException();
618 OUString RegistryKey::getLinkTarget(OUString const &)
620 osl::MutexGuard g(service_.mutex_);
621 service_.checkValid_RuntimeException();
622 return OUString();
625 OUString RegistryKey::getResolvedName(OUString const & aKeyName)
627 osl::MutexGuard g(service_.mutex_);
628 service_.checkValid_RuntimeException();
629 return aKeyName;
634 css::uno::Reference< css::uno::XInterface > create(
635 css::uno::Reference< css::uno::XComponentContext > const & context)
637 return static_cast< cppu::OWeakObject * >(new Service(context));
640 OUString getImplementationName() {
641 return OUString("com.sun.star.comp.configuration.ConfigurationRegistry");
644 css::uno::Sequence< OUString > getSupportedServiceNames() {
645 return css::uno::Sequence< OUString > { "com.sun.star.configuration.ConfigurationRegistry" };
650 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */