LanguageTool: don't crash if REST protocol isn't set
[LibreOffice.git] / sfx2 / source / appl / openuriexternally.cxx
blob7cc923aba24148f4858641599b5d1f5ff888c916
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/sfxresid.hxx>
21 #include <tools/urlobj.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/weld.hxx>
24 #include <openuriexternally.hxx>
25 #include <comphelper/lok.hxx>
26 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
28 #include <sfx2/viewsh.hxx>
29 #include <sfx2/strings.hrc>
31 namespace {
33 class URITools
35 private:
36 Timer aOpenURITimer { "sfx2::openUriExternallyTimer" };
37 OUString msURI;
38 weld::Widget* mpDialogParent;
39 bool mbHandleSystemShellExecuteException;
40 DECL_LINK(onOpenURI, Timer*, void);
42 public:
43 URITools(weld::Widget* pDialogParent)
44 : mpDialogParent(pDialogParent)
45 , mbHandleSystemShellExecuteException(false)
48 void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException);
53 void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException)
55 if (comphelper::LibreOfficeKit::isActive())
57 if (SfxViewShell* pViewShell = SfxViewShell::Current())
59 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED,
60 sURI.toUtf8().getStr());
62 delete this;
63 return;
66 mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException;
67 msURI = sURI;
69 // tdf#116305 Workaround: Use timer to bring browsers to the front
70 aOpenURITimer.SetInvokeHandler(LINK(this, URITools, onOpenURI));
71 #ifdef _WIN32
72 // 200ms seems to be the best compromise between responsiveness and success rate
73 aOpenURITimer.SetTimeout(200);
74 #else
75 aOpenURITimer.SetTimeout(0);
76 #endif
77 aOpenURITimer.Start();
80 IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void)
82 std::unique_ptr<URITools> guard(this);
83 css::uno::Reference< css::system::XSystemShellExecute > exec(
84 css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
85 for (sal_Int32 flags = css::system::SystemShellExecuteFlags::URIS_ONLY;;) {
86 try {
87 exec->execute(msURI, OUString(), flags);
88 } catch (css::lang::IllegalArgumentException & e) {
89 if (e.ArgumentPosition != 0) {
90 throw css::uno::RuntimeException(
91 "unexpected IllegalArgumentException: " + e.Message);
93 SolarMutexGuard g;
94 if (flags == css::system::SystemShellExecuteFlags::URIS_ONLY) {
95 std::unique_ptr<weld::MessageDialog> eb(
96 Application::CreateMessageDialog(
97 mpDialogParent, VclMessageType::Warning, VclButtonsType::OkCancel,
98 SfxResId(STR_DANGEROUS_TO_OPEN)));
99 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
100 if (eb->run() == RET_OK) {
101 flags = 0;
102 continue;
104 } else {
105 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
106 VclMessageType::Warning, VclButtonsType::Ok,
107 SfxResId(STR_NO_ABS_URI_REF)));
108 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
109 eb->run();
111 } catch (css::system::SystemShellExecuteException & e) {
112 if (!mbHandleSystemShellExecuteException) {
113 throw;
115 SolarMutexGuard g;
116 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
117 VclMessageType::Warning, VclButtonsType::Ok,
118 SfxResId(STR_NO_WEBBROWSER_FOUND)));
119 eb->set_primary_text(
120 eb->get_primary_text().replaceFirst("$(ARG1)", msURI)
121 .replaceFirst("$(ARG2)", OUString::number(e.PosixError))
122 .replaceFirst("$(ARG3)", e.Message));
123 //TODO: avoid subsequent replaceFirst acting on previous replacement
124 eb->run();
126 break;
130 void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException, weld::Widget* pDialogParent)
132 URITools* uriTools = new URITools(pDialogParent);
133 uriTools->openURI(sURI, bHandleSystemShellExecuteException);
136 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */