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 <optional>
16 #include <string_view>
17 
18 #include <com/sun/star/uno/Any.hxx>
19 #include <com/sun/star/uno/Reference.h>
22 #include <sal/types.h>
23 #include <memory>
24 
25 namespace com::sun::star {
26  namespace configuration { class XReadWriteAccess; }
27  namespace container {
28  class XHierarchicalNameAccess;
29  class XHierarchicalNameReplace;
30  class XNameAccess;
31  class XNameContainer;
32  }
33  namespace uno { class XComponentContext; }
34 }
35 
36 namespace comphelper {
37 
38 namespace detail { class ConfigurationWrapper; }
39 
48 public:
49  static std::shared_ptr<ConfigurationChanges> create(
50  css::uno::Reference< css::uno::XComponentContext >
51  const & context = comphelper::getProcessComponentContext());
52 
54 
55  void commit() const;
56 
57 private:
59  ConfigurationChanges& operator=(const ConfigurationChanges&) = delete;
60 
61  SAL_DLLPRIVATE ConfigurationChanges(
62  css::uno::Reference< css::uno::XComponentContext >
63  const & context);
64 
65  SAL_DLLPRIVATE void setPropertyValue(
66  OUString const & path, css::uno::Any const & value)
67  const;
68 
69  SAL_DLLPRIVATE css::uno::Reference<
70  css::container::XHierarchicalNameReplace >
71  getGroup(OUString const & path) const;
72 
73  SAL_DLLPRIVATE
74  css::uno::Reference< css::container::XNameContainer >
75  getSet(OUString const & path) const;
76 
77  css::uno::Reference<
78  css::configuration::XReadWriteAccess > access_;
79 
81 };
82 
83 namespace detail {
84 
87 public:
88  static ConfigurationWrapper const & get(
89  css::uno::Reference< css::uno::XComponentContext >
90  const & context);
91 
92  SAL_DLLPRIVATE explicit ConfigurationWrapper(
93  css::uno::Reference< css::uno::XComponentContext >
94  const & context);
95 
96  SAL_DLLPRIVATE ~ConfigurationWrapper();
97 
98  bool isReadOnly(OUString const & path) const;
99 
100  css::uno::Any getPropertyValue(OUString const & path) const;
101 
102  static void setPropertyValue(
103  std::shared_ptr< ConfigurationChanges > const & batch,
104  OUString const & path, css::uno::Any const & value);
105 
106  css::uno::Any getLocalizedPropertyValue(
107  std::u16string_view path) const;
108 
109  static void setLocalizedPropertyValue(
110  std::shared_ptr< ConfigurationChanges > const & batch,
111  OUString const & path, css::uno::Any const & value);
112 
113  css::uno::Reference<
114  css::container::XHierarchicalNameAccess >
115  getGroupReadOnly(OUString const & path) const;
116 
117  static css::uno::Reference<
118  css::container::XHierarchicalNameReplace >
119  getGroupReadWrite(
120  std::shared_ptr< ConfigurationChanges > const & batch,
121  OUString const & path);
122 
123  css::uno::Reference< css::container::XNameAccess >
124  getSetReadOnly(OUString const & path) const;
125 
126  static css::uno::Reference< css::container::XNameContainer >
127  getSetReadWrite(
128  std::shared_ptr< ConfigurationChanges > const & batch,
129  OUString const & path);
130 
131  std::shared_ptr< ConfigurationChanges > createChanges() const;
132 
133 private:
134  ConfigurationWrapper(const ConfigurationWrapper&) = delete;
135  ConfigurationWrapper& operator=(const ConfigurationWrapper&) = delete;
136 
137  css::uno::Reference< css::uno::XComponentContext > context_;
138 
139  css::uno::Reference< css::configuration::XReadWriteAccess > access_;
140  // should really be a css.configuration.ReadOnlyAccess (with added
141  // css.beans.XHierarchicalPropertySetInfo), but then
142  // configmgr::Access::asProperty() would report all properties as
143  // READONLY, so isReadOnly() would not work
144 };
145 
147 template< typename T > struct Convert {
148  static css::uno::Any toAny(T const & value)
149  { return css::uno::makeAny(value); }
150 
151  static T fromAny(css::uno::Any const & value)
152  { return value.get< T >(); }
153 
154 private:
155  Convert(const Convert&) = delete;
156  Convert& operator=(const Convert&) = delete;
157 
158  Convert() = delete;
159  ~Convert() = delete;
160 };
161 
163 template< typename T > struct Convert< std::optional< T > >
164 {
165  static css::uno::Any toAny(std::optional< T > const & value) {
166  return value
167  ? css::uno::makeAny(*value)
168  : css::uno::Any();
169  }
170 
171  static std::optional< T > fromAny(css::uno::Any const & value)
172  {
173  return value.hasValue()
174  ? std::optional< T >(value.get< T >()) : std::optional< T >();
175  }
176 
177 private:
178  Convert(const Convert&) = delete;
179  Convert& operator=(const Convert&) = delete;
180 
181  Convert() = delete;
182  ~Convert() = delete;
183 };
184 
185 }
186 
192 template< typename T, typename U > struct ConfigurationProperty
193 {
196  static bool isReadOnly(
197  css::uno::Reference<css::uno::XComponentContext> const & context
199  {
200  return detail::ConfigurationWrapper::get(context).isReadOnly(T::path());
201  }
202 
206  static U get(
207  css::uno::Reference< css::uno::XComponentContext >
208  const & context = comphelper::getProcessComponentContext())
209  {
210  // Folding this into one statement causes a bogus error at least with
211  // Red Hat GCC 4.6.2-1:
212  css::uno::Any a(
214  T::path()));
216  }
217 
222  static void set(
223  U const & value,
224  std::shared_ptr< ConfigurationChanges > const & batch)
225  {
227  batch, T::path(), detail::Convert< U >::toAny(value));
228  }
229 
230 private:
233 
234  ConfigurationProperty() = delete;
235  ~ConfigurationProperty() = delete;
236 };
237 
243 template< typename T, typename U > struct ConfigurationLocalizedProperty
244 {
250  static U get(css::uno::Reference< css::uno::XComponentContext > const & context)
251  {
252  // Folding this into one statement causes a bogus error at least with
253  // Red Hat GCC 4.6.2-1:
254  css::uno::Any a(
256  getLocalizedPropertyValue(T::path()));
258  }
259 
266  static void set(
267  U const & value,
268  std::shared_ptr< ConfigurationChanges > const & batch)
269  {
271  batch, T::path(), detail::Convert< U >::toAny(value));
272  }
273 
274 private:
277 
280 };
281 
287 template< typename T > struct ConfigurationGroup {
289  static css::uno::Reference<
290  css::container::XHierarchicalNameAccess >
291  get(css::uno::Reference< css::uno::XComponentContext >
292  const & context = comphelper::getProcessComponentContext())
293  {
295  T::path());
296  }
297 
300  static css::uno::Reference<
301  css::container::XHierarchicalNameReplace >
302  get(std::shared_ptr< ConfigurationChanges > const & batch)
303  {
305  batch, T::path());
306  }
307 
308 private:
309  ConfigurationGroup(const ConfigurationGroup&) = delete;
311 
312  ConfigurationGroup() = delete;
313  ~ConfigurationGroup() = delete;
314 };
315 
321 template< typename T > struct ConfigurationSet {
323  static
324  css::uno::Reference< css::container::XNameAccess >
325  get(css::uno::Reference< css::uno::XComponentContext >
326  const & context = comphelper::getProcessComponentContext())
327  {
329  T::path());
330  }
331 
334  static
335  css::uno::Reference< css::container::XNameContainer >
336  get(std::shared_ptr< ConfigurationChanges > const & batch)
337  {
339  batch, T::path());
340  }
341 
342 private:
343  ConfigurationSet(const ConfigurationSet&) = delete;
344  ConfigurationSet& operator=(const ConfigurationSet&) = delete;
345 
346  ConfigurationSet() = delete;
347  ~ConfigurationSet() = delete;
348 };
349 
350 }
351 
352 #endif
353 
354 /* 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)
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)
exports com.sun.star. container
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
static css::uno::Any toAny(std::optional< T > const &value)
ConfigurationGroup & operator=(const ConfigurationGroup &)=delete
uno_Any a
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)
exports com.sun.star. configuration
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
static std::optional< T > fromAny(css::uno::Any const &value)
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)