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 { namespace sun { namespace star { namespace beans { struct PropertyChangeEvent; } } } }
23 namespace com { namespace sun { namespace star { namespace 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 
63  public cppu::WeakImplHelper< css::beans::XPropertyChangeListener >
64 {
65  css::uno::Reference< css::beans::XPropertySet > mxConfig;
66  std::vector< ConfigurationListenerPropertyBase * > maListeners;
67 public:
69  ConfigurationListener(const OUString &rPath,
70  css::uno::Reference< css::uno::XComponentContext >
71  const & xContext = comphelper::getProcessComponentContext())
72  : mxConfig( ConfigurationHelper::openConfig( xContext, rPath, EConfigurationModes::ReadOnly ),
73  css::uno::UNO_QUERY_THROW )
74  { }
75 
76  virtual ~ConfigurationListener() override
77  {
78  dispose();
79  }
80 
83 
86 
88  void dispose();
89 
90  // XPropertyChangeListener implementation
91  virtual void SAL_CALL disposing(css::lang::EventObject const &) override;
92 
94  virtual void SAL_CALL propertyChange(
95  css::beans::PropertyChangeEvent const &rEvt ) override;
96 };
97 
99  : maValue()
100 {
101  maName = rProp;
102  mxListener = xListener;
103  mxListener->addListener(this);
104 }
105 
107 {
108  if (mxListener.is())
109  mxListener->removeListener(this);
110 }
111 
112 } // namespace comphelper
113 
114 #endif // INCLUDED_COMPHELPER_CONFIGURATIONLISTENER_HXX
115 
116 /* 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.
double const maValue
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()
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