1 /**************************************************************
2 *
3 * Licensed to the Apache Software Foundation (ASF) under one
4 * or more contributor license agreements. See the NOTICE file
5 * distributed with this work for additional information
6 * regarding copyright ownership. The ASF licenses this file
7 * to you under the Apache License, Version 2.0 (the
8 * "License"); you may not use this file except in compliance
9 * with the License. You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing,
14 * software distributed under the License is distributed on an
15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16 * KIND, either express or implied. See the License for the
17 * specific language governing permissions and limitations
18 * under the License.
19 *
20 *************************************************************/
21
22
23
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 #include "EventThread.hxx"
27 #include <comphelper/guarding.hxx>
28 #include <tools/debug.hxx>
29
30 //.........................................................................
31 namespace frm
32 {
33 //.........................................................................
34 using namespace ::com::sun::star::uno;
35 using namespace ::com::sun::star::awt;
36 using namespace ::com::sun::star::lang;
37
DBG_NAME(OComponentEventThread)38 DBG_NAME( OComponentEventThread )
39 OComponentEventThread::OComponentEventThread( ::cppu::OComponentHelper* pCompImpl ) :
40 m_pCompImpl( pCompImpl )
41 {
42 DBG_CTOR( OComponentEventThread, NULL );
43
44 increment(m_refCount);
45
46 // Eine Referenz des Controls halten
47 {
48 InterfaceRef xIFace(static_cast<XWeak*>(pCompImpl));
49 query_interface(xIFace, m_xComp);
50 }
51
52 // und uns an dem Control anmelden
53 {
54 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
55 m_xComp->addEventListener( xEvtLstnr );
56 }
57
58 decrement(m_refCount);
59 }
60
~OComponentEventThread()61 OComponentEventThread::~OComponentEventThread()
62 {
63 DBG_DTOR( OComponentEventThread, NULL );
64
65 DBG_ASSERT( m_aEvents.size() == 0,
66 "OComponentEventThread::~OComponentEventThread: Kein dispose gerufen?" );
67
68 impl_clearEventQueue();
69 }
70
queryInterface(const Type & _rType)71 Any SAL_CALL OComponentEventThread::queryInterface(const Type& _rType) throw (RuntimeException)
72 {
73 Any aReturn;
74
75 aReturn = OWeakObject::queryInterface(_rType);
76
77 if (!aReturn.hasValue())
78 aReturn = ::cppu::queryInterface(_rType,
79 static_cast<XEventListener*>(this)
80 );
81
82 return aReturn;
83 }
84
impl_clearEventQueue()85 void OComponentEventThread::impl_clearEventQueue()
86 {
87 while ( m_aEvents.size() )
88 {
89 delete *m_aEvents.begin();
90 m_aEvents.erase( m_aEvents.begin() );
91 }
92 m_aControls.erase( m_aControls.begin(), m_aControls.end() );
93 m_aFlags.erase( m_aFlags.begin(), m_aFlags.end() );
94 }
95
disposing(const EventObject & evt)96 void OComponentEventThread::disposing( const EventObject& evt ) throw ( ::com::sun::star::uno::RuntimeException)
97 {
98 if( evt.Source == m_xComp )
99 {
100 ::osl::MutexGuard aGuard( m_aMutex );
101
102 // Event-Listener abmelden
103 Reference<XEventListener> xEvtLstnr = static_cast<XEventListener*>(this);
104 m_xComp->removeEventListener( xEvtLstnr );
105
106 // Event-Queue loeschen
107 impl_clearEventQueue();
108
109 // Das Control loslassen und pCompImpl auf 0 setzen, damit der
110 // Thread weiss, dass er sich beenden soll.
111 m_xComp = 0;
112 m_pCompImpl = 0;
113
114 // Den Thread aufwecken und beenden.
115 m_aCond.set();
116 terminate();
117 }
118 }
119
addEvent(const EventObject * _pEvt,sal_Bool bFlag)120 void OComponentEventThread::addEvent( const EventObject* _pEvt, sal_Bool bFlag )
121 {
122 Reference<XControl> xTmp;
123 addEvent( _pEvt, xTmp, bFlag );
124 }
125
addEvent(const EventObject * _pEvt,const Reference<XControl> & rControl,sal_Bool bFlag)126 void OComponentEventThread::addEvent( const EventObject* _pEvt,
127 const Reference<XControl>& rControl,
128 sal_Bool bFlag )
129 {
130 ::osl::MutexGuard aGuard( m_aMutex );
131
132 // Daten in die Queue stellen
133 m_aEvents.push_back( cloneEvent( _pEvt ) );
134
135 Reference<XWeak> xWeakControl(rControl, UNO_QUERY);
136 Reference<XAdapter> xControlAdapter = xWeakControl.is() ? xWeakControl->queryAdapter() : Reference<XAdapter>();
137 m_aControls.push_back( xControlAdapter );
138
139 m_aFlags.push_back( bFlag );
140
141 // Thread aufwecken
142 m_aCond.set();
143 }
144
145 //---------------------------------------------------------------------
146 //--- 22.08.01 15:48:15 -----------------------------------------------
147
implStarted()148 void OComponentEventThread::implStarted( )
149 {
150 acquire( );
151 }
152
153 //---------------------------------------------------------------------
154 //--- 22.08.01 15:48:16 -----------------------------------------------
155
implTerminated()156 void OComponentEventThread::implTerminated( )
157 {
158 release( );
159 }
160
161 //---------------------------------------------------------------------
162 //--- 22.08.01 15:47:31 -----------------------------------------------
163
kill()164 void SAL_CALL OComponentEventThread::kill()
165 {
166 OComponentEventThread_TBASE::kill();
167
168 implTerminated( );
169 }
170
171 //---------------------------------------------------------------------
172 //--- 22.08.01 15:47:33 -----------------------------------------------
173
onTerminated()174 void SAL_CALL OComponentEventThread::onTerminated()
175 {
176 OComponentEventThread_TBASE::onTerminated();
177
178 implTerminated( );
179 }
180
run()181 void OComponentEventThread::run()
182 {
183 implStarted( );
184
185 // uns selbst festhalten, damit wir nicht geloescht werden,
186 // wenn zwischendrinne mal ein dispose gerufen wird.
187 InterfaceRef xThis(static_cast<XWeak*>(this));
188
189 do
190 {
191 ::osl::MutexGuard aGuard(m_aMutex);
192
193 while( m_aEvents.size() > 0 )
194 {
195 // Das Control holen und festhalten, damit es waehrend des
196 // actionPerformed nicht geloescht werden kann.
197 Reference<XComponent> xComp = m_xComp;
198 ::cppu::OComponentHelper *pCompImpl = m_pCompImpl;
199
200 ThreadEvents::iterator firstEvent( m_aEvents.begin() );
201 EventObject* pEvt = *firstEvent;
202 m_aEvents.erase( firstEvent );
203
204 ThreadObjects::iterator firstControl( m_aControls.begin() );
205 Reference<XAdapter> xControlAdapter = *firstControl;
206 m_aControls.erase( firstControl );
207
208 ThreadBools::iterator firstFlag( m_aFlags.begin() );
209 sal_Bool bFlag = *firstFlag;
210 m_aFlags.erase( firstFlag );
211
212 {
213 MutexRelease aReleaseOnce(m_aMutex);
214 // Weil ein queryHardRef eine Exception schmeissen kann sollte
215 // es nicht bei gelocktem Mutex aufgerufen werden.
216 Reference<XControl> xControl;
217 if ( xControlAdapter.is() )
218 query_interface(xControlAdapter->queryAdapted(), xControl);
219
220 if( xComp.is() )
221 processEvent( pCompImpl, pEvt, xControl, bFlag );
222 }
223
224 delete pEvt;
225 };
226
227 // Nach einem dispose kennen wir das Control nicht mehr. Dann darf
228 // auch nicht gewartet werden.
229 if( !m_xComp.is() )
230 return;
231
232 // Warte-Bedingung zuruecksetzen
233 m_aCond.reset();
234 {
235 MutexRelease aReleaseOnce(m_aMutex);
236 // und warten ... falls nicht zwischenzeitlich doch noch ein
237 // Event eingetroffen ist.
238 m_aCond.wait();
239 }
240 }
241 while( sal_True );
242 }
243
244 //.........................................................................
245 } // namespace frm
246 //.........................................................................
247
248