LibreOffice Module framework (master) 1
systemexec.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 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
21#include <services.h>
22
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>
29#include <utility>
30
31namespace framework{
32
33constexpr OUStringLiteral PROTOCOL_VALUE = u"systemexecute:";
34
35// XInterface, XTypeProvider, XServiceInfo
36
38{
39 return "com.sun.star.comp.framework.SystemExecute";
40}
41
42sal_Bool SAL_CALL SystemExec::supportsService( const OUString& sServiceName )
43{
45}
46
47css::uno::Sequence< OUString > SAL_CALL SystemExec::getSupportedServiceNames()
48{
50}
51
52SystemExec::SystemExec( css::uno::Reference< css::uno::XComponentContext > xContext )
53 : m_xContext (std::move( xContext ))
54{
55}
56
58{
59}
60
61css::uno::Reference< css::frame::XDispatch > SAL_CALL SystemExec::queryDispatch( const css::util::URL& aURL ,
62 const OUString&,
63 sal_Int32 )
64{
65 css::uno::Reference< css::frame::XDispatch > xDispatcher;
66 if (aURL.Complete.startsWith(PROTOCOL_VALUE))
67 xDispatcher = this;
68 return xDispatcher;
69}
70
71css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL SystemExec::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor )
72{
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 )
77 {
78 lDispatcherRange[i] = queryDispatch(
79 lDescriptor[i].FeatureURL,
80 lDescriptor[i].FrameName,
81 lDescriptor[i].SearchFlags);
82 }
83 return lDispatcher;
84}
85
86void SAL_CALL SystemExec::dispatch( const css::util::URL& aURL ,
87 const css::uno::Sequence< css::beans::PropertyValue >& lArguments )
88{
89 dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
90}
91
92void 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 )
95{
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 ...
99 {
100 impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
101 return;
102 }
103 OUString sSystemURLWithVariables = aURL.Complete.copy(PROTOCOL_VALUE.getLength(), c);
104
105 // TODO check security settings ...
106
107 try
108 {
109 css::uno::Reference< css::util::XStringSubstitution > xPathSubst( css::util::PathSubstitution::create(m_xContext) );
110
111 OUString sSystemURL = xPathSubst->substituteVariables(sSystemURLWithVariables, true); // sal_True force an exception if unknown variables exists !
112
113 css::uno::Reference< css::system::XSystemShellExecute > xShell = css::system::SystemShellExecute::create( m_xContext );
114
115 xShell->execute(sSystemURL, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY);
116 impl_notifyResultListener(xListener, css::frame::DispatchResultState::SUCCESS);
117 }
118 catch(const css::uno::Exception&)
119 {
120 impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
121 }
122}
123
124void SAL_CALL SystemExec::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
125 const css::util::URL& )
126{
127 // not supported yet
128}
129
130void SAL_CALL SystemExec::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
131 const css::util::URL& )
132{
133 // not supported yet
134}
135
136void SystemExec::impl_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener,
137 const sal_Int16 nState )
138{
139 if (xListener.is())
140 {
141 css::frame::DispatchResultEvent aEvent;
142 aEvent.State = nState;
143 xListener->dispatchFinished(aEvent);
144 }
145}
146
147} // namespace framework
148
149extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
151 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any> const& )
152{
153 return cppu::acquire(new framework::SystemExec(context));
154}
155
156/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
constexpr OUStringLiteral sServiceName
AnyEventRef aEvent
protocol handler for "systemexec:*" URLs @descr It's a special dispatch/provider object which is regi...
Definition: systemexec.hxx:51
virtual OUString SAL_CALL getImplementationName() override
Definition: systemexec.cxx:37
void impl_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener > &xListener, const sal_Int16 nState)
Definition: systemexec.cxx:136
virtual sal_Bool SAL_CALL supportsService(const OUString &sServiceName) override
Definition: systemexec.cxx:42
virtual void SAL_CALL removeStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xListener, const css::util::URL &aURL) override
Definition: systemexec.cxx:130
css::uno::Reference< css::uno::XComponentContext > m_xContext
reference to global uno service manager which had created us
Definition: systemexec.hxx:56
virtual void SAL_CALL dispatchWithNotification(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &lArguments, const css::uno::Reference< css::frame::XDispatchResultListener > &xListener) override
Definition: systemexec.cxx:92
virtual css::uno::Sequence< OUString > SAL_CALL getSupportedServiceNames() override
Definition: systemexec.cxx:47
virtual css::uno::Reference< css::frame::XDispatch > SAL_CALL queryDispatch(const css::util::URL &aURL, const OUString &sTarget, sal_Int32 nFlags) override
Definition: systemexec.cxx:61
SystemExec(css::uno::Reference< css::uno::XComponentContext > xContext)
Definition: systemexec.cxx:52
virtual css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL queryDispatches(const css::uno::Sequence< css::frame::DispatchDescriptor > &lDescriptor) override
Definition: systemexec.cxx:71
virtual void SAL_CALL addStatusListener(const css::uno::Reference< css::frame::XStatusListener > &xListener, const css::util::URL &aURL) override
Definition: systemexec.cxx:124
virtual void SAL_CALL dispatch(const css::util::URL &aURL, const css::uno::Sequence< css::beans::PropertyValue > &lArguments) override
Definition: systemexec.cxx:86
virtual ~SystemExec() override
Definition: systemexec.cxx:57
int nCount
URL aURL
float u
sal_Int32 nState
css::uno::Reference< css::uno::XComponentContext > m_xContext
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
constexpr OUStringLiteral PROTOCOL_VALUE
constexpr OUStringLiteral SERVICENAME_PROTOCOLHANDLER
Definition: services.h:32
int i
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * framework_SystemExecute_get_implementation(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Definition: systemexec.cxx:150
unsigned char sal_Bool