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