1*f6e50924SAndrew Rist /************************************************************** 2cdf0e10cSrcweir * 3*f6e50924SAndrew Rist * Licensed to the Apache Software Foundation (ASF) under one 4*f6e50924SAndrew Rist * or more contributor license agreements. See the NOTICE file 5*f6e50924SAndrew Rist * distributed with this work for additional information 6*f6e50924SAndrew Rist * regarding copyright ownership. The ASF licenses this file 7*f6e50924SAndrew Rist * to you under the Apache License, Version 2.0 (the 8*f6e50924SAndrew Rist * "License"); you may not use this file except in compliance 9*f6e50924SAndrew Rist * with the License. You may obtain a copy of the License at 10*f6e50924SAndrew Rist * 11*f6e50924SAndrew Rist * http://www.apache.org/licenses/LICENSE-2.0 12*f6e50924SAndrew Rist * 13*f6e50924SAndrew Rist * Unless required by applicable law or agreed to in writing, 14*f6e50924SAndrew Rist * software distributed under the License is distributed on an 15*f6e50924SAndrew Rist * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16*f6e50924SAndrew Rist * KIND, either express or implied. See the License for the 17*f6e50924SAndrew Rist * specific language governing permissions and limitations 18*f6e50924SAndrew Rist * under the License. 19*f6e50924SAndrew Rist * 20*f6e50924SAndrew Rist *************************************************************/ 21*f6e50924SAndrew Rist 22*f6e50924SAndrew Rist 23cdf0e10cSrcweir 24cdf0e10cSrcweir // MARKER(update_precomp.py): autogen include statement, do not remove 25cdf0e10cSrcweir #include "precompiled_svx.hxx" 26cdf0e10cSrcweir #include "fmscriptingenv.hxx" 27cdf0e10cSrcweir #include "svx/fmmodel.hxx" 28cdf0e10cSrcweir 29cdf0e10cSrcweir /** === begin UNO includes === **/ 30cdf0e10cSrcweir #include <com/sun/star/lang/IllegalArgumentException.hpp> 31cdf0e10cSrcweir #include <com/sun/star/script/XScriptListener.hpp> 32cdf0e10cSrcweir #include <com/sun/star/container/XHierarchicalNameAccess.hpp> 33cdf0e10cSrcweir #include <com/sun/star/reflection/XInterfaceMethodTypeDescription.hpp> 34cdf0e10cSrcweir #include <com/sun/star/lang/DisposedException.hpp> 35cdf0e10cSrcweir /** === end UNO includes === **/ 36cdf0e10cSrcweir 37cdf0e10cSrcweir #include <tools/diagnose_ex.h> 38cdf0e10cSrcweir #include <cppuhelper/implbase1.hxx> 39cdf0e10cSrcweir #include <comphelper/implementationreference.hxx> 40cdf0e10cSrcweir #include <comphelper/componentcontext.hxx> 41cdf0e10cSrcweir #include <comphelper/processfactory.hxx> 42cdf0e10cSrcweir #include <vcl/svapp.hxx> 43cdf0e10cSrcweir #include <vos/mutex.hxx> 44cdf0e10cSrcweir #include <sfx2/objsh.hxx> 45cdf0e10cSrcweir #include <sfx2/app.hxx> 46cdf0e10cSrcweir #include <basic/basmgr.hxx> 47cdf0e10cSrcweir 48cdf0e10cSrcweir #include <boost/shared_ptr.hpp> 49cdf0e10cSrcweir 50cdf0e10cSrcweir //........................................................................ 51cdf0e10cSrcweir namespace svxform 52cdf0e10cSrcweir { 53cdf0e10cSrcweir //........................................................................ 54cdf0e10cSrcweir 55cdf0e10cSrcweir /** === begin UNO using === **/ 56cdf0e10cSrcweir using ::com::sun::star::uno::Reference; 57cdf0e10cSrcweir using ::com::sun::star::script::XEventAttacherManager; 58cdf0e10cSrcweir using ::com::sun::star::lang::IllegalArgumentException; 59cdf0e10cSrcweir using ::com::sun::star::script::XScriptListener; 60cdf0e10cSrcweir using ::com::sun::star::script::ScriptEvent; 61cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 62cdf0e10cSrcweir using ::com::sun::star::lang::EventObject; 63cdf0e10cSrcweir using ::com::sun::star::reflection::InvocationTargetException; 64cdf0e10cSrcweir using ::com::sun::star::uno::Any; 65cdf0e10cSrcweir using ::com::sun::star::container::XHierarchicalNameAccess; 66cdf0e10cSrcweir using ::com::sun::star::reflection::XInterfaceMethodTypeDescription; 67cdf0e10cSrcweir using ::com::sun::star::uno::UNO_QUERY_THROW; 68cdf0e10cSrcweir using ::com::sun::star::lang::DisposedException; 69cdf0e10cSrcweir using ::com::sun::star::uno::RuntimeException; 70cdf0e10cSrcweir using ::com::sun::star::uno::Exception; 71cdf0e10cSrcweir using ::com::sun::star::uno::Sequence; 72cdf0e10cSrcweir using ::com::sun::star::uno::XInterface; 73cdf0e10cSrcweir /** === end UNO using === **/ 74cdf0e10cSrcweir 75cdf0e10cSrcweir class FormScriptingEnvironment; 76cdf0e10cSrcweir 77cdf0e10cSrcweir //==================================================================== 78cdf0e10cSrcweir //= FormScriptListener 79cdf0e10cSrcweir //==================================================================== 80cdf0e10cSrcweir typedef ::cppu::WeakImplHelper1 < XScriptListener 81cdf0e10cSrcweir > FormScriptListener_Base; 82cdf0e10cSrcweir 83cdf0e10cSrcweir /** implements the XScriptListener interface, is used by FormScriptingEnvironment 84cdf0e10cSrcweir */ 85cdf0e10cSrcweir class FormScriptListener :public FormScriptListener_Base 86cdf0e10cSrcweir { 87cdf0e10cSrcweir private: 88cdf0e10cSrcweir ::osl::Mutex m_aMutex; 89cdf0e10cSrcweir ::rtl::Reference< FormScriptingEnvironment > m_pScriptExecutor; 90cdf0e10cSrcweir 91cdf0e10cSrcweir public: 92cdf0e10cSrcweir FormScriptListener( const ::rtl::Reference< FormScriptingEnvironment >& _pScriptExecutor ); 93cdf0e10cSrcweir 94cdf0e10cSrcweir // XScriptListener 95cdf0e10cSrcweir virtual void SAL_CALL firing( const ScriptEvent& aEvent ) throw (RuntimeException); 96cdf0e10cSrcweir virtual Any SAL_CALL approveFiring( const ScriptEvent& aEvent ) throw (InvocationTargetException, RuntimeException); 97cdf0e10cSrcweir // XEventListener 98cdf0e10cSrcweir virtual void SAL_CALL disposing( const EventObject& Source ) throw (RuntimeException); 99cdf0e10cSrcweir 100cdf0e10cSrcweir // lifetime control 101cdf0e10cSrcweir void SAL_CALL dispose(); 102cdf0e10cSrcweir 103cdf0e10cSrcweir protected: 104cdf0e10cSrcweir ~FormScriptListener(); 105cdf0e10cSrcweir 106cdf0e10cSrcweir private: 107cdf0e10cSrcweir /** determines whether calling a given method at a given listener interface can be done asynchronously 108cdf0e10cSrcweir 109cdf0e10cSrcweir @param _rListenerType 110cdf0e10cSrcweir the name of the UNO type whose method is to be checked 111cdf0e10cSrcweir @param _rMethodName 112cdf0e10cSrcweir the name of the method at the interface determined by _rListenerType 113cdf0e10cSrcweir 114cdf0e10cSrcweir @return 115cdf0e10cSrcweir <TRUE/> if and only if the method is declared <code>oneway</code>, i.e. can be called asynchronously 116cdf0e10cSrcweir */ 117cdf0e10cSrcweir bool impl_allowAsynchronousCall_nothrow( const ::rtl::OUString& _rListenerType, const ::rtl::OUString& _rMethodName ) const; 118cdf0e10cSrcweir 119cdf0e10cSrcweir /** determines whether the instance is already disposed 120cdf0e10cSrcweir */ impl_isDisposed_nothrow() const121cdf0e10cSrcweir bool impl_isDisposed_nothrow() const { return !m_pScriptExecutor.is(); } 122cdf0e10cSrcweir 123cdf0e10cSrcweir /** fires the given script event in a thread-safe manner 124cdf0e10cSrcweir 125cdf0e10cSrcweir This methods calls our script executor's doFireScriptEvent, with previously releasing the given mutex guard, 126cdf0e10cSrcweir but ensuring that our script executor is not deleted between this release and the actual call. 127cdf0e10cSrcweir 128cdf0e10cSrcweir @param _rGuard 129cdf0e10cSrcweir a clearable guard to our mutex. Must be the only active guard to our mutex. 130cdf0e10cSrcweir @param _rEvent 131cdf0e10cSrcweir the event to fire 132cdf0e10cSrcweir @param _pSyncronousResult 133cdf0e10cSrcweir a place to take a possible result of the script call. 134cdf0e10cSrcweir 135cdf0e10cSrcweir @precond 136cdf0e10cSrcweir m_pScriptExecutor is not <NULL/>. 137cdf0e10cSrcweir */ 138cdf0e10cSrcweir void impl_doFireScriptEvent_nothrow( ::osl::ClearableMutexGuard& _rGuard, const ScriptEvent& _rEvent, Any* _pSyncronousResult ); 139cdf0e10cSrcweir 140cdf0e10cSrcweir private: 141cdf0e10cSrcweir DECL_LINK( OnAsyncScriptEvent, ScriptEvent* ); 142cdf0e10cSrcweir }; 143cdf0e10cSrcweir 144cdf0e10cSrcweir //==================================================================== 145cdf0e10cSrcweir //= FormScriptingEnvironment 146cdf0e10cSrcweir //==================================================================== 147cdf0e10cSrcweir class FormScriptingEnvironment : public IFormScriptingEnvironment 148cdf0e10cSrcweir { 149cdf0e10cSrcweir private: 150cdf0e10cSrcweir typedef ::comphelper::ImplementationReference< FormScriptListener, XScriptListener > ListenerImplementation; 151cdf0e10cSrcweir 152cdf0e10cSrcweir private: 153cdf0e10cSrcweir ::osl::Mutex m_aMutex; 154cdf0e10cSrcweir oslInterlockedCount m_refCount; 155cdf0e10cSrcweir ListenerImplementation m_pScriptListener; 156cdf0e10cSrcweir FmFormModel& m_rFormModel; 157cdf0e10cSrcweir bool m_bDisposed; 158cdf0e10cSrcweir 159cdf0e10cSrcweir public: 160cdf0e10cSrcweir FormScriptingEnvironment( FmFormModel& _rModel ); 161cdf0e10cSrcweir virtual ~FormScriptingEnvironment(); 162cdf0e10cSrcweir 163cdf0e10cSrcweir // callback for FormScriptListener 164cdf0e10cSrcweir void doFireScriptEvent( const ScriptEvent& _rEvent, Any* _pSyncronousResult ); 165cdf0e10cSrcweir 166cdf0e10cSrcweir // IFormScriptingEnvironment 167cdf0e10cSrcweir virtual void registerEventAttacherManager( const Reference< XEventAttacherManager >& _rxManager ); 168cdf0e10cSrcweir virtual void revokeEventAttacherManager( const Reference< XEventAttacherManager >& _rxManager ); 169cdf0e10cSrcweir virtual void dispose(); 170cdf0e10cSrcweir 171cdf0e10cSrcweir // IReference 172cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL acquire(); 173cdf0e10cSrcweir virtual oslInterlockedCount SAL_CALL release(); 174cdf0e10cSrcweir 175cdf0e10cSrcweir private: 176cdf0e10cSrcweir void impl_registerOrRevoke_throw( const Reference< XEventAttacherManager >& _rxManager, bool _bRegister ); 177cdf0e10cSrcweir 178cdf0e10cSrcweir private: 179cdf0e10cSrcweir FormScriptingEnvironment(); // never implemented 180cdf0e10cSrcweir FormScriptingEnvironment( const FormScriptingEnvironment& ); // never implemented 181cdf0e10cSrcweir FormScriptingEnvironment& operator=( const FormScriptingEnvironment& ); // never implemented 182cdf0e10cSrcweir }; 183cdf0e10cSrcweir 184cdf0e10cSrcweir //==================================================================== 185cdf0e10cSrcweir //= FormScriptListener 186cdf0e10cSrcweir //==================================================================== 187cdf0e10cSrcweir //-------------------------------------------------------------------- FormScriptListener(const::rtl::Reference<FormScriptingEnvironment> & _pScriptExecutor)188cdf0e10cSrcweir FormScriptListener::FormScriptListener( const ::rtl::Reference< FormScriptingEnvironment >& _pScriptExecutor ) 189cdf0e10cSrcweir :m_pScriptExecutor( _pScriptExecutor ) 190cdf0e10cSrcweir { 191cdf0e10cSrcweir } 192cdf0e10cSrcweir 193cdf0e10cSrcweir //-------------------------------------------------------------------- ~FormScriptListener()194cdf0e10cSrcweir FormScriptListener::~FormScriptListener() 195cdf0e10cSrcweir { 196cdf0e10cSrcweir } 197cdf0e10cSrcweir 198cdf0e10cSrcweir //-------------------------------------------------------------------- impl_allowAsynchronousCall_nothrow(const::rtl::OUString & _rListenerType,const::rtl::OUString & _rMethodName) const199cdf0e10cSrcweir bool FormScriptListener::impl_allowAsynchronousCall_nothrow( const ::rtl::OUString& _rListenerType, const ::rtl::OUString& _rMethodName ) const 200cdf0e10cSrcweir { 201cdf0e10cSrcweir bool bAllowAsynchronousCall = false; 202cdf0e10cSrcweir try 203cdf0e10cSrcweir { 204cdf0e10cSrcweir ::comphelper::ComponentContext aContext( ::comphelper::getProcessServiceFactory() ); 205cdf0e10cSrcweir Reference< XHierarchicalNameAccess > xTypeDescriptions( aContext.getSingleton( "com.sun.star.reflection.theTypeDescriptionManager" ), UNO_QUERY_THROW ); 206cdf0e10cSrcweir 207cdf0e10cSrcweir ::rtl::OUString sMethodDescription( _rListenerType ); 208cdf0e10cSrcweir sMethodDescription += ::rtl::OUString::createFromAscii( "::" ); 209cdf0e10cSrcweir sMethodDescription += _rMethodName; 210cdf0e10cSrcweir 211cdf0e10cSrcweir Reference< XInterfaceMethodTypeDescription > xMethod( xTypeDescriptions->getByHierarchicalName( sMethodDescription ), UNO_QUERY_THROW ); 212cdf0e10cSrcweir bAllowAsynchronousCall = xMethod->isOneway(); 213cdf0e10cSrcweir } 214cdf0e10cSrcweir catch( const Exception& ) 215cdf0e10cSrcweir { 216cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 217cdf0e10cSrcweir } 218cdf0e10cSrcweir return bAllowAsynchronousCall; 219cdf0e10cSrcweir } 220cdf0e10cSrcweir 221cdf0e10cSrcweir //-------------------------------------------------------------------- impl_doFireScriptEvent_nothrow(::osl::ClearableMutexGuard & _rGuard,const ScriptEvent & _rEvent,Any * _pSyncronousResult)222cdf0e10cSrcweir void FormScriptListener::impl_doFireScriptEvent_nothrow( ::osl::ClearableMutexGuard& _rGuard, const ScriptEvent& _rEvent, Any* _pSyncronousResult ) 223cdf0e10cSrcweir { 224cdf0e10cSrcweir OSL_PRECOND( m_pScriptExecutor.is(), "FormScriptListener::impl_doFireScriptEvent_nothrow: this will crash!" ); 225cdf0e10cSrcweir 226cdf0e10cSrcweir ::rtl::Reference< FormScriptingEnvironment > pExecutor( m_pScriptExecutor ); 227cdf0e10cSrcweir _rGuard.clear(); 228cdf0e10cSrcweir pExecutor->doFireScriptEvent( _rEvent, _pSyncronousResult ); 229cdf0e10cSrcweir } 230cdf0e10cSrcweir 231cdf0e10cSrcweir //-------------------------------------------------------------------- firing(const ScriptEvent & _rEvent)232cdf0e10cSrcweir void SAL_CALL FormScriptListener::firing( const ScriptEvent& _rEvent ) throw (RuntimeException) 233cdf0e10cSrcweir { 234cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_aMutex ); 235cdf0e10cSrcweir static const ::rtl::OUString vbaInterOp = 236cdf0e10cSrcweir ::rtl::OUString::createFromAscii("VBAInterop"); 237cdf0e10cSrcweir if ( _rEvent.ScriptType.equals(vbaInterOp) ) 238cdf0e10cSrcweir return; // not handled here 239cdf0e10cSrcweir 240cdf0e10cSrcweir if ( impl_isDisposed_nothrow() ) 241cdf0e10cSrcweir return; 242cdf0e10cSrcweir 243cdf0e10cSrcweir if ( !impl_allowAsynchronousCall_nothrow( _rEvent.ListenerType.getTypeName(), _rEvent.MethodName ) ) 244cdf0e10cSrcweir { 245cdf0e10cSrcweir impl_doFireScriptEvent_nothrow( aGuard, _rEvent, NULL ); 246cdf0e10cSrcweir return; 247cdf0e10cSrcweir } 248cdf0e10cSrcweir 249cdf0e10cSrcweir acquire(); 250cdf0e10cSrcweir Application::PostUserEvent( LINK( this, FormScriptListener, OnAsyncScriptEvent ), new ScriptEvent( _rEvent ) ); 251cdf0e10cSrcweir } 252cdf0e10cSrcweir 253cdf0e10cSrcweir //-------------------------------------------------------------------- approveFiring(const ScriptEvent & _rEvent)254cdf0e10cSrcweir Any SAL_CALL FormScriptListener::approveFiring( const ScriptEvent& _rEvent ) throw (InvocationTargetException, RuntimeException) 255cdf0e10cSrcweir { 256cdf0e10cSrcweir Any aResult; 257cdf0e10cSrcweir 258cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_aMutex ); 259cdf0e10cSrcweir if ( !impl_isDisposed_nothrow() ) 260cdf0e10cSrcweir impl_doFireScriptEvent_nothrow( aGuard, _rEvent, &aResult ); 261cdf0e10cSrcweir 262cdf0e10cSrcweir return aResult; 263cdf0e10cSrcweir } 264cdf0e10cSrcweir 265cdf0e10cSrcweir //-------------------------------------------------------------------- disposing(const EventObject &)266cdf0e10cSrcweir void SAL_CALL FormScriptListener::disposing( const EventObject& /*Source*/ ) throw (RuntimeException) 267cdf0e10cSrcweir { 268cdf0e10cSrcweir // not interested in 269cdf0e10cSrcweir } 270cdf0e10cSrcweir 271cdf0e10cSrcweir //-------------------------------------------------------------------- dispose()272cdf0e10cSrcweir void SAL_CALL FormScriptListener::dispose() 273cdf0e10cSrcweir { 274cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 275cdf0e10cSrcweir m_pScriptExecutor = NULL; 276cdf0e10cSrcweir } 277cdf0e10cSrcweir 278cdf0e10cSrcweir //-------------------------------------------------------------------- IMPL_LINK(FormScriptListener,OnAsyncScriptEvent,ScriptEvent *,_pEvent)279cdf0e10cSrcweir IMPL_LINK( FormScriptListener, OnAsyncScriptEvent, ScriptEvent*, _pEvent ) 280cdf0e10cSrcweir { 281cdf0e10cSrcweir OSL_PRECOND( _pEvent != NULL, "FormScriptListener::OnAsyncScriptEvent: invalid event!" ); 282cdf0e10cSrcweir if ( !_pEvent ) 283cdf0e10cSrcweir return 1L; 284cdf0e10cSrcweir 285cdf0e10cSrcweir { 286cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_aMutex ); 287cdf0e10cSrcweir 288cdf0e10cSrcweir if ( !impl_isDisposed_nothrow() ) 289cdf0e10cSrcweir impl_doFireScriptEvent_nothrow( aGuard, *_pEvent, NULL ); 290cdf0e10cSrcweir } 291cdf0e10cSrcweir 292cdf0e10cSrcweir delete _pEvent; 293cdf0e10cSrcweir // we acquired ourself immediately before posting the event 294cdf0e10cSrcweir release(); 295cdf0e10cSrcweir return 0L; 296cdf0e10cSrcweir } 297cdf0e10cSrcweir 298cdf0e10cSrcweir //==================================================================== 299cdf0e10cSrcweir //= FormScriptingEnvironment 300cdf0e10cSrcweir //==================================================================== 301cdf0e10cSrcweir //-------------------------------------------------------------------- FormScriptingEnvironment(FmFormModel & _rModel)302cdf0e10cSrcweir FormScriptingEnvironment::FormScriptingEnvironment( FmFormModel& _rModel ) 303cdf0e10cSrcweir :m_refCount( 0 ) 304cdf0e10cSrcweir ,m_pScriptListener( NULL ) 305cdf0e10cSrcweir ,m_rFormModel( _rModel ) 306cdf0e10cSrcweir ,m_bDisposed( false ) 307cdf0e10cSrcweir { 308cdf0e10cSrcweir m_pScriptListener = ListenerImplementation( new FormScriptListener( this ) ); 309cdf0e10cSrcweir // note that this is a cyclic reference between the FormScriptListener and the FormScriptingEnvironment 310cdf0e10cSrcweir // This cycle is broken up when our instance is disposed. 311cdf0e10cSrcweir } 312cdf0e10cSrcweir 313cdf0e10cSrcweir //-------------------------------------------------------------------- ~FormScriptingEnvironment()314cdf0e10cSrcweir FormScriptingEnvironment::~FormScriptingEnvironment() 315cdf0e10cSrcweir { 316cdf0e10cSrcweir } 317cdf0e10cSrcweir 318cdf0e10cSrcweir //-------------------------------------------------------------------- impl_registerOrRevoke_throw(const Reference<XEventAttacherManager> & _rxManager,bool _bRegister)319cdf0e10cSrcweir void FormScriptingEnvironment::impl_registerOrRevoke_throw( const Reference< XEventAttacherManager >& _rxManager, bool _bRegister ) 320cdf0e10cSrcweir { 321cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 322cdf0e10cSrcweir 323cdf0e10cSrcweir if ( !_rxManager.is() ) 324cdf0e10cSrcweir throw IllegalArgumentException(); 325cdf0e10cSrcweir if ( m_bDisposed ) 326cdf0e10cSrcweir throw DisposedException(); 327cdf0e10cSrcweir 328cdf0e10cSrcweir try 329cdf0e10cSrcweir { 330cdf0e10cSrcweir if ( _bRegister ) 331cdf0e10cSrcweir _rxManager->addScriptListener( m_pScriptListener.getRef() ); 332cdf0e10cSrcweir else 333cdf0e10cSrcweir _rxManager->removeScriptListener( m_pScriptListener.getRef() ); 334cdf0e10cSrcweir } 335cdf0e10cSrcweir catch( const RuntimeException& ) { throw; } 336cdf0e10cSrcweir catch( const Exception& ) 337cdf0e10cSrcweir { 338cdf0e10cSrcweir DBG_UNHANDLED_EXCEPTION(); 339cdf0e10cSrcweir } 340cdf0e10cSrcweir } 341cdf0e10cSrcweir 342cdf0e10cSrcweir //-------------------------------------------------------------------- registerEventAttacherManager(const Reference<XEventAttacherManager> & _rxManager)343cdf0e10cSrcweir void FormScriptingEnvironment::registerEventAttacherManager( const Reference< XEventAttacherManager >& _rxManager ) 344cdf0e10cSrcweir { 345cdf0e10cSrcweir impl_registerOrRevoke_throw( _rxManager, true ); 346cdf0e10cSrcweir } 347cdf0e10cSrcweir 348cdf0e10cSrcweir //-------------------------------------------------------------------- revokeEventAttacherManager(const Reference<XEventAttacherManager> & _rxManager)349cdf0e10cSrcweir void FormScriptingEnvironment::revokeEventAttacherManager( const Reference< XEventAttacherManager >& _rxManager ) 350cdf0e10cSrcweir { 351cdf0e10cSrcweir impl_registerOrRevoke_throw( _rxManager, false ); 352cdf0e10cSrcweir } 353cdf0e10cSrcweir 354cdf0e10cSrcweir //-------------------------------------------------------------------- acquire()355cdf0e10cSrcweir oslInterlockedCount SAL_CALL FormScriptingEnvironment::acquire() 356cdf0e10cSrcweir { 357cdf0e10cSrcweir return osl_incrementInterlockedCount( &m_refCount ); 358cdf0e10cSrcweir } 359cdf0e10cSrcweir 360cdf0e10cSrcweir //-------------------------------------------------------------------- release()361cdf0e10cSrcweir oslInterlockedCount SAL_CALL FormScriptingEnvironment::release() 362cdf0e10cSrcweir { 363cdf0e10cSrcweir if ( 0 == osl_decrementInterlockedCount( &m_refCount ) ) 364cdf0e10cSrcweir { 365cdf0e10cSrcweir delete this; 366cdf0e10cSrcweir return 0; 367cdf0e10cSrcweir } 368cdf0e10cSrcweir return m_refCount; 369cdf0e10cSrcweir } 370cdf0e10cSrcweir 371cdf0e10cSrcweir //-------------------------------------------------------------------- ~IFormScriptingEnvironment()372cdf0e10cSrcweir IFormScriptingEnvironment::~IFormScriptingEnvironment() 373cdf0e10cSrcweir { 374cdf0e10cSrcweir } 375cdf0e10cSrcweir 376cdf0e10cSrcweir //-------------------------------------------------------------------- 377cdf0e10cSrcweir namespace 378cdf0e10cSrcweir { 379cdf0e10cSrcweir //................................................................ 380cdf0e10cSrcweir //. NewStyleUNOScript 381cdf0e10cSrcweir //................................................................ 382cdf0e10cSrcweir class SAL_NO_VTABLE IScript 383cdf0e10cSrcweir { 384cdf0e10cSrcweir public: 385cdf0e10cSrcweir virtual void invoke( const Sequence< Any >& _rArguments, Any& _rSynchronousResult ) = 0; 386cdf0e10cSrcweir ~IScript()387cdf0e10cSrcweir virtual ~IScript() { } 388cdf0e10cSrcweir }; 389cdf0e10cSrcweir typedef ::boost::shared_ptr< IScript > PScript; 390cdf0e10cSrcweir 391cdf0e10cSrcweir //................................................................ 392cdf0e10cSrcweir //. NewStyleUNOScript 393cdf0e10cSrcweir //................................................................ 394cdf0e10cSrcweir class NewStyleUNOScript : public IScript 395cdf0e10cSrcweir { 396cdf0e10cSrcweir SfxObjectShell& m_rObjectShell; 397cdf0e10cSrcweir const ::rtl::OUString m_sScriptCode; 398cdf0e10cSrcweir 399cdf0e10cSrcweir public: NewStyleUNOScript(SfxObjectShell & _rObjectShell,const::rtl::OUString & _rScriptCode)400cdf0e10cSrcweir NewStyleUNOScript( SfxObjectShell& _rObjectShell, const ::rtl::OUString& _rScriptCode ) 401cdf0e10cSrcweir :m_rObjectShell( _rObjectShell ) 402cdf0e10cSrcweir ,m_sScriptCode( _rScriptCode ) 403cdf0e10cSrcweir { 404cdf0e10cSrcweir } 405cdf0e10cSrcweir 406cdf0e10cSrcweir // IScript 407cdf0e10cSrcweir virtual void invoke( const Sequence< Any >& _rArguments, Any& _rSynchronousResult ); 408cdf0e10cSrcweir }; 409cdf0e10cSrcweir 410cdf0e10cSrcweir //................................................................ invoke(const Sequence<Any> & _rArguments,Any & _rSynchronousResult)411cdf0e10cSrcweir void NewStyleUNOScript::invoke( const Sequence< Any >& _rArguments, Any& _rSynchronousResult ) 412cdf0e10cSrcweir { 413cdf0e10cSrcweir Sequence< sal_Int16 > aOutArgsIndex; 414cdf0e10cSrcweir Sequence< Any > aOutArgs; 415cdf0e10cSrcweir 416cdf0e10cSrcweir m_rObjectShell.CallXScript( m_sScriptCode, _rArguments, _rSynchronousResult, aOutArgsIndex, aOutArgs ); 417cdf0e10cSrcweir } 418cdf0e10cSrcweir } 419cdf0e10cSrcweir 420cdf0e10cSrcweir //-------------------------------------------------------------------- doFireScriptEvent(const ScriptEvent & _rEvent,Any * _pSyncronousResult)421cdf0e10cSrcweir void FormScriptingEnvironment::doFireScriptEvent( const ScriptEvent& _rEvent, Any* _pSyncronousResult ) 422cdf0e10cSrcweir { 423cdf0e10cSrcweir ::vos::OClearableGuard aSolarGuard( Application::GetSolarMutex() ); 424cdf0e10cSrcweir ::osl::ClearableMutexGuard aGuard( m_aMutex ); 425cdf0e10cSrcweir 426cdf0e10cSrcweir if ( m_bDisposed ) 427cdf0e10cSrcweir return; 428cdf0e10cSrcweir 429cdf0e10cSrcweir // SfxObjectShellRef is good here since the model controls the lifetime of the object 430cdf0e10cSrcweir SfxObjectShellRef xObjectShell = m_rFormModel.GetObjectShell(); 431cdf0e10cSrcweir if( !xObjectShell.Is() ) 432cdf0e10cSrcweir return; 433cdf0e10cSrcweir 434cdf0e10cSrcweir // the script to execute 435cdf0e10cSrcweir PScript pScript; 436cdf0e10cSrcweir 437cdf0e10cSrcweir if ( !_rEvent.ScriptType.equalsAscii( "StarBasic" ) ) 438cdf0e10cSrcweir { 439cdf0e10cSrcweir pScript.reset( new NewStyleUNOScript( *xObjectShell, _rEvent.ScriptCode ) ); 440cdf0e10cSrcweir } 441cdf0e10cSrcweir else 442cdf0e10cSrcweir { 443cdf0e10cSrcweir ::rtl::OUString sScriptCode = _rEvent.ScriptCode; 444cdf0e10cSrcweir ::rtl::OUString sMacroLocation; 445cdf0e10cSrcweir 446cdf0e10cSrcweir // is there a location in the script name ("application" or "document")? 447cdf0e10cSrcweir sal_Int32 nPrefixLen = sScriptCode.indexOf( ':' ); 448cdf0e10cSrcweir DBG_ASSERT( 0 <= nPrefixLen, "FormScriptingEnvironment::doFireScriptEvent: Basic script name in old format encountered!" ); 449cdf0e10cSrcweir 450cdf0e10cSrcweir if ( 0 <= nPrefixLen ) 451cdf0e10cSrcweir { 452cdf0e10cSrcweir // and it has such a prefix 453cdf0e10cSrcweir sMacroLocation = sScriptCode.copy( 0, nPrefixLen ); 454cdf0e10cSrcweir DBG_ASSERT( 0 == sMacroLocation.compareToAscii( "document" ) 455cdf0e10cSrcweir || 0 == sMacroLocation.compareToAscii( "application" ), 456cdf0e10cSrcweir "FormScriptingEnvironment::doFireScriptEvent: invalid (unknown) prefix!" ); 457cdf0e10cSrcweir 458cdf0e10cSrcweir // strip the prefix: the SfxObjectShell::CallScript knows nothing about such prefixes 459cdf0e10cSrcweir sScriptCode = sScriptCode.copy( nPrefixLen + 1 ); 460cdf0e10cSrcweir } 461cdf0e10cSrcweir 462cdf0e10cSrcweir if ( !sMacroLocation.getLength() ) 463cdf0e10cSrcweir { 464cdf0e10cSrcweir // legacy format: use the app-wide Basic, if it has a respective method, otherwise fall back to the doc's Basic 465cdf0e10cSrcweir if ( SFX_APP()->GetBasicManager()->HasMacro( sScriptCode ) ) 466cdf0e10cSrcweir sMacroLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "application" ) ); 467cdf0e10cSrcweir else 468cdf0e10cSrcweir sMacroLocation = ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "document" ) ); 469cdf0e10cSrcweir } 470cdf0e10cSrcweir 471cdf0e10cSrcweir ::rtl::OUStringBuffer aScriptURI; 472cdf0e10cSrcweir aScriptURI.appendAscii( "vnd.sun.star.script:" ); 473cdf0e10cSrcweir aScriptURI.append( sScriptCode ); 474cdf0e10cSrcweir aScriptURI.appendAscii( "?language=Basic" ); 475cdf0e10cSrcweir aScriptURI.appendAscii( "&location=" ); 476cdf0e10cSrcweir aScriptURI.append( sMacroLocation ); 477cdf0e10cSrcweir 478cdf0e10cSrcweir const ::rtl::OUString sScriptURI( aScriptURI.makeStringAndClear() ); 479cdf0e10cSrcweir pScript.reset( new NewStyleUNOScript( *xObjectShell, sScriptURI ) ); 480cdf0e10cSrcweir } 481cdf0e10cSrcweir 482cdf0e10cSrcweir OSL_ENSURE( pScript.get(), "FormScriptingEnvironment::doFireScriptEvent: no script to execute!" ); 483cdf0e10cSrcweir if ( !pScript.get() ) 484cdf0e10cSrcweir // this is an internal error in the above code 485cdf0e10cSrcweir throw RuntimeException(); 486cdf0e10cSrcweir 487cdf0e10cSrcweir aGuard.clear(); 488cdf0e10cSrcweir aSolarGuard.clear(); 489cdf0e10cSrcweir 490cdf0e10cSrcweir Any aIgnoreResult; 491cdf0e10cSrcweir pScript->invoke( _rEvent.Arguments, _pSyncronousResult ? *_pSyncronousResult : aIgnoreResult ); 492cdf0e10cSrcweir pScript.reset(); 493cdf0e10cSrcweir 494cdf0e10cSrcweir { 495cdf0e10cSrcweir // object shells are not thread safe, so guard the destruction 496cdf0e10cSrcweir ::vos::OGuard aSolarGuarsReset( Application::GetSolarMutex() ); 497cdf0e10cSrcweir xObjectShell = NULL; 498cdf0e10cSrcweir } 499cdf0e10cSrcweir } 500cdf0e10cSrcweir 501cdf0e10cSrcweir //-------------------------------------------------------------------- dispose()502cdf0e10cSrcweir void FormScriptingEnvironment::dispose() 503cdf0e10cSrcweir { 504cdf0e10cSrcweir ::osl::MutexGuard aGuard( m_aMutex ); 505cdf0e10cSrcweir m_bDisposed = true; 506cdf0e10cSrcweir m_pScriptListener->dispose(); 507cdf0e10cSrcweir } 508cdf0e10cSrcweir 509cdf0e10cSrcweir //-------------------------------------------------------------------- createDefaultFormScriptingEnvironment(FmFormModel & _rModel)510cdf0e10cSrcweir PFormScriptingEnvironment createDefaultFormScriptingEnvironment( FmFormModel& _rModel ) 511cdf0e10cSrcweir { 512cdf0e10cSrcweir return new FormScriptingEnvironment( _rModel ); 513cdf0e10cSrcweir } 514cdf0e10cSrcweir 515cdf0e10cSrcweir //........................................................................ 516cdf0e10cSrcweir } // namespace svxform 517cdf0e10cSrcweir //........................................................................ 518cdf0e10cSrcweir 519