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 <boost/noncopyable.hpp>
16 #include <boost/optional.hpp>
17 #include <boost/shared_ptr.hpp>
18 #include <com/sun/star/uno/Any.hxx>
19 #include <com/sun/star/uno/Reference.hxx>
20 #include <comphelper/comphelperdllapi.h>
21 #include <comphelper/processfactory.hxx>
22 #include <sal/types.h>
24 namespace com
{ namespace sun
{ namespace star
{
25 namespace configuration
{ class XReadWriteAccess
; }
27 class XHierarchicalNameAccess
;
28 class XHierarchicalNameReplace
;
32 namespace uno
{ class XComponentContext
; }
35 namespace comphelper
{
37 namespace detail
{ class ConfigurationWrapper
; }
39 /// A batch of configuration changes that is committed as a whole.
41 /// Client code needs to call commit explicitly; otherwise the changes are lost
42 /// when the instance is destroyed.
44 /// This is the only class from this header file that client code should use
46 class COMPHELPER_DLLPUBLIC ConfigurationChanges
: private boost::noncopyable
{
48 static boost::shared_ptr
< ConfigurationChanges
> create(
49 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
50 const & context
= comphelper::getProcessComponentContext());
52 ~ConfigurationChanges();
57 SAL_DLLPRIVATE
ConfigurationChanges(
58 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
61 SAL_DLLPRIVATE
void setPropertyValue(
62 OUString
const & path
, com::sun::star::uno::Any
const & value
)
65 SAL_DLLPRIVATE
com::sun::star::uno::Reference
<
66 com::sun::star::container::XHierarchicalNameReplace
>
67 getGroup(OUString
const & path
) const;
70 com::sun::star::uno::Reference
< com::sun::star::container::XNameContainer
>
71 getSet(OUString
const & path
) const;
73 com::sun::star::uno::Reference
<
74 com::sun::star::configuration::XReadWriteAccess
> access_
;
76 friend class detail::ConfigurationWrapper
;
82 class COMPHELPER_DLLPUBLIC ConfigurationWrapper
: private boost::noncopyable
{
84 static ConfigurationWrapper
const & get(
85 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
88 SAL_DLLPRIVATE
explicit ConfigurationWrapper(
89 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
92 SAL_DLLPRIVATE
~ConfigurationWrapper();
94 com::sun::star::uno::Any
getPropertyValue(OUString
const & path
) const;
96 void setPropertyValue(
97 boost::shared_ptr
< ConfigurationChanges
> const & batch
,
98 OUString
const & path
, com::sun::star::uno::Any
const & value
)
101 com::sun::star::uno::Any
getLocalizedPropertyValue(
102 OUString
const & path
) const;
104 void setLocalizedPropertyValue(
105 boost::shared_ptr
< ConfigurationChanges
> const & batch
,
106 OUString
const & path
, com::sun::star::uno::Any
const & value
)
109 com::sun::star::uno::Reference
<
110 com::sun::star::container::XHierarchicalNameAccess
>
111 getGroupReadOnly(OUString
const & path
) const;
113 com::sun::star::uno::Reference
<
114 com::sun::star::container::XHierarchicalNameReplace
>
116 boost::shared_ptr
< ConfigurationChanges
> const & batch
,
117 OUString
const & path
) const;
119 com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
>
120 getSetReadOnly(OUString
const & path
) const;
122 com::sun::star::uno::Reference
< com::sun::star::container::XNameContainer
>
124 boost::shared_ptr
< ConfigurationChanges
> const & batch
,
125 OUString
const & path
) const;
127 boost::shared_ptr
< ConfigurationChanges
> createChanges() const;
130 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
133 com::sun::star::uno::Reference
<
134 com::sun::star::container::XHierarchicalNameAccess
> access_
;
138 template< typename T
> struct Convert
: private boost::noncopyable
{
139 static com::sun::star::uno::Any
toAny(T
const & value
)
140 { return com::sun::star::uno::makeAny(value
); }
142 static T
fromAny(com::sun::star::uno::Any
const & value
)
143 { return value
.get
< T
>(); }
146 Convert(); // not defined
147 ~Convert(); // not defined
151 template< typename T
> struct Convert
< boost::optional
< T
> >:
152 private boost::noncopyable
154 static com::sun::star::uno::Any
toAny(boost::optional
< T
> const & value
) {
156 ? com::sun::star::uno::makeAny(value
.get())
157 : com::sun::star::uno::Any();
160 static boost::optional
< T
> fromAny(com::sun::star::uno::Any
const & value
)
162 return value
.hasValue()
163 ? boost::optional
< T
>(value
.get
< T
>()) : boost::optional
< T
>();
167 Convert(); // not defined
168 ~Convert(); // not defined
173 /// A type-safe wrapper around a (non-localized) configuration property.
175 /// Automatically generated headers for the various configuration properties
176 /// derive from this template and make available its member functions to access
177 /// each given configuration property.
178 template< typename T
, typename U
> struct ConfigurationProperty
:
179 private boost::noncopyable
181 /// Get the value of the given (non-localized) configuration property.
183 /// For nillable properties, U is of type boost::optional<U'>.
185 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
186 const & context
= comphelper::getProcessComponentContext())
188 // Folding this into one statement causes a bogus error at least with
189 // Red Hat GCC 4.6.2-1:
190 com::sun::star::uno::Any
a(
191 detail::ConfigurationWrapper::get(context
).getPropertyValue(
193 return detail::Convert
< U
>::fromAny(a
);
196 /// Set the value of the given (non-localized) configuration property, via a
197 /// given changes batch.
199 /// For nillable properties, U is of type boost::optional<U'>.
202 boost::shared_ptr
< ConfigurationChanges
> const & batch
,
203 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
204 const & context
= comphelper::getProcessComponentContext())
206 detail::ConfigurationWrapper::get(context
).setPropertyValue(
207 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
211 ConfigurationProperty(); // not defined
212 ~ConfigurationProperty(); // not defined
215 /// A type-safe wrapper around a localized configuration property.
217 /// Automatically generated headers for the various localized configuration
218 /// properties derive from this template and make available its member functions
219 /// to access each given localized configuration property.
220 template< typename T
, typename U
> struct ConfigurationLocalizedProperty
:
221 private boost::noncopyable
223 /// Get the value of the given localized configuration property, for the
224 /// locale currently set at the
225 /// com.sun.star.configuration.theDefaultProvider.
227 /// For nillable properties, U is of type boost::optional<U'>.
229 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
230 const & context
= comphelper::getProcessComponentContext())
232 // Folding this into one statement causes a bogus error at least with
233 // Red Hat GCC 4.6.2-1:
234 com::sun::star::uno::Any
a(
235 detail::ConfigurationWrapper::get(context
).
236 getLocalizedPropertyValue(T::path()));
237 return detail::Convert
< U
>::fromAny(a
);
240 /// Set the value of the given localized configuration property, for the
241 /// locale currently set at the
242 /// com.sun.star.configuration.theDefaultProvider, via a given changes
245 /// For nillable properties, U is of type boost::optional<U'>.
248 boost::shared_ptr
< ConfigurationChanges
> const & batch
,
249 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
250 const & context
= comphelper::getProcessComponentContext())
252 detail::ConfigurationWrapper::get(context
).setLocalizedPropertyValue(
253 batch
, T::path(), detail::Convert
< U
>::toAny(value
));
257 ConfigurationLocalizedProperty(); // not defined
258 ~ConfigurationLocalizedProperty(); // not defined
261 /// A type-safe wrapper around a configuration group.
263 /// Automatically generated headers for the various configuration groups derive
264 /// from this template and make available its member functions to access each
265 /// given configuration group.
266 template< typename T
> struct ConfigurationGroup
: private boost::noncopyable
{
267 /// Get read-only access to the given configuration group.
268 static com::sun::star::uno::Reference
<
269 com::sun::star::container::XHierarchicalNameAccess
>
270 get(com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
271 const & context
= comphelper::getProcessComponentContext())
273 return detail::ConfigurationWrapper::get(context
).getGroupReadOnly(
277 /// Get read/write access to the given configuration group, storing any
278 /// modifications via the given changes batch.
279 static com::sun::star::uno::Reference
<
280 com::sun::star::container::XHierarchicalNameReplace
>
281 get(boost::shared_ptr
< ConfigurationChanges
> const & batch
,
282 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
283 const & context
= comphelper::getProcessComponentContext())
285 return detail::ConfigurationWrapper::get(context
).getGroupReadWrite(
290 ConfigurationGroup(); // not defined
291 ~ConfigurationGroup(); // not defined
294 /// A type-safe wrapper around a configuration set.
296 /// Automatically generated headers for the various configuration sets derive
297 /// from this template and make available its member functions to access each
298 /// given configuration set.
299 template< typename T
> struct ConfigurationSet
: private boost::noncopyable
{
300 /// Get read-only access to the given configuration set.
302 com::sun::star::uno::Reference
< com::sun::star::container::XNameAccess
>
303 get(com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
304 const & context
= comphelper::getProcessComponentContext())
306 return detail::ConfigurationWrapper::get(context
).getSetReadOnly(
310 /// Get read/write access to the given configuration set, storing any
311 /// modifications via the given changes batch.
313 com::sun::star::uno::Reference
< com::sun::star::container::XNameContainer
>
314 get(boost::shared_ptr
< ConfigurationChanges
> const & batch
,
315 com::sun::star::uno::Reference
< com::sun::star::uno::XComponentContext
>
316 const & context
= comphelper::getProcessComponentContext())
318 return detail::ConfigurationWrapper::get(context
).getSetReadWrite(
323 ConfigurationSet(); // not defined
324 ~ConfigurationSet(); // not defined
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */