LibreOffice Module comphelper (master)  1
configurationlistener.hxx
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 #ifndef INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
11 #define INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
12 
13 #include <vector>
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>
21 
22 namespace com::sun::star::beans { struct PropertyChangeEvent; }
23 namespace com::sun::star::uno { class XComponentContext; }
24 
25 namespace comphelper {
26 
27 class ConfigurationListener;
28 
30 public:
31  OUString maName;
33 
35  virtual void setProperty(const css::uno::Any &aProperty) = 0;
36  void dispose() { mxListener.clear(); }
37 };
38 
40 template< typename uno_type > class ConfigurationListenerProperty : public ConfigurationListenerPropertyBase
41 {
42  uno_type maValue;
43 protected:
44  virtual void setProperty(const css::uno::Any &aProperty) override
45  {
46  aProperty >>= maValue;
47  }
48 public:
55  const OUString &rProp );
56 
57  virtual inline ~ConfigurationListenerProperty() override;
58 
59  uno_type get() const { return maValue; }
60 };
61 
62 // workaround for incremental linking bugs in MSVC2019
63 class SAL_DLLPUBLIC_TEMPLATE ConfigurationListener_Base : public cppu::WeakImplHelper< css::beans::XPropertyChangeListener > {};
65 {
66  css::uno::Reference< css::beans::XPropertySet > mxConfig;
67  std::vector< ConfigurationListenerPropertyBase * > maListeners;
68  bool mbDisposed;
69 public:
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 )
76  , mbDisposed(false)
77  { }
78 
79  virtual ~ConfigurationListener() override
80  {
81  dispose();
82  }
83 
86 
89 
91  void dispose();
92 
93  // XPropertyChangeListener implementation
94  virtual void SAL_CALL disposing(css::lang::EventObject const &) override;
95 
97  virtual void SAL_CALL propertyChange(
98  css::beans::PropertyChangeEvent const &rEvt ) override;
99 
100  bool isDisposed() const { return mbDisposed; }
101 };
102 
104  : maValue()
105 {
106  maName = rProp;
107  mxListener = xListener;
108  mxListener->addListener(this);
109 }
110 
112 {
113  if (mxListener.is())
114  mxListener->removeListener(this);
115 }
116 
117 } // namespace comphelper
118 
119 #endif // INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
120 
121 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
ConfigurationListener(const OUString &rPath, css::uno::Reference< css::uno::XComponentContext > const &xContext=comphelper::getProcessComponentContext())
Public health warning, you -must- dispose this if you use it.
std::vector< ConfigurationListenerPropertyBase * > maListeners
rtl::Reference< ConfigurationListener > mxListener
void addListener(const InterfaceRef &xObject, const css::uno::Reference< css::lang::XEventListener > &xListener)
void removeListener(const InterfaceRef &xObject, const css::uno::Reference< css::lang::XEventListener > &xListener)
configuration will be opened readonly
EConfigurationModes
specify all possible modes, which can be used to open a configuration access.
Reference< script::XScriptListener > mxListener
Reference< XComponentContext > getProcessComponentContext()
This function gets the process service factory's default component context.
virtual void setProperty(const css::uno::Any &aProperty) override
Access to this class must be protected by the SolarMutex.
#define COMPHELPER_DLLPUBLIC
void dispose()
double maValue
ConfigurationListenerProperty(const rtl::Reference< ConfigurationListener > &xListener, const OUString &rProp)
Provide a mirror of the configmgr's version of this property for the lifecycle of this property...
css::uno::Reference< css::beans::XPropertySet > mxConfig