1*dde7d3faSAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*dde7d3faSAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*dde7d3faSAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*dde7d3faSAndrew Rist  * distributed with this work for additional information
6*dde7d3faSAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*dde7d3faSAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*dde7d3faSAndrew Rist  * "License"); you may not use this file except in compliance
9*dde7d3faSAndrew Rist  * with the License.  You may obtain a copy of the License at
10*dde7d3faSAndrew Rist  *
11*dde7d3faSAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12*dde7d3faSAndrew Rist  *
13*dde7d3faSAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*dde7d3faSAndrew Rist  * software distributed under the License is distributed on an
15*dde7d3faSAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*dde7d3faSAndrew Rist  * KIND, either express or implied.  See the License for the
17*dde7d3faSAndrew Rist  * specific language governing permissions and limitations
18*dde7d3faSAndrew Rist  * under the License.
19*dde7d3faSAndrew Rist  *
20*dde7d3faSAndrew Rist  *************************************************************/
21*dde7d3faSAndrew Rist 
22*dde7d3faSAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove
25cdf0e10cSrcweir #include "precompiled_comphelper.hxx"
26cdf0e10cSrcweir 
27cdf0e10cSrcweir #include <com/sun/star/lang/XMultiComponentFactory.hpp>
28cdf0e10cSrcweir #include <com/sun/star/awt/XRequestCallback.hpp>
29cdf0e10cSrcweir #include <com/sun/star/frame/XDesktop.hpp>
30cdf0e10cSrcweir #include <com/sun/star/beans/XPropertySet.hpp>
31cdf0e10cSrcweir 
32cdf0e10cSrcweir #include <comphelper_module.hxx>
33cdf0e10cSrcweir #include "officerestartmanager.hxx"
34cdf0e10cSrcweir 
35cdf0e10cSrcweir using namespace ::com::sun::star;
36cdf0e10cSrcweir 
37cdf0e10cSrcweir namespace comphelper
38cdf0e10cSrcweir {
39cdf0e10cSrcweir 
40cdf0e10cSrcweir // ----------------------------------------------------------
getSupportedServiceNames_static()41cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames_static()
42cdf0e10cSrcweir {
43cdf0e10cSrcweir     uno::Sequence< rtl::OUString > aResult( 1 );
44cdf0e10cSrcweir     aResult[0] = getServiceName_static();
45cdf0e10cSrcweir     return aResult;
46cdf0e10cSrcweir }
47cdf0e10cSrcweir 
48cdf0e10cSrcweir // ----------------------------------------------------------
getImplementationName_static()49cdf0e10cSrcweir ::rtl::OUString SAL_CALL OOfficeRestartManager::getImplementationName_static()
50cdf0e10cSrcweir {
51cdf0e10cSrcweir     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.task.OfficeRestartManager" ) );
52cdf0e10cSrcweir }
53cdf0e10cSrcweir 
54cdf0e10cSrcweir // ----------------------------------------------------------
getSingletonName_static()55cdf0e10cSrcweir ::rtl::OUString SAL_CALL OOfficeRestartManager::getSingletonName_static()
56cdf0e10cSrcweir {
57cdf0e10cSrcweir     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.task.OfficeRestartManager" ) );
58cdf0e10cSrcweir }
59cdf0e10cSrcweir 
60cdf0e10cSrcweir // ----------------------------------------------------------
getServiceName_static()61cdf0e10cSrcweir ::rtl::OUString SAL_CALL OOfficeRestartManager::getServiceName_static()
62cdf0e10cSrcweir {
63cdf0e10cSrcweir     return rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.comp.task.OfficeRestartManager" ) );
64cdf0e10cSrcweir }
65cdf0e10cSrcweir 
66cdf0e10cSrcweir // ----------------------------------------------------------
Create(const uno::Reference<uno::XComponentContext> & rxContext)67cdf0e10cSrcweir uno::Reference< uno::XInterface > SAL_CALL OOfficeRestartManager::Create( const uno::Reference< uno::XComponentContext >& rxContext )
68cdf0e10cSrcweir {
69cdf0e10cSrcweir     return static_cast< cppu::OWeakObject* >( new OOfficeRestartManager( rxContext ) );
70cdf0e10cSrcweir }
71cdf0e10cSrcweir 
72cdf0e10cSrcweir // XRestartManager
73cdf0e10cSrcweir // ----------------------------------------------------------
requestRestart(const uno::Reference<task::XInteractionHandler> &)74cdf0e10cSrcweir void SAL_CALL OOfficeRestartManager::requestRestart( const uno::Reference< task::XInteractionHandler >& /* xInteractionHandler */ )
75cdf0e10cSrcweir     throw (uno::Exception, uno::RuntimeException)
76cdf0e10cSrcweir {
77cdf0e10cSrcweir     if ( !m_xContext.is() )
78cdf0e10cSrcweir         throw uno::RuntimeException();
79cdf0e10cSrcweir 
80cdf0e10cSrcweir     {
81cdf0e10cSrcweir         ::osl::MutexGuard aGuard( m_aMutex );
82cdf0e10cSrcweir 
83cdf0e10cSrcweir         // if the restart already running there is no need to trigger it again
84cdf0e10cSrcweir         if ( m_bRestartRequested )
85cdf0e10cSrcweir             return;
86cdf0e10cSrcweir 
87cdf0e10cSrcweir         m_bRestartRequested = sal_True;
88cdf0e10cSrcweir 
89cdf0e10cSrcweir         // the office is still not initialized, no need to terminate, changing the state is enough
90cdf0e10cSrcweir         if ( !m_bOfficeInitialized )
91cdf0e10cSrcweir             return;
92cdf0e10cSrcweir     }
93cdf0e10cSrcweir 
94cdf0e10cSrcweir     // TODO: use InteractionHandler to report errors
95cdf0e10cSrcweir     try
96cdf0e10cSrcweir     {
97cdf0e10cSrcweir         // register itself as a job that should be executed asynchronously
98cdf0e10cSrcweir         uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
99cdf0e10cSrcweir 
100cdf0e10cSrcweir         uno::Reference< awt::XRequestCallback > xRequestCallback(
101cdf0e10cSrcweir             xFactory->createInstanceWithContext(
102cdf0e10cSrcweir                  ::rtl::OUString::createFromAscii("com.sun.star.awt.AsyncCallback"),
103cdf0e10cSrcweir                  m_xContext ),
104cdf0e10cSrcweir              uno::UNO_QUERY_THROW );
105cdf0e10cSrcweir 
106cdf0e10cSrcweir         xRequestCallback->addCallback( this, uno::Any() );
107cdf0e10cSrcweir     }
108cdf0e10cSrcweir     catch ( uno::Exception& )
109cdf0e10cSrcweir     {
110cdf0e10cSrcweir         // the try to request restart has failed
111cdf0e10cSrcweir         m_bRestartRequested = sal_False;
112cdf0e10cSrcweir     }
113cdf0e10cSrcweir }
114cdf0e10cSrcweir 
115cdf0e10cSrcweir // ----------------------------------------------------------
isRestartRequested(::sal_Bool bOfficeInitialized)116cdf0e10cSrcweir ::sal_Bool SAL_CALL OOfficeRestartManager::isRestartRequested( ::sal_Bool bOfficeInitialized )
117cdf0e10cSrcweir     throw (uno::Exception, uno::RuntimeException)
118cdf0e10cSrcweir {
119cdf0e10cSrcweir     ::osl::MutexGuard aGuard( m_aMutex );
120cdf0e10cSrcweir 
121cdf0e10cSrcweir     if ( bOfficeInitialized && !m_bOfficeInitialized )
122cdf0e10cSrcweir         m_bOfficeInitialized = bOfficeInitialized;
123cdf0e10cSrcweir 
124cdf0e10cSrcweir     return m_bRestartRequested;
125cdf0e10cSrcweir }
126cdf0e10cSrcweir 
127cdf0e10cSrcweir // XCallback
128cdf0e10cSrcweir // ----------------------------------------------------------
notify(const uno::Any &)129cdf0e10cSrcweir void SAL_CALL OOfficeRestartManager::notify( const uno::Any& /* aData */ )
130cdf0e10cSrcweir     throw ( uno::RuntimeException )
131cdf0e10cSrcweir {
132cdf0e10cSrcweir     try
133cdf0e10cSrcweir     {
134cdf0e10cSrcweir         sal_Bool bSuccess = sal_False;
135cdf0e10cSrcweir 
136cdf0e10cSrcweir         if ( m_xContext.is() )
137cdf0e10cSrcweir         {
138cdf0e10cSrcweir             uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
139cdf0e10cSrcweir             uno::Reference< frame::XDesktop > xDesktop(
140cdf0e10cSrcweir                 xFactory->createInstanceWithContext(
141cdf0e10cSrcweir                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), m_xContext ),
142cdf0e10cSrcweir                 uno::UNO_QUERY_THROW );
143cdf0e10cSrcweir 
144cdf0e10cSrcweir             // Turn Quickstarter veto off
145cdf0e10cSrcweir             uno::Reference< beans::XPropertySet > xPropertySet( xDesktop, uno::UNO_QUERY_THROW );
146cdf0e10cSrcweir             ::rtl::OUString aVetoPropName( RTL_CONSTASCII_USTRINGPARAM( "SuspendQuickstartVeto" ) );
147cdf0e10cSrcweir             uno::Any aValue;
148cdf0e10cSrcweir             aValue <<= (sal_Bool)sal_True;
149cdf0e10cSrcweir             xPropertySet->setPropertyValue( aVetoPropName, aValue );
150cdf0e10cSrcweir 
151cdf0e10cSrcweir             try
152cdf0e10cSrcweir             {
153cdf0e10cSrcweir                 bSuccess = xDesktop->terminate();
154cdf0e10cSrcweir             } catch( uno::Exception& )
155cdf0e10cSrcweir             {}
156cdf0e10cSrcweir 
157cdf0e10cSrcweir             if ( !bSuccess )
158cdf0e10cSrcweir             {
159cdf0e10cSrcweir                 aValue <<= (sal_Bool)sal_False;
160cdf0e10cSrcweir                 xPropertySet->setPropertyValue( aVetoPropName, aValue );
161cdf0e10cSrcweir             }
162cdf0e10cSrcweir         }
163cdf0e10cSrcweir 
164cdf0e10cSrcweir         if ( !bSuccess )
165cdf0e10cSrcweir             m_bRestartRequested = sal_False;
166cdf0e10cSrcweir     }
167cdf0e10cSrcweir     catch( uno::Exception& )
168cdf0e10cSrcweir     {
169cdf0e10cSrcweir         // the try to restart has failed
170cdf0e10cSrcweir         m_bRestartRequested = sal_False;
171cdf0e10cSrcweir     }
172cdf0e10cSrcweir }
173cdf0e10cSrcweir 
174cdf0e10cSrcweir // XServiceInfo
175cdf0e10cSrcweir // ----------------------------------------------------------
getImplementationName()176cdf0e10cSrcweir ::rtl::OUString SAL_CALL OOfficeRestartManager::getImplementationName() throw (uno::RuntimeException)
177cdf0e10cSrcweir {
178cdf0e10cSrcweir     return getImplementationName_static();
179cdf0e10cSrcweir }
180cdf0e10cSrcweir 
181cdf0e10cSrcweir // ----------------------------------------------------------
supportsService(const::rtl::OUString & aServiceName)182cdf0e10cSrcweir ::sal_Bool SAL_CALL OOfficeRestartManager::supportsService( const ::rtl::OUString& aServiceName ) throw (uno::RuntimeException)
183cdf0e10cSrcweir {
184cdf0e10cSrcweir     const uno::Sequence< rtl::OUString > & aSupportedNames = getSupportedServiceNames_static();
185cdf0e10cSrcweir     for ( sal_Int32 nInd = 0; nInd < aSupportedNames.getLength(); nInd++ )
186cdf0e10cSrcweir     {
187cdf0e10cSrcweir         if ( aSupportedNames[ nInd ].equals( aServiceName ) )
188cdf0e10cSrcweir             return sal_True;
189cdf0e10cSrcweir     }
190cdf0e10cSrcweir 
191cdf0e10cSrcweir     return sal_False;
192cdf0e10cSrcweir }
193cdf0e10cSrcweir 
194cdf0e10cSrcweir // ----------------------------------------------------------
getSupportedServiceNames()195cdf0e10cSrcweir uno::Sequence< ::rtl::OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames() throw (uno::RuntimeException)
196cdf0e10cSrcweir {
197cdf0e10cSrcweir     return getSupportedServiceNames_static();
198cdf0e10cSrcweir }
199cdf0e10cSrcweir 
200cdf0e10cSrcweir } // namespace comphelper
201cdf0e10cSrcweir 
createRegistryInfo_OOfficeRestartManager()202cdf0e10cSrcweir void createRegistryInfo_OOfficeRestartManager()
203cdf0e10cSrcweir {
204cdf0e10cSrcweir     static ::comphelper::module::OAutoRegistration< ::comphelper::OOfficeRestartManager > aAutoRegistration;
205cdf0e10cSrcweir     static ::comphelper::module::OSingletonRegistration< ::comphelper::OOfficeRestartManager > aSingletonRegistration;
206cdf0e10cSrcweir }
207