Version 5.2.6.1, tag libreoffice-5.2.6.1
[LibreOffice.git] / include / comphelper / configuration.hxx
blob915d803b6f1691eec7502e465d15dd5ddbca3f9b
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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 */
10 #ifndef INCLUDED_COMPHELPER_CONFIGURATION_HXX
11 #define INCLUDED_COMPHELPER_CONFIGURATION_HXX
13 #include <sal/config.h>
15 #include <boost/optional.hpp>
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Reference.hxx>
18 #include <comphelper/comphelperdllapi.h>
19 #include <comphelper/processfactory.hxx>
20 #include <sal/types.h>
21 #include <memory>
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;
31 namespace uno { class XComponentContext; }
32 } } }
34 namespace comphelper {
36 namespace detail { class ConfigurationWrapper; }
38 /// A batch of configuration changes that is committed as a whole.
39 ///
40 /// Client code needs to call commit explicitly; otherwise the changes are lost
41 /// when the instance is destroyed.
42 ///
43 /// This is the only class from this header file that client code should use
44 /// directly.
45 class COMPHELPER_DLLPUBLIC ConfigurationChanges {
46 public:
47 static std::shared_ptr<ConfigurationChanges> create(
48 css::uno::Reference< css::uno::XComponentContext >
49 const & context = comphelper::getProcessComponentContext());
51 ~ConfigurationChanges();
53 void commit() const;
55 private:
56 ConfigurationChanges(const ConfigurationChanges&) = delete;
57 ConfigurationChanges& operator=(const ConfigurationChanges&) = delete;
59 SAL_DLLPRIVATE ConfigurationChanges(
60 css::uno::Reference< css::uno::XComponentContext >
61 const & context);
63 SAL_DLLPRIVATE void setPropertyValue(
64 OUString const & path, css::uno::Any const & value)
65 const;
67 SAL_DLLPRIVATE css::uno::Reference<
68 css::container::XHierarchicalNameReplace >
69 getGroup(OUString const & path) const;
71 SAL_DLLPRIVATE
72 css::uno::Reference< css::container::XNameContainer >
73 getSet(OUString const & path) const;
75 css::uno::Reference<
76 css::configuration::XReadWriteAccess > access_;
78 friend class detail::ConfigurationWrapper;
81 namespace detail {
83 /// @internal
84 class COMPHELPER_DLLPUBLIC ConfigurationWrapper {
85 public:
86 static ConfigurationWrapper const & get(
87 css::uno::Reference< css::uno::XComponentContext >
88 const & context);
90 SAL_DLLPRIVATE explicit ConfigurationWrapper(
91 css::uno::Reference< css::uno::XComponentContext >
92 const & context);
94 SAL_DLLPRIVATE ~ConfigurationWrapper();
96 bool isReadOnly(OUString const & path) const;
98 css::uno::Any getPropertyValue(OUString const & path) const;
100 static void setPropertyValue(
101 std::shared_ptr< ConfigurationChanges > const & batch,
102 OUString const & path, css::uno::Any const & value);
104 css::uno::Any getLocalizedPropertyValue(
105 OUString const & path) const;
107 static void setLocalizedPropertyValue(
108 std::shared_ptr< ConfigurationChanges > const & batch,
109 OUString const & path, css::uno::Any const & value);
111 css::uno::Reference<
112 css::container::XHierarchicalNameAccess >
113 getGroupReadOnly(OUString const & path) const;
115 static css::uno::Reference<
116 css::container::XHierarchicalNameReplace >
117 getGroupReadWrite(
118 std::shared_ptr< ConfigurationChanges > const & batch,
119 OUString const & path);
121 css::uno::Reference< css::container::XNameAccess >
122 getSetReadOnly(OUString const & path) const;
124 static css::uno::Reference< css::container::XNameContainer >
125 getSetReadWrite(
126 std::shared_ptr< ConfigurationChanges > const & batch,
127 OUString const & path);
129 std::shared_ptr< ConfigurationChanges > createChanges() const;
131 private:
132 ConfigurationWrapper(const ConfigurationWrapper&) = delete;
133 ConfigurationWrapper& operator=(const ConfigurationWrapper&) = delete;
135 css::uno::Reference< css::uno::XComponentContext > context_;
137 css::uno::Reference< css::configuration::XReadWriteAccess > access_;
138 // should really be an 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
144 /// @internal
145 template< typename T > struct Convert {
146 static css::uno::Any toAny(T const & value)
147 { return css::uno::makeAny(value); }
149 static T fromAny(css::uno::Any const & value)
150 { return value.get< T >(); }
152 private:
153 Convert(const Convert&) = delete;
154 Convert& operator=(const Convert&) = delete;
156 Convert() = delete;
157 ~Convert() = delete;
160 /// @internal
161 template< typename T > struct Convert< boost::optional< T > >
163 static css::uno::Any toAny(boost::optional< T > const & value) {
164 return value
165 ? css::uno::makeAny(value.get())
166 : css::uno::Any();
169 static boost::optional< T > fromAny(css::uno::Any const & value)
171 return value.hasValue()
172 ? boost::optional< T >(value.get< T >()) : boost::optional< T >();
175 private:
176 Convert(const Convert&) = delete;
177 Convert& operator=(const Convert&) = delete;
179 Convert() = delete;
180 ~Convert() = delete;
185 /// A type-safe wrapper around a (non-localized) configuration property.
187 /// Automatically generated headers for the various configuration properties
188 /// derive from this template and make available its member functions to access
189 /// each given configuration property.
190 template< typename T, typename U > struct ConfigurationProperty
192 /// Get the read-only status of the given (non-localized) configuration
193 /// property.
194 static bool isReadOnly(
195 css::uno::Reference<css::uno::XComponentContext> const & context
196 = comphelper::getProcessComponentContext())
198 return detail::ConfigurationWrapper::get(context).isReadOnly(T::path());
201 /// Get the value of the given (non-localized) configuration property.
203 /// For nillable properties, U is of type boost::optional<U'>.
204 static U get(
205 css::uno::Reference< css::uno::XComponentContext >
206 const & context = comphelper::getProcessComponentContext())
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(
211 detail::ConfigurationWrapper::get(context).getPropertyValue(
212 T::path()));
213 return detail::Convert< U >::fromAny(a);
216 /// Set the value of the given (non-localized) configuration property, via a
217 /// given changes batch.
219 /// For nillable properties, U is of type boost::optional<U'>.
220 static void set(
221 U const & value,
222 std::shared_ptr< ConfigurationChanges > const & batch)
224 comphelper::detail::ConfigurationWrapper::setPropertyValue(
225 batch, T::path(), detail::Convert< U >::toAny(value));
228 private:
229 ConfigurationProperty(const ConfigurationProperty&) = delete;
230 ConfigurationProperty& operator=(const ConfigurationProperty&) = delete;
232 ConfigurationProperty() = delete;
233 ~ConfigurationProperty() = delete;
236 /// A type-safe wrapper around a localized configuration property.
238 /// Automatically generated headers for the various localized configuration
239 /// properties derive from this template and make available its member functions
240 /// to access each given localized configuration property.
241 template< typename T, typename U > struct ConfigurationLocalizedProperty
243 /// Get the value of the given localized configuration property, for the
244 /// locale currently set at the
245 /// com.sun.star.configuration.theDefaultProvider.
247 /// For nillable properties, U is of type boost::optional<U'>.
248 static U get(
249 css::uno::Reference< css::uno::XComponentContext >
250 const & context = comphelper::getProcessComponentContext())
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(
255 detail::ConfigurationWrapper::get(context).
256 getLocalizedPropertyValue(T::path()));
257 return detail::Convert< U >::fromAny(a);
260 /// Set the value of the given localized configuration property, for the
261 /// locale currently set at the
262 /// com.sun.star.configuration.theDefaultProvider, via a given changes
263 /// batch.
265 /// For nillable properties, U is of type boost::optional<U'>.
266 static void set(
267 U const & value,
268 std::shared_ptr< ConfigurationChanges > const & batch)
270 comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
271 batch, T::path(), detail::Convert< U >::toAny(value));
274 private:
275 ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty&) = delete;
276 ConfigurationLocalizedProperty& operator=(const ConfigurationLocalizedProperty&) = delete;
278 ConfigurationLocalizedProperty() = delete;
279 ~ConfigurationLocalizedProperty() = delete;
282 /// A type-safe wrapper around a configuration group.
284 /// Automatically generated headers for the various configuration groups derive
285 /// from this template and make available its member functions to access each
286 /// given configuration group.
287 template< typename T > struct ConfigurationGroup {
288 /// Get read-only access to the given configuration group.
289 static css::uno::Reference<
290 css::container::XHierarchicalNameAccess >
291 get(css::uno::Reference< css::uno::XComponentContext >
292 const & context = comphelper::getProcessComponentContext())
294 return detail::ConfigurationWrapper::get(context).getGroupReadOnly(
295 T::path());
298 /// Get read/write access to the given configuration group, storing any
299 /// modifications via the given changes batch.
300 static css::uno::Reference<
301 css::container::XHierarchicalNameReplace >
302 get(std::shared_ptr< ConfigurationChanges > const & batch)
304 return comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
305 batch, T::path());
308 private:
309 ConfigurationGroup(const ConfigurationGroup&) = delete;
310 ConfigurationGroup& operator=(const ConfigurationGroup&) = delete;
312 ConfigurationGroup() = delete;
313 ~ConfigurationGroup() = delete;
316 /// A type-safe wrapper around a configuration set.
318 /// Automatically generated headers for the various configuration sets derive
319 /// from this template and make available its member functions to access each
320 /// given configuration set.
321 template< typename T > struct ConfigurationSet {
322 /// Get read-only access to the given configuration set.
323 static
324 css::uno::Reference< css::container::XNameAccess >
325 get(css::uno::Reference< css::uno::XComponentContext >
326 const & context = comphelper::getProcessComponentContext())
328 return detail::ConfigurationWrapper::get(context).getSetReadOnly(
329 T::path());
332 /// Get read/write access to the given configuration set, storing any
333 /// modifications via the given changes batch.
334 static
335 css::uno::Reference< css::container::XNameContainer >
336 get(std::shared_ptr< ConfigurationChanges > const & batch)
338 return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
339 batch, T::path());
342 private:
343 ConfigurationSet(const ConfigurationSet&) = delete;
344 ConfigurationSet& operator=(const ConfigurationSet&) = delete;
346 ConfigurationSet() = delete;
347 ~ConfigurationSet() = delete;
352 #endif
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */