LibreOffice Module sfx2 (master) 1
openuriexternally.cxx
Go to the documentation of this file.
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>
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
32namespace {
33
34class URITools
35{
36private:
37 Timer aOpenURITimer { "sfx2::openUriExternallyTimer" };
38 OUString msURI;
39 weld::Widget* mpDialogParent;
40 bool mbHandleSystemShellExecuteException;
41 DECL_LINK(onOpenURI, Timer*, void);
42
43public:
44 URITools(weld::Widget* pDialogParent)
45 : mpDialogParent(pDialogParent)
46 , mbHandleSystemShellExecuteException(false)
47 {
48 }
49 void openURI(const OUString& sURI, bool bHandleSystemShellExecuteException);
50};
51
52}
53
54void URITools::openURI(const OUString& sURI, bool bHandleSystemShellExecuteException)
55{
57 {
58 if (SfxViewShell* pViewShell = SfxViewShell::Current())
59 {
60 pViewShell->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED,
61 sURI.toUtf8());
62 }
63 delete this;
64 return;
65 }
66
67 mbHandleSystemShellExecuteException = bHandleSystemShellExecuteException;
68 msURI = sURI;
69
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();
79}
80
81IMPL_LINK_NOARG(URITools, onOpenURI, Timer*, void)
82{
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);
93 }
95 std::unique_ptr<weld::MessageDialog> eb(
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;
103 }
104 } catch (css::lang::IllegalArgumentException & e) {
105 if (e.ArgumentPosition != 0) {
106 throw css::uno::RuntimeException(
107 "unexpected IllegalArgumentException: " + e.Message);
108 }
110 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
111 VclMessageType::Warning, VclButtonsType::Ok,
112 SfxResId(STR_NO_ABS_URI_REF)));
113 eb->set_primary_text(eb->get_primary_text().replaceFirst("$(ARG1)", INetURLObject::decode(msURI, INetURLObject::DecodeMechanism::Unambiguous)));
114 eb->run();
115 } catch (css::system::SystemShellExecuteException & e) {
116 if (!mbHandleSystemShellExecuteException) {
117 throw;
118 }
120 std::unique_ptr<weld::MessageDialog> eb(Application::CreateMessageDialog(mpDialogParent,
121 VclMessageType::Warning, VclButtonsType::Ok,
122 SfxResId(STR_NO_WEBBROWSER_FOUND)));
123 eb->set_primary_text(
124 eb->get_primary_text().replaceFirst("$(ARG1)", msURI)
125 .replaceFirst("$(ARG2)", OUString::number(e.PosixError))
126 .replaceFirst("$(ARG3)", e.Message));
127 //TODO: avoid subsequent replaceFirst acting on previous replacement
128 eb->run();
129 }
130 break;
131 }
132}
133
134void sfx2::openUriExternally(const OUString& sURI, bool bHandleSystemShellExecuteException, weld::Widget* pDialogParent)
135{
136 URITools* uriTools = new URITools(pDialogParent);
137 uriTools->openURI(sURI, bHandleSystemShellExecuteException);
138}
139
140/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
static weld::MessageDialog * CreateMessageDialog(weld::Widget *pParent, VclMessageType eMessageType, VclButtonsType eButtonType, const OUString &rPrimaryMessage, const ILibreOfficeKitNotifier *pNotifier=nullptr)
static OUString decode(std::u16string_view rText, DecodeMechanism eMechanism, rtl_TextEncoding eCharset=RTL_TEXTENCODING_UTF8)
One SfxViewShell more or less represents one edit window for a document, there can be multiple ones f...
Definition: viewsh.hxx:165
static SAL_WARN_UNUSED_RESULT SfxViewShell * Current()
Definition: viewsh.cxx:1848
DECL_LINK(CheckNameHdl, SvxNameDialog &, bool)
Reference< XComponentContext > getProcessComponentContext()
void openUriExternally(const OUString &sURI, bool bHandleSystemShellExecuteException, weld::Widget *pDialogParent)
Open a URI via com.sun.star.system.SystemShellExecute.
IMPL_LINK_NOARG(URITools, onOpenURI, Timer *, void)
OUString SfxResId(TranslateId aId)
Definition: sfxresid.cxx:22
RET_OK