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