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>
15 #include <com/sun/star/uno/Any.hxx>
16 #include <com/sun/star/uno/Reference.h>
17 #include <comphelper/comphelperdllapi.h>
18 #include <comphelper/processfactory.hxx>
19 #include <sal/types.h>
23 #include <string_view>
24 #include <unordered_map>
26 namespace com::sun::star
{
27 namespace configuration
{ class XReadWriteAccess
; }
29 class XHierarchicalNameAccess
;
30 class XHierarchicalNameReplace
;
34 namespace uno
{ class XComponentContext
; }
36 class XChangesListener
;
37 class XChangesNotifier
;
41 namespace comphelper
{
43 namespace detail
{ class ConfigurationWrapper
; }
45 /// A batch of configuration changes that is committed as a whole.
47 /// Client code needs to call commit explicitly; otherwise the changes are lost
48 /// when the instance is destroyed.
50 /// This is the only class from this header file that client code should use
52 class COMPHELPER_DLLPUBLIC ConfigurationChanges
{
54 static std::shared_ptr
<ConfigurationChanges
> create();
56 ~ConfigurationChanges();
61 ConfigurationChanges(const ConfigurationChanges
&) = delete;
62 ConfigurationChanges
& operator=(const ConfigurationChanges
&) = delete;
64 SAL_DLLPRIVATE
ConfigurationChanges(
65 css::uno::Reference
< css::uno::XComponentContext
>
68 SAL_DLLPRIVATE
void setPropertyValue(
69 OUString
const & path
, css::uno::Any
const & value
)
72 SAL_DLLPRIVATE
css::uno::Reference
<
73 css::container::XHierarchicalNameReplace
>
74 getGroup(OUString
const & path
) const;
77 css::uno::Reference
< css::container::XNameContainer
>
78 getSet(OUString
const & path
) const;
81 css::configuration::XReadWriteAccess
> access_
;
83 friend class detail::ConfigurationWrapper
;
88 class ConfigurationChangesListener
;
91 class COMPHELPER_DLLPUBLIC ConfigurationWrapper
{
92 friend class ConfigurationChangesListener
;
94 static ConfigurationWrapper
const & get();
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 std::u16string_view 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 SAL_DLLPRIVATE
explicit ConfigurationWrapper();
134 SAL_DLLPRIVATE
~ConfigurationWrapper();
136 ConfigurationWrapper(const ConfigurationWrapper
&) = delete;
137 ConfigurationWrapper
& operator=(const ConfigurationWrapper
&) = delete;
139 css::uno::Reference
< css::uno::XComponentContext
> context_
;
141 css::uno::Reference
< css::configuration::XReadWriteAccess
> access_
;
142 // should really be a css.configuration.ReadOnlyAccess (with added
143 // css.beans.XHierarchicalPropertySetInfo), but then
144 // configmgr::Access::asProperty() would report all properties as
145 // READONLY, so isReadOnly() would not work
147 mutable std::mutex maMutex
;
149 mutable std::unordered_map
<OUString
, css::uno::Any
> maPropertyCache
;
150 css::uno::Reference
< css::util::XChangesNotifier
> maNotifier
;
151 css::uno::Reference
< css::util::XChangesListener
> maListener
;
155 template< typename T
> struct Convert
{
156 static css::uno::Any
toAny(T
const & value
)
157 { return css::uno::Any(value
); }
159 static T
fromAny(css::uno::Any
const & value
)
160 { return value
.get
< T
>(); }
163 Convert(const Convert
&) = delete;
164 Convert
& operator=(const Convert
&) = delete;
171 template< typename T
> struct Convert
< std::optional
< T
> >
173 static css::uno::Any
toAny(std::optional
< T
> const & value
) {
175 ? css::uno::Any(*value
)
179 static std::optional
< T
> fromAny(css::uno::Any
const & value
)
181 return value
.hasValue()
182 ? std::optional
< T
>(value
.get
< T
>()) : std::optional
< T
>();
186 Convert(const Convert
&) = delete;
187 Convert
& operator=(const Convert
&) = delete;
195 /// A type-safe wrapper around a (non-localized) configuration property.
197 /// Automatically generated headers for the various configuration properties
198 /// derive from this template and make available its member functions to access
199 /// each given configuration property.
200 template< typename T
, typename U
> struct ConfigurationProperty
202 /// Get the read-only status of the given (non-localized) configuration
204 static bool isReadOnly()
206 return detail::ConfigurationWrapper::get().isReadOnly(T::path());
209 /// Get the value of the given (non-localized) configuration property.
211 /// For nillable properties, U is of type std::optional<U'>.
214 // Folding this into one statement causes a bogus error at least with
215 // Red Hat GCC 4.6.2-1:
217 detail::ConfigurationWrapper::get().getPropertyValue(
219 return detail::Convert
< U
>::fromAny(a
);
222 /// Set the value of the given (non-localized) configuration property, via a
223 /// given changes batch.
225 /// For nillable properties, U is of type std::optional<U'>.
228 std::shared_ptr
< ConfigurationChanges
> const & batch
)
230 comphelper::detail::ConfigurationWrapper::setPropertyValue(
231 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
235 ConfigurationProperty(const ConfigurationProperty
&) = delete;
236 ConfigurationProperty
& operator=(const ConfigurationProperty
&) = delete;
238 ConfigurationProperty() = delete;
239 ~ConfigurationProperty() = delete;
242 /// A type-safe wrapper around a localized configuration property.
244 /// Automatically generated headers for the various localized configuration
245 /// properties derive from this template and make available its member functions
246 /// to access each given localized configuration property.
247 template< typename T
, typename U
> struct ConfigurationLocalizedProperty
249 /// Get the value of the given localized configuration property, for the
250 /// locale currently set at the
251 /// com.sun.star.configuration.theDefaultProvider.
253 /// For nillable properties, U is of type std::optional<U'>.
256 // Folding this into one statement causes a bogus error at least with
257 // Red Hat GCC 4.6.2-1:
259 detail::ConfigurationWrapper::get().
260 getLocalizedPropertyValue(T::path()));
261 return detail::Convert
< U
>::fromAny(a
);
264 /// Set the value of the given localized configuration property, for the
265 /// locale currently set at the
266 /// com.sun.star.configuration.theDefaultProvider, via a given changes
269 /// For nillable properties, U is of type std::optional<U'>.
272 std::shared_ptr
< ConfigurationChanges
> const & batch
)
274 comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
275 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
279 ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty
&) = delete;
280 ConfigurationLocalizedProperty
& operator=(const ConfigurationLocalizedProperty
&) = delete;
282 ConfigurationLocalizedProperty() = delete;
283 ~ConfigurationLocalizedProperty() = delete;
286 /// A type-safe wrapper around a configuration group.
288 /// Automatically generated headers for the various configuration groups derive
289 /// from this template and make available its member functions to access each
290 /// given configuration group.
291 template< typename T
> struct ConfigurationGroup
{
292 /// Get read-only access to the given configuration group.
293 static css::uno::Reference
<
294 css::container::XHierarchicalNameAccess
>
297 return detail::ConfigurationWrapper::get().getGroupReadOnly(
301 /// Get read/write access to the given configuration group, storing any
302 /// modifications via the given changes batch.
303 static css::uno::Reference
<
304 css::container::XHierarchicalNameReplace
>
305 get(std::shared_ptr
< ConfigurationChanges
> const & batch
)
307 return comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
312 ConfigurationGroup(const ConfigurationGroup
&) = delete;
313 ConfigurationGroup
& operator=(const ConfigurationGroup
&) = delete;
315 ConfigurationGroup() = delete;
316 ~ConfigurationGroup() = delete;
319 /// A type-safe wrapper around a configuration set.
321 /// Automatically generated headers for the various configuration sets derive
322 /// from this template and make available its member functions to access each
323 /// given configuration set.
324 template< typename T
> struct ConfigurationSet
{
325 /// Get read-only access to the given configuration set.
327 css::uno::Reference
< css::container::XNameAccess
>
330 return detail::ConfigurationWrapper::get().getSetReadOnly(
334 /// Get read/write access to the given configuration set, storing any
335 /// modifications via the given changes batch.
337 css::uno::Reference
< css::container::XNameContainer
>
338 get(std::shared_ptr
< ConfigurationChanges
> const & batch
)
340 return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
345 ConfigurationSet(const ConfigurationSet
&) = delete;
346 ConfigurationSet
& operator=(const ConfigurationSet
&) = delete;
348 ConfigurationSet() = delete;
349 ~ConfigurationSet() = delete;
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */