1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
10 #ifndef INCLUDED_COMPHELPER_CONFIGURATION_HXX
11 #define INCLUDED_COMPHELPER_CONFIGURATION_HXX
13 #include <sal/config.h>
16 #include <com/sun/star/uno/Any.hxx>
17 #include <com/sun/star/uno/Reference.h>
18 #include <comphelper/comphelperdllapi.h>
19 #include <comphelper/processfactory.hxx>
20 #include <sal/types.h>
23 namespace com::sun::star
{
24 namespace configuration
{ class XReadWriteAccess
; }
26 class XHierarchicalNameAccess
;
27 class XHierarchicalNameReplace
;
31 namespace uno
{ class XComponentContext
; }
34 namespace comphelper
{
36 namespace detail
{ class ConfigurationWrapper
; }
38 /// A batch of configuration changes that is committed as a whole.
40 /// Client code needs to call commit explicitly; otherwise the changes are lost
41 /// when the instance is destroyed.
43 /// This is the only class from this header file that client code should use
45 class COMPHELPER_DLLPUBLIC ConfigurationChanges
{
47 static std::shared_ptr
<ConfigurationChanges
> create(
48 css::uno::Reference
< css::uno::XComponentContext
>
49 const & context
= comphelper::getProcessComponentContext());
51 ~ConfigurationChanges();
56 ConfigurationChanges(const ConfigurationChanges
&) = delete;
57 ConfigurationChanges
& operator=(const ConfigurationChanges
&) = delete;
59 SAL_DLLPRIVATE
ConfigurationChanges(
60 css::uno::Reference
< css::uno::XComponentContext
>
63 SAL_DLLPRIVATE
void setPropertyValue(
64 OUString
const & path
, css::uno::Any
const & value
)
67 SAL_DLLPRIVATE
css::uno::Reference
<
68 css::container::XHierarchicalNameReplace
>
69 getGroup(OUString
const & path
) const;
72 css::uno::Reference
< css::container::XNameContainer
>
73 getSet(OUString
const & path
) const;
76 css::configuration::XReadWriteAccess
> access_
;
78 friend class detail::ConfigurationWrapper
;
84 class COMPHELPER_DLLPUBLIC ConfigurationWrapper
{
86 static ConfigurationWrapper
const & get(
87 css::uno::Reference
< css::uno::XComponentContext
>
90 SAL_DLLPRIVATE
explicit ConfigurationWrapper(
91 css::uno::Reference
< css::uno::XComponentContext
>
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
);
112 css::container::XHierarchicalNameAccess
>
113 getGroupReadOnly(OUString
const & path
) const;
115 static css::uno::Reference
<
116 css::container::XHierarchicalNameReplace
>
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
>
126 std::shared_ptr
< ConfigurationChanges
> const & batch
,
127 OUString
const & path
);
129 std::shared_ptr
< ConfigurationChanges
> createChanges() const;
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 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
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
>(); }
153 Convert(const Convert
&) = delete;
154 Convert
& operator=(const Convert
&) = delete;
161 template< typename T
> struct Convert
< std::optional
< T
> >
163 static css::uno::Any
toAny(std::optional
< T
> const & value
) {
165 ? css::uno::makeAny(*value
)
169 static std::optional
< T
> fromAny(css::uno::Any
const & value
)
171 return value
.hasValue()
172 ? std::optional
< T
>(value
.get
< T
>()) : std::optional
< T
>();
176 Convert(const Convert
&) = delete;
177 Convert
& operator=(const 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
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 std::optional<U'>.
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:
211 detail::ConfigurationWrapper::get(context
).getPropertyValue(
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 std::optional<U'>.
222 std::shared_ptr
< ConfigurationChanges
> const & batch
)
224 comphelper::detail::ConfigurationWrapper::setPropertyValue(
225 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
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 std::optional<U'>.
248 static U
get(css::uno::Reference
< css::uno::XComponentContext
> const & context
)
250 // Folding this into one statement causes a bogus error at least with
251 // Red Hat GCC 4.6.2-1:
253 detail::ConfigurationWrapper::get(context
).
254 getLocalizedPropertyValue(T::path()));
255 return detail::Convert
< U
>::fromAny(a
);
258 /// Set the value of the given localized configuration property, for the
259 /// locale currently set at the
260 /// com.sun.star.configuration.theDefaultProvider, via a given changes
263 /// For nillable properties, U is of type std::optional<U'>.
266 std::shared_ptr
< ConfigurationChanges
> const & batch
)
268 comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
269 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
273 ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty
&) = delete;
274 ConfigurationLocalizedProperty
& operator=(const ConfigurationLocalizedProperty
&) = delete;
276 ConfigurationLocalizedProperty() = delete;
277 ~ConfigurationLocalizedProperty() = delete;
280 /// A type-safe wrapper around a configuration group.
282 /// Automatically generated headers for the various configuration groups derive
283 /// from this template and make available its member functions to access each
284 /// given configuration group.
285 template< typename T
> struct ConfigurationGroup
{
286 /// Get read-only access to the given configuration group.
287 static css::uno::Reference
<
288 css::container::XHierarchicalNameAccess
>
289 get(css::uno::Reference
< css::uno::XComponentContext
>
290 const & context
= comphelper::getProcessComponentContext())
292 return detail::ConfigurationWrapper::get(context
).getGroupReadOnly(
296 /// Get read/write access to the given configuration group, storing any
297 /// modifications via the given changes batch.
298 static css::uno::Reference
<
299 css::container::XHierarchicalNameReplace
>
300 get(std::shared_ptr
< ConfigurationChanges
> const & batch
)
302 return comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
307 ConfigurationGroup(const ConfigurationGroup
&) = delete;
308 ConfigurationGroup
& operator=(const ConfigurationGroup
&) = delete;
310 ConfigurationGroup() = delete;
311 ~ConfigurationGroup() = delete;
314 /// A type-safe wrapper around a configuration set.
316 /// Automatically generated headers for the various configuration sets derive
317 /// from this template and make available its member functions to access each
318 /// given configuration set.
319 template< typename T
> struct ConfigurationSet
{
320 /// Get read-only access to the given configuration set.
322 css::uno::Reference
< css::container::XNameAccess
>
323 get(css::uno::Reference
< css::uno::XComponentContext
>
324 const & context
= comphelper::getProcessComponentContext())
326 return detail::ConfigurationWrapper::get(context
).getSetReadOnly(
330 /// Get read/write access to the given configuration set, storing any
331 /// modifications via the given changes batch.
333 css::uno::Reference
< css::container::XNameContainer
>
334 get(std::shared_ptr
< ConfigurationChanges
> const & batch
)
336 return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
341 ConfigurationSet(const ConfigurationSet
&) = delete;
342 ConfigurationSet
& operator=(const ConfigurationSet
&) = delete;
344 ConfigurationSet() = delete;
345 ~ConfigurationSet() = delete;
352 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */