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/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <dispatch/systemexec.hxx>
23 #include <com/sun/star/system/SystemShellExecute.hpp>
24 #include <com/sun/star/util/PathSubstitution.hpp>
25 #include <com/sun/star/util/XStringSubstitution.hpp>
26 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
27 #include <com/sun/star/frame/DispatchResultState.hpp>
28 #include <cppuhelper/supportsservice.hxx>
33 constexpr OUString PROTOCOL_VALUE
= u
"systemexecute:"_ustr
;
35 // XInterface, XTypeProvider, XServiceInfo
37 OUString SAL_CALL
SystemExec::getImplementationName()
39 return "com.sun.star.comp.framework.SystemExecute";
42 sal_Bool SAL_CALL
SystemExec::supportsService( const OUString
& sServiceName
)
44 return cppu::supportsService(this, sServiceName
);
47 css::uno::Sequence
< OUString
> SAL_CALL
SystemExec::getSupportedServiceNames()
49 return { SERVICENAME_PROTOCOLHANDLER
};
52 SystemExec::SystemExec( css::uno::Reference
< css::uno::XComponentContext
> xContext
)
53 : m_xContext (std::move( xContext
))
57 SystemExec::~SystemExec()
61 css::uno::Reference
< css::frame::XDispatch
> SAL_CALL
SystemExec::queryDispatch( const css::util::URL
& aURL
,
65 css::uno::Reference
< css::frame::XDispatch
> xDispatcher
;
66 if (aURL
.Complete
.startsWith(PROTOCOL_VALUE
))
71 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > SAL_CALL
SystemExec::queryDispatches( const css::uno::Sequence
< css::frame::DispatchDescriptor
>& lDescriptor
)
73 sal_Int32 nCount
= lDescriptor
.getLength();
74 css::uno::Sequence
< css::uno::Reference
< css::frame::XDispatch
> > lDispatcher( nCount
);
75 auto lDispatcherRange
= asNonConstRange(lDispatcher
);
76 for( sal_Int32 i
=0; i
<nCount
; ++i
)
78 lDispatcherRange
[i
] = queryDispatch(
79 lDescriptor
[i
].FeatureURL
,
80 lDescriptor
[i
].FrameName
,
81 lDescriptor
[i
].SearchFlags
);
86 void SAL_CALL
SystemExec::dispatch( const css::util::URL
& aURL
,
87 const css::uno::Sequence
< css::beans::PropertyValue
>& lArguments
)
89 dispatchWithNotification(aURL
, lArguments
, css::uno::Reference
< css::frame::XDispatchResultListener
>());
92 void SAL_CALL
SystemExec::dispatchWithNotification( const css::util::URL
& aURL
,
93 const css::uno::Sequence
< css::beans::PropertyValue
>&,
94 const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
)
96 // convert "systemexec:file:///c:/temp/test.html" => "file:///c:/temp/test.html"
97 sal_Int32 c
= aURL
.Complete
.getLength()-PROTOCOL_VALUE
.getLength();
98 if (c
<1) // we don't check for valid URLs here! The system will show an error message ...
100 impl_notifyResultListener(xListener
, css::frame::DispatchResultState::FAILURE
);
103 OUString sSystemURLWithVariables
= aURL
.Complete
.copy(PROTOCOL_VALUE
.getLength(), c
);
105 // TODO check security settings ...
109 css::uno::Reference
< css::util::XStringSubstitution
> xPathSubst( css::util::PathSubstitution::create(m_xContext
) );
111 OUString sSystemURL
= xPathSubst
->substituteVariables(sSystemURLWithVariables
, true); // sal_True force an exception if unknown variables exists !
113 css::uno::Reference
< css::system::XSystemShellExecute
> xShell
= css::system::SystemShellExecute::create( m_xContext
);
115 xShell
->execute(sSystemURL
, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY
);
116 impl_notifyResultListener(xListener
, css::frame::DispatchResultState::SUCCESS
);
118 catch(const css::uno::Exception
&)
120 impl_notifyResultListener(xListener
, css::frame::DispatchResultState::FAILURE
);
124 void SAL_CALL
SystemExec::addStatusListener( const css::uno::Reference
< css::frame::XStatusListener
>&,
125 const css::util::URL
& )
130 void SAL_CALL
SystemExec::removeStatusListener( const css::uno::Reference
< css::frame::XStatusListener
>&,
131 const css::util::URL
& )
136 void SystemExec::impl_notifyResultListener(const css::uno::Reference
< css::frame::XDispatchResultListener
>& xListener
,
137 const sal_Int16 nState
)
141 css::frame::DispatchResultEvent aEvent
;
142 aEvent
.State
= nState
;
143 xListener
->dispatchFinished(aEvent
);
147 } // namespace framework
149 extern "C" SAL_DLLPUBLIC_EXPORT
css::uno::XInterface
*
150 framework_SystemExecute_get_implementation(
151 css::uno::XComponentContext
* context
, css::uno::Sequence
<css::uno::Any
> const& )
153 return cppu::acquire(new framework::SystemExec(context
));
156 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */