build fix: no comphelper/profilezone.hxx in this branch
[LibreOffice.git] / vcl / source / app / session.cxx
blob483ab6ef0691e439369b53d86a5a500a7d2fc14e
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/compbase.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::WeakComponentImplHelper < XSessionManagerClient >
52 struct Listener
54 css::uno::Reference< XSessionManagerListener > m_xListener;
55 bool m_bInteractionRequested;
56 bool m_bInteractionDone;
57 bool m_bSaveDone;
59 explicit 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() override {}
78 virtual void SAL_CALL addSessionManagerListener( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) override;
79 virtual void SAL_CALL removeSessionManagerListener( const css::uno::Reference< XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception ) override;
80 virtual void SAL_CALL queryInteraction( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) override;
81 virtual void SAL_CALL interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) override;
82 virtual void SAL_CALL saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception ) override;
83 virtual sal_Bool SAL_CALL cancelShutdown() throw( RuntimeException, std::exception ) override;
85 void SAL_CALL disposing() override;
87 void callSaveRequested( bool bShutdown );
88 void callShutdownCancelled();
89 void callInteractionGranted( bool bGranted );
90 void callQuit();
92 public:
93 VCLSession();
96 VCLSession::VCLSession()
97 : cppu::WeakComponentImplHelper< XSessionManagerClient >( m_aMutex ),
98 m_xSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
99 m_bInteractionRequested( false ),
100 m_bInteractionGranted( false ),
101 m_bInteractionDone( false ),
102 m_bSaveDone( false )
104 if (m_xSession)
105 m_xSession->SetCallback( SalSessionEventProc, this );
108 void VCLSession::callSaveRequested( bool bShutdown )
110 std::list< Listener > aListeners;
112 osl::MutexGuard aGuard( m_aMutex );
113 // reset listener states
114 for( std::list< Listener >::iterator it = m_aListeners.begin();
115 it != m_aListeners.end(); ++it )
117 it->m_bSaveDone = it->m_bInteractionRequested = it->m_bInteractionDone = false;
120 // copy listener list since calling a listener may remove it.
121 aListeners = m_aListeners;
122 // set back interaction state
123 m_bSaveDone = false;
124 m_bInteractionDone = false;
125 // without session we assume UI is always possible,
126 // so it was reqeusted and granted
127 m_bInteractionRequested = m_bInteractionGranted = !m_xSession;
129 // answer the session manager even if no listeners available anymore
130 SAL_WARN_IF( aListeners.empty(), "vcl", "saveRequested but no listeners !" );
131 if( aListeners.empty() )
133 if (m_xSession)
134 m_xSession->saveDone();
135 return;
139 SolarMutexReleaser aReleaser;
140 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
141 it->m_xListener->doSave( bShutdown, false/*bCancelable*/ );
144 void VCLSession::callInteractionGranted( bool bInteractionGranted )
146 std::list< Listener > aListeners;
148 osl::MutexGuard aGuard( m_aMutex );
149 // copy listener list since calling a listener may remove it.
150 for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
151 if( it->m_bInteractionRequested )
152 aListeners.push_back( *it );
154 m_bInteractionGranted = bInteractionGranted;
156 // answer the session manager even if no listeners available anymore
157 SAL_WARN_IF( aListeners.empty(), "vcl", "interactionGranted but no listeners !" );
158 if( aListeners.empty() )
160 if (m_xSession)
161 m_xSession->interactionDone();
162 return;
166 SolarMutexReleaser aReleaser;
167 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
168 it->m_xListener->approveInteraction( bInteractionGranted );
171 void VCLSession::callShutdownCancelled()
173 std::list< Listener > aListeners;
175 osl::MutexGuard aGuard( m_aMutex );
176 // copy listener list since calling a listener may remove it.
177 aListeners = m_aListeners;
178 // set back interaction state
179 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
182 SolarMutexReleaser aReleaser;
183 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
184 it->m_xListener->shutdownCanceled();
187 void VCLSession::callQuit()
189 std::list< Listener > aListeners;
191 osl::MutexGuard aGuard( m_aMutex );
192 // copy listener list since calling a listener may remove it.
193 aListeners = m_aListeners;
194 // set back interaction state
195 m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
198 SolarMutexReleaser aReleaser;
199 for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
201 css::uno::Reference< XSessionManagerListener2 > xListener2( it->m_xListener, UNO_QUERY );
202 if( xListener2.is() )
203 xListener2->doQuit();
207 void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
209 VCLSession * pThis = static_cast< VCLSession * >( pData );
210 switch( pEvent->m_eType )
212 case Interaction:
214 SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
215 pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
217 break;
218 case SaveRequest:
220 SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
221 pThis->callSaveRequested( pSEv->m_bShutdown );
223 break;
224 case ShutdownCancel:
225 pThis->callShutdownCancelled();
226 break;
227 case Quit:
228 pThis->callQuit();
229 break;
233 void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
235 osl::MutexGuard aGuard( m_aMutex );
237 m_aListeners.push_back( Listener( xListener ) );
240 void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
242 osl::MutexGuard aGuard( m_aMutex );
244 std::list< Listener >::iterator it = m_aListeners.begin();
245 while( it != m_aListeners.end() )
247 if( it->m_xListener == xListener )
249 it = m_aListeners.erase(it);
251 else
252 ++it;
256 void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
258 if( m_bInteractionGranted )
260 if( m_bInteractionDone )
261 xListener->approveInteraction( false );
262 else
263 xListener->approveInteraction( true );
264 return;
267 osl::MutexGuard aGuard( m_aMutex );
268 if( ! m_bInteractionRequested )
270 m_xSession->queryInteraction();
271 m_bInteractionRequested = true;
273 for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
275 if( it->m_xListener == xListener )
277 it->m_bInteractionRequested = true;
278 it->m_bInteractionDone = false;
283 void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception )
285 osl::MutexGuard aGuard( m_aMutex );
286 int nRequested = 0, nDone = 0;
287 for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
289 if( it->m_bInteractionRequested )
291 nRequested++;
292 if( xListener == it->m_xListener )
293 it->m_bInteractionDone = true;
295 if( it->m_bInteractionDone )
296 nDone++;
298 if( nDone == nRequested && nDone > 0 )
300 m_bInteractionDone = true;
301 if (m_xSession)
302 m_xSession->interactionDone();
306 void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception )
308 osl::MutexGuard aGuard( m_aMutex );
310 bool bSaveDone = true;
311 for( std::list< Listener >::iterator it = m_aListeners.begin();
312 it != m_aListeners.end(); ++it )
314 if( it->m_xListener == xListener )
315 it->m_bSaveDone = true;
316 if( ! it->m_bSaveDone )
317 bSaveDone = false;
319 if( bSaveDone )
321 m_bSaveDone = true;
322 if (m_xSession)
323 m_xSession->saveDone();
327 sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException, std::exception )
329 return m_xSession && m_xSession->cancelShutdown();
332 void VCLSession::disposing() {
333 std::list<Listener> list;
335 osl::MutexGuard g(m_aMutex);
336 list.swap(m_aListeners);
338 css::lang::EventObject src(static_cast<OWeakObject *>(this));
339 for (auto const & i: list) {
340 try {
341 i.m_xListener->disposing(src);
342 } catch (css::uno::RuntimeException & e) {
343 SAL_WARN("vcl.app", "ignoring RuntimeException " << e.Message);
348 // service implementation
350 OUString SAL_CALL vcl_session_getImplementationName()
352 return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
355 Sequence< OUString > SAL_CALL vcl_session_getSupportedServiceNames()
357 Sequence< OUString > aRet { "com.sun.star.frame.SessionManagerClient" };
358 return aRet;
361 css::uno::Reference< XInterface > SAL_CALL vcl_session_createInstance( SAL_UNUSED_PARAMETER const css::uno::Reference< XMultiServiceFactory > & )
363 return static_cast< cppu::OWeakObject * >(new VCLSession);
366 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */