Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include "sal/config.h"
21 :
22 : #include <cppuhelper/basemutex.hxx>
23 : #include <cppuhelper/compbase1.hxx>
24 :
25 : #include <tools/debug.hxx>
26 :
27 : #include <vcl/svapp.hxx>
28 :
29 : #include <factory.hxx>
30 : #include <svdata.hxx>
31 : #include <salinst.hxx>
32 : #include <salsession.hxx>
33 :
34 : #include <com/sun/star/frame/XSessionManagerClient.hpp>
35 : #include <com/sun/star/lang/XMultiServiceFactory.hpp>
36 : #include <com/sun/star/frame/XSessionManagerListener2.hpp>
37 :
38 : #include <list>
39 :
40 : using namespace com::sun::star::uno;
41 : using namespace com::sun::star::lang;
42 : using namespace com::sun::star::frame;
43 :
44 0 : SalSession::~SalSession()
45 : {
46 0 : }
47 :
48 : class VCLSession:
49 : private cppu::BaseMutex,
50 : public cppu::WeakComponentImplHelper1 < XSessionManagerClient >
51 : {
52 0 : struct Listener
53 : {
54 : css::uno::Reference< XSessionManagerListener > m_xListener;
55 : bool m_bInteractionRequested;
56 : bool m_bInteractionDone;
57 : bool m_bSaveDone;
58 :
59 0 : explicit Listener( const css::uno::Reference< XSessionManagerListener >& xListener )
60 : : m_xListener( xListener ),
61 : m_bInteractionRequested( false ),
62 : m_bInteractionDone( false ),
63 0 : m_bSaveDone( false )
64 0 : {}
65 : };
66 :
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;
73 :
74 : static void SalSessionEventProc( void* pData, SalSessionEvent* pEvent );
75 :
76 0 : virtual ~VCLSession() {}
77 :
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;
84 :
85 : void callSaveRequested( bool bShutdown, bool bCancelable );
86 : void callShutdownCancelled();
87 : void callInteractionGranted( bool bGranted );
88 : void callQuit();
89 :
90 : public:
91 : VCLSession();
92 : };
93 :
94 0 : VCLSession::VCLSession()
95 : : cppu::WeakComponentImplHelper1< XSessionManagerClient >( m_aMutex ),
96 0 : m_xSession( ImplGetSVData()->mpDefInst->CreateSalSession() ),
97 : m_bInteractionRequested( false ),
98 : m_bInteractionGranted( false ),
99 : m_bInteractionDone( false ),
100 0 : m_bSaveDone( false )
101 : {
102 0 : if (m_xSession)
103 0 : m_xSession->SetCallback( SalSessionEventProc, this );
104 0 : }
105 :
106 0 : void VCLSession::callSaveRequested( bool bShutdown, bool bCancelable )
107 : {
108 0 : std::list< Listener > aListeners;
109 : {
110 0 : osl::MutexGuard aGuard( m_aMutex );
111 : // reset listener states
112 0 : for( std::list< Listener >::iterator it = m_aListeners.begin();
113 0 : it != m_aListeners.end(); ++it )
114 : {
115 0 : it->m_bSaveDone = it->m_bInteractionRequested = it->m_bInteractionDone = false;
116 : }
117 :
118 : // copy listener list since calling a listener may remove it.
119 0 : aListeners = m_aListeners;
120 : // set back interaction state
121 0 : m_bSaveDone = false;
122 0 : m_bInteractionDone = false;
123 : // without session we assume UI is always possible,
124 : // so it was reqeusted and granted
125 0 : m_bInteractionRequested = m_bInteractionGranted = !m_xSession;
126 :
127 : // answer the session manager even if no listeners available anymore
128 : DBG_ASSERT( ! aListeners.empty(), "saveRequested but no listeners !" );
129 0 : if( aListeners.empty() )
130 : {
131 0 : if (m_xSession)
132 0 : m_xSession->saveDone();
133 0 : return;
134 0 : }
135 : }
136 :
137 0 : SolarMutexReleaser aReleaser;
138 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
139 0 : it->m_xListener->doSave( bShutdown, bCancelable );
140 : }
141 :
142 0 : void VCLSession::callInteractionGranted( bool bInteractionGranted )
143 : {
144 0 : std::list< Listener > aListeners;
145 : {
146 0 : osl::MutexGuard aGuard( m_aMutex );
147 : // copy listener list since calling a listener may remove it.
148 0 : for( std::list< Listener >::const_iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
149 0 : if( it->m_bInteractionRequested )
150 0 : aListeners.push_back( *it );
151 :
152 0 : m_bInteractionGranted = bInteractionGranted;
153 :
154 : // answer the session manager even if no listeners available anymore
155 : DBG_ASSERT( ! aListeners.empty(), "interactionGranted but no listeners !" );
156 0 : if( aListeners.empty() )
157 : {
158 0 : if (m_xSession)
159 0 : m_xSession->interactionDone();
160 0 : return;
161 0 : }
162 : }
163 :
164 0 : SolarMutexReleaser aReleaser;
165 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
166 0 : it->m_xListener->approveInteraction( bInteractionGranted );
167 : }
168 :
169 0 : void VCLSession::callShutdownCancelled()
170 : {
171 0 : std::list< Listener > aListeners;
172 : {
173 0 : osl::MutexGuard aGuard( m_aMutex );
174 : // copy listener list since calling a listener may remove it.
175 0 : aListeners = m_aListeners;
176 : // set back interaction state
177 0 : m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
178 : }
179 :
180 0 : SolarMutexReleaser aReleaser;
181 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
182 0 : it->m_xListener->shutdownCanceled();
183 0 : }
184 :
185 0 : void VCLSession::callQuit()
186 : {
187 0 : std::list< Listener > aListeners;
188 : {
189 0 : osl::MutexGuard aGuard( m_aMutex );
190 : // copy listener list since calling a listener may remove it.
191 0 : aListeners = m_aListeners;
192 : // set back interaction state
193 0 : m_bInteractionRequested = m_bInteractionDone = m_bInteractionGranted = false;
194 : }
195 :
196 0 : SolarMutexReleaser aReleaser;
197 0 : for( std::list< Listener >::const_iterator it = aListeners.begin(); it != aListeners.end(); ++it )
198 : {
199 0 : css::uno::Reference< XSessionManagerListener2 > xListener2( it->m_xListener, UNO_QUERY );
200 0 : if( xListener2.is() )
201 0 : xListener2->doQuit();
202 0 : }
203 0 : }
204 :
205 0 : void VCLSession::SalSessionEventProc( void* pData, SalSessionEvent* pEvent )
206 : {
207 0 : VCLSession * pThis = static_cast< VCLSession * >( pData );
208 0 : switch( pEvent->m_eType )
209 : {
210 : case Interaction:
211 : {
212 0 : SalSessionInteractionEvent* pIEv = static_cast<SalSessionInteractionEvent*>(pEvent);
213 0 : pThis->callInteractionGranted( pIEv->m_bInteractionGranted );
214 : }
215 0 : break;
216 : case SaveRequest:
217 : {
218 0 : SalSessionSaveRequestEvent* pSEv = static_cast<SalSessionSaveRequestEvent*>(pEvent);
219 0 : pThis->callSaveRequested( pSEv->m_bShutdown, pSEv->m_bCancelable );
220 : }
221 0 : break;
222 : case ShutdownCancel:
223 0 : pThis->callShutdownCancelled();
224 0 : break;
225 : case Quit:
226 0 : pThis->callQuit();
227 0 : break;
228 : }
229 0 : }
230 :
231 0 : void SAL_CALL VCLSession::addSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
232 : {
233 0 : osl::MutexGuard aGuard( m_aMutex );
234 :
235 0 : m_aListeners.push_back( Listener( xListener ) );
236 0 : }
237 :
238 0 : void SAL_CALL VCLSession::removeSessionManagerListener( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
239 : {
240 0 : osl::MutexGuard aGuard( m_aMutex );
241 :
242 0 : std::list< Listener >::iterator it = m_aListeners.begin();
243 0 : while( it != m_aListeners.end() )
244 : {
245 0 : if( it->m_xListener == xListener )
246 : {
247 0 : it = m_aListeners.erase(it);
248 : }
249 : else
250 0 : ++it;
251 0 : }
252 0 : }
253 :
254 0 : void SAL_CALL VCLSession::queryInteraction( const css::uno::Reference<XSessionManagerListener>& xListener ) throw( RuntimeException, std::exception )
255 : {
256 0 : if( m_bInteractionGranted )
257 : {
258 0 : if( m_bInteractionDone )
259 0 : xListener->approveInteraction( false );
260 : else
261 0 : xListener->approveInteraction( true );
262 0 : return;
263 : }
264 :
265 0 : osl::MutexGuard aGuard( m_aMutex );
266 0 : if( ! m_bInteractionRequested )
267 : {
268 0 : m_xSession->queryInteraction();
269 0 : m_bInteractionRequested = true;
270 : }
271 0 : for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
272 : {
273 0 : if( it->m_xListener == xListener )
274 : {
275 0 : it->m_bInteractionRequested = true;
276 0 : it->m_bInteractionDone = false;
277 : }
278 0 : }
279 : }
280 :
281 0 : void SAL_CALL VCLSession::interactionDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception )
282 : {
283 0 : osl::MutexGuard aGuard( m_aMutex );
284 0 : int nRequested = 0, nDone = 0;
285 0 : for( std::list< Listener >::iterator it = m_aListeners.begin(); it != m_aListeners.end(); ++it )
286 : {
287 0 : if( it->m_bInteractionRequested )
288 : {
289 0 : nRequested++;
290 0 : if( xListener == it->m_xListener )
291 0 : it->m_bInteractionDone = true;
292 : }
293 0 : if( it->m_bInteractionDone )
294 0 : nDone++;
295 : }
296 0 : if( nDone == nRequested && nDone > 0 )
297 : {
298 0 : m_bInteractionDone = true;
299 0 : if (m_xSession)
300 0 : m_xSession->interactionDone();
301 0 : }
302 0 : }
303 :
304 0 : void SAL_CALL VCLSession::saveDone( const css::uno::Reference< XSessionManagerListener >& xListener ) throw( RuntimeException, std::exception )
305 : {
306 0 : osl::MutexGuard aGuard( m_aMutex );
307 :
308 0 : bool bSaveDone = true;
309 0 : for( std::list< Listener >::iterator it = m_aListeners.begin();
310 0 : it != m_aListeners.end(); ++it )
311 : {
312 0 : if( it->m_xListener == xListener )
313 0 : it->m_bSaveDone = true;
314 0 : if( ! it->m_bSaveDone )
315 0 : bSaveDone = false;
316 : }
317 0 : if( bSaveDone )
318 : {
319 0 : m_bSaveDone = true;
320 0 : if (m_xSession)
321 0 : m_xSession->saveDone();
322 0 : }
323 0 : }
324 :
325 0 : sal_Bool SAL_CALL VCLSession::cancelShutdown() throw( RuntimeException, std::exception )
326 : {
327 0 : return m_xSession && m_xSession->cancelShutdown();
328 : }
329 :
330 : // service implementation
331 :
332 179 : OUString SAL_CALL vcl_session_getImplementationName()
333 : {
334 179 : return OUString( "com.sun.star.frame.VCLSessionManagerClient" );
335 : }
336 :
337 0 : Sequence< OUString > SAL_CALL vcl_session_getSupportedServiceNames()
338 : {
339 0 : Sequence< OUString > aRet(1);
340 0 : aRet[0] = "com.sun.star.frame.SessionManagerClient";
341 0 : return aRet;
342 : }
343 :
344 0 : css::uno::Reference< XInterface > SAL_CALL vcl_session_createInstance( SAL_UNUSED_PARAMETER const css::uno::Reference< XMultiServiceFactory > & )
345 : {
346 0 : return static_cast< cppu::OWeakObject * >(new VCLSession);
347 : }
348 :
349 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|