LibreOffice Module comphelper (master) 1
eventattachermgr.cxx
Go to the documentation of this file.
1/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2/*
3 * This file is part of the LibreOffice project.
4 *
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/.
8 *
9 * This file incorporates work covered by the following license notice:
10 *
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 .
18 */
19
20#include <sal/config.h>
21
22#include <o3tl/any.hxx>
23#include <o3tl/safeint.hxx>
24#include <osl/diagnose.h>
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>
47#include <rtl/ref.hxx>
48
49#include <deque>
50#include <mutex>
51#include <algorithm>
52#include <utility>
53
54using namespace com::sun::star::uno;
55using namespace com::sun::star::io;
56using namespace com::sun::star::lang;
57using namespace com::sun::star::beans;
58using namespace com::sun::star::script;
59using namespace com::sun::star::reflection;
60using namespace cppu;
61using namespace osl;
62
63
64namespace comphelper
65{
66
67namespace {
68
69struct AttachedObject_Impl
70{
72 std::vector< Reference< XEventListener > > aAttachedListenerSeq;
74};
75
76struct AttacherIndex_Impl
77{
78 std::deque< ScriptEventDescriptor > aEventList;
79 std::deque< AttachedObject_Impl > aObjList;
80};
81
82
83class ImplEventAttacherManager
84 : public WeakImplHelper< XEventAttacherManager, XPersistObject >
85{
86 friend class AttacherAllListener_Impl;
87 std::deque< AttacherIndex_Impl > aIndex;
89 // Container for the ScriptListener
90 OInterfaceContainerHelper4<XScriptListener> aScriptListeners;
91 // Instance of EventAttacher
96 sal_Int16 nVersion;
97public:
98 ImplEventAttacherManager( const Reference< XIntrospection > & rIntrospection,
99 const Reference< XComponentContext >& rContext );
100
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;
113
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;
118
119private:
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);
125
127 Reference< XIdlReflection > getReflection(std::unique_lock<std::mutex>&);
128
135 std::deque<AttacherIndex_Impl>::iterator implCheckIndex( sal_Int32 _nIndex );
136};
137
138
139// Implementation of an EventAttacher-subclass 'AllListeners', which
140// only passes individual events of the general AllListeners.
141class AttacherAllListener_Impl : public WeakImplHelper< XAllListener >
142{
144 OUString const aScriptType;
145 OUString const aScriptCode;
146
148 void convertToEventReturn( Any & rRet, const Type & rRetType );
149public:
150 AttacherAllListener_Impl( ImplEventAttacherManager* pManager_, OUString aScriptType_,
151 OUString aScriptCode_ );
152
153 // Methods of XAllListener
154 virtual void SAL_CALL firing(const AllEventObject& Event) override;
155 virtual Any SAL_CALL approveFiring(const AllEventObject& Event) override;
156
157 // Methods of XEventListener
158 virtual void SAL_CALL disposing(const EventObject& Source) override;
159};
160
161}
162
163AttacherAllListener_Impl::AttacherAllListener_Impl
164(
165 ImplEventAttacherManager* pManager_,
166 OUString aScriptType_,
167 OUString aScriptCode_
168)
169 : mxManager( pManager_ )
170 , aScriptType(std::move( aScriptType_ ))
171 , aScriptCode(std::move( aScriptCode_ ))
172{
173}
174
175
176// Methods of XAllListener
177void SAL_CALL AttacherAllListener_Impl::firing(const AllEventObject& Event)
178{
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;
187
188 // Iterate over all listeners and pass events.
189 std::unique_lock l(mxManager->m_aMutex);
190 mxManager->aScriptListeners.notifyEach( l, &XScriptListener::firing, aScriptEvent );
191}
192
193
194// Convert to the standard event return
195void AttacherAllListener_Impl::convertToEventReturn( Any & rRet, const Type & rRetType )
196{
197 // no return value? Set to the specified values
198 if( rRet.getValueType().getTypeClass() == TypeClass_VOID )
199 {
200 switch( rRetType.getTypeClass() )
201 {
202 case TypeClass_INTERFACE:
203 {
204 rRet <<= Reference< XInterface >();
205 }
206 break;
207
208 case TypeClass_BOOLEAN:
209 rRet <<= true;
210 break;
211
212 case TypeClass_STRING:
213 rRet <<= OUString();
214 break;
215
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;
223
224 default:
225 OSL_ASSERT(false);
226 break;
227 }
228 }
229 else if( !rRet.getValueType().equals( rRetType ) )
230 {
231 if( !mxManager->xConverter.is() )
232 throw CannotConvertException();
233 rRet = mxManager->xConverter->convertTo( rRet, rRetType );
234 }
235}
236
237// Methods of XAllListener
238Any SAL_CALL AttacherAllListener_Impl::approveFiring( const AllEventObject& Event )
239{
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;
248
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() )
254 {
255 aRet = aIt.next()->approveFiring( aScriptEvent );
256 try
257 {
258 Reference< XIdlClass > xListenerType = mxManager->getReflection(l)->
259 forName( Event.ListenerType.getTypeName() );
260 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
261 if( xMeth.is() )
262 {
263 Reference< XIdlClass > xRetType = xMeth->getReturnType();
264 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
265 convertToEventReturn( aRet, aRetType );
266 }
267
268 switch( aRet.getValueType().getTypeClass() )
269 {
270 case TypeClass_INTERFACE:
271 {
272 // Interface not null, return
274 aRet >>= x;
275 if( x.is() )
276 return aRet;
277 }
278 break;
279
280 case TypeClass_BOOLEAN:
281 // FALSE -> Return
282 if( !(*o3tl::forceAccess<bool>(aRet)) )
283 return aRet;
284 break;
285
286 case TypeClass_STRING:
287 // none empty string -> return
288 if( !o3tl::forceAccess<OUString>(aRet)->isEmpty() )
289 return aRet;
290 break;
291
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;
300
301 default:
302 OSL_ASSERT(false);
303 break;
304 }
305 }
306 catch (const CannotConvertException&)
307 {
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() )
313 {
314 Reference< XIdlClass > xRetType = xMeth->getReturnType();
315 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
316 aRet.clear();
317 try
318 {
319 convertToEventReturn( aRet, aRetType );
320 }
321 catch (const CannotConvertException& e)
322 {
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);
327 }
328 }
329 }
330 }
331 return aRet;
332}
333
334// Methods of XEventListener
335void SAL_CALL AttacherAllListener_Impl::disposing(const EventObject& )
336{
337 // It is up to the container to release the object
338}
339
340// Constructor method for EventAttacherManager
342{
343 Reference< XIntrospection > xIntrospection = theIntrospection::get( rxContext );
344 return new ImplEventAttacherManager( xIntrospection, rxContext );
345}
346
347
348ImplEventAttacherManager::ImplEventAttacherManager( const Reference< XIntrospection > & rIntrospection,
349 const Reference< XComponentContext >& rContext )
350 : mxContext( rContext )
351 , nVersion(0)
352{
353 if ( rContext.is() )
354 {
355 Reference< XInterface > xIFace( rContext->getServiceManager()->createInstanceWithContext(
356 "com.sun.star.script.EventAttacher", rContext) );
357 if ( xIFace.is() )
358 {
359 xAttacher.set( xIFace, UNO_QUERY );
360 }
361 xConverter = Converter::create(rContext);
362 }
363
364 Reference< XInitialization > xInit( xAttacher, UNO_QUERY );
365 if( xInit.is() )
366 {
367 xInit->initialize({ Any(rIntrospection) });
368 }
369}
370
371Reference< XIdlReflection > ImplEventAttacherManager::getReflection(std::unique_lock<std::mutex>&)
372{
373 // Do we already have a service? If not, create one.
374 if( !mxCoreReflection.is() )
375 {
376 mxCoreReflection = theCoreReflection::get(mxContext);
377 }
378 return mxCoreReflection;
379}
380
381
382std::deque< AttacherIndex_Impl >::iterator ImplEventAttacherManager::implCheckIndex( sal_Int32 _nIndex )
383{
384 if ( (_nIndex < 0) || (o3tl::make_unsigned(_nIndex) >= aIndex.size()) )
385 throw IllegalArgumentException("wrong index", static_cast<cppu::OWeakObject*>(this), 1);
386
387 std::deque<AttacherIndex_Impl>::iterator aIt = aIndex.begin() + _nIndex;
388 return aIt;
389}
390
391// Methods of XEventAttacherManager
392void SAL_CALL ImplEventAttacherManager::registerScriptEvent
393(
394 sal_Int32 nIndex,
395 const ScriptEventDescriptor& ScriptEvent
396)
397{
398 std::unique_lock l(m_aMutex);
399 registerScriptEvent(l, nIndex, ScriptEvent);
400}
401
402void ImplEventAttacherManager::registerScriptEvent
403(
404 std::unique_lock<std::mutex>&,
405 sal_Int32 nIndex,
406 const ScriptEventDescriptor& ScriptEvent
407)
408{
409 // Examine the index and apply the array
410 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
411
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 );
417
418 // register new Event
419 for( auto& rObj : aIt->aObjList )
420 {
422 new AttacherAllListener_Impl( this, ScriptEvent.ScriptType, ScriptEvent.ScriptCode );
423 try
424 {
425 rObj.aAttachedListenerSeq.push_back( xAttacher->attachSingleEventListener( rObj.xTarget, xAll,
426 rObj.aHelper, ScriptEvent.ListenerType,
427 ScriptEvent.AddListenerParam, ScriptEvent.EventMethod ) );
428 }
429 catch( Exception& )
430 {
431 }
432 }
433}
434
435
436void SAL_CALL ImplEventAttacherManager::registerScriptEvents
437(
438 sal_Int32 nIndex,
439 const Sequence< ScriptEventDescriptor >& ScriptEvents
440)
441{
442 std::unique_lock l(m_aMutex);
443 registerScriptEvents(l, nIndex, ScriptEvents);
444}
445
446void ImplEventAttacherManager::registerScriptEvents
447(
448 std::unique_lock<std::mutex>& l,
449 sal_Int32 nIndex,
450 const Sequence< ScriptEventDescriptor >& ScriptEvents
451)
452{
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 );
457
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 ] );
462
463 for( const auto& rObj : aList )
464 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
465}
466
467
468void SAL_CALL ImplEventAttacherManager::revokeScriptEvent
469(
470 sal_Int32 nIndex,
471 const OUString& ListenerType,
472 const OUString& EventMethod,
473 const OUString& ToRemoveListenerParam
474)
475{
476 std::unique_lock l(m_aMutex);
477
478 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
479
480 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
481 for( const auto& rObj : aList )
482 detach( l, nIndex, rObj.xTarget );
483
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);
488
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;
494 });
495 if (aEvtIt != aIt->aEventList.end())
496 aIt->aEventList.erase( aEvtIt );
497
498 for( const auto& rObj : aList )
499 attach( l, nIndex, rObj.xTarget, rObj.aHelper );
500}
501
502
503void SAL_CALL ImplEventAttacherManager::revokeScriptEvents(sal_Int32 nIndex )
504{
505 std::unique_lock l(m_aMutex);
506 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
507
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 );
514}
515
516
517void SAL_CALL ImplEventAttacherManager::insertEntry(sal_Int32 nIndex)
518{
519 std::unique_lock l(m_aMutex);
520 if( nIndex < 0 )
521 throw IllegalArgumentException("negative index", static_cast<cppu::OWeakObject*>(this), 1);
522
523 insertEntry(l, nIndex);
524}
525
526void ImplEventAttacherManager::insertEntry(std::unique_lock<std::mutex>&, sal_Int32 nIndex)
527{
528 if ( o3tl::make_unsigned(nIndex) >= aIndex.size() )
529 aIndex.resize(nIndex+1);
530
531 AttacherIndex_Impl aTmp;
532 aIndex.insert( aIndex.begin() + nIndex, aTmp );
533}
534
535void SAL_CALL ImplEventAttacherManager::removeEntry(sal_Int32 nIndex)
536{
537 std::unique_lock l(m_aMutex);
538 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
539
540 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
541 for( const auto& rObj : aList )
542 detach( l, nIndex, rObj.xTarget );
543
544 aIndex.erase( aIt );
545}
546
547
548Sequence< ScriptEventDescriptor > SAL_CALL ImplEventAttacherManager::getScriptEvents(sal_Int32 nIndex)
549{
550 std::unique_lock l(m_aMutex);
551 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
552 return comphelper::containerToSequence(aIt->aEventList);
553}
554
555
556void SAL_CALL ImplEventAttacherManager::attach(sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
557{
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);
562}
563
564void ImplEventAttacherManager::attach(std::unique_lock<std::mutex>& l, sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
565{
566 if( o3tl::make_unsigned(nIndex) >= aIndex.size() )
567 {
568 // read older files
569 if( nVersion != 1 )
570 throw IllegalArgumentException();
571 insertEntry( l, nIndex );
572 attach( l, nIndex, xObject, Helper );
573 return;
574 }
575
576 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
577
578 AttachedObject_Impl aTmp;
579 aTmp.xTarget = xObject;
580 aTmp.aHelper = Helper;
581 aCurrentPosition->aObjList.push_back( aTmp );
582
583 AttachedObject_Impl & rCurObj = aCurrentPosition->aObjList.back();
584 rCurObj.aAttachedListenerSeq = std::vector< Reference< XEventListener > >( aCurrentPosition->aEventList.size() );
585
586 if (aCurrentPosition->aEventList.empty())
587 return;
588
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)
593 {
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;
602 }
603
604 try
605 {
606 rCurObj.aAttachedListenerSeq = comphelper::sequenceToContainer<std::vector<Reference< XEventListener >>>(
607 xAttacher->attachMultipleEventListeners(rCurObj.xTarget, aEvents));
608 }
609 catch (const Exception&)
610 {
611 // Fail gracefully.
612 }
613}
614
615
616void SAL_CALL ImplEventAttacherManager::detach(sal_Int32 nIndex, const Reference< XInterface >& xObject)
617{
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);
623}
624
625void ImplEventAttacherManager::detach(std::unique_lock<std::mutex>&, sal_Int32 nIndex, const Reference< XInterface >& xObject)
626{
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;
632
633 sal_Int32 i = 0;
634 for( const auto& rEvt : aCurrentPosition->aEventList )
635 {
636 if( aObjIt->aAttachedListenerSeq[i].is() )
637 {
638 try
639 {
640 xAttacher->removeListener( aObjIt->xTarget, rEvt.ListenerType,
641 rEvt.AddListenerParam, aObjIt->aAttachedListenerSeq[i] );
642 }
643 catch( Exception& )
644 {
645 }
646 }
647 ++i;
648 }
649 aCurrentPosition->aObjList.erase( aObjIt );
650}
651
652void SAL_CALL ImplEventAttacherManager::addScriptListener(const Reference< XScriptListener >& aListener)
653{
654 std::unique_lock l(m_aMutex);
655 aScriptListeners.addInterface( l, aListener );
656}
657
658void SAL_CALL ImplEventAttacherManager::removeScriptListener(const Reference< XScriptListener >& aListener)
659{
660 std::unique_lock l(m_aMutex);
661 aScriptListeners.removeInterface( l, aListener );
662}
663
664
665// Methods of XPersistObject
666OUString SAL_CALL ImplEventAttacherManager::getServiceName()
667{
668 return "com.sun.star.uno.script.EventAttacherManager";
669}
670
671void SAL_CALL ImplEventAttacherManager::write(const Reference< XObjectOutputStream >& OutStream)
672{
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;
678
679 // Write out the version
680 OutStream->writeShort( 2 );
681
682 // Remember position for length
683 sal_Int32 nObjLenMark = xMarkStream->createMark();
684 OutStream->writeLong( 0 );
685
686 OutStream->writeLong( aIndex.size() );
687
688 // Write out sequences
689 for( const auto& rIx : aIndex )
690 {
691 OutStream->writeLong( rIx.aEventList.size() );
692 for( const auto& rDesc : rIx.aEventList )
693 {
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 );
699 }
700 }
701
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 );
708}
709
710void SAL_CALL ImplEventAttacherManager::read(const Reference< XObjectInputStream >& InStream)
711{
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;
717
718 // Read in the version
719 nVersion = InStream->readShort();
720
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();
724
725 // Position for comparative purposes
726 sal_Int32 nObjLenMark = xMarkStream->createMark();
727
728 // Number of read sequences
729 sal_Int32 nItemCount = InStream->readLong();
730
731 for( sal_Int32 i = 0 ; i < nItemCount ; i++ )
732 {
733 insertEntry( l, i );
734 // Read the length of the sequence
735 sal_Int32 nSeqLen = InStream->readLong();
736
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++ )
741 {
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();
748 }
749 registerScriptEvents( l, i, aSEDSeq );
750 }
751
752 // Have we read the specified length?
753 sal_Int32 nRealLen = xMarkStream->offsetToMark( nObjLenMark );
754 if( nRealLen != nLen )
755 {
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 )
759 {
760 OSL_FAIL( "ImplEventAttacherManager::read(): Fatal Error, wrong object length" );
761 }
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 );
767 }
768 }
769 xMarkStream->jumpToFurthest();
770 xMarkStream->deleteMark( nObjLenMark );
771}
772
773} // namespace comphelper
774
775
776/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
float x
rtl::Reference< ImplEventAttacherManager > mxManager
std::deque< AttachedObject_Impl > aObjList
std::mutex m_aMutex
OUString const aScriptCode
Any aHelper
Reference< XComponentContext > mxContext
std::vector< Reference< XEventListener > > aAttachedListenerSeq
sal_Int16 nVersion
Reference< XEventAttacher2 > xAttacher
Reference< XInterface > xTarget
std::deque< AttacherIndex_Impl > aIndex
OUString const aScriptType
Reference< XIdlReflection > mxCoreReflection
Reference< XTypeConverter > xConverter
OInterfaceContainerHelper4< XScriptListener > aScriptListeners
std::deque< ScriptEventDescriptor > aEventList
sal_Int32 nIndex
Reference< XIntrospection > xIntrospection
void * p
Reference< XEventAttacherManager > createEventAttacherManager(const Reference< XComponentContext > &rxContext)
css::uno::Sequence< DstElementType > containerToSequence(const SrcType &i_Container)
Copy from a container into a Sequence.
Definition: sequence.hxx:190
Type
Any SAL_CALL getCaughtException()
int i
constexpr std::enable_if_t< std::is_signed_v< T >, std::make_unsigned_t< T > > make_unsigned(T value)
std::mutex mutex
Definition: random.cxx:41
unsigned char sal_uInt8