xref: /trunk/main/comphelper/source/misc/officerestartmanager.cxx (revision 91144cd0085a7583d2099b982122deb2184ab956)
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 {
76     if ( !m_xContext.is() )
77         throw uno::RuntimeException();
78 
79     {
80         ::osl::MutexGuard aGuard( m_aMutex );
81 
82         // if the restart already running there is no need to trigger it again
83         if ( m_bRestartRequested )
84             return;
85 
86         m_bRestartRequested = sal_True;
87 
88         // the office is still not initialized, no need to terminate, changing the state is enough
89         if ( !m_bOfficeInitialized )
90             return;
91     }
92 
93     // TODO: use InteractionHandler to report errors
94     try
95     {
96         // register itself as a job that should be executed asynchronously
97         uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
98 
99         uno::Reference< awt::XRequestCallback > xRequestCallback(
100             xFactory->createInstanceWithContext(
101                  ::rtl::OUString::createFromAscii("com.sun.star.awt.AsyncCallback"),
102                  m_xContext ),
103              uno::UNO_QUERY_THROW );
104 
105         xRequestCallback->addCallback( this, uno::Any() );
106     }
107     catch ( uno::Exception& )
108     {
109         // the try to request restart has failed
110         m_bRestartRequested = sal_False;
111     }
112 }
113 
114 // ----------------------------------------------------------
isRestartRequested(::sal_Bool bOfficeInitialized)115 ::sal_Bool SAL_CALL OOfficeRestartManager::isRestartRequested( ::sal_Bool bOfficeInitialized )
116 {
117     ::osl::MutexGuard aGuard( m_aMutex );
118 
119     if ( bOfficeInitialized && !m_bOfficeInitialized )
120         m_bOfficeInitialized = bOfficeInitialized;
121 
122     return m_bRestartRequested;
123 }
124 
125 // XCallback
126 // ----------------------------------------------------------
notify(const uno::Any &)127 void SAL_CALL OOfficeRestartManager::notify( const uno::Any& /* aData */ )
128 {
129     try
130     {
131         sal_Bool bSuccess = sal_False;
132 
133         if ( m_xContext.is() )
134         {
135             uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
136             uno::Reference< frame::XDesktop > xDesktop(
137                 xFactory->createInstanceWithContext(
138                     ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "com.sun.star.frame.Desktop" ) ), m_xContext ),
139                 uno::UNO_QUERY_THROW );
140 
141             // Turn Quickstarter veto off
142             uno::Reference< beans::XPropertySet > xPropertySet( xDesktop, uno::UNO_QUERY_THROW );
143             ::rtl::OUString aVetoPropName( RTL_CONSTASCII_USTRINGPARAM( "SuspendQuickstartVeto" ) );
144             uno::Any aValue;
145             aValue <<= (sal_Bool)sal_True;
146             xPropertySet->setPropertyValue( aVetoPropName, aValue );
147 
148             try
149             {
150                 bSuccess = xDesktop->terminate();
151             } catch( uno::Exception& )
152             {}
153 
154             if ( !bSuccess )
155             {
156                 aValue <<= (sal_Bool)sal_False;
157                 xPropertySet->setPropertyValue( aVetoPropName, aValue );
158             }
159         }
160 
161         if ( !bSuccess )
162             m_bRestartRequested = sal_False;
163     }
164     catch( uno::Exception& )
165     {
166         // the try to restart has failed
167         m_bRestartRequested = sal_False;
168     }
169 }
170 
171 // XServiceInfo
172 // ----------------------------------------------------------
getImplementationName()173 ::rtl::OUString SAL_CALL OOfficeRestartManager::getImplementationName()
174 {
175     return getImplementationName_static();
176 }
177 
178 // ----------------------------------------------------------
supportsService(const::rtl::OUString & aServiceName)179 ::sal_Bool SAL_CALL OOfficeRestartManager::supportsService( const ::rtl::OUString& aServiceName )
180 {
181     const uno::Sequence< rtl::OUString > & aSupportedNames = getSupportedServiceNames_static();
182     for ( sal_Int32 nInd = 0; nInd < aSupportedNames.getLength(); nInd++ )
183     {
184         if ( aSupportedNames[ nInd ].equals( aServiceName ) )
185             return sal_True;
186     }
187 
188     return sal_False;
189 }
190 
191 // ----------------------------------------------------------
getSupportedServiceNames()192 uno::Sequence< ::rtl::OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames()
193 {
194     return getSupportedServiceNames_static();
195 }
196 
197 } // namespace comphelper
198 
createRegistryInfo_OOfficeRestartManager()199 void createRegistryInfo_OOfficeRestartManager()
200 {
201     static ::comphelper::module::OAutoRegistration< ::comphelper::OOfficeRestartManager > aAutoRegistration;
202     static ::comphelper::module::OSingletonRegistration< ::comphelper::OOfficeRestartManager > aSingletonRegistration;
203 }
204