LibreOffice Module comphelper (master)  1
officerestartmanager.cxx
Go to the documentation of this file.
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  *
9  * This file incorporates work covered by the following license notice:
10  *
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 .
18  */
19 
20 
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>
25 
27 #include <rtl/ref.hxx>
28 #include "officerestartmanager.hxx"
29 
30 using namespace ::com::sun::star;
31 
32 namespace comphelper
33 {
34 
35 // XRestartManager
36 
37 void SAL_CALL OOfficeRestartManager::requestRestart( const uno::Reference< task::XInteractionHandler >& /* xInteractionHandler */ )
38 {
39  if ( !m_xContext.is() )
40  throw uno::RuntimeException();
41 
42  {
43  ::osl::MutexGuard aGuard( m_aMutex );
44 
45  // if the restart already running there is no need to trigger it again
46  if ( m_bRestartRequested )
47  return;
48 
49  m_bRestartRequested = true;
50 
51  // the office is still not initialized, no need to terminate, changing the state is enough
52  if ( !m_bOfficeInitialized )
53  return;
54  }
55 
56  // TODO: use InteractionHandler to report errors
57  try
58  {
59  // register itself as a job that should be executed asynchronously
60  uno::Reference< lang::XMultiComponentFactory > xFactory( m_xContext->getServiceManager(), uno::UNO_SET_THROW );
61 
62  uno::Reference< awt::XRequestCallback > xRequestCallback(
63  xFactory->createInstanceWithContext(
64  "com.sun.star.awt.AsyncCallback",
65  m_xContext ),
66  uno::UNO_QUERY_THROW );
67 
68  xRequestCallback->addCallback( this, uno::Any() );
69  }
70  catch ( uno::Exception& )
71  {
72  // the try to request restart has failed
73  m_bRestartRequested = false;
74  }
75 }
76 
77 
79 {
80  ::osl::MutexGuard aGuard( m_aMutex );
81 
82  if ( bOfficeInitialized && !m_bOfficeInitialized )
83  m_bOfficeInitialized = bOfficeInitialized;
84 
85  return m_bRestartRequested;
86 }
87 
88 // XCallback
89 
90 void SAL_CALL OOfficeRestartManager::notify( const uno::Any& /* aData */ )
91 {
92  try
93  {
94  bool bSuccess = false;
95 
96  if ( m_xContext.is() )
97  {
98  uno::Reference< frame::XDesktop2 > xDesktop = frame::Desktop::create(m_xContext);
99 
100  // Turn Quickstarter veto off
101  uno::Reference< beans::XPropertySet > xPropertySet( xDesktop, uno::UNO_QUERY_THROW );
102  OUString aVetoPropName( "SuspendQuickstartVeto" );
103  uno::Any aValue;
104  aValue <<= true;
105  xPropertySet->setPropertyValue( aVetoPropName, aValue );
106 
107  try
108  {
109  bSuccess = xDesktop->terminate();
110  } catch( uno::Exception& )
111  {}
112 
113  if ( !bSuccess )
114  {
115  aValue <<= false;
116  xPropertySet->setPropertyValue( aVetoPropName, aValue );
117  }
118  }
119 
120  if ( !bSuccess )
121  m_bRestartRequested = false;
122  }
123  catch( uno::Exception& )
124  {
125  // the try to restart has failed
126  m_bRestartRequested = false;
127  }
128 }
129 
130 // XServiceInfo
131 
133 {
134  return "com.sun.star.comp.task.OfficeRestartManager";
135 }
136 
137 sal_Bool SAL_CALL OOfficeRestartManager::supportsService( const OUString& aServiceName )
138 {
139  return cppu::supportsService(this, aServiceName);
140 }
141 
142 uno::Sequence< OUString > SAL_CALL OOfficeRestartManager::getSupportedServiceNames()
143 {
144  return { "com.sun.star.comp.task.OfficeRestartManager" };
145 }
146 
147 } // namespace comphelper
148 
149 namespace {
150 
151 struct Instance {
152  explicit Instance(
153  css::uno::Reference<css::uno::XComponentContext> const & context):
154  instance(static_cast<cppu::OWeakObject *>(new comphelper::OOfficeRestartManager(context)))
155  {}
156 
158 };
159 
160 struct Singleton:
161  public rtl::StaticWithArg<
162  Instance, css::uno::Reference<css::uno::XComponentContext>, Singleton>
163 {};
164 
165 }
166 
167 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface *
169  css::uno::XComponentContext *context,
170  css::uno::Sequence<css::uno::Any> const &)
171 {
172  return cppu::acquire(static_cast<cppu::OWeakObject *>(
173  Singleton::get(context).instance.get()));
174 }
175 
176 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
virtual void SAL_CALL notify(const css::uno::Any &aData) override
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * com_sun_star_comp_task_OfficeRestartManager(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
css::uno::Reference< css::uno::XComponentContext > m_xContext
virtual sal_Bool SAL_CALL supportsService(const OUString &ServiceName) override
unsigned char sal_Bool
virtual sal_Bool SAL_CALL isRestartRequested(sal_Bool bInitialized) override
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
virtual OUString SAL_CALL getImplementationName() override
Reference< XSingleServiceFactory > xFactory
virtual void SAL_CALL requestRestart(const css::uno::Reference< css::task::XInteractionHandler > &xInteractionHandler) override