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 <unotools/eventlisteneradapter.hxx>
21 : : #include <osl/diagnose.h>
22 : : #include <cppuhelper/implbase1.hxx>
23 : : #include <comphelper/stl_types.hxx>
24 : :
25 : : //.........................................................................
26 : : namespace utl
27 : : {
28 : : //.........................................................................
29 : :
30 : : using namespace ::com::sun::star::uno;
31 : : using namespace ::com::sun::star::lang;
32 : :
33 : : //=====================================================================
34 : : //= OEventListenerImpl
35 : : //=====================================================================
36 [ - + ]: 15338 : class OEventListenerImpl : public ::cppu::WeakImplHelper1< XEventListener >
37 : : {
38 : : protected:
39 : : OEventListenerAdapter* m_pAdapter;
40 : : Reference< XEventListener > m_xKeepMeAlive;
41 : : // imagine an implementation of XComponent which holds it's listeners with a weak reference ...
42 : : // would be very bad if we don't hold ourself
43 : : Reference< XComponent > m_xComponent;
44 : :
45 : : public:
46 : : OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp );
47 : :
48 : : void dispose();
49 : 2 : const Reference< XComponent >& getComponent() const { return m_xComponent; }
50 : :
51 : : protected:
52 : : virtual void SAL_CALL disposing( const EventObject& _rSource ) throw (RuntimeException);
53 : : };
54 : :
55 : : //---------------------------------------------------------------------
56 : 7764 : OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
57 : 7764 : :m_pAdapter(_pAdapter)
58 : : {
59 : : OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
60 : : // no checks of _rxComp !!
61 : : // (OEventListenerAdapter is responsible for this)
62 : :
63 : : // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
64 : : // is done
65 [ + - ]: 7764 : Reference< XEventListener > xMeMyselfAndI = this;
66 [ + - ][ + - ]: 7764 : _rxComp->addEventListener(xMeMyselfAndI);
67 : :
68 [ + - ]: 7764 : m_xComponent = _rxComp;
69 [ + - ]: 7764 : m_xKeepMeAlive = xMeMyselfAndI;
70 : 7764 : }
71 : :
72 : : //---------------------------------------------------------------------
73 : 7669 : void OEventListenerImpl::dispose()
74 : : {
75 [ + + ]: 7669 : if (m_xComponent.is())
76 : : {
77 : 6333 : m_xComponent->removeEventListener(m_xKeepMeAlive);
78 : 6333 : m_xComponent.clear();
79 : 6333 : m_xKeepMeAlive.clear();
80 : : }
81 : 7669 : }
82 : :
83 : : //---------------------------------------------------------------------
84 : 1398 : void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException)
85 : : {
86 : 1398 : Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
87 : 1398 : m_xKeepMeAlive.clear();
88 : 1398 : m_xComponent.clear();
89 : :
90 [ + - ]: 1398 : m_pAdapter->_disposing(_rSource);
91 : 1398 : }
92 : :
93 : : //=====================================================================
94 : : //= OEventListenerAdapterImpl
95 : : //=====================================================================
96 : 16797 : struct OEventListenerAdapterImpl
97 : : {
98 : : public:
99 : : ::std::vector< void* > aListeners;
100 : : };
101 : :
102 : : //=====================================================================
103 : : //= OEventListenerAdapter
104 : : //=====================================================================
105 : : //---------------------------------------------------------------------
106 : 8590 : OEventListenerAdapter::OEventListenerAdapter()
107 [ + - ]: 8590 : :m_pImpl(new OEventListenerAdapterImpl)
108 : : {
109 : 8590 : }
110 : :
111 : : //---------------------------------------------------------------------
112 : 8207 : OEventListenerAdapter::~OEventListenerAdapter()
113 : : {
114 : 8207 : stopAllComponentListening( );
115 [ + - ]: 8207 : delete m_pImpl;
116 : 8207 : m_pImpl = NULL;
117 [ - + ]: 8207 : }
118 : :
119 : : //---------------------------------------------------------------------
120 : 2 : void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
121 : : {
122 [ + - ]: 2 : if ( m_pImpl->aListeners.empty() )
123 : 2 : return;
124 : :
125 : 2 : ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
126 [ - + ][ + - ]: 2 : do
127 : : {
128 [ + - ]: 2 : OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
129 [ + - ][ + - ]: 2 : if ( pListenerImpl->getComponent().get() == _rxComp.get() )
[ - + ]
130 : : {
131 [ # # ]: 0 : pListenerImpl->dispose();
132 : 0 : pListenerImpl->release();
133 [ # # ]: 0 : dispose = m_pImpl->aListeners.erase( dispose );
134 : : }
135 : : else
136 [ + - ]: 2 : ++dispose;
137 : : }
138 : 2 : while ( dispose != m_pImpl->aListeners.end() );
139 : : }
140 : :
141 : : //---------------------------------------------------------------------
142 : 9820 : void OEventListenerAdapter::stopAllComponentListening( )
143 : : {
144 [ + - ]: 34978 : for ( ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
[ + - + - ]
[ + + ]
145 : 17489 : aDisposeLoop != m_pImpl->aListeners.end();
146 : : ++aDisposeLoop
147 : : )
148 : : {
149 [ + - ]: 7669 : OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
150 [ + - ]: 7669 : pListenerImpl->dispose();
151 : 7669 : pListenerImpl->release();
152 : : }
153 : 9820 : m_pImpl->aListeners.clear();
154 : 9820 : }
155 : :
156 : : //---------------------------------------------------------------------
157 : 7764 : void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
158 : : {
159 [ - + ]: 7764 : if (!_rxComp.is())
160 : : {
161 : : OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
162 : 7764 : return;
163 : : }
164 : :
165 [ + - ]: 7764 : OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
166 : 7764 : pListenerImpl->acquire();
167 [ + - ]: 7764 : m_pImpl->aListeners.push_back(pListenerImpl);
168 : : }
169 : :
170 : : //.........................................................................
171 : : } // namespace utl
172 : : //.........................................................................
173 : :
174 : : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|