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 :
33 : //= OPropertyChangeListener
34 :
35 :
36 1274 : OPropertyChangeListener::~OPropertyChangeListener()
37 : {
38 1274 : if (m_pAdapter)
39 38 : m_pAdapter->dispose();
40 1274 : }
41 :
42 :
43 716 : void OPropertyChangeListener::_disposing(const EventObject&)
44 : throw (RuntimeException, std::exception)
45 : {
46 : // nothing to do here
47 716 : }
48 :
49 :
50 0 : void OPropertyChangeListener::disposeAdapter()
51 : {
52 0 : if ( m_pAdapter )
53 0 : m_pAdapter->dispose();
54 :
55 : // will automatically set a new adapter
56 : OSL_ENSURE( !m_pAdapter, "OPropertyChangeListener::disposeAdapter: what did dispose do?" );
57 0 : }
58 :
59 :
60 2416 : void OPropertyChangeListener::setAdapter(OPropertyChangeMultiplexer* pAdapter)
61 : {
62 2416 : if (m_pAdapter)
63 : {
64 1166 : ::osl::MutexGuard aGuard(m_rMutex);
65 1166 : m_pAdapter->release();
66 1166 : m_pAdapter = NULL;
67 : }
68 :
69 2416 : if (pAdapter)
70 : {
71 1250 : ::osl::MutexGuard aGuard(m_rMutex);
72 1250 : m_pAdapter = pAdapter;
73 1250 : m_pAdapter->acquire();
74 : }
75 2416 : }
76 :
77 :
78 : //= OPropertyChangeMultiplexer
79 :
80 :
81 1250 : OPropertyChangeMultiplexer::OPropertyChangeMultiplexer(OPropertyChangeListener* _pListener, const Reference< XPropertySet>& _rxSet, bool _bAutoReleaseSet)
82 : :m_xSet(_rxSet)
83 : ,m_pListener(_pListener)
84 : ,m_nLockCount(0)
85 : ,m_bListening(false)
86 1250 : ,m_bAutoSetRelease(_bAutoReleaseSet)
87 : {
88 1250 : m_pListener->setAdapter(this);
89 1250 : }
90 :
91 :
92 2324 : OPropertyChangeMultiplexer::~OPropertyChangeMultiplexer()
93 : {
94 2324 : }
95 :
96 :
97 40 : void OPropertyChangeMultiplexer::lock()
98 : {
99 40 : ++m_nLockCount;
100 40 : }
101 :
102 :
103 40 : void OPropertyChangeMultiplexer::unlock()
104 : {
105 40 : --m_nLockCount;
106 40 : }
107 :
108 :
109 2030 : void OPropertyChangeMultiplexer::dispose()
110 : {
111 2030 : if (m_bListening)
112 : {
113 450 : Reference< XPropertyChangeListener> xPreventDelete(this);
114 :
115 450 : const OUString* pProperties = m_aProperties.getConstArray();
116 1468 : for (sal_Int32 i = 0; i < m_aProperties.getLength(); ++i, ++pProperties)
117 1018 : m_xSet->removePropertyChangeListener(*pProperties, static_cast< XPropertyChangeListener*>(this));
118 :
119 450 : m_pListener->setAdapter(NULL);
120 :
121 450 : m_pListener = NULL;
122 450 : m_bListening = false;
123 :
124 450 : if (m_bAutoSetRelease)
125 126 : m_xSet = NULL;
126 : }
127 2030 : }
128 :
129 : // XEventListener
130 :
131 836 : void SAL_CALL OPropertyChangeMultiplexer::disposing( const EventObject& _rSource) throw( RuntimeException, std::exception)
132 : {
133 836 : if (m_pListener)
134 : {
135 : // tell the listener
136 716 : if (!locked())
137 716 : m_pListener->_disposing(_rSource);
138 : // disconnect the listener
139 716 : if (m_pListener) // may have been reset whilest calling into _disposing
140 716 : m_pListener->setAdapter(NULL);
141 : }
142 :
143 836 : m_pListener = NULL;
144 836 : m_bListening = false;
145 :
146 836 : if (m_bAutoSetRelease)
147 174 : m_xSet = NULL;
148 836 : }
149 :
150 : // XPropertyChangeListener
151 :
152 1251 : void SAL_CALL OPropertyChangeMultiplexer::propertyChange( const PropertyChangeEvent& _rEvent ) throw( RuntimeException, std::exception)
153 : {
154 1251 : if (m_pListener && !locked())
155 1205 : m_pListener->_propertyChanged(_rEvent);
156 1251 : }
157 :
158 :
159 1910 : void OPropertyChangeMultiplexer::addProperty(const OUString& _sPropertyName)
160 : {
161 1910 : if (m_xSet.is())
162 : {
163 1910 : m_xSet->addPropertyChangeListener(_sPropertyName, static_cast< XPropertyChangeListener*>(this));
164 1910 : m_aProperties.realloc(m_aProperties.getLength() + 1);
165 1910 : m_aProperties.getArray()[m_aProperties.getLength()-1] = _sPropertyName;
166 1910 : m_bListening = true;
167 : }
168 1910 : }
169 :
170 :
171 : }
172 :
173 :
174 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|