use insert function instead of for loop
[LibreOffice.git] / desktop / source / deployment / gui / dp_gui_service.cxx
blob23733bd349aae66586ac10edd423ab4992d0eb91
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_theextmgr.hxx"
23 #include <osl/diagnose.h>
24 #include <cppuhelper/implbase.hxx>
25 #include <cppuhelper/supportsservice.hxx>
26 #include <unotools/configmgr.hxx>
27 #include <comphelper/processfactory.hxx>
28 #include <comphelper/unwrapargs.hxx>
29 #include <unotools/resmgr.hxx>
30 #include <vcl/weld.hxx>
31 #include <vcl/window.hxx>
32 #include <vcl/svapp.hxx>
33 #include <com/sun/star/task/XJobExecutor.hpp>
34 #include <com/sun/star/ui/dialogs/XAsynchronousExecutableDialog.hpp>
36 #include <optional>
37 #include "license_dialog.hxx"
38 #include "dp_gui_dialog2.hxx"
39 #include "dp_gui_extensioncmdqueue.hxx"
40 #include <dp_misc.h>
42 using namespace ::dp_misc;
43 using namespace ::com::sun::star;
44 using namespace ::com::sun::star::uno;
46 namespace dp_gui {
48 namespace {
50 class MyApp : public Application
52 public:
53 MyApp();
55 MyApp(const MyApp&) = delete;
56 const MyApp& operator=(const MyApp&) = delete;
58 // Application
59 virtual int Main() override;
60 virtual void DeInit() override;
65 MyApp::MyApp()
70 int MyApp::Main()
72 return EXIT_SUCCESS;
75 void MyApp::DeInit()
77 const css::uno::Reference< css::uno::XComponentContext >& context(
78 comphelper::getProcessComponentContext());
79 dp_misc::disposeBridges(context);
80 css::uno::Reference< css::lang::XComponent >(
81 context, css::uno::UNO_QUERY_THROW)->dispose();
82 comphelper::setProcessServiceFactory(nullptr);
85 static OUString ReplaceProductNameHookProc( const OUString& rStr )
87 if (rStr.indexOf( "%PRODUCT" ) == -1)
88 return rStr;
90 static const OUString sProductName = utl::ConfigManager::getProductName();
91 static const OUString sVersion = utl::ConfigManager::getProductVersion();
92 static const OUString sAboutBoxVersion = utl::ConfigManager::getAboutBoxProductVersion();
93 static const OUString sAboutBoxVersionSuffix = utl::ConfigManager::getAboutBoxProductVersionSuffix();
94 static const OUString sExtension = utl::ConfigManager::getProductExtension();
95 static const OUString sOOOVendor = utl::ConfigManager::getVendor();
97 OUString sRet = rStr.replaceAll( "%PRODUCTNAME", sProductName );
98 sRet = sRet.replaceAll( "%PRODUCTVERSION", sVersion );
99 sRet = sRet.replaceAll( "%ABOUTBOXPRODUCTVERSIONSUFFIX", sAboutBoxVersionSuffix );
100 sRet = sRet.replaceAll( "%ABOUTBOXPRODUCTVERSION", sAboutBoxVersion );
101 sRet = sRet.replaceAll( "%OOOVENDOR", sOOOVendor );
102 sRet = sRet.replaceAll( "%PRODUCTEXTENSION", sExtension );
103 return sRet;
106 namespace {
108 class ServiceImpl
109 : public ::cppu::WeakImplHelper<ui::dialogs::XAsynchronousExecutableDialog,
110 task::XJobExecutor, css::lang::XServiceInfo>
112 Reference<XComponentContext> const m_xComponentContext;
113 std::optional< Reference<awt::XWindow> > /* const */ m_parent;
114 std::optional<OUString> m_extensionURL;
115 OUString m_initialTitle;
116 bool m_bShowUpdateOnly;
118 public:
119 ServiceImpl( Sequence<Any> const & args,
120 Reference<XComponentContext> const & xComponentContext );
122 // XServiceInfo
123 virtual OUString SAL_CALL getImplementationName() override;
124 virtual sal_Bool SAL_CALL supportsService( const OUString& ServiceName ) override;
125 virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override;
127 // XAsynchronousExecutableDialog
128 virtual void SAL_CALL setDialogTitle( OUString const & aTitle ) override;
129 virtual void SAL_CALL startExecuteModal(
130 Reference< ui::dialogs::XDialogClosedListener > const & xListener ) override;
132 // XJobExecutor
133 virtual void SAL_CALL trigger( OUString const & event ) override;
138 ServiceImpl::ServiceImpl( Sequence<Any> const& args,
139 Reference<XComponentContext> const& xComponentContext)
140 : m_xComponentContext(xComponentContext),
141 m_bShowUpdateOnly( false )
143 /* if true then this service is running in a unopkg process and not in an office process */
144 std::optional<OUString> view;
145 try {
146 std::optional<sal_Bool> unopkg;
147 comphelper::unwrapArgs( args, m_parent, view, unopkg );
148 return;
149 } catch ( const css::lang::IllegalArgumentException & ) {
151 try {
152 comphelper::unwrapArgs( args, m_extensionURL);
153 } catch ( const css::lang::IllegalArgumentException & ) {
156 ResHookProc pProc = Translate::GetReadStringHook();
157 if ( !pProc )
158 Translate::SetReadStringHook(ReplaceProductNameHookProc);
161 // XServiceInfo
162 OUString ServiceImpl::getImplementationName()
164 return u"com.sun.star.comp.deployment.ui.PackageManagerDialog"_ustr;
167 sal_Bool ServiceImpl::supportsService( const OUString& ServiceName )
169 return cppu::supportsService(this, ServiceName);
172 css::uno::Sequence< OUString > ServiceImpl::getSupportedServiceNames()
174 return { u"com.sun.star.deployment.ui.PackageManagerDialog"_ustr };
177 // XAsynchronousExecutableDialog
179 void ServiceImpl::setDialogTitle( OUString const & title )
181 if ( dp_gui::TheExtensionManager::s_ExtMgr.is() )
183 const SolarMutexGuard guard;
184 ::rtl::Reference< ::dp_gui::TheExtensionManager > dialog(
185 ::dp_gui::TheExtensionManager::get( m_xComponentContext,
186 m_parent ? *m_parent : Reference<awt::XWindow>(),
187 m_extensionURL ? *m_extensionURL : OUString() ) );
188 dialog->SetText( title );
190 else
191 m_initialTitle = title;
195 void ServiceImpl::startExecuteModal(
196 Reference< ui::dialogs::XDialogClosedListener > const & xListener )
198 bool bCloseDialog = true; // only used if m_bShowUpdateOnly is true
199 std::unique_ptr<Application> app;
200 //ToDo: synchronize access to s_dialog !!!
201 if (! dp_gui::TheExtensionManager::s_ExtMgr.is())
203 const bool bAppUp = (GetpApp() != nullptr);
204 bool bOfficePipePresent;
205 try {
206 bOfficePipePresent = dp_misc::office_is_running();
208 catch (const Exception & exc) {
209 if (bAppUp) {
210 const SolarMutexGuard guard;
211 vcl::Window* pWin = Application::GetActiveTopWindow();
212 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(pWin ? pWin->GetFrameWeld() : nullptr,
213 VclMessageType::Warning, VclButtonsType::Ok, exc.Message));
214 xBox->run();
216 throw;
219 if (! bOfficePipePresent) {
220 OSL_ASSERT( ! bAppUp );
221 app.reset( new MyApp );
222 if (! InitVCL() )
223 throw RuntimeException( u"Cannot initialize VCL!"_ustr,
224 static_cast<OWeakObject *>(this) );
225 Application::SetDisplayName(
226 utl::ConfigManager::getProductName() +
227 " " +
228 utl::ConfigManager::getProductVersion());
229 ExtensionCmdQueue::syncRepositories( m_xComponentContext );
232 else
234 // When m_bShowUpdateOnly is set, we are inside the office and the user clicked
235 // the update notification icon in the menu bar. We must not close the extensions
236 // dialog after displaying the update dialog when it has been visible before
237 if ( m_bShowUpdateOnly )
238 bCloseDialog = ! dp_gui::TheExtensionManager::s_ExtMgr->isVisible();
242 const SolarMutexGuard guard;
243 ::rtl::Reference< ::dp_gui::TheExtensionManager > myExtMgr(
244 ::dp_gui::TheExtensionManager::get(
245 m_xComponentContext,
246 m_parent ? *m_parent : Reference<awt::XWindow>(),
247 m_extensionURL ? *m_extensionURL : OUString() ) );
248 myExtMgr->createDialog( false );
249 if (!m_initialTitle.isEmpty()) {
250 myExtMgr->SetText( m_initialTitle );
251 m_initialTitle.clear();
253 if ( m_bShowUpdateOnly )
255 myExtMgr->checkUpdates();
256 if ( bCloseDialog )
257 myExtMgr->Close();
258 else
259 myExtMgr->ToTop();
261 else
263 myExtMgr->Show();
264 myExtMgr->ToTop();
268 if (app != nullptr)
270 Application::Execute();
271 DeInitVCL();
274 if (xListener.is())
275 xListener->dialogClosed(
276 ui::dialogs::DialogClosedEvent(
277 static_cast< ::cppu::OWeakObject * >(this),
278 sal_Int16(0)) );
281 // XJobExecutor
283 void ServiceImpl::trigger( OUString const &rEvent )
285 if ( rEvent == "SHOW_UPDATE_DIALOG" )
286 m_bShowUpdateOnly = true;
287 else
288 m_bShowUpdateOnly = false;
290 startExecuteModal( Reference< ui::dialogs::XDialogClosedListener >() );
293 } // namespace dp_gui
295 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
296 desktop_LicenseDialog_get_implementation(
297 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
299 return cppu::acquire(new dp_gui::LicenseDialog(args, context));
302 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
303 desktop_ServiceImpl_get_implementation(
304 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
306 return cppu::acquire(new dp_gui::ServiceImpl(args, context));
309 extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
310 desktop_UpdateRequiredDialogService_get_implementation(
311 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& args)
313 return cppu::acquire(new dp_gui::UpdateRequiredDialogService(args, context));
316 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */