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