bump product version to 4.1.6.2
[LibreOffice.git] / forms / source / component / EventThread.cxx
blob4ee8700defe64772049e849034109ba30df4864f
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 "EventThread.hxx"
21 #include <comphelper/guarding.hxx>
22 #include <tools/debug.hxx>
24 //.........................................................................
25 namespace frm
27 //.........................................................................
28 using namespace ::com::sun::star::uno;
29 using namespace ::com::sun::star::awt;
30 using namespace ::com::sun::star::lang;
32 DBG_NAME( OComponentEventThread )
33 OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
34 m_pCompImpl( pCompImpl )
36 DBG_CTOR( OComponentEventThread, NULL );
38 increment(m_refCount);
40 // Hold a reference of the Control
42 InterfaceRef xIFace(static_cast<XWeak*>(pCompImpl));
43 query_interface(xIFace, m_xComp);
46 // and add us at the Control
48 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
49 m_xComp->addEventListener( xEvtLstnr );
52 decrement(m_refCount);
55 OComponentEventThread::~OComponentEventThread()
57 DBG_DTOR( OComponentEventThread, NULL );
59 DBG_ASSERT( m_aEvents.empty(),
60 "OComponentEventThread::~OComponentEventThread: Didn't call dispose?" );
62 impl_clearEventQueue();
65 Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType) throw (RuntimeException)
67 Any aReturn;
69 aReturn = OWeakObject::queryInterface(_rType);
71 if (!aReturn.hasValue())
72 aReturn = ::cppu::queryInterface(_rType,
73 static_cast<XEventListener*>(this)
76 return aReturn;
79 void OComponentEventThread::impl_clearEventQueue()
81 while ( m_aEvents.size() )
83 delete *m_aEvents.begin();
84 m_aEvents.erase( m_aEvents.begin() );
86 m_aControls.erase( m_aControls.begin(), m_aControls.end() );
87 m_aFlags.erase( m_aFlags.begin(), m_aFlags.end() );
90 void OComponentEventThread::disposing( const EventObject& evt ) throw ( ::com::sun::star::uno::RuntimeException)
92 if( evt.Source == m_xComp )
94 ::osl::MutexGuard aGuard( m_aMutex );
96 // Remove EventListener
97 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
98 m_xComp->removeEventListener( xEvtLstnr );
100 // Clear EventQueue
101 impl_clearEventQueue();
103 // Free the Control and set pCompImpl to 0,
104 // so that the thread knows, that it should terminate.
105 m_xComp = 0;
106 m_pCompImpl = 0;
108 // Wake up the thread and terminate
109 m_aCond.set();
110 terminate();
114 void OComponentEventThread::addEvent( const EventObject* _pEvt, sal_Bool bFlag )
116 Reference<XControl> xTmp;
117 addEvent( _pEvt, xTmp, bFlag );
120 void OComponentEventThread::addEvent( const EventObject* _pEvt,
121 const Reference<XControl>& rControl,
122 sal_Bool bFlag )
124 ::osl::MutexGuard aGuard( m_aMutex );
126 // Put data into the queue
127 m_aEvents.push_back( cloneEvent( _pEvt ) );
129 Reference<XWeak> xWeakControl(rControl, UNO_QUERY);
130 Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
131 m_aControls.push_back( xControlAdapter );
133 m_aFlags.push_back( bFlag );
135 // Wake up thread
136 m_aCond.set();
139 void OComponentEventThread::implStarted( )
141 acquire( );
144 void OComponentEventThread::implTerminated( )
146 release( );
149 void SAL_CALL OComponentEventThread::kill()
151 OComponentEventThread_TBASE::terminate();
152 OComponentEventThread_TBASE::join();
154 implTerminated( );
157 void SAL_CALL OComponentEventThread::onTerminated()
159 OComponentEventThread_TBASE::onTerminated();
161 implTerminated( );
164 void OComponentEventThread::run()
166 implStarted( );
168 // Hold on to ourselves, so that we're not deleted if a dispose is called at some point in time
169 InterfaceRef xThis(static_cast<XWeak*>(this));
173 ::osl::MutexGuard aGuard(m_aMutex);
175 while( m_aEvents.size() > 0 )
177 // Get the Control and hold on to it so that it cannot be deleted during actionPerformed
178 Reference<XComponent> xComp = m_xComp;
179 ::cppu::OComponentHelper *pCompImpl = m_pCompImpl;
181 ThreadEvents::iterator firstEvent( m_aEvents.begin() );
182 EventObject* pEvt = *firstEvent;
183 m_aEvents.erase( firstEvent );
185 ThreadObjects::iterator firstControl( m_aControls.begin() );
186 Reference<XAdapter> xControlAdapter = *firstControl;
187 m_aControls.erase( firstControl );
189 ThreadBools::iterator firstFlag( m_aFlags.begin() );
190 sal_Bool bFlag = *firstFlag;
191 m_aFlags.erase( firstFlag );
194 MutexRelease aReleaseOnce(m_aMutex);
195 // Because a queryHardRef can throw an Exception, it shoudln't be called when
196 // the mutex is locked.
197 Reference<XControl> xControl;
198 if ( xControlAdapter.is() )
199 query_interface(xControlAdapter->queryAdapted(), xControl);
201 if( xComp.is() )
202 processEvent( pCompImpl, pEvt, xControl, bFlag );
205 delete pEvt;
208 // After a Dispose, we do not know the Control anymore.
209 // Thus, we must not wait either.
210 if( !m_xComp.is() )
211 return;
213 // Reset waiting condition
214 m_aCond.reset();
216 MutexRelease aReleaseOnce(m_aMutex);
217 // And wait ... if, in the meantime, an Event came in after all
218 m_aCond.wait();
221 while( true );
224 //.........................................................................
225 } // namespace frm
226 //.........................................................................
228 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */