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 : using ::rtl::OUString;
44 :
45 0 : SalSession::~SalSession()
46 : {
47 0 : }
48 :
49 : class VCLSession:
50 : private osl::Mutex,
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 );
80 : virtual void SAL_CALL removeSessionManagerListener( const css::uno::Reference< XSessionManagerListener>& xListener ) throw( RuntimeException );
81 : virtual void SAL_CALL queryInteraction( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
82 : virtual void SAL_CALL interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
83 : virtual void SAL_CALL saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException );
84 : virtual sal_Bool SAL_CALL cancelShutdown() throw( RuntimeException );
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 >( *static_cast< osl::Mutex * >(this) ),
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( *this );
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( *this );
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( *this );
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( *this );
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 )
238 : {
239 0 : osl::MutexGuard aGuard( *this );
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 )
245 : {
246 0 : osl::MutexGuard aGuard( *this );
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 : m_aListeners.erase( it );
254 0 : it = m_aListeners.begin();
255 : }
256 : else
257 0 : ++it;
258 0 : }
259 0 : }
260 :
261 0 : void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
262 : {
263 0 : if( m_bInteractionGranted )
264 : {
265 0 : if( m_bInteractionDone )
266 0 : xListener->approveInteraction( false );
267 : else
268 0 : xListener->approveInteraction( true );
269 0 : return;
270 : }
271 :
272 0 : osl::MutexGuard aGuard( *this );
273 0 : if( ! m_bInteractionRequested )
274 : {
275 0 : m_pSession->queryInteraction();
276 0 : m_bInteractionRequested = true;
277 : }
278 0 : for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
279 : {
280 0 : if( it->m_xListener == xListener )
281 : {
282 0 : it->m_bInteractionRequested = true;
283 0 : it->m_bInteractionDone = false;
284 : }
285 0 : }
286 : }
287 :
288 0 : void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
289 : {
290 0 : osl::MutexGuard aGuard( *this );
291 0 : int nRequested = 0, nDone = 0;
292 0 : for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
293 : {
294 0 : if( it->m_bInteractionRequested )
295 : {
296 0 : nRequested++;
297 0 : if( xListener == it->m_xListener )
298 0 : it->m_bInteractionDone = true;
299 : }
300 0 : if( it->m_bInteractionDone )
301 0 : nDone++;
302 : }
303 0 : if( nDone == nRequested && nDone > 0 )
304 : {
305 0 : m_bInteractionDone = true;
306 0 : if( m_pSession )
307 0 : m_pSession->interactionDone();
308 0 : }
309 0 : }
310 :
311 0 : void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
312 : {
313 0 : osl::MutexGuard aGuard( *this );
314 :
315 0 : bool bSaveDone = true;
316 0 : for( std::list< Listener >::iterator it = m_aListeners.begin();
317 0 : it != m_aListeners.end(); ++it )
318 : {
319 0 : if( it->m_xListener == xListener )
320 0 : it->m_bSaveDone = true;
321 0 : if( ! it->m_bSaveDone )
322 0 : bSaveDone = false;
323 : }
324 0 : if( bSaveDone )
325 : {
326 0 : m_bSaveDone = true;
327 0 : if( m_pSession )
328 0 : m_pSession->saveDone();
329 0 : }
330 0 : }
331 :
332 0 : sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException )
333 : {
334 0 : return m_pSession && m_pSession->cancelShutdown();
335 : }
336 :
337 : // service implementation
338 :
339 0 : OUString SAL_CALL vcl_session_getImplementationName()
340 : {
341 0 : return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
342 : }
343 :
344 0 : Sequence< rtl::OUString > SAL_CALL vcl_session_getSupportedServiceNames()
345 : {
346 0 : Sequence< OUString > aRet(1);
347 0 : aRet[0] = "com.sun.star.frame.SessionManagerClient";
348 0 : return aRet;
349 : }
350 :
351 0 : css::uno::Reference< XInterface > SAL_CALL vcl_session_createInstance( SAL_UNUSED_PARAMETER const css::uno::Reference< XMultiServiceFactory > & )
352 : {
353 0 : return static_cast< cppu::OWeakObject * >(new VCLSession);
354 : }
355 :
356 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|