1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svtools.hxx"
30 
31 #include <svtools/asynclink.hxx>
32 #include <vos/mutex.hxx>
33 #include <tools/debug.hxx>
34 #include <vcl/timer.hxx>
35 #include <vcl/svapp.hxx>
36 
37 //--------------------------------------------------------------------
38 namespace svtools {
39 
40 void AsynchronLink::CreateMutex()
41 {
42     if( !_pMutex ) _pMutex = new vos::OMutex;
43 }
44 
45 void AsynchronLink::Call( void* pObj, sal_Bool
46 #ifdef DBG_UTIL
47 bAllowDoubles
48 #endif
49 , sal_Bool bUseTimer )
50 {
51 #ifdef DBG_UTIL
52 	if ( bUseTimer || !_bInCall )
53 		DBG_WARNING( "Recursives Call. Eher ueber Timer. TLX Fragen" );
54 #endif
55 	if( _aLink.IsSet() )
56 	{
57 		_pArg = pObj;
58 		DBG_ASSERT( bAllowDoubles ||
59 					( !_nEventId && ( !_pTimer || !_pTimer->IsActive() ) ),
60 					"Schon ein Call unterwegs" );
61 		if( _nEventId )
62 		{
63 			if( _pMutex ) _pMutex->acquire();
64 			Application::RemoveUserEvent( _nEventId );
65 			if( _pMutex ) _pMutex->release();
66 		}
67 		if( _pTimer )_pTimer->Stop();
68 		if( bUseTimer )
69 		{
70 			if( !_pTimer )
71 			{
72 				_pTimer = new Timer;
73 				_pTimer->SetTimeout( 0 );
74 				_pTimer->SetTimeoutHdl( STATIC_LINK(
75 					this, AsynchronLink, HandleCall) );
76 			}
77 			_pTimer->Start();
78 		}
79 		else
80 		{
81 			if( _pMutex ) _pMutex->acquire();
82             Application::PostUserEvent( _nEventId, STATIC_LINK( this, AsynchronLink, HandleCall), 0 );
83 			if( _pMutex ) _pMutex->release();
84 		}
85 	}
86 }
87 
88 AsynchronLink::~AsynchronLink()
89 {
90 	if( _nEventId )
91 	{
92 		Application::RemoveUserEvent( _nEventId );
93 	}
94 	delete _pTimer;
95 	if( _pDeleted ) *_pDeleted = sal_True;
96 	delete _pMutex;
97 }
98 
99 IMPL_STATIC_LINK( AsynchronLink, HandleCall, void*, EMPTYARG )
100 {
101 	if( pThis->_pMutex ) pThis->_pMutex->acquire();
102 	pThis->_nEventId = 0;
103 	if( pThis->_pMutex ) pThis->_pMutex->release();
104 	pThis->Call_Impl( pThis->_pArg );
105 	return 0;
106 }
107 
108 void AsynchronLink::ForcePendingCall()
109 {
110 	ClearPendingCall();
111 	Call_Impl( _pArg );
112 }
113 
114 void AsynchronLink::ClearPendingCall()
115 {
116     if( _pMutex ) _pMutex->acquire();
117 	if( _nEventId )
118 	{
119 		Application::RemoveUserEvent( _nEventId );
120 		_nEventId = 0;
121 	}
122     if( _pMutex ) _pMutex->release();
123 	if( _pTimer ) _pTimer->Stop();
124 }
125 
126 void AsynchronLink::Call_Impl( void* pArg )
127 {
128 	_bInCall = sal_True;
129 	sal_Bool bDeleted = sal_False;
130 	_pDeleted = &bDeleted;
131 	_aLink.Call( pArg );
132 	if( !bDeleted )
133 	{
134 		_bInCall = sal_False;
135 		_pDeleted = 0;
136 	}
137 }
138 
139 }
140