19#include <com/sun/star/lang/ServiceNotRegisteredException.hpp>
20#include <com/sun/star/lang/XServiceInfo.hpp>
21#include <com/sun/star/lang/XInitialization.hpp>
22#include <com/sun/star/beans/IntrospectionException.hpp>
23#include <com/sun/star/beans/theIntrospection.hpp>
24#include <com/sun/star/beans/MethodConcept.hpp>
25#include <com/sun/star/script/CannotConvertException.hpp>
26#include <com/sun/star/script/CannotCreateAdapterException.hpp>
27#include <com/sun/star/script/XEventAttacher2.hpp>
28#include <com/sun/star/script/Converter.hpp>
29#include <com/sun/star/script/XAllListener.hpp>
30#include <com/sun/star/script/InvocationAdapterFactory.hpp>
31#include <com/sun/star/reflection/theCoreReflection.hpp>
32#include <com/sun/star/reflection/XIdlReflection.hpp>
35#include <com/sun/star/script/XInvocation.hpp>
65class InvocationToAllListenerMapper :
public WeakImplHelper< XInvocation >
74 virtual void SAL_CALL
setValue(
const OUString& PropertyName,
const Any&
Value)
override;
75 virtual Any SAL_CALL
getValue(
const OUString& PropertyName)
override;
76 virtual sal_Bool SAL_CALL hasMethod(
const OUString&
Name)
override;
97 if( xInvocationAdapterFactory.is() && xListenerType.is() && xListener.is() )
100 new InvocationToAllListenerMapper(xListenerType, xListener, Helper);
101 Type aListenerType( xListenerType->getTypeClass(), xListenerType->getName());
103 xAdapter = xInvocationAdapterFactory->createAdapter( xInvocationToAllListenerMapper, arg2 );
110InvocationToAllListenerMapper::InvocationToAllListenerMapper
125Any SAL_CALL InvocationToAllListenerMapper::invoke(
const OUString& FunctionName,
const Sequence< Any >& Params,
132 bool bApproveFiring =
false;
137 if( ( xReturnType.is() && xReturnType->getTypeClass() != TypeClass_VOID ) ||
138 aExceptionSeq.hasElements() )
140 bApproveFiring =
true;
145 sal_uInt32 nParamCount = aParamSeq.getLength();
146 if( nParamCount > 1 )
148 const ParamInfo* pInfo = aParamSeq.getConstArray();
149 for( sal_uInt32 i = 0 ;
i < nParamCount ;
i++ )
151 if( pInfo[ i ].aMode != ParamMode_IN )
153 bApproveFiring =
true;
160 AllEventObject aAllEvent;
161 aAllEvent.Source = getXWeak();
164 aAllEvent.MethodName = FunctionName;
165 aAllEvent.Arguments = Params;
174void SAL_CALL InvocationToAllListenerMapper::setValue(
const OUString& ,
const Any& )
179Any SAL_CALL InvocationToAllListenerMapper::getValue(
const OUString& )
185sal_Bool SAL_CALL InvocationToAllListenerMapper::hasMethod(
const OUString& Name)
192sal_Bool SAL_CALL InvocationToAllListenerMapper::hasProperty(
const OUString& Name)
204class EventAttacherImpl :
public WeakImplHelper < XEventAttacher2, XInitialization, XServiceInfo >
215 virtual void SAL_CALL initialize(
const Sequence< Any >& aArguments )
override;
220 const OUString& ListenerType,
const OUString& AddListenerParam)
override;
223 const OUString& ListenerType,
const OUString& AddListenerParam,
224 const OUString& EventMethod)
override;
226 const OUString& ListenerType,
const OUString& AddListenerParam,
237 friend class FilterAllListenerImpl;
246 const OUString& aListenerType,
247 const OUString& aAddListenerParam );
281OUString SAL_CALL EventAttacherImpl::getImplementationName( )
283 return "com.sun.star.comp.EventAttacher";
286sal_Bool SAL_CALL EventAttacherImpl::supportsService(
const OUString& ServiceName )
293 return {
"com.sun.star.script.EventAttacher" };
296void SAL_CALL EventAttacherImpl::initialize(
const Sequence< Any >& Arguments)
299 for(
const Any& arg : Arguments )
301 if( arg.getValueType().getTypeClass() != TypeClass_INTERFACE )
302 throw IllegalArgumentException();
309 std::scoped_lock aGuard(
m_aMutex );
317 std::scoped_lock aGuard(
m_aMutex );
325 std::scoped_lock aGuard(
m_aMutex );
333 std::scoped_lock aGuard(
m_aMutex );
338 if( !xALAS.is() && !xI.is() && !xIdlR.is() && !xC.is() )
339 throw IllegalArgumentException();
347 std::scoped_lock aGuard(
m_aMutex );
359 std::scoped_lock aGuard(
m_aMutex );
371 std::scoped_lock aGuard(
m_aMutex );
383 std::scoped_lock aGuard(
m_aMutex );
395class FilterAllListenerImpl :
public WeakImplHelper< XAllListener >
398 FilterAllListenerImpl( EventAttacherImpl * pEA_, OUString EventMethod_,
402 virtual void SAL_CALL firing(
const AllEventObject& Event)
override;
403 virtual Any SAL_CALL approveFiring(
const AllEventObject& Event)
override;
406 virtual void SAL_CALL disposing(
const EventObject& Source)
override;
412 void convertToEventReturn( Any & rRet,
const Type& rRetType );
421FilterAllListenerImpl::FilterAllListenerImpl( EventAttacherImpl * pEA_, OUString EventMethod_,
430void SAL_CALL FilterAllListenerImpl::firing(
const AllEventObject& Event)
437void FilterAllListenerImpl::convertToEventReturn( Any & rRet,
const Type & rRetType )
440 if( rRet.getValueType().getTypeClass() == TypeClass_VOID )
442 switch( rRetType.getTypeClass() )
444 case TypeClass_INTERFACE:
446 rRet <<= Reference< XInterface >();
450 case TypeClass_BOOLEAN:
454 case TypeClass_STRING:
458 case TypeClass_FLOAT: rRet <<= float(0);
break;
459 case TypeClass_DOUBLE: rRet <<= 0.0;
break;
460 case TypeClass_BYTE: rRet <<=
sal_uInt8( 0 );
break;
461 case TypeClass_SHORT: rRet <<= sal_Int16( 0 );
break;
462 case TypeClass_LONG: rRet <<= sal_Int32( 0 );
break;
463 case TypeClass_UNSIGNED_SHORT: rRet <<= sal_uInt16( 0 );
break;
464 case TypeClass_UNSIGNED_LONG: rRet <<= sal_uInt32( 0 );
break;
469 else if( !rRet.getValueType().equals( rRetType ) )
473 throw CannotConvertException();
474 rRet =
xConverter->convertTo( rRet, rRetType );
479Any SAL_CALL FilterAllListenerImpl::approveFiring(
const AllEventObject& Event )
491 forName( Event.ListenerType.getTypeName() );
496 Type aRetType( xRetType->getTypeClass(), xRetType->getName() );
497 convertToEventReturn( aRet, aRetType );
500 catch(
const CannotConvertException& )
510void FilterAllListenerImpl::disposing(
const EventObject& )
521 const OUString& ListenerType,
522 const OUString& AddListenerParam
525 if( !xObject.is() || !AllListener.is() )
526 throw IllegalArgumentException();
529 if( !xInvocationAdapterFactory.is() )
530 throw ServiceNotRegisteredException();
533 if( !xReflection.is() )
534 throw ServiceNotRegisteredException();
551 return attachListenerForTarget(
552 xAccess, xInvocationAdapterFactory, AllListener, aObjAny, Helper,
553 ListenerType, AddListenerParam);
562 const OUString& aListenerType,
563 const OUString& aAddListenerParam)
568 sal_Int32
nIndex = aListenerType.lastIndexOf(
'.');
576 OUString aListenerName = (!aListenerType.isEmpty() && aListenerType[
nIndex] ==
'X') ? aListenerType.copy(nIndex+1) : aListenerType;
577 OUString aAddListenerName =
"add" + aListenerName;
584 OUString aMethName = rxMethod->getName();
586 if (aAddListenerName != aMethName)
590 sal_uInt32 nParamCount = params.getLength();
593 if( nParamCount == 1 )
594 xListenerType = params.getConstArray()[0];
595 else if( nParamCount == 2 )
596 xListenerType = params.getConstArray()[1];
600 xInvocationAdapterFactory, xListenerType, xAllListener, aHelper );
603 throw CannotCreateAdapterException();
604 xRet.set( xAdapter, UNO_QUERY );
607 if( nParamCount == 1 )
610 args.getArray()[0] <<= xAdapter;
613 rxMethod->invoke( aObject, args );
615 catch(
const InvocationTargetException& )
617 throw IntrospectionException();
621 else if( nParamCount == 2 )
628 if( xParamClass->getTypeClass() == TypeClass_STRING )
630 pAnys[0] <<= aAddListenerParam;
634 pAnys[1] <<= xAdapter;
640 rxMethod->invoke( aObject, args );
642 catch(
const InvocationTargetException& )
644 throw IntrospectionException();
661 if (nCount != AllListeners.getLength())
666 throw IllegalArgumentException();
669 if( !xInvocationAdapterFactory.is() )
670 throw ServiceNotRegisteredException();
673 if( !xReflection.is() )
674 throw ServiceNotRegisteredException();
694 for (sal_Int32 i = 0;
i <
nCount; ++
i)
696 pArray[
i] = attachListenerForTarget(
697 xAccess, xInvocationAdapterFactory, AllListeners[ i ],
698 aObjAny, aListeners[i].Helper, aListeners[i].ListenerType, aListeners[i].AddListenerParam);
710 const OUString& ListenerType,
711 const OUString& AddListenerParam,
712 const OUString& EventMethod
717 =
new FilterAllListenerImpl(
this, EventMethod, AllListener);
718 return attachListener( xObject, aFilterListener, Helper, ListenerType, AddListenerParam);
722void EventAttacherImpl::removeListener
725 const OUString& ListenerType,
726 const OUString& AddListenerParam,
730 if( !xObject.is() || !aToRemoveListener.is() )
731 throw IllegalArgumentException();
734 if( !xReflection.is() )
735 throw IntrospectionException();
743 throw IntrospectionException();
749 throw IntrospectionException();
752 OUString aRemoveListenerName;
753 OUString aListenerName( ListenerType );
754 sal_Int32
nIndex = aListenerName.lastIndexOf(
'.' );
761 if( aListenerName[nIndex] ==
'X' )
763 aListenerName = aListenerName.copy( nIndex +1 );
764 aRemoveListenerName =
"remove" + aListenerName;
768 sal_uInt32
i, nLen = aMethodSeq.getLength();
770 for( i = 0 ;
i < nLen ;
i++ )
776 if( aRemoveListenerName == rxMethod->getName() )
779 sal_uInt32 nParamCount = params.getLength();
782 if( nParamCount == 1 )
785 args.getArray()[0] <<= aToRemoveListener;
788 rxMethod->invoke( aObjAny, args );
790 catch(
const InvocationTargetException& )
792 throw IntrospectionException();
796 else if( nParamCount == 2 )
803 if( xParamClass->getTypeClass() == TypeClass_STRING )
804 pAnys[0] <<= AddListenerParam;
807 pAnys[1] <<= aToRemoveListener;
813 rxMethod->invoke( aObjAny, args );
815 catch(
const InvocationTargetException& )
817 throw IntrospectionException();
830 auto aFilterListenersRange = asNonConstRange(aFilterListeners);
831 for (sal_Int32 i = 0;
i <
nCount; ++
i)
833 aFilterListenersRange[
i]
834 =
new FilterAllListenerImpl(
this, aListeners[i].EventMethod, aListeners[i].AllListener);
837 return attachListeners(xObject, aFilterListeners, aListeners);
842extern "C" SAL_DLLPUBLIC_EXPORT css::uno::XInterface*
844 css::uno::XComponentContext* context, css::uno::Sequence<css::uno::Any>
const& )
846 return cppu::acquire(
new comp_EventAttacher::EventAttacherImpl(context));
Reference< XTypeConverter > m_xConverter
SAL_DLLPUBLIC_EXPORT css::uno::XInterface * eventattacher_EventAttacher(css::uno::XComponentContext *context, css::uno::Sequence< css::uno::Any > const &)
Reference< XIdlReflection > m_xReflection
Reference< XIntrospection > m_xIntrospection
Reference< XIdlClass > m_xListenerType
EventAttacherImpl * m_pEA
Reference< XInvocationAdapterFactory2 > m_xInvocationAdapterFactory
Reference< XAllListener > m_AllListener
Reference< XComponentContext > m_xContext
Reference< XAllListener > m_xAllListener
Reference< XTypeConverter > xConverter
Reference< XIntrospection > xIntrospection
void removeListener(const InterfaceRef &xObject, const css::uno::Reference< css::lang::XEventListener > &xListener)
static Reference< XInterface > createAllListenerAdapter(const Reference< XInvocationAdapterFactory2 > &xInvocationAdapterFactory, const Reference< XIdlClass > &xListenerType, const Reference< XAllListener > &xListener, const Any &Helper)
bool hasProperty(const OUString &_rName, const Reference< XPropertySet > &_rxSet)
css::uno::Sequence< OUString > getSupportedServiceNames()
OUString getImplementationName()
bool CPPUHELPER_DLLPUBLIC supportsService(css::lang::XServiceInfo *implementation, rtl::OUString const &name)
Any SAL_CALL getCaughtException()
css::beans::Optional< css::uno::Any > getValue(std::u16string_view id)
def invoke(object, methodname, argTuple)
RegError REGISTRY_CALLTYPE setValue(RegKeyHandle hKey, rtl_uString *keyName, RegValueType valueType, RegValue pData, sal_uInt32 valueSize)