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 "EventThread.hxx"
21 : #include <comphelper/guarding.hxx>
22 : #include <tools/debug.hxx>
23 : #include <cppuhelper/queryinterface.hxx>
24 :
25 : #include <boost/scoped_ptr.hpp>
26 :
27 : namespace frm
28 : {
29 :
30 : using namespace ::com::sun::star::uno;
31 : using namespace ::com::sun::star::awt;
32 : using namespace ::com::sun::star::lang;
33 :
34 0 : OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
35 0 : m_pCompImpl( pCompImpl )
36 : {
37 :
38 0 : osl_atomic_increment(&m_refCount);
39 :
40 : // Hold a reference of the Control
41 : {
42 0 : InterfaceRef xIFace(static_cast<XWeak*>(pCompImpl));
43 0 : m_xComp.set(xIFace, css::uno::UNO_QUERY);
44 : }
45 :
46 : // and add us at the Control
47 : {
48 0 : Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
49 0 : m_xComp->addEventListener( xEvtLstnr );
50 : }
51 :
52 0 : osl_atomic_decrement(&m_refCount);
53 0 : }
54 :
55 0 : OComponentEventThread::~OComponentEventThread()
56 : {
57 :
58 : DBG_ASSERT( m_aEvents.empty(),
59 : "OComponentEventThread::~OComponentEventThread: Didn't call dispose?" );
60 :
61 0 : impl_clearEventQueue();
62 0 : }
63 :
64 0 : Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType) throw (RuntimeException, std::exception)
65 : {
66 0 : Any aReturn;
67 :
68 0 : aReturn = OWeakObject::queryInterface(_rType);
69 :
70 0 : if (!aReturn.hasValue())
71 0 : aReturn = ::cppu::queryInterface(_rType,
72 : static_cast<XEventListener*>(this)
73 0 : );
74 :
75 0 : return aReturn;
76 : }
77 :
78 0 : void OComponentEventThread::impl_clearEventQueue()
79 : {
80 0 : while ( m_aEvents.size() )
81 : {
82 0 : delete *m_aEvents.begin();
83 0 : m_aEvents.erase( m_aEvents.begin() );
84 : }
85 0 : m_aControls.erase( m_aControls.begin(), m_aControls.end() );
86 0 : m_aFlags.erase( m_aFlags.begin(), m_aFlags.end() );
87 0 : }
88 :
89 0 : void OComponentEventThread::disposing( const EventObject& evt ) throw ( ::com::sun::star::uno::RuntimeException, std::exception)
90 : {
91 0 : if( evt.Source == m_xComp )
92 : {
93 0 : ::osl::MutexGuard aGuard( m_aMutex );
94 :
95 : // Remove EventListener
96 0 : Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
97 0 : m_xComp->removeEventListener( xEvtLstnr );
98 :
99 : // Clear EventQueue
100 0 : impl_clearEventQueue();
101 :
102 : // Free the Control and set pCompImpl to 0,
103 : // so that the thread knows, that it should terminate.
104 0 : m_xComp = 0;
105 0 : m_pCompImpl = 0;
106 :
107 : // Wake up the thread and terminate
108 0 : m_aCond.set();
109 0 : terminate();
110 : }
111 0 : }
112 :
113 0 : void OComponentEventThread::addEvent( const EventObject* _pEvt, bool bFlag )
114 : {
115 0 : Reference<XControl> xTmp;
116 0 : addEvent( _pEvt, xTmp, bFlag );
117 0 : }
118 :
119 0 : void OComponentEventThread::addEvent( const EventObject* _pEvt,
120 : const Reference<XControl>& rControl,
121 : bool bFlag )
122 : {
123 0 : ::osl::MutexGuard aGuard( m_aMutex );
124 :
125 : // Put data into the queue
126 0 : m_aEvents.push_back( cloneEvent( _pEvt ) );
127 :
128 0 : Reference<XWeak> xWeakControl(rControl, UNO_QUERY);
129 0 : Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
130 0 : m_aControls.push_back( xControlAdapter );
131 :
132 0 : m_aFlags.push_back( bFlag );
133 :
134 : // Wake up thread
135 0 : m_aCond.set();
136 0 : }
137 :
138 0 : void OComponentEventThread::implStarted( )
139 : {
140 0 : acquire( );
141 0 : }
142 :
143 0 : void OComponentEventThread::implTerminated( )
144 : {
145 0 : release( );
146 0 : }
147 :
148 0 : void SAL_CALL OComponentEventThread::onTerminated()
149 : {
150 0 : OComponentEventThread_TBASE::onTerminated();
151 :
152 0 : implTerminated( );
153 0 : }
154 :
155 0 : void OComponentEventThread::run()
156 : {
157 0 : osl_setThreadName("frm::OComponentEventThread");
158 :
159 0 : implStarted( );
160 :
161 : // Hold on to ourselves, so that we're not deleted if a dispose is called at some point in time
162 0 : InterfaceRef xThis(static_cast<XWeak*>(this));
163 :
164 : do
165 : {
166 0 : ::osl::MutexGuard aGuard(m_aMutex);
167 :
168 0 : while( m_aEvents.size() > 0 )
169 : {
170 : // Get the Control and hold on to it so that it cannot be deleted during actionPerformed
171 0 : Reference<XComponent> xComp = m_xComp;
172 0 : ::cppu::OComponentHelper *pCompImpl = m_pCompImpl;
173 :
174 0 : ThreadEvents::iterator firstEvent( m_aEvents.begin() );
175 0 : boost::scoped_ptr<EventObject> pEvt(*firstEvent);
176 0 : m_aEvents.erase( firstEvent );
177 :
178 0 : ThreadObjects::iterator firstControl( m_aControls.begin() );
179 0 : Reference<XAdapter> xControlAdapter = *firstControl;
180 0 : m_aControls.erase( firstControl );
181 :
182 0 : ThreadBools::iterator firstFlag( m_aFlags.begin() );
183 0 : bool bFlag = *firstFlag;
184 0 : m_aFlags.erase( firstFlag );
185 :
186 : {
187 0 : MutexRelease aReleaseOnce(m_aMutex);
188 : // Because a queryHardRef can throw an Exception, it should not be called when
189 : // the mutex is locked.
190 0 : Reference<XControl> xControl;
191 0 : if ( xControlAdapter.is() )
192 : xControl.set(
193 0 : xControlAdapter->queryAdapted(), css::uno::UNO_QUERY);
194 :
195 0 : if( xComp.is() )
196 0 : processEvent( pCompImpl, pEvt.get(), xControl, bFlag );
197 : }
198 0 : }
199 :
200 : // After a Dispose, we do not know the Control anymore.
201 : // Thus, we must not wait either.
202 0 : if( !m_xComp.is() )
203 0 : return;
204 :
205 : // Reset waiting condition
206 0 : m_aCond.reset();
207 : {
208 0 : MutexRelease aReleaseOnce(m_aMutex);
209 : // And wait ... if, in the meantime, an Event came in after all
210 0 : m_aCond.wait();
211 0 : }
212 : }
213 0 : while( true );
214 : }
215 :
216 :
217 : } // namespace frm
218 :
219 :
220 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|