cid#1636693 COPY_INSTEAD_OF_MOVE
[LibreOffice.git] / sfx2 / source / appl / openuriexternally.cxx
blob4de7230afd3ea3379f8e640a13c0a72869102b8e
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/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>
29 #include <sfx2/viewsh.hxx>
30 #include <sfx2/strings.hrc>
32 namespace {
34 class URITools
36 private:
37 Timer aOpenURITimer { "sfx2::openUriExternallyTimer" };
38 OUString msURI;
39 weld::Widget* mpDialogParent;
40 bool mbHandleSystemShellExecuteException;
41 DECL_LINK(onOpenURI, Timer*, void);
43 public:
44 URITools(weld::Widget* pDialogParent)
45 : mpDialogParent(pDialogParent)
46 , mbHandleSystemShellExecuteException(false)
49 void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException);
54 void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException)
56 if (comphelper::LibreOfficeKit::isActive())
58 if (SfxViewShell* pViewShell = SfxViewShell::Current())
60 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED,
61 sURI.toUtf8());
63 delete this;
64 return;
67 mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException;
68 msURI = sURI;
70 // tdf#116305 Workaround: Use timer to bring browsers to the front
71 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 aOpenURITimer.SetTimeout(0);
77 #endif
78 aOpenURITimer.Start();
81 IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void)
83 std::unique_ptr<URITools> guard(this);
84 css::uno::Reference< css::system::XSystemShellExecute > exec(
85 css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
86 for (sal_Int32 flags = css::system::SystemShellExecuteFlags::URIS_ONLY;;) {
87 try {
88 exec->execute(msURI, OUString(), flags);
89 } catch (css::security::AccessControlException & e) {
90 if (e.LackingPermission.hasValue() || flags == 0) {
91 throw css::uno::RuntimeException(
92 "unexpected AccessControlException: " + e.Message);
94 SolarMutexGuard g;
95 std::unique_ptr<weld::MessageDialog> eb(
96 Application::CreateMessageDialog(
97 mpDialogParent, VclMessageType::Warning, VclButtonsType::YesNo,
98 SfxResId(STR_DANGEROUS_TO_OPEN)));
99 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
100 eb->set_default_response(RET_NO);
101 if (eb->run() == RET_YES) {
102 flags = 0;
103 continue;
105 } catch (css::lang::IllegalArgumentException & e) {
106 if (e.ArgumentPosition != 0) {
107 throw css::uno::RuntimeException(
108 "unexpected IllegalArgumentException: " + e.Message);
110 SolarMutexGuard g;
111 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
112 VclMessageType::Warning, VclButtonsType::Ok,
113 SfxResId(STR_NO_ABS_URI_REF)));
114 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
115 eb->run();
116 } catch (css::system::SystemShellExecuteException & e) {
117 if (!mbHandleSystemShellExecuteException) {
118 throw;
120 SolarMutexGuard g;
121 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
122 VclMessageType::Warning, VclButtonsType::Ok,
123 SfxResId(STR_NO_WEBBROWSER_FOUND)));
124 eb->set_primary_text(
125 eb->get_primary_text().replaceFirst("$(ARG1)", msURI)
126 .replaceFirst("$(ARG2)", OUString::number(e.PosixError))
127 .replaceFirst("$(ARG3)", e.Message));
128 //TODO: avoid subsequent replaceFirst acting on previous replacement
129 eb->run();
131 break;
135 void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException, weld::Widget* pDialogParent)
137 URITools* uriTools = new URITools(pDialogParent);
138 uriTools->openURI(sURI, bHandleSystemShellExecuteException);
139 } //-V773
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */