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 <string_view>
18 #include <com/sun/star/uno/Any.hxx>
19 #include <com/sun/star/uno/Reference.h>
20 #include <comphelper/comphelperdllapi.h>
21 #include <comphelper/processfactory.hxx>
22 #include <sal/types.h>
25 namespace com::sun::star
{
26 namespace configuration
{ class XReadWriteAccess
; }
28 class XHierarchicalNameAccess
;
29 class XHierarchicalNameReplace
;
33 namespace uno
{ class XComponentContext
; }
36 namespace comphelper
{
38 namespace detail
{ class ConfigurationWrapper
; }
40 /// A batch of configuration changes that is committed as a whole.
42 /// Client code needs to call commit explicitly; otherwise the changes are lost
43 /// when the instance is destroyed.
45 /// This is the only class from this header file that client code should use
47 class COMPHELPER_DLLPUBLIC ConfigurationChanges
{
49 static std::shared_ptr
<ConfigurationChanges
> create(
50 css::uno::Reference
<css::uno::XComponentContext
> const & context
51 = css::uno::Reference
<css::uno::XComponentContext
>());
53 ~ConfigurationChanges();
58 ConfigurationChanges(const ConfigurationChanges
&) = delete;
59 ConfigurationChanges
& operator=(const ConfigurationChanges
&) = delete;
61 SAL_DLLPRIVATE
ConfigurationChanges(
62 css::uno::Reference
< css::uno::XComponentContext
>
65 SAL_DLLPRIVATE
void setPropertyValue(
66 OUString
const & path
, css::uno::Any
const & value
)
69 SAL_DLLPRIVATE
css::uno::Reference
<
70 css::container::XHierarchicalNameReplace
>
71 getGroup(OUString
const & path
) const;
74 css::uno::Reference
< css::container::XNameContainer
>
75 getSet(OUString
const & path
) const;
78 css::configuration::XReadWriteAccess
> access_
;
80 friend class detail::ConfigurationWrapper
;
86 class COMPHELPER_DLLPUBLIC ConfigurationWrapper
{
88 static ConfigurationWrapper
const & get(
89 css::uno::Reference
<css::uno::XComponentContext
> const & context
);
91 bool isReadOnly(OUString
const & path
) const;
93 css::uno::Any
getPropertyValue(std::u16string_view path
) const;
95 static void setPropertyValue(
96 std::shared_ptr
< ConfigurationChanges
> const & batch
,
97 OUString
const & path
, css::uno::Any
const & value
);
99 css::uno::Any
getLocalizedPropertyValue(
100 std::u16string_view path
) const;
102 static void setLocalizedPropertyValue(
103 std::shared_ptr
< ConfigurationChanges
> const & batch
,
104 OUString
const & path
, css::uno::Any
const & value
);
107 css::container::XHierarchicalNameAccess
>
108 getGroupReadOnly(OUString
const & path
) const;
110 static css::uno::Reference
<
111 css::container::XHierarchicalNameReplace
>
113 std::shared_ptr
< ConfigurationChanges
> const & batch
,
114 OUString
const & path
);
116 css::uno::Reference
< css::container::XNameAccess
>
117 getSetReadOnly(OUString
const & path
) const;
119 static css::uno::Reference
< css::container::XNameContainer
>
121 std::shared_ptr
< ConfigurationChanges
> const & batch
,
122 OUString
const & path
);
124 std::shared_ptr
< ConfigurationChanges
> createChanges() const;
127 SAL_DLLPRIVATE
explicit ConfigurationWrapper(
128 css::uno::Reference
<css::uno::XComponentContext
> const & context
);
130 SAL_DLLPRIVATE
~ConfigurationWrapper();
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::Any(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::Any(*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 // Avoid using the config layer and rely on defaults which is only useful
186 // for special test tool targets (typically fuzzing) where start-up speed
188 #if defined(FUZZING_BUILD_MODE_UNSAFE_FOR_PRODUCTION)
189 constexpr bool IsFuzzing() { return true; }
191 COMPHELPER_DLLPUBLIC
bool IsFuzzing();
193 COMPHELPER_DLLPUBLIC
void EnableFuzzing();
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(
205 css::uno::Reference
<css::uno::XComponentContext
> const & context
206 = css::uno::Reference
<css::uno::XComponentContext
>())
208 return detail::ConfigurationWrapper::get(context
).isReadOnly(T::path());
211 /// Get the value of the given (non-localized) configuration property.
213 /// For nillable properties, U is of type std::optional<U'>.
215 css::uno::Reference
<css::uno::XComponentContext
> const & context
216 = css::uno::Reference
<css::uno::XComponentContext
>())
218 if (comphelper::IsFuzzing())
220 // Folding this into one statement causes a bogus error at least with
221 // Red Hat GCC 4.6.2-1:
223 detail::ConfigurationWrapper::get(context
).getPropertyValue(
225 return detail::Convert
< U
>::fromAny(a
);
228 /// Set the value of the given (non-localized) configuration property, via a
229 /// given changes batch.
231 /// For nillable properties, U is of type std::optional<U'>.
234 std::shared_ptr
< ConfigurationChanges
> const & batch
)
236 comphelper::detail::ConfigurationWrapper::setPropertyValue(
237 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
241 ConfigurationProperty(const ConfigurationProperty
&) = delete;
242 ConfigurationProperty
& operator=(const ConfigurationProperty
&) = delete;
244 ConfigurationProperty() = delete;
245 ~ConfigurationProperty() = delete;
248 /// A type-safe wrapper around a localized configuration property.
250 /// Automatically generated headers for the various localized configuration
251 /// properties derive from this template and make available its member functions
252 /// to access each given localized configuration property.
253 template< typename T
, typename U
> struct ConfigurationLocalizedProperty
255 /// Get the read-only status of the given (localized) configuration
257 static bool isReadOnly(
258 css::uno::Reference
<css::uno::XComponentContext
> const & context
259 = css::uno::Reference
<css::uno::XComponentContext
>())
261 return detail::ConfigurationWrapper::get(context
).isReadOnly(T::path());
264 /// Get the value of the given localized configuration property, for the
265 /// locale currently set at the
266 /// com.sun.star.configuration.theDefaultProvider.
268 /// For nillable properties, U is of type std::optional<U'>.
270 css::uno::Reference
<css::uno::XComponentContext
> const & context
271 = css::uno::Reference
<css::uno::XComponentContext
>())
273 // Folding this into one statement causes a bogus error at least with
274 // Red Hat GCC 4.6.2-1:
276 detail::ConfigurationWrapper::get(context
).
277 getLocalizedPropertyValue(T::path()));
278 return detail::Convert
< U
>::fromAny(a
);
281 /// Set the value of the given localized configuration property, for the
282 /// locale currently set at the
283 /// com.sun.star.configuration.theDefaultProvider, via a given changes
286 /// For nillable properties, U is of type std::optional<U'>.
289 std::shared_ptr
< ConfigurationChanges
> const & batch
)
291 comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
292 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
296 ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty
&) = delete;
297 ConfigurationLocalizedProperty
& operator=(const ConfigurationLocalizedProperty
&) = delete;
299 ConfigurationLocalizedProperty() = delete;
300 ~ConfigurationLocalizedProperty() = delete;
303 /// A type-safe wrapper around a configuration group.
305 /// Automatically generated headers for the various configuration groups derive
306 /// from this template and make available its member functions to access each
307 /// given configuration group.
308 template< typename T
> struct ConfigurationGroup
{
309 /// Get the read-only status of the given configuration group.
310 static bool isReadOnly(
311 css::uno::Reference
<css::uno::XComponentContext
> const & context
312 = css::uno::Reference
<css::uno::XComponentContext
>())
314 return detail::ConfigurationWrapper::get(context
).isReadOnly(T::path());
317 /// Get read-only access to the given configuration group.
318 static css::uno::Reference
<
319 css::container::XHierarchicalNameAccess
>
320 get(css::uno::Reference
<css::uno::XComponentContext
> const & context
321 = css::uno::Reference
<css::uno::XComponentContext
>())
323 return detail::ConfigurationWrapper::get(context
).getGroupReadOnly(
327 /// Get read/write access to the given configuration group, storing any
328 /// modifications via the given changes batch.
329 static css::uno::Reference
<
330 css::container::XHierarchicalNameReplace
>
331 get(std::shared_ptr
< ConfigurationChanges
> const & batch
)
333 return comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
338 ConfigurationGroup(const ConfigurationGroup
&) = delete;
339 ConfigurationGroup
& operator=(const ConfigurationGroup
&) = delete;
341 ConfigurationGroup() = delete;
342 ~ConfigurationGroup() = delete;
345 /// A type-safe wrapper around a configuration set.
347 /// Automatically generated headers for the various configuration sets derive
348 /// from this template and make available its member functions to access each
349 /// given configuration set.
350 template< typename T
> struct ConfigurationSet
{
351 /// Get the read-only status of the given configuration set.
352 static bool isReadOnly(
353 css::uno::Reference
<css::uno::XComponentContext
> const & context
354 = css::uno::Reference
<css::uno::XComponentContext
>())
356 return detail::ConfigurationWrapper::get(context
).isReadOnly(T::path());
359 /// Get read-only access to the given configuration set.
361 css::uno::Reference
< css::container::XNameAccess
>
362 get(css::uno::Reference
<css::uno::XComponentContext
> const & context
363 = css::uno::Reference
<css::uno::XComponentContext
>())
365 return detail::ConfigurationWrapper::get(context
).getSetReadOnly(
369 /// Get read/write access to the given configuration set, storing any
370 /// modifications via the given changes batch.
372 css::uno::Reference
< css::container::XNameContainer
>
373 get(std::shared_ptr
< ConfigurationChanges
> const & batch
)
375 return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
380 ConfigurationSet(const ConfigurationSet
&) = delete;
381 ConfigurationSet
& operator=(const ConfigurationSet
&) = delete;
383 ConfigurationSet() = delete;
384 ~ConfigurationSet() = delete;
391 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */