update credits
[LibreOffice.git] / unotools / source / misc / eventlisteneradapter.cxx
blobffd38778d7c81208e9e399d021f9ffce9abde209
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <unotools/eventlisteneradapter.hxx>
21 #include <osl/diagnose.h>
22 #include <cppuhelper/implbase1.hxx>
23 #include <comphelper/stl_types.hxx>
25 //.........................................................................
26 namespace utl
28 //.........................................................................
30 using namespace ::com::sun::star::uno;
31 using namespace ::com::sun::star::lang;
33 //=====================================================================
34 //= OEventListenerImpl
35 //=====================================================================
36 class OEventListenerImpl : public ::cppu::WeakImplHelper1< XEventListener >
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;
45 public:
46 OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp );
48 void dispose();
49 const Reference< XComponent >& getComponent() const { return m_xComponent; }
51 protected:
52 virtual void SAL_CALL disposing( const EventObject& _rSource ) throw (RuntimeException);
55 //---------------------------------------------------------------------
56 OEventListenerImpl::OEventListenerImpl( OEventListenerAdapter* _pAdapter, const Reference< XComponent >& _rxComp )
57 :m_pAdapter(_pAdapter)
59 OSL_ENSURE(m_pAdapter, "OEventListenerImpl::OEventListenerImpl: invalid adapter!");
60 // no checks of _rxComp !!
61 // (OEventListenerAdapter is responsible for this)
63 // just in case addEventListener throws an exception ... don't initialize m_xKeepMeAlive before this
64 // is done
65 Reference< XEventListener > xMeMyselfAndI = this;
66 _rxComp->addEventListener(xMeMyselfAndI);
68 m_xComponent = _rxComp;
69 m_xKeepMeAlive = xMeMyselfAndI;
72 //---------------------------------------------------------------------
73 void OEventListenerImpl::dispose()
75 if (m_xComponent.is())
77 m_xComponent->removeEventListener(m_xKeepMeAlive);
78 m_xComponent.clear();
79 m_xKeepMeAlive.clear();
83 //---------------------------------------------------------------------
84 void SAL_CALL OEventListenerImpl::disposing( const EventObject& _rSource ) throw (RuntimeException)
86 Reference< XEventListener > xDeleteUponLeaving = m_xKeepMeAlive;
87 m_xKeepMeAlive.clear();
88 m_xComponent.clear();
90 m_pAdapter->_disposing(_rSource);
93 //=====================================================================
94 //= OEventListenerAdapterImpl
95 //=====================================================================
96 struct OEventListenerAdapterImpl
98 public:
99 ::std::vector< void* > aListeners;
102 //=====================================================================
103 //= OEventListenerAdapter
104 //=====================================================================
105 //---------------------------------------------------------------------
106 OEventListenerAdapter::OEventListenerAdapter()
107 :m_pImpl(new OEventListenerAdapterImpl)
111 //---------------------------------------------------------------------
112 OEventListenerAdapter::~OEventListenerAdapter()
114 stopAllComponentListening( );
115 delete m_pImpl;
116 m_pImpl = NULL;
119 //---------------------------------------------------------------------
120 void OEventListenerAdapter::stopComponentListening( const ::com::sun::star::uno::Reference< ::com::sun::star::lang::XComponent >& _rxComp )
122 if ( m_pImpl->aListeners.empty() )
123 return;
125 ::std::vector< void* >::iterator dispose = m_pImpl->aListeners.begin();
128 OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >( *dispose );
129 if ( pListenerImpl->getComponent().get() == _rxComp.get() )
131 pListenerImpl->dispose();
132 pListenerImpl->release();
133 dispose = m_pImpl->aListeners.erase( dispose );
135 else
136 ++dispose;
138 while ( dispose != m_pImpl->aListeners.end() );
141 //---------------------------------------------------------------------
142 void OEventListenerAdapter::stopAllComponentListening( )
144 for ( ::std::vector< void* >::const_iterator aDisposeLoop = m_pImpl->aListeners.begin();
145 aDisposeLoop != m_pImpl->aListeners.end();
146 ++aDisposeLoop
149 OEventListenerImpl* pListenerImpl = static_cast< OEventListenerImpl* >(*aDisposeLoop);
150 pListenerImpl->dispose();
151 pListenerImpl->release();
153 m_pImpl->aListeners.clear();
156 //---------------------------------------------------------------------
157 void OEventListenerAdapter::startComponentListening( const Reference< XComponent >& _rxComp )
159 if (!_rxComp.is())
161 OSL_FAIL("OEventListenerAdapter::startComponentListening: invalid component!");
162 return;
165 OEventListenerImpl* pListenerImpl = new OEventListenerImpl(this, _rxComp);
166 pListenerImpl->acquire();
167 m_pImpl->aListeners.push_back(pListenerImpl);
170 //.........................................................................
171 } // namespace utl
172 //.........................................................................
174 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */