bump product version to 5.0.4.1
[LibreOffice.git] / scripting / source / basprov / basscript.cxx
blobafd4a307aa42c00fb8c21a6d5fd2fd99bb6c60c6
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 "basscript.hxx"
21 #include <osl/mutex.hxx>
22 #include <vcl/svapp.hxx>
23 #include <basic/sbx.hxx>
24 #include <basic/sbstar.hxx>
25 #include <basic/sbmod.hxx>
26 #include <basic/sbmeth.hxx>
27 #include <basic/sbuno.hxx>
28 #include <basic/basmgr.hxx>
29 #include <com/sun/star/script/provider/ScriptFrameworkErrorType.hpp>
30 #include "bcholder.hxx"
31 #include <comphelper/proparrhlp.hxx>
32 #include <comphelper/propertycontainer.hxx>
33 #include <com/sun/star/beans/PropertyAttribute.hpp>
34 #include <map>
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::lang;
39 using namespace ::com::sun::star::uno;
40 using namespace ::com::sun::star::script;
41 using namespace ::com::sun::star::document;
42 using namespace ::com::sun::star::beans;
46 namespace basprov
49 #define BASSCRIPT_PROPERTY_ID_CALLER 1
50 #define BASSCRIPT_PROPERTY_CALLER OUString( "Caller" )
52 #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
54 typedef ::std::map< sal_Int16, Any, ::std::less< sal_Int16 > > OutParamMap;
57 // BasicScriptImpl
62 BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef xMethod )
63 : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
64 ,OPropertyContainer( GetBroadcastHelper() )
65 ,m_xMethod( xMethod )
66 ,m_funcName( funcName )
67 ,m_documentBasicManager( NULL )
68 ,m_xDocumentScriptContext()
70 registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, cppu::UnoType<decltype(m_caller)>::get() );
75 BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef xMethod,
76 BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
77 ,OPropertyContainer( GetBroadcastHelper() )
78 ,m_xMethod( xMethod )
79 ,m_funcName( funcName )
80 ,m_documentBasicManager( &documentBasicManager )
81 ,m_xDocumentScriptContext( documentScriptContext )
83 StartListening( *m_documentBasicManager );
84 registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, cppu::UnoType<decltype(m_caller)>::get() );
88 BasicScriptImpl::~BasicScriptImpl()
90 SolarMutexGuard g;
92 if ( m_documentBasicManager )
93 EndListening( *m_documentBasicManager );
97 // SfxListener
99 void BasicScriptImpl::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
101 if ( &rBC != m_documentBasicManager )
103 OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
104 // not interested in
105 return;
107 const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
108 if ( pSimpleHint && ( pSimpleHint->GetId() == SFX_HINT_DYING ) )
110 m_documentBasicManager = NULL;
111 EndListening( rBC ); // prevent multiple notifications
116 // XInterface
119 IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
122 // XTypeProvider
125 IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
128 // OPropertySetHelper
131 ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper( )
133 return *getArrayHelper();
137 // OPropertyArrayUsageHelper
140 ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper( ) const
142 Sequence< Property > aProps;
143 describeProperties( aProps );
144 return new ::cppu::OPropertyArrayHelper( aProps );
148 // XPropertySet
151 Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo( ) throw (RuntimeException, std::exception)
153 Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
154 return xInfo;
158 // XScript
161 Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
162 throw ( provider::ScriptFrameworkErrorException, reflection::InvocationTargetException, uno::RuntimeException, std::exception)
164 // TODO: throw CannotConvertException
165 // TODO: check length of aOutParamIndex, aOutParam
167 SolarMutexGuard aGuard;
169 Any aReturn;
171 if ( m_xMethod )
173 // check if compiled
174 SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
175 if ( pModule && !pModule->IsCompiled() )
176 pModule->Compile();
178 // check number of parameters
179 sal_Int32 nParamsCount = aParams.getLength();
180 SbxInfo* pInfo = m_xMethod->GetInfo();
181 if ( pInfo )
183 sal_Int32 nSbxOptional = 0;
184 sal_uInt16 n = 1;
185 for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
187 if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != SBX_NONE )
188 ++nSbxOptional;
189 else
190 nSbxOptional = 0;
192 sal_Int32 nSbxCount = n - 1;
193 if ( nParamsCount < nSbxCount - nSbxOptional )
195 throw provider::ScriptFrameworkErrorException(
196 OUString(
197 "wrong number of parameters!" ),
198 Reference< XInterface >(),
199 m_funcName,
200 OUString( "Basic" ),
201 provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
205 // set parameters
206 SbxArrayRef xSbxParams;
207 if ( nParamsCount > 0 )
209 xSbxParams = new SbxArray;
210 const Any* pParams = aParams.getConstArray();
211 for ( sal_Int32 i = 0; i < nParamsCount; ++i )
213 SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
214 unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
215 xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 );
217 // Enable passing by ref
218 if ( xSbxVar->GetType() != SbxVARIANT )
219 xSbxVar->SetFlag( SBX_FIXED );
222 if ( xSbxParams.Is() )
223 m_xMethod->SetParameters( xSbxParams );
225 // call method
226 SbxVariableRef xReturn = new SbxVariable;
227 ErrCode nErr = SbxERR_OK;
229 // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
230 Any aOldThisComponent;
231 if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
232 aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
234 if ( m_caller.getLength() && m_caller[ 0 ].hasValue() )
236 SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
237 unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
238 nErr = m_xMethod->Call( xReturn, xCallerVar );
240 else
241 nErr = m_xMethod->Call( xReturn );
243 if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
244 m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
246 if ( nErr != SbxERR_OK )
248 // TODO: throw InvocationTargetException ?
251 // get output parameters
252 if ( xSbxParams.Is() )
254 SbxInfo* pInfo_ = m_xMethod->GetInfo();
255 if ( pInfo_ )
257 OutParamMap aOutParamMap;
258 for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n )
260 const SbxParamInfo* pParamInfo = pInfo_->GetParam( n );
261 if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
263 SbxVariable* pVar = xSbxParams->Get( n );
264 if ( pVar )
266 SbxVariableRef xVar = pVar;
267 aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
271 sal_Int32 nOutParamCount = aOutParamMap.size();
272 aOutParamIndex.realloc( nOutParamCount );
273 aOutParam.realloc( nOutParamCount );
274 sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
275 Any* pOutParam = aOutParam.getArray();
276 for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
278 *pOutParamIndex = aIt->first;
279 *pOutParam = aIt->second;
284 // get return value
285 aReturn = sbxToUnoValue( xReturn );
287 // reset parameters
288 m_xMethod->SetParameters( NULL );
291 return aReturn;
297 } // namespace basprov
300 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */