Version 6.4.0.3, tag libreoffice-6.4.0.3
[LibreOffice.git] / desktop / source / deployment / gui / dp_gui_service.cxx
blob10325631d6a24fb62aa89cf0459f41556f123a5c
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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 <memory>
22 #include "dp_gui.h"
23 #include "dp_gui_theextmgr.hxx"
24 #include <osl/diagnose.h>
25 #include <cppuhelper/implbase.hxx>
26 #include <cppuhelper/implementationentry.hxx>
27 #include <unotools/configmgr.hxx>
28 #include <comphelper/processfactory.hxx>
29 #include <comphelper/servicedecl.hxx>
30 #include <comphelper/unwrapargs.hxx>
31 #include <unotools/resmgr.hxx>
32 #include <vcl/weld.hxx>
33 #include <vcl/svapp.hxx>
34 #include <vcl/settings.hxx>
35 #include <com/sun/star/lang/XServiceInfo.hpp>
36 #include <com/sun/star/task/XJobExecutor.hpp>
37 #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
39 #include <boost/optional.hpp>
40 #include "license_dialog.hxx"
41 #include "dp_gui_dialog2.hxx"
42 #include "dp_gui_extensioncmdqueue.hxx"
44 using namespace ::dp_misc;
45 using namespace ::com::sun::star;
46 using namespace ::com::sun::star::uno;
48 namespace sdecl = comphelper::service_decl;
50 namespace dp_gui {
53 class MyApp : public Application
55 public:
56 MyApp();
58 MyApp(const MyApp&) = delete;
59 const MyApp& operator=(const MyApp&) = delete;
61 // Application
62 virtual int Main() override;
63 virtual void DeInit() override;
66 MyApp::MyApp()
71 int MyApp::Main()
73 return EXIT_SUCCESS;
76 void MyApp::DeInit()
78 css::uno::Reference< css::uno::XComponentContext > context(
79 comphelper::getProcessComponentContext());
80 dp_misc::disposeBridges(context);
81 css::uno::Reference< css::lang::XComponent >(
82 context, css::uno::UNO_QUERY_THROW)->dispose();
83 comphelper::setProcessServiceFactory(nullptr);
86 namespace
88 struct ProductName
89 : public rtl::Static< OUString, ProductName > {};
90 struct Version
91 : public rtl::Static< OUString, Version > {};
92 struct AboutBoxVersion
93 : public rtl::Static< OUString, AboutBoxVersion > {};
94 struct AboutBoxVersionSuffix
95 : public rtl::Static< OUString, AboutBoxVersionSuffix > {};
96 struct OOOVendor
97 : public rtl::Static< OUString, OOOVendor > {};
98 struct Extension
99 : public rtl::Static< OUString, Extension > {};
102 static OUString ReplaceProductNameHookProc( const OUString& rStr )
104 if (rStr.indexOf( "%PRODUCT" ) == -1)
105 return rStr;
107 OUString sProductName = ProductName::get();
108 OUString sVersion = Version::get();
109 OUString sAboutBoxVersion = AboutBoxVersion::get();
110 OUString sAboutBoxVersionSuffix = AboutBoxVersionSuffix::get();
111 OUString sExtension = Extension::get();
112 OUString sOOOVendor = OOOVendor::get();
114 if ( sProductName.isEmpty() )
116 sProductName = utl::ConfigManager::getProductName();
117 sVersion = utl::ConfigManager::getProductVersion();
118 sAboutBoxVersion = utl::ConfigManager::getAboutBoxProductVersion();
119 sAboutBoxVersionSuffix = utl::ConfigManager::getAboutBoxProductVersionSuffix();
120 sOOOVendor = utl::ConfigManager::getVendor();
121 if ( sExtension.isEmpty() )
123 sExtension = utl::ConfigManager::getProductExtension();
127 OUString sRet = rStr.replaceAll( "%PRODUCTNAME", sProductName );
128 sRet = sRet.replaceAll( "%PRODUCTVERSION", sVersion );
129 sRet = sRet.replaceAll( "%ABOUTBOXPRODUCTVERSIONSUFFIX", sAboutBoxVersionSuffix );
130 sRet = sRet.replaceAll( "%ABOUTBOXPRODUCTVERSION", sAboutBoxVersion );
131 sRet = sRet.replaceAll( "%OOOVENDOR", sOOOVendor );
132 sRet = sRet.replaceAll( "%PRODUCTEXTENSION", sExtension );
133 return sRet;
137 class ServiceImpl
138 : public ::cppu::WeakImplHelper<ui::dialogs::XAsynchronousExecutableDialog,
139 task::XJobExecutor>
141 Reference<XComponentContext> const m_xComponentContext;
142 boost::optional< Reference<awt::XWindow> > /* const */ m_parent;
143 boost::optional<OUString> m_extensionURL;
144 OUString m_initialTitle;
145 bool m_bShowUpdateOnly;
147 public:
148 ServiceImpl( Sequence<Any> const & args,
149 Reference<XComponentContext> const & xComponentContext );
151 // XAsynchronousExecutableDialog
152 virtual void SAL_CALL setDialogTitle( OUString const & aTitle ) override;
153 virtual void SAL_CALL startExecuteModal(
154 Reference< ui::dialogs::XDialogClosedListener > const & xListener ) override;
156 // XJobExecutor
157 virtual void SAL_CALL trigger( OUString const & event ) override;
161 ServiceImpl::ServiceImpl( Sequence<Any> const& args,
162 Reference<XComponentContext> const& xComponentContext)
163 : m_xComponentContext(xComponentContext),
164 m_bShowUpdateOnly( false )
166 /* if true then this service is running in a unopkg process and not in an office process */
167 boost::optional<sal_Bool> unopkg;
168 boost::optional<OUString> view;
169 try {
170 comphelper::unwrapArgs( args, m_parent, view, unopkg );
171 return;
172 } catch ( const css::lang::IllegalArgumentException & ) {
174 try {
175 comphelper::unwrapArgs( args, m_extensionURL);
176 } catch ( const css::lang::IllegalArgumentException & ) {
179 ResHookProc pProc = Translate::GetReadStringHook();
180 if ( !pProc )
181 Translate::SetReadStringHook(ReplaceProductNameHookProc);
184 // XAsynchronousExecutableDialog
186 void ServiceImpl::setDialogTitle( OUString const & title )
188 if ( dp_gui::TheExtensionManager::s_ExtMgr.is() )
190 const SolarMutexGuard guard;
191 ::rtl::Reference< ::dp_gui::TheExtensionManager > dialog(
192 ::dp_gui::TheExtensionManager::get( m_xComponentContext,
193 m_parent ? *m_parent : Reference<awt::XWindow>(),
194 m_extensionURL ? *m_extensionURL : OUString() ) );
195 dialog->SetText( title );
197 else
198 m_initialTitle = title;
202 void ServiceImpl::startExecuteModal(
203 Reference< ui::dialogs::XDialogClosedListener > const & xListener )
205 bool bCloseDialog = true; // only used if m_bShowUpdateOnly is true
206 std::unique_ptr<Application> app;
207 //ToDo: synchronize access to s_dialog !!!
208 if (! dp_gui::TheExtensionManager::s_ExtMgr.is())
210 const bool bAppUp = (GetpApp() != nullptr);
211 bool bOfficePipePresent;
212 try {
213 bOfficePipePresent = dp_misc::office_is_running();
215 catch (const Exception & exc) {
216 if (bAppUp) {
217 const SolarMutexGuard guard;
218 vcl::Window* pWin = Application::GetActiveTopWindow();
219 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pWin ? pWin->GetFrameWeld() : nullptr,
220 VclMessageType::Warning, VclButtonsType::Ok, exc.Message));
221 xBox->run();
223 throw;
226 if (! bOfficePipePresent) {
227 OSL_ASSERT( ! bAppUp );
228 app.reset( new MyApp );
229 if (! InitVCL() )
230 throw RuntimeException( "Cannot initialize VCL!",
231 static_cast<OWeakObject *>(this) );
232 Application::SetDisplayName(
233 utl::ConfigManager::getProductName() +
234 " " +
235 utl::ConfigManager::getProductVersion());
236 ExtensionCmdQueue::syncRepositories( m_xComponentContext );
239 else
241 // When m_bShowUpdateOnly is set, we are inside the office and the user clicked
242 // the update notification icon in the menu bar. We must not close the extensions
243 // dialog after displaying the update dialog when it has been visible before
244 if ( m_bShowUpdateOnly )
245 bCloseDialog = ! dp_gui::TheExtensionManager::s_ExtMgr->isVisible();
249 const SolarMutexGuard guard;
250 ::rtl::Reference< ::dp_gui::TheExtensionManager > myExtMgr(
251 ::dp_gui::TheExtensionManager::get(
252 m_xComponentContext,
253 m_parent ? *m_parent : Reference<awt::XWindow>(),
254 m_extensionURL ? *m_extensionURL : OUString() ) );
255 myExtMgr->createDialog( false );
256 if (!m_initialTitle.isEmpty()) {
257 myExtMgr->SetText( m_initialTitle );
258 m_initialTitle.clear();
260 if ( m_bShowUpdateOnly )
262 myExtMgr->checkUpdates();
263 if ( bCloseDialog )
264 myExtMgr->Close();
265 else
266 myExtMgr->ToTop();
268 else
270 myExtMgr->Show();
271 myExtMgr->ToTop();
275 if (app != nullptr)
277 Application::Execute();
278 DeInitVCL();
281 if (xListener.is())
282 xListener->dialogClosed(
283 ui::dialogs::DialogClosedEvent(
284 static_cast< ::cppu::OWeakObject * >(this),
285 sal_Int16(0)) );
288 // XJobExecutor
290 void ServiceImpl::trigger( OUString const &rEvent )
292 if ( rEvent == "SHOW_UPDATE_DIALOG" )
293 m_bShowUpdateOnly = true;
294 else
295 m_bShowUpdateOnly = false;
297 startExecuteModal( Reference< ui::dialogs::XDialogClosedListener >() );
300 sdecl::class_<ServiceImpl, sdecl::with_args<true> > const serviceSI;
301 sdecl::ServiceDecl const serviceDecl(
302 serviceSI,
303 "com.sun.star.comp.deployment.ui.PackageManagerDialog",
304 "com.sun.star.deployment.ui.PackageManagerDialog" );
306 sdecl::class_<LicenseDialog, sdecl::with_args<true> > const licenseSI;
307 sdecl::ServiceDecl const licenseDecl(
308 licenseSI,
309 "com.sun.star.comp.deployment.ui.LicenseDialog",
310 "com.sun.star.deployment.ui.LicenseDialog" );
312 sdecl::class_<UpdateRequiredDialogService, sdecl::with_args<true> > const updateSI;
313 sdecl::ServiceDecl const updateDecl(
314 updateSI,
315 "com.sun.star.comp.deployment.ui.UpdateRequiredDialog",
316 "com.sun.star.deployment.ui.UpdateRequiredDialog" );
317 } // namespace dp_gui
319 extern "C" {
321 SAL_DLLPUBLIC_EXPORT void * deploymentgui_component_getFactory(
322 sal_Char const * pImplName, void *, void *)
324 return sdecl::component_getFactoryHelper(
325 pImplName,
326 {&dp_gui::serviceDecl, &dp_gui::licenseDecl, &dp_gui::updateDecl});
329 } // extern "C"
331 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */