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