Branch libreoffice-5-0-4
[LibreOffice.git] / framework / source / dispatch / systemexec.cxx
blob97975a1f4b9671b409c8559c1a6a51762c201b58
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
21 #include <general.h>
22 #include <services.h>
24 #include <com/sun/star/system/SystemShellExecute.hpp>
25 #include <com/sun/star/util/PathSubstitution.hpp>
26 #include <com/sun/star/util/XStringSubstitution.hpp>
27 #include <com/sun/star/system/SystemShellExecuteFlags.hpp>
28 #include <com/sun/star/frame/DispatchResultState.hpp>
30 #include <vcl/svapp.hxx>
31 #include <comphelper/processfactory.hxx>
33 namespace framework{
35 #define PROTOCOL_VALUE "systemexecute:"
36 #define PROTOCOL_LENGTH 14
38 // XInterface, XTypeProvider, XServiceInfo
40 DEFINE_XSERVICEINFO_MULTISERVICE_2(SystemExec ,
41 ::cppu::OWeakObject ,
42 SERVICENAME_PROTOCOLHANDLER ,
43 IMPLEMENTATIONNAME_SYSTEMEXEC)
45 DEFINE_INIT_SERVICE(SystemExec,
47 /*Attention
48 I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance()
49 to create a new instance of this class by our own supported service factory.
50 see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further information!
55 SystemExec::SystemExec( const css::uno::Reference< css::uno::XComponentContext >& rxContext )
56 : m_xContext ( rxContext )
60 SystemExec::~SystemExec()
64 css::uno::Reference< css::frame::XDispatch > SAL_CALL SystemExec::queryDispatch( const css::util::URL& aURL ,
65 const OUString&,
66 sal_Int32 ) throw( css::uno::RuntimeException, std::exception )
68 css::uno::Reference< css::frame::XDispatch > xDispatcher;
69 if (aURL.Complete.startsWith(PROTOCOL_VALUE))
70 xDispatcher = this;
71 return xDispatcher;
74 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > SAL_CALL SystemExec::queryDispatches( const css::uno::Sequence< css::frame::DispatchDescriptor >& lDescriptor ) throw( css::uno::RuntimeException, std::exception )
76 sal_Int32 nCount = lDescriptor.getLength();
77 css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount );
78 for( sal_Int32 i=0; i<nCount; ++i )
80 lDispatcher[i] = this->queryDispatch(
81 lDescriptor[i].FeatureURL,
82 lDescriptor[i].FrameName,
83 lDescriptor[i].SearchFlags);
85 return lDispatcher;
88 void SAL_CALL SystemExec::dispatch( const css::util::URL& aURL ,
89 const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException, std::exception )
91 dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >());
94 void SAL_CALL SystemExec::dispatchWithNotification( const css::util::URL& aURL ,
95 const css::uno::Sequence< css::beans::PropertyValue >&,
96 const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw( css::uno::RuntimeException, std::exception )
98 // convert "systemexec:file:///c:/temp/test.html" => "file:///c:/temp/test.html"
99 sal_Int32 c = aURL.Complete.getLength()-PROTOCOL_LENGTH;
100 if (c<1) // we dont check for valid URLs here! The system will show an error message ...
102 impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
103 return;
105 OUString sSystemURLWithVariables = aURL.Complete.copy(PROTOCOL_LENGTH, c);
107 // TODO check security settings ...
111 css::uno::Reference< css::util::XStringSubstitution > xPathSubst( css::util::PathSubstitution::create(m_xContext) );
113 OUString sSystemURL = xPathSubst->substituteVariables(sSystemURLWithVariables, sal_True); // sal_True force an exception if unknown variables exists !
115 css::uno::Reference< css::system::XSystemShellExecute > xShell = css::system::SystemShellExecute::create( m_xContext );
117 xShell->execute(sSystemURL, OUString(), css::system::SystemShellExecuteFlags::URIS_ONLY);
118 impl_notifyResultListener(xListener, css::frame::DispatchResultState::SUCCESS);
120 catch(const css::uno::Exception&)
122 impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE);
126 void SAL_CALL SystemExec::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
127 const css::util::URL& ) throw( css::uno::RuntimeException, std::exception )
129 // not supported yet
132 void SAL_CALL SystemExec::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >&,
133 const css::util::URL& ) throw( css::uno::RuntimeException, std::exception )
135 // not supported yet
138 void SystemExec::impl_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener,
139 const sal_Int16 nState )
141 if (xListener.is())
143 css::frame::DispatchResultEvent aEvent;
144 aEvent.State = nState;
145 xListener->dispatchFinished(aEvent);
149 } // namespace framework
151 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */