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
16 #include <comphelper/comphelperdllapi.h>
17 #include <com/sun/star/lang/XComponent.hpp>
18 #include <com/sun/star/beans/XPropertySet.hpp>
19 #include <com/sun/star/beans/PropertyChangeEvent.hpp>
20 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
21 #include <rtl/ref.hxx>
22 #include <cppuhelper/implbase.hxx>
23 #include <comphelper/processfactory.hxx>
24 #include <comphelper/configurationhelper.hxx>
26 namespace comphelper
{
28 class ConfigurationListener
;
30 class COMPHELPER_DLLPUBLIC ConfigurationListenerPropertyBase
{
33 rtl::Reference
<ConfigurationListener
> mxListener
;
35 virtual ~ConfigurationListenerPropertyBase() {}
36 virtual void setProperty(const css::uno::Any
&aProperty
) = 0;
37 void dispose() { mxListener
.clear(); }
40 /// Access to this class must be protected by the SolarMutex
41 template< typename uno_type
> class ConfigurationListenerProperty
: public ConfigurationListenerPropertyBase
45 virtual void setProperty(const css::uno::Any
&aProperty
) override
47 aProperty
>>= maValue
;
51 * Provide a mirror of the configmgr's version of this property
52 * for the lifecycle of this property. The property value tracks
53 * the same value in the configuration.
55 inline ConfigurationListenerProperty(const rtl::Reference
< ConfigurationListener
> &xListener
,
56 const OUString
&rProp
);
58 virtual inline ~ConfigurationListenerProperty() override
;
60 uno_type
get() const { return maValue
; }
63 class COMPHELPER_DLLPUBLIC ConfigurationListener
:
64 public cppu::WeakImplHelper
< css::beans::XPropertyChangeListener
>
66 css::uno::Reference
< css::beans::XPropertySet
> mxConfig
;
67 std::vector
< ConfigurationListenerPropertyBase
* > maListeners
;
69 /// Public health warning, you -must- dispose this if you use it.
70 ConfigurationListener(const OUString
&rPath
,
71 css::uno::Reference
< css::uno::XComponentContext
>
72 const & xContext
= comphelper::getProcessComponentContext())
73 : mxConfig( ConfigurationHelper::openConfig( xContext
, rPath
, EConfigurationModes::ReadOnly
),
74 css::uno::UNO_QUERY_THROW
)
77 virtual ~ConfigurationListener() override
82 /// Listen for the specific property denoted by the listener
83 void addListener(ConfigurationListenerPropertyBase
*pListener
);
86 void removeListener(ConfigurationListenerPropertyBase
*pListener
);
88 /// Release various circular references
91 // XPropertyChangeListener implementation
92 virtual void SAL_CALL
disposing(css::lang::EventObject
const &) override
;
94 /// Notify of the property change
95 virtual void SAL_CALL
propertyChange(
96 css::beans::PropertyChangeEvent
const &rEvt
) override
;
99 template< typename uno_type
> ConfigurationListenerProperty
< uno_type
>::ConfigurationListenerProperty(const rtl::Reference
< ConfigurationListener
> &xListener
, const OUString
&rProp
)
103 mxListener
= xListener
;
104 mxListener
->addListener(this);
107 template< typename uno_type
> ConfigurationListenerProperty
< uno_type
>::~ConfigurationListenerProperty()
110 mxListener
->removeListener(this);
113 } // namespace comphelper
115 #endif // INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
117 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */