Avoid potential negative array index access to cached text.
[LibreOffice.git] / scripting / source / dlgprov / dlgevtatt.cxx
blobd697628c4f018d6556fdfe175fedc70641c25de2
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 "dlgevtatt.hxx"
22 #include "dlgprov.hxx"
24 #include <sfx2/strings.hrc>
25 #include <sfx2/sfxresid.hxx>
26 #include <utility>
27 #include <vcl/svapp.hxx>
28 #include <vcl/weld.hxx>
29 #include <comphelper/diagnose_ex.hxx>
31 #include <com/sun/star/awt/XControl.hpp>
32 #include <com/sun/star/awt/XControlContainer.hpp>
33 #include <com/sun/star/awt/XDialogEventHandler.hpp>
34 #include <com/sun/star/awt/XContainerWindowEventHandler.hpp>
35 #include <com/sun/star/beans/XPropertySet.hpp>
36 #include <com/sun/star/script/ScriptEventDescriptor.hpp>
37 #include <com/sun/star/script/XScriptEventsSupplier.hpp>
38 #include <com/sun/star/script/provider/XScriptProvider.hpp>
39 #include <com/sun/star/script/provider/theMasterScriptProviderFactory.hpp>
40 #include <com/sun/star/script/provider/XScriptProviderSupplier.hpp>
41 #include <com/sun/star/script/vba/XVBACompatibility.hpp>
42 #include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
43 #include <com/sun/star/reflection/XIdlMethod.hpp>
44 #include <com/sun/star/beans/MethodConcept.hpp>
45 #include <com/sun/star/beans/XMaterialHolder.hpp>
47 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
49 using namespace ::com::sun::star;
50 using namespace ::com::sun::star::awt;
51 using namespace ::com::sun::star::beans;
52 using namespace ::com::sun::star::lang;
53 using namespace ::com::sun::star::script;
54 using namespace ::com::sun::star::uno;
55 using namespace ::com::sun::star::reflection;
58 namespace dlgprov
60 namespace {
62 class DialogSFScriptListenerImpl : public DialogScriptListenerImpl
64 protected:
65 Reference< frame::XModel > m_xModel;
66 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
67 public:
68 DialogSFScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel ) : DialogScriptListenerImpl( rxContext ), m_xModel( rxModel ) {}
71 class DialogLegacyScriptListenerImpl : public DialogSFScriptListenerImpl
73 protected:
74 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
75 public:
76 DialogLegacyScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel ) : DialogSFScriptListenerImpl( rxContext, rxModel ){}
79 class DialogUnoScriptListenerImpl : public DialogSFScriptListenerImpl
81 Reference< awt::XControl > m_xControl;
82 Reference< XInterface > m_xHandler;
83 Reference< beans::XIntrospectionAccess > m_xIntrospectionAccess;
84 bool m_bDialogProviderMode;
86 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
88 public:
89 DialogUnoScriptListenerImpl( const Reference< XComponentContext >& rxContext,
90 const Reference< frame::XModel >& rxModel,
91 const Reference< awt::XControl >& rxControl,
92 const Reference< XInterface >& rxHandler,
93 const Reference< beans::XIntrospectionAccess >& rxIntrospectionAccess,
94 bool bDialogProviderMode ); // false: ContainerWindowProvider mode
98 class DialogVBAScriptListenerImpl : public DialogScriptListenerImpl
100 protected:
101 OUString msDialogCodeName;
102 OUString msDialogLibName;
103 Reference< script::XScriptListener > mxListener;
104 virtual void firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* pRet ) override;
105 public:
106 DialogVBAScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< awt::XControl >& rxControl, const Reference< frame::XModel >& xModel, OUString sDialogLibName );
111 DialogVBAScriptListenerImpl::DialogVBAScriptListenerImpl( const Reference< XComponentContext >& rxContext, const Reference< awt::XControl >& rxControl, const Reference< frame::XModel >& xModel, OUString sDialogLibName ) : DialogScriptListenerImpl( rxContext ), msDialogLibName(std::move( sDialogLibName ))
113 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
114 Sequence< Any > args(1);
115 if ( xSMgr.is() )
117 args.getArray()[0] <<= xModel;
118 mxListener.set( xSMgr->createInstanceWithArgumentsAndContext( "ooo.vba.EventListener", args, m_xContext ), UNO_QUERY );
120 if ( !rxControl.is() )
121 return;
125 Reference< XPropertySet > xProps( rxControl->getModel(), UNO_QUERY_THROW );
126 xProps->getPropertyValue("Name") >>= msDialogCodeName;
127 xProps.set( mxListener, UNO_QUERY_THROW );
128 xProps->setPropertyValue("Model", args[ 0 ] );
130 catch( const Exception& )
132 DBG_UNHANDLED_EXCEPTION("scripting");
137 void DialogVBAScriptListenerImpl::firing_impl( const script::ScriptEvent& aScriptEvent, uno::Any* )
139 if ( !(aScriptEvent.ScriptType == "VBAInterop" && mxListener.is()) )
140 return;
142 ScriptEvent aScriptEventCopy( aScriptEvent );
143 aScriptEventCopy.ScriptCode = msDialogLibName + "." + msDialogCodeName;
146 mxListener->firing( aScriptEventCopy );
148 catch( const Exception& )
150 DBG_UNHANDLED_EXCEPTION("scripting");
155 // DialogEventsAttacherImpl
158 DialogEventsAttacherImpl::DialogEventsAttacherImpl( const Reference< XComponentContext >& rxContext, const Reference< frame::XModel >& rxModel, const Reference< awt::XControl >& rxControl, const Reference< XInterface >& rxHandler, const Reference< beans::XIntrospectionAccess >& rxIntrospect, bool bProviderMode, const Reference< script::XScriptListener >& rxRTLListener, const OUString& sDialogLibName )
159 :mbUseFakeVBAEvents( false ), m_xContext( rxContext )
161 // key listeners by protocol when ScriptType = 'Script'
162 // otherwise key is the ScriptType e.g. StarBasic
163 if ( rxRTLListener.is() ) // set up handler for RTL_BASIC
164 listenersForTypes[ OUString("StarBasic") ] = rxRTLListener;
165 else
166 listenersForTypes[ OUString("StarBasic") ] = new DialogLegacyScriptListenerImpl( rxContext, rxModel );
167 // handler for Script & OUString("vnd.sun.star.UNO:")
168 listenersForTypes[ OUString("vnd.sun.star.UNO") ] = new DialogUnoScriptListenerImpl( rxContext, rxModel, rxControl, rxHandler, rxIntrospect, bProviderMode );
169 listenersForTypes[ OUString("vnd.sun.star.script") ] = new DialogSFScriptListenerImpl( rxContext, rxModel );
171 // determine the VBA compatibility mode from the Basic library container
174 uno::Reference< beans::XPropertySet > xModelProps( rxModel, uno::UNO_QUERY_THROW );
175 uno::Reference< script::vba::XVBACompatibility > xVBACompat(
176 xModelProps->getPropertyValue("BasicLibraries"), uno::UNO_QUERY_THROW );
177 mbUseFakeVBAEvents = xVBACompat->getVBACompatibilityMode();
179 catch( uno::Exception& )
182 if ( mbUseFakeVBAEvents )
183 listenersForTypes[ OUString("VBAInterop") ] = new DialogVBAScriptListenerImpl( rxContext, rxControl, rxModel, sDialogLibName );
187 DialogEventsAttacherImpl::~DialogEventsAttacherImpl()
192 Reference< script::XScriptListener > const &
193 DialogEventsAttacherImpl::getScriptListenerForKey( const OUString& sKey )
195 ListenerHash::iterator it = listenersForTypes.find( sKey );
196 if ( it == listenersForTypes.end() )
197 throw RuntimeException(); // more text info here please
198 return it->second;
200 Reference< XScriptEventsSupplier > DialogEventsAttacherImpl::getFakeVbaEventsSupplier( const Reference< XControl >& xControl, OUString const & sControlName )
202 Reference< XScriptEventsSupplier > xEventsSupplier;
203 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
204 if ( xSMgr.is() )
206 Reference< ooo::vba::XVBAToOOEventDescGen > xVBAToOOEvtDesc( xSMgr->createInstanceWithContext("ooo.vba.VBAToOOEventDesc", m_xContext ), UNO_QUERY );
207 if ( xVBAToOOEvtDesc.is() )
208 xEventsSupplier = xVBAToOOEvtDesc->getEventSupplier( xControl, sControlName );
211 return xEventsSupplier;
215 void DialogEventsAttacherImpl::attachEventsToControl( const Reference< XControl>& xControl, const Reference< XScriptEventsSupplier >& xEventsSupplier, const Any& Helper )
217 if ( !xEventsSupplier.is() )
218 return;
220 Reference< container::XNameContainer > xEventCont = xEventsSupplier->getEvents();
222 Reference< XControlModel > xControlModel = xControl->getModel();
223 if ( !xEventCont.is() )
224 return;
226 const Sequence< OUString > aNames = xEventCont->getElementNames();
228 for ( const OUString& rName : aNames )
230 ScriptEventDescriptor aDesc;
232 Any aElement = xEventCont->getByName( rName );
233 aElement >>= aDesc;
234 OUString sKey = aDesc.ScriptType;
235 if ( aDesc.ScriptType == "Script" || aDesc.ScriptType == "UNO" )
237 sal_Int32 nIndex = aDesc.ScriptCode.indexOf( ':' );
238 sKey = aDesc.ScriptCode.copy( 0, nIndex );
240 Reference< XAllListener > xAllListener =
241 new DialogAllListenerImpl( getScriptListenerForKey( sKey ), aDesc.ScriptType, aDesc.ScriptCode );
243 // try first to attach event to the ControlModel
244 bool bSuccess = false;
247 Reference< XEventListener > xListener_ = m_xEventAttacher->attachSingleEventListener(
248 xControlModel, xAllListener, Helper, aDesc.ListenerType,
249 aDesc.AddListenerParam, aDesc.EventMethod );
251 if ( xListener_.is() )
252 bSuccess = true;
254 catch ( const Exception& )
256 DBG_UNHANDLED_EXCEPTION("scripting");
261 // if we had no success, try to attach to the control
262 if ( !bSuccess )
264 m_xEventAttacher->attachSingleEventListener(
265 xControl, xAllListener, Helper, aDesc.ListenerType,
266 aDesc.AddListenerParam, aDesc.EventMethod );
269 catch ( const Exception& )
271 DBG_UNHANDLED_EXCEPTION("scripting");
277 void DialogEventsAttacherImpl::nestedAttachEvents( const Sequence< Reference< XInterface > >& Objects, const Any& Helper, OUString& sDialogCodeName )
279 for ( const Reference< XInterface >& rObject : Objects )
281 // We know that we have to do with instances of XControl.
282 // Otherwise this is not the right implementation for
283 // XScriptEventsAttacher and we have to give up.
284 Reference< XControl > xControl( rObject, UNO_QUERY );
285 Reference< XControlContainer > xControlContainer( xControl, UNO_QUERY );
286 Reference< XDialog > xDialog( xControl, UNO_QUERY );
287 if ( !xControl.is() )
288 throw IllegalArgumentException();
290 // get XEventsSupplier from control model
291 Reference< XControlModel > xControlModel = xControl->getModel();
292 Reference< XScriptEventsSupplier > xEventsSupplier( xControlModel, UNO_QUERY );
293 attachEventsToControl( xControl, xEventsSupplier, Helper );
294 if ( mbUseFakeVBAEvents )
296 xEventsSupplier.set( getFakeVbaEventsSupplier( xControl, sDialogCodeName ) );
297 Any newHelper(xControl );
298 attachEventsToControl( xControl, xEventsSupplier, newHelper );
300 if ( xControlContainer.is() && !xDialog.is() )
302 Sequence< Reference< XControl > > aControls = xControlContainer->getControls();
303 sal_Int32 nControlCount = aControls.getLength();
305 Sequence< Reference< XInterface > > aObjects( nControlCount );
306 Reference< XInterface >* pObjects2 = aObjects.getArray();
307 const Reference< XControl >* pControls = aControls.getConstArray();
309 for ( sal_Int32 i2 = 0; i2 < nControlCount; ++i2 )
311 pObjects2[i2].set( pControls[i2], UNO_QUERY );
313 nestedAttachEvents( aObjects, Helper, sDialogCodeName );
319 // XScriptEventsAttacher
322 void SAL_CALL DialogEventsAttacherImpl::attachEvents( const Sequence< Reference< XInterface > >& Objects,
323 const css::uno::Reference<css::script::XScriptListener>&,
324 const Any& Helper )
326 // get EventAttacher
328 ::osl::MutexGuard aGuard( getMutex() );
330 if ( !m_xEventAttacher.is() )
332 Reference< XMultiComponentFactory > xSMgr( m_xContext->getServiceManager() );
333 if ( !xSMgr.is() )
334 throw RuntimeException();
336 m_xEventAttacher.set( xSMgr->createInstanceWithContext(
337 "com.sun.star.script.EventAttacher", m_xContext ), UNO_QUERY );
339 if ( !m_xEventAttacher.is() )
340 throw ServiceNotRegisteredException();
343 OUString sDialogCodeName;
344 sal_Int32 nObjCount = Objects.getLength();
345 Reference< awt::XControl > xDlgControl( Objects[ nObjCount - 1 ], uno::UNO_QUERY ); // last object is the dialog
346 if ( xDlgControl.is() )
348 Reference< XPropertySet > xProps( xDlgControl->getModel(), UNO_QUERY );
351 xProps->getPropertyValue("Name") >>= sDialogCodeName;
353 catch( Exception& ){}
355 // go over all objects
356 nestedAttachEvents( Objects, Helper, sDialogCodeName );
360 // DialogAllListenerImpl
363 DialogAllListenerImpl::DialogAllListenerImpl( const Reference< XScriptListener >& rxListener,
364 OUString sScriptType, OUString sScriptCode )
365 :m_xScriptListener( rxListener )
366 ,m_sScriptType(std::move( sScriptType ))
367 ,m_sScriptCode(std::move( sScriptCode ))
372 DialogAllListenerImpl::~DialogAllListenerImpl()
377 void DialogAllListenerImpl::firing_impl( const AllEventObject& Event, Any* pRet )
379 ScriptEvent aScriptEvent;
380 aScriptEvent.Source = getXWeak(); // get correct XInterface
381 aScriptEvent.ListenerType = Event.ListenerType;
382 aScriptEvent.MethodName = Event.MethodName;
383 aScriptEvent.Arguments = Event.Arguments;
384 aScriptEvent.Helper = Event.Helper;
385 aScriptEvent.ScriptType = m_sScriptType;
386 aScriptEvent.ScriptCode = m_sScriptCode;
388 if ( m_xScriptListener.is() )
390 if ( pRet )
391 *pRet = m_xScriptListener->approveFiring( aScriptEvent );
392 else
393 m_xScriptListener->firing( aScriptEvent );
398 // XEventListener
401 void DialogAllListenerImpl::disposing(const EventObject& )
406 // XAllListener
409 void DialogAllListenerImpl::firing( const AllEventObject& Event )
411 //::osl::MutexGuard aGuard( getMutex() );
413 firing_impl( Event, nullptr );
417 Any DialogAllListenerImpl::approveFiring( const AllEventObject& Event )
419 //::osl::MutexGuard aGuard( getMutex() );
421 Any aReturn;
422 firing_impl( Event, &aReturn );
423 return aReturn;
427 // DialogScriptListenerImpl
430 DialogUnoScriptListenerImpl::DialogUnoScriptListenerImpl( const Reference< XComponentContext >& rxContext,
431 const Reference< css::frame::XModel >& rxModel,
432 const Reference< css::awt::XControl >& rxControl,
433 const Reference< css::uno::XInterface >& rxHandler,
434 const Reference< css::beans::XIntrospectionAccess >& rxIntrospectionAccess,
435 bool bDialogProviderMode )
436 : DialogSFScriptListenerImpl( rxContext, rxModel )
437 ,m_xControl( rxControl )
438 ,m_xHandler( rxHandler )
439 ,m_xIntrospectionAccess( rxIntrospectionAccess )
440 ,m_bDialogProviderMode( bDialogProviderMode )
445 DialogScriptListenerImpl::~DialogScriptListenerImpl()
450 void DialogSFScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
454 Reference< provider::XScriptProvider > xScriptProvider;
455 if ( m_xModel.is() )
457 Reference< provider::XScriptProviderSupplier > xSupplier( m_xModel, UNO_QUERY );
458 OSL_ENSURE( xSupplier.is(), "DialogScriptListenerImpl::firing_impl: failed to get script provider supplier" );
459 if ( xSupplier.is() )
460 xScriptProvider.set( xSupplier->getScriptProvider() );
462 else
464 OSL_ASSERT( m_xContext.is() );
465 if ( m_xContext.is() )
467 Reference< provider::XScriptProviderFactory > xFactory =
468 provider::theMasterScriptProviderFactory::get( m_xContext );
470 Any aCtx;
471 aCtx <<= OUString("user");
472 xScriptProvider = xFactory->createScriptProvider( aCtx );
476 OSL_ENSURE( xScriptProvider.is(), "DialogScriptListenerImpl::firing_impl: failed to get script provider" );
478 if ( xScriptProvider.is() )
480 Reference< provider::XScript > xScript = xScriptProvider->getScript( aScriptEvent.ScriptCode );
481 OSL_ENSURE( xScript.is(), "DialogScriptListenerImpl::firing_impl: failed to get script" );
483 if ( xScript.is() )
485 Sequence< Any > aInParams;
486 Sequence< sal_Int16 > aOutParamsIndex;
487 Sequence< Any > aOutParams;
489 // get arguments for script
490 aInParams = aScriptEvent.Arguments;
492 Any aResult = xScript->invoke( aInParams, aOutParamsIndex, aOutParams );
493 if ( pRet )
494 *pRet = aResult;
498 catch ( const Exception& )
500 DBG_UNHANDLED_EXCEPTION("scripting");
504 void DialogLegacyScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
506 OUString sScriptURL;
507 OUString sScriptCode( aScriptEvent.ScriptCode );
509 if ( aScriptEvent.ScriptType != "StarBasic" )
510 return;
512 // StarBasic script: convert ScriptCode to scriptURL
513 sal_Int32 nIndex = sScriptCode.indexOf( ':' );
514 if ( nIndex >= 0 && nIndex < sScriptCode.getLength() )
516 sScriptURL = OUString::Concat("vnd.sun.star.script:") +
517 sScriptCode.subView( nIndex + 1 ) +
518 "?language=Basic&location=" +
519 sScriptCode.subView( 0, nIndex );
521 ScriptEvent aSFScriptEvent( aScriptEvent );
522 aSFScriptEvent.ScriptCode = sScriptURL;
523 DialogSFScriptListenerImpl::firing_impl( aSFScriptEvent, pRet );
526 void DialogUnoScriptListenerImpl::firing_impl( const ScriptEvent& aScriptEvent, Any* pRet )
528 OUString aMethodName = aScriptEvent.ScriptCode.copy( strlen("vnd.sun.star.UNO:") );
530 const Any* pArguments = aScriptEvent.Arguments.getConstArray();
531 Any aEventObject = pArguments[0];
533 bool bHandled = false;
534 if( m_xHandler.is() )
536 if( m_bDialogProviderMode )
538 Reference< XDialogEventHandler > xDialogEventHandler( m_xHandler, UNO_QUERY );
539 if( xDialogEventHandler.is() )
541 Reference< XDialog > xDialog( m_xControl, UNO_QUERY );
542 bHandled = xDialogEventHandler->callHandlerMethod( xDialog, aEventObject, aMethodName );
545 else
547 Reference< XContainerWindowEventHandler > xContainerWindowEventHandler( m_xHandler, UNO_QUERY );
548 if( xContainerWindowEventHandler.is() )
550 Reference< XWindow > xWindow( m_xControl, UNO_QUERY );
551 bHandled = xContainerWindowEventHandler->callHandlerMethod( xWindow, aEventObject, aMethodName );
556 Any aRet;
557 if( !bHandled && m_xIntrospectionAccess.is() )
561 // call method
562 const Reference< XIdlMethod >& rxMethod = m_xIntrospectionAccess->
563 getMethod( aMethodName, MethodConcept::ALL - MethodConcept::DANGEROUS );
565 Reference< XMaterialHolder > xMaterialHolder =
566 Reference< XMaterialHolder >::query( m_xIntrospectionAccess );
567 Any aHandlerObject = xMaterialHolder->getMaterial();
569 Sequence< Reference< XIdlClass > > aParamTypeSeq = rxMethod->getParameterTypes();
570 sal_Int32 nParamCount = aParamTypeSeq.getLength();
571 if( nParamCount == 0 )
573 Sequence<Any> args;
574 rxMethod->invoke( aHandlerObject, args );
575 bHandled = true;
577 else if( nParamCount == 2 )
579 // Signature check automatically done by reflection
580 Sequence<Any> Args(2);
581 Any* pArgs = Args.getArray();
582 if( m_bDialogProviderMode )
584 Reference< XDialog > xDialog( m_xControl, UNO_QUERY );
585 pArgs[0] <<= xDialog;
587 else
589 Reference< XWindow > xWindow( m_xControl, UNO_QUERY );
590 pArgs[0] <<= xWindow;
592 pArgs[1] = aEventObject;
593 aRet = rxMethod->invoke( aHandlerObject, Args );
594 bHandled = true;
597 catch( const Exception& )
599 DBG_UNHANDLED_EXCEPTION("scripting");
603 if( bHandled )
605 if( pRet )
606 *pRet = aRet;
608 else
610 OUString aRes(SfxResId(STR_ERRUNOEVENTBINDUNG));
611 OUString aQuoteChar( "\"" );
613 sal_Int32 nIndex = aRes.indexOf( '%' );
615 OUString aOUFinal =
616 aRes.subView( 0, nIndex ) +
617 aQuoteChar + aMethodName + aQuoteChar +
618 aRes.subView( nIndex + 2 );
620 std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(nullptr,
621 VclMessageType::Warning, VclButtonsType::Ok, aOUFinal));
622 xBox->run();
627 // XEventListener
630 void DialogScriptListenerImpl::disposing(const EventObject& )
635 // XScriptListener
638 void DialogScriptListenerImpl::firing( const ScriptEvent& aScriptEvent )
640 //::osl::MutexGuard aGuard( getMutex() );
642 firing_impl( aScriptEvent, nullptr );
646 Any DialogScriptListenerImpl::approveFiring( const ScriptEvent& aScriptEvent )
648 //::osl::MutexGuard aGuard( getMutex() );
650 Any aReturn;
651 firing_impl( aScriptEvent, &aReturn );
652 return aReturn;
656 } // namespace dlgprov
659 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */