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 <boost/scoped_ptr.hpp>
23 #include <cppuhelper/compbase1.hxx>
25 #include <tools/debug.hxx>
27 #include <vcl/svapp.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>
39 using namespace com::sun::star::uno
;
40 using namespace com::sun::star::lang
;
41 using namespace com::sun::star::frame
;
44 SalSession::~SalSession()
50 public cppu::WeakComponentImplHelper1
< XSessionManagerClient
>
54 css::uno::Reference
< XSessionManagerListener
> m_xListener
;
55 bool m_bInteractionRequested
;
56 bool m_bInteractionDone
;
59 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 boost::scoped_ptr
< SalSession
> m_pSession
;
69 bool m_bInteractionRequested
;
70 bool m_bInteractionGranted
;
71 bool m_bInteractionDone
;
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
);
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 ),
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
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() )
132 m_pSession
->saveDone();
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() )
160 m_pSession
->interactionDone();
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
)
217 SalSessionInteractionEvent
* pIEv
= static_cast<SalSessionInteractionEvent
*>(pEvent
);
218 pThis
->callInteractionGranted( pIEv
->m_bInteractionGranted
);
223 SalSessionSaveRequestEvent
* pSEv
= static_cast<SalSessionSaveRequestEvent
*>(pEvent
);
224 pThis
->callSaveRequested( pSEv
->m_bShutdown
, pSEv
->m_bCancelable
);
228 pThis
->callShutdownCancelled();
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 it
= m_aListeners
.erase(it
);
259 void SAL_CALL
VCLSession::queryInteraction( const css::uno::Reference
<XSessionManagerListener
>& xListener
) throw( RuntimeException
)
261 if( m_bInteractionGranted
)
263 if( m_bInteractionDone
)
264 xListener
->approveInteraction( false );
266 xListener
->approveInteraction( true );
270 osl::MutexGuard
aGuard( *this );
271 if( ! m_bInteractionRequested
)
273 m_pSession
->queryInteraction();
274 m_bInteractionRequested
= true;
276 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin(); it
!= m_aListeners
.end(); ++it
)
278 if( it
->m_xListener
== xListener
)
280 it
->m_bInteractionRequested
= true;
281 it
->m_bInteractionDone
= false;
286 void SAL_CALL
VCLSession::interactionDone( const css::uno::Reference
< XSessionManagerListener
>& xListener
) throw( RuntimeException
)
288 osl::MutexGuard
aGuard( *this );
289 int nRequested
= 0, nDone
= 0;
290 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin(); it
!= m_aListeners
.end(); ++it
)
292 if( it
->m_bInteractionRequested
)
295 if( xListener
== it
->m_xListener
)
296 it
->m_bInteractionDone
= true;
298 if( it
->m_bInteractionDone
)
301 if( nDone
== nRequested
&& nDone
> 0 )
303 m_bInteractionDone
= true;
305 m_pSession
->interactionDone();
309 void SAL_CALL
VCLSession::saveDone( const css::uno::Reference
< XSessionManagerListener
>& xListener
) throw( RuntimeException
)
311 osl::MutexGuard
aGuard( *this );
313 bool bSaveDone
= true;
314 for( std::list
< Listener
>::iterator it
= m_aListeners
.begin();
315 it
!= m_aListeners
.end(); ++it
)
317 if( it
->m_xListener
== xListener
)
318 it
->m_bSaveDone
= true;
319 if( ! it
->m_bSaveDone
)
326 m_pSession
->saveDone();
330 sal_Bool SAL_CALL
VCLSession::cancelShutdown() throw( RuntimeException
)
332 return m_pSession
&& m_pSession
->cancelShutdown();
335 // service implementation
337 OUString SAL_CALL
vcl_session_getImplementationName()
339 return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
342 Sequence
< OUString
> SAL_CALL
vcl_session_getSupportedServiceNames()
344 Sequence
< OUString
> aRet(1);
345 aRet
[0] = "com.sun.star.frame.SessionManagerClient";
349 css::uno::Reference
< XInterface
> SAL_CALL
vcl_session_createInstance( SAL_UNUSED_PARAMETER
const css::uno::Reference
< XMultiServiceFactory
> & )
351 return static_cast< cppu::OWeakObject
* >(new VCLSession
);
354 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */