LibreOffice Module comphelper (master) 1
officerestartmanager.cxx
Go to the documentation of this file.
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
21#include <com/sun/star/lang/XMultiComponentFactory.hpp>
22#include <com/sun/star/awt/XRequestCallback.hpp>
23#include <com/sun/star/frame/Desktop.hpp>
24#include <com/sun/star/beans/XPropertySet.hpp>
25
28
29using namespace ::com::sun::star;
30
31namespace comphelper
32{
33
34// XRestartManager
35
36void SAL_CALL OOfficeRestartManager::requestRestart( const uno::Reference< task::XInteractionHandler >& /* xInteractionHandler */ )
37{
38 if ( !m_xContext.is() )
39 throw uno::RuntimeException();
40
41 {
42 std::unique_lock aGuard( m_aMutex );
43
44 // if the restart already running there is no need to trigger it again
46 return;
47
49
50 // the office is still not initialized, no need to terminate, changing the state is enough
52 return;
53 }
54
55 // TODO: use InteractionHandler to report errors
56 try
57 {
58 // register itself as a job that should be executed asynchronously
59 uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
60
61 uno::Reference< awt::XRequestCallback > xRequestCallback(
62 xFactory->createInstanceWithContext(
63 "com.sun.star.awt.AsyncCallback",
64 m_xContext ),
65 uno::UNO_QUERY_THROW );
66
67 xRequestCallback->addCallback( this, uno::Any() );
68 }
69 catch ( uno::Exception& )
70 {
71 // the try to request restart has failed
72 m_bRestartRequested = false;
73 }
74}
75
76
78{
79 std::unique_lock aGuard( m_aMutex );
80
81 if ( bOfficeInitialized && !m_bOfficeInitialized )
82 m_bOfficeInitialized = bOfficeInitialized;
83
85}
86
87// XCallback
88
89void SAL_CALL OOfficeRestartManager::notify( const uno::Any& /* aData */ )
90{
91 try
92 {
93 bool bSuccess = false;
94
95 if ( m_xContext.is() )
96 {
97 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(m_xContext);
98
99 // Turn Quickstarter veto off
100 uno::Reference< beans::XPropertySet > xPropertySet( xDesktop, uno::UNO_QUERY_THROW );
101 OUString aVetoPropName( "SuspendQuickstartVeto" );
102 uno::Any aValue;
103 aValue <<= true;
104 xPropertySet->setPropertyValue( aVetoPropName, aValue );
105
106 try
107 {
108 bSuccess = xDesktop->terminate();
109 } catch( uno::Exception& )
110 {}
111
112 if ( !bSuccess )
113 {
114 aValue <<= false;
115 xPropertySet->setPropertyValue( aVetoPropName, aValue );
116 }
117 }
118
119 if ( !bSuccess )
120 m_bRestartRequested = false;
121 }
122 catch( uno::Exception& )
123 {
124 // the try to restart has failed
125 m_bRestartRequested = false;
126 }
127}
128
129// XServiceInfo
130
132{
133 return "com.sun.star.comp.task.OfficeRestartManager";
134}
135
136sal_Bool SAL_CALL OOfficeRestartManager::supportsService( const OUString& aServiceName )
137{
138 return cppu::supportsService(this, aServiceName);
139}
140
141uno::Sequence< OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames()
142{
143 return { "com.sun.star.comp.task.OfficeRestartManager" };
144}
145
146} // namespace comphelper
147
148
149extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
151 css::uno::XComponentContext *context,
152 css::uno::Sequence<css::uno::Any> const &)
153{
154 return cppu::acquire(new comphelper::OOfficeRestartManager(context));
155}
156
157/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual void SAL_CALL notify(const css::uno::Any &aData) override
virtual void SAL_CALL requestRestart(const css::uno::Reference< css::task::XInteractionHandler > &xInteractionHandler) override
virtual sal_Bool SAL_CALL isRestartRequested(sal_Bool bInitialized) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
Reference< XSingleServiceFactory > xFactory
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_task_OfficeRestartManager(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
unsigned char sal_Bool