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/app.hxx>
21 #include <sfx2/sfxresid.hxx>
22 #include <vcl/svapp.hxx>
23 #include <vcl/window.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>
39 bool mbHandleSystemShellExecuteException
;
40 DECL_LINK(onOpenURI
, Timer
*, void);
44 : mbHandleSystemShellExecuteException(false)
47 void openURI(const OUString
& sURI
, bool bHandleSystemShellExecuteException
);
52 void URITools::openURI(const OUString
& sURI
, bool bHandleSystemShellExecuteException
)
54 if (comphelper::LibreOfficeKit::isActive())
56 if (SfxViewShell
* pViewShell
= SfxViewShell::Current())
58 pViewShell
->libreOfficeKitViewCallback(LOK_CALLBACK_HYPERLINK_CLICKED
,
59 sURI
.toUtf8().getStr());
65 mbHandleSystemShellExecuteException
= bHandleSystemShellExecuteException
;
68 // tdf#116305 Workaround: Use timer to bring browsers to the front
69 aOpenURITimer
.SetInvokeHandler(LINK(this, URITools
, onOpenURI
));
71 // 200ms seems to be the best compromise between responsiveness and success rate
72 aOpenURITimer
.SetTimeout(200);
74 aOpenURITimer
.SetTimeout(0);
76 aOpenURITimer
.SetDebugName("sfx2::openUriExternallyTimer");
77 aOpenURITimer
.Start();
80 IMPL_LINK_NOARG(URITools
, onOpenURI
, Timer
*, void)
82 css::uno::Reference
< css::system::XSystemShellExecute
> exec(
83 css::system::SystemShellExecute::create(comphelper::getProcessComponentContext()));
87 css::system::SystemShellExecuteFlags::URIS_ONLY
);
89 } catch (css::lang::IllegalArgumentException
& e
) {
90 if (e
.ArgumentPosition
!= 0) {
91 throw css::uno::RuntimeException(
92 "unexpected IllegalArgumentException: " + e
.Message
);
95 vcl::Window
*pWindow
= SfxGetpApp()->GetTopWindow();
96 std::unique_ptr
<weld::MessageDialog
> eb(Application::CreateMessageDialog(pWindow
? pWindow
->GetFrameWeld() : nullptr,
97 VclMessageType::Warning
, VclButtonsType::Ok
,
98 SfxResId(STR_NO_ABS_URI_REF
)));
99 eb
->set_primary_text(eb
->get_primary_text().replaceFirst("$(ARG1)", msURI
));
101 } catch (css::system::SystemShellExecuteException
& e
) {
102 if (!mbHandleSystemShellExecuteException
) {
106 vcl::Window
*pWindow
= SfxGetpApp()->GetTopWindow();
107 std::unique_ptr
<weld::MessageDialog
> eb(Application::CreateMessageDialog(pWindow
? pWindow
->GetFrameWeld() : nullptr,
108 VclMessageType::Warning
, VclButtonsType::Ok
,
109 SfxResId(STR_NO_WEBBROWSER_FOUND
)));
110 eb
->set_primary_text(
111 eb
->get_primary_text().replaceFirst("$(ARG1)", msURI
)
112 .replaceFirst("$(ARG2)", OUString::number(e
.PosixError
))
113 .replaceFirst("$(ARG3)", e
.Message
));
114 //TODO: avoid subsequent replaceFirst acting on previous replacement
120 void sfx2::openUriExternally(const OUString
& sURI
, bool bHandleSystemShellExecuteException
)
122 URITools
* uriTools
= new URITools
;
123 uriTools
->openURI(sURI
, bHandleSystemShellExecuteException
);
126 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */