CppunitTest_sc_tiledrendering2: move to tiledrendering folder
[LibreOffice.git] / configmgr / source / configurationregistry.cxx
blobc5dcf0cf58ed48c505f653c736f64d7e7f31d567
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 <mutex>
54 #include <rtl/ustring.hxx>
55 #include <utility>
56 #include <sal/types.h>
58 namespace com::sun::star::util {
59 class XFlushListener;
62 namespace configmgr::configuration_registry {
64 namespace {
66 class Service:
67 public cppu::WeakImplHelper<
68 css::lang::XServiceInfo, css::registry::XSimpleRegistry,
69 css::util::XFlushable >
71 public:
72 explicit Service(css::uno::Reference< css::uno::XComponentContext > const & context);
74 private:
75 Service(const Service&) = delete;
76 Service& operator=(const Service&) = delete;
78 virtual ~Service() override {}
80 virtual OUString SAL_CALL getImplementationName() override
81 { return u"com.sun.star.comp.configuration.ConfigurationRegistry"_ustr; }
83 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
84 { return cppu::supportsService(this, ServiceName); }
86 virtual css::uno::Sequence< OUString > SAL_CALL
87 getSupportedServiceNames() override
88 { return { u"com.sun.star.configuration.ConfigurationRegistry"_ustr }; }
90 virtual OUString SAL_CALL getURL() override;
92 virtual void SAL_CALL open(
93 OUString const & rURL, sal_Bool bReadOnly, sal_Bool) override;
95 virtual sal_Bool SAL_CALL isValid() override;
97 virtual void SAL_CALL close() override;
99 virtual void SAL_CALL destroy() override;
101 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL
102 getRootKey() override;
104 virtual sal_Bool SAL_CALL isReadOnly() override;
106 virtual void SAL_CALL mergeKey(OUString const &, OUString const &) override;
108 virtual void SAL_CALL flush() override;
110 virtual void SAL_CALL addFlushListener(
111 css::uno::Reference< css::util::XFlushListener > const &) override;
113 virtual void SAL_CALL removeFlushListener(
114 css::uno::Reference< css::util::XFlushListener > const &) override;
116 void checkValid();
118 void checkValid_RuntimeException();
120 void doClose();
122 css::uno::Reference< css::lang::XMultiServiceFactory > provider_;
123 std::mutex mutex_;
124 css::uno::Reference< css::uno::XInterface > access_;
125 OUString url_;
126 bool readOnly_;
128 friend class RegistryKey;
131 class RegistryKey:
132 public cppu::WeakImplHelper< css::registry::XRegistryKey >
134 public:
135 RegistryKey(Service & service, css::uno::Any value):
136 service_(service), value_(std::move(value)) {}
138 private:
139 RegistryKey(const RegistryKey&) = delete;
140 RegistryKey& operator=(const RegistryKey&) = delete;
142 virtual ~RegistryKey() override {}
144 virtual OUString SAL_CALL getKeyName() override;
146 virtual sal_Bool SAL_CALL isReadOnly() override;
148 virtual sal_Bool SAL_CALL isValid() override;
150 virtual css::registry::RegistryKeyType SAL_CALL getKeyType(
151 OUString const &) override;
153 virtual css::registry::RegistryValueType SAL_CALL getValueType() override;
155 virtual sal_Int32 SAL_CALL getLongValue() override;
157 virtual void SAL_CALL setLongValue(sal_Int32) override;
159 virtual css::uno::Sequence< sal_Int32 > SAL_CALL getLongListValue() override;
161 virtual void SAL_CALL setLongListValue(
162 css::uno::Sequence< sal_Int32 > const &) override;
164 virtual OUString SAL_CALL getAsciiValue() override;
166 virtual void SAL_CALL setAsciiValue(OUString const &) override;
168 virtual css::uno::Sequence< OUString > SAL_CALL getAsciiListValue() override;
170 virtual void SAL_CALL setAsciiListValue(
171 css::uno::Sequence< OUString > const &) override;
173 virtual OUString SAL_CALL getStringValue() override;
175 virtual void SAL_CALL setStringValue(OUString const &) override;
177 virtual css::uno::Sequence< OUString > SAL_CALL getStringListValue() override;
179 virtual void SAL_CALL setStringListValue(
180 css::uno::Sequence< OUString > const &) override;
182 virtual css::uno::Sequence< sal_Int8 > SAL_CALL getBinaryValue() override;
184 virtual void SAL_CALL setBinaryValue(css::uno::Sequence< sal_Int8 > const &) override;
186 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL openKey(
187 OUString const & aKeyName) override;
189 virtual css::uno::Reference< css::registry::XRegistryKey > SAL_CALL
190 createKey(OUString const &) override;
192 virtual void SAL_CALL closeKey() override;
194 virtual void SAL_CALL deleteKey(OUString const &) override;
196 virtual
197 css::uno::Sequence< css::uno::Reference< css::registry::XRegistryKey > >
198 SAL_CALL openKeys() override;
200 virtual css::uno::Sequence< OUString > SAL_CALL getKeyNames() override;
202 virtual sal_Bool SAL_CALL createLink(
203 OUString const &, OUString const &) override;
205 virtual void SAL_CALL deleteLink(OUString const &) override;
207 virtual OUString SAL_CALL getLinkTarget(OUString const &) override;
209 virtual OUString SAL_CALL getResolvedName(
210 OUString const & aKeyName) override;
212 Service & service_;
213 css::uno::Any value_;
216 Service::Service(
217 css::uno::Reference< css::uno::XComponentContext > const & context)
218 : readOnly_(false)
220 assert(context.is());
221 try {
222 provider_.set(
223 context->getServiceManager()->createInstanceWithContext(
224 u"com.sun.star.configuration.DefaultProvider"_ustr, context),
225 css::uno::UNO_QUERY_THROW);
226 } catch (css::uno::RuntimeException &) {
227 throw;
228 } catch (css::uno::Exception & e) {
229 throw css::uno::DeploymentException(
230 ("component context fails to supply service"
231 " com.sun.star.configuration.DefaultProvider of type"
232 " com.sun.star.lang.XMultiServiceFactory: " + e.Message),
233 context);
237 OUString Service::getURL() {
238 std::unique_lock g(mutex_);
239 checkValid_RuntimeException();
240 return url_;
243 void Service::open(OUString const & rURL, sal_Bool bReadOnly, sal_Bool)
245 //TODO: bCreate
246 std::unique_lock g(mutex_);
247 if (access_.is()) {
248 doClose();
250 css::uno::Sequence< css::uno::Any > args{ css::uno::Any(
251 css::beans::NamedValue(u"nodepath"_ustr, css::uno::Any(rURL))) };
252 try {
253 access_ = provider_->createInstanceWithArguments(
254 (bReadOnly
255 ? u"com.sun.star.configuration.ConfigurationAccess"_ustr
256 : u"com.sun.star.configuration.ConfigurationUpdateAccess"_ustr),
257 args);
258 } catch (css::uno::RuntimeException &) {
259 throw;
260 } catch (css::uno::Exception & e) {
261 css::uno::Any anyEx = cppu::getCaughtException();
262 throw css::lang::WrappedTargetRuntimeException(
263 "com.sun.star.configuration.ConfigurationRegistry: open failed: " +
264 e.Message,
265 getXWeak(), anyEx );
267 url_ = rURL;
268 readOnly_ = bReadOnly;
271 sal_Bool Service::isValid() {
272 std::unique_lock g(mutex_);
273 return access_.is();
276 void Service::close()
278 std::unique_lock g(mutex_);
279 checkValid();
280 doClose();
283 void Service::destroy()
285 throw css::uno::RuntimeException(
286 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
287 getXWeak());
290 css::uno::Reference< css::registry::XRegistryKey > Service::getRootKey()
292 std::unique_lock g(mutex_);
293 checkValid();
294 return new RegistryKey(*this, css::uno::Any(access_));
297 sal_Bool Service::isReadOnly() {
298 std::unique_lock g(mutex_);
299 checkValid_RuntimeException();
300 return readOnly_;
303 void Service::mergeKey(OUString const &, OUString const &)
305 throw css::uno::RuntimeException(
306 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
307 getXWeak());
310 void Service::flush()
312 throw css::uno::RuntimeException(
313 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
314 getXWeak());
317 void Service::addFlushListener(
318 css::uno::Reference< css::util::XFlushListener > const &)
320 throw css::uno::RuntimeException(
321 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
322 getXWeak());
325 void Service::removeFlushListener(
326 css::uno::Reference< css::util::XFlushListener > const &)
328 throw css::uno::RuntimeException(
329 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
330 getXWeak());
333 void Service::checkValid() {
334 if (!access_.is()) {
335 throw css::registry::InvalidRegistryException(
336 u"com.sun.star.configuration.ConfigurationRegistry: not valid"_ustr,
337 getXWeak());
341 void Service::checkValid_RuntimeException() {
342 if (!access_.is()) {
343 throw css::uno::RuntimeException(
344 u"com.sun.star.configuration.ConfigurationRegistry: not valid"_ustr,
345 getXWeak());
349 void Service::doClose() {
350 access_.clear();
353 OUString RegistryKey::getKeyName() {
354 std::unique_lock g(service_.mutex_);
355 service_.checkValid_RuntimeException();
356 css::uno::Reference< css::container::XNamed > named;
357 if (value_ >>= named) {
358 return named->getName();
360 throw css::uno::RuntimeException(
361 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
362 getXWeak());
365 sal_Bool RegistryKey::isReadOnly()
367 std::unique_lock g(service_.mutex_);
368 service_.checkValid_RuntimeException();
369 return service_.readOnly_; //TODO: read-only sub-nodes in update access?
372 sal_Bool RegistryKey::isValid() {
373 return service_.isValid();
376 css::registry::RegistryKeyType RegistryKey::getKeyType(OUString const &)
378 std::unique_lock g(service_.mutex_);
379 service_.checkValid();
380 return css::registry::RegistryKeyType_KEY;
383 css::registry::RegistryValueType RegistryKey::getValueType()
385 std::unique_lock g(service_.mutex_);
386 service_.checkValid();
387 css::uno::Type t(value_.getValueType());
388 switch (t.getTypeClass()) {
389 case css::uno::TypeClass_LONG:
390 return css::registry::RegistryValueType_LONG;
391 case css::uno::TypeClass_STRING:
392 return css::registry::RegistryValueType_STRING;
393 case css::uno::TypeClass_SEQUENCE:
394 if (t == cppu::UnoType< css::uno::Sequence< sal_Int8 > >::get()) {
395 return css::registry::RegistryValueType_BINARY;
396 } else if (t == cppu::UnoType< css::uno::Sequence< sal_Int32 > >::get())
398 return css::registry::RegistryValueType_LONGLIST;
399 } else if (t ==
400 cppu::UnoType< css::uno::Sequence< OUString > >::get())
402 return css::registry::RegistryValueType_STRINGLIST;
404 [[fallthrough]];
405 default:
406 return css::registry::RegistryValueType_NOT_DEFINED;
410 sal_Int32 RegistryKey::getLongValue()
412 std::unique_lock g(service_.mutex_);
413 service_.checkValid();
414 sal_Int32 v = 0;
415 if (value_ >>= v) {
416 return v;
418 throw css::registry::InvalidValueException(
419 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
420 getXWeak());
423 void RegistryKey::setLongValue(sal_Int32)
425 throw css::uno::RuntimeException(
426 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
427 getXWeak());
430 css::uno::Sequence< sal_Int32 > RegistryKey::getLongListValue()
432 std::unique_lock g(service_.mutex_);
433 service_.checkValid();
434 css::uno::Sequence< sal_Int32 > v;
435 if (value_ >>= v) {
436 return v;
438 throw css::registry::InvalidValueException(
439 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
440 getXWeak());
443 void RegistryKey::setLongListValue(css::uno::Sequence< sal_Int32 > const &)
445 throw css::uno::RuntimeException(
446 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
447 getXWeak());
450 OUString RegistryKey::getAsciiValue()
452 std::unique_lock g(service_.mutex_);
453 service_.checkValid();
454 OUString v;
455 if (value_ >>= v) {
456 return v;
458 throw css::registry::InvalidValueException(
459 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
460 getXWeak());
463 void RegistryKey::setAsciiValue(OUString const &)
465 throw css::uno::RuntimeException(
466 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
467 getXWeak());
470 css::uno::Sequence< OUString > RegistryKey::getAsciiListValue()
472 std::unique_lock g(service_.mutex_);
473 service_.checkValid();
474 css::uno::Sequence< OUString > v;
475 if (value_ >>= v) {
476 return v;
478 throw css::registry::InvalidValueException(
479 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
480 getXWeak());
483 void RegistryKey::setAsciiListValue(css::uno::Sequence< OUString > const &)
485 throw css::uno::RuntimeException(
486 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
487 getXWeak());
490 OUString RegistryKey::getStringValue()
492 std::unique_lock g(service_.mutex_);
493 service_.checkValid();
494 OUString v;
495 if (value_ >>= v) {
496 return v;
498 throw css::registry::InvalidValueException(
499 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
500 getXWeak());
503 void RegistryKey::setStringValue(OUString const &)
505 throw css::uno::RuntimeException(
506 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
507 getXWeak());
510 css::uno::Sequence< OUString > RegistryKey::getStringListValue()
512 std::unique_lock g(service_.mutex_);
513 service_.checkValid();
514 css::uno::Sequence< OUString > v;
515 if (value_ >>= v) {
516 return v;
518 throw css::registry::InvalidValueException(
519 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
520 getXWeak());
523 void RegistryKey::setStringListValue(
524 css::uno::Sequence< OUString > const &)
526 throw css::uno::RuntimeException(
527 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
528 getXWeak());
531 css::uno::Sequence< sal_Int8 > RegistryKey::getBinaryValue()
533 std::unique_lock g(service_.mutex_);
534 service_.checkValid();
535 css::uno::Sequence< sal_Int8 > v;
536 if (value_ >>= v) {
537 return v;
539 throw css::registry::InvalidValueException(
540 u"com.sun.star.configuration.ConfigurationRegistry"_ustr,
541 getXWeak());
544 void RegistryKey::setBinaryValue(css::uno::Sequence< sal_Int8 > const &)
546 throw css::uno::RuntimeException(
547 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
548 getXWeak());
551 css::uno::Reference< css::registry::XRegistryKey > RegistryKey::openKey(
552 OUString const & aKeyName)
554 std::unique_lock g(service_.mutex_);
555 service_.checkValid_RuntimeException();
556 css::uno::Reference< css::container::XHierarchicalNameAccess > access;
557 if (value_ >>= access) {
558 try {
559 return new RegistryKey(
560 service_, access->getByHierarchicalName(aKeyName));
561 } catch (css::container::NoSuchElementException &) {}
563 return css::uno::Reference< css::registry::XRegistryKey >();
566 css::uno::Reference< css::registry::XRegistryKey > RegistryKey::createKey(
567 OUString const &)
569 throw css::uno::RuntimeException(
570 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
571 getXWeak());
574 void RegistryKey::closeKey()
576 std::unique_lock g(service_.mutex_);
577 service_.checkValid_RuntimeException();
580 void RegistryKey::deleteKey(OUString const &)
582 throw css::uno::RuntimeException(
583 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
584 getXWeak());
587 css::uno::Sequence< css::uno::Reference< css::registry::XRegistryKey > >
588 RegistryKey::openKeys()
590 throw css::uno::RuntimeException(
591 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
592 getXWeak());
595 css::uno::Sequence< OUString > RegistryKey::getKeyNames()
597 throw css::uno::RuntimeException(
598 u"com.sun.star.configuration.ConfigurationRegistry: not implemented"_ustr,
599 getXWeak());
602 sal_Bool RegistryKey::createLink(OUString const &, OUString const &)
604 std::unique_lock g(service_.mutex_);
605 service_.checkValid_RuntimeException();
606 return false;
609 void RegistryKey::deleteLink(OUString const &)
611 std::unique_lock g(service_.mutex_);
612 service_.checkValid_RuntimeException();
615 OUString RegistryKey::getLinkTarget(OUString const &)
617 std::unique_lock g(service_.mutex_);
618 service_.checkValid_RuntimeException();
619 return OUString();
622 OUString RegistryKey::getResolvedName(OUString const & aKeyName)
624 std::unique_lock g(service_.mutex_);
625 service_.checkValid_RuntimeException();
626 return aKeyName;
631 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
632 com_sun_star_comp_configuration_ConfigurationRegistry_get_implementation(
633 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
635 return cppu::acquire(new Service(context));
640 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */