16d739b60SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 36d739b60SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 46d739b60SAndrew Rist * or more contributor license agreements. See the NOTICE file 56d739b60SAndrew Rist * distributed with this work for additional information 66d739b60SAndrew Rist * regarding copyright ownership. The ASF licenses this file 76d739b60SAndrew Rist * to you under the Apache License, Version 2.0 (the 86d739b60SAndrew Rist * "License"); you may not use this file except in compliance 96d739b60SAndrew Rist * with the License. You may obtain a copy of the License at 10cdf0e10cSrcweir * 116d739b60SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12cdf0e10cSrcweir * 136d739b60SAndrew Rist * Unless required by applicable law or agreed to in writing, 146d739b60SAndrew Rist * software distributed under the License is distributed on an 156d739b60SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 166d739b60SAndrew Rist * KIND, either express or implied. See the License for the 176d739b60SAndrew Rist * specific language governing permissions and limitations 186d739b60SAndrew Rist * under the License. 19cdf0e10cSrcweir * 206d739b60SAndrew Rist *************************************************************/ 216d739b60SAndrew Rist 226d739b60SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_framework.hxx" 26cdf0e10cSrcweir 27cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 28cdf0e10cSrcweir // my own includes 29cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 30cdf0e10cSrcweir #include <dispatch/systemexec.hxx> 31cdf0e10cSrcweir #include <threadhelp/readguard.hxx> 32cdf0e10cSrcweir #include <general.h> 33cdf0e10cSrcweir #include <services.h> 34cdf0e10cSrcweir 35cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 36cdf0e10cSrcweir // interface includes 37cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 38*9807c9deSAriel Constenla-Haile #include <com/sun/star/system/SystemShellExecute.hpp> 39cdf0e10cSrcweir #include <com/sun/star/util/XStringSubstitution.hpp> 40cdf0e10cSrcweir #include <com/sun/star/system/SystemShellExecuteFlags.hpp> 41cdf0e10cSrcweir #include <com/sun/star/frame/DispatchResultState.hpp> 42cdf0e10cSrcweir 43cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 44cdf0e10cSrcweir // includes of other projects 45cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 46cdf0e10cSrcweir 47cdf0e10cSrcweir #include <vcl/svapp.hxx> 48cdf0e10cSrcweir 49cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 50cdf0e10cSrcweir // namespace 51cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 52cdf0e10cSrcweir 53cdf0e10cSrcweir namespace framework{ 54cdf0e10cSrcweir 55cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 56cdf0e10cSrcweir // non exported const 57cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 58cdf0e10cSrcweir 59cdf0e10cSrcweir #define PROTOCOL_VALUE "systemexecute:" 60cdf0e10cSrcweir #define PROTOCOL_LENGTH 14 61cdf0e10cSrcweir 62cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 63cdf0e10cSrcweir // non exported definitions 64cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 65cdf0e10cSrcweir 66cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 67cdf0e10cSrcweir // declarations 68cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 69cdf0e10cSrcweir 70cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 71cdf0e10cSrcweir // XInterface, XTypeProvider, XServiceInfo 72cdf0e10cSrcweir 73cdf0e10cSrcweir DEFINE_XINTERFACE_5(SystemExec , 74cdf0e10cSrcweir OWeakObject , 75cdf0e10cSrcweir DIRECT_INTERFACE(css::lang::XTypeProvider ), 76cdf0e10cSrcweir DIRECT_INTERFACE(css::lang::XServiceInfo ), 77cdf0e10cSrcweir DIRECT_INTERFACE(css::frame::XDispatchProvider ), 78cdf0e10cSrcweir DIRECT_INTERFACE(css::frame::XNotifyingDispatch), 79cdf0e10cSrcweir DIRECT_INTERFACE(css::frame::XDispatch )) 80cdf0e10cSrcweir 81cdf0e10cSrcweir DEFINE_XTYPEPROVIDER_5(SystemExec , 82cdf0e10cSrcweir css::lang::XTypeProvider , 83cdf0e10cSrcweir css::lang::XServiceInfo , 84cdf0e10cSrcweir css::frame::XDispatchProvider , 85cdf0e10cSrcweir css::frame::XNotifyingDispatch, 86cdf0e10cSrcweir css::frame::XDispatch ) 87cdf0e10cSrcweir 88*9807c9deSAriel Constenla-Haile DEFINE_XSERVICEINFO_MULTISERVICE_2(SystemExec , 89cdf0e10cSrcweir ::cppu::OWeakObject , 90cdf0e10cSrcweir SERVICENAME_PROTOCOLHANDLER , 91cdf0e10cSrcweir IMPLEMENTATIONNAME_SYSTEMEXEC) 92cdf0e10cSrcweir 93cdf0e10cSrcweir DEFINE_INIT_SERVICE(SystemExec, 94cdf0e10cSrcweir { 95cdf0e10cSrcweir /*Attention 96cdf0e10cSrcweir I think we don't need any mutex or lock here ... because we are called by our own static method impl_createInstance() 97cdf0e10cSrcweir to create a new instance of this class by our own supported service factory. 98cdf0e10cSrcweir see macro DEFINE_XSERVICEINFO_MULTISERVICE and "impl_initService()" for further informations! 99cdf0e10cSrcweir */ 100cdf0e10cSrcweir } 101cdf0e10cSrcweir ) 102cdf0e10cSrcweir 103cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 104cdf0e10cSrcweir 105*9807c9deSAriel Constenla-Haile SystemExec::SystemExec( const css::uno::Reference< css::uno::XComponentContext >& xContext ) 106cdf0e10cSrcweir // Init baseclasses first 107cdf0e10cSrcweir : ThreadHelpBase( &Application::GetSolarMutex() ) 108cdf0e10cSrcweir , OWeakObject ( ) 109cdf0e10cSrcweir // Init member 110*9807c9deSAriel Constenla-Haile , m_xContext ( xContext ) 111cdf0e10cSrcweir { 112cdf0e10cSrcweir } 113cdf0e10cSrcweir 114cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 115cdf0e10cSrcweir 116cdf0e10cSrcweir SystemExec::~SystemExec() 117cdf0e10cSrcweir { 118*9807c9deSAriel Constenla-Haile m_xContext = NULL; 119cdf0e10cSrcweir } 120cdf0e10cSrcweir 121cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 122cdf0e10cSrcweir 123cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatch > SAL_CALL SystemExec::queryDispatch( const css::util::URL& aURL , 124cdf0e10cSrcweir const ::rtl::OUString&, 125cdf0e10cSrcweir sal_Int32 ) throw( css::uno::RuntimeException ) 126cdf0e10cSrcweir { 127cdf0e10cSrcweir css::uno::Reference< css::frame::XDispatch > xDispatcher; 128cdf0e10cSrcweir if (aURL.Complete.compareToAscii(PROTOCOL_VALUE,PROTOCOL_LENGTH)==0) 129cdf0e10cSrcweir xDispatcher = this; 130cdf0e10cSrcweir return xDispatcher; 131cdf0e10cSrcweir } 132cdf0e10cSrcweir 133cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 134cdf0e10cSrcweir 135cdf0e10cSrcweir 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 ) 136cdf0e10cSrcweir { 137cdf0e10cSrcweir sal_Int32 nCount = lDescriptor.getLength(); 138cdf0e10cSrcweir css::uno::Sequence< css::uno::Reference< css::frame::XDispatch > > lDispatcher( nCount ); 139cdf0e10cSrcweir for( sal_Int32 i=0; i<nCount; ++i ) 140cdf0e10cSrcweir { 141cdf0e10cSrcweir lDispatcher[i] = this->queryDispatch( 142cdf0e10cSrcweir lDescriptor[i].FeatureURL, 143cdf0e10cSrcweir lDescriptor[i].FrameName, 144cdf0e10cSrcweir lDescriptor[i].SearchFlags); 145cdf0e10cSrcweir } 146cdf0e10cSrcweir return lDispatcher; 147cdf0e10cSrcweir } 148cdf0e10cSrcweir 149cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 150cdf0e10cSrcweir 151cdf0e10cSrcweir void SAL_CALL SystemExec::dispatch( const css::util::URL& aURL , 152cdf0e10cSrcweir const css::uno::Sequence< css::beans::PropertyValue >& lArguments ) throw( css::uno::RuntimeException ) 153cdf0e10cSrcweir { 154cdf0e10cSrcweir dispatchWithNotification(aURL, lArguments, css::uno::Reference< css::frame::XDispatchResultListener >()); 155cdf0e10cSrcweir } 156cdf0e10cSrcweir 157cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 158cdf0e10cSrcweir 159cdf0e10cSrcweir void SAL_CALL SystemExec::dispatchWithNotification( const css::util::URL& aURL , 160cdf0e10cSrcweir const css::uno::Sequence< css::beans::PropertyValue >&, 161cdf0e10cSrcweir const css::uno::Reference< css::frame::XDispatchResultListener >& xListener ) throw( css::uno::RuntimeException ) 162cdf0e10cSrcweir { 163cdf0e10cSrcweir // convert "systemexec:file:///c:/temp/test.html" => "file:///c:/temp/test.html" 164cdf0e10cSrcweir sal_Int32 c = aURL.Complete.getLength()-PROTOCOL_LENGTH; 165cdf0e10cSrcweir if (c<1) // we dont check for valid URLs here! The system will show an error message ... 166cdf0e10cSrcweir { 167cdf0e10cSrcweir impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE); 168cdf0e10cSrcweir return; 169cdf0e10cSrcweir } 170cdf0e10cSrcweir ::rtl::OUString sSystemURLWithVariables = aURL.Complete.copy(PROTOCOL_LENGTH, c); 171cdf0e10cSrcweir 172cdf0e10cSrcweir // SAFE -> 173cdf0e10cSrcweir ReadGuard aReadLock(m_aLock); 174*9807c9deSAriel Constenla-Haile css::uno::Reference< css::uno::XComponentContext > xContext = m_xContext; 175cdf0e10cSrcweir aReadLock.unlock(); 176cdf0e10cSrcweir // <- SAFE 177cdf0e10cSrcweir 178cdf0e10cSrcweir // TODO check security settings ... 179cdf0e10cSrcweir 180cdf0e10cSrcweir try 181cdf0e10cSrcweir { 182cdf0e10cSrcweir css::uno::Reference< css::util::XStringSubstitution > xPathSubst( 183*9807c9deSAriel Constenla-Haile xContext->getServiceManager()->createInstanceWithContext( 184*9807c9deSAriel Constenla-Haile SERVICENAME_SUBSTITUTEPATHVARIABLES, xContext), 185cdf0e10cSrcweir css::uno::UNO_QUERY_THROW); 186cdf0e10cSrcweir 187cdf0e10cSrcweir ::rtl::OUString sSystemURL = xPathSubst->substituteVariables(sSystemURLWithVariables, sal_True); // sal_True force an exception if unknown variables exists ! 188cdf0e10cSrcweir 189cdf0e10cSrcweir css::uno::Reference< css::system::XSystemShellExecute > xShell( 190*9807c9deSAriel Constenla-Haile css::system::SystemShellExecute::create(xContext)); 191cdf0e10cSrcweir 192cdf0e10cSrcweir xShell->execute(sSystemURL, ::rtl::OUString(), css::system::SystemShellExecuteFlags::DEFAULTS); 193cdf0e10cSrcweir impl_notifyResultListener(xListener, css::frame::DispatchResultState::SUCCESS); 194cdf0e10cSrcweir } 195cdf0e10cSrcweir catch(const css::uno::Exception&) 196cdf0e10cSrcweir { 197cdf0e10cSrcweir impl_notifyResultListener(xListener, css::frame::DispatchResultState::FAILURE); 198cdf0e10cSrcweir } 199cdf0e10cSrcweir } 200cdf0e10cSrcweir 201cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 202cdf0e10cSrcweir 203cdf0e10cSrcweir void SAL_CALL SystemExec::addStatusListener( const css::uno::Reference< css::frame::XStatusListener >&, 204cdf0e10cSrcweir const css::util::URL& ) throw( css::uno::RuntimeException ) 205cdf0e10cSrcweir { 206cdf0e10cSrcweir // not suported yet 207cdf0e10cSrcweir } 208cdf0e10cSrcweir 209cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 210cdf0e10cSrcweir 211cdf0e10cSrcweir void SAL_CALL SystemExec::removeStatusListener( const css::uno::Reference< css::frame::XStatusListener >&, 212cdf0e10cSrcweir const css::util::URL& ) throw( css::uno::RuntimeException ) 213cdf0e10cSrcweir { 214cdf0e10cSrcweir // not suported yet 215cdf0e10cSrcweir } 216cdf0e10cSrcweir 217cdf0e10cSrcweir //_________________________________________________________________________________________________________________ 218cdf0e10cSrcweir 219cdf0e10cSrcweir void SystemExec::impl_notifyResultListener(const css::uno::Reference< css::frame::XDispatchResultListener >& xListener, 220cdf0e10cSrcweir const sal_Int16 nState ) 221cdf0e10cSrcweir { 222cdf0e10cSrcweir if (xListener.is()) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir css::frame::DispatchResultEvent aEvent; 225cdf0e10cSrcweir aEvent.State = nState; 226cdf0e10cSrcweir xListener->dispatchFinished(aEvent); 227cdf0e10cSrcweir } 228cdf0e10cSrcweir } 229cdf0e10cSrcweir 230cdf0e10cSrcweir } // namespace framework 231