CppunitTest_sc_tiledrendering2: move to tiledrendering folder
[LibreOffice.git] / configmgr / source / readonlyaccess.cxx
blob8e4f7ab2f9f4f96d62d7c69276267e625b416e33
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/.
8 */
10 #include <sal/config.h>
12 #include <com/sun/star/container/XHierarchicalNameAccess.hpp>
13 #include <com/sun/star/lang/IllegalArgumentException.hpp>
14 #include <com/sun/star/lang/NotInitializedException.hpp>
15 #include <com/sun/star/lang/XInitialization.hpp>
16 #include <com/sun/star/lang/XServiceInfo.hpp>
17 #include <com/sun/star/uno/Any.hxx>
18 #include <com/sun/star/uno/Reference.hxx>
19 #include <com/sun/star/uno/RuntimeException.hpp>
20 #include <com/sun/star/uno/Sequence.hxx>
21 #include <com/sun/star/uno/XInterface.hpp>
22 #include <cppuhelper/implbase.hxx>
23 #include <cppuhelper/supportsservice.hxx>
24 #include <cppuhelper/weak.hxx>
25 #include <rtl/ref.hxx>
26 #include <rtl/ustring.hxx>
27 #include <utility>
28 #include <sal/types.h>
30 #include "components.hxx"
31 #include "lock.hxx"
32 #include "rootaccess.hxx"
33 #include <mutex>
35 namespace configmgr::read_only_access {
37 namespace {
39 class Service:
40 public cppu::WeakImplHelper<
41 css::lang::XServiceInfo, css::lang::XInitialization,
42 css::container::XHierarchicalNameAccess >
44 public:
45 explicit Service(
46 css::uno::Reference< css::uno::XComponentContext > context):
47 context_(std::move(context)) {}
49 private:
50 Service(const Service&) = delete;
51 Service& operator=(const Service&) = delete;
53 virtual ~Service() override {}
55 virtual OUString SAL_CALL getImplementationName() override
56 { return u"com.sun.star.comp.configuration.ReadOnlyAccess"_ustr; }
58 virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
59 { return cppu::supportsService(this, ServiceName); }
61 virtual css::uno::Sequence< OUString > SAL_CALL
62 getSupportedServiceNames() override
63 { return { u"com.sun.star.configuration.ReadOnlyAccess"_ustr }; }
65 virtual void SAL_CALL initialize(
66 css::uno::Sequence< css::uno::Any > const & aArguments) override;
68 virtual css::uno::Any SAL_CALL getByHierarchicalName(
69 OUString const & aName) override
70 { return getRoot()->getByHierarchicalName(aName); }
72 virtual sal_Bool SAL_CALL hasByHierarchicalName(OUString const & aName) override
73 { return getRoot()->hasByHierarchicalName(aName); }
75 rtl::Reference< RootAccess > getRoot();
77 css::uno::Reference< css::uno::XComponentContext > context_;
79 std::mutex mutex_;
80 rtl::Reference< RootAccess > root_;
83 void Service::initialize(css::uno::Sequence< css::uno::Any > const & aArguments)
85 OUString locale;
86 if (aArguments.getLength() != 1 || !(aArguments[0] >>= locale)) {
87 throw css::lang::IllegalArgumentException(
88 u"not exactly one string argument"_ustr,
89 getXWeak(), -1);
91 std::unique_lock g1(mutex_);
92 if (root_.is()) {
93 throw css::uno::RuntimeException(
94 u"already initialized"_ustr, getXWeak());
96 osl::MutexGuard g2(*lock());
97 Components & components = Components::getSingleton(context_);
98 root_ = new RootAccess(components, u"/"_ustr, locale, false);
99 components.addRootAccess(root_);
102 rtl::Reference< RootAccess > Service::getRoot() {
103 std::unique_lock g(mutex_);
104 if (!root_.is()) {
105 throw css::lang::NotInitializedException(
106 u"not initialized"_ustr, getXWeak());
108 return root_;
114 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
115 com_sun_star_comp_configuration_ReadOnlyAccess_get_implementation(
116 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const&)
118 return cppu::acquire(new configmgr::read_only_access::Service(context));
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */