1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
|
/*************************************************************************
*
* OpenOffice.org - a multi-platform office productivity suite
*
* $RCSfile: session.cxx,v $
*
* $Revision: 1.10 $
*
* last change: $Author: rt $ $Date: 2007-07-24 10:04:29 $
*
* The Contents of this file are made available subject to
* the terms of GNU Lesser General Public License Version 2.1.
*
*
* GNU Lesser General Public License Version 2.1
* =============================================
* Copyright 2005 by Sun Microsystems, Inc.
* 901 San Antonio Road, Palo Alto, CA 94303, USA
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License version 2.1, as published by the Free Software Foundation.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston,
* MA 02111-1307 USA
*
************************************************************************/
// MARKER(update_precomp.py): autogen include statement, do not remove
#include "precompiled_vcl.hxx"
#ifndef _SV_SVAPP_HXX
#include <vcl/svapp.hxx>
#endif
#ifndef _SV_SVDATA_HXX
#include <vcl/svdata.hxx>
#endif
#ifndef _SV_SALINST_HXX
#include <vcl/salinst.hxx>
#endif
#ifndef _VCL_SALSESSION_HXX
#include <vcl/salsession.hxx>
#endif
#ifndef _CPPUHELPER_COMPBASE1_HXX_
#include <cppuhelper/compbase1.hxx>
#endif
#ifndef _DEBUG_HXX
#include <tools/debug.hxx>
#endif
#ifndef _COM_SUN_STAR_FRAME_XSESSIONMANAGERCLIENT_HPP_
#include <com/sun/star/frame/XSessionManagerClient.hpp>
#endif
#ifndef _COM_SUN_STAR_LANG_XMULTISERVICEFACTORY_HPP_
#include <com/sun/star/lang/XMultiServiceFactory.hpp>
#endif
#include <list>
using namespace com::sun::star::uno;
using namespace com::sun::star::lang;
using namespace com::sun::star::frame;
using namespace rtl;
SalSession::~SalSession()
{
}
class VCLSession : public cppu::WeakComponentImplHelper1 < XSessionManagerClient >
{
struct Listener
{
Reference< XSessionManagerListener > m_xListener;
bool m_bInteractionRequested;
bool m_bInteractionDone;
bool m_bSaveDone;
Listener( const Reference< XSessionManagerListener >& xListener )
: m_xListener( xListener ),
m_bInteractionRequested( false ),
m_bInteractionDone( false ),
m_bSaveDone( false )
{}
};
std::list< Listener > m_aListeners;
SalSession* m_pSession;
osl::Mutex m_aMutex;
bool m_bInteractionRequested;
bool m_bInteractionGranted;
bool m_bInteractionDone;
bool m_bSaveDone;
static void SalSessionEventProc( SalSessionEvent* pEvent );
static VCLSession* pOneInstance;
void callSaveRequested( bool bShutdown, bool bCancelable );
void callShutdownCancelled();
void callInteractionGranted( bool bGranted );
public:
VCLSession();
virtual ~VCLSession();
virtual void SAL_CALL addSessionManagerListener( const Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
virtual void SAL_CALL removeSessionManagerListener( const Reference< XSessionManagerListener>& xListener ) throw( RuntimeException );
virtual void SAL_CALL queryInteraction( const Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
virtual void SAL_CALL interactionDone( const Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
virtual void SAL_CALL saveDone( const Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
virtual sal_Bool SAL_CALL cancelShutdown() throw( RuntimeException );
};
VCLSession* VCLSession::pOneInstance = NULL;
VCLSession::VCLSession()
: cppu::WeakComponentImplHelper1< XSessionManagerClient >( m_aMutex ),
m_bInteractionRequested( false ),
m_bInteractionGranted( false ),
m_bInteractionDone( false ),
m_bSaveDone( false )
{
DBG_ASSERT( pOneInstance == 0, "One instance of VCLSession only !" );
pOneInstance = this;
m_pSession = ImplGetSVData()->mpDefInst->CreateSalSession();
if( m_pSession )
m_pSession->SetCallback( SalSessionEventProc );
}
VCLSession::~VCLSession()
{
DBG_ASSERT( pOneInstance == this, "Another instance of VCLSession in destructor !" );
pOneInstance = NULL;
delete m_pSession;
}
void VCLSession::callSaveRequested( bool bShutdown, bool bCancelable )
{
std::list< Listener > aListeners;
{
osl::MutexGuard aGuard( m_aMutex );
// reset listener states
for( std::list< Listener >::iterator it = m_aListeners.begin();
it != m_aListeners.end(); ++it )
{
it->m_bSaveDone = it->m_bInteractionRequested = it->m_bInteractionDone = false;
}
// copy listener list since calling a listener may remove it.
aListeners = m_aListeners;
// set back interaction state
m_bSaveDone = false;
m_bInteractionDone = false;
// without session we assume UI is always possible,
// so it was reqeusted and granted
m_bInteractionRequested = m_bInteractionGranted = m_pSession ? false : true;
// answer the session manager even if no listeners available anymore
DBG_ASSERT( ! aListeners.empty(), "saveRequested but no listeners !" );
if( aListeners.empty() )
{
if( m_pSession )
m_pSession->saveDone();
return;
}
}
ULONG nAcquireCount = Application::ReleaseSolarMutex();
for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
it->m_xListener->doSave( bShutdown, bCancelable );
Application::AcquireSolarMutex( nAcquireCount );
}
void VCLSession::callInteractionGranted( bool bInteractionGranted )
{
std::list< Listener > aListeners;
{
osl::MutexGuard aGuard( m_aMutex );
// copy listener list since calling a listener may remove it.
for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
if( it->m_bInteractionRequested )
aListeners.push_back( *it );
m_bInteractionGranted = bInteractionGranted;
// answer the session manager even if no listeners available anymore
DBG_ASSERT( ! aListeners.empty(), "interactionGranted but no listeners !" );
if( aListeners.empty() )
{
if( m_pSession )
m_pSession->interactionDone();
return;
}
}
ULONG nAcquireCount = Application::ReleaseSolarMutex();
for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
it->m_xListener->approveInteraction( bInteractionGranted );
Application::AcquireSolarMutex( nAcquireCount );
}
void VCLSession::callShutdownCancelled()
{
std::list< Listener > aListeners;
{
osl::MutexGuard aGuard( m_aMutex );
// copy listener list since calling a listener may remove it.
aListeners = m_aListeners;
// set back interaction state
m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
}
ULONG nAcquireCount = Application::ReleaseSolarMutex();
for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
it->m_xListener->shutdownCanceled();
Application::AcquireSolarMutex( nAcquireCount );
}
void VCLSession::SalSessionEventProc( SalSessionEvent* pEvent )
{
switch( pEvent->m_eType )
{
case Interaction:
{
SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
pOneInstance->callInteractionGranted( pIEv->m_bInteractionGranted );
}
break;
case SaveRequest:
{
SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
pOneInstance->callSaveRequested( pSEv->m_bShutdown, pSEv->m_bCancelable );
}
break;
case ShutdownCancel:
pOneInstance->callShutdownCancelled();
break;
}
}
void SAL_CALL VCLSession::addSessionManagerListener( const Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
{
osl::MutexGuard aGuard( m_aMutex );
m_aListeners.push_back( Listener( xListener ) );
}
void SAL_CALL VCLSession::removeSessionManagerListener( const Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
{
osl::MutexGuard aGuard( m_aMutex );
std::list< Listener >::iterator it = m_aListeners.begin();
while( it != m_aListeners.end() )
{
if( it->m_xListener == xListener )
{
m_aListeners.erase( it );
it = m_aListeners.begin();
}
else
++it;
}
}
void SAL_CALL VCLSession::queryInteraction( const Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
{
if( m_bInteractionGranted )
{
if( m_bInteractionDone )
xListener->approveInteraction( false );
else
xListener->approveInteraction( true );
return;
}
osl::MutexGuard aGuard( m_aMutex );
if( ! m_bInteractionRequested )
{
m_pSession->queryInteraction();
m_bInteractionRequested = true;
}
for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
{
if( it->m_xListener == xListener )
{
it->m_bInteractionRequested = true;
it->m_bInteractionDone = false;
}
}
}
void SAL_CALL VCLSession::interactionDone( const Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
{
osl::MutexGuard aGuard( m_aMutex );
int nRequested = 0, nDone = 0;
for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
{
if( it->m_bInteractionRequested )
{
nRequested++;
if( xListener == it->m_xListener )
it->m_bInteractionDone = true;
}
if( it->m_bInteractionDone )
nDone++;
}
if( nDone == nRequested && nDone > 0 )
{
m_bInteractionDone = true;
if( m_pSession )
m_pSession->interactionDone();
}
}
void SAL_CALL VCLSession::saveDone( const Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
{
osl::MutexGuard aGuard( m_aMutex );
bool bSaveDone = true;
for( std::list< Listener >::iterator it = m_aListeners.begin();
it != m_aListeners.end(); ++it )
{
if( it->m_xListener == xListener )
it->m_bSaveDone = true;
if( ! it->m_bSaveDone )
bSaveDone = false;
}
if( bSaveDone )
{
m_bSaveDone = true;
if( m_pSession )
m_pSession->saveDone();
}
}
sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException )
{
return m_pSession ? (sal_Bool)m_pSession->cancelShutdown() : sal_False;
}
// service implementation
OUString SAL_CALL vcl_session_getImplementationName()
{
static OUString aImplementationName( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.VCLSessionManagerClient" ) );
return aImplementationName;
}
Sequence< rtl::OUString > SAL_CALL vcl_session_getSupportedServiceNames()
{
Sequence< OUString > aRet(1);
aRet[0] = OUString::createFromAscii("com.sun.star.frame.SessionManagerClient");
return aRet;
}
Reference< XInterface > SAL_CALL vcl_session_createInstance( const Reference< XMultiServiceFactory > & /*xMultiServiceFactory*/ )
{
ImplSVData* pSVData = ImplGetSVData();
if( ! pSVData->xSMClient.is() )
pSVData->xSMClient = new VCLSession();
return Reference< XInterface >(pSVData->xSMClient, UNO_QUERY );
}
|