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