Version 7.1.7.1, tag libreoffice-7.1.7.1
[LibreOffice.git] / sfx2 / source / appl / openuriexternally.cxx
blobebff4452a1c74bbf8f50226978150f91fd3ab017
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/.
8 */
10 #include <sal/config.h>
12 #include <com/sun/star/lang/IllegalArgumentException.hpp>
13 #include <com/sun/star/system/SystemShellExecute.hpp>
14 #include <com/sun/star/system/SystemShellExecuteException.hpp>
15 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
16 #include <com/sun/star/uno/Reference.hxx>
17 #include <com/sun/star/uno/RuntimeException.hpp>
18 #include <comphelper/processfactory.hxx>
19 #include <rtl/ustring.hxx>
20 #include <sfx2/app.hxx>
21 #include <sfx2/sfxresid.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/window.hxx>
24 #include <vcl/weld.hxx>
25 #include <openuriexternally.hxx>
26 #include <comphelper/lok.hxx>
27 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
29 #include <sfx2/viewsh.hxx>
30 #include <sfx2/strings.hrc>
32 namespace {
34 class URITools
36 private:
37 Timer aOpenURITimer;
38 OUString msURI;
39 bool mbHandleSystemShellExecuteException;
40 DECL_LINK(onOpenURI, Timer*, void);
42 public:
43 URITools()
44 : mbHandleSystemShellExecuteException(false)
47 void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException);
52 void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException)
54 if (comphelper::LibreOfficeKit::isActive())
56 if (SfxViewShell* pViewShell = SfxViewShell::Current())
58 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED,
59 sURI.toUtf8().getStr());
61 delete this;
62 return;
65 mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException;
66 msURI = sURI;
68 // tdf#116305 Workaround: Use timer to bring browsers to the front
69 aOpenURITimer.SetInvokeHandler(LINK(this, URITools, onOpenURI));
70 #ifdef _WIN32
71 // 200ms seems to be the best compromise between responsiveness and success rate
72 aOpenURITimer.SetTimeout(200);
73 #else
74 aOpenURITimer.SetTimeout(0);
75 #endif
76 aOpenURITimer.SetDebugName("sfx2::openUriExternallyTimer");
77 aOpenURITimer.Start();
80 IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void)
82 css::uno::Reference< css::system::XSystemShellExecute > exec(
83 css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
84 try {
85 exec->execute(
86 msURI, OUString(),
87 css::system::SystemShellExecuteFlags::URIS_ONLY);
88 return;
89 } catch (css::lang::IllegalArgumentException & e) {
90 if (e.ArgumentPosition != 0) {
91 throw css::uno::RuntimeException(
92 "unexpected IllegalArgumentException: " + e.Message);
94 SolarMutexGuard g;
95 vcl::Window *pWindow = SfxGetpApp()->GetTopWindow();
96 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(pWindow ? pWindow->GetFrameWeld() : nullptr,
97 VclMessageType::Warning, VclButtonsType::Ok,
98 SfxResId(STR_NO_ABS_URI_REF)));
99 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", msURI));
100 eb->run();
101 } catch (css::system::SystemShellExecuteException & e) {
102 if (!mbHandleSystemShellExecuteException) {
103 throw;
105 SolarMutexGuard g;
106 vcl::Window *pWindow = SfxGetpApp()->GetTopWindow();
107 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(pWindow ? pWindow->GetFrameWeld() : nullptr,
108 VclMessageType::Warning, VclButtonsType::Ok,
109 SfxResId(STR_NO_WEBBROWSER_FOUND)));
110 eb->set_primary_text(
111 eb->get_primary_text().replaceFirst("$(ARG1)", msURI)
112 .replaceFirst("$(ARG2)", OUString::number(e.PosixError))
113 .replaceFirst("$(ARG3)", e.Message));
114 //TODO: avoid subsequent replaceFirst acting on previous replacement
115 eb->run();
117 delete this;
120 void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException)
122 URITools* uriTools = new URITools;
123 uriTools->openURI(sURI, bHandleSystemShellExecuteException);
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */