Branch libreoffice-5-0-4
[LibreOffice.git] / vcl / source / app / session.cxx
blob5db137f4d463e4a5173d69fcb19664c96257d487
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 <cppuhelper/basemutex.hxx>
23 #include <cppuhelper/compbase1.hxx>
25 #include <tools/debug.hxx>
27 #include <vcl/svapp.hxx>
29 #include <factory.hxx>
30 #include <svdata.hxx>
31 #include <salinst.hxx>
32 #include <salsession.hxx>
34 #include <com/sun/star/frame/XSessionManagerClient.hpp>
35 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 #include <com/sun/star/frame/XSessionManagerListener2.hpp>
38 #include <list>
40 using namespace com::sun::star::uno;
41 using namespace com::sun::star::lang;
42 using namespace com::sun::star::frame;
44 SalSession::~SalSession()
48 class VCLSession:
49 private cppu::BaseMutex,
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 std::unique_ptr< SalSession > m_xSession;
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, std::exception ) SAL_OVERRIDE;
79 virtual void SAL_CALL removeSessionManagerListener( const css::uno::Reference< XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
80 virtual void SAL_CALL queryInteraction( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
81 virtual void SAL_CALL interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
82 virtual void SAL_CALL saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) SAL_OVERRIDE;
83 virtual sal_Bool SAL_CALL cancelShutdown() throw( RuntimeException, std::exception ) SAL_OVERRIDE;
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 >( m_aMutex ),
96 m_xSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
97 m_bInteractionRequested( false ),
98 m_bInteractionGranted( false ),
99 m_bInteractionDone( false ),
100 m_bSaveDone( false )
102 if (m_xSession)
103 m_xSession->SetCallback( SalSessionEventProc, this );
106 void VCLSession::callSaveRequested( bool bShutdown, bool bCancelable )
108 std::list< Listener > aListeners;
110 osl::MutexGuard aGuard( m_aMutex );
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_xSession;
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_xSession)
132 m_xSession->saveDone();
133 return;
137 SolarMutexReleaser aReleaser;
138 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
139 it->m_xListener->doSave( bShutdown, bCancelable );
142 void VCLSession::callInteractionGranted( bool bInteractionGranted )
144 std::list< Listener > aListeners;
146 osl::MutexGuard aGuard( m_aMutex );
147 // copy listener list since calling a listener may remove it.
148 for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
149 if( it->m_bInteractionRequested )
150 aListeners.push_back( *it );
152 m_bInteractionGranted = bInteractionGranted;
154 // answer the session manager even if no listeners available anymore
155 DBG_ASSERT( ! aListeners.empty(), "interactionGranted but no listeners !" );
156 if( aListeners.empty() )
158 if (m_xSession)
159 m_xSession->interactionDone();
160 return;
164 SolarMutexReleaser aReleaser;
165 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
166 it->m_xListener->approveInteraction( bInteractionGranted );
169 void VCLSession::callShutdownCancelled()
171 std::list< Listener > aListeners;
173 osl::MutexGuard aGuard( m_aMutex );
174 // copy listener list since calling a listener may remove it.
175 aListeners = m_aListeners;
176 // set back interaction state
177 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
180 SolarMutexReleaser aReleaser;
181 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
182 it->m_xListener->shutdownCanceled();
185 void VCLSession::callQuit()
187 std::list< Listener > aListeners;
189 osl::MutexGuard aGuard( m_aMutex );
190 // copy listener list since calling a listener may remove it.
191 aListeners = m_aListeners;
192 // set back interaction state
193 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
196 SolarMutexReleaser aReleaser;
197 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
199 css::uno::Reference< XSessionManagerListener2 > xListener2( it->m_xListener, UNO_QUERY );
200 if( xListener2.is() )
201 xListener2->doQuit();
205 void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
207 VCLSession * pThis = static_cast< VCLSession * >( pData );
208 switch( pEvent->m_eType )
210 case Interaction:
212 SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
213 pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
215 break;
216 case SaveRequest:
218 SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
219 pThis->callSaveRequested( pSEv->m_bShutdown, pSEv->m_bCancelable );
221 break;
222 case ShutdownCancel:
223 pThis->callShutdownCancelled();
224 break;
225 case Quit:
226 pThis->callQuit();
227 break;
231 void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
233 osl::MutexGuard aGuard( m_aMutex );
235 m_aListeners.push_back( Listener( xListener ) );
238 void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
240 osl::MutexGuard aGuard( m_aMutex );
242 std::list< Listener >::iterator it = m_aListeners.begin();
243 while( it != m_aListeners.end() )
245 if( it->m_xListener == xListener )
247 it = m_aListeners.erase(it);
249 else
250 ++it;
254 void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
256 if( m_bInteractionGranted )
258 if( m_bInteractionDone )
259 xListener->approveInteraction( false );
260 else
261 xListener->approveInteraction( true );
262 return;
265 osl::MutexGuard aGuard( m_aMutex );
266 if( ! m_bInteractionRequested )
268 m_xSession->queryInteraction();
269 m_bInteractionRequested = true;
271 for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
273 if( it->m_xListener == xListener )
275 it->m_bInteractionRequested = true;
276 it->m_bInteractionDone = false;
281 void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception )
283 osl::MutexGuard aGuard( m_aMutex );
284 int nRequested = 0, nDone = 0;
285 for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
287 if( it->m_bInteractionRequested )
289 nRequested++;
290 if( xListener == it->m_xListener )
291 it->m_bInteractionDone = true;
293 if( it->m_bInteractionDone )
294 nDone++;
296 if( nDone == nRequested && nDone > 0 )
298 m_bInteractionDone = true;
299 if (m_xSession)
300 m_xSession->interactionDone();
304 void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception )
306 osl::MutexGuard aGuard( m_aMutex );
308 bool bSaveDone = true;
309 for( std::list< Listener >::iterator it = m_aListeners.begin();
310 it != m_aListeners.end(); ++it )
312 if( it->m_xListener == xListener )
313 it->m_bSaveDone = true;
314 if( ! it->m_bSaveDone )
315 bSaveDone = false;
317 if( bSaveDone )
319 m_bSaveDone = true;
320 if (m_xSession)
321 m_xSession->saveDone();
325 sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException, std::exception )
327 return m_xSession && m_xSession->cancelShutdown();
330 // service implementation
332 OUString SAL_CALL vcl_session_getImplementationName()
334 return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
337 Sequence< OUString > SAL_CALL vcl_session_getSupportedServiceNames()
339 Sequence< OUString > aRet(1);
340 aRet[0] = "com.sun.star.frame.SessionManagerClient";
341 return aRet;
344 css::uno::Reference< XInterface > SAL_CALL vcl_session_createInstance( SAL_UNUSED_PARAMETER const css::uno::Reference< XMultiServiceFactory > & )
346 return static_cast< cppu::OWeakObject * >(new VCLSession);
349 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */