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_CONFIGURATIONLISTENER_HXX
11 #define INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
14 #include <comphelper/comphelperdllapi.h>
15 #include <com/sun/star/beans/XPropertySet.hpp>
16 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
17 #include <rtl/ref.hxx>
18 #include <cppuhelper/implbase.hxx>
19 #include <comphelper/processfactory.hxx>
20 #include <comphelper/configurationhelper.hxx>
22 namespace com::sun::star::beans
{ struct PropertyChangeEvent
; }
23 namespace com::sun::star::uno
{ class XComponentContext
; }
25 namespace comphelper
{
27 class ConfigurationListener
;
29 class COMPHELPER_DLLPUBLIC ConfigurationListenerPropertyBase
{
32 rtl::Reference
<ConfigurationListener
> mxListener
;
34 virtual ~ConfigurationListenerPropertyBase() {}
35 virtual void setProperty(const css::uno::Any
&aProperty
) = 0;
36 void dispose() { mxListener
.clear(); }
39 /// Access to this class must be protected by the SolarMutex
40 template< typename uno_type
> class ConfigurationListenerProperty
: public ConfigurationListenerPropertyBase
44 virtual void setProperty(const css::uno::Any
&aProperty
) override
46 aProperty
>>= maValue
;
50 * Provide a mirror of the configmgr's version of this property
51 * for the lifecycle of this property. The property value tracks
52 * the same value in the configuration.
54 inline ConfigurationListenerProperty(const rtl::Reference
< ConfigurationListener
> &xListener
,
55 const OUString
&rProp
);
57 virtual inline ~ConfigurationListenerProperty() override
;
59 uno_type
get() const { return maValue
; }
62 // workaround for incremental linking bugs in MSVC2019
63 class SAL_DLLPUBLIC_TEMPLATE ConfigurationListener_Base
: public cppu::WeakImplHelper
< css::beans::XPropertyChangeListener
> {};
64 class COMPHELPER_DLLPUBLIC ConfigurationListener final
: public ConfigurationListener_Base
66 css::uno::Reference
< css::beans::XPropertySet
> mxConfig
;
67 std::vector
< ConfigurationListenerPropertyBase
* > maListeners
;
70 /// Public health warning, you -must- dispose this if you use it.
71 ConfigurationListener(const OUString
&rPath
,
72 css::uno::Reference
< css::uno::XComponentContext
>
73 const & xContext
= comphelper::getProcessComponentContext())
74 : mxConfig( ConfigurationHelper::openConfig( xContext
, rPath
, EConfigurationModes::ReadOnly
),
75 css::uno::UNO_QUERY_THROW
)
79 virtual ~ConfigurationListener() override
84 /// Listen for the specific property denoted by the listener
85 void addListener(ConfigurationListenerPropertyBase
*pListener
);
88 void removeListener(ConfigurationListenerPropertyBase
*pListener
);
90 /// Release various circular references
93 // XPropertyChangeListener implementation
94 virtual void SAL_CALL
disposing(css::lang::EventObject
const &) override
;
96 /// Notify of the property change
97 virtual void SAL_CALL
propertyChange(
98 css::beans::PropertyChangeEvent
const &rEvt
) override
;
100 bool isDisposed() const { return mbDisposed
; }
103 template< typename uno_type
> ConfigurationListenerProperty
< uno_type
>::ConfigurationListenerProperty(const rtl::Reference
< ConfigurationListener
> &xListener
, const OUString
&rProp
)
107 mxListener
= xListener
;
108 mxListener
->addListener(this);
111 template< typename uno_type
> ConfigurationListenerProperty
< uno_type
>::~ConfigurationListenerProperty()
114 mxListener
->removeListener(this);
117 } // namespace comphelper
119 #endif // INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */