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/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>
37 Timer aOpenURITimer
{ "sfx2::openUriExternallyTimer" };
39 weld::Widget
* mpDialogParent
;
40 bool mbHandleSystemShellExecuteException
;
41 DECL_LINK(onOpenURI
, Timer
*, void);
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
,
67 mbHandleSystemShellExecuteException
= bHandleSystemShellExecuteException
;
70 // tdf#116305 Workaround: Use timer to bring browsers to the front
71 aOpenURITimer
.SetInvokeHandler(LINK(this, URITools
, onOpenURI
));
73 // 200ms seems to be the best compromise between responsiveness and success rate
74 aOpenURITimer
.SetTimeout(200);
76 aOpenURITimer
.SetTimeout(0);
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
;;) {
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
);
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
) {
105 } catch (css::lang::IllegalArgumentException
& e
) {
106 if (e
.ArgumentPosition
!= 0) {
107 throw css::uno::RuntimeException(
108 "unexpected IllegalArgumentException: " + e
.Message
);
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
)));
116 } catch (css::system::SystemShellExecuteException
& e
) {
117 if (!mbHandleSystemShellExecuteException
) {
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
135 void sfx2::openUriExternally(const OUString
& sURI
, bool bHandleSystemShellExecuteException
, weld::Widget
* pDialogParent
)
137 URITools
* uriTools
= new URITools(pDialogParent
);
138 uriTools
->openURI(sURI
, bHandleSystemShellExecuteException
);
141 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */