From ccf8bdcf929e842ef42ae968e4f0532282357277 Mon Sep 17 00:00:00 2001 From: Michael Meeks Date: Mon, 5 Oct 2015 18:08:02 +0100 Subject: Create a wrapper to make listening for configmgr changes easy. Change-Id: Ib58d04f9e046e604b24e0e338796a7a60aa1d6fd Reviewed-on: https://gerrit.libreoffice.org/19253 Tested-by: Jenkins Reviewed-by: Michael Meeks --- comphelper/source/misc/configuration.cxx | 51 ++++++++++++++++++++++++++++++++ 1 file changed, 51 insertions(+) (limited to 'comphelper') diff --git a/comphelper/source/misc/configuration.cxx b/comphelper/source/misc/configuration.cxx index 6280fdc84ac2..e5bf8b84d2d2 100644 --- a/comphelper/source/misc/configuration.cxx +++ b/comphelper/source/misc/configuration.cxx @@ -27,6 +27,7 @@ #include #include #include +#include #include #include #include @@ -209,4 +210,54 @@ comphelper::detail::ConfigurationWrapper::createChanges() const { new ConfigurationChanges(context_)); } +void comphelper::ConfigurationListener::addListener(ConfigurationListenerPropertyBase *pListener) +{ + maListeners.push_back( pListener ); + mxConfig->addPropertyChangeListener( pListener->maName, this ); + pListener->setProperty( mxConfig->getPropertyValue( pListener->maName ) ); +} + +void comphelper::ConfigurationListener::removeListener(ConfigurationListenerPropertyBase *pListener) +{ + auto it = maListeners.begin(); + it = std::find( maListeners.begin(), maListeners.end(), pListener ); + if ( it != maListeners.end() ) + { + maListeners.erase( it ); + mxConfig->removePropertyChangeListener( pListener->maName, this ); + } +} + +void comphelper::ConfigurationListener::dispose() +{ + for (auto it = maListeners.begin(); it != maListeners.end(); ++it) + { + mxConfig->removePropertyChangeListener( (*it)->maName, this ); + (*it)->dispose(); + } + maListeners.clear(); +} + +void SAL_CALL comphelper::ConfigurationListener::disposing(css::lang::EventObject const &) + throw (css::uno::RuntimeException, std::exception) +{ + dispose(); +} + +void SAL_CALL comphelper::ConfigurationListener::propertyChange( + css::beans::PropertyChangeEvent const &rEvt ) + throw (css::uno::RuntimeException, std::exception) +{ + assert( rEvt.Source == mxConfig ); + for ( auto it = maListeners.begin(); it != maListeners.end(); ++it ) + { + if ( (*it)->maName == rEvt.PropertyName ) + { + // ignore rEvt.NewValue - in theory it could be stale => not set. + css::uno::Any aValue = mxConfig->getPropertyValue( (*it)->maName ); + (*it)->setProperty( aValue ); + } + } +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ -- cgit