1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
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 <sal/config.h>
22 #include <o3tl/any.hxx>
23 #include <o3tl/safeint.hxx>
24 #include <osl/diagnose.h>
25 #include <comphelper/eventattachermgr.hxx>
26 #include <comphelper/sequence.hxx>
27 #include <com/sun/star/beans/theIntrospection.hpp>
28 #include <com/sun/star/io/XObjectInputStream.hpp>
29 #include <com/sun/star/io/XPersistObject.hpp>
30 #include <com/sun/star/io/XObjectOutputStream.hpp>
31 #include <com/sun/star/io/XMarkableStream.hpp>
32 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
33 #include <com/sun/star/lang/XInitialization.hpp>
34 #include <com/sun/star/reflection/theCoreReflection.hpp>
35 #include <com/sun/star/reflection/XIdlClass.hpp>
36 #include <com/sun/star/reflection/XIdlReflection.hpp>
37 #include <com/sun/star/reflection/XIdlMethod.hpp>
38 #include <com/sun/star/script/CannotConvertException.hpp>
39 #include <com/sun/star/script/Converter.hpp>
40 #include <com/sun/star/script/XEventAttacher2.hpp>
41 #include <com/sun/star/script/XEventAttacherManager.hpp>
42 #include <com/sun/star/script/XScriptListener.hpp>
43 #include <cppuhelper/weak.hxx>
44 #include <comphelper/interfacecontainer4.hxx>
45 #include <cppuhelper/exc_hlp.hxx>
46 #include <cppuhelper/implbase.hxx>
47 #include <rtl/ref.hxx>
54 using namespace com::sun::star::uno
;
55 using namespace com::sun::star::io
;
56 using namespace com::sun::star::lang
;
57 using namespace com::sun::star::beans
;
58 using namespace com::sun::star::script
;
59 using namespace com::sun::star::reflection
;
69 struct AttachedObject_Impl
71 Reference
< XInterface
> xTarget
;
72 std::vector
< Reference
< XEventListener
> > aAttachedListenerSeq
;
76 struct AttacherIndex_Impl
78 std::deque
< ScriptEventDescriptor
> aEventList
;
79 std::deque
< AttachedObject_Impl
> aObjList
;
83 class ImplEventAttacherManager
84 : public WeakImplHelper
< XEventAttacherManager
, XPersistObject
>
86 friend class AttacherAllListener_Impl
;
87 std::deque
< AttacherIndex_Impl
> aIndex
;
89 // Container for the ScriptListener
90 OInterfaceContainerHelper4
<XScriptListener
> aScriptListeners
;
91 // Instance of EventAttacher
92 Reference
< XEventAttacher2
> xAttacher
;
93 Reference
< XComponentContext
> mxContext
;
94 Reference
< XIdlReflection
> mxCoreReflection
;
95 Reference
< XTypeConverter
> xConverter
;
98 ImplEventAttacherManager( const Reference
< XIntrospection
> & rIntrospection
,
99 const Reference
< XComponentContext
>& rContext
);
101 // Methods of XEventAttacherManager
102 virtual void SAL_CALL
registerScriptEvent(sal_Int32 Index
, const ScriptEventDescriptor
& ScriptEvent
) override
;
103 virtual void SAL_CALL
registerScriptEvents(sal_Int32 Index
, const Sequence
< ScriptEventDescriptor
>& ScriptEvents
) override
;
104 virtual void SAL_CALL
revokeScriptEvent(sal_Int32 Index
, const OUString
& ListenerType
, const OUString
& EventMethod
, const OUString
& removeListenerParam
) override
;
105 virtual void SAL_CALL
revokeScriptEvents(sal_Int32 Index
) override
;
106 virtual void SAL_CALL
insertEntry(sal_Int32 Index
) override
;
107 virtual void SAL_CALL
removeEntry(sal_Int32 Index
) override
;
108 virtual Sequence
< ScriptEventDescriptor
> SAL_CALL
getScriptEvents(sal_Int32 Index
) override
;
109 virtual void SAL_CALL
attach(sal_Int32 Index
, const Reference
< XInterface
>& Object
, const Any
& Helper
) override
;
110 virtual void SAL_CALL
detach(sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
) override
;
111 virtual void SAL_CALL
addScriptListener(const Reference
< XScriptListener
>& aListener
) override
;
112 virtual void SAL_CALL
removeScriptListener(const Reference
< XScriptListener
>& Listener
) override
;
114 // Methods of XPersistObject
115 virtual OUString SAL_CALL
getServiceName() override
;
116 virtual void SAL_CALL
write(const Reference
< XObjectOutputStream
>& OutStream
) override
;
117 virtual void SAL_CALL
read(const Reference
< XObjectInputStream
>& InStream
) override
;
120 void registerScriptEvent(std::unique_lock
<std::mutex
>&, sal_Int32 Index
, const ScriptEventDescriptor
& ScriptEvent
);
121 void registerScriptEvents(std::unique_lock
<std::mutex
>&, sal_Int32 Index
, const Sequence
< ScriptEventDescriptor
>& ScriptEvents
);
122 void attach(std::unique_lock
<std::mutex
>&, sal_Int32 Index
, const Reference
< XInterface
>& Object
, const Any
& Helper
);
123 void detach(std::unique_lock
<std::mutex
>&, sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
);
124 void insertEntry(std::unique_lock
<std::mutex
>&, sal_Int32 Index
);
126 /// @throws Exception
127 Reference
< XIdlReflection
> getReflection(std::unique_lock
<std::mutex
>&);
129 /** checks if <arg>_nIndex</arg> is a valid index, throws an <type>IllegalArgumentException</type> if not
133 the iterator pointing to the position indicated by the index
135 std::deque
<AttacherIndex_Impl
>::iterator
implCheckIndex( sal_Int32 _nIndex
);
139 // Implementation of an EventAttacher-subclass 'AllListeners', which
140 // only passes individual events of the general AllListeners.
141 class AttacherAllListener_Impl
: public WeakImplHelper
< XAllListener
>
143 rtl::Reference
<ImplEventAttacherManager
> mxManager
;
144 OUString
const aScriptType
;
145 OUString
const aScriptCode
;
147 /// @throws CannotConvertException
148 void convertToEventReturn( Any
& rRet
, const Type
& rRetType
);
150 AttacherAllListener_Impl( ImplEventAttacherManager
* pManager_
, OUString aScriptType_
,
151 OUString aScriptCode_
);
153 // Methods of XAllListener
154 virtual void SAL_CALL
firing(const AllEventObject
& Event
) override
;
155 virtual Any SAL_CALL
approveFiring(const AllEventObject
& Event
) override
;
157 // Methods of XEventListener
158 virtual void SAL_CALL
disposing(const EventObject
& Source
) override
;
163 AttacherAllListener_Impl::AttacherAllListener_Impl
165 ImplEventAttacherManager
* pManager_
,
166 OUString aScriptType_
,
167 OUString aScriptCode_
169 : mxManager( pManager_
)
170 , aScriptType(std::move( aScriptType_
))
171 , aScriptCode(std::move( aScriptCode_
))
176 // Methods of XAllListener
177 void SAL_CALL
AttacherAllListener_Impl::firing(const AllEventObject
& Event
)
179 ScriptEvent aScriptEvent
;
180 aScriptEvent
.Source
= static_cast<OWeakObject
*>(mxManager
.get()); // get correct XInterface
181 aScriptEvent
.ListenerType
= Event
.ListenerType
;
182 aScriptEvent
.MethodName
= Event
.MethodName
;
183 aScriptEvent
.Arguments
= Event
.Arguments
;
184 aScriptEvent
.Helper
= Event
.Helper
;
185 aScriptEvent
.ScriptType
= aScriptType
;
186 aScriptEvent
.ScriptCode
= aScriptCode
;
188 // Iterate over all listeners and pass events.
189 std::unique_lock
l(mxManager
->m_aMutex
);
190 mxManager
->aScriptListeners
.notifyEach( l
, &XScriptListener::firing
, aScriptEvent
);
194 // Convert to the standard event return
195 void AttacherAllListener_Impl::convertToEventReturn( Any
& rRet
, const Type
& rRetType
)
197 // no return value? Set to the specified values
198 if( rRet
.getValueType().getTypeClass() == TypeClass_VOID
)
200 switch( rRetType
.getTypeClass() )
202 case TypeClass_INTERFACE
:
204 rRet
<<= Reference
< XInterface
>();
208 case TypeClass_BOOLEAN
:
212 case TypeClass_STRING
:
216 case TypeClass_FLOAT
: rRet
<<= float(0); break;
217 case TypeClass_DOUBLE
: rRet
<<= 0.0; break;
218 case TypeClass_BYTE
: rRet
<<= sal_uInt8(0); break;
219 case TypeClass_SHORT
: rRet
<<= sal_Int16( 0 ); break;
220 case TypeClass_LONG
: rRet
<<= sal_Int32( 0 ); break;
221 case TypeClass_UNSIGNED_SHORT
: rRet
<<= sal_uInt16( 0 ); break;
222 case TypeClass_UNSIGNED_LONG
: rRet
<<= sal_uInt32( 0 ); break;
229 else if( !rRet
.getValueType().equals( rRetType
) )
231 if( !mxManager
->xConverter
.is() )
232 throw CannotConvertException();
233 rRet
= mxManager
->xConverter
->convertTo( rRet
, rRetType
);
237 // Methods of XAllListener
238 Any SAL_CALL
AttacherAllListener_Impl::approveFiring( const AllEventObject
& Event
)
240 ScriptEvent aScriptEvent
;
241 aScriptEvent
.Source
= static_cast<OWeakObject
*>(mxManager
.get()); // get correct XInterface
242 aScriptEvent
.ListenerType
= Event
.ListenerType
;
243 aScriptEvent
.MethodName
= Event
.MethodName
;
244 aScriptEvent
.Arguments
= Event
.Arguments
;
245 aScriptEvent
.Helper
= Event
.Helper
;
246 aScriptEvent
.ScriptType
= aScriptType
;
247 aScriptEvent
.ScriptCode
= aScriptCode
;
250 // Iterate over all listeners and pass events.
251 std::unique_lock
l(mxManager
->m_aMutex
);
252 OInterfaceIteratorHelper4
aIt( l
, mxManager
->aScriptListeners
);
253 while( aIt
.hasMoreElements() )
255 // cannot hold lock over call to approveFiring, since it might recurse back into us
257 aRet
= aIt
.next()->approveFiring( aScriptEvent
);
261 Reference
< XIdlClass
> xListenerType
= mxManager
->getReflection(l
)->
262 forName( Event
.ListenerType
.getTypeName() );
263 Reference
< XIdlMethod
> xMeth
= xListenerType
->getMethod( Event
.MethodName
);
266 Reference
< XIdlClass
> xRetType
= xMeth
->getReturnType();
267 Type
aRetType(xRetType
->getTypeClass(), xRetType
->getName());
268 convertToEventReturn( aRet
, aRetType
);
271 switch( aRet
.getValueType().getTypeClass() )
273 case TypeClass_INTERFACE
:
275 // Interface not null, return
276 Reference
< XInterface
> x
;
283 case TypeClass_BOOLEAN
:
285 if( !(*o3tl::forceAccess
<bool>(aRet
)) )
289 case TypeClass_STRING
:
290 // none empty string -> return
291 if( !o3tl::forceAccess
<OUString
>(aRet
)->isEmpty() )
295 // none zero number -> return
296 case TypeClass_FLOAT
: if( *o3tl::forceAccess
<float>(aRet
) ) return aRet
; break;
297 case TypeClass_DOUBLE
: if( *o3tl::forceAccess
<double>(aRet
) ) return aRet
; break;
298 case TypeClass_BYTE
: if( *o3tl::forceAccess
<sal_Int8
>(aRet
) ) return aRet
; break;
299 case TypeClass_SHORT
: if( *o3tl::forceAccess
<sal_Int16
>(aRet
) ) return aRet
; break;
300 case TypeClass_LONG
: if( *o3tl::forceAccess
<sal_Int32
>(aRet
) ) return aRet
; break;
301 case TypeClass_UNSIGNED_SHORT
: if( *o3tl::forceAccess
<sal_uInt16
>(aRet
) ) return aRet
; break;
302 case TypeClass_UNSIGNED_LONG
: if( *o3tl::forceAccess
<sal_uInt32
>(aRet
) ) return aRet
; break;
309 catch (const CannotConvertException
&)
311 // silent ignore conversions errors from a script call
312 Reference
< XIdlClass
> xListenerType
= mxManager
->getReflection(l
)->
313 forName( Event
.ListenerType
.getTypeName() );
314 Reference
< XIdlMethod
> xMeth
= xListenerType
->getMethod( Event
.MethodName
);
317 Reference
< XIdlClass
> xRetType
= xMeth
->getReturnType();
318 Type
aRetType(xRetType
->getTypeClass(), xRetType
->getName());
322 convertToEventReturn( aRet
, aRetType
);
324 catch (const CannotConvertException
& e
)
326 css::uno::Any anyEx
= cppu::getCaughtException();
327 throw css::lang::WrappedTargetRuntimeException(
328 "wrapped CannotConvertException " + e
.Message
,
329 css::uno::Reference
<css::uno::XInterface
>(), anyEx
);
337 // Methods of XEventListener
338 void SAL_CALL
AttacherAllListener_Impl::disposing(const EventObject
& )
340 // It is up to the container to release the object
343 // Constructor method for EventAttacherManager
344 Reference
< XEventAttacherManager
> createEventAttacherManager( const Reference
< XComponentContext
> & rxContext
)
346 Reference
< XIntrospection
> xIntrospection
= theIntrospection::get( rxContext
);
347 return new ImplEventAttacherManager( xIntrospection
, rxContext
);
351 ImplEventAttacherManager::ImplEventAttacherManager( const Reference
< XIntrospection
> & rIntrospection
,
352 const Reference
< XComponentContext
>& rContext
)
353 : mxContext( rContext
)
358 Reference
< XInterface
> xIFace( rContext
->getServiceManager()->createInstanceWithContext(
359 "com.sun.star.script.EventAttacher", rContext
) );
362 xAttacher
.set( xIFace
, UNO_QUERY
);
364 xConverter
= Converter::create(rContext
);
367 Reference
< XInitialization
> xInit( xAttacher
, UNO_QUERY
);
370 xInit
->initialize({ Any(rIntrospection
) });
374 Reference
< XIdlReflection
> ImplEventAttacherManager::getReflection(std::unique_lock
<std::mutex
>&)
376 // Do we already have a service? If not, create one.
377 if( !mxCoreReflection
.is() )
379 mxCoreReflection
= theCoreReflection::get(mxContext
);
381 return mxCoreReflection
;
385 std::deque
< AttacherIndex_Impl
>::iterator
ImplEventAttacherManager::implCheckIndex( sal_Int32 _nIndex
)
387 if ( (_nIndex
< 0) || (o3tl::make_unsigned(_nIndex
) >= aIndex
.size()) )
388 throw IllegalArgumentException("wrong index", static_cast<cppu::OWeakObject
*>(this), 1);
390 std::deque
<AttacherIndex_Impl
>::iterator aIt
= aIndex
.begin() + _nIndex
;
394 // Methods of XEventAttacherManager
395 void SAL_CALL
ImplEventAttacherManager::registerScriptEvent
398 const ScriptEventDescriptor
& ScriptEvent
401 std::unique_lock
l(m_aMutex
);
402 registerScriptEvent(l
, nIndex
, ScriptEvent
);
405 void ImplEventAttacherManager::registerScriptEvent
407 std::unique_lock
<std::mutex
>&,
409 const ScriptEventDescriptor
& ScriptEvent
412 // Examine the index and apply the array
413 std::deque
<AttacherIndex_Impl
>::iterator aIt
= implCheckIndex( nIndex
);
415 ScriptEventDescriptor aEvt
= ScriptEvent
;
416 sal_Int32 nLastDot
= aEvt
.ListenerType
.lastIndexOf('.');
418 aEvt
.ListenerType
= aEvt
.ListenerType
.copy(nLastDot
+1);
419 aIt
->aEventList
.push_back( aEvt
);
421 // register new Event
422 for( auto& rObj
: aIt
->aObjList
)
424 Reference
< XAllListener
> xAll
=
425 new AttacherAllListener_Impl( this, ScriptEvent
.ScriptType
, ScriptEvent
.ScriptCode
);
428 rObj
.aAttachedListenerSeq
.push_back( xAttacher
->attachSingleEventListener( rObj
.xTarget
, xAll
,
429 rObj
.aHelper
, ScriptEvent
.ListenerType
,
430 ScriptEvent
.AddListenerParam
, ScriptEvent
.EventMethod
) );
439 void SAL_CALL
ImplEventAttacherManager::registerScriptEvents
442 const Sequence
< ScriptEventDescriptor
>& ScriptEvents
445 std::unique_lock
l(m_aMutex
);
446 registerScriptEvents(l
, nIndex
, ScriptEvents
);
449 void ImplEventAttacherManager::registerScriptEvents
451 std::unique_lock
<std::mutex
>& l
,
453 const Sequence
< ScriptEventDescriptor
>& ScriptEvents
456 // Examine the index and apply the array
457 std::deque
< AttachedObject_Impl
> aList
= implCheckIndex( nIndex
)->aObjList
;
458 for( const auto& rObj
: aList
)
459 detach( l
, nIndex
, rObj
.xTarget
);
461 const ScriptEventDescriptor
* pArray
= ScriptEvents
.getConstArray();
462 sal_Int32 nLen
= ScriptEvents
.getLength();
463 for( sal_Int32 i
= 0 ; i
< nLen
; i
++ )
464 registerScriptEvent( l
, nIndex
, pArray
[ i
] );
466 for( const auto& rObj
: aList
)
467 attach( l
, nIndex
, rObj
.xTarget
, rObj
.aHelper
);
471 void SAL_CALL
ImplEventAttacherManager::revokeScriptEvent
474 const OUString
& ListenerType
,
475 const OUString
& EventMethod
,
476 const OUString
& ToRemoveListenerParam
479 std::unique_lock
l(m_aMutex
);
481 std::deque
<AttacherIndex_Impl
>::iterator aIt
= implCheckIndex( nIndex
);
483 std::deque
< AttachedObject_Impl
> aList
= aIt
->aObjList
;
484 for( const auto& rObj
: aList
)
485 detach( l
, nIndex
, rObj
.xTarget
);
487 std::u16string_view aLstType
= ListenerType
;
488 size_t nLastDot
= aLstType
.rfind('.');
489 if (nLastDot
!= std::u16string_view::npos
)
490 aLstType
= aLstType
.substr(nLastDot
+1);
492 auto aEvtIt
= std::find_if(aIt
->aEventList
.begin(), aIt
->aEventList
.end(),
493 [&aLstType
, &EventMethod
, &ToRemoveListenerParam
](const ScriptEventDescriptor
& rEvent
) {
494 return aLstType
== rEvent
.ListenerType
495 && EventMethod
== rEvent
.EventMethod
496 && ToRemoveListenerParam
== rEvent
.AddListenerParam
;
498 if (aEvtIt
!= aIt
->aEventList
.end())
499 aIt
->aEventList
.erase( aEvtIt
);
501 for( const auto& rObj
: aList
)
502 attach( l
, nIndex
, rObj
.xTarget
, rObj
.aHelper
);
506 void SAL_CALL
ImplEventAttacherManager::revokeScriptEvents(sal_Int32 nIndex
)
508 std::unique_lock
l(m_aMutex
);
509 std::deque
<AttacherIndex_Impl
>::iterator aIt
= implCheckIndex( nIndex
);
511 std::deque
< AttachedObject_Impl
> aList
= aIt
->aObjList
;
512 for( const auto& rObj
: aList
)
513 detach( l
, nIndex
, rObj
.xTarget
);
514 aIt
->aEventList
.clear();
515 for( const auto& rObj
: aList
)
516 attach( l
, nIndex
, rObj
.xTarget
, rObj
.aHelper
);
520 void SAL_CALL
ImplEventAttacherManager::insertEntry(sal_Int32 nIndex
)
522 std::unique_lock
l(m_aMutex
);
524 throw IllegalArgumentException("negative index", static_cast<cppu::OWeakObject
*>(this), 1);
526 insertEntry(l
, nIndex
);
529 void ImplEventAttacherManager::insertEntry(std::unique_lock
<std::mutex
>&, sal_Int32 nIndex
)
531 if ( o3tl::make_unsigned(nIndex
) >= aIndex
.size() )
532 aIndex
.resize(nIndex
+1);
534 AttacherIndex_Impl aTmp
;
535 aIndex
.insert( aIndex
.begin() + nIndex
, aTmp
);
538 void SAL_CALL
ImplEventAttacherManager::removeEntry(sal_Int32 nIndex
)
540 std::unique_lock
l(m_aMutex
);
541 std::deque
<AttacherIndex_Impl
>::iterator aIt
= implCheckIndex( nIndex
);
543 std::deque
< AttachedObject_Impl
> aList
= aIt
->aObjList
;
544 for( const auto& rObj
: aList
)
545 detach( l
, nIndex
, rObj
.xTarget
);
551 Sequence
< ScriptEventDescriptor
> SAL_CALL
ImplEventAttacherManager::getScriptEvents(sal_Int32 nIndex
)
553 std::unique_lock
l(m_aMutex
);
554 std::deque
<AttacherIndex_Impl
>::iterator aIt
= implCheckIndex( nIndex
);
555 return comphelper::containerToSequence(aIt
->aEventList
);
559 void SAL_CALL
ImplEventAttacherManager::attach(sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
, const Any
& Helper
)
561 std::unique_lock
l(m_aMutex
);
562 if( nIndex
< 0 || !xObject
.is() )
563 throw IllegalArgumentException("negative index, or null object", static_cast<cppu::OWeakObject
*>(this), -1);
564 attach(l
, nIndex
, xObject
, Helper
);
567 void ImplEventAttacherManager::attach(std::unique_lock
<std::mutex
>& l
, sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
, const Any
& Helper
)
569 if( o3tl::make_unsigned(nIndex
) >= aIndex
.size() )
573 throw IllegalArgumentException();
574 insertEntry( l
, nIndex
);
575 attach( l
, nIndex
, xObject
, Helper
);
579 std::deque
< AttacherIndex_Impl
>::iterator aCurrentPosition
= aIndex
.begin() + nIndex
;
581 AttachedObject_Impl aTmp
;
582 aTmp
.xTarget
= xObject
;
583 aTmp
.aHelper
= Helper
;
584 aCurrentPosition
->aObjList
.push_back( aTmp
);
586 AttachedObject_Impl
& rCurObj
= aCurrentPosition
->aObjList
.back();
587 rCurObj
.aAttachedListenerSeq
= std::vector
< Reference
< XEventListener
> >( aCurrentPosition
->aEventList
.size() );
589 if (aCurrentPosition
->aEventList
.empty())
592 Sequence
<css::script::EventListener
> aEvents(aCurrentPosition
->aEventList
.size());
593 css::script::EventListener
* p
= aEvents
.getArray();
595 for (const auto& rEvent
: aCurrentPosition
->aEventList
)
597 css::script::EventListener aListener
;
598 aListener
.AllListener
=
599 new AttacherAllListener_Impl(this, rEvent
.ScriptType
, rEvent
.ScriptCode
);
600 aListener
.Helper
= rCurObj
.aHelper
;
601 aListener
.ListenerType
= rEvent
.ListenerType
;
602 aListener
.EventMethod
= rEvent
.EventMethod
;
603 aListener
.AddListenerParam
= rEvent
.AddListenerParam
;
609 rCurObj
.aAttachedListenerSeq
= comphelper::sequenceToContainer
<std::vector
<Reference
< XEventListener
>>>(
610 xAttacher
->attachMultipleEventListeners(rCurObj
.xTarget
, aEvents
));
612 catch (const Exception
&)
619 void SAL_CALL
ImplEventAttacherManager::detach(sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
)
621 std::unique_lock
l(m_aMutex
);
623 if( nIndex
< 0 || o3tl::make_unsigned(nIndex
) >= aIndex
.size() || !xObject
.is() )
624 throw IllegalArgumentException("bad index or null object", static_cast<cppu::OWeakObject
*>(this), 1);
625 detach(l
, nIndex
, xObject
);
628 void ImplEventAttacherManager::detach(std::unique_lock
<std::mutex
>&, sal_Int32 nIndex
, const Reference
< XInterface
>& xObject
)
630 std::deque
< AttacherIndex_Impl
>::iterator aCurrentPosition
= aIndex
.begin() + nIndex
;
631 auto aObjIt
= std::find_if(aCurrentPosition
->aObjList
.begin(), aCurrentPosition
->aObjList
.end(),
632 [&xObject
](const AttachedObject_Impl
& rObj
) { return rObj
.xTarget
== xObject
; });
633 if (aObjIt
== aCurrentPosition
->aObjList
.end())
637 for( const auto& rEvt
: aCurrentPosition
->aEventList
)
639 if( aObjIt
->aAttachedListenerSeq
[i
].is() )
643 xAttacher
->removeListener( aObjIt
->xTarget
, rEvt
.ListenerType
,
644 rEvt
.AddListenerParam
, aObjIt
->aAttachedListenerSeq
[i
] );
652 aCurrentPosition
->aObjList
.erase( aObjIt
);
655 void SAL_CALL
ImplEventAttacherManager::addScriptListener(const Reference
< XScriptListener
>& aListener
)
657 std::unique_lock
l(m_aMutex
);
658 aScriptListeners
.addInterface( l
, aListener
);
661 void SAL_CALL
ImplEventAttacherManager::removeScriptListener(const Reference
< XScriptListener
>& aListener
)
663 std::unique_lock
l(m_aMutex
);
664 aScriptListeners
.removeInterface( l
, aListener
);
668 // Methods of XPersistObject
669 OUString SAL_CALL
ImplEventAttacherManager::getServiceName()
671 return "com.sun.star.uno.script.EventAttacherManager";
674 void SAL_CALL
ImplEventAttacherManager::write(const Reference
< XObjectOutputStream
>& OutStream
)
676 std::unique_lock
l(m_aMutex
);
677 // Don't run without XMarkableStream
678 Reference
< XMarkableStream
> xMarkStream( OutStream
, UNO_QUERY
);
679 if( !xMarkStream
.is() )
682 // Write out the version
683 OutStream
->writeShort( 2 );
685 // Remember position for length
686 sal_Int32 nObjLenMark
= xMarkStream
->createMark();
687 OutStream
->writeLong( 0 );
689 OutStream
->writeLong( aIndex
.size() );
691 // Write out sequences
692 for( const auto& rIx
: aIndex
)
694 OutStream
->writeLong( rIx
.aEventList
.size() );
695 for( const auto& rDesc
: rIx
.aEventList
)
697 OutStream
->writeUTF( rDesc
.ListenerType
);
698 OutStream
->writeUTF( rDesc
.EventMethod
);
699 OutStream
->writeUTF( rDesc
.AddListenerParam
);
700 OutStream
->writeUTF( rDesc
.ScriptType
);
701 OutStream
->writeUTF( rDesc
.ScriptCode
);
705 // The length is now known
706 sal_Int32 nObjLen
= xMarkStream
->offsetToMark( nObjLenMark
) -4;
707 xMarkStream
->jumpToMark( nObjLenMark
);
708 OutStream
->writeLong( nObjLen
);
709 xMarkStream
->jumpToFurthest();
710 xMarkStream
->deleteMark( nObjLenMark
);
713 void SAL_CALL
ImplEventAttacherManager::read(const Reference
< XObjectInputStream
>& InStream
)
715 std::unique_lock
l(m_aMutex
);
716 // Don't run without XMarkableStream
717 Reference
< XMarkableStream
> xMarkStream( InStream
, UNO_QUERY
);
718 if( !xMarkStream
.is() )
721 // Read in the version
722 nVersion
= InStream
->readShort();
724 // At first there's the data according to version 1 --
725 // this part needs to be kept in later versions.
726 sal_Int32 nLen
= InStream
->readLong();
728 // Position for comparative purposes
729 sal_Int32 nObjLenMark
= xMarkStream
->createMark();
731 // Number of read sequences
732 sal_Int32 nItemCount
= InStream
->readLong();
734 for( sal_Int32 i
= 0 ; i
< nItemCount
; i
++ )
737 // Read the length of the sequence
738 sal_Int32 nSeqLen
= InStream
->readLong();
740 // Display the sequences and read the descriptions
741 Sequence
< ScriptEventDescriptor
> aSEDSeq( nSeqLen
);
742 ScriptEventDescriptor
* pArray
= aSEDSeq
.getArray();
743 for( sal_Int32 j
= 0 ; j
< nSeqLen
; j
++ )
745 ScriptEventDescriptor
& rDesc
= pArray
[ j
];
746 rDesc
.ListenerType
= InStream
->readUTF();
747 rDesc
.EventMethod
= InStream
->readUTF();
748 rDesc
.AddListenerParam
= InStream
->readUTF();
749 rDesc
.ScriptType
= InStream
->readUTF();
750 rDesc
.ScriptCode
= InStream
->readUTF();
752 registerScriptEvents( l
, i
, aSEDSeq
);
755 // Have we read the specified length?
756 sal_Int32 nRealLen
= xMarkStream
->offsetToMark( nObjLenMark
);
757 if( nRealLen
!= nLen
)
759 // Only if the StreamVersion is > 1 and the date still follows, can
760 // this be true. Otherwise, something is completely gone.
761 if( nRealLen
> nLen
|| nVersion
== 1 )
763 OSL_FAIL( "ImplEventAttacherManager::read(): Fatal Error, wrong object length" );
766 { // TODO: Examine if caching the dates would be useful
767 // But for now, it's easier to skip it.
768 sal_Int32 nSkipCount
= nLen
- nRealLen
;
769 InStream
->skipBytes( nSkipCount
);
772 xMarkStream
->jumpToFurthest();
773 xMarkStream
->deleteMark( nObjLenMark
);
776 } // namespace comphelper
779 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */