Line data Source code
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 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <comphelper/propmultiplex.hxx>
21 : #include <osl/diagnose.h>
22 :
23 :
24 : namespace comphelper
25 : {
26 :
27 :
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::com::sun::star::lang;
30 : using namespace ::com::sun::star::beans;
31 :
32 626 : OPropertyChangeListener::~OPropertyChangeListener()
33 : {
34 626 : if (m_pAdapter)
35 21 : m_pAdapter->dispose();
36 626 : }
37 :
38 :
39 383 : void OPropertyChangeListener::_disposing(const EventObject&)
40 : throw (RuntimeException, std::exception)
41 : {
42 : // nothing to do here
43 383 : }
44 :
45 :
46 1 : void OPropertyChangeListener::disposeAdapter()
47 : {
48 1 : if ( m_pAdapter )
49 0 : m_pAdapter->dispose();
50 :
51 : // will automatically set a new adapter
52 : OSL_ENSURE( !m_pAdapter, "OPropertyChangeListener::disposeAdapter: what did dispose do?" );
53 1 : }
54 :
55 :
56 1302 : void OPropertyChangeListener::setAdapter(OPropertyChangeMultiplexer* pAdapter)
57 : {
58 1302 : if (m_pAdapter)
59 : {
60 629 : ::osl::MutexGuard aGuard(m_rMutex);
61 629 : m_pAdapter->release();
62 629 : m_pAdapter = NULL;
63 : }
64 :
65 1302 : if (pAdapter)
66 : {
67 673 : ::osl::MutexGuard aGuard(m_rMutex);
68 673 : m_pAdapter = pAdapter;
69 673 : m_pAdapter->acquire();
70 : }
71 1302 : }
72 :
73 673 : 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 673 : ,m_bAutoSetRelease(_bAutoReleaseSet)
79 : {
80 673 : m_pListener->setAdapter(this);
81 673 : }
82 :
83 :
84 1254 : OPropertyChangeMultiplexer::~OPropertyChangeMultiplexer()
85 : {
86 1254 : }
87 :
88 :
89 27 : void OPropertyChangeMultiplexer::lock()
90 : {
91 27 : ++m_nLockCount;
92 27 : }
93 :
94 :
95 27 : void OPropertyChangeMultiplexer::unlock()
96 : {
97 27 : --m_nLockCount;
98 27 : }
99 :
100 :
101 1103 : void OPropertyChangeMultiplexer::dispose()
102 : {
103 1103 : if (m_bListening)
104 : {
105 246 : Reference< XPropertyChangeListener> xPreventDelete(this);
106 :
107 246 : const OUString* pProperties = m_aProperties.getConstArray();
108 797 : for (sal_Int32 i = 0; i < m_aProperties.getLength(); ++i, ++pProperties)
109 551 : m_xSet->removePropertyChangeListener(*pProperties, static_cast< XPropertyChangeListener*>(this));
110 :
111 246 : m_pListener->setAdapter(NULL);
112 :
113 246 : m_pListener = NULL;
114 246 : m_bListening = false;
115 :
116 246 : if (m_bAutoSetRelease)
117 63 : m_xSet = NULL;
118 : }
119 1103 : }
120 :
121 : // XEventListener
122 :
123 454 : void SAL_CALL OPropertyChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException, std::exception)
124 : {
125 454 : if (m_pListener)
126 : {
127 : // tell the listener
128 383 : if (!locked())
129 383 : m_pListener->_disposing(_rSource);
130 : // disconnect the listener
131 383 : if (m_pListener) // may have been reset whilest calling into _disposing
132 383 : m_pListener->setAdapter(NULL);
133 : }
134 :
135 454 : m_pListener = NULL;
136 454 : m_bListening = false;
137 :
138 454 : if (m_bAutoSetRelease)
139 92 : m_xSet = NULL;
140 454 : }
141 :
142 : // XPropertyChangeListener
143 :
144 669 : void SAL_CALL OPropertyChangeMultiplexer::propertyChange( const PropertyChangeEvent& _rEvent ) throw( RuntimeException, std::exception)
145 : {
146 669 : if (m_pListener && !locked())
147 632 : m_pListener->_propertyChanged(_rEvent);
148 669 : }
149 :
150 :
151 1033 : void OPropertyChangeMultiplexer::addProperty(const OUString& _sPropertyName)
152 : {
153 1033 : if (m_xSet.is())
154 : {
155 1033 : m_xSet->addPropertyChangeListener(_sPropertyName, static_cast< XPropertyChangeListener*>(this));
156 1033 : m_aProperties.realloc(m_aProperties.getLength() + 1);
157 1033 : m_aProperties.getArray()[m_aProperties.getLength()-1] = _sPropertyName;
158 1033 : m_bListening = true;
159 : }
160 1033 : }
161 :
162 :
163 : }
164 :
165 :
166 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|