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, ::getCppuType( &m_caller ) );
71 0 : }
72 :
73 :
74 :
75 154 : BasicScriptImpl::BasicScriptImpl( const OUString& funcName, SbMethodRef xMethod,
76 : BasicManager& documentBasicManager, const Reference< XScriptInvocationContext >& documentScriptContext ) : ::scripting_helper::OBroadcastHelperHolder( m_aMutex )
77 154 : ,OPropertyContainer( GetBroadcastHelper() )
78 : ,m_xMethod( xMethod )
79 : ,m_funcName( funcName )
80 : ,m_documentBasicManager( &documentBasicManager )
81 308 : ,m_xDocumentScriptContext( documentScriptContext )
82 : {
83 154 : StartListening( *m_documentBasicManager );
84 154 : registerProperty( BASSCRIPT_PROPERTY_CALLER, BASSCRIPT_PROPERTY_ID_CALLER, BASSCRIPT_DEFAULT_ATTRIBS(), &m_caller, ::getCppuType( &m_caller ) );
85 154 : }
86 :
87 :
88 462 : BasicScriptImpl::~BasicScriptImpl()
89 : {
90 154 : SolarMutexGuard g;
91 :
92 154 : if ( m_documentBasicManager )
93 152 : EndListening( *m_documentBasicManager );
94 308 : }
95 :
96 :
97 : // SfxListener
98 :
99 2 : void BasicScriptImpl::Notify( SfxBroadcaster& rBC, const SfxHint& rHint )
100 : {
101 2 : if ( &rBC != m_documentBasicManager )
102 : {
103 : OSL_ENSURE( false, "BasicScriptImpl::Notify: where does this come from?" );
104 : // not interested in
105 2 : return;
106 : }
107 2 : const SfxSimpleHint* pSimpleHint = dynamic_cast<const SfxSimpleHint*>( &rHint );
108 2 : if ( pSimpleHint && ( pSimpleHint->GetId() == SFX_HINT_DYING ) )
109 : {
110 2 : m_documentBasicManager = NULL;
111 2 : EndListening( rBC ); // prevent multiple notifications
112 : }
113 : }
114 :
115 :
116 : // XInterface
117 :
118 :
119 1074 : 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 6 : ::cppu::IPropertyArrayHelper& BasicScriptImpl::getInfoHelper( )
132 : {
133 6 : return *getArrayHelper();
134 : }
135 :
136 :
137 : // OPropertyArrayUsageHelper
138 :
139 :
140 2 : ::cppu::IPropertyArrayHelper* BasicScriptImpl::createArrayHelper( ) const
141 : {
142 2 : Sequence< Property > aProps;
143 2 : describeProperties( aProps );
144 2 : 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 154 : 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 154 : SolarMutexGuard aGuard;
168 :
169 154 : Any aReturn;
170 :
171 154 : if ( m_xMethod )
172 : {
173 : // check if compiled
174 154 : SbModule* pModule = static_cast< SbModule* >( m_xMethod->GetParent() );
175 154 : if ( pModule && !pModule->IsCompiled() )
176 58 : pModule->Compile();
177 :
178 : // check number of parameters
179 154 : sal_Int32 nParamsCount = aParams.getLength();
180 154 : SbxInfo* pInfo = m_xMethod->GetInfo();
181 154 : if ( pInfo )
182 : {
183 154 : sal_Int32 nSbxOptional = 0;
184 154 : sal_uInt16 n = 1;
185 228 : for ( const SbxParamInfo* pParamInfo = pInfo->GetParam( n ); pParamInfo; pParamInfo = pInfo->GetParam( ++n ) )
186 : {
187 74 : if ( ( pParamInfo->nFlags & SBX_OPTIONAL ) != SBX_NONE )
188 4 : ++nSbxOptional;
189 : else
190 70 : nSbxOptional = 0;
191 : }
192 154 : sal_Int32 nSbxCount = n - 1;
193 154 : 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 154 : SbxArrayRef xSbxParams;
207 154 : if ( nParamsCount > 0 )
208 : {
209 98 : xSbxParams = new SbxArray;
210 98 : const Any* pParams = aParams.getConstArray();
211 200 : for ( sal_Int32 i = 0; i < nParamsCount; ++i )
212 : {
213 102 : SbxVariableRef xSbxVar = new SbxVariable( SbxVARIANT );
214 102 : unoToSbxValue( static_cast< SbxVariable* >( xSbxVar ), pParams[i] );
215 102 : xSbxParams->Put( xSbxVar, static_cast< sal_uInt16 >( i ) + 1 );
216 :
217 : // Enable passing by ref
218 102 : if ( xSbxVar->GetType() != SbxVARIANT )
219 102 : xSbxVar->SetFlag( SBX_FIXED );
220 102 : }
221 : }
222 154 : if ( xSbxParams.Is() )
223 98 : m_xMethod->SetParameters( xSbxParams );
224 :
225 : // call method
226 308 : SbxVariableRef xReturn = new SbxVariable;
227 154 : ErrCode nErr = SbxERR_OK;
228 :
229 : // if it's a document-based script, temporarily reset ThisComponent to the script invocation context
230 308 : Any aOldThisComponent;
231 154 : if ( m_documentBasicManager && m_xDocumentScriptContext.is() )
232 0 : aOldThisComponent = m_documentBasicManager->SetGlobalUNOConstant( "ThisComponent", makeAny( m_xDocumentScriptContext ) );
233 :
234 154 : if ( m_caller.getLength() && m_caller[ 0 ].hasValue() )
235 : {
236 2 : SbxVariableRef xCallerVar = new SbxVariable( SbxVARIANT );
237 2 : unoToSbxValue( static_cast< SbxVariable* >( xCallerVar ), m_caller[ 0 ] );
238 2 : nErr = m_xMethod->Call( xReturn, xCallerVar );
239 : }
240 : else
241 152 : nErr = m_xMethod->Call( xReturn );
242 :
243 154 : 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 154 : if ( xSbxParams.Is() )
253 : {
254 98 : SbxInfo* pInfo_ = m_xMethod->GetInfo();
255 98 : if ( pInfo_ )
256 : {
257 98 : OutParamMap aOutParamMap;
258 200 : for ( sal_uInt16 n = 1, nCount = xSbxParams->Count(); n < nCount; ++n )
259 : {
260 102 : const SbxParamInfo* pParamInfo = pInfo_->GetParam( n );
261 102 : if ( pParamInfo && ( pParamInfo->eType & SbxBYREF ) != 0 )
262 : {
263 4 : SbxVariable* pVar = xSbxParams->Get( n );
264 4 : if ( pVar )
265 : {
266 4 : SbxVariableRef xVar = pVar;
267 4 : aOutParamMap.insert( OutParamMap::value_type( n - 1, sbxToUnoValue( xVar ) ) );
268 : }
269 : }
270 : }
271 98 : sal_Int32 nOutParamCount = aOutParamMap.size();
272 98 : aOutParamIndex.realloc( nOutParamCount );
273 98 : aOutParam.realloc( nOutParamCount );
274 98 : sal_Int16* pOutParamIndex = aOutParamIndex.getArray();
275 98 : Any* pOutParam = aOutParam.getArray();
276 102 : for ( OutParamMap::iterator aIt = aOutParamMap.begin(); aIt != aOutParamMap.end(); ++aIt, ++pOutParamIndex, ++pOutParam )
277 : {
278 4 : *pOutParamIndex = aIt->first;
279 4 : *pOutParam = aIt->second;
280 98 : }
281 : }
282 : }
283 :
284 : // get return value
285 154 : aReturn = sbxToUnoValue( xReturn );
286 :
287 : // reset parameters
288 308 : m_xMethod->SetParameters( NULL );
289 : }
290 :
291 154 : return aReturn;
292 : }
293 :
294 :
295 :
296 :
297 24 : } // namespace basprov
298 :
299 :
300 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|