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 <services/sessionlistener.hxx>
21 : #include <services/desktop.hxx>
22 : #include <threadhelp/readguard.hxx>
23 : #include <threadhelp/resetableguard.hxx>
24 : #include <protocols.h>
25 : #include <services.h>
26 :
27 : #include <osl/thread.h>
28 :
29 : #include <vcl/svapp.hxx>
30 : #include <unotools/tempfile.hxx>
31 : #include <com/sun/star/lang/XSingleServiceFactory.hpp>
32 : #include <com/sun/star/lang/XComponent.hpp>
33 : #include <com/sun/star/container/XNameAccess.hpp>
34 : #include <com/sun/star/container/XNameContainer.hpp>
35 : #include <com/sun/star/beans/NamedValue.hpp>
36 : #include <com/sun/star/beans/PropertyValue.hpp>
37 : #include <com/sun/star/beans/PropertyState.hpp>
38 : #include <com/sun/star/beans/XPropertySet.hpp>
39 : #include <com/sun/star/frame/AutoRecovery.hpp>
40 : #include <com/sun/star/frame/XFramesSupplier.hpp>
41 : #include <com/sun/star/frame/XStorable.hpp>
42 : #include <com/sun/star/frame/XComponentLoader.hpp>
43 : #include <com/sun/star/frame/XDispatch.hpp>
44 : #include <com/sun/star/frame/Desktop.hpp>
45 : #include <com/sun/star/util/XModifiable.hpp>
46 : #include <com/sun/star/util/XChangesBatch.hpp>
47 : #include <com/sun/star/util/URLTransformer.hpp>
48 : #include <com/sun/star/util/XURLTransformer.hpp>
49 : #include <com/sun/star/util/URL.hpp>
50 : #include <osl/time.h>
51 : #include <comphelper/processfactory.hxx>
52 : #include <unotools/pathoptions.hxx>
53 : #include <stdio.h>
54 :
55 : #include <com/sun/star/uno/Any.hxx>
56 :
57 : #include <com/sun/star/uno/Sequence.hxx>
58 :
59 : using namespace com::sun::star::uno;
60 : using namespace com::sun::star::util;
61 : using namespace com::sun::star::frame;
62 : using namespace com::sun::star::lang;
63 : using namespace com::sun::star::beans;
64 : using namespace com::sun::star::container;
65 :
66 :
67 : namespace framework{
68 :
69 : //***********************************************
70 : // XInterface, XTypeProvider, XServiceInfo
71 :
72 0 : DEFINE_XINTERFACE_6(
73 : SessionListener,
74 : OWeakObject,
75 : DIRECT_INTERFACE(css::lang::XTypeProvider),
76 : DIRECT_INTERFACE(css::lang::XInitialization),
77 : DIRECT_INTERFACE(css::frame::XSessionManagerListener),
78 : DIRECT_INTERFACE(css::frame::XSessionManagerListener2),
79 : DIRECT_INTERFACE(css::frame::XStatusListener),
80 : DIRECT_INTERFACE(css::lang::XServiceInfo))
81 :
82 0 : DEFINE_XTYPEPROVIDER_5(
83 : SessionListener,
84 : css::lang::XTypeProvider,
85 : css::lang::XInitialization,
86 : css::frame::XSessionManagerListener2,
87 : css::frame::XStatusListener,
88 : css::lang::XServiceInfo)
89 :
90 941 : DEFINE_XSERVICEINFO_ONEINSTANCESERVICE_2(
91 : SessionListener,
92 : cppu::OWeakObject,
93 : "com.sun.star.frame.SessionListener",
94 : IMPLEMENTATIONNAME_SESSIONLISTENER)
95 :
96 0 : DEFINE_INIT_SERVICE(SessionListener,
97 : {
98 : /* Add special code for initialization here, if you have to use your own instance
99 : during your ctor is still in progress! */
100 : }
101 : )
102 :
103 0 : SessionListener::SessionListener(const css::uno::Reference< css::uno::XComponentContext >& rxContext )
104 0 : : ThreadHelpBase (&Application::GetSolarMutex())
105 : , OWeakObject ( )
106 : , m_xContext (rxContext )
107 : , m_bRestored( sal_False )
108 : , m_bSessionStoreRequested( sal_False )
109 : , m_bAllowUserInteractionOnQuit( sal_False )
110 0 : , m_bTerminated( sal_False )
111 : {
112 : SAL_INFO("fwk.session", "SessionListener::SessionListener");
113 0 : }
114 :
115 0 : SessionListener::~SessionListener()
116 : {
117 : SAL_INFO("fwk.session", "SessionListener::~SessionListener");
118 0 : if (m_rSessionManager.is())
119 : {
120 0 : css::uno::Reference< XSessionManagerListener> me(this);
121 0 : m_rSessionManager->removeSessionManagerListener(me);
122 : }
123 0 : }
124 :
125 0 : void SessionListener::StoreSession( sal_Bool bAsync )
126 : {
127 : SAL_INFO("fwk.session", "SessionListener::StoreSession");
128 0 : ResetableGuard aGuard(m_aLock);
129 : try
130 : {
131 : // xd create SERVICENAME_AUTORECOVERY -> XDispatch
132 : // xd->dispatch("vnd.sun.star.autorecovery:/doSessionSave, async=bAsync
133 : // on stop event m_rSessionManager->saveDone(this); in case of asynchronous call
134 : // in case of synchronous call the caller should do saveDone() call himself!
135 :
136 0 : css::uno::Reference< XDispatch > xDispatch = css::frame::AutoRecovery::create( m_xContext );
137 0 : css::uno::Reference< XURLTransformer > xURLTransformer = URLTransformer::create( m_xContext );
138 0 : URL aURL;
139 0 : aURL.Complete = OUString("vnd.sun.star.autorecovery:/doSessionSave");
140 0 : xURLTransformer->parseStrict(aURL);
141 :
142 : // in case of asynchronous call the notification will trigger saveDone()
143 0 : if ( bAsync )
144 0 : xDispatch->addStatusListener(this, aURL);
145 :
146 0 : Sequence< PropertyValue > args(1);
147 0 : args[0] = PropertyValue(OUString("DispatchAsynchron"),-1,makeAny(bAsync),PropertyState_DIRECT_VALUE);
148 0 : xDispatch->dispatch(aURL, args);
149 0 : } catch (const com::sun::star::uno::Exception& e) {
150 : SAL_WARN("fwk.session",e.Message);
151 : // save failed, but tell manager to go on if we havent yet dispatched the request
152 : // in case of synchronous saving the notification is done by the caller
153 0 : if ( bAsync && m_rSessionManager.is() )
154 0 : m_rSessionManager->saveDone(this);
155 0 : }
156 0 : }
157 :
158 0 : void SessionListener::QuitSessionQuietly()
159 : {
160 : SAL_INFO("fwk.session", "SessionListener::QuitSessionQuietly");
161 0 : ResetableGuard aGuard(m_aLock);
162 : try
163 : {
164 : // xd create SERVICENAME_AUTORECOVERY -> XDispatch
165 : // xd->dispatch("vnd.sun.star.autorecovery:/doSessionQuietQuit, async=false
166 : // it is done synchronously to avoid conflict with normal quit process
167 :
168 0 : css::uno::Reference< XDispatch > xDispatch = css::frame::AutoRecovery::create( m_xContext );
169 0 : css::uno::Reference< XURLTransformer > xURLTransformer = URLTransformer::create( m_xContext );
170 0 : URL aURL;
171 0 : aURL.Complete = OUString("vnd.sun.star.autorecovery:/doSessionQuietQuit");
172 0 : xURLTransformer->parseStrict(aURL);
173 :
174 0 : Sequence< PropertyValue > args(1);
175 0 : args[0] = PropertyValue(OUString("DispatchAsynchron"),-1,makeAny(sal_False),PropertyState_DIRECT_VALUE);
176 0 : xDispatch->dispatch(aURL, args);
177 0 : } catch (const com::sun::star::uno::Exception& e) {
178 : SAL_WARN("fwk.session",e.Message);
179 0 : }
180 0 : }
181 :
182 0 : void SAL_CALL SessionListener::disposing(const com::sun::star::lang::EventObject&) throw (RuntimeException)
183 : {
184 : SAL_INFO("fwk.session", "SessionListener::disposing");
185 0 : }
186 :
187 0 : void SAL_CALL SessionListener::initialize(const Sequence< Any >& args)
188 : throw (RuntimeException)
189 : {
190 : SAL_INFO("fwk.session", "SessionListener::initialize");
191 :
192 0 : OUString aSMgr("com.sun.star.frame.SessionManagerClient");
193 0 : if ( (args.getLength() == 1) && (args[0] >>= m_bAllowUserInteractionOnQuit) )
194 : ;// do nothing
195 0 : else if (args.getLength() > 0)
196 : {
197 0 : NamedValue v;
198 0 : for (int i = 0; i < args.getLength(); i++)
199 : {
200 0 : if (args[i] >>= v)
201 : {
202 0 : if ( v.Name == "SessionManagerName" )
203 0 : v.Value >>= aSMgr;
204 0 : else if ( v.Name == "SessionManager" )
205 0 : v.Value >>= m_rSessionManager;
206 0 : else if ( v.Name == "AllowUserInteractionOnQuit" )
207 0 : v.Value >>= m_bAllowUserInteractionOnQuit;
208 : }
209 0 : }
210 : }
211 0 : if (!m_rSessionManager.is())
212 0 : m_rSessionManager = css::uno::Reference< XSessionManagerClient >
213 0 : (m_xContext->getServiceManager()->createInstanceWithContext(aSMgr, m_xContext), UNO_QUERY);
214 :
215 0 : if (m_rSessionManager.is())
216 : {
217 0 : m_rSessionManager->addSessionManagerListener(this);
218 0 : }
219 0 : }
220 :
221 0 : void SAL_CALL SessionListener::statusChanged(const FeatureStateEvent& event)
222 : throw (css::uno::RuntimeException)
223 : {
224 : SAL_INFO("fwk.session", "SessionListener::statusChanged");
225 0 : if ( event.FeatureURL.Complete == "vnd.sun.star.autorecovery:/doSessionRestore" )
226 : {
227 0 : if (event.FeatureDescriptor.compareToAscii("update")==0)
228 0 : m_bRestored = sal_True; // a document was restored
229 :
230 : }
231 0 : else if ( event.FeatureURL.Complete == "vnd.sun.star.autorecovery:/doSessionSave" )
232 : {
233 0 : if (event.FeatureDescriptor.compareToAscii("stop")==0)
234 : {
235 0 : if (m_rSessionManager.is())
236 0 : m_rSessionManager->saveDone(this); // done with save
237 : }
238 : }
239 0 : }
240 :
241 :
242 0 : sal_Bool SAL_CALL SessionListener::doRestore()
243 : throw (RuntimeException)
244 : {
245 : SAL_INFO("fwk.session", "SessionListener::doRestore");
246 0 : ResetableGuard aGuard(m_aLock);
247 0 : m_bRestored = sal_False;
248 : try {
249 0 : css::uno::Reference< XDispatch > xDispatch = css::frame::AutoRecovery::create( m_xContext );
250 :
251 0 : URL aURL;
252 0 : aURL.Complete = OUString("vnd.sun.star.autorecovery:/doSessionRestore");
253 0 : css::uno::Reference< XURLTransformer > xURLTransformer(URLTransformer::create(m_xContext));
254 0 : xURLTransformer->parseStrict(aURL);
255 0 : Sequence< PropertyValue > args;
256 0 : xDispatch->addStatusListener(this, aURL);
257 0 : xDispatch->dispatch(aURL, args);
258 0 : m_bRestored = sal_True;
259 :
260 0 : } catch (const com::sun::star::uno::Exception& e) {
261 : SAL_WARN("fwk.session",e.Message);
262 : }
263 :
264 0 : return m_bRestored;
265 : }
266 :
267 :
268 0 : void SAL_CALL SessionListener::doSave( sal_Bool bShutdown, sal_Bool /*bCancelable*/ )
269 : throw (RuntimeException)
270 : {
271 : SAL_INFO("fwk.session", "SessionListener::doSave");
272 0 : if (bShutdown)
273 : {
274 0 : m_bSessionStoreRequested = sal_True; // there is no need to protect it with mutex
275 0 : if ( m_bAllowUserInteractionOnQuit && m_rSessionManager.is() )
276 0 : m_rSessionManager->queryInteraction( static_cast< css::frame::XSessionManagerListener* >( this ) );
277 : else
278 0 : StoreSession( sal_True );
279 : }
280 : // we don't have anything to do so tell the session manager we're done
281 0 : else if( m_rSessionManager.is() )
282 0 : m_rSessionManager->saveDone( this );
283 0 : }
284 :
285 0 : void SAL_CALL SessionListener::approveInteraction( sal_Bool bInteractionGranted )
286 : throw (RuntimeException)
287 : {
288 : SAL_INFO("fwk.session", "SessionListener::approveInteraction");
289 : // do AutoSave as the first step
290 0 : ResetableGuard aGuard(m_aLock);
291 :
292 0 : if ( bInteractionGranted )
293 : {
294 : // close the office documents in normal way
295 : try
296 : {
297 : // first of all let the session be stored to be sure that we lose no information
298 0 : StoreSession( sal_False );
299 :
300 0 : css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create( m_xContext );
301 : // honestly: how many implementations of XDesktop will we ever have?
302 : // so casting this directly to the implementation
303 0 : Desktop* pDesktop(dynamic_cast<Desktop*>(xDesktop.get()));
304 0 : if(pDesktop)
305 : {
306 : SAL_INFO("fwk.session", "XDesktop is a framework::Desktop -- good.");
307 0 : m_bTerminated = pDesktop->terminateQuickstarterToo();
308 : }
309 : else
310 : {
311 : SAL_WARN("fwk.session", "XDesktop is not a framework::Desktop -- this should never happen.");
312 0 : m_bTerminated = xDesktop->terminate();
313 : }
314 :
315 0 : if ( m_rSessionManager.is() )
316 : {
317 : // false means that the application closing has been cancelled
318 0 : if ( !m_bTerminated )
319 0 : m_rSessionManager->cancelShutdown();
320 : else
321 0 : m_rSessionManager->interactionDone( this );
322 0 : }
323 : }
324 0 : catch( const css::uno::Exception& )
325 : {
326 0 : StoreSession( sal_True );
327 0 : m_rSessionManager->interactionDone( this );
328 : }
329 :
330 0 : if ( m_rSessionManager.is() )
331 0 : m_rSessionManager->saveDone(this);
332 : }
333 : else
334 : {
335 0 : StoreSession( sal_True );
336 0 : }
337 0 : }
338 :
339 0 : void SessionListener::shutdownCanceled()
340 : throw (RuntimeException)
341 : {
342 : SAL_INFO("fwk.session", "SessionListener::shutdownCanceled");
343 : // set the state back
344 0 : m_bSessionStoreRequested = sal_False; // there is no need to protect it with mutex
345 0 : }
346 :
347 0 : void SessionListener::doQuit()
348 : throw (RuntimeException)
349 : {
350 : SAL_INFO("fwk.session", "SessionListener::doQuit");
351 0 : if ( m_bSessionStoreRequested && !m_bTerminated )
352 : {
353 : // let the session be closed quietly in this case
354 0 : QuitSessionQuietly();
355 : }
356 0 : }
357 :
358 402 : }
359 :
360 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|