1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 "officerestartmanager.hxx"
29 using namespace ::com::sun::star
;
34 // ----------------------------------------------------------
35 uno::Sequence
< OUString
> SAL_CALL
OOfficeRestartManager::getSupportedServiceNames_static()
37 uno::Sequence
< OUString
> aResult( 1 );
38 aResult
[0] = getServiceName_static();
42 // ----------------------------------------------------------
43 OUString SAL_CALL
OOfficeRestartManager::getImplementationName_static()
45 return OUString( "com.sun.star.comp.task.OfficeRestartManager" );
48 // ----------------------------------------------------------
49 OUString SAL_CALL
OOfficeRestartManager::getSingletonName_static()
51 return OUString( "com.sun.star.task.OfficeRestartManager" );
54 // ----------------------------------------------------------
55 OUString SAL_CALL
OOfficeRestartManager::getServiceName_static()
57 return OUString( "com.sun.star.comp.task.OfficeRestartManager" );
60 // ----------------------------------------------------------
61 uno::Reference
< uno::XInterface
> SAL_CALL
OOfficeRestartManager::Create( const uno::Reference
< uno::XComponentContext
>& rxContext
)
63 return static_cast< cppu::OWeakObject
* >( new OOfficeRestartManager( rxContext
) );
67 // ----------------------------------------------------------
68 void SAL_CALL
OOfficeRestartManager::requestRestart( const uno::Reference
< task::XInteractionHandler
>& /* xInteractionHandler */ )
69 throw (uno::Exception
, uno::RuntimeException
)
71 if ( !m_xContext
.is() )
72 throw uno::RuntimeException();
75 ::osl::MutexGuard
aGuard( m_aMutex
);
77 // if the restart already running there is no need to trigger it again
78 if ( m_bRestartRequested
)
81 m_bRestartRequested
= sal_True
;
83 // the office is still not initialized, no need to terminate, changing the state is enough
84 if ( !m_bOfficeInitialized
)
88 // TODO: use InteractionHandler to report errors
91 // register itself as a job that should be executed asynchronously
92 uno::Reference
< lang::XMultiComponentFactory
> xFactory( m_xContext
->getServiceManager(), uno::UNO_SET_THROW
);
94 uno::Reference
< awt::XRequestCallback
> xRequestCallback(
95 xFactory
->createInstanceWithContext(
96 OUString( "com.sun.star.awt.AsyncCallback" ),
98 uno::UNO_QUERY_THROW
);
100 xRequestCallback
->addCallback( this, uno::Any() );
102 catch ( uno::Exception
& )
104 // the try to request restart has failed
105 m_bRestartRequested
= sal_False
;
109 // ----------------------------------------------------------
110 ::sal_Bool SAL_CALL
OOfficeRestartManager::isRestartRequested( ::sal_Bool bOfficeInitialized
)
111 throw (uno::Exception
, uno::RuntimeException
)
113 ::osl::MutexGuard
aGuard( m_aMutex
);
115 if ( bOfficeInitialized
&& !m_bOfficeInitialized
)
116 m_bOfficeInitialized
= bOfficeInitialized
;
118 return m_bRestartRequested
;
122 // ----------------------------------------------------------
123 void SAL_CALL
OOfficeRestartManager::notify( const uno::Any
& /* aData */ )
124 throw ( uno::RuntimeException
)
128 sal_Bool bSuccess
= sal_False
;
130 if ( m_xContext
.is() )
132 uno::Reference
< frame::XDesktop2
> xDesktop
= frame::Desktop::create(m_xContext
);
134 // Turn Quickstarter veto off
135 uno::Reference
< beans::XPropertySet
> xPropertySet( xDesktop
, uno::UNO_QUERY_THROW
);
136 OUString
aVetoPropName( "SuspendQuickstartVeto" );
138 aValue
<<= (sal_Bool
)sal_True
;
139 xPropertySet
->setPropertyValue( aVetoPropName
, aValue
);
143 bSuccess
= xDesktop
->terminate();
144 } catch( uno::Exception
& )
149 aValue
<<= (sal_Bool
)sal_False
;
150 xPropertySet
->setPropertyValue( aVetoPropName
, aValue
);
155 m_bRestartRequested
= sal_False
;
157 catch( uno::Exception
& )
159 // the try to restart has failed
160 m_bRestartRequested
= sal_False
;
165 // ----------------------------------------------------------
166 OUString SAL_CALL
OOfficeRestartManager::getImplementationName() throw (uno::RuntimeException
)
168 return getImplementationName_static();
171 // ----------------------------------------------------------
172 ::sal_Bool SAL_CALL
OOfficeRestartManager::supportsService( const OUString
& aServiceName
) throw (uno::RuntimeException
)
174 const uno::Sequence
< OUString
> & aSupportedNames
= getSupportedServiceNames_static();
175 for ( sal_Int32 nInd
= 0; nInd
< aSupportedNames
.getLength(); nInd
++ )
177 if ( aSupportedNames
[ nInd
].equals( aServiceName
) )
184 // ----------------------------------------------------------
185 uno::Sequence
< OUString
> SAL_CALL
OOfficeRestartManager::getSupportedServiceNames() throw (uno::RuntimeException
)
187 return getSupportedServiceNames_static();
190 } // namespace comphelper
192 void createRegistryInfo_OOfficeRestartManager()
194 static ::comphelper::module::OAutoRegistration
< ::comphelper::OOfficeRestartManager
> aAutoRegistration
;
195 static ::comphelper::module::OSingletonRegistration
< ::comphelper::OOfficeRestartManager
> aSingletonRegistration
;
198 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */