/src/libreoffice/sfx2/source/appl/openuriexternally.cxx
Line | Count | Source (jump to first uncovered line) |
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/security/AccessControlException.hpp> |
14 | | #include <com/sun/star/system/SystemShellExecute.hpp> |
15 | | #include <com/sun/star/system/SystemShellExecuteException.hpp> |
16 | | #include <com/sun/star/system/SystemShellExecuteFlags.hpp> |
17 | | #include <com/sun/star/uno/Reference.hxx> |
18 | | #include <com/sun/star/uno/RuntimeException.hpp> |
19 | | #include <comphelper/processfactory.hxx> |
20 | | #include <rtl/ustring.hxx> |
21 | | #include <sfx2/sfxresid.hxx> |
22 | | #include <tools/urlobj.hxx> |
23 | | #include <vcl/svapp.hxx> |
24 | | #include <vcl/weld.hxx> |
25 | | #include <openuriexternally.hxx> |
26 | | #include <comphelper/lok.hxx> |
27 | | #include <LibreOfficeKit/LibreOfficeKitEnums.h> |
28 | | |
29 | | #include <sfx2/viewsh.hxx> |
30 | | #include <sfx2/strings.hrc> |
31 | | |
32 | | namespace { |
33 | | |
34 | | class URITools |
35 | | { |
36 | | private: |
37 | | Timer aOpenURITimer { "sfx2::openUriExternallyTimer" }; |
38 | | OUString msURI; |
39 | | weld::Widget* mpDialogParent; |
40 | | bool mbHandleSystemShellExecuteException; |
41 | | DECL_LINK(onOpenURI, Timer*, void); |
42 | | |
43 | | public: |
44 | | URITools(weld::Widget* pDialogParent) |
45 | 0 | : mpDialogParent(pDialogParent) |
46 | 0 | , mbHandleSystemShellExecuteException(false) |
47 | 0 | { |
48 | 0 | } |
49 | | void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException); |
50 | | }; |
51 | | |
52 | | } |
53 | | |
54 | | void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException) |
55 | 0 | { |
56 | 0 | if (comphelper::LibreOfficeKit::isActive()) |
57 | 0 | { |
58 | 0 | if (SfxViewShell* pViewShell = SfxViewShell::Current()) |
59 | 0 | { |
60 | 0 | pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED, |
61 | 0 | sURI.toUtf8()); |
62 | 0 | } |
63 | 0 | delete this; |
64 | 0 | return; |
65 | 0 | } |
66 | | |
67 | 0 | mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException; |
68 | 0 | msURI = sURI; |
69 | | |
70 | | // tdf#116305 Workaround: Use timer to bring browsers to the front |
71 | 0 | aOpenURITimer.SetInvokeHandler(LINK(this, URITools, onOpenURI)); |
72 | | #ifdef _WIN32 |
73 | | // 200ms seems to be the best compromise between responsiveness and success rate |
74 | | aOpenURITimer.SetTimeout(200); |
75 | | #else |
76 | 0 | aOpenURITimer.SetTimeout(0); |
77 | 0 | #endif |
78 | 0 | aOpenURITimer.Start(); |
79 | 0 | } |
80 | | |
81 | | IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void) |
82 | 0 | { |
83 | 0 | std::unique_ptr<URITools> guard(this); |
84 | 0 | css::uno::Reference< css::system::XSystemShellExecute > exec( |
85 | 0 | css::system::SystemShellExecute::create(comphelper::getProcessComponentContext())); |
86 | 0 | for (sal_Int32 flags = css::system::SystemShellExecuteFlags::URIS_ONLY;;) { |
87 | 0 | try { |
88 | 0 | exec->execute(msURI, OUString(), flags); |
89 | 0 | } catch (css::security::AccessControlException & e) { |
90 | 0 | if (e.LackingPermission.hasValue() || flags == 0) { |
91 | 0 | throw css::uno::RuntimeException( |
92 | 0 | "unexpected AccessControlException: " + e.Message); |
93 | 0 | } |
94 | 0 | SolarMutexGuard g; |
95 | 0 | std::unique_ptr<weld::MessageDialog> eb( |
96 | 0 | Application::CreateMessageDialog( |
97 | 0 | mpDialogParent, VclMessageType::Warning, VclButtonsType::YesNo, |
98 | 0 | SfxResId(STR_DANGEROUS_TO_OPEN))); |
99 | 0 | eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous))); |
100 | 0 | eb->set_default_response(RET_NO); |
101 | 0 | if (eb->run() == RET_YES) { |
102 | 0 | flags = 0; |
103 | 0 | continue; |
104 | 0 | } |
105 | 0 | } catch (css::lang::IllegalArgumentException & e) { |
106 | 0 | if (e.ArgumentPosition != 0) { |
107 | 0 | throw css::uno::RuntimeException( |
108 | 0 | "unexpected IllegalArgumentException: " + e.Message); |
109 | 0 | } |
110 | 0 | SolarMutexGuard g; |
111 | 0 | std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent, |
112 | 0 | VclMessageType::Warning, VclButtonsType::Ok, |
113 | 0 | SfxResId(STR_NO_ABS_URI_REF))); |
114 | 0 | eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous))); |
115 | 0 | eb->run(); |
116 | 0 | } catch (css::system::SystemShellExecuteException & e) { |
117 | 0 | if (!mbHandleSystemShellExecuteException) { |
118 | 0 | throw; |
119 | 0 | } |
120 | 0 | SolarMutexGuard g; |
121 | 0 | std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent, |
122 | 0 | VclMessageType::Warning, VclButtonsType::Ok, |
123 | 0 | SfxResId(STR_NO_WEBBROWSER_FOUND))); |
124 | 0 | eb->set_primary_text( |
125 | 0 | eb->get_primary_text().replaceFirst("$(ARG1)", msURI) |
126 | 0 | .replaceFirst("$(ARG2)", OUString::number(e.PosixError)) |
127 | 0 | .replaceFirst("$(ARG3)", e.Message)); |
128 | | //TODO: avoid subsequent replaceFirst acting on previous replacement |
129 | 0 | eb->run(); |
130 | 0 | } |
131 | 0 | break; |
132 | 0 | } |
133 | 0 | } |
134 | | |
135 | | void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException, weld::Widget* pDialogParent) |
136 | 0 | { |
137 | 0 | URITools* uriTools = new URITools(pDialogParent); |
138 | 0 | uriTools->openURI(sURI, bHandleSystemShellExecuteException); |
139 | 0 | } //-V773 |
140 | | |
141 | | /* vim:set shiftwidth=4 softtabstop=4 expandtab: */ |