Use #REF! error in INDEX()
[LibreOffice.git] / comphelper / source / eventattachermgr / eventattachermgr.cxx
blob260121ba15178c9ca15f7d327955a15ceeafaf4c
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 <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>
49 #include <deque>
50 #include <mutex>
51 #include <algorithm>
52 #include <utility>
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;
60 using namespace cppu;
61 using namespace osl;
64 namespace comphelper
67 namespace {
69 struct AttachedObject_Impl
71 Reference< XInterface > xTarget;
72 std::vector< Reference< XEventListener > > aAttachedListenerSeq;
73 Any aHelper;
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;
88 std::mutex m_aMutex;
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;
96 sal_Int16 nVersion;
97 public:
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;
119 private:
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
130 @param _nIndex
131 the index to check
132 @return
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 );
149 public:
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 >();
206 break;
208 case TypeClass_BOOLEAN:
209 rRet <<= true;
210 break;
212 case TypeClass_STRING:
213 rRet <<= OUString();
214 break;
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;
224 default:
225 OSL_ASSERT(false);
226 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;
249 Any aRet;
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 aRet = aIt.next()->approveFiring( aScriptEvent );
258 Reference< XIdlClass > xListenerType = mxManager->getReflection(l)->
259 forName( Event.ListenerType.getTypeName() );
260 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
261 if( xMeth.is() )
263 Reference< XIdlClass > xRetType = xMeth->getReturnType();
264 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
265 convertToEventReturn( aRet, aRetType );
268 switch( aRet.getValueType().getTypeClass() )
270 case TypeClass_INTERFACE:
272 // Interface not null, return
273 Reference< XInterface > x;
274 aRet >>= x;
275 if( x.is() )
276 return aRet;
278 break;
280 case TypeClass_BOOLEAN:
281 // FALSE -> Return
282 if( !(*o3tl::forceAccess<bool>(aRet)) )
283 return aRet;
284 break;
286 case TypeClass_STRING:
287 // none empty string -> return
288 if( !o3tl::forceAccess<OUString>(aRet)->isEmpty() )
289 return aRet;
290 break;
292 // none zero number -> return
293 case TypeClass_FLOAT: if( *o3tl::forceAccess<float>(aRet) ) return aRet; break;
294 case TypeClass_DOUBLE: if( *o3tl::forceAccess<double>(aRet) ) return aRet; break;
295 case TypeClass_BYTE: if( *o3tl::forceAccess<sal_Int8>(aRet) ) return aRet; break;
296 case TypeClass_SHORT: if( *o3tl::forceAccess<sal_Int16>(aRet) ) return aRet; break;
297 case TypeClass_LONG: if( *o3tl::forceAccess<sal_Int32>(aRet) ) return aRet; break;
298 case TypeClass_UNSIGNED_SHORT: if( *o3tl::forceAccess<sal_uInt16>(aRet) ) return aRet; break;
299 case TypeClass_UNSIGNED_LONG: if( *o3tl::forceAccess<sal_uInt32>(aRet) ) return aRet; break;
301 default:
302 OSL_ASSERT(false);
303 break;
306 catch (const CannotConvertException&)
308 // silent ignore conversions errors from a script call
309 Reference< XIdlClass > xListenerType = mxManager->getReflection(l)->
310 forName( Event.ListenerType.getTypeName() );
311 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
312 if( xMeth.is() )
314 Reference< XIdlClass > xRetType = xMeth->getReturnType();
315 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
316 aRet.clear();
319 convertToEventReturn( aRet, aRetType );
321 catch (const CannotConvertException& e)
323 css::uno::Any anyEx = cppu::getCaughtException();
324 throw css::lang::WrappedTargetRuntimeException(
325 "wrapped CannotConvertException " + e.Message,
326 css::uno::Reference<css::uno::XInterface>(), anyEx);
331 return aRet;
334 // Methods of XEventListener
335 void SAL_CALL AttacherAllListener_Impl::disposing(const EventObject& )
337 // It is up to the container to release the object
340 // Constructor method for EventAttacherManager
341 Reference< XEventAttacherManager > createEventAttacherManager( const Reference< XComponentContext > & rxContext )
343 Reference< XIntrospection > xIntrospection = theIntrospection::get( rxContext );
344 return new ImplEventAttacherManager( xIntrospection, rxContext );
348 ImplEventAttacherManager::ImplEventAttacherManager( const Reference< XIntrospection > & rIntrospection,
349 const Reference< XComponentContext >& rContext )
350 : mxContext( rContext )
351 , nVersion(0)
353 if ( rContext.is() )
355 Reference< XInterface > xIFace( rContext->getServiceManager()->createInstanceWithContext(
356 "com.sun.star.script.EventAttacher", rContext) );
357 if ( xIFace.is() )
359 xAttacher.set( xIFace, UNO_QUERY );
361 xConverter = Converter::create(rContext);
364 Reference< XInitialization > xInit( xAttacher, UNO_QUERY );
365 if( xInit.is() )
367 xInit->initialize({ Any(rIntrospection) });
371 Reference< XIdlReflection > ImplEventAttacherManager::getReflection(std::unique_lock<std::mutex>&)
373 // Do we already have a service? If not, create one.
374 if( !mxCoreReflection.is() )
376 mxCoreReflection = theCoreReflection::get(mxContext);
378 return mxCoreReflection;
382 std::deque< AttacherIndex_Impl >::iterator ImplEventAttacherManager::implCheckIndex( sal_Int32 _nIndex )
384 if ( (_nIndex < 0) || (o3tl::make_unsigned(_nIndex) >= aIndex.size()) )
385 throw IllegalArgumentException("wrong index", static_cast<cppu::OWeakObject*>(this), 1);
387 std::deque<AttacherIndex_Impl>::iterator aIt = aIndex.begin() + _nIndex;
388 return aIt;
391 // Methods of XEventAttacherManager
392 void SAL_CALL ImplEventAttacherManager::registerScriptEvent
394 sal_Int32 nIndex,
395 const ScriptEventDescriptor& ScriptEvent
398 std::unique_lock l(m_aMutex);
399 registerScriptEvent(l, nIndex, ScriptEvent);
402 void ImplEventAttacherManager::registerScriptEvent
404 std::unique_lock<std::mutex>&,
405 sal_Int32 nIndex,
406 const ScriptEventDescriptor& ScriptEvent
409 // Examine the index and apply the array
410 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
412 ScriptEventDescriptor aEvt = ScriptEvent;
413 sal_Int32 nLastDot = aEvt.ListenerType.lastIndexOf('.');
414 if (nLastDot != -1)
415 aEvt.ListenerType = aEvt.ListenerType.copy(nLastDot+1);
416 aIt->aEventList.push_back( aEvt );
418 // register new Event
419 for( auto& rObj : aIt->aObjList )
421 Reference< XAllListener > xAll =
422 new AttacherAllListener_Impl( this, ScriptEvent.ScriptType, ScriptEvent.ScriptCode );
425 rObj.aAttachedListenerSeq.push_back( xAttacher->attachSingleEventListener( rObj.xTarget, xAll,
426 rObj.aHelper, ScriptEvent.ListenerType,
427 ScriptEvent.AddListenerParam, ScriptEvent.EventMethod ) );
429 catch( Exception& )
436 void SAL_CALL ImplEventAttacherManager::registerScriptEvents
438 sal_Int32 nIndex,
439 const Sequence< ScriptEventDescriptor >& ScriptEvents
442 std::unique_lock l(m_aMutex);
443 registerScriptEvents(l, nIndex, ScriptEvents);
446 void ImplEventAttacherManager::registerScriptEvents
448 std::unique_lock<std::mutex>& l,
449 sal_Int32 nIndex,
450 const Sequence< ScriptEventDescriptor >& ScriptEvents
453 // Examine the index and apply the array
454 std::deque< AttachedObject_Impl > aList = implCheckIndex( nIndex )->aObjList;
455 for( const auto& rObj : aList )
456 detach( l, nIndex, rObj.xTarget );
458 const ScriptEventDescriptor* pArray = ScriptEvents.getConstArray();
459 sal_Int32 nLen = ScriptEvents.getLength();
460 for( sal_Int32 i = 0 ; i < nLen ; i++ )
461 registerScriptEvent( l, nIndex, pArray[ i ] );
463 for( const auto& rObj : aList )
464 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
468 void SAL_CALL ImplEventAttacherManager::revokeScriptEvent
470 sal_Int32 nIndex,
471 const OUString& ListenerType,
472 const OUString& EventMethod,
473 const OUString& ToRemoveListenerParam
476 std::unique_lock l(m_aMutex);
478 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
480 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
481 for( const auto& rObj : aList )
482 detach( l, nIndex, rObj.xTarget );
484 std::u16string_view aLstType = ListenerType;
485 size_t nLastDot = aLstType.rfind('.');
486 if (nLastDot != std::u16string_view::npos)
487 aLstType = aLstType.substr(nLastDot+1);
489 auto aEvtIt = std::find_if(aIt->aEventList.begin(), aIt->aEventList.end(),
490 [&aLstType, &EventMethod, &ToRemoveListenerParam](const ScriptEventDescriptor& rEvent) {
491 return aLstType == rEvent.ListenerType
492 && EventMethod == rEvent.EventMethod
493 && ToRemoveListenerParam == rEvent.AddListenerParam;
495 if (aEvtIt != aIt->aEventList.end())
496 aIt->aEventList.erase( aEvtIt );
498 for( const auto& rObj : aList )
499 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
503 void SAL_CALL ImplEventAttacherManager::revokeScriptEvents(sal_Int32 nIndex )
505 std::unique_lock l(m_aMutex);
506 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
508 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
509 for( const auto& rObj : aList )
510 detach( l, nIndex, rObj.xTarget );
511 aIt->aEventList.clear();
512 for( const auto& rObj : aList )
513 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
517 void SAL_CALL ImplEventAttacherManager::insertEntry(sal_Int32 nIndex)
519 std::unique_lock l(m_aMutex);
520 if( nIndex < 0 )
521 throw IllegalArgumentException("negative index", static_cast<cppu::OWeakObject*>(this), 1);
523 insertEntry(l, nIndex);
526 void ImplEventAttacherManager::insertEntry(std::unique_lock<std::mutex>&, sal_Int32 nIndex)
528 if ( o3tl::make_unsigned(nIndex) >= aIndex.size() )
529 aIndex.resize(nIndex+1);
531 AttacherIndex_Impl aTmp;
532 aIndex.insert( aIndex.begin() + nIndex, aTmp );
535 void SAL_CALL ImplEventAttacherManager::removeEntry(sal_Int32 nIndex)
537 std::unique_lock l(m_aMutex);
538 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
540 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
541 for( const auto& rObj : aList )
542 detach( l, nIndex, rObj.xTarget );
544 aIndex.erase( aIt );
548 Sequence< ScriptEventDescriptor > SAL_CALL ImplEventAttacherManager::getScriptEvents(sal_Int32 nIndex)
550 std::unique_lock l(m_aMutex);
551 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
552 return comphelper::containerToSequence(aIt->aEventList);
556 void SAL_CALL ImplEventAttacherManager::attach(sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
558 std::unique_lock l(m_aMutex);
559 if( nIndex < 0 || !xObject.is() )
560 throw IllegalArgumentException("negative index, or null object", static_cast<cppu::OWeakObject*>(this), -1);
561 attach(l, nIndex, xObject, Helper);
564 void ImplEventAttacherManager::attach(std::unique_lock<std::mutex>& l, sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
566 if( o3tl::make_unsigned(nIndex) >= aIndex.size() )
568 // read older files
569 if( nVersion != 1 )
570 throw IllegalArgumentException();
571 insertEntry( l, nIndex );
572 attach( l, nIndex, xObject, Helper );
573 return;
576 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
578 AttachedObject_Impl aTmp;
579 aTmp.xTarget = xObject;
580 aTmp.aHelper = Helper;
581 aCurrentPosition->aObjList.push_back( aTmp );
583 AttachedObject_Impl & rCurObj = aCurrentPosition->aObjList.back();
584 rCurObj.aAttachedListenerSeq = std::vector< Reference< XEventListener > >( aCurrentPosition->aEventList.size() );
586 if (aCurrentPosition->aEventList.empty())
587 return;
589 Sequence<css::script::EventListener> aEvents(aCurrentPosition->aEventList.size());
590 css::script::EventListener* p = aEvents.getArray();
591 size_t i = 0;
592 for (const auto& rEvent : aCurrentPosition->aEventList)
594 css::script::EventListener aListener;
595 aListener.AllListener =
596 new AttacherAllListener_Impl(this, rEvent.ScriptType, rEvent.ScriptCode);
597 aListener.Helper = rCurObj.aHelper;
598 aListener.ListenerType = rEvent.ListenerType;
599 aListener.EventMethod = rEvent.EventMethod;
600 aListener.AddListenerParam = rEvent.AddListenerParam;
601 p[i++] = aListener;
606 rCurObj.aAttachedListenerSeq = comphelper::sequenceToContainer<std::vector<Reference< XEventListener >>>(
607 xAttacher->attachMultipleEventListeners(rCurObj.xTarget, aEvents));
609 catch (const Exception&)
611 // Fail gracefully.
616 void SAL_CALL ImplEventAttacherManager::detach(sal_Int32 nIndex, const Reference< XInterface >& xObject)
618 std::unique_lock l(m_aMutex);
619 //return;
620 if( nIndex < 0 || o3tl::make_unsigned(nIndex) >= aIndex.size() || !xObject.is() )
621 throw IllegalArgumentException("bad index or null object", static_cast<cppu::OWeakObject*>(this), 1);
622 detach(l, nIndex, xObject);
625 void ImplEventAttacherManager::detach(std::unique_lock<std::mutex>&, sal_Int32 nIndex, const Reference< XInterface >& xObject)
627 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
628 auto aObjIt = std::find_if(aCurrentPosition->aObjList.begin(), aCurrentPosition->aObjList.end(),
629 [&xObject](const AttachedObject_Impl& rObj) { return rObj.xTarget == xObject; });
630 if (aObjIt == aCurrentPosition->aObjList.end())
631 return;
633 sal_Int32 i = 0;
634 for( const auto& rEvt : aCurrentPosition->aEventList )
636 if( aObjIt->aAttachedListenerSeq[i].is() )
640 xAttacher->removeListener( aObjIt->xTarget, rEvt.ListenerType,
641 rEvt.AddListenerParam, aObjIt->aAttachedListenerSeq[i] );
643 catch( Exception& )
647 ++i;
649 aCurrentPosition->aObjList.erase( aObjIt );
652 void SAL_CALL ImplEventAttacherManager::addScriptListener(const Reference< XScriptListener >& aListener)
654 std::unique_lock l(m_aMutex);
655 aScriptListeners.addInterface( l, aListener );
658 void SAL_CALL ImplEventAttacherManager::removeScriptListener(const Reference< XScriptListener >& aListener)
660 std::unique_lock l(m_aMutex);
661 aScriptListeners.removeInterface( l, aListener );
665 // Methods of XPersistObject
666 OUString SAL_CALL ImplEventAttacherManager::getServiceName()
668 return "com.sun.star.uno.script.EventAttacherManager";
671 void SAL_CALL ImplEventAttacherManager::write(const Reference< XObjectOutputStream >& OutStream)
673 std::unique_lock l(m_aMutex);
674 // Don't run without XMarkableStream
675 Reference< XMarkableStream > xMarkStream( OutStream, UNO_QUERY );
676 if( !xMarkStream.is() )
677 return;
679 // Write out the version
680 OutStream->writeShort( 2 );
682 // Remember position for length
683 sal_Int32 nObjLenMark = xMarkStream->createMark();
684 OutStream->writeLong( 0 );
686 OutStream->writeLong( aIndex.size() );
688 // Write out sequences
689 for( const auto& rIx : aIndex )
691 OutStream->writeLong( rIx.aEventList.size() );
692 for( const auto& rDesc : rIx.aEventList )
694 OutStream->writeUTF( rDesc.ListenerType );
695 OutStream->writeUTF( rDesc.EventMethod );
696 OutStream->writeUTF( rDesc.AddListenerParam );
697 OutStream->writeUTF( rDesc.ScriptType );
698 OutStream->writeUTF( rDesc.ScriptCode );
702 // The length is now known
703 sal_Int32 nObjLen = xMarkStream->offsetToMark( nObjLenMark ) -4;
704 xMarkStream->jumpToMark( nObjLenMark );
705 OutStream->writeLong( nObjLen );
706 xMarkStream->jumpToFurthest();
707 xMarkStream->deleteMark( nObjLenMark );
710 void SAL_CALL ImplEventAttacherManager::read(const Reference< XObjectInputStream >& InStream)
712 std::unique_lock l(m_aMutex);
713 // Don't run without XMarkableStream
714 Reference< XMarkableStream > xMarkStream( InStream, UNO_QUERY );
715 if( !xMarkStream.is() )
716 return;
718 // Read in the version
719 nVersion = InStream->readShort();
721 // At first there's the data according to version 1 --
722 // this part needs to be kept in later versions.
723 sal_Int32 nLen = InStream->readLong();
725 // Position for comparative purposes
726 sal_Int32 nObjLenMark = xMarkStream->createMark();
728 // Number of read sequences
729 sal_Int32 nItemCount = InStream->readLong();
731 for( sal_Int32 i = 0 ; i < nItemCount ; i++ )
733 insertEntry( l, i );
734 // Read the length of the sequence
735 sal_Int32 nSeqLen = InStream->readLong();
737 // Display the sequences and read the descriptions
738 Sequence< ScriptEventDescriptor > aSEDSeq( nSeqLen );
739 ScriptEventDescriptor* pArray = aSEDSeq.getArray();
740 for( sal_Int32 j = 0 ; j < nSeqLen ; j++ )
742 ScriptEventDescriptor& rDesc = pArray[ j ];
743 rDesc.ListenerType = InStream->readUTF();
744 rDesc.EventMethod = InStream->readUTF();
745 rDesc.AddListenerParam = InStream->readUTF();
746 rDesc.ScriptType = InStream->readUTF();
747 rDesc.ScriptCode = InStream->readUTF();
749 registerScriptEvents( l, i, aSEDSeq );
752 // Have we read the specified length?
753 sal_Int32 nRealLen = xMarkStream->offsetToMark( nObjLenMark );
754 if( nRealLen != nLen )
756 // Only if the StreamVersion is > 1 and the date still follows, can
757 // this be true. Otherwise, something is completely gone.
758 if( nRealLen > nLen || nVersion == 1 )
760 OSL_FAIL( "ImplEventAttacherManager::read(): Fatal Error, wrong object length" );
762 else
763 { // TODO: Examine if caching the dates would be useful
764 // But for now, it's easier to skip it.
765 sal_Int32 nSkipCount = nLen - nRealLen;
766 InStream->skipBytes( nSkipCount );
769 xMarkStream->jumpToFurthest();
770 xMarkStream->deleteMark( nObjLenMark );
773 } // namespace comphelper
776 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */