LibreOffice Module comphelper (master) 1
configuration.cxx
Go to the documentation of this file.
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#include <sal/config.h>
11
12#include <cassert>
13
14#include <com/sun/star/beans/NamedValue.hpp>
15#include <com/sun/star/beans/PropertyAttribute.hpp>
16#include <com/sun/star/configuration/ReadOnlyAccess.hpp>
17#include <com/sun/star/configuration/ReadWriteAccess.hpp>
18#include <com/sun/star/configuration/XReadWriteAccess.hpp>
19#include <com/sun/star/configuration/theDefaultProvider.hpp>
20#include <com/sun/star/container/XHierarchicalNameAccess.hpp>
21#include <com/sun/star/container/XHierarchicalNameReplace.hpp>
22#include <com/sun/star/container/XNameAccess.hpp>
23#include <com/sun/star/container/XNameContainer.hpp>
24#include <com/sun/star/util/XChangesListener.hpp>
25#include <com/sun/star/util/XChangesNotifier.hpp>
26#include <com/sun/star/lang/DisposedException.hpp>
27#include <com/sun/star/lang/XLocalizable.hpp>
28#include <com/sun/star/uno/Any.hxx>
29#include <com/sun/star/uno/Reference.hxx>
34#include <rtl/ustring.hxx>
35#include <sal/log.hxx>
37
38namespace com::sun::star::uno { class XComponentContext; }
39
40namespace {
41
42OUString getDefaultLocale(
43 css::uno::Reference< css::uno::XComponentContext > const & context)
44{
45 return LanguageTag(
46 css::uno::Reference< css::lang::XLocalizable >(
47 css::configuration::theDefaultProvider::get(context),
48 css::uno::UNO_QUERY_THROW)->
49 getLocale()).getBcp47(false);
50}
51
52OUString extendLocalizedPath(std::u16string_view path, OUString const & locale) {
54 locale.match("*"), "comphelper",
55 "Locale \"" << locale << "\" starts with \"*\"");
56 assert(locale.indexOf('&') == -1);
57 assert(locale.indexOf('"') == -1);
58 assert(locale.indexOf('\'') == -1);
59 return OUString::Concat(path) + "/['*" + locale + "']";
60}
61
62}
63
64std::shared_ptr< comphelper::ConfigurationChanges >
66{
68}
69
71
73 access_->commitChanges();
74}
75
77 css::uno::Reference< css::uno::XComponentContext > const & context):
78 access_(
79 css::configuration::ReadWriteAccess::create(
80 context, getDefaultLocale(context)))
81{}
82
84 OUString const & path, css::uno::Any const & value) const
85{
86 access_->replaceByHierarchicalName(path, value);
87}
88
89css::uno::Reference< css::container::XHierarchicalNameReplace >
91{
92 return css::uno::Reference< css::container::XHierarchicalNameReplace >(
93 access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
94}
95
96css::uno::Reference< css::container::XNameContainer >
97comphelper::ConfigurationChanges::getSet(OUString const & path) const
98{
99 return css::uno::Reference< css::container::XNameContainer >(
100 access_->getByHierarchicalName(path), css::uno::UNO_QUERY_THROW);
101}
102
105{
107 return WRAPPER;
108}
109
111 : public ::cppu::WeakImplHelper<css::util::XChangesListener>
112{
114public:
116 : mrConfigurationWrapper(rWrapper)
117 {}
118 // util::XChangesListener
119 virtual void SAL_CALL changesOccurred( const css::util::ChangesEvent& ) override
120 {
121 std::scoped_lock aGuard(mrConfigurationWrapper.maMutex);
123 }
124 virtual void SAL_CALL disposing(const css::lang::EventObject&) override
125 {
126 std::scoped_lock aGuard(mrConfigurationWrapper.maMutex);
131 }
132};
133
136 access_(css::configuration::ReadWriteAccess::create(context_, "*")),
137 mbDisposed(false)
138{
139 // Set up a configuration notifier to invalidate the cache as needed.
140 try
141 {
142 css::uno::Reference< css::lang::XMultiServiceFactory > xConfigProvider(
143 css::configuration::theDefaultProvider::get( context_ ) );
144
145 // set root path
146 css::uno::Sequence< css::uno::Any > params {
147 css::uno::Any( css::beans::NamedValue{ "nodepath", css::uno::Any( OUString("/"))} ),
148 css::uno::Any( css::beans::NamedValue{ "locale", css::uno::Any( OUString("*"))} ) };
149
150 css::uno::Reference< css::uno::XInterface > xCfg
151 = xConfigProvider->createInstanceWithArguments(u"com.sun.star.configuration.ConfigurationAccess",
152 params);
153
154 maNotifier = css::uno::Reference< css::util::XChangesNotifier >(xCfg, css::uno::UNO_QUERY);
155 assert(maNotifier.is());
157 maNotifier->addChangesListener(maListener);
158 }
159 catch(const css::uno::Exception&)
160 {
161 assert(false);
162 }
163}
164
166{
167 maPropertyCache.clear();
168 maNotifier.clear();
169 maListener.clear();
170}
171
173 const
174{
175 return
176 (access_->getPropertyByHierarchicalName(path).Attributes
177 & css::beans::PropertyAttribute::READONLY)
178 != 0;
179}
180
181css::uno::Any comphelper::detail::ConfigurationWrapper::getPropertyValue(OUString const& path) const
182{
183 std::scoped_lock aGuard(maMutex);
184 if (mbDisposed)
185 throw css::lang::DisposedException();
186 // Cache the configuration access, since some of the keys are used in hot code.
187 auto it = maPropertyCache.find(path);
188 if( it != maPropertyCache.end())
189 return it->second;
190
191 sal_Int32 idx = path.lastIndexOf("/");
192 assert(idx!=-1);
193 OUString parentPath = path.copy(0, idx);
194 OUString childName = path.copy(idx+1);
195
196 css::uno::Reference<css::container::XNameAccess> access(
197 access_->getByHierarchicalName(parentPath), css::uno::UNO_QUERY_THROW);
198 css::uno::Any property = access->getByName(childName);
199 maPropertyCache.emplace(path, property);
200 return property;
201}
202
204 std::shared_ptr< ConfigurationChanges > const & batch,
205 OUString const & path, css::uno::Any const & value)
206{
207 assert(batch);
208 batch->setPropertyValue(path, value);
209}
210
211css::uno::Any
213 std::u16string_view path) const
214{
215 return access_->getByHierarchicalName(
216 extendLocalizedPath(path, getDefaultLocale(context_)));
217}
218
220 std::shared_ptr< ConfigurationChanges > const & batch,
221 OUString const & path, css::uno::Any const & value)
222{
223 assert(batch);
224 batch->setPropertyValue(path, value);
225}
226
227css::uno::Reference< css::container::XHierarchicalNameAccess >
229 OUString const & path) const
230{
231 return css::uno::Reference< css::container::XHierarchicalNameAccess >(
232 (css::configuration::ReadOnlyAccess::create(
233 context_, getDefaultLocale(context_))->
234 getByHierarchicalName(path)),
235 css::uno::UNO_QUERY_THROW);
236}
237
238css::uno::Reference< css::container::XHierarchicalNameReplace >
240 std::shared_ptr< ConfigurationChanges > const & batch,
241 OUString const & path)
242{
243 assert(batch);
244 return batch->getGroup(path);
245}
246
247css::uno::Reference< css::container::XNameAccess >
249 OUString const & path) const
250{
251 return css::uno::Reference< css::container::XNameAccess >(
252 (css::configuration::ReadOnlyAccess::create(
253 context_, getDefaultLocale(context_))->
254 getByHierarchicalName(path)),
255 css::uno::UNO_QUERY_THROW);
256}
257
258css::uno::Reference< css::container::XNameContainer >
260 std::shared_ptr< ConfigurationChanges > const & batch,
261 OUString const & path)
262{
263 assert(batch);
264 return batch->getSet(path);
265}
266
267std::shared_ptr< comphelper::ConfigurationChanges >
269 return std::shared_ptr< ConfigurationChanges >(
271}
272
274{
275 maListeners.push_back( pListener );
276 mxConfig->addPropertyChangeListener( pListener->maName, this );
277 pListener->setProperty( mxConfig->getPropertyValue( pListener->maName ) );
278}
279
281{
282 auto it = std::find( maListeners.begin(), maListeners.end(), pListener );
283 if ( it != maListeners.end() )
284 {
285 maListeners.erase( it );
286 mxConfig->removePropertyChangeListener( pListener->maName, this );
287 }
288}
289
291{
292 for (auto const& listener : maListeners)
293 {
294 mxConfig->removePropertyChangeListener( listener->maName, this );
295 listener->dispose();
296 }
297 maListeners.clear();
298 mbDisposed = true;
299}
300
301void SAL_CALL comphelper::ConfigurationListener::disposing(css::lang::EventObject const &)
302{
303 dispose();
304}
305
307 css::beans::PropertyChangeEvent const &rEvt )
308{
309 // Code is commonly used inside the SolarMutexGuard
310 // so to avoid concurrent writes to the property,
311 // and allow fast, lock-less access, guard here.
312 //
313 // Note that we are abusing rtl::Reference here to do acquire/release because,
314 // unlike osl::Guard, it is tolerant of null pointers, and on some code paths, the
315 // SolarMutex does not exist.
317
318 assert( rEvt.Source == mxConfig );
319 for (auto const& listener : maListeners)
320 {
321 if ( listener->maName == rEvt.PropertyName )
322 {
323 // ignore rEvt.NewValue - in theory it could be stale => not set.
324 css::uno::Any aValue = mxConfig->getPropertyValue( listener->maName );
325 listener->setProperty( aValue );
326 }
327 }
328}
329
330/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
const OUString & getBcp47(bool bResolveSystem=true) const
A batch of configuration changes that is committed as a whole.
SAL_DLLPRIVATE css::uno::Reference< css::container::XHierarchicalNameReplace > getGroup(OUString const &path) const
static std::shared_ptr< ConfigurationChanges > create()
SAL_DLLPRIVATE void setPropertyValue(OUString const &path, css::uno::Any const &value) const
ConfigurationChanges(const ConfigurationChanges &)=delete
SAL_DLLPRIVATE css::uno::Reference< css::container::XNameContainer > getSet(OUString const &path) const
virtual void setProperty(const css::uno::Any &aProperty)=0
void removeListener(ConfigurationListenerPropertyBase *pListener)
Stop listening.
virtual void SAL_CALL propertyChange(css::beans::PropertyChangeEvent const &rEvt) override
Notify of the property change.
void addListener(ConfigurationListenerPropertyBase *pListener)
Listen for the specific property denoted by the listener.
void dispose()
Release various circular references.
virtual void SAL_CALL disposing(css::lang::EventObject const &) override
static SolarMutex * get()
Help components to get the SolarMutex easily.
Definition: solarmutex.cxx:34
virtual void SAL_CALL disposing(const css::lang::EventObject &) override
ConfigurationChangesListener(comphelper::detail::ConfigurationWrapper &rWrapper)
virtual void SAL_CALL changesOccurred(const css::util::ChangesEvent &) override
comphelper::detail::ConfigurationWrapper & mrConfigurationWrapper
css::uno::Reference< css::container::XHierarchicalNameAccess > getGroupReadOnly(OUString const &path) const
css::uno::Any getLocalizedPropertyValue(std::u16string_view path) const
bool isReadOnly(OUString const &path) const
css::uno::Any getPropertyValue(OUString const &path) const
static void setPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
css::uno::Reference< css::util::XChangesListener > maListener
css::uno::Reference< css::util::XChangesNotifier > maNotifier
css::uno::Reference< css::uno::XComponentContext > context_
static void setLocalizedPropertyValue(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path, css::uno::Any const &value)
std::unordered_map< OUString, css::uno::Any > maPropertyCache
css::uno::Reference< css::container::XNameAccess > getSetReadOnly(OUString const &path) const
static css::uno::Reference< css::container::XNameContainer > getSetReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
static ConfigurationWrapper const & get()
static css::uno::Reference< css::container::XHierarchicalNameReplace > getGroupReadWrite(std::shared_ptr< ConfigurationChanges > const &batch, OUString const &path)
std::shared_ptr< ConfigurationChanges > createChanges() const
Any value
css::uno::Reference< css::uno::XInterface > access_
float u
const sal_uInt16 idx[]
#define SAL_WARN_IF(condition, area, stream)
std::mutex maMutex
const LanguageTag & getLocale()
Get the current LOK's locale.
Definition: lok.cxx:192
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
css::uno::Reference< css::deployment::XPackageRegistry > create(css::uno::Reference< css::deployment::XPackageRegistry > const &xRootRegistry, OUString const &context, OUString const &cachePath, css::uno::Reference< css::uno::XComponentContext > const &xComponentContext)
void dispose()
static PropertyMapEntry const * find(const rtl::Reference< PropertySetInfo > &mxInfo, const OUString &aName) noexcept
uno::Reference< uno::XComponentContext > context_