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 "sal/config.h"
21 :
22 : #include <boost/scoped_ptr.hpp>
23 : #include <cppuhelper/compbase1.hxx>
24 :
25 : #include <tools/debug.hxx>
26 :
27 : #include <vcl/svapp.hxx>
28 :
29 : #include <svdata.hxx>
30 : #include <salinst.hxx>
31 : #include <salsession.hxx>
32 :
33 : #include <com/sun/star/frame/XSessionManagerClient.hpp>
34 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 : #include <com/sun/star/frame/XSessionManagerListener2.hpp>
36 :
37 : #include <list>
38 :
39 : using namespace com::sun::star::uno;
40 : using namespace com::sun::star::lang;
41 : using namespace com::sun::star::frame;
42 :
43 :
44 0 : SalSession::~SalSession()
45 : {
46 0 : }
47 :
48 : class VCLSession:
49 : private osl::Mutex,
50 : public cppu::WeakComponentImplHelper1 < XSessionManagerClient >
51 : {
52 0 : struct Listener
53 : {
54 : css::uno::Reference< XSessionManagerListener > m_xListener;
55 : bool m_bInteractionRequested;
56 : bool m_bInteractionDone;
57 : bool m_bSaveDone;
58 :
59 0 : Listener( const css::uno::Reference< XSessionManagerListener >& xListener )
60 : : m_xListener( xListener ),
61 : m_bInteractionRequested( false ),
62 : m_bInteractionDone( false ),
63 0 : m_bSaveDone( false )
64 0 : {}
65 : };
66 :
67 : std::list< Listener > m_aListeners;
68 : boost::scoped_ptr< SalSession > m_pSession;
69 : bool m_bInteractionRequested;
70 : bool m_bInteractionGranted;
71 : bool m_bInteractionDone;
72 : bool m_bSaveDone;
73 :
74 : static void SalSessionEventProc( void* pData, SalSessionEvent* pEvent );
75 :
76 0 : virtual ~VCLSession() {}
77 :
78 : virtual void SAL_CALL addSessionManagerListener( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
79 : virtual void SAL_CALL removeSessionManagerListener( const css::uno::Reference< XSessionManagerListener>& xListener ) throw( RuntimeException );
80 : virtual void SAL_CALL queryInteraction( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
81 : virtual void SAL_CALL interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
82 : virtual void SAL_CALL saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
83 : virtual sal_Bool SAL_CALL cancelShutdown() throw( RuntimeException );
84 :
85 : void callSaveRequested( bool bShutdown, bool bCancelable );
86 : void callShutdownCancelled();
87 : void callInteractionGranted( bool bGranted );
88 : void callQuit();
89 :
90 : public:
91 : VCLSession();
92 : };
93 :
94 0 : VCLSession::VCLSession()
95 : : cppu::WeakComponentImplHelper1< XSessionManagerClient >( *static_cast< osl::Mutex * >(this) ),
96 0 : m_pSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
97 : m_bInteractionRequested( false ),
98 : m_bInteractionGranted( false ),
99 : m_bInteractionDone( false ),
100 0 : m_bSaveDone( false )
101 : {
102 0 : if( m_pSession )
103 0 : m_pSession->SetCallback( SalSessionEventProc, this );
104 0 : }
105 :
106 0 : void VCLSession::callSaveRequested( bool bShutdown, bool bCancelable )
107 : {
108 0 : std::list< Listener > aListeners;
109 : {
110 0 : osl::MutexGuard aGuard( *this );
111 : // reset listener states
112 0 : for( std::list< Listener >::iterator it = m_aListeners.begin();
113 0 : it != m_aListeners.end(); ++it )
114 : {
115 0 : it->m_bSaveDone = it->m_bInteractionRequested = it->m_bInteractionDone = false;
116 : }
117 :
118 : // copy listener list since calling a listener may remove it.
119 0 : aListeners = m_aListeners;
120 : // set back interaction state
121 0 : m_bSaveDone = false;
122 0 : m_bInteractionDone = false;
123 : // without session we assume UI is always possible,
124 : // so it was reqeusted and granted
125 0 : m_bInteractionRequested = m_bInteractionGranted = !m_pSession;
126 :
127 : // answer the session manager even if no listeners available anymore
128 : DBG_ASSERT( ! aListeners.empty(), "saveRequested but no listeners !" );
129 0 : if( aListeners.empty() )
130 : {
131 0 : if( m_pSession )
132 0 : m_pSession->saveDone();
133 0 : return;
134 0 : }
135 : }
136 :
137 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
138 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
139 0 : it->m_xListener->doSave( bShutdown, bCancelable );
140 0 : Application::AcquireSolarMutex( nAcquireCount );
141 : }
142 :
143 0 : void VCLSession::callInteractionGranted( bool bInteractionGranted )
144 : {
145 0 : std::list< Listener > aListeners;
146 : {
147 0 : osl::MutexGuard aGuard( *this );
148 : // copy listener list since calling a listener may remove it.
149 0 : for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
150 0 : if( it->m_bInteractionRequested )
151 0 : aListeners.push_back( *it );
152 :
153 0 : m_bInteractionGranted = bInteractionGranted;
154 :
155 : // answer the session manager even if no listeners available anymore
156 : DBG_ASSERT( ! aListeners.empty(), "interactionGranted but no listeners !" );
157 0 : if( aListeners.empty() )
158 : {
159 0 : if( m_pSession )
160 0 : m_pSession->interactionDone();
161 0 : return;
162 0 : }
163 : }
164 :
165 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
166 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
167 0 : it->m_xListener->approveInteraction( bInteractionGranted );
168 :
169 0 : Application::AcquireSolarMutex( nAcquireCount );
170 : }
171 :
172 0 : void VCLSession::callShutdownCancelled()
173 : {
174 0 : std::list< Listener > aListeners;
175 : {
176 0 : osl::MutexGuard aGuard( *this );
177 : // copy listener list since calling a listener may remove it.
178 0 : aListeners = m_aListeners;
179 : // set back interaction state
180 0 : m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
181 : }
182 :
183 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
184 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
185 0 : it->m_xListener->shutdownCanceled();
186 0 : Application::AcquireSolarMutex( nAcquireCount );
187 0 : }
188 :
189 0 : void VCLSession::callQuit()
190 : {
191 0 : std::list< Listener > aListeners;
192 : {
193 0 : osl::MutexGuard aGuard( *this );
194 : // copy listener list since calling a listener may remove it.
195 0 : aListeners = m_aListeners;
196 : // set back interaction state
197 0 : m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
198 : }
199 :
200 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
201 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
202 : {
203 0 : css::uno::Reference< XSessionManagerListener2 > xListener2( it->m_xListener, UNO_QUERY );
204 0 : if( xListener2.is() )
205 0 : xListener2->doQuit();
206 0 : }
207 0 : Application::AcquireSolarMutex( nAcquireCount );
208 0 : }
209 :
210 0 : void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
211 : {
212 0 : VCLSession * pThis = static_cast< VCLSession * >( pData );
213 0 : switch( pEvent->m_eType )
214 : {
215 : case Interaction:
216 : {
217 0 : SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
218 0 : pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
219 : }
220 0 : break;
221 : case SaveRequest:
222 : {
223 0 : SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
224 0 : pThis->callSaveRequested( pSEv->m_bShutdown, pSEv->m_bCancelable );
225 : }
226 0 : break;
227 : case ShutdownCancel:
228 0 : pThis->callShutdownCancelled();
229 0 : break;
230 : case Quit:
231 0 : pThis->callQuit();
232 0 : break;
233 : }
234 0 : }
235 :
236 0 : void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
237 : {
238 0 : osl::MutexGuard aGuard( *this );
239 :
240 0 : m_aListeners.push_back( Listener( xListener ) );
241 0 : }
242 :
243 0 : void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
244 : {
245 0 : osl::MutexGuard aGuard( *this );
246 :
247 0 : std::list< Listener >::iterator it = m_aListeners.begin();
248 0 : while( it != m_aListeners.end() )
249 : {
250 0 : if( it->m_xListener == xListener )
251 : {
252 0 : m_aListeners.erase( it );
253 0 : it = m_aListeners.begin();
254 : }
255 : else
256 0 : ++it;
257 0 : }
258 0 : }
259 :
260 0 : void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
261 : {
262 0 : if( m_bInteractionGranted )
263 : {
264 0 : if( m_bInteractionDone )
265 0 : xListener->approveInteraction( false );
266 : else
267 0 : xListener->approveInteraction( true );
268 0 : return;
269 : }
270 :
271 0 : osl::MutexGuard aGuard( *this );
272 0 : if( ! m_bInteractionRequested )
273 : {
274 0 : m_pSession->queryInteraction();
275 0 : m_bInteractionRequested = true;
276 : }
277 0 : for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
278 : {
279 0 : if( it->m_xListener == xListener )
280 : {
281 0 : it->m_bInteractionRequested = true;
282 0 : it->m_bInteractionDone = false;
283 : }
284 0 : }
285 : }
286 :
287 0 : void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
288 : {
289 0 : osl::MutexGuard aGuard( *this );
290 0 : int nRequested = 0, nDone = 0;
291 0 : for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
292 : {
293 0 : if( it->m_bInteractionRequested )
294 : {
295 0 : nRequested++;
296 0 : if( xListener == it->m_xListener )
297 0 : it->m_bInteractionDone = true;
298 : }
299 0 : if( it->m_bInteractionDone )
300 0 : nDone++;
301 : }
302 0 : if( nDone == nRequested && nDone > 0 )
303 : {
304 0 : m_bInteractionDone = true;
305 0 : if( m_pSession )
306 0 : m_pSession->interactionDone();
307 0 : }
308 0 : }
309 :
310 0 : void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
311 : {
312 0 : osl::MutexGuard aGuard( *this );
313 :
314 0 : bool bSaveDone = true;
315 0 : for( std::list< Listener >::iterator it = m_aListeners.begin();
316 0 : it != m_aListeners.end(); ++it )
317 : {
318 0 : if( it->m_xListener == xListener )
319 0 : it->m_bSaveDone = true;
320 0 : if( ! it->m_bSaveDone )
321 0 : bSaveDone = false;
322 : }
323 0 : if( bSaveDone )
324 : {
325 0 : m_bSaveDone = true;
326 0 : if( m_pSession )
327 0 : m_pSession->saveDone();
328 0 : }
329 0 : }
330 :
331 0 : sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException )
332 : {
333 0 : return m_pSession && m_pSession->cancelShutdown();
334 : }
335 :
336 : // service implementation
337 :
338 98 : OUString SAL_CALL vcl_session_getImplementationName()
339 : {
340 98 : return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
341 : }
342 :
343 0 : Sequence< OUString > SAL_CALL vcl_session_getSupportedServiceNames()
344 : {
345 0 : Sequence< OUString > aRet(1);
346 0 : aRet[0] = "com.sun.star.frame.SessionManagerClient";
347 0 : return aRet;
348 : }
349 :
350 0 : css::uno::Reference< XInterface > SAL_CALL vcl_session_createInstance( SAL_UNUSED_PARAMETER const css::uno::Reference< XMultiServiceFactory > & )
351 : {
352 0 : return static_cast< cppu::OWeakObject * >(new VCLSession);
353 465 : }
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|