android: Update app-specific/MIME type icons
[LibreOffice.git] / include / comphelper / configuration.hxx
blob45228b7009443e823701dd808598c461acbf5127
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
8 */
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>
20 #include <memory>
21 #include <mutex>
22 #include <optional>
23 #include <string_view>
24 #include <unordered_map>
26 namespace com::sun::star {
27 namespace configuration { class XReadWriteAccess; }
28 namespace container {
29 class XHierarchicalNameAccess;
30 class XHierarchicalNameReplace;
31 class XNameAccess;
32 class XNameContainer;
34 namespace uno { class XComponentContext; }
35 namespace util {
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.
46 ///
47 /// Client code needs to call commit explicitly; otherwise the changes are lost
48 /// when the instance is destroyed.
49 ///
50 /// This is the only class from this header file that client code should use
51 /// directly.
52 class COMPHELPER_DLLPUBLIC ConfigurationChanges {
53 public:
54 static std::shared_ptr<ConfigurationChanges> create();
56 ~ConfigurationChanges();
58 void commit() const;
60 private:
61 ConfigurationChanges(const ConfigurationChanges&) = delete;
62 ConfigurationChanges& operator=(const ConfigurationChanges&) = delete;
64 SAL_DLLPRIVATE ConfigurationChanges(
65 css::uno::Reference< css::uno::XComponentContext >
66 const & context);
68 SAL_DLLPRIVATE void setPropertyValue(
69 OUString const & path, css::uno::Any const & value)
70 const;
72 SAL_DLLPRIVATE css::uno::Reference<
73 css::container::XHierarchicalNameReplace >
74 getGroup(OUString const & path) const;
76 SAL_DLLPRIVATE
77 css::uno::Reference< css::container::XNameContainer >
78 getSet(OUString const & path) const;
80 css::uno::Reference<
81 css::configuration::XReadWriteAccess > access_;
83 friend class detail::ConfigurationWrapper;
86 namespace detail {
88 class ConfigurationChangesListener;
90 /// @internal
91 class COMPHELPER_DLLPUBLIC ConfigurationWrapper {
92 friend class ConfigurationChangesListener;
93 public:
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);
111 css::uno::Reference<
112 css::container::XHierarchicalNameAccess >
113 getGroupReadOnly(OUString const & path) const;
115 static css::uno::Reference<
116 css::container::XHierarchicalNameReplace >
117 getGroupReadWrite(
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 >
125 getSetReadWrite(
126 std::shared_ptr< ConfigurationChanges > const & batch,
127 OUString const & path);
129 std::shared_ptr< ConfigurationChanges > createChanges() const;
131 private:
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;
148 bool mbDisposed;
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;
154 /// @internal
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 >(); }
162 private:
163 Convert(const Convert&) = delete;
164 Convert& operator=(const Convert&) = delete;
166 Convert() = delete;
167 ~Convert() = delete;
170 /// @internal
171 template< typename T > struct Convert< std::optional< T > >
173 static css::uno::Any toAny(std::optional< T > const & value) {
174 return value
175 ? css::uno::Any(*value)
176 : css::uno::Any();
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 >();
185 private:
186 Convert(const Convert&) = delete;
187 Convert& operator=(const Convert&) = delete;
189 Convert() = delete;
190 ~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
203 /// property.
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'>.
212 static U get()
214 // Folding this into one statement causes a bogus error at least with
215 // Red Hat GCC 4.6.2-1:
216 css::uno::Any a(
217 detail::ConfigurationWrapper::get().getPropertyValue(
218 T::path()));
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'>.
226 static void set(
227 U const & value,
228 std::shared_ptr< ConfigurationChanges > const & batch)
230 comphelper::detail::ConfigurationWrapper::setPropertyValue(
231 batch, T::path(), detail::Convert< U >::toAny(value));
234 private:
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'>.
254 static U get()
256 // Folding this into one statement causes a bogus error at least with
257 // Red Hat GCC 4.6.2-1:
258 css::uno::Any a(
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
267 /// batch.
269 /// For nillable properties, U is of type std::optional<U'>.
270 static void set(
271 U const & value,
272 std::shared_ptr< ConfigurationChanges > const & batch)
274 comphelper::detail::ConfigurationWrapper::setLocalizedPropertyValue(
275 batch, T::path(), detail::Convert< U >::toAny(value));
278 private:
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 >
295 get()
297 return detail::ConfigurationWrapper::get().getGroupReadOnly(
298 T::path());
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(
308 batch, T::path());
311 private:
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.
326 static
327 css::uno::Reference< css::container::XNameAccess >
328 get()
330 return detail::ConfigurationWrapper::get().getSetReadOnly(
331 T::path());
334 /// Get read/write access to the given configuration set, storing any
335 /// modifications via the given changes batch.
336 static
337 css::uno::Reference< css::container::XNameContainer >
338 get(std::shared_ptr< ConfigurationChanges > const & batch)
340 return comphelper::detail::ConfigurationWrapper::getSetReadWrite(
341 batch, T::path());
344 private:
345 ConfigurationSet(const ConfigurationSet&) = delete;
346 ConfigurationSet& operator=(const ConfigurationSet&) = delete;
348 ConfigurationSet() = delete;
349 ~ConfigurationSet() = delete;
354 #endif
356 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */