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
10 #include <sal/config.h>
11
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/weld.hxx>
24 #include <openuriexternally.hxx>
25 #include <comphelper/lok.hxx>
26 #include <LibreOfficeKit/LibreOfficeKitEnums.h>
27
28 #include <sfx2/viewsh.hxx>
29 #include <sfx2/strings.hrc>
30
31 namespace {
32
33 class URITools
34 {
35 private:
36 Timer aOpenURITimer;
37 OUString msURI;
38 bool mbHandleSystemShellExecuteException;
39 DECL_LINK(onOpenURI, Timer*, void);
40
41 public:
URITools()42 URITools()
43 : mbHandleSystemShellExecuteException(false)
44 {
45 }
46 void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException);
47 };
48
49 }
50
openURI(const OUString & sURI,bool bHandleSystemShellExecuteException)51 void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException)
52 {
53 if (comphelper::LibreOfficeKit::isActive())
54 {
55 if (SfxViewShell* pViewShell = SfxViewShell::Current())
56 {
57 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED,
58 sURI.toUtf8().getStr());
59 }
60 delete this;
61 return;
62 }
63
64 mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException;
65 msURI = sURI;
66
67 // tdf#116305 Workaround: Use timer to bring browsers to the front
68 aOpenURITimer.SetInvokeHandler(LINK(this, URITools, onOpenURI));
69 #ifdef _WIN32
70 // 200ms seems to be the best compromise between responsiveness and success rate
71 aOpenURITimer.SetTimeout(200);
72 #else
73 aOpenURITimer.SetTimeout(0);
74 #endif
75 aOpenURITimer.SetDebugName("sfx2::openUriExternallyTimer");
76 aOpenURITimer.Start();
77 }
78
IMPL_LINK_NOARG(URITools,onOpenURI,Timer *,void)79 IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void)
80 {
81 css::uno::Reference< css::system::XSystemShellExecute > exec(
82 css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
83 try {
84 exec->execute(
85 msURI, OUString(),
86 css::system::SystemShellExecuteFlags::URIS_ONLY);
87 return;
88 } catch (css::lang::IllegalArgumentException & e) {
89 if (e.ArgumentPosition != 0) {
90 throw css::uno::RuntimeException(
91 "unexpected IllegalArgumentException: " + e.Message);
92 }
93 SolarMutexGuard g;
94 weld::Window *pWindow = SfxGetpApp()->GetTopWindow();
95 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(pWindow,
96 VclMessageType::Warning, VclButtonsType::Ok,
97 SfxResId(STR_NO_ABS_URI_REF)));
98 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", msURI));
99 eb->run();
100 } catch (css::system::SystemShellExecuteException & e) {
101 if (!mbHandleSystemShellExecuteException) {
102 throw;
103 }
104 SolarMutexGuard g;
105 weld::Window *pWindow = SfxGetpApp()->GetTopWindow();
106 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(pWindow,
107 VclMessageType::Warning, VclButtonsType::Ok,
108 SfxResId(STR_NO_WEBBROWSER_FOUND)));
109 eb->set_primary_text(
110 eb->get_primary_text().replaceFirst("$(ARG1)", msURI)
111 .replaceFirst("$(ARG2)", OUString::number(e.PosixError))
112 .replaceFirst("$(ARG3)", e.Message));
113 //TODO: avoid subsequent replaceFirst acting on previous replacement
114 eb->run();
115 }
116 delete this;
117 }
118
openUriExternally(const OUString & sURI,bool bHandleSystemShellExecuteException)119 void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException)
120 {
121 URITools* uriTools = new URITools;
122 uriTools->openURI(sURI, bHandleSystemShellExecuteException);
123 }
124
125 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
126