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 <sal/config.h>
21 :
22 : #include <vector>
23 :
24 : #include <unotools/eventlisteneradapter.hxx>
25 : #include <osl/diagnose.h>
26 : #include <cppuhelper/implbase1.hxx>
27 :
28 : namespace utl
29 : {
30 :
31 : using namespace ::com::sun::star::uno;
32 : using namespace ::com::sun::star::lang;
33 :
34 : //= OEventListenerImpl
35 :
36 35184 : 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 1 : const Reference< XComponent >& getComponent() const { return m_xComponent; }
50 :
51 : protected:
52 : virtual void SAL_CALL disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception) SAL_OVERRIDE;
53 : };
54 :
55 19636 : OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
56 19636 : :m_pAdapter(_pAdapter)
57 : {
58 : OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
59 : // no checks of _rxComp !!
60 : // (OEventListenerAdapter is responsible for this)
61 :
62 : // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
63 : // is done
64 19636 : Reference< XEventListener > xMeMyselfAndI = this;
65 19636 : _rxComp->addEventListener(xMeMyselfAndI);
66 :
67 19636 : m_xComponent = _rxComp;
68 19636 : m_xKeepMeAlive = xMeMyselfAndI;
69 19636 : }
70 :
71 17592 : void OEventListenerImpl::dispose()
72 : {
73 17592 : if (m_xComponent.is())
74 : {
75 11746 : m_xComponent->removeEventListener(m_xKeepMeAlive);
76 11746 : m_xComponent.clear();
77 11746 : m_xKeepMeAlive.clear();
78 : }
79 17592 : }
80 :
81 7852 : void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException, std::exception)
82 : {
83 7852 : Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
84 7852 : m_xKeepMeAlive.clear();
85 7852 : m_xComponent.clear();
86 :
87 7852 : m_pAdapter->_disposing(_rSource);
88 7852 : }
89 :
90 : //= OEventListenerAdapterImpl
91 :
92 36990 : struct OEventListenerAdapterImpl
93 : {
94 : public:
95 : ::std::vector< void* > aListeners;
96 : };
97 :
98 : //= OEventListenerAdapter
99 :
100 18692 : OEventListenerAdapter::OEventListenerAdapter()
101 18692 : :m_pImpl(new OEventListenerAdapterImpl)
102 : {
103 18692 : }
104 :
105 18298 : OEventListenerAdapter::~OEventListenerAdapter()
106 : {
107 18298 : stopAllComponentListening( );
108 18298 : delete m_pImpl;
109 18298 : m_pImpl = NULL;
110 18298 : }
111 :
112 11 : void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
113 : {
114 11 : if ( m_pImpl->aListeners.empty() )
115 21 : return;
116 :
117 1 : ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
118 2 : do
119 : {
120 1 : OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
121 1 : if ( pListenerImpl->getComponent().get() == _rxComp.get() )
122 : {
123 0 : pListenerImpl->dispose();
124 0 : pListenerImpl->release();
125 0 : dispose = m_pImpl->aListeners.erase( dispose );
126 : }
127 : else
128 1 : ++dispose;
129 : }
130 2 : while ( dispose != m_pImpl->aListeners.end() );
131 : }
132 :
133 25077 : void OEventListenerAdapter::stopAllComponentListening( )
134 : {
135 128007 : for ( ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
136 85338 : aDisposeLoop != m_pImpl->aListeners.end();
137 : ++aDisposeLoop
138 : )
139 : {
140 17592 : OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
141 17592 : pListenerImpl->dispose();
142 17592 : pListenerImpl->release();
143 : }
144 25077 : m_pImpl->aListeners.clear();
145 25077 : }
146 :
147 19636 : void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
148 : {
149 19636 : if (!_rxComp.is())
150 : {
151 : OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
152 19636 : return;
153 : }
154 :
155 19636 : OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
156 19636 : pListenerImpl->acquire();
157 19636 : m_pImpl->aListeners.push_back(pListenerImpl);
158 : }
159 :
160 : } // namespace utl
161 :
162 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|