1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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/.
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>
36 Timer aOpenURITimer
{ "sfx2::openUriExternallyTimer" };
38 weld::Widget
* mpDialogParent
;
39 bool mbHandleSystemShellExecuteException
;
40 DECL_LINK(onOpenURI
, Timer
*, void);
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());
66 mbHandleSystemShellExecuteException
= bHandleSystemShellExecuteException
;
69 // tdf#116305 Workaround: Use timer to bring browsers to the front
70 aOpenURITimer
.SetInvokeHandler(LINK(this, URITools
, onOpenURI
));
72 // 200ms seems to be the best compromise between responsiveness and success rate
73 aOpenURITimer
.SetTimeout(200);
75 aOpenURITimer
.SetTimeout(0);
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
;;) {
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
);
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
) {
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
)));
111 } catch (css::system::SystemShellExecuteException
& e
) {
112 if (!mbHandleSystemShellExecuteException
) {
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
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: */