Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
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>
35 :
36 :
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;
43 :
44 :
45 :
46 : namespace basprov
47 : {
48 :
49 : #define BASSCRIPT_PROPERTY_ID_CALLER 1
50 : #define BASSCRIPT_PROPERTY_CALLER OUString( "Caller" )
51 :
52 : #define BASSCRIPT_DEFAULT_ATTRIBS() PropertyAttribute::BOUND | PropertyAttribute::TRANSIENT
53 :
54 : typedef ::std::map< sal_Int16, Any, ::std::less< sal_Int16 > > OutParamMap;
55 :
56 :
57 : // BasicScriptImpl
58 :
59 :
60 :
61 :
62 0 : BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef xMethod )
63 : : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
64 0 : ,OPropertyContainer( GetBroadcastHelper() )
65 : ,m_xMethod( xMethod )
66 : ,m_funcName( funcName )
67 : ,m_documentBasicManager( NULL )
68 0 : ,m_xDocumentScriptContext()
69 : {
70 0 : registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, cppu::UnoType<decltype(m_caller)>::get() );
71 0 : }
72 :
73 :
74 :
75 80 : BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef xMethod,
76 : BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
77 80 : ,OPropertyContainer( GetBroadcastHelper() )
78 : ,m_xMethod( xMethod )
79 : ,m_funcName( funcName )
80 : ,m_documentBasicManager( &documentBasicManager )
81 160 : ,m_xDocumentScriptContext( documentScriptContext )
82 : {
83 80 : StartListening( *m_documentBasicManager );
84 80 : registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, cppu::UnoType<decltype(m_caller)>::get() );
85 80 : }
86 :
87 :
88 240 : BasicScriptImpl::~BasicScriptImpl()
89 : {
90 80 : SolarMutexGuard g;
91 :
92 80 : if ( m_documentBasicManager )
93 79 : EndListening( *m_documentBasicManager );
94 160 : }
95 :
96 :
97 : // SfxListener
98 :
99 1 : void BasicScriptImpl::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
100 : {
101 1 : if ( &rBC != m_documentBasicManager )
102 : {
103 : OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
104 : // not interested in
105 1 : return;
106 : }
107 1 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
108 1 : if ( pSimpleHint && ( pSimpleHint->GetId() == SFX_HINT_DYING ) )
109 : {
110 1 : m_documentBasicManager = NULL;
111 1 : EndListening( rBC ); // prevent multiple notifications
112 : }
113 : }
114 :
115 :
116 : // XInterface
117 :
118 :
119 558 : IMPLEMENT_FORWARD_XINTERFACE2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
120 :
121 :
122 : // XTypeProvider
123 :
124 :
125 0 : IMPLEMENT_FORWARD_XTYPEPROVIDER2( BasicScriptImpl, BasicScriptImpl_BASE, OPropertyContainer )
126 :
127 :
128 : // OPropertySetHelper
129 :
130 :
131 3 : ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper( )
132 : {
133 3 : return *getArrayHelper();
134 : }
135 :
136 :
137 : // OPropertyArrayUsageHelper
138 :
139 :
140 1 : ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper( ) const
141 : {
142 1 : Sequence< Property > aProps;
143 1 : describeProperties( aProps );
144 1 : return new ::cppu::OPropertyArrayHelper( aProps );
145 : }
146 :
147 :
148 : // XPropertySet
149 :
150 :
151 0 : Reference< XPropertySetInfo > BasicScriptImpl::getPropertySetInfo( ) throw (RuntimeException, std::exception)
152 : {
153 0 : Reference< XPropertySetInfo > xInfo( createPropertySetInfo( getInfoHelper() ) );
154 0 : return xInfo;
155 : }
156 :
157 :
158 : // XScript
159 :
160 :
161 80 : Any BasicScriptImpl::invoke( const Sequence< Any >& aParams, Sequence< sal_Int16 >& aOutParamIndex, Sequence< Any >& aOutParam )
162 : throw ( provider::ScriptFrameworkErrorException, reflection::InvocationTargetException, uno::RuntimeException, std::exception)
163 : {
164 : // TODO: throw CannotConvertException
165 : // TODO: check length of aOutParamIndex, aOutParam
166 :
167 80 : SolarMutexGuard aGuard;
168 :
169 80 : Any aReturn;
170 :
171 80 : if ( m_xMethod )
172 : {
173 : // check if compiled
174 80 : SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
175 80 : if ( pModule && !pModule->IsCompiled() )
176 31 : pModule->Compile();
177 :
178 : // check number of parameters
179 80 : sal_Int32 nParamsCount = aParams.getLength();
180 80 : SbxInfo* pInfo = m_xMethod->GetInfo();
181 80 : if ( pInfo )
182 : {
183 80 : sal_Int32 nSbxOptional = 0;
184 80 : sal_uInt16 n = 1;
185 117 : for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
186 : {
187 37 : if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != SBX_NONE )
188 2 : ++nSbxOptional;
189 : else
190 35 : nSbxOptional = 0;
191 : }
192 80 : sal_Int32 nSbxCount = n - 1;
193 80 : if ( nParamsCount < nSbxCount - nSbxOptional )
194 : {
195 : throw provider::ScriptFrameworkErrorException(
196 : OUString(
197 : "wrong number of parameters!" ),
198 : Reference< XInterface >(),
199 : m_funcName,
200 : OUString( "Basic" ),
201 0 : provider::ScriptFrameworkErrorType::NO_SUCH_SCRIPT );
202 : }
203 : }
204 :
205 : // set parameters
206 80 : SbxArrayRef xSbxParams;
207 80 : if ( nParamsCount > 0 )
208 : {
209 51 : xSbxParams = new SbxArray;
210 51 : const Any* pParams = aParams.getConstArray();
211 106 : for ( sal_Int32 i = 0; i < nParamsCount; ++i )
212 : {
213 55 : SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
214 55 : unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
215 55 : xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 );
216 :
217 : // Enable passing by ref
218 55 : if ( xSbxVar->GetType() != SbxVARIANT )
219 55 : xSbxVar->SetFlag( SBX_FIXED );
220 55 : }
221 : }
222 80 : if ( xSbxParams.Is() )
223 51 : m_xMethod->SetParameters( xSbxParams );
224 :
225 : // call method
226 160 : SbxVariableRef xReturn = new SbxVariable;
227 80 : ErrCode nErr = SbxERR_OK;
228 :
229 : // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
230 160 : Any aOldThisComponent;
231 80 : if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
232 0 : aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
233 :
234 80 : if ( m_caller.getLength() && m_caller[ 0 ].hasValue() )
235 : {
236 1 : SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
237 1 : unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
238 1 : nErr = m_xMethod->Call( xReturn, xCallerVar );
239 : }
240 : else
241 79 : nErr = m_xMethod->Call( xReturn );
242 :
243 80 : if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
244 0 : m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", aOldThisComponent );
245 :
246 : if ( nErr != SbxERR_OK )
247 : {
248 : // TODO: throw InvocationTargetException ?
249 : }
250 :
251 : // get output parameters
252 80 : if ( xSbxParams.Is() )
253 : {
254 51 : SbxInfo* pInfo_ = m_xMethod->GetInfo();
255 51 : if ( pInfo_ )
256 : {
257 51 : OutParamMap aOutParamMap;
258 106 : for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n )
259 : {
260 55 : const SbxParamInfo* pParamInfo = pInfo_->GetParam( n );
261 55 : if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
262 : {
263 2 : SbxVariable* pVar = xSbxParams->Get( n );
264 2 : if ( pVar )
265 : {
266 2 : SbxVariableRef xVar = pVar;
267 2 : aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
268 : }
269 : }
270 : }
271 51 : sal_Int32 nOutParamCount = aOutParamMap.size();
272 51 : aOutParamIndex.realloc( nOutParamCount );
273 51 : aOutParam.realloc( nOutParamCount );
274 51 : sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
275 51 : Any* pOutParam = aOutParam.getArray();
276 53 : for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
277 : {
278 2 : *pOutParamIndex = aIt->first;
279 2 : *pOutParam = aIt->second;
280 51 : }
281 : }
282 : }
283 :
284 : // get return value
285 80 : aReturn = sbxToUnoValue( xReturn );
286 :
287 : // reset parameters
288 160 : m_xMethod->SetParameters( NULL );
289 : }
290 :
291 80 : return aReturn;
292 : }
293 :
294 :
295 :
296 :
297 : } // namespace basprov
298 :
299 :
300 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|