bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / property / propmultiplex.cxx
blob51613c7b45f17e87b6c5423b48446f8c39920ca7
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <comphelper/propmultiplex.hxx>
21 #include <osl/diagnose.h>
24 namespace comphelper
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::lang;
30 using namespace ::com::sun::star::beans;
32 OPropertyChangeListener::~OPropertyChangeListener()
34 if (m_pAdapter)
35 m_pAdapter->dispose();
39 void OPropertyChangeListener::_disposing(const EventObject&)
40 throw (RuntimeException, std::exception)
42 // nothing to do here
46 void OPropertyChangeListener::disposeAdapter()
48 if ( m_pAdapter )
49 m_pAdapter->dispose();
51 // will automatically set a new adapter
52 OSL_ENSURE( !m_pAdapter, "OPropertyChangeListener::disposeAdapter: what did dispose do?" );
56 void OPropertyChangeListener::setAdapter(OPropertyChangeMultiplexer* pAdapter)
58 if (m_pAdapter)
60 ::osl::MutexGuard aGuard(m_rMutex);
61 m_pAdapter->release();
62 m_pAdapter = NULL;
65 if (pAdapter)
67 ::osl::MutexGuard aGuard(m_rMutex);
68 m_pAdapter = pAdapter;
69 m_pAdapter->acquire();
73 OPropertyChangeMultiplexer::OPropertyChangeMultiplexer(OPropertyChangeListener* _pListener, const Reference< XPropertySet>& _rxSet, bool _bAutoReleaseSet)
74 :m_xSet(_rxSet)
75 ,m_pListener(_pListener)
76 ,m_nLockCount(0)
77 ,m_bListening(false)
78 ,m_bAutoSetRelease(_bAutoReleaseSet)
80 m_pListener->setAdapter(this);
84 OPropertyChangeMultiplexer::~OPropertyChangeMultiplexer()
89 void OPropertyChangeMultiplexer::lock()
91 ++m_nLockCount;
95 void OPropertyChangeMultiplexer::unlock()
97 --m_nLockCount;
101 void OPropertyChangeMultiplexer::dispose()
103 if (m_bListening)
105 Reference< XPropertyChangeListener> xPreventDelete(this);
107 const OUString* pProperties = m_aProperties.getConstArray();
108 for (sal_Int32 i = 0; i < m_aProperties.getLength(); ++i, ++pProperties)
109 m_xSet->removePropertyChangeListener(*pProperties, static_cast< XPropertyChangeListener*>(this));
111 m_pListener->setAdapter(NULL);
113 m_pListener = NULL;
114 m_bListening = false;
116 if (m_bAutoSetRelease)
117 m_xSet = NULL;
121 // XEventListener
123 void SAL_CALL OPropertyChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException, std::exception)
125 if (m_pListener)
127 // tell the listener
128 if (!locked())
129 m_pListener->_disposing(_rSource);
130 // disconnect the listener
131 if (m_pListener) // may have been reset whilest calling into _disposing
132 m_pListener->setAdapter(NULL);
135 m_pListener = NULL;
136 m_bListening = false;
138 if (m_bAutoSetRelease)
139 m_xSet = NULL;
142 // XPropertyChangeListener
144 void SAL_CALL OPropertyChangeMultiplexer::propertyChange( const PropertyChangeEvent& _rEvent ) throw( RuntimeException, std::exception)
146 if (m_pListener && !locked())
147 m_pListener->_propertyChanged(_rEvent);
151 void OPropertyChangeMultiplexer::addProperty(const OUString& _sPropertyName)
153 if (m_xSet.is())
155 m_xSet->addPropertyChangeListener(_sPropertyName, static_cast< XPropertyChangeListener*>(this));
156 m_aProperties.realloc(m_aProperties.getLength() + 1);
157 m_aProperties.getArray()[m_aProperties.getLength()-1] = _sPropertyName;
158 m_bListening = true;
166 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */