update credits
[LibreOffice.git] / vcl / source / app / session.cxx
blob9c0f30b84fbbb45ee1da6dbda8a753a703f9fea1
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include "sal/config.h"
22 #include <boost/scoped_ptr.hpp>
23 #include <cppuhelper/compbase1.hxx>
25 #include <tools/debug.hxx>
27 #include <vcl/svapp.hxx>
29 #include <svdata.hxx>
30 #include <salinst.hxx>
31 #include <salsession.hxx>
33 #include <com/sun/star/frame/XSessionManagerClient.hpp>
34 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
35 #include <com/sun/star/frame/XSessionManagerListener2.hpp>
37 #include <list>
39 using namespace com::sun::star::uno;
40 using namespace com::sun::star::lang;
41 using namespace com::sun::star::frame;
44 SalSession::~SalSession()
48 class VCLSession:
49 private osl::Mutex,
50 public cppu::WeakComponentImplHelper1 < XSessionManagerClient >
52 struct Listener
54 css::uno::Reference< XSessionManagerListener > m_xListener;
55 bool m_bInteractionRequested;
56 bool m_bInteractionDone;
57 bool m_bSaveDone;
59 Listener( const css::uno::Reference< XSessionManagerListener >& xListener )
60 : m_xListener( xListener ),
61 m_bInteractionRequested( false ),
62 m_bInteractionDone( false ),
63 m_bSaveDone( false )
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;
74 static void SalSessionEventProc( void* pData, SalSessionEvent* pEvent );
76 virtual ~VCLSession() {}
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 );
85 void callSaveRequested( bool bShutdown, bool bCancelable );
86 void callShutdownCancelled();
87 void callInteractionGranted( bool bGranted );
88 void callQuit();
90 public:
91 VCLSession();
94 VCLSession::VCLSession()
95 : cppu::WeakComponentImplHelper1< XSessionManagerClient >( *static_cast< osl::Mutex * >(this) ),
96 m_pSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
97 m_bInteractionRequested( false ),
98 m_bInteractionGranted( false ),
99 m_bInteractionDone( false ),
100 m_bSaveDone( false )
102 if( m_pSession )
103 m_pSession->SetCallback( SalSessionEventProc, this );
106 void VCLSession::callSaveRequested( bool bShutdown, bool bCancelable )
108 std::list< Listener > aListeners;
110 osl::MutexGuard aGuard( *this );
111 // reset listener states
112 for( std::list< Listener >::iterator it = m_aListeners.begin();
113 it != m_aListeners.end(); ++it )
115 it->m_bSaveDone = it->m_bInteractionRequested = it->m_bInteractionDone = false;
118 // copy listener list since calling a listener may remove it.
119 aListeners = m_aListeners;
120 // set back interaction state
121 m_bSaveDone = false;
122 m_bInteractionDone = false;
123 // without session we assume UI is always possible,
124 // so it was reqeusted and granted
125 m_bInteractionRequested = m_bInteractionGranted = !m_pSession;
127 // answer the session manager even if no listeners available anymore
128 DBG_ASSERT( ! aListeners.empty(), "saveRequested but no listeners !" );
129 if( aListeners.empty() )
131 if( m_pSession )
132 m_pSession->saveDone();
133 return;
137 sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
138 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
139 it->m_xListener->doSave( bShutdown, bCancelable );
140 Application::AcquireSolarMutex( nAcquireCount );
143 void VCLSession::callInteractionGranted( bool bInteractionGranted )
145 std::list< Listener > aListeners;
147 osl::MutexGuard aGuard( *this );
148 // copy listener list since calling a listener may remove it.
149 for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
150 if( it->m_bInteractionRequested )
151 aListeners.push_back( *it );
153 m_bInteractionGranted = bInteractionGranted;
155 // answer the session manager even if no listeners available anymore
156 DBG_ASSERT( ! aListeners.empty(), "interactionGranted but no listeners !" );
157 if( aListeners.empty() )
159 if( m_pSession )
160 m_pSession->interactionDone();
161 return;
165 sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
166 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
167 it->m_xListener->approveInteraction( bInteractionGranted );
169 Application::AcquireSolarMutex( nAcquireCount );
172 void VCLSession::callShutdownCancelled()
174 std::list< Listener > aListeners;
176 osl::MutexGuard aGuard( *this );
177 // copy listener list since calling a listener may remove it.
178 aListeners = m_aListeners;
179 // set back interaction state
180 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
183 sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
184 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
185 it->m_xListener->shutdownCanceled();
186 Application::AcquireSolarMutex( nAcquireCount );
189 void VCLSession::callQuit()
191 std::list< Listener > aListeners;
193 osl::MutexGuard aGuard( *this );
194 // copy listener list since calling a listener may remove it.
195 aListeners = m_aListeners;
196 // set back interaction state
197 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
200 sal_uLong nAcquireCount = Application::ReleaseSolarMutex();
201 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
203 css::uno::Reference< XSessionManagerListener2 > xListener2( it->m_xListener, UNO_QUERY );
204 if( xListener2.is() )
205 xListener2->doQuit();
207 Application::AcquireSolarMutex( nAcquireCount );
210 void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
212 VCLSession * pThis = static_cast< VCLSession * >( pData );
213 switch( pEvent->m_eType )
215 case Interaction:
217 SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
218 pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
220 break;
221 case SaveRequest:
223 SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
224 pThis->callSaveRequested( pSEv->m_bShutdown, pSEv->m_bCancelable );
226 break;
227 case ShutdownCancel:
228 pThis->callShutdownCancelled();
229 break;
230 case Quit:
231 pThis->callQuit();
232 break;
236 void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
238 osl::MutexGuard aGuard( *this );
240 m_aListeners.push_back( Listener( xListener ) );
243 void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
245 osl::MutexGuard aGuard( *this );
247 std::list< Listener >::iterator it = m_aListeners.begin();
248 while( it != m_aListeners.end() )
250 if( it->m_xListener == xListener )
252 m_aListeners.erase( it );
253 it = m_aListeners.begin();
255 else
256 ++it;
260 void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException )
262 if( m_bInteractionGranted )
264 if( m_bInteractionDone )
265 xListener->approveInteraction( false );
266 else
267 xListener->approveInteraction( true );
268 return;
271 osl::MutexGuard aGuard( *this );
272 if( ! m_bInteractionRequested )
274 m_pSession->queryInteraction();
275 m_bInteractionRequested = true;
277 for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
279 if( it->m_xListener == xListener )
281 it->m_bInteractionRequested = true;
282 it->m_bInteractionDone = false;
287 void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
289 osl::MutexGuard aGuard( *this );
290 int nRequested = 0, nDone = 0;
291 for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
293 if( it->m_bInteractionRequested )
295 nRequested++;
296 if( xListener == it->m_xListener )
297 it->m_bInteractionDone = true;
299 if( it->m_bInteractionDone )
300 nDone++;
302 if( nDone == nRequested && nDone > 0 )
304 m_bInteractionDone = true;
305 if( m_pSession )
306 m_pSession->interactionDone();
310 void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException )
312 osl::MutexGuard aGuard( *this );
314 bool bSaveDone = true;
315 for( std::list< Listener >::iterator it = m_aListeners.begin();
316 it != m_aListeners.end(); ++it )
318 if( it->m_xListener == xListener )
319 it->m_bSaveDone = true;
320 if( ! it->m_bSaveDone )
321 bSaveDone = false;
323 if( bSaveDone )
325 m_bSaveDone = true;
326 if( m_pSession )
327 m_pSession->saveDone();
331 sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException )
333 return m_pSession && m_pSession->cancelShutdown();
336 // service implementation
338 OUString SAL_CALL vcl_session_getImplementationName()
340 return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
343 Sequence< OUString > SAL_CALL vcl_session_getSupportedServiceNames()
345 Sequence< OUString > aRet(1);
346 aRet[0] = "com.sun.star.frame.SessionManagerClient";
347 return aRet;
350 css::uno::Reference< XInterface > SAL_CALL vcl_session_createInstance( SAL_UNUSED_PARAMETER const css::uno::Reference< XMultiServiceFactory > & )
352 return static_cast< cppu::OWeakObject * >(new VCLSession);
355 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */