1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 .
21 #include "sal/config.h"
23 #include <cppuhelper/basemutex.hxx>
24 #include <cppuhelper/compbase.hxx>
27 #include <vcl/svapp.hxx>
29 #include <factory.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>
40 using namespace com::sun::star::uno
;
41 using namespace com::sun::star::lang
;
42 using namespace com::sun::star::frame
;
44 SalSession::~SalSession()
49 private cppu::BaseMutex
,
50 public cppu::WeakComponentImplHelper
< XSessionManagerClient
>
54 css::uno::Reference
< XSessionManagerListener
> m_xListener
;
55 bool m_bInteractionRequested
;
56 bool m_bInteractionDone
;
59 explicit Listener( const css::uno::Reference
< XSessionManagerListener
>& xListener
)
60 : m_xListener( xListener
),
61 m_bInteractionRequested( false ),
62 m_bInteractionDone( 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
;
74 static void SalSessionEventProc( void* pData
, SalSessionEvent
* pEvent
);
76 virtual ~VCLSession() override
{}
78 virtual void SAL_CALL
addSessionManagerListener( const css::uno::Reference
< XSessionManagerListener
>& xListener
) override
;
79 virtual void SAL_CALL
removeSessionManagerListener( const css::uno::Reference
< XSessionManagerListener
>& xListener
) override
;
80 virtual void SAL_CALL
queryInteraction( const css::uno::Reference
< XSessionManagerListener
>& xListener
) override
;
81 virtual void SAL_CALL
interactionDone( const css::uno::Reference
< XSessionManagerListener
>& xListener
) override
;
82 virtual void SAL_CALL
saveDone( const css::uno::Reference
< XSessionManagerListener
>& xListener
) override
;
83 virtual sal_Bool SAL_CALL
cancelShutdown() override
;
85 void SAL_CALL
disposing() override
;
87 void callSaveRequested( bool bShutdown
);
88 void callShutdownCancelled();
89 void callInteractionGranted( bool bGranted
);
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 ),
104 SAL_INFO("vcl.se", "VCLSession::VCLSession" );
107 m_xSession
->SetCallback( SalSessionEventProc
, this );
110 void VCLSession::callSaveRequested( bool bShutdown
)
112 SAL_INFO("vcl.se", "VCLSession::callSaveRequested" );
114 std::list
< Listener
> aListeners
;
116 osl::MutexGuard
aGuard( m_aMutex
);
117 // reset listener states
118 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin();
119 it
!= m_aListeners
.end(); ++it
)
121 it
->m_bSaveDone
= it
->m_bInteractionRequested
= it
->m_bInteractionDone
= false;
124 // copy listener list since calling a listener may remove it.
125 aListeners
= m_aListeners
;
126 // set back interaction state
128 m_bInteractionDone
= false;
129 // without session we assume UI is always possible,
130 // so it was requested and granted
131 m_bInteractionRequested
= m_bInteractionGranted
= !m_xSession
;
133 // answer the session manager even if no listeners available anymore
134 SAL_WARN_IF( aListeners
.empty(), "vcl.se", "saveRequested but no listeners !" );
136 SAL_INFO("vcl.se.debug", " aListeners.empty() = " << (aListeners
.empty() ? "true" : "false") <<
137 ", bShutdown = " << (bShutdown
? "true" : "false"));
138 if( aListeners
.empty() )
141 m_xSession
->saveDone();
146 SolarMutexReleaser aReleaser
;
147 for( std::list
< Listener
>::const_iterator it
= aListeners
.begin(); it
!= aListeners
.end(); ++it
)
148 it
->m_xListener
->doSave( bShutdown
, false/*bCancelable*/ );
151 void VCLSession::callInteractionGranted( bool bInteractionGranted
)
153 SAL_INFO("vcl.se", "VCLSession::callInteractionGranted" );
155 std::list
< Listener
> aListeners
;
157 osl::MutexGuard
aGuard( m_aMutex
);
158 // copy listener list since calling a listener may remove it.
159 for( std::list
< Listener
>::const_iterator it
= m_aListeners
.begin(); it
!= m_aListeners
.end(); ++it
)
160 if( it
->m_bInteractionRequested
)
161 aListeners
.push_back( *it
);
163 m_bInteractionGranted
= bInteractionGranted
;
165 // answer the session manager even if no listeners available anymore
166 SAL_WARN_IF( aListeners
.empty(), "vcl.se", "interactionGranted but no listeners !" );
168 SAL_INFO("vcl.se.debug", " aListeners.empty() = " << (aListeners
.empty() ? "true" : "false") <<
169 ", bInteractionGranted = " << (bInteractionGranted
? "true" : "false"));
170 if( aListeners
.empty() )
173 m_xSession
->interactionDone();
178 SolarMutexReleaser aReleaser
;
179 for( std::list
< Listener
>::const_iterator it
= aListeners
.begin(); it
!= aListeners
.end(); ++it
)
180 it
->m_xListener
->approveInteraction( bInteractionGranted
);
183 void VCLSession::callShutdownCancelled()
185 SAL_INFO("vcl.se", "VCLSession::callShutdownCancelled");
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
)
198 it
->m_xListener
->shutdownCanceled();
201 void VCLSession::callQuit()
203 SAL_INFO("vcl.se", "VCLSession::callQuit");
205 std::list
< Listener
> aListeners
;
207 osl::MutexGuard
aGuard( m_aMutex
);
208 // copy listener list since calling a listener may remove it.
209 aListeners
= m_aListeners
;
210 // set back interaction state
211 m_bInteractionRequested
= m_bInteractionDone
= m_bInteractionGranted
= false;
214 SolarMutexReleaser aReleaser
;
215 for( std::list
< Listener
>::const_iterator it
= aListeners
.begin(); it
!= aListeners
.end(); ++it
)
217 css::uno::Reference
< XSessionManagerListener2
> xListener2( it
->m_xListener
, UNO_QUERY
);
218 if( xListener2
.is() )
219 xListener2
->doQuit();
223 void VCLSession::SalSessionEventProc( void* pData
, SalSessionEvent
* pEvent
)
225 SAL_INFO("vcl.se", "VCLSession::SalSessionEventProc");
227 VCLSession
* pThis
= static_cast< VCLSession
* >( pData
);
228 switch( pEvent
->m_eType
)
232 SAL_INFO("vcl.se.debug", " EventProcType = Interaction");
233 SalSessionInteractionEvent
* pIEv
= static_cast<SalSessionInteractionEvent
*>(pEvent
);
234 pThis
->callInteractionGranted( pIEv
->m_bInteractionGranted
);
239 SAL_INFO("vcl.se.debug", " EventProcType = SaveRequest");
240 SalSessionSaveRequestEvent
* pSEv
= static_cast<SalSessionSaveRequestEvent
*>(pEvent
);
241 pThis
->callSaveRequested( pSEv
->m_bShutdown
);
245 SAL_INFO("vcl.se.debug", " EventProcType = ShutdownCancel");
246 pThis
->callShutdownCancelled();
249 SAL_INFO("vcl.se.debug", " EventProcType = Quit");
255 void SAL_CALL
VCLSession::addSessionManagerListener( const css::uno::Reference
<XSessionManagerListener
>& xListener
)
257 SAL_INFO("vcl.se", "VCLSession::addSessionManagerListener" );
259 osl::MutexGuard
aGuard( m_aMutex
);
261 SAL_INFO("vcl.se.debug", " m_aListeners.size() = " << m_aListeners
.size() );
262 m_aListeners
.push_back( Listener( xListener
) );
265 void SAL_CALL
VCLSession::removeSessionManagerListener( const css::uno::Reference
<XSessionManagerListener
>& xListener
)
267 SAL_INFO("vcl.se", "VCLSession::removeSessionManagerListener" );
269 osl::MutexGuard
aGuard( m_aMutex
);
271 std::list
< Listener
>::iterator it
= m_aListeners
.begin();
273 SAL_INFO("vcl.se.debug", " m_aListeners.size() = " << m_aListeners
.size() );
274 while( it
!= m_aListeners
.end() )
276 if( it
->m_xListener
== xListener
)
278 it
= m_aListeners
.erase(it
);
285 void SAL_CALL
VCLSession::queryInteraction( const css::uno::Reference
<XSessionManagerListener
>& xListener
)
287 SAL_INFO("vcl.se", "VCLSession::queryInteraction");
289 SAL_INFO("vcl.se.debug", " m_bInteractionGranted = " << (m_bInteractionGranted
? "true" : "false") <<
290 ", m_bInteractionRequested = "<< (m_bInteractionRequested
? "true" : "false"));
291 if( m_bInteractionGranted
)
293 if( m_bInteractionDone
)
294 xListener
->approveInteraction( false );
296 xListener
->approveInteraction( true );
300 osl::MutexGuard
aGuard( m_aMutex
);
301 if( ! m_bInteractionRequested
)
303 m_xSession
->queryInteraction();
304 m_bInteractionRequested
= true;
306 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin(); it
!= m_aListeners
.end(); ++it
)
308 if( it
->m_xListener
== xListener
)
310 SAL_INFO("vcl.se.debug", " it->m_xListener == xListener");
311 it
->m_bInteractionRequested
= true;
312 it
->m_bInteractionDone
= false;
317 void SAL_CALL
VCLSession::interactionDone( const css::uno::Reference
< XSessionManagerListener
>& xListener
)
319 SAL_INFO("vcl.se", "VCLSession::interactionDone");
321 osl::MutexGuard
aGuard( m_aMutex
);
322 int nRequested
= 0, nDone
= 0;
323 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin(); it
!= m_aListeners
.end(); ++it
)
325 if( it
->m_bInteractionRequested
)
328 if( xListener
== it
->m_xListener
)
329 it
->m_bInteractionDone
= true;
331 if( it
->m_bInteractionDone
)
335 SAL_INFO("vcl.se.debug", " nDone = " << nDone
<<
336 ", nRequested =" << nRequested
);
337 if( nDone
== nRequested
&& nDone
> 0 )
339 m_bInteractionDone
= true;
341 m_xSession
->interactionDone();
345 void SAL_CALL
VCLSession::saveDone( const css::uno::Reference
< XSessionManagerListener
>& xListener
)
347 SAL_INFO("vcl.se", "VCLSession::saveDone");
349 osl::MutexGuard
aGuard( m_aMutex
);
351 bool bSaveDone
= true;
352 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin();
353 it
!= m_aListeners
.end(); ++it
)
355 if( it
->m_xListener
== xListener
)
356 it
->m_bSaveDone
= true;
357 if( ! it
->m_bSaveDone
)
361 SAL_INFO("vcl.se.debug", " bSaveDone = " << (bSaveDone
? "true" : "false"));
363 if( bSaveDone
&& !m_bSaveDone
)
367 m_xSession
->saveDone();
371 sal_Bool SAL_CALL
VCLSession::cancelShutdown()
373 SAL_INFO("vcl.se", "VCLSession::cancelShutdown");
375 return m_xSession
&& m_xSession
->cancelShutdown();
378 void VCLSession::disposing() {
379 SAL_INFO("vcl.se", "VCLSession::disposing");
381 std::list
<Listener
> list
;
383 osl::MutexGuard
g(m_aMutex
);
384 list
.swap(m_aListeners
);
386 css::lang::EventObject
src(static_cast<OWeakObject
*>(this));
387 for (auto const & i
: list
) {
389 i
.m_xListener
->disposing(src
);
390 SAL_INFO("vcl.se.debug", " call Listener disposing");
391 } catch (css::uno::RuntimeException
& e
) {
392 SAL_WARN("vcl.se", "ignoring RuntimeException " << e
.Message
);
397 // service implementation
399 OUString SAL_CALL
vcl_session_getImplementationName()
401 SAL_INFO("vcl.se", "vcl_session_getImplementationName");
403 return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
406 Sequence
< OUString
> SAL_CALL
vcl_session_getSupportedServiceNames()
408 SAL_INFO("vcl.se", "vcl_session_getSupportedServiceNames");
410 Sequence
< OUString
> aRet
{ "com.sun.star.frame.SessionManagerClient" };
414 css::uno::Reference
< XInterface
> SAL_CALL
vcl_session_createInstance( SAL_UNUSED_PARAMETER
const css::uno::Reference
< XMultiServiceFactory
> & )
416 SAL_INFO("vcl.se", "vcl_session_createInstance");
418 return static_cast< cppu::OWeakObject
* >(new VCLSession
);
421 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */