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 .
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>
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
) 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
);
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 ),
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
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() )
134 m_xSession
->saveDone();
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() )
161 m_xSession
->interactionDone();
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
)
214 SalSessionInteractionEvent
* pIEv
= static_cast<SalSessionInteractionEvent
*>(pEvent
);
215 pThis
->callInteractionGranted( pIEv
->m_bInteractionGranted
);
220 SalSessionSaveRequestEvent
* pSEv
= static_cast<SalSessionSaveRequestEvent
*>(pEvent
);
221 pThis
->callSaveRequested( pSEv
->m_bShutdown
);
225 pThis
->callShutdownCancelled();
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
);
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 );
263 xListener
->approveInteraction( true );
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
)
292 if( xListener
== it
->m_xListener
)
293 it
->m_bInteractionDone
= true;
295 if( it
->m_bInteractionDone
)
298 if( nDone
== nRequested
&& nDone
> 0 )
300 m_bInteractionDone
= true;
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
)
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
) {
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" };
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: */