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/basemutex.hxx>
24 : #include <cppuhelper/compbase1.hxx>
25 :
26 : #include <tools/debug.hxx>
27 :
28 : #include <vcl/svapp.hxx>
29 :
30 : #include <factory.hxx>
31 : #include <svdata.hxx>
32 : #include <salinst.hxx>
33 : #include <salsession.hxx>
34 :
35 : #include <com/sun/star/frame/XSessionManagerClient.hpp>
36 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
37 : #include <com/sun/star/frame/XSessionManagerListener2.hpp>
38 :
39 : #include <list>
40 :
41 : using namespace com::sun::star::uno;
42 : using namespace com::sun::star::lang;
43 : using namespace com::sun::star::frame;
44 :
45 0 : SalSession::~SalSession()
46 : {
47 0 : }
48 :
49 : class VCLSession:
50 : private cppu::BaseMutex,
51 : public cppu::WeakComponentImplHelper1 < XSessionManagerClient >
52 : {
53 0 : struct Listener
54 : {
55 : css::uno::Reference< XSessionManagerListener > m_xListener;
56 : bool m_bInteractionRequested;
57 : bool m_bInteractionDone;
58 : bool m_bSaveDone;
59 :
60 0 : Listener( const css::uno::Reference< XSessionManagerListener >& xListener )
61 : : m_xListener( xListener ),
62 : m_bInteractionRequested( false ),
63 : m_bInteractionDone( false ),
64 0 : m_bSaveDone( false )
65 0 : {}
66 : };
67 :
68 : std::list< Listener > m_aListeners;
69 : boost::scoped_ptr< SalSession > m_pSession;
70 : bool m_bInteractionRequested;
71 : bool m_bInteractionGranted;
72 : bool m_bInteractionDone;
73 : bool m_bSaveDone;
74 :
75 : static void SalSessionEventProc( void* pData, SalSessionEvent* pEvent );
76 :
77 0 : virtual ~VCLSession() {}
78 :
79 : virtual void SAL_CALL addSessionManagerListener( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
80 : virtual void SAL_CALL removeSessionManagerListener( const css::uno::Reference< XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
81 : virtual void SAL_CALL queryInteraction( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
82 : virtual void SAL_CALL interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
83 : virtual void SAL_CALL saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
84 : virtual sal_Bool SAL_CALL cancelShutdown() throw( RuntimeException, std::exception ) SAL_OVERRIDE;
85 :
86 : void callSaveRequested( bool bShutdown, bool bCancelable );
87 : void callShutdownCancelled();
88 : void callInteractionGranted( bool bGranted );
89 : void callQuit();
90 :
91 : public:
92 : VCLSession();
93 : };
94 :
95 0 : VCLSession::VCLSession()
96 : : cppu::WeakComponentImplHelper1< XSessionManagerClient >( m_aMutex ),
97 0 : m_pSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
98 : m_bInteractionRequested( false ),
99 : m_bInteractionGranted( false ),
100 : m_bInteractionDone( false ),
101 0 : m_bSaveDone( false )
102 : {
103 0 : if( m_pSession )
104 0 : m_pSession->SetCallback( SalSessionEventProc, this );
105 0 : }
106 :
107 0 : void VCLSession::callSaveRequested( bool bShutdown, bool bCancelable )
108 : {
109 0 : std::list< Listener > aListeners;
110 : {
111 0 : osl::MutexGuard aGuard( m_aMutex );
112 : // reset listener states
113 0 : for( std::list< Listener >::iterator it = m_aListeners.begin();
114 0 : it != m_aListeners.end(); ++it )
115 : {
116 0 : it->m_bSaveDone = it->m_bInteractionRequested = it->m_bInteractionDone = false;
117 : }
118 :
119 : // copy listener list since calling a listener may remove it.
120 0 : aListeners = m_aListeners;
121 : // set back interaction state
122 0 : m_bSaveDone = false;
123 0 : m_bInteractionDone = false;
124 : // without session we assume UI is always possible,
125 : // so it was reqeusted and granted
126 0 : m_bInteractionRequested = m_bInteractionGranted = !m_pSession;
127 :
128 : // answer the session manager even if no listeners available anymore
129 : DBG_ASSERT( ! aListeners.empty(), "saveRequested but no listeners !" );
130 0 : if( aListeners.empty() )
131 : {
132 0 : if( m_pSession )
133 0 : m_pSession->saveDone();
134 0 : return;
135 0 : }
136 : }
137 :
138 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
139 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
140 0 : it->m_xListener->doSave( bShutdown, bCancelable );
141 0 : Application::AcquireSolarMutex( nAcquireCount );
142 : }
143 :
144 0 : void VCLSession::callInteractionGranted( bool bInteractionGranted )
145 : {
146 0 : std::list< Listener > aListeners;
147 : {
148 0 : osl::MutexGuard aGuard( m_aMutex );
149 : // copy listener list since calling a listener may remove it.
150 0 : for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
151 0 : if( it->m_bInteractionRequested )
152 0 : aListeners.push_back( *it );
153 :
154 0 : m_bInteractionGranted = bInteractionGranted;
155 :
156 : // answer the session manager even if no listeners available anymore
157 : DBG_ASSERT( ! aListeners.empty(), "interactionGranted but no listeners !" );
158 0 : if( aListeners.empty() )
159 : {
160 0 : if( m_pSession )
161 0 : m_pSession->interactionDone();
162 0 : return;
163 0 : }
164 : }
165 :
166 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
167 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
168 0 : it->m_xListener->approveInteraction( bInteractionGranted );
169 :
170 0 : Application::AcquireSolarMutex( nAcquireCount );
171 : }
172 :
173 0 : void VCLSession::callShutdownCancelled()
174 : {
175 0 : std::list< Listener > aListeners;
176 : {
177 0 : osl::MutexGuard aGuard( m_aMutex );
178 : // copy listener list since calling a listener may remove it.
179 0 : aListeners = m_aListeners;
180 : // set back interaction state
181 0 : m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
182 : }
183 :
184 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
185 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
186 0 : it->m_xListener->shutdownCanceled();
187 0 : Application::AcquireSolarMutex( nAcquireCount );
188 0 : }
189 :
190 0 : void VCLSession::callQuit()
191 : {
192 0 : std::list< Listener > aListeners;
193 : {
194 0 : osl::MutexGuard aGuard( m_aMutex );
195 : // copy listener list since calling a listener may remove it.
196 0 : aListeners = m_aListeners;
197 : // set back interaction state
198 0 : m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
199 : }
200 :
201 0 : sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
202 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
203 : {
204 0 : css::uno::Reference< XSessionManagerListener2 > xListener2( it->m_xListener, UNO_QUERY );
205 0 : if( xListener2.is() )
206 0 : xListener2->doQuit();
207 0 : }
208 0 : Application::AcquireSolarMutex( nAcquireCount );
209 0 : }
210 :
211 0 : void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
212 : {
213 0 : VCLSession * pThis = static_cast< VCLSession * >( pData );
214 0 : switch( pEvent->m_eType )
215 : {
216 : case Interaction:
217 : {
218 0 : SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
219 0 : pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
220 : }
221 0 : break;
222 : case SaveRequest:
223 : {
224 0 : SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
225 0 : pThis->callSaveRequested( pSEv->m_bShutdown, pSEv->m_bCancelable );
226 : }
227 0 : break;
228 : case ShutdownCancel:
229 0 : pThis->callShutdownCancelled();
230 0 : break;
231 : case Quit:
232 0 : pThis->callQuit();
233 0 : break;
234 : }
235 0 : }
236 :
237 0 : void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
238 : {
239 0 : osl::MutexGuard aGuard( m_aMutex );
240 :
241 0 : m_aListeners.push_back( Listener( xListener ) );
242 0 : }
243 :
244 0 : void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
245 : {
246 0 : osl::MutexGuard aGuard( m_aMutex );
247 :
248 0 : std::list< Listener >::iterator it = m_aListeners.begin();
249 0 : while( it != m_aListeners.end() )
250 : {
251 0 : if( it->m_xListener == xListener )
252 : {
253 0 : it = m_aListeners.erase(it);
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, std::exception )
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( m_aMutex );
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, std::exception )
288 : {
289 0 : osl::MutexGuard aGuard( m_aMutex );
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, std::exception )
311 : {
312 0 : osl::MutexGuard aGuard( m_aMutex );
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, std::exception )
332 : {
333 0 : return m_pSession && m_pSession->cancelShutdown();
334 : }
335 :
336 : // service implementation
337 :
338 0 : OUString SAL_CALL vcl_session_getImplementationName()
339 : {
340 0 : 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 : }
354 :
355 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|