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