LibreOffice Module comphelper (master)  1
configuration.hxx
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 #ifndef INCLUDED_COMPHELPER_CONFIGURATION_HXX
11 #define INCLUDED_COMPHELPER_CONFIGURATION_HXX
12 
13 #include <sal/config.h>
14 
15 #include <o3tl/optional.hxx>
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Reference.h>
20 #include <sal/types.h>
21 #include <memory>
22 
23 namespace com { namespace sun { namespace star {
24  namespace configuration { class XReadWriteAccess; }
25  namespace container {
26  class XHierarchicalNameAccess;
27  class XHierarchicalNameReplace;
28  class XNameAccess;
29  class XNameContainer;
30  }
31  namespace uno { class XComponentContext; }
32 } } }
33 
34 namespace comphelper {
35 
36 namespace detail { class ConfigurationWrapper; }
37 
46 public:
47  static std::shared_ptr<ConfigurationChanges> create(
48  css::uno::Reference< css::uno::XComponentContext >
49  const & context = comphelper::getProcessComponentContext());
50 
52 
53  void commit() const;
54 
55 private:
57  ConfigurationChanges& operator=(const ConfigurationChanges&) = delete;
58 
59  SAL_DLLPRIVATE ConfigurationChanges(
60  css::uno::Reference< css::uno::XComponentContext >
61  const & context);
62 
63  SAL_DLLPRIVATE void setPropertyValue(
64  OUString const & path, css::uno::Any const & value)
65  const;
66 
67  SAL_DLLPRIVATE css::uno::Reference<
68  css::container::XHierarchicalNameReplace >
69  getGroup(OUString const & path) const;
70 
71  SAL_DLLPRIVATE
72  css::uno::Reference< css::container::XNameContainer >
73  getSet(OUString const & path) const;
74 
75  css::uno::Reference<
76  css::configuration::XReadWriteAccess > access_;
77 
79 };
80 
81 namespace detail {
82 
85 public:
86  static ConfigurationWrapper const & get(
87  css::uno::Reference< css::uno::XComponentContext >
88  const & context);
89 
90  SAL_DLLPRIVATE explicit ConfigurationWrapper(
91  css::uno::Reference< css::uno::XComponentContext >
92  const & context);
93 
94  SAL_DLLPRIVATE ~ConfigurationWrapper();
95 
96  bool isReadOnly(OUString const & path) const;
97 
98  css::uno::Any getPropertyValue(OUString const & path) const;
99 
100  static void setPropertyValue(
101  std::shared_ptr< ConfigurationChanges > const & batch,
102  OUString const & path, css::uno::Any const & value);
103 
104  css::uno::Any getLocalizedPropertyValue(
105  OUString const & path) const;
106 
107  static void setLocalizedPropertyValue(
108  std::shared_ptr< ConfigurationChanges > const & batch,
109  OUString const & path, css::uno::Any const & value);
110 
111  css::uno::Reference<
112  css::container::XHierarchicalNameAccess >
113  getGroupReadOnly(OUString const & path) const;
114 
115  static css::uno::Reference<
116  css::container::XHierarchicalNameReplace >
117  getGroupReadWrite(
118  std::shared_ptr< ConfigurationChanges > const & batch,
119  OUString const & path);
120 
121  css::uno::Reference< css::container::XNameAccess >
122  getSetReadOnly(OUString const & path) const;
123 
124  static css::uno::Reference< css::container::XNameContainer >
125  getSetReadWrite(
126  std::shared_ptr< ConfigurationChanges > const & batch,
127  OUString const & path);
128 
129  std::shared_ptr< ConfigurationChanges > createChanges() const;
130 
131 private:
132  ConfigurationWrapper(const ConfigurationWrapper&) = delete;
133  ConfigurationWrapper& operator=(const ConfigurationWrapper&) = delete;
134 
135  css::uno::Reference< css::uno::XComponentContext > context_;
136 
137  css::uno::Reference< css::configuration::XReadWriteAccess > access_;
138  // should really be a css.configuration.ReadOnlyAccess (with added
139  // css.beans.XHierarchicalPropertySetInfo), but then
140  // configmgr::Access::asProperty() would report all properties as
141  // READONLY, so isReadOnly() would not work
142 };
143 
145 template< typename T > struct Convert {
146  static css::uno::Any toAny(T const & value)
147  { return css::uno::makeAny(value); }
148 
149  static T fromAny(css::uno::Any const & value)
150  { return value.get< T >(); }
151 
152 private:
153  Convert(const Convert&) = delete;
154  Convert& operator=(const Convert&) = delete;
155 
156  Convert() = delete;
157  ~Convert() = delete;
158 };
159 
161 template< typename T > struct Convert< o3tl::optional< T > >
162 {
163  static css::uno::Any toAny(o3tl::optional< T > const & value) {
164  return value
165  ? css::uno::makeAny(*value)
166  : css::uno::Any();
167  }
168 
169  static o3tl::optional< T > fromAny(css::uno::Any const & value)
170  {
171  return value.hasValue()
172  ? o3tl::optional< T >(value.get< T >()) : o3tl::optional< T >();
173  }
174 
175 private:
176  Convert(const Convert&) = delete;
177  Convert& operator=(const Convert&) = delete;
178 
179  Convert() = delete;
180  ~Convert() = delete;
181 };
182 
183 }
184 
190 template< typename T, typename U > struct ConfigurationProperty
191 {
194  static bool isReadOnly(
195  css::uno::Reference<css::uno::XComponentContext> const & context
197  {
198  return detail::ConfigurationWrapper::get(context).isReadOnly(T::path());
199  }
200 
204  static U get(
205  css::uno::Reference< css::uno::XComponentContext >
206  const & context = comphelper::getProcessComponentContext())
207  {
208  // Folding this into one statement causes a bogus error at least with
209  // Red Hat GCC 4.6.2-1:
210  css::uno::Any a(
212  T::path()));
214  }
215 
220  static void set(
221  U const & value,
222  std::shared_ptr< ConfigurationChanges > const & batch)
223  {
225  batch, T::path(), detail::Convert< U >::toAny(value));
226  }
227 
228 private:
231 
232  ConfigurationProperty() = delete;
233  ~ConfigurationProperty() = delete;
234 };
235 
241 template< typename T, typename U > struct ConfigurationLocalizedProperty
242 {
248  static U get(css::uno::Reference< css::uno::XComponentContext > const & context)
249  {
250  // Folding this into one statement causes a bogus error at least with
251  // Red Hat GCC 4.6.2-1:
252  css::uno::Any a(
254  getLocalizedPropertyValue(T::path()));
256  }
257 
264  static void set(
265  U const & value,
266  std::shared_ptr< ConfigurationChanges > const & batch)
267  {
269  batch, T::path(), detail::Convert< U >::toAny(value));
270  }
271 
272 private:
275 
278 };
279 
285 template< typename T > struct ConfigurationGroup {
287  static css::uno::Reference<
288  css::container::XHierarchicalNameAccess >
289  get(css::uno::Reference< css::uno::XComponentContext >
290  const & context = comphelper::getProcessComponentContext())
291  {
293  T::path());
294  }
295 
298  static css::uno::Reference<
299  css::container::XHierarchicalNameReplace >
300  get(std::shared_ptr< ConfigurationChanges > const & batch)
301  {
303  batch, T::path());
304  }
305 
306 private:
307  ConfigurationGroup(const ConfigurationGroup&) = delete;
309 
310  ConfigurationGroup() = delete;
311  ~ConfigurationGroup() = delete;
312 };
313 
319 template< typename T > struct ConfigurationSet {
321  static
322  css::uno::Reference< css::container::XNameAccess >
323  get(css::uno::Reference< css::uno::XComponentContext >
324  const & context = comphelper::getProcessComponentContext())
325  {
327  T::path());
328  }
329 
332  static
333  css::uno::Reference< css::container::XNameContainer >
334  get(std::shared_ptr< ConfigurationChanges > const & batch)
335  {
337  batch, T::path());
338  }
339 
340 private:
341  ConfigurationSet(const ConfigurationSet&) = delete;
342  ConfigurationSet& operator=(const ConfigurationSet&) = delete;
343 
344  ConfigurationSet() = delete;
345  ~ConfigurationSet() = delete;
346 };
347 
348 }
349 
350 #endif
351 
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
A type-safe wrapper around a localized configuration property.
static void set(U const &value, std::shared_ptr< ConfigurationChanges > const &batch)
Set the value of the given localized configuration property, for the locale currently set at the com...
ConfigurationLocalizedProperty & operator=(const ConfigurationLocalizedProperty &)=delete
static void setPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
A type-safe wrapper around a configuration set.
static css::uno::Reference< css::container::XHierarchicalNameReplace > getGroupReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
static css::uno::Any toAny(o3tl::optional< T > const &value)
Convert & operator=(const Convert &)=delete
ConfigurationProperty & operator=(const ConfigurationProperty &)=delete
A type-safe wrapper around a configuration group.
static T fromAny(css::uno::Any const &value)
css::uno::Reference< css::configuration::XReadWriteAccess > access_
bool getPropertyValue(ValueType &rValue, css::uno::Reference< css::beans::XPropertySet > const &xPropSet, OUString const &propName)
static css::uno::Any toAny(T const &value)
css::uno::Reference< css::configuration::XReadWriteAccess > access_
Reference< deployment::XPackageRegistry > create(Reference< deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, Reference< XComponentContext > const &xComponentContext)
A type-safe wrapper around a (non-localized) configuration property.
css::uno::Reference< css::container::XNameAccess > getSetReadOnly(OUString const &path) const
ConfigurationGroup & operator=(const ConfigurationGroup &)=delete
uno_Any a
static o3tl::optional< T > fromAny(css::uno::Any const &value)
bool setPropertyValue(uno::Sequence< beans::PropertyValue > &aProp, const OUString &aName, const uno::Any &aValue)
static void setLocalizedPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
A batch of configuration changes that is committed as a whole.
css::uno::Reference< css::uno::XComponentContext > context_
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
Any value
bool isReadOnly(OUString const &path) const
ConfigurationSet & operator=(const ConfigurationSet &)=delete
static bool isReadOnly(css::uno::Reference< css::uno::XComponentContext > const &context=comphelper::getProcessComponentContext())
Get the read-only status of the given (non-localized) configuration property.
#define COMPHELPER_DLLPUBLIC
static css::uno::Reference< css::container::XNameContainer > getSetReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
css::uno::Reference< css::container::XHierarchicalNameAccess > getGroupReadOnly(OUString const &path) const
static void set(U const &value, std::shared_ptr< ConfigurationChanges > const &batch)
Set the value of the given (non-localized) configuration property, via a given changes batch...
static ConfigurationWrapper const & get(css::uno::Reference< css::uno::XComponentContext > const &context)