Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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 : */
9 :
10 : #ifndef INCLUDED_COMPHELPER_CONFIGURATION_HXX
11 : #define INCLUDED_COMPHELPER_CONFIGURATION_HXX
12 :
13 : #include <sal/config.h>
14 :
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>
22 :
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;
30 : }
31 : namespace uno { class XComponentContext; }
32 : } } }
33 :
34 : namespace comphelper {
35 :
36 : namespace detail { class ConfigurationWrapper; }
37 :
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 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
49 : const & context = comphelper::getProcessComponentContext());
50 :
51 : ~ConfigurationChanges();
52 :
53 : void commit() const;
54 :
55 : private:
56 : ConfigurationChanges(const ConfigurationChanges&) SAL_DELETED_FUNCTION;
57 : ConfigurationChanges& operator=(const ConfigurationChanges&) SAL_DELETED_FUNCTION;
58 :
59 : SAL_DLLPRIVATE ConfigurationChanges(
60 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
61 : const & context);
62 :
63 : SAL_DLLPRIVATE void setPropertyValue(
64 : OUString const & path, com::sun::star::uno::Any const & value)
65 : const;
66 :
67 : SAL_DLLPRIVATE com::sun::star::uno::Reference<
68 : com::sun::star::container::XHierarchicalNameReplace >
69 : getGroup(OUString const & path) const;
70 :
71 : SAL_DLLPRIVATE
72 : com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >
73 : getSet(OUString const & path) const;
74 :
75 : com::sun::star::uno::Reference<
76 : com::sun::star::configuration::XReadWriteAccess > access_;
77 :
78 : friend class detail::ConfigurationWrapper;
79 : };
80 :
81 : namespace detail {
82 :
83 : /// @internal
84 : class COMPHELPER_DLLPUBLIC ConfigurationWrapper {
85 : public:
86 : static ConfigurationWrapper const & get(
87 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
88 : const & context);
89 :
90 : SAL_DLLPRIVATE explicit ConfigurationWrapper(
91 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
92 : const & context);
93 :
94 : SAL_DLLPRIVATE ~ConfigurationWrapper();
95 :
96 : bool isReadOnly(OUString const & path) const;
97 :
98 : com::sun::star::uno::Any getPropertyValue(OUString const & path) const;
99 :
100 : static void setPropertyValue(
101 : std::shared_ptr< ConfigurationChanges > const & batch,
102 : OUString const & path, com::sun::star::uno::Any const & value);
103 :
104 : com::sun::star::uno::Any getLocalizedPropertyValue(
105 : OUString const & path) const;
106 :
107 : static void setLocalizedPropertyValue(
108 : std::shared_ptr< ConfigurationChanges > const & batch,
109 : OUString const & path, com::sun::star::uno::Any const & value);
110 :
111 : com::sun::star::uno::Reference<
112 : com::sun::star::container::XHierarchicalNameAccess >
113 : getGroupReadOnly(OUString const & path) const;
114 :
115 : static com::sun::star::uno::Reference<
116 : com::sun::star::container::XHierarchicalNameReplace >
117 : getGroupReadWrite(
118 : std::shared_ptr< ConfigurationChanges > const & batch,
119 : OUString const & path);
120 :
121 : com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >
122 : getSetReadOnly(OUString const & path) const;
123 :
124 : static com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >
125 : getSetReadWrite(
126 : std::shared_ptr< ConfigurationChanges > const & batch,
127 : OUString const & path);
128 :
129 : std::shared_ptr< ConfigurationChanges > createChanges() const;
130 :
131 : private:
132 : ConfigurationWrapper(const ConfigurationWrapper&) SAL_DELETED_FUNCTION;
133 : ConfigurationWrapper& operator=(const ConfigurationWrapper&) SAL_DELETED_FUNCTION;
134 :
135 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
136 : context_;
137 :
138 : com::sun::star::uno::Reference<
139 : com::sun::star::configuration::XReadWriteAccess > access_;
140 : // should really be an css.configuration.ReadOnlyAccess (with added
141 : // css.beans.XHierarchicalPropertySetInfo), but then
142 : // configmgr::Access::asProperty() would report all properties as
143 : // READONLY, so isReadOnly() would not work
144 : };
145 :
146 : /// @internal
147 : template< typename T > struct Convert {
148 517 : static com::sun::star::uno::Any toAny(T const & value)
149 517 : { return com::sun::star::uno::makeAny(value); }
150 :
151 189737 : static T fromAny(com::sun::star::uno::Any const & value)
152 189737 : { return value.get< T >(); }
153 :
154 : private:
155 : Convert(const Convert&) SAL_DELETED_FUNCTION;
156 : Convert& operator=(const Convert&) SAL_DELETED_FUNCTION;
157 :
158 : Convert() SAL_DELETED_FUNCTION;
159 : ~Convert() SAL_DELETED_FUNCTION;
160 : };
161 :
162 : /// @internal
163 : template< typename T > struct Convert< boost::optional< T > >
164 : {
165 0 : static com::sun::star::uno::Any toAny(boost::optional< T > const & value) {
166 : return value
167 0 : ? com::sun::star::uno::makeAny(value.get())
168 0 : : com::sun::star::uno::Any();
169 : }
170 :
171 3999 : static boost::optional< T > fromAny(com::sun::star::uno::Any const & value)
172 : {
173 3999 : return value.hasValue()
174 3999 : ? boost::optional< T >(value.get< T >()) : boost::optional< T >();
175 : }
176 :
177 : private:
178 : Convert(const Convert&) SAL_DELETED_FUNCTION;
179 : Convert& operator=(const Convert&) SAL_DELETED_FUNCTION;
180 :
181 : Convert() SAL_DELETED_FUNCTION;
182 : ~Convert() SAL_DELETED_FUNCTION;
183 : };
184 :
185 : }
186 :
187 : /// A type-safe wrapper around a (non-localized) configuration property.
188 : ///
189 : /// Automatically generated headers for the various configuration properties
190 : /// derive from this template and make available its member functions to access
191 : /// each given configuration property.
192 : template< typename T, typename U > struct ConfigurationProperty
193 : {
194 : /// Get the read-only status of the given (non-localized) configuration
195 : /// property.
196 0 : static bool isReadOnly(
197 : css::uno::Reference<css::uno::XComponentContext> const & context
198 : = comphelper::getProcessComponentContext())
199 : {
200 0 : return detail::ConfigurationWrapper::get(context).isReadOnly(T::path());
201 : }
202 :
203 : /// Get the value of the given (non-localized) configuration property.
204 : ///
205 : /// For nillable properties, U is of type boost::optional<U'>.
206 193736 : static U get(
207 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
208 : const & context = comphelper::getProcessComponentContext())
209 : {
210 : // Folding this into one statement causes a bogus error at least with
211 : // Red Hat GCC 4.6.2-1:
212 : com::sun::star::uno::Any a(
213 193736 : detail::ConfigurationWrapper::get(context).getPropertyValue(
214 387472 : T::path()));
215 193736 : return detail::Convert< U >::fromAny(a);
216 : }
217 :
218 : /// Set the value of the given (non-localized) configuration property, via a
219 : /// given changes batch.
220 : ///
221 : /// For nillable properties, U is of type boost::optional<U'>.
222 517 : static void set(
223 : U const & value,
224 : std::shared_ptr< ConfigurationChanges > const & batch)
225 : {
226 517 : comphelper::detail::ConfigurationWrapper::setPropertyValue(
227 1034 : batch, T::path(), detail::Convert< U >::toAny(value));
228 517 : }
229 :
230 : private:
231 : ConfigurationProperty(const ConfigurationProperty&) SAL_DELETED_FUNCTION;
232 : ConfigurationProperty& operator=(const ConfigurationProperty&) SAL_DELETED_FUNCTION;
233 :
234 : ConfigurationProperty() SAL_DELETED_FUNCTION;
235 : ~ConfigurationProperty() SAL_DELETED_FUNCTION;
236 : };
237 :
238 : /// A type-safe wrapper around a localized configuration property.
239 : ///
240 : /// Automatically generated headers for the various localized configuration
241 : /// properties derive from this template and make available its member functions
242 : /// to access each given localized configuration property.
243 : template< typename T, typename U > struct ConfigurationLocalizedProperty
244 : {
245 : /// Get the read-only status of the given localized configuration property.
246 : static bool isReadOnly(
247 : css::uno::Reference<css::uno::XComponentContext> const & context
248 : = comphelper::getProcessComponentContext())
249 : {
250 : return detail::ConfigurationWrapper::get(context).isReadOnly(T::path());
251 : }
252 :
253 : /// Get the value of the given localized configuration property, for the
254 : /// locale currently set at the
255 : /// com.sun.star.configuration.theDefaultProvider.
256 : ///
257 : /// For nillable properties, U is of type boost::optional<U'>.
258 : static U get(
259 : com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
260 : const & context = comphelper::getProcessComponentContext())
261 : {
262 : // Folding this into one statement causes a bogus error at least with
263 : // Red Hat GCC 4.6.2-1:
264 : com::sun::star::uno::Any a(
265 : detail::ConfigurationWrapper::get(context).
266 : getLocalizedPropertyValue(T::path()));
267 : return detail::Convert< U >::fromAny(a);
268 : }
269 :
270 : /// Set the value of the given localized configuration property, for the
271 : /// locale currently set at the
272 : /// com.sun.star.configuration.theDefaultProvider, via a given changes
273 : /// batch.
274 : ///
275 : /// For nillable properties, U is of type boost::optional<U'>.
276 : static void set(
277 : U const & value,
278 : std::shared_ptr< ConfigurationChanges > const & batch)
279 : {
280 : comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
281 : batch, T::path(), detail::Convert< U >::toAny(value));
282 : }
283 :
284 : private:
285 : ConfigurationLocalizedProperty(const ConfigurationLocalizedProperty&) SAL_DELETED_FUNCTION;
286 : ConfigurationLocalizedProperty& operator=(const ConfigurationLocalizedProperty&) SAL_DELETED_FUNCTION;
287 :
288 : ConfigurationLocalizedProperty() SAL_DELETED_FUNCTION;
289 : ~ConfigurationLocalizedProperty() SAL_DELETED_FUNCTION;
290 : };
291 :
292 : /// A type-safe wrapper around a configuration group.
293 : ///
294 : /// Automatically generated headers for the various configuration groups derive
295 : /// from this template and make available its member functions to access each
296 : /// given configuration group.
297 : template< typename T > struct ConfigurationGroup {
298 : /// Get read-only access to the given configuration group.
299 : static com::sun::star::uno::Reference<
300 : com::sun::star::container::XHierarchicalNameAccess >
301 3 : get(com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
302 : const & context = comphelper::getProcessComponentContext())
303 : {
304 3 : return detail::ConfigurationWrapper::get(context).getGroupReadOnly(
305 6 : T::path());
306 : }
307 :
308 : /// Get read/write access to the given configuration group, storing any
309 : /// modifications via the given changes batch.
310 : static com::sun::star::uno::Reference<
311 : com::sun::star::container::XHierarchicalNameReplace >
312 : get(std::shared_ptr< ConfigurationChanges > const & batch)
313 : {
314 : return comphelper::detail::ConfigurationWrapper::getGroupReadWrite(
315 : batch, T::path());
316 : }
317 :
318 : private:
319 : ConfigurationGroup(const ConfigurationGroup&) SAL_DELETED_FUNCTION;
320 : ConfigurationGroup& operator=(const ConfigurationGroup&) SAL_DELETED_FUNCTION;
321 :
322 : ConfigurationGroup() SAL_DELETED_FUNCTION;
323 : ~ConfigurationGroup() SAL_DELETED_FUNCTION;
324 : };
325 :
326 : /// A type-safe wrapper around a configuration set.
327 : ///
328 : /// Automatically generated headers for the various configuration sets derive
329 : /// from this template and make available its member functions to access each
330 : /// given configuration set.
331 : template< typename T > struct ConfigurationSet {
332 : /// Get read-only access to the given configuration set.
333 : static
334 : com::sun::star::uno::Reference< com::sun::star::container::XNameAccess >
335 4459 : get(com::sun::star::uno::Reference< com::sun::star::uno::XComponentContext >
336 : const & context = comphelper::getProcessComponentContext())
337 : {
338 4459 : return detail::ConfigurationWrapper::get(context).getSetReadOnly(
339 8918 : T::path());
340 : }
341 :
342 : /// Get read/write access to the given configuration set, storing any
343 : /// modifications via the given changes batch.
344 : static
345 : com::sun::star::uno::Reference< com::sun::star::container::XNameContainer >
346 0 : get(std::shared_ptr< ConfigurationChanges > const & batch)
347 : {
348 : return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
349 0 : batch, T::path());
350 : }
351 :
352 : private:
353 : ConfigurationSet(const ConfigurationSet&) SAL_DELETED_FUNCTION;
354 : ConfigurationSet& operator=(const ConfigurationSet&) SAL_DELETED_FUNCTION;
355 :
356 : ConfigurationSet() SAL_DELETED_FUNCTION;
357 : ~ConfigurationSet() SAL_DELETED_FUNCTION;
358 : };
359 :
360 : }
361 :
362 : #endif
363 :
364 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|