Version 4.0.0.1, tag libreoffice-4.0.0.1
[LibreOffice.git] / configmgr / source / readwriteaccess.cxx
blobb302ad8f4dedb3ef8a2cdac63edeee8de9025b29
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * Version: MPL 1.1 / GPLv3+ / LGPLv3+
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License or as specified alternatively below. You may obtain a copy of
8 * the License at http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * Major Contributor(s):
16 * Copyright (C) 2011 Red Hat, Inc., Stephan Bergmann <sbergman@redhat.com>
17 * (initial developer)
19 * All Rights Reserved.
21 * For minor contributions see the git repository.
23 * Alternatively, the contents of this file may be used under the terms of
24 * either the GNU General Public License Version 3 or later (the "GPLv3+"), or
25 * the GNU Lesser General Public License Version 3 or later (the "LGPLv3+"),
26 * in which case the provisions of the GPLv3+ or the LGPLv3+ are applicable
27 * instead of those above.
30 #include "sal/config.h"
32 #include "boost/noncopyable.hpp"
33 #include "com/sun/star/configuration/XReadWriteAccess.hpp"
34 #include "com/sun/star/container/NoSuchElementException.hpp"
35 #include "com/sun/star/lang/IllegalArgumentException.hpp"
36 #include "com/sun/star/lang/NotInitializedException.hpp"
37 #include "com/sun/star/lang/WrappedTargetException.hpp"
38 #include "com/sun/star/lang/XInitialization.hpp"
39 #include "com/sun/star/lang/XServiceInfo.hpp"
40 #include "com/sun/star/uno/Any.hxx"
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/XComponentContext.hpp"
46 #include "com/sun/star/uno/XInterface.hpp"
47 #include "com/sun/star/util/ChangesSet.hpp"
48 #include "cppuhelper/implbase3.hxx"
49 #include "cppuhelper/weak.hxx"
50 #include "osl/mutex.hxx"
51 #include "rtl/ref.hxx"
52 #include "rtl/ustring.h"
53 #include "rtl/ustring.hxx"
54 #include "sal/types.h"
56 #include "components.hxx"
57 #include "lock.hxx"
58 #include "readwriteaccess.hxx"
59 #include "rootaccess.hxx"
61 namespace configmgr { namespace read_write_access {
63 namespace {
65 class Service:
66 public cppu::WeakImplHelper3<
67 css::lang::XServiceInfo, css::lang::XInitialization,
68 css::configuration::XReadWriteAccess >,
69 private boost::noncopyable
71 public:
72 explicit Service(
73 css::uno::Reference< css::uno::XComponentContext > const & context):
74 context_(context) {}
76 private:
77 virtual ~Service() {}
79 virtual rtl::OUString SAL_CALL getImplementationName()
80 throw (css::uno::RuntimeException)
81 { return read_write_access::getImplementationName(); }
83 virtual sal_Bool SAL_CALL supportsService(rtl::OUString const &)
84 throw (css::uno::RuntimeException)
85 { return false; }
87 virtual css::uno::Sequence< rtl::OUString > SAL_CALL
88 getSupportedServiceNames() throw (css::uno::RuntimeException)
89 { return read_write_access::getSupportedServiceNames(); }
91 virtual void SAL_CALL initialize(
92 css::uno::Sequence< css::uno::Any > const & aArguments)
93 throw (css::uno::Exception, css::uno::RuntimeException);
95 virtual css::uno::Any SAL_CALL getByHierarchicalName(
96 rtl::OUString const & aName)
97 throw (
98 css::container::NoSuchElementException, css::uno::RuntimeException)
99 { return getRoot()->getByHierarchicalName(aName); }
101 virtual sal_Bool SAL_CALL hasByHierarchicalName(rtl::OUString const & aName)
102 throw (css::uno::RuntimeException)
103 { return getRoot()->hasByHierarchicalName(aName); }
105 virtual void SAL_CALL replaceByHierarchicalName(
106 rtl::OUString const & aName, css::uno::Any const & aElement)
107 throw (
108 css::lang::IllegalArgumentException,
109 css::container::NoSuchElementException,
110 css::lang::WrappedTargetException, css::uno::RuntimeException)
111 { getRoot()->replaceByHierarchicalName(aName, aElement); }
113 virtual void SAL_CALL commitChanges()
114 throw (css::lang::WrappedTargetException, css::uno::RuntimeException)
115 { getRoot()->commitChanges(); }
117 virtual sal_Bool SAL_CALL hasPendingChanges()
118 throw (css::uno::RuntimeException)
119 { return getRoot()->hasPendingChanges(); }
121 virtual css::util::ChangesSet SAL_CALL getPendingChanges()
122 throw (css::uno::RuntimeException)
123 { return getRoot()->getPendingChanges(); }
125 rtl::Reference< RootAccess > getRoot();
127 css::uno::Reference< css::uno::XComponentContext > context_;
129 osl::Mutex mutex_;
130 rtl::Reference< RootAccess > root_;
133 void Service::initialize(css::uno::Sequence< css::uno::Any > const & aArguments)
134 throw (css::uno::Exception, css::uno::RuntimeException)
136 OUString locale;
137 if (aArguments.getLength() != 1 || !(aArguments[0] >>= locale)) {
138 throw css::lang::IllegalArgumentException(
139 "not exactly one string argument",
140 static_cast< cppu::OWeakObject * >(this), -1);
142 osl::MutexGuard g1(mutex_);
143 if (root_.is()) {
144 throw css::uno::RuntimeException(
145 "already initialized", static_cast< cppu::OWeakObject * >(this));
147 osl::MutexGuard g2(*lock());
148 Components & components = Components::getSingleton(context_);
149 root_ = new RootAccess(components, "/", locale, true);
150 components.addRootAccess(root_);
153 rtl::Reference< RootAccess > Service::getRoot() {
154 osl::MutexGuard g(mutex_);
155 if (!root_.is()) {
156 throw css::lang::NotInitializedException(
157 "not initialized", static_cast< cppu::OWeakObject * >(this));
159 return root_;
164 css::uno::Reference< css::uno::XInterface > create(
165 css::uno::Reference< css::uno::XComponentContext > const & context)
167 return static_cast< cppu::OWeakObject * >(new Service(context));
170 rtl::OUString getImplementationName() {
171 return rtl::OUString(
172 RTL_CONSTASCII_USTRINGPARAM(
173 "com.sun.star.comp.configuration.ReadWriteAccess"));
176 css::uno::Sequence< rtl::OUString > getSupportedServiceNames() {
177 rtl::OUString name(
178 RTL_CONSTASCII_USTRINGPARAM(
179 "com.sun.star.configuration.ReadWriteAccess"));
180 return css::uno::Sequence< rtl::OUString >(&name, 1);
185 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */