1*cdf0e10cSrcweir /************************************************************************* 2*cdf0e10cSrcweir * 3*cdf0e10cSrcweir * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4*cdf0e10cSrcweir * 5*cdf0e10cSrcweir * Copyright 2000, 2010 Oracle and/or its affiliates. 6*cdf0e10cSrcweir * 7*cdf0e10cSrcweir * OpenOffice.org - a multi-platform office productivity suite 8*cdf0e10cSrcweir * 9*cdf0e10cSrcweir * This file is part of OpenOffice.org. 10*cdf0e10cSrcweir * 11*cdf0e10cSrcweir * OpenOffice.org is free software: you can redistribute it and/or modify 12*cdf0e10cSrcweir * it under the terms of the GNU Lesser General Public License version 3 13*cdf0e10cSrcweir * only, as published by the Free Software Foundation. 14*cdf0e10cSrcweir * 15*cdf0e10cSrcweir * OpenOffice.org is distributed in the hope that it will be useful, 16*cdf0e10cSrcweir * but WITHOUT ANY WARRANTY; without even the implied warranty of 17*cdf0e10cSrcweir * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18*cdf0e10cSrcweir * GNU Lesser General Public License version 3 for more details 19*cdf0e10cSrcweir * (a copy is included in the LICENSE file that accompanied this code). 20*cdf0e10cSrcweir * 21*cdf0e10cSrcweir * You should have received a copy of the GNU Lesser General Public License 22*cdf0e10cSrcweir * version 3 along with OpenOffice.org. If not, see 23*cdf0e10cSrcweir * <http://www.openoffice.org/license.html> 24*cdf0e10cSrcweir * for a copy of the LGPLv3 License. 25*cdf0e10cSrcweir * 26*cdf0e10cSrcweir ************************************************************************/ 27*cdf0e10cSrcweir 28*cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 29*cdf0e10cSrcweir #include "precompiled_scripting.hxx" 30*cdf0e10cSrcweir #include "basscript.hxx" 31*cdf0e10cSrcweir #include <vos/mutex.hxx> 32*cdf0e10cSrcweir #include <vcl/svapp.hxx> 33*cdf0e10cSrcweir #include <basic/sbx.hxx> 34*cdf0e10cSrcweir #include <basic/sbstar.hxx> 35*cdf0e10cSrcweir #include <basic/sbmod.hxx> 36*cdf0e10cSrcweir #include <basic/sbmeth.hxx> 37*cdf0e10cSrcweir #include <basic/basmgr.hxx> 38*cdf0e10cSrcweir #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp> 39*cdf0e10cSrcweir 40*cdf0e10cSrcweir #include <map> 41*cdf0e10cSrcweir 42*cdf0e10cSrcweir 43*cdf0e10cSrcweir using namespace ::com::sun::star; 44*cdf0e10cSrcweir using namespace ::com::sun::star::lang; 45*cdf0e10cSrcweir using namespace ::com::sun::star::uno; 46*cdf0e10cSrcweir using namespace ::com::sun::star::script; 47*cdf0e10cSrcweir using namespace ::com::sun::star::document; 48*cdf0e10cSrcweir 49*cdf0e10cSrcweir extern ::com::sun::star::uno::Any sbxToUnoValue( SbxVariable* pVar ); 50*cdf0e10cSrcweir extern void unoToSbxValue( SbxVariable* pVar, const ::com::sun::star::uno::Any& aValue ); 51*cdf0e10cSrcweir 52*cdf0e10cSrcweir 53*cdf0e10cSrcweir //......................................................................... 54*cdf0e10cSrcweir namespace basprov 55*cdf0e10cSrcweir { 56*cdf0e10cSrcweir //......................................................................... 57*cdf0e10cSrcweir 58*cdf0e10cSrcweir typedef ::std::map< sal_Int16, Any, ::std::less< sal_Int16 > > OutParamMap; 59*cdf0e10cSrcweir 60*cdf0e10cSrcweir // ============================================================================= 61*cdf0e10cSrcweir // BasicScriptImpl 62*cdf0e10cSrcweir // ============================================================================= 63*cdf0e10cSrcweir 64*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 65*cdf0e10cSrcweir 66*cdf0e10cSrcweir BasicScriptImpl::BasicScriptImpl( const ::rtl::OUString& funcName, SbMethodRef xMethod ) 67*cdf0e10cSrcweir :m_xMethod( xMethod ) 68*cdf0e10cSrcweir ,m_funcName( funcName ) 69*cdf0e10cSrcweir ,m_documentBasicManager( NULL ) 70*cdf0e10cSrcweir ,m_xDocumentScriptContext() 71*cdf0e10cSrcweir { 72*cdf0e10cSrcweir } 73*cdf0e10cSrcweir 74*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 75*cdf0e10cSrcweir 76*cdf0e10cSrcweir BasicScriptImpl::BasicScriptImpl( const ::rtl::OUString& funcName, SbMethodRef xMethod, 77*cdf0e10cSrcweir BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) 78*cdf0e10cSrcweir :m_xMethod( xMethod ) 79*cdf0e10cSrcweir ,m_funcName( funcName ) 80*cdf0e10cSrcweir ,m_documentBasicManager( &documentBasicManager ) 81*cdf0e10cSrcweir ,m_xDocumentScriptContext( documentScriptContext ) 82*cdf0e10cSrcweir { 83*cdf0e10cSrcweir StartListening( *m_documentBasicManager ); 84*cdf0e10cSrcweir } 85*cdf0e10cSrcweir 86*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 87*cdf0e10cSrcweir BasicScriptImpl::~BasicScriptImpl() 88*cdf0e10cSrcweir { 89*cdf0e10cSrcweir if ( m_documentBasicManager ) 90*cdf0e10cSrcweir EndListening( *m_documentBasicManager ); 91*cdf0e10cSrcweir } 92*cdf0e10cSrcweir 93*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 94*cdf0e10cSrcweir // SfxListener 95*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 96*cdf0e10cSrcweir void BasicScriptImpl::Notify( SfxBroadcaster& rBC, const SfxHint& rHint ) 97*cdf0e10cSrcweir { 98*cdf0e10cSrcweir if ( &rBC != m_documentBasicManager ) 99*cdf0e10cSrcweir { 100*cdf0e10cSrcweir OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" ); 101*cdf0e10cSrcweir // not interested in 102*cdf0e10cSrcweir return; 103*cdf0e10cSrcweir } 104*cdf0e10cSrcweir const SfxSimpleHint* pSimpleHint = PTR_CAST( SfxSimpleHint, &rHint ); 105*cdf0e10cSrcweir if ( pSimpleHint && ( pSimpleHint->GetId() == SFX_HINT_DYING ) ) 106*cdf0e10cSrcweir { 107*cdf0e10cSrcweir m_documentBasicManager = NULL; 108*cdf0e10cSrcweir EndListening( rBC ); // prevent multiple notifications 109*cdf0e10cSrcweir } 110*cdf0e10cSrcweir } 111*cdf0e10cSrcweir 112*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 113*cdf0e10cSrcweir // XScript 114*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 115*cdf0e10cSrcweir 116*cdf0e10cSrcweir Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam ) 117*cdf0e10cSrcweir throw ( provider::ScriptFrameworkErrorException, reflection::InvocationTargetException, uno::RuntimeException) 118*cdf0e10cSrcweir { 119*cdf0e10cSrcweir // TODO: throw CannotConvertException 120*cdf0e10cSrcweir // TODO: check length of aOutParamIndex, aOutParam 121*cdf0e10cSrcweir 122*cdf0e10cSrcweir ::vos::OGuard aGuard( Application::GetSolarMutex() ); 123*cdf0e10cSrcweir 124*cdf0e10cSrcweir Any aReturn; 125*cdf0e10cSrcweir 126*cdf0e10cSrcweir if ( m_xMethod ) 127*cdf0e10cSrcweir { 128*cdf0e10cSrcweir // check if compiled 129*cdf0e10cSrcweir SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() ); 130*cdf0e10cSrcweir if ( pModule && !pModule->IsCompiled() ) 131*cdf0e10cSrcweir pModule->Compile(); 132*cdf0e10cSrcweir 133*cdf0e10cSrcweir // check number of parameters 134*cdf0e10cSrcweir sal_Int32 nParamsCount = aParams.getLength(); 135*cdf0e10cSrcweir SbxInfo* pInfo = m_xMethod->GetInfo(); 136*cdf0e10cSrcweir if ( pInfo ) 137*cdf0e10cSrcweir { 138*cdf0e10cSrcweir sal_Int32 nSbxOptional = 0; 139*cdf0e10cSrcweir sal_uInt16 n = 1; 140*cdf0e10cSrcweir for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) ) 141*cdf0e10cSrcweir { 142*cdf0e10cSrcweir if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != 0 ) 143*cdf0e10cSrcweir ++nSbxOptional; 144*cdf0e10cSrcweir else 145*cdf0e10cSrcweir nSbxOptional = 0; 146*cdf0e10cSrcweir } 147*cdf0e10cSrcweir sal_Int32 nSbxCount = n - 1; 148*cdf0e10cSrcweir if ( nParamsCount < nSbxCount - nSbxOptional ) 149*cdf0e10cSrcweir { 150*cdf0e10cSrcweir throw provider::ScriptFrameworkErrorException( 151*cdf0e10cSrcweir ::rtl::OUString( 152*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( 153*cdf0e10cSrcweir "wrong number of parameters!" ) ), 154*cdf0e10cSrcweir Reference< XInterface >(), 155*cdf0e10cSrcweir m_funcName, 156*cdf0e10cSrcweir ::rtl::OUString( 157*cdf0e10cSrcweir RTL_CONSTASCII_USTRINGPARAM( "Basic" ) ), 158*cdf0e10cSrcweir provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT ); 159*cdf0e10cSrcweir } 160*cdf0e10cSrcweir } 161*cdf0e10cSrcweir 162*cdf0e10cSrcweir // set parameters 163*cdf0e10cSrcweir SbxArrayRef xSbxParams; 164*cdf0e10cSrcweir if ( nParamsCount > 0 ) 165*cdf0e10cSrcweir { 166*cdf0e10cSrcweir xSbxParams = new SbxArray; 167*cdf0e10cSrcweir const Any* pParams = aParams.getConstArray(); 168*cdf0e10cSrcweir for ( sal_Int32 i = 0; i < nParamsCount; ++i ) 169*cdf0e10cSrcweir { 170*cdf0e10cSrcweir SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT ); 171*cdf0e10cSrcweir unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] ); 172*cdf0e10cSrcweir xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 ); 173*cdf0e10cSrcweir 174*cdf0e10cSrcweir // Enable passing by ref 175*cdf0e10cSrcweir if ( xSbxVar->GetType() != SbxVARIANT ) 176*cdf0e10cSrcweir xSbxVar->SetFlag( SBX_FIXED ); 177*cdf0e10cSrcweir } 178*cdf0e10cSrcweir } 179*cdf0e10cSrcweir if ( xSbxParams.Is() ) 180*cdf0e10cSrcweir m_xMethod->SetParameters( xSbxParams ); 181*cdf0e10cSrcweir 182*cdf0e10cSrcweir // call method 183*cdf0e10cSrcweir SbxVariableRef xReturn = new SbxVariable; 184*cdf0e10cSrcweir ErrCode nErr = SbxERR_OK; 185*cdf0e10cSrcweir { 186*cdf0e10cSrcweir // if it's a document-based script, temporarily reset ThisComponent to the script invocation context 187*cdf0e10cSrcweir Any aOldThisComponent; 188*cdf0e10cSrcweir if ( m_documentBasicManager && m_xDocumentScriptContext.is() ) 189*cdf0e10cSrcweir aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) ); 190*cdf0e10cSrcweir 191*cdf0e10cSrcweir nErr = m_xMethod->Call( xReturn ); 192*cdf0e10cSrcweir 193*cdf0e10cSrcweir if ( m_documentBasicManager && m_xDocumentScriptContext.is() ) 194*cdf0e10cSrcweir m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent ); 195*cdf0e10cSrcweir } 196*cdf0e10cSrcweir if ( nErr != SbxERR_OK ) 197*cdf0e10cSrcweir { 198*cdf0e10cSrcweir // TODO: throw InvocationTargetException ? 199*cdf0e10cSrcweir } 200*cdf0e10cSrcweir 201*cdf0e10cSrcweir // get output parameters 202*cdf0e10cSrcweir if ( xSbxParams.Is() ) 203*cdf0e10cSrcweir { 204*cdf0e10cSrcweir SbxInfo* pInfo_ = m_xMethod->GetInfo(); 205*cdf0e10cSrcweir if ( pInfo_ ) 206*cdf0e10cSrcweir { 207*cdf0e10cSrcweir OutParamMap aOutParamMap; 208*cdf0e10cSrcweir for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n ) 209*cdf0e10cSrcweir { 210*cdf0e10cSrcweir const SbxParamInfo* pParamInfo = pInfo_->GetParam( n ); 211*cdf0e10cSrcweir if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 ) 212*cdf0e10cSrcweir { 213*cdf0e10cSrcweir SbxVariable* pVar = xSbxParams->Get( n ); 214*cdf0e10cSrcweir if ( pVar ) 215*cdf0e10cSrcweir { 216*cdf0e10cSrcweir SbxVariableRef xVar = pVar; 217*cdf0e10cSrcweir aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) ); 218*cdf0e10cSrcweir } 219*cdf0e10cSrcweir } 220*cdf0e10cSrcweir } 221*cdf0e10cSrcweir sal_Int32 nOutParamCount = aOutParamMap.size(); 222*cdf0e10cSrcweir aOutParamIndex.realloc( nOutParamCount ); 223*cdf0e10cSrcweir aOutParam.realloc( nOutParamCount ); 224*cdf0e10cSrcweir sal_Int16* pOutParamIndex = aOutParamIndex.getArray(); 225*cdf0e10cSrcweir Any* pOutParam = aOutParam.getArray(); 226*cdf0e10cSrcweir for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam ) 227*cdf0e10cSrcweir { 228*cdf0e10cSrcweir *pOutParamIndex = aIt->first; 229*cdf0e10cSrcweir *pOutParam = aIt->second; 230*cdf0e10cSrcweir } 231*cdf0e10cSrcweir } 232*cdf0e10cSrcweir } 233*cdf0e10cSrcweir 234*cdf0e10cSrcweir // get return value 235*cdf0e10cSrcweir aReturn = sbxToUnoValue( xReturn ); 236*cdf0e10cSrcweir 237*cdf0e10cSrcweir // reset parameters 238*cdf0e10cSrcweir m_xMethod->SetParameters( NULL ); 239*cdf0e10cSrcweir } 240*cdf0e10cSrcweir 241*cdf0e10cSrcweir return aReturn; 242*cdf0e10cSrcweir } 243*cdf0e10cSrcweir 244*cdf0e10cSrcweir // ----------------------------------------------------------------------------- 245*cdf0e10cSrcweir 246*cdf0e10cSrcweir //......................................................................... 247*cdf0e10cSrcweir } // namespace basprov 248*cdf0e10cSrcweir //......................................................................... 249