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 1918 : 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 0 : 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 2047 : OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
57 2047 : :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 2047 : Reference< XEventListener > xMeMyselfAndI = this;
66 2047 : _rxComp->addEventListener(xMeMyselfAndI);
67 :
68 2047 : m_xComponent = _rxComp;
69 2047 : m_xKeepMeAlive = xMeMyselfAndI;
70 2047 : }
71 :
72 : //---------------------------------------------------------------------
73 959 : void OEventListenerImpl::dispose()
74 : {
75 959 : if (m_xComponent.is())
76 : {
77 320 : m_xComponent->removeEventListener(m_xKeepMeAlive);
78 320 : m_xComponent.clear();
79 320 : m_xKeepMeAlive.clear();
80 : }
81 959 : }
82 :
83 : //---------------------------------------------------------------------
84 909 : void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException)
85 : {
86 909 : Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
87 909 : m_xKeepMeAlive.clear();
88 909 : m_xComponent.clear();
89 :
90 909 : m_pAdapter->_disposing(_rSource);
91 909 : }
92 :
93 : //=====================================================================
94 : //= OEventListenerAdapterImpl
95 : //=====================================================================
96 3762 : struct OEventListenerAdapterImpl
97 : {
98 : public:
99 : ::std::vector< void* > aListeners;
100 : };
101 :
102 : //=====================================================================
103 : //= OEventListenerAdapter
104 : //=====================================================================
105 : //---------------------------------------------------------------------
106 2259 : OEventListenerAdapter::OEventListenerAdapter()
107 2259 : :m_pImpl(new OEventListenerAdapterImpl)
108 : {
109 2259 : }
110 :
111 : //---------------------------------------------------------------------
112 1503 : OEventListenerAdapter::~OEventListenerAdapter()
113 : {
114 1503 : stopAllComponentListening( );
115 1503 : delete m_pImpl;
116 1503 : m_pImpl = NULL;
117 1503 : }
118 :
119 : //---------------------------------------------------------------------
120 0 : void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
121 : {
122 0 : if ( m_pImpl->aListeners.empty() )
123 0 : return;
124 :
125 0 : ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
126 0 : do
127 : {
128 0 : OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
129 0 : 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 0 : ++dispose;
137 : }
138 0 : while ( dispose != m_pImpl->aListeners.end() );
139 : }
140 :
141 : //---------------------------------------------------------------------
142 2762 : void OEventListenerAdapter::stopAllComponentListening( )
143 : {
144 11163 : for ( ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
145 7442 : aDisposeLoop != m_pImpl->aListeners.end();
146 : ++aDisposeLoop
147 : )
148 : {
149 959 : OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
150 959 : pListenerImpl->dispose();
151 959 : pListenerImpl->release();
152 : }
153 2762 : m_pImpl->aListeners.clear();
154 2762 : }
155 :
156 : //---------------------------------------------------------------------
157 2047 : void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
158 : {
159 2047 : if (!_rxComp.is())
160 : {
161 : OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
162 2047 : return;
163 : }
164 :
165 2047 : OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
166 2047 : pListenerImpl->acquire();
167 2047 : m_pImpl->aListeners.push_back(pListenerImpl);
168 : }
169 :
170 : //.........................................................................
171 : } // namespace utl
172 : //.........................................................................
173 :
174 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|