Simplify a bit
[LibreOffice.git] / comphelper / source / eventattachermgr / eventattachermgr.cxx
blobaf2c7bbc3edb32031b26f04fecc11ff9998cefd7
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;
63 namespace comphelper
66 namespace {
68 struct AttachedObject_Impl
70 Reference< XInterface > xTarget;
71 std::vector< Reference< XEventListener > > aAttachedListenerSeq;
72 Any aHelper;
75 struct AttacherIndex_Impl
77 std::deque< ScriptEventDescriptor > aEventList;
78 std::deque< AttachedObject_Impl > aObjList;
82 class ImplEventAttacherManager
83 : public WeakImplHelper< XEventAttacherManager, XPersistObject >
85 friend class AttacherAllListener_Impl;
86 std::deque< AttacherIndex_Impl > aIndex;
87 std::mutex m_aMutex;
88 // Container for the ScriptListener
89 OInterfaceContainerHelper4<XScriptListener> aScriptListeners;
90 // Instance of EventAttacher
91 Reference< XEventAttacher2 > xAttacher;
92 Reference< XComponentContext > mxContext;
93 Reference< XIdlReflection > mxCoreReflection;
94 Reference< XTypeConverter > xConverter;
95 sal_Int16 nVersion;
96 public:
97 ImplEventAttacherManager( const Reference< XIntrospection > & rIntrospection,
98 const Reference< XComponentContext >& rContext );
100 // Methods of XEventAttacherManager
101 virtual void SAL_CALL registerScriptEvent(sal_Int32 Index, const ScriptEventDescriptor& ScriptEvent) override;
102 virtual void SAL_CALL registerScriptEvents(sal_Int32 Index, const Sequence< ScriptEventDescriptor >& ScriptEvents) override;
103 virtual void SAL_CALL revokeScriptEvent(sal_Int32 Index, const OUString& ListenerType, const OUString& EventMethod, const OUString& removeListenerParam) override;
104 virtual void SAL_CALL revokeScriptEvents(sal_Int32 Index) override;
105 virtual void SAL_CALL insertEntry(sal_Int32 Index) override;
106 virtual void SAL_CALL removeEntry(sal_Int32 Index) override;
107 virtual Sequence< ScriptEventDescriptor > SAL_CALL getScriptEvents(sal_Int32 Index) override;
108 virtual void SAL_CALL attach(sal_Int32 Index, const Reference< XInterface >& Object, const Any& Helper) override;
109 virtual void SAL_CALL detach(sal_Int32 nIndex, const Reference< XInterface >& xObject) override;
110 virtual void SAL_CALL addScriptListener(const Reference< XScriptListener >& aListener) override;
111 virtual void SAL_CALL removeScriptListener(const Reference< XScriptListener >& Listener) override;
113 // Methods of XPersistObject
114 virtual OUString SAL_CALL getServiceName() override;
115 virtual void SAL_CALL write(const Reference< XObjectOutputStream >& OutStream) override;
116 virtual void SAL_CALL read(const Reference< XObjectInputStream >& InStream) override;
118 private:
119 void registerScriptEvent(std::unique_lock<std::mutex>&, sal_Int32 Index, const ScriptEventDescriptor& ScriptEvent);
120 void registerScriptEvents(std::unique_lock<std::mutex>&, sal_Int32 Index, const Sequence< ScriptEventDescriptor >& ScriptEvents);
121 void attach(std::unique_lock<std::mutex>&, sal_Int32 Index, const Reference< XInterface >& Object, const Any& Helper);
122 void detach(std::unique_lock<std::mutex>&, sal_Int32 nIndex, const Reference< XInterface >& xObject);
123 void insertEntry(std::unique_lock<std::mutex>&, sal_Int32 Index);
125 /// @throws Exception
126 Reference< XIdlReflection > getReflection(std::unique_lock<std::mutex>&);
128 /** checks if <arg>_nIndex</arg> is a valid index, throws an <type>IllegalArgumentException</type> if not
129 @param _nIndex
130 the index to check
131 @return
132 the iterator pointing to the position indicated by the index
134 std::deque<AttacherIndex_Impl>::iterator implCheckIndex( sal_Int32 _nIndex );
138 // Implementation of an EventAttacher-subclass 'AllListeners', which
139 // only passes individual events of the general AllListeners.
140 class AttacherAllListener_Impl : public WeakImplHelper< XAllListener >
142 rtl::Reference<ImplEventAttacherManager> mxManager;
143 OUString const aScriptType;
144 OUString const aScriptCode;
146 /// @throws CannotConvertException
147 void convertToEventReturn( Any & rRet, const Type & rRetType );
148 public:
149 AttacherAllListener_Impl( ImplEventAttacherManager* pManager_, OUString aScriptType_,
150 OUString aScriptCode_ );
152 // Methods of XAllListener
153 virtual void SAL_CALL firing(const AllEventObject& Event) override;
154 virtual Any SAL_CALL approveFiring(const AllEventObject& Event) override;
156 // Methods of XEventListener
157 virtual void SAL_CALL disposing(const EventObject& Source) override;
162 AttacherAllListener_Impl::AttacherAllListener_Impl
164 ImplEventAttacherManager* pManager_,
165 OUString aScriptType_,
166 OUString aScriptCode_
168 : mxManager( pManager_ )
169 , aScriptType(std::move( aScriptType_ ))
170 , aScriptCode(std::move( aScriptCode_ ))
175 // Methods of XAllListener
176 void SAL_CALL AttacherAllListener_Impl::firing(const AllEventObject& Event)
178 ScriptEvent aScriptEvent;
179 aScriptEvent.Source = static_cast<OWeakObject *>(mxManager.get()); // get correct XInterface
180 aScriptEvent.ListenerType = Event.ListenerType;
181 aScriptEvent.MethodName = Event.MethodName;
182 aScriptEvent.Arguments = Event.Arguments;
183 aScriptEvent.Helper = Event.Helper;
184 aScriptEvent.ScriptType = aScriptType;
185 aScriptEvent.ScriptCode = aScriptCode;
187 // Iterate over all listeners and pass events.
188 std::unique_lock l(mxManager->m_aMutex);
189 mxManager->aScriptListeners.notifyEach( l, &XScriptListener::firing, aScriptEvent );
193 // Convert to the standard event return
194 void AttacherAllListener_Impl::convertToEventReturn( Any & rRet, const Type & rRetType )
196 // no return value? Set to the specified values
197 if( rRet.getValueType().getTypeClass() == TypeClass_VOID )
199 switch( rRetType.getTypeClass() )
201 case TypeClass_INTERFACE:
203 rRet <<= Reference< XInterface >();
205 break;
207 case TypeClass_BOOLEAN:
208 rRet <<= true;
209 break;
211 case TypeClass_STRING:
212 rRet <<= OUString();
213 break;
215 case TypeClass_FLOAT: rRet <<= float(0); break;
216 case TypeClass_DOUBLE: rRet <<= 0.0; break;
217 case TypeClass_BYTE: rRet <<= sal_uInt8(0); break;
218 case TypeClass_SHORT: rRet <<= sal_Int16( 0 ); break;
219 case TypeClass_LONG: rRet <<= sal_Int32( 0 ); break;
220 case TypeClass_UNSIGNED_SHORT: rRet <<= sal_uInt16( 0 ); break;
221 case TypeClass_UNSIGNED_LONG: rRet <<= sal_uInt32( 0 ); break;
223 default:
224 OSL_ASSERT(false);
225 break;
228 else if( !rRet.getValueType().equals( rRetType ) )
230 if( !mxManager->xConverter.is() )
231 throw CannotConvertException();
232 rRet = mxManager->xConverter->convertTo( rRet, rRetType );
236 // Methods of XAllListener
237 Any SAL_CALL AttacherAllListener_Impl::approveFiring( const AllEventObject& Event )
239 ScriptEvent aScriptEvent;
240 aScriptEvent.Source = static_cast<OWeakObject *>(mxManager.get()); // get correct XInterface
241 aScriptEvent.ListenerType = Event.ListenerType;
242 aScriptEvent.MethodName = Event.MethodName;
243 aScriptEvent.Arguments = Event.Arguments;
244 aScriptEvent.Helper = Event.Helper;
245 aScriptEvent.ScriptType = aScriptType;
246 aScriptEvent.ScriptCode = aScriptCode;
248 Any aRet;
249 // Iterate over all listeners and pass events.
250 std::unique_lock l(mxManager->m_aMutex);
251 OInterfaceIteratorHelper4 aIt( l, mxManager->aScriptListeners );
252 while( aIt.hasMoreElements() )
254 // cannot hold lock over call to approveFiring, since it might recurse back into us
255 l.unlock();
256 aRet = aIt.next()->approveFiring( aScriptEvent );
257 l.lock();
260 Reference< XIdlClass > xListenerType = mxManager->getReflection(l)->
261 forName( Event.ListenerType.getTypeName() );
262 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
263 if( xMeth.is() )
265 Reference< XIdlClass > xRetType = xMeth->getReturnType();
266 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
267 convertToEventReturn( aRet, aRetType );
270 switch( aRet.getValueType().getTypeClass() )
272 case TypeClass_INTERFACE:
274 // Interface not null, return
275 Reference< XInterface > x;
276 aRet >>= x;
277 if( x.is() )
278 return aRet;
280 break;
282 case TypeClass_BOOLEAN:
283 // FALSE -> Return
284 if( !(*o3tl::forceAccess<bool>(aRet)) )
285 return aRet;
286 break;
288 case TypeClass_STRING:
289 // none empty string -> return
290 if( !o3tl::forceAccess<OUString>(aRet)->isEmpty() )
291 return aRet;
292 break;
294 // none zero number -> return
295 case TypeClass_FLOAT: if( *o3tl::forceAccess<float>(aRet) ) return aRet; break;
296 case TypeClass_DOUBLE: if( *o3tl::forceAccess<double>(aRet) ) return aRet; break;
297 case TypeClass_BYTE: if( *o3tl::forceAccess<sal_Int8>(aRet) ) return aRet; break;
298 case TypeClass_SHORT: if( *o3tl::forceAccess<sal_Int16>(aRet) ) return aRet; break;
299 case TypeClass_LONG: if( *o3tl::forceAccess<sal_Int32>(aRet) ) return aRet; break;
300 case TypeClass_UNSIGNED_SHORT: if( *o3tl::forceAccess<sal_uInt16>(aRet) ) return aRet; break;
301 case TypeClass_UNSIGNED_LONG: if( *o3tl::forceAccess<sal_uInt32>(aRet) ) return aRet; break;
303 default:
304 OSL_ASSERT(false);
305 break;
308 catch (const CannotConvertException&)
310 // silent ignore conversions errors from a script call
311 Reference< XIdlClass > xListenerType = mxManager->getReflection(l)->
312 forName( Event.ListenerType.getTypeName() );
313 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
314 if( xMeth.is() )
316 Reference< XIdlClass > xRetType = xMeth->getReturnType();
317 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
318 aRet.clear();
321 convertToEventReturn( aRet, aRetType );
323 catch (const CannotConvertException& e)
325 css::uno::Any anyEx = cppu::getCaughtException();
326 throw css::lang::WrappedTargetRuntimeException(
327 "wrapped CannotConvertException " + e.Message,
328 css::uno::Reference<css::uno::XInterface>(), anyEx);
333 return aRet;
336 // Methods of XEventListener
337 void SAL_CALL AttacherAllListener_Impl::disposing(const EventObject& )
339 // It is up to the container to release the object
342 // Constructor method for EventAttacherManager
343 Reference< XEventAttacherManager > createEventAttacherManager( const Reference< XComponentContext > & rxContext )
345 Reference< XIntrospection > xIntrospection = theIntrospection::get( rxContext );
346 return new ImplEventAttacherManager( xIntrospection, rxContext );
350 ImplEventAttacherManager::ImplEventAttacherManager( const Reference< XIntrospection > & rIntrospection,
351 const Reference< XComponentContext >& rContext )
352 : mxContext( rContext )
353 , nVersion(0)
355 if ( rContext.is() )
357 Reference< XInterface > xIFace( rContext->getServiceManager()->createInstanceWithContext(
358 "com.sun.star.script.EventAttacher", rContext) );
359 if ( xIFace.is() )
361 xAttacher.set( xIFace, UNO_QUERY );
363 xConverter = Converter::create(rContext);
366 Reference< XInitialization > xInit( xAttacher, UNO_QUERY );
367 if( xInit.is() )
369 xInit->initialize({ Any(rIntrospection) });
373 Reference< XIdlReflection > ImplEventAttacherManager::getReflection(std::unique_lock<std::mutex>&)
375 // Do we already have a service? If not, create one.
376 if( !mxCoreReflection.is() )
378 mxCoreReflection = theCoreReflection::get(mxContext);
380 return mxCoreReflection;
384 std::deque< AttacherIndex_Impl >::iterator ImplEventAttacherManager::implCheckIndex( sal_Int32 _nIndex )
386 if ( (_nIndex < 0) || (o3tl::make_unsigned(_nIndex) >= aIndex.size()) )
387 throw IllegalArgumentException("wrong index", static_cast<cppu::OWeakObject*>(this), 1);
389 std::deque<AttacherIndex_Impl>::iterator aIt = aIndex.begin() + _nIndex;
390 return aIt;
393 // Methods of XEventAttacherManager
394 void SAL_CALL ImplEventAttacherManager::registerScriptEvent
396 sal_Int32 nIndex,
397 const ScriptEventDescriptor& ScriptEvent
400 std::unique_lock l(m_aMutex);
401 registerScriptEvent(l, nIndex, ScriptEvent);
404 void ImplEventAttacherManager::registerScriptEvent
406 std::unique_lock<std::mutex>&,
407 sal_Int32 nIndex,
408 const ScriptEventDescriptor& ScriptEvent
411 // Examine the index and apply the array
412 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
414 ScriptEventDescriptor aEvt = ScriptEvent;
415 sal_Int32 nLastDot = aEvt.ListenerType.lastIndexOf('.');
416 if (nLastDot != -1)
417 aEvt.ListenerType = aEvt.ListenerType.copy(nLastDot+1);
418 aIt->aEventList.push_back( aEvt );
420 // register new Event
421 for( auto& rObj : aIt->aObjList )
423 Reference< XAllListener > xAll =
424 new AttacherAllListener_Impl( this, ScriptEvent.ScriptType, ScriptEvent.ScriptCode );
427 rObj.aAttachedListenerSeq.push_back( xAttacher->attachSingleEventListener( rObj.xTarget, xAll,
428 rObj.aHelper, ScriptEvent.ListenerType,
429 ScriptEvent.AddListenerParam, ScriptEvent.EventMethod ) );
431 catch( Exception& )
438 void SAL_CALL ImplEventAttacherManager::registerScriptEvents
440 sal_Int32 nIndex,
441 const Sequence< ScriptEventDescriptor >& ScriptEvents
444 std::unique_lock l(m_aMutex);
445 registerScriptEvents(l, nIndex, ScriptEvents);
448 void ImplEventAttacherManager::registerScriptEvents
450 std::unique_lock<std::mutex>& l,
451 sal_Int32 nIndex,
452 const Sequence< ScriptEventDescriptor >& ScriptEvents
455 // Examine the index and apply the array
456 std::deque< AttachedObject_Impl > aList = implCheckIndex( nIndex )->aObjList;
457 for( const auto& rObj : aList )
458 detach( l, nIndex, rObj.xTarget );
460 sal_Int32 nLen = ScriptEvents.getLength();
461 for( sal_Int32 i = 0 ; i < nLen ; i++ )
462 registerScriptEvent(l, nIndex, ScriptEvents[i]);
464 for( const auto& rObj : aList )
465 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
469 void SAL_CALL ImplEventAttacherManager::revokeScriptEvent
471 sal_Int32 nIndex,
472 const OUString& ListenerType,
473 const OUString& EventMethod,
474 const OUString& ToRemoveListenerParam
477 std::unique_lock l(m_aMutex);
479 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
481 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
482 for( const auto& rObj : aList )
483 detach( l, nIndex, rObj.xTarget );
485 std::u16string_view aLstType = ListenerType;
486 size_t nLastDot = aLstType.rfind('.');
487 if (nLastDot != std::u16string_view::npos)
488 aLstType = aLstType.substr(nLastDot+1);
490 auto aEvtIt = std::find_if(aIt->aEventList.begin(), aIt->aEventList.end(),
491 [&aLstType, &EventMethod, &ToRemoveListenerParam](const ScriptEventDescriptor& rEvent) {
492 return aLstType == rEvent.ListenerType
493 && EventMethod == rEvent.EventMethod
494 && ToRemoveListenerParam == rEvent.AddListenerParam;
496 if (aEvtIt != aIt->aEventList.end())
497 aIt->aEventList.erase( aEvtIt );
499 for( const auto& rObj : aList )
500 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
504 void SAL_CALL ImplEventAttacherManager::revokeScriptEvents(sal_Int32 nIndex )
506 std::unique_lock l(m_aMutex);
507 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
509 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
510 for( const auto& rObj : aList )
511 detach( l, nIndex, rObj.xTarget );
512 aIt->aEventList.clear();
513 for( const auto& rObj : aList )
514 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
518 void SAL_CALL ImplEventAttacherManager::insertEntry(sal_Int32 nIndex)
520 std::unique_lock l(m_aMutex);
521 if( nIndex < 0 )
522 throw IllegalArgumentException("negative index", static_cast<cppu::OWeakObject*>(this), 1);
524 insertEntry(l, nIndex);
527 void ImplEventAttacherManager::insertEntry(std::unique_lock<std::mutex>&, sal_Int32 nIndex)
529 if ( o3tl::make_unsigned(nIndex) >= aIndex.size() )
530 aIndex.resize(nIndex+1);
532 AttacherIndex_Impl aTmp;
533 aIndex.insert( aIndex.begin() + nIndex, aTmp );
536 void SAL_CALL ImplEventAttacherManager::removeEntry(sal_Int32 nIndex)
538 std::unique_lock l(m_aMutex);
539 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
541 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
542 for( const auto& rObj : aList )
543 detach( l, nIndex, rObj.xTarget );
545 aIndex.erase( aIt );
549 Sequence< ScriptEventDescriptor > SAL_CALL ImplEventAttacherManager::getScriptEvents(sal_Int32 nIndex)
551 std::unique_lock l(m_aMutex);
552 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
553 return comphelper::containerToSequence(aIt->aEventList);
557 void SAL_CALL ImplEventAttacherManager::attach(sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
559 std::unique_lock l(m_aMutex);
560 if( nIndex < 0 || !xObject.is() )
561 throw IllegalArgumentException("negative index, or null object", static_cast<cppu::OWeakObject*>(this), -1);
562 attach(l, nIndex, xObject, Helper);
565 void ImplEventAttacherManager::attach(std::unique_lock<std::mutex>& l, sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
567 if( o3tl::make_unsigned(nIndex) >= aIndex.size() )
569 // read older files
570 if( nVersion != 1 )
571 throw IllegalArgumentException();
572 insertEntry( l, nIndex );
573 attach( l, nIndex, xObject, Helper );
574 return;
577 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
579 AttachedObject_Impl aTmp;
580 aTmp.xTarget = xObject;
581 aTmp.aHelper = Helper;
582 aCurrentPosition->aObjList.push_back( aTmp );
584 AttachedObject_Impl & rCurObj = aCurrentPosition->aObjList.back();
585 rCurObj.aAttachedListenerSeq = std::vector< Reference< XEventListener > >( aCurrentPosition->aEventList.size() );
587 if (aCurrentPosition->aEventList.empty())
588 return;
590 Sequence<css::script::EventListener> aEvents(aCurrentPosition->aEventList.size());
591 css::script::EventListener* p = aEvents.getArray();
592 size_t i = 0;
593 for (const auto& rEvent : aCurrentPosition->aEventList)
595 css::script::EventListener aListener;
596 aListener.AllListener =
597 new AttacherAllListener_Impl(this, rEvent.ScriptType, rEvent.ScriptCode);
598 aListener.Helper = rCurObj.aHelper;
599 aListener.ListenerType = rEvent.ListenerType;
600 aListener.EventMethod = rEvent.EventMethod;
601 aListener.AddListenerParam = rEvent.AddListenerParam;
602 p[i++] = aListener;
607 rCurObj.aAttachedListenerSeq = comphelper::sequenceToContainer<std::vector<Reference< XEventListener >>>(
608 xAttacher->attachMultipleEventListeners(rCurObj.xTarget, aEvents));
610 catch (const Exception&)
612 // Fail gracefully.
617 void SAL_CALL ImplEventAttacherManager::detach(sal_Int32 nIndex, const Reference< XInterface >& xObject)
619 std::unique_lock l(m_aMutex);
620 //return;
621 if( nIndex < 0 || o3tl::make_unsigned(nIndex) >= aIndex.size() || !xObject.is() )
622 throw IllegalArgumentException("bad index or null object", static_cast<cppu::OWeakObject*>(this), 1);
623 detach(l, nIndex, xObject);
626 void ImplEventAttacherManager::detach(std::unique_lock<std::mutex>&, sal_Int32 nIndex, const Reference< XInterface >& xObject)
628 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
629 auto aObjIt = std::find_if(aCurrentPosition->aObjList.begin(), aCurrentPosition->aObjList.end(),
630 [&xObject](const AttachedObject_Impl& rObj) { return rObj.xTarget == xObject; });
631 if (aObjIt == aCurrentPosition->aObjList.end())
632 return;
634 sal_Int32 i = 0;
635 for( const auto& rEvt : aCurrentPosition->aEventList )
637 if( aObjIt->aAttachedListenerSeq[i].is() )
641 xAttacher->removeListener( aObjIt->xTarget, rEvt.ListenerType,
642 rEvt.AddListenerParam, aObjIt->aAttachedListenerSeq[i] );
644 catch( Exception& )
648 ++i;
650 aCurrentPosition->aObjList.erase( aObjIt );
653 void SAL_CALL ImplEventAttacherManager::addScriptListener(const Reference< XScriptListener >& aListener)
655 std::unique_lock l(m_aMutex);
656 aScriptListeners.addInterface( l, aListener );
659 void SAL_CALL ImplEventAttacherManager::removeScriptListener(const Reference< XScriptListener >& aListener)
661 std::unique_lock l(m_aMutex);
662 aScriptListeners.removeInterface( l, aListener );
666 // Methods of XPersistObject
667 OUString SAL_CALL ImplEventAttacherManager::getServiceName()
669 return "com.sun.star.uno.script.EventAttacherManager";
672 void SAL_CALL ImplEventAttacherManager::write(const Reference< XObjectOutputStream >& OutStream)
674 std::unique_lock l(m_aMutex);
675 // Don't run without XMarkableStream
676 Reference< XMarkableStream > xMarkStream( OutStream, UNO_QUERY );
677 if( !xMarkStream.is() )
678 return;
680 // Write out the version
681 OutStream->writeShort( 2 );
683 // Remember position for length
684 sal_Int32 nObjLenMark = xMarkStream->createMark();
685 OutStream->writeLong( 0 );
687 OutStream->writeLong( aIndex.size() );
689 // Write out sequences
690 for( const auto& rIx : aIndex )
692 OutStream->writeLong( rIx.aEventList.size() );
693 for( const auto& rDesc : rIx.aEventList )
695 OutStream->writeUTF( rDesc.ListenerType );
696 OutStream->writeUTF( rDesc.EventMethod );
697 OutStream->writeUTF( rDesc.AddListenerParam );
698 OutStream->writeUTF( rDesc.ScriptType );
699 OutStream->writeUTF( rDesc.ScriptCode );
703 // The length is now known
704 sal_Int32 nObjLen = xMarkStream->offsetToMark( nObjLenMark ) -4;
705 xMarkStream->jumpToMark( nObjLenMark );
706 OutStream->writeLong( nObjLen );
707 xMarkStream->jumpToFurthest();
708 xMarkStream->deleteMark( nObjLenMark );
711 void SAL_CALL ImplEventAttacherManager::read(const Reference< XObjectInputStream >& InStream)
713 std::unique_lock l(m_aMutex);
714 // Don't run without XMarkableStream
715 Reference< XMarkableStream > xMarkStream( InStream, UNO_QUERY );
716 if( !xMarkStream.is() )
717 return;
719 // Read in the version
720 nVersion = InStream->readShort();
722 // At first there's the data according to version 1 --
723 // this part needs to be kept in later versions.
724 sal_Int32 nLen = InStream->readLong();
726 // Position for comparative purposes
727 sal_Int32 nObjLenMark = xMarkStream->createMark();
729 // Number of read sequences
730 sal_Int32 nItemCount = InStream->readLong();
732 for( sal_Int32 i = 0 ; i < nItemCount ; i++ )
734 insertEntry( l, i );
735 // Read the length of the sequence
736 sal_Int32 nSeqLen = InStream->readLong();
738 // Display the sequences and read the descriptions
739 Sequence< ScriptEventDescriptor > aSEDSeq( nSeqLen );
740 ScriptEventDescriptor* pArray = aSEDSeq.getArray();
741 for( sal_Int32 j = 0 ; j < nSeqLen ; j++ )
743 ScriptEventDescriptor& rDesc = pArray[ j ];
744 rDesc.ListenerType = InStream->readUTF();
745 rDesc.EventMethod = InStream->readUTF();
746 rDesc.AddListenerParam = InStream->readUTF();
747 rDesc.ScriptType = InStream->readUTF();
748 rDesc.ScriptCode = InStream->readUTF();
750 registerScriptEvents( l, i, aSEDSeq );
753 // Have we read the specified length?
754 sal_Int32 nRealLen = xMarkStream->offsetToMark( nObjLenMark );
755 if( nRealLen != nLen )
757 // Only if the StreamVersion is > 1 and the date still follows, can
758 // this be true. Otherwise, something is completely gone.
759 if( nRealLen > nLen || nVersion == 1 )
761 OSL_FAIL( "ImplEventAttacherManager::read(): Fatal Error, wrong object length" );
763 else
764 { // TODO: Examine if caching the dates would be useful
765 // But for now, it's easier to skip it.
766 sal_Int32 nSkipCount = nLen - nRealLen;
767 InStream->skipBytes( nSkipCount );
770 xMarkStream->jumpToFurthest();
771 xMarkStream->deleteMark( nObjLenMark );
774 } // namespace comphelper
777 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */