bump product version to 5.0.4.1
[LibreOffice.git] / comphelper / source / misc / officerestartmanager.cxx
blob462b59cd6f208bfc8396a646eca535f3398b3386
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
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>
26 #include <comphelper_module.hxx>
27 #include <comphelper_services.hxx>
28 #include <cppuhelper/supportsservice.hxx>
29 #include "officerestartmanager.hxx"
31 using namespace ::com::sun::star;
33 namespace comphelper
37 uno::Sequence< OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames_static()
39 uno::Sequence< OUString > aResult( 1 );
40 aResult[0] = getServiceName_static();
41 return aResult;
45 OUString SAL_CALL OOfficeRestartManager::getImplementationName_static()
47 return OUString( "com.sun.star.comp.task.OfficeRestartManager" );
51 OUString SAL_CALL OOfficeRestartManager::getSingletonName_static()
53 return OUString( "com.sun.star.task.OfficeRestartManager" );
57 OUString SAL_CALL OOfficeRestartManager::getServiceName_static()
59 return OUString( "com.sun.star.comp.task.OfficeRestartManager" );
63 uno::Reference< uno::XInterface > SAL_CALL OOfficeRestartManager::Create( const uno::Reference< uno::XComponentContext >& rxContext )
65 return static_cast< cppu::OWeakObject* >( new OOfficeRestartManager( rxContext ) );
68 // XRestartManager
70 void SAL_CALL OOfficeRestartManager::requestRestart( const uno::Reference< task::XInteractionHandler >& /* xInteractionHandler */ )
71 throw (uno::Exception, uno::RuntimeException, std::exception)
73 if ( !m_xContext.is() )
74 throw uno::RuntimeException();
77 ::osl::MutexGuard aGuard( m_aMutex );
79 // if the restart already running there is no need to trigger it again
80 if ( m_bRestartRequested )
81 return;
83 m_bRestartRequested = true;
85 // the office is still not initialized, no need to terminate, changing the state is enough
86 if ( !m_bOfficeInitialized )
87 return;
90 // TODO: use InteractionHandler to report errors
91 try
93 // register itself as a job that should be executed asynchronously
94 uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
96 uno::Reference< awt::XRequestCallback > xRequestCallback(
97 xFactory->createInstanceWithContext(
98 OUString( "com.sun.star.awt.AsyncCallback" ),
99 m_xContext ),
100 uno::UNO_QUERY_THROW );
102 xRequestCallback->addCallback( this, uno::Any() );
104 catch ( uno::Exception& )
106 // the try to request restart has failed
107 m_bRestartRequested = false;
112 sal_Bool SAL_CALL OOfficeRestartManager::isRestartRequested( sal_Bool bOfficeInitialized )
113 throw (uno::Exception, uno::RuntimeException, std::exception)
115 ::osl::MutexGuard aGuard( m_aMutex );
117 if ( bOfficeInitialized && !m_bOfficeInitialized )
118 m_bOfficeInitialized = bOfficeInitialized;
120 return m_bRestartRequested;
123 // XCallback
125 void SAL_CALL OOfficeRestartManager::notify( const uno::Any& /* aData */ )
126 throw ( uno::RuntimeException, std::exception )
130 bool bSuccess = false;
132 if ( m_xContext.is() )
134 uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(m_xContext);
136 // Turn Quickstarter veto off
137 uno::Reference< beans::XPropertySet > xPropertySet( xDesktop, uno::UNO_QUERY_THROW );
138 OUString aVetoPropName( "SuspendQuickstartVeto" );
139 uno::Any aValue;
140 aValue <<= true;
141 xPropertySet->setPropertyValue( aVetoPropName, aValue );
145 bSuccess = xDesktop->terminate();
146 } catch( uno::Exception& )
149 if ( !bSuccess )
151 aValue <<= false;
152 xPropertySet->setPropertyValue( aVetoPropName, aValue );
156 if ( !bSuccess )
157 m_bRestartRequested = false;
159 catch( uno::Exception& )
161 // the try to restart has failed
162 m_bRestartRequested = false;
166 // XServiceInfo
168 OUString SAL_CALL OOfficeRestartManager::getImplementationName() throw (uno::RuntimeException, std::exception)
170 return getImplementationName_static();
173 sal_Bool SAL_CALL OOfficeRestartManager::supportsService( const OUString& aServiceName ) throw (uno::RuntimeException, std::exception)
175 return cppu::supportsService(this, aServiceName);
178 uno::Sequence< OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames() throw (uno::RuntimeException, std::exception)
180 return getSupportedServiceNames_static();
183 } // namespace comphelper
185 void createRegistryInfo_OOfficeRestartManager()
187 static ::comphelper::module::OAutoRegistration< ::comphelper::OOfficeRestartManager > aAutoRegistration;
188 static ::comphelper::module::OSingletonRegistration< ::comphelper::OOfficeRestartManager > aSingletonRegistration;
191 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */