LibreOffice Module configmgr (master)  1
readwriteaccess.cxx
Go to the documentation of this file.
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 <com/sun/star/configuration/XReadWriteAccess.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>
24 #include <cppuhelper/weak.hxx>
25 #include <osl/mutex.hxx>
26 #include <rtl/ref.hxx>
27 #include <rtl/ustring.hxx>
28 #include <sal/types.h>
29 
30 #include "components.hxx"
31 #include "lock.hxx"
32 #include "rootaccess.hxx"
33 
35 
36 namespace {
37 
38 class Service:
39  public cppu::WeakImplHelper<
40  css::lang::XServiceInfo, css::lang::XInitialization,
41  css::configuration::XReadWriteAccess >
42 {
43 public:
44  explicit Service(
45  css::uno::Reference< css::uno::XComponentContext > const & context):
46  context_(context) {}
47 
48 private:
49  Service(const Service&) = delete;
50  Service& operator=(const Service&) = delete;
51 
52  virtual ~Service() override {}
53 
54  virtual OUString SAL_CALL getImplementationName() override
55  { return "com.sun.star.comp.configuration.ReadWriteAccess"; }
56 
57  virtual sal_Bool SAL_CALL supportsService(OUString const & ServiceName) override
58  { return cppu::supportsService(this, ServiceName); }
59 
60  virtual css::uno::Sequence< OUString > SAL_CALL
61  getSupportedServiceNames() override
62  { return { "com.sun.star.configuration.ReadWriteAccess" }; }
63 
64  virtual void SAL_CALL initialize(
65  css::uno::Sequence< css::uno::Any > const & aArguments) override;
66 
67  virtual css::uno::Any SAL_CALL getByHierarchicalName(
68  OUString const & aName) override
69  { return getRoot()->getByHierarchicalName(aName); }
70 
71  virtual sal_Bool SAL_CALL hasByHierarchicalName(OUString const & aName) override
72  { return getRoot()->hasByHierarchicalName(aName); }
73 
74  virtual void SAL_CALL replaceByHierarchicalName(
75  OUString const & aName, css::uno::Any const & aElement) override
76  { getRoot()->replaceByHierarchicalName(aName, aElement); }
77 
78  virtual void SAL_CALL commitChanges() override
79  { getRoot()->commitChanges(); }
80 
81  virtual sal_Bool SAL_CALL hasPendingChanges() override
82  { return getRoot()->hasPendingChanges(); }
83 
84  virtual css::uno::Sequence< ::css::util::ElementChange > SAL_CALL getPendingChanges() override
85  { return getRoot()->getPendingChanges(); }
86 
87  css::beans::Property SAL_CALL getPropertyByHierarchicalName(
88  OUString const & aHierarchicalName)
89  override
90  { return getRoot()->getPropertyByHierarchicalName(aHierarchicalName); }
91 
92  sal_Bool SAL_CALL hasPropertyByHierarchicalName(
93  OUString const & aHierarchicalName) override
94  { return getRoot()->hasPropertyByHierarchicalName(aHierarchicalName); }
95 
97 
98  css::uno::Reference< css::uno::XComponentContext > context_;
99 
100  osl::Mutex mutex_;
102 };
103 
104 void Service::initialize(css::uno::Sequence< css::uno::Any > const & aArguments)
105 {
106  OUString locale;
107  if (aArguments.getLength() != 1 || !(aArguments[0] >>= locale)) {
108  throw css::lang::IllegalArgumentException(
109  "not exactly one string argument",
110  static_cast< cppu::OWeakObject * >(this), -1);
111  }
112  osl::MutexGuard g1(mutex_);
113  if (root_.is()) {
114  throw css::uno::RuntimeException(
115  "already initialized", static_cast< cppu::OWeakObject * >(this));
116  }
117  osl::MutexGuard g2(*lock());
119  root_ = new RootAccess(components, "/", locale, true);
120  components.addRootAccess(root_);
121 }
122 
123 rtl::Reference< RootAccess > Service::getRoot() {
124  osl::MutexGuard g(mutex_);
125  if (!root_.is()) {
126  throw css::lang::NotInitializedException(
127  "not initialized", static_cast< cppu::OWeakObject * >(this));
128  }
129  return root_;
130 }
131 
132 }
133 }
134 
135 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
137  css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
138 {
139  return cppu::acquire(new configmgr::read_write_access::Service(context));
140 }
141 
142 
143 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
rtl::Reference< RootAccess > root_
osl::Mutex mutex_
css::uno::Sequence< OUString > getSupportedServiceNames()
Sequence< PropertyValue > aArguments
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Reference< css::uno::XComponentContext > context_
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_configuration_ReadWriteAccess_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool
std::vector< uno::Reference< sheet::XSpreadsheetDocument > > Components
OUString aName
std::shared_ptr< osl::Mutex > const & lock()
Definition: lock.cxx:28
static Components & getSingleton(css::uno::Reference< css::uno::XComponentContext > const &context)
Definition: components.cxx:197