Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #include "sal/config.h"
11 :
12 : #include "boost/noncopyable.hpp"
13 : #include "com/sun/star/configuration/XReadWriteAccess.hpp"
14 : #include "com/sun/star/container/NoSuchElementException.hpp"
15 : #include "com/sun/star/lang/IllegalArgumentException.hpp"
16 : #include "com/sun/star/lang/NotInitializedException.hpp"
17 : #include "com/sun/star/lang/WrappedTargetException.hpp"
18 : #include "com/sun/star/lang/XInitialization.hpp"
19 : #include "com/sun/star/lang/XServiceInfo.hpp"
20 : #include "com/sun/star/uno/Any.hxx"
21 : #include "com/sun/star/uno/Exception.hpp"
22 : #include "com/sun/star/uno/Reference.hxx"
23 : #include "com/sun/star/uno/RuntimeException.hpp"
24 : #include "com/sun/star/uno/Sequence.hxx"
25 : #include "com/sun/star/uno/XComponentContext.hpp"
26 : #include "com/sun/star/uno/XInterface.hpp"
27 : #include "com/sun/star/util/ChangesSet.hpp"
28 : #include "cppuhelper/implbase3.hxx"
29 : #include "cppuhelper/weak.hxx"
30 : #include "osl/mutex.hxx"
31 : #include "rtl/ref.hxx"
32 : #include "rtl/ustring.h"
33 : #include "rtl/ustring.hxx"
34 : #include "sal/types.h"
35 :
36 : #include "components.hxx"
37 : #include "lock.hxx"
38 : #include "readwriteaccess.hxx"
39 : #include "rootaccess.hxx"
40 :
41 : namespace configmgr { namespace read_write_access {
42 :
43 : namespace {
44 :
45 : class Service:
46 : public cppu::WeakImplHelper3<
47 : css::lang::XServiceInfo, css::lang::XInitialization,
48 : css::configuration::XReadWriteAccess >,
49 : private boost::noncopyable
50 : {
51 : public:
52 1542 : explicit Service(
53 : css::uno::Reference< css::uno::XComponentContext > const & context):
54 1542 : context_(context) {}
55 :
56 : private:
57 3084 : virtual ~Service() {}
58 :
59 0 : virtual OUString SAL_CALL getImplementationName()
60 : throw (css::uno::RuntimeException)
61 0 : { return read_write_access::getImplementationName(); }
62 :
63 0 : virtual sal_Bool SAL_CALL supportsService(OUString const &)
64 : throw (css::uno::RuntimeException)
65 0 : { return false; }
66 :
67 : virtual css::uno::Sequence< OUString > SAL_CALL
68 0 : getSupportedServiceNames() throw (css::uno::RuntimeException)
69 0 : { return read_write_access::getSupportedServiceNames(); }
70 :
71 : virtual void SAL_CALL initialize(
72 : css::uno::Sequence< css::uno::Any > const & aArguments)
73 : throw (css::uno::Exception, css::uno::RuntimeException);
74 :
75 0 : virtual css::uno::Any SAL_CALL getByHierarchicalName(
76 : OUString const & aName)
77 : throw (
78 : css::container::NoSuchElementException, css::uno::RuntimeException)
79 0 : { return getRoot()->getByHierarchicalName(aName); }
80 :
81 0 : virtual sal_Bool SAL_CALL hasByHierarchicalName(OUString const & aName)
82 : throw (css::uno::RuntimeException)
83 0 : { return getRoot()->hasByHierarchicalName(aName); }
84 :
85 64 : virtual void SAL_CALL replaceByHierarchicalName(
86 : OUString const & aName, css::uno::Any const & aElement)
87 : throw (
88 : css::lang::IllegalArgumentException,
89 : css::container::NoSuchElementException,
90 : css::lang::WrappedTargetException, css::uno::RuntimeException)
91 64 : { getRoot()->replaceByHierarchicalName(aName, aElement); }
92 :
93 64 : virtual void SAL_CALL commitChanges()
94 : throw (css::lang::WrappedTargetException, css::uno::RuntimeException)
95 64 : { getRoot()->commitChanges(); }
96 :
97 0 : virtual sal_Bool SAL_CALL hasPendingChanges()
98 : throw (css::uno::RuntimeException)
99 0 : { return getRoot()->hasPendingChanges(); }
100 :
101 0 : virtual css::util::ChangesSet SAL_CALL getPendingChanges()
102 : throw (css::uno::RuntimeException)
103 0 : { return getRoot()->getPendingChanges(); }
104 :
105 : rtl::Reference< RootAccess > getRoot();
106 :
107 : css::uno::Reference< css::uno::XComponentContext > context_;
108 :
109 : osl::Mutex mutex_;
110 : rtl::Reference< RootAccess > root_;
111 : };
112 :
113 1542 : void Service::initialize(css::uno::Sequence< css::uno::Any > const & aArguments)
114 : throw (css::uno::Exception, css::uno::RuntimeException)
115 : {
116 1542 : OUString locale;
117 1542 : if (aArguments.getLength() != 1 || !(aArguments[0] >>= locale)) {
118 : throw css::lang::IllegalArgumentException(
119 : "not exactly one string argument",
120 0 : static_cast< cppu::OWeakObject * >(this), -1);
121 : }
122 3084 : osl::MutexGuard g1(mutex_);
123 1542 : if (root_.is()) {
124 : throw css::uno::RuntimeException(
125 0 : "already initialized", static_cast< cppu::OWeakObject * >(this));
126 : }
127 3084 : osl::MutexGuard g2(*lock());
128 1542 : Components & components = Components::getSingleton(context_);
129 1542 : root_ = new RootAccess(components, "/", locale, true);
130 3084 : components.addRootAccess(root_);
131 1542 : }
132 :
133 128 : rtl::Reference< RootAccess > Service::getRoot() {
134 128 : osl::MutexGuard g(mutex_);
135 128 : if (!root_.is()) {
136 : throw css::lang::NotInitializedException(
137 0 : "not initialized", static_cast< cppu::OWeakObject * >(this));
138 : }
139 128 : return root_;
140 : }
141 :
142 : }
143 :
144 1542 : css::uno::Reference< css::uno::XInterface > create(
145 : css::uno::Reference< css::uno::XComponentContext > const & context)
146 : {
147 1542 : return static_cast< cppu::OWeakObject * >(new Service(context));
148 : }
149 :
150 436 : OUString getImplementationName() {
151 436 : return OUString("com.sun.star.comp.configuration.ReadWriteAccess");
152 : }
153 :
154 109 : css::uno::Sequence< OUString > getSupportedServiceNames() {
155 109 : OUString name("com.sun.star.configuration.ReadWriteAccess");
156 109 : return css::uno::Sequence< OUString >(&name, 1);
157 : }
158 :
159 : } }
160 :
161 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|