26#include <com/sun/star/beans/NamedValue.hpp>
27#include <com/sun/star/beans/PropertyValue.hpp>
28#include <com/sun/star/frame/theAutoRecovery.hpp>
29#include <com/sun/star/frame/Desktop.hpp>
30#include <com/sun/star/frame/FeatureStateEvent.hpp>
31#include <com/sun/star/frame/XDispatch.hpp>
32#include <com/sun/star/frame/XSessionManagerListener2.hpp>
33#include <com/sun/star/frame/XSessionManagerClient.hpp>
34#include <com/sun/star/frame/XStatusListener.hpp>
35#include <com/sun/star/lang/EventObject.hpp>
36#include <com/sun/star/lang/XInitialization.hpp>
37#include <com/sun/star/util/URLTransformer.hpp>
38#include <com/sun/star/util/XURLTransformer.hpp>
39#include <com/sun/star/util/URL.hpp>
43#include <com/sun/star/uno/Any.hxx>
44#include <com/sun/star/uno/Sequence.hxx>
73typedef cppu::WeakImplHelper<
74 css::lang::XInitialization,
75 css::frame::XSessionManagerListener2,
76 css::frame::XStatusListener,
77 css::lang::XServiceInfo> SessionListener_BASE;
79class SessionListener :
public SessionListener_BASE
86 css::uno::Reference< css::uno::XComponentContext >
m_xContext;
88 css::uno::Reference< css::frame::XSessionManagerClient > m_rSessionManager;
93 bool m_bSessionStoreRequested;
95 bool m_bAllowUserInteractionOnQuit;
99 void StoreSession(
bool bAsync );
102 void QuitSessionQuietly();
105 explicit SessionListener(css::uno::Reference< css::uno::XComponentContext > xContext);
107 virtual ~SessionListener()
override;
111 return "com.sun.star.comp.frame.SessionListener";
121 return {
"com.sun.star.frame.SessionListener"};
124 virtual void SAL_CALL disposing(
const css::lang::EventObject&)
override;
127 virtual void SAL_CALL initialize(
const css::uno::Sequence< css::uno::Any >& args)
override;
130 virtual void SAL_CALL doSave(
sal_Bool bShutdown,
sal_Bool bCancelable )
override;
131 virtual void SAL_CALL approveInteraction(
sal_Bool bInteractionGranted )
override;
132 virtual void SAL_CALL shutdownCanceled()
override;
133 virtual sal_Bool SAL_CALL doRestore()
override;
136 virtual void SAL_CALL doQuit()
override;
139 virtual void SAL_CALL statusChanged(
const css::frame::FeatureStateEvent& event)
override;
142SessionListener::SessionListener(css::uno::Reference< css::uno::XComponentContext > rxContext )
144 , m_bRestored( false )
145 , m_bSessionStoreRequested( false )
146 , m_bAllowUserInteractionOnQuit( false )
149 SAL_INFO(
"fwk.session",
"SessionListener::SessionListener");
152SessionListener::~SessionListener()
154 SAL_INFO(
"fwk.session",
"SessionListener::~SessionListener");
155 if (m_rSessionManager.is())
157 css::uno::Reference< XSessionManagerListener> me(
this);
158 m_rSessionManager->removeSessionManagerListener(me);
162void SessionListener::StoreSession(
bool bAsync )
164 SAL_INFO(
"fwk.session",
"SessionListener::StoreSession");
173 css::uno::Reference< frame::XDispatch >
xDispatch = css::frame::theAutoRecovery::get(
m_xContext );
174 css::uno::Reference< XURLTransformer > xURLTransformer = URLTransformer::create(
m_xContext );
176 aURL.Complete =
"vnd.sun.star.autorecovery:/doSessionSave";
177 xURLTransformer->parseStrict(aURL);
181 xDispatch->addStatusListener(
this, aURL);
184 PropertyState_DIRECT_VALUE) };
186 }
catch (
const css::uno::Exception&) {
190 if ( bAsync && m_rSessionManager.is() )
191 m_rSessionManager->saveDone(
this);
195void SessionListener::QuitSessionQuietly()
197 SAL_INFO(
"fwk.session",
"SessionListener::QuitSessionQuietly");
205 css::uno::Reference< frame::XDispatch >
xDispatch = css::frame::theAutoRecovery::get(
m_xContext );
206 css::uno::Reference< XURLTransformer > xURLTransformer = URLTransformer::create(
m_xContext );
208 aURL.Complete =
"vnd.sun.star.autorecovery:/doSessionQuietQuit";
209 xURLTransformer->parseStrict(aURL);
212 PropertyState_DIRECT_VALUE) };
214 }
catch (
const css::uno::Exception&) {
219void SAL_CALL SessionListener::disposing(
const css::lang::EventObject& Source)
221 SAL_INFO(
"fwk.session",
"SessionListener::disposing");
222 if (
Source.Source == m_rSessionManager) {
223 m_rSessionManager.clear();
229 SAL_INFO(
"fwk.session",
"SessionListener::initialize");
231 OUString aSMgr(
"com.sun.star.frame.SessionManagerClient");
232 if ( (
args.getLength() == 1) && (args[0] >>= m_bAllowUserInteractionOnQuit) )
234 else if (
args.hasElements())
237 for (
const Any& rArg : args)
241 if (
v.Name ==
"SessionManagerName" )
243 else if (
v.Name ==
"SessionManager" )
244 v.Value >>= m_rSessionManager;
245 else if (
v.Name ==
"AllowUserInteractionOnQuit" )
246 v.Value >>= m_bAllowUserInteractionOnQuit;
251 SAL_INFO(
"fwk.session.debug",
" m_bAllowUserInteractionOnQuit = " << (m_bAllowUserInteractionOnQuit ?
"true" :
"false"));
252 if (!m_rSessionManager.is())
253 m_rSessionManager = css::uno::Reference< frame::XSessionManagerClient >
256 if (m_rSessionManager.is())
258 m_rSessionManager->addSessionManagerListener(
this);
262void SAL_CALL SessionListener::statusChanged(
const frame::FeatureStateEvent& event)
264 SAL_INFO(
"fwk.session",
"SessionListener::statusChanged");
266 SAL_INFO(
"fwk.session.debug",
" ev.Feature = " << event.FeatureURL.Complete <<
267 ", ev.Descript = " << event.FeatureDescriptor);
268 if ( event.FeatureURL.Complete ==
"vnd.sun.star.autorecovery:/doSessionRestore" )
270 if (event.FeatureDescriptor ==
"update")
274 else if ( event.FeatureURL.Complete ==
"vnd.sun.star.autorecovery:/doAutoSave" )
277 if (event.FeatureDescriptor ==
"update")
279 if (m_rSessionManager.is())
280 m_rSessionManager->saveDone(
this);
285sal_Bool SAL_CALL SessionListener::doRestore()
287 SAL_INFO(
"fwk.session",
"SessionListener::doRestore");
291 css::uno::Reference< frame::XDispatch >
xDispatch = css::frame::theAutoRecovery::get(
m_xContext );
294 aURL.Complete =
"vnd.sun.star.autorecovery:/doSessionRestore";
295 css::uno::Reference< XURLTransformer > xURLTransformer(URLTransformer::create(
m_xContext));
296 xURLTransformer->parseStrict(aURL);
298 xDispatch->addStatusListener(
this, aURL);
302 }
catch (
const css::uno::Exception&) {
311 SAL_INFO(
"fwk.session",
"SessionListener::doSave");
313 SAL_INFO(
"fwk.session.debug",
" m_bAllowUserInteractionOnQuit = " << (m_bAllowUserInteractionOnQuit ?
"true" :
"false") <<
314 ", bShutdown = " << (bShutdown ?
"true" :
"false"));
317 m_bSessionStoreRequested =
true;
318 if ( m_bAllowUserInteractionOnQuit && m_rSessionManager.is() )
319 m_rSessionManager->queryInteraction(
static_cast< css::frame::XSessionManagerListener*
>(
this ) );
321 StoreSession(
true );
324 else if( m_rSessionManager.is() )
325 m_rSessionManager->saveDone(
this );
328void SAL_CALL SessionListener::approveInteraction(
sal_Bool bInteractionGranted )
330 SAL_INFO(
"fwk.session",
"SessionListener::approveInteraction");
334 if ( bInteractionGranted )
340 StoreSession(
false );
342 css::uno::Reference< css::frame::XDesktop2 > xDesktop = css::frame::Desktop::create(
m_xContext );
348 SAL_INFO(
"fwk.session",
" XDesktop is a framework::Desktop -- good.");
353 SAL_WARN(
"fwk.session",
" XDesktop is not a framework::Desktop -- this should never happen.");
357 if ( m_rSessionManager.is() )
360 if ( !m_bTerminated )
361 m_rSessionManager->cancelShutdown();
363 m_rSessionManager->interactionDone(
this );
366 catch(
const css::uno::Exception& )
368 StoreSession(
true );
369 m_rSessionManager->interactionDone(
this );
372 if ( m_rSessionManager.is() && m_bTerminated )
373 m_rSessionManager->saveDone(
this);
377 StoreSession(
true );
381void SessionListener::shutdownCanceled()
383 SAL_INFO(
"fwk.session",
"SessionListener::shutdownCanceled");
385 m_bSessionStoreRequested =
false;
387 if ( m_rSessionManager.is() )
388 m_rSessionManager->saveDone(
this);
391void SessionListener::doQuit()
393 SAL_INFO(
"fwk.session",
"SessionListener::doQuit");
394 if ( m_bSessionStoreRequested && !m_bTerminated )
397 QuitSessionQuietly();
403extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
405 css::uno::XComponentContext *context,
406 css::uno::Sequence<css::uno::Any>
const &)
408 SAL_INFO(
"fwk.session",
"com_sun_star_comp_frame_SessionListener_get_implementation");
410 return cppu::acquire(
new SessionListener(context));
#define TOOLS_WARN_EXCEPTION(area, stream)
Reference< XDispatch > xDispatch
css::uno::Reference< css::uno::XComponentContext > m_xContext
#define SAL_WARN(area, stream)
#define SAL_INFO(area, stream)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_frame_SessionListener_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)