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/mutex.hxx>
25#include <osl/diagnose.h>
28#include <com/sun/star/beans/theIntrospection.hpp>
29#include <com/sun/star/io/XObjectInputStream.hpp>
30#include <com/sun/star/io/XPersistObject.hpp>
31#include <com/sun/star/io/XObjectOutputStream.hpp>
32#include <com/sun/star/io/XMarkableStream.hpp>
33#include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
34#include <com/sun/star/lang/XInitialization.hpp>
35#include <com/sun/star/reflection/theCoreReflection.hpp>
36#include <com/sun/star/reflection/XIdlClass.hpp>
37#include <com/sun/star/reflection/XIdlReflection.hpp>
38#include <com/sun/star/reflection/XIdlMethod.hpp>
39#include <com/sun/star/script/CannotConvertException.hpp>
40#include <com/sun/star/script/Converter.hpp>
41#include <com/sun/star/script/XEventAttacher2.hpp>
42#include <com/sun/star/script/XEventAttacherManager.hpp>
43#include <com/sun/star/script/XScriptListener.hpp>
44#include <cppuhelper/weak.hxx>
48#include <rtl/ref.hxx>
49
50#include <deque>
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;
88 Mutex aLock;
89 // Container for the ScriptListener
90 OInterfaceContainerHelper3<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:
121 Reference< XIdlReflection > getReflection();
122
129 std::deque<AttacherIndex_Impl>::iterator implCheckIndex( sal_Int32 _nIndex );
130};
131
132
133// Implementation of an EventAttacher-subclass 'AllListeners', which
134// only passes individual events of the general AllListeners.
135class AttacherAllListener_Impl : public WeakImplHelper< XAllListener >
136{
138 OUString const aScriptType;
139 OUString const aScriptCode;
140
142 void convertToEventReturn( Any & rRet, const Type & rRetType );
143public:
144 AttacherAllListener_Impl( ImplEventAttacherManager* pManager_, OUString aScriptType_,
145 OUString aScriptCode_ );
146
147 // Methods of XAllListener
148 virtual void SAL_CALL firing(const AllEventObject& Event) override;
149 virtual Any SAL_CALL approveFiring(const AllEventObject& Event) override;
150
151 // Methods of XEventListener
152 virtual void SAL_CALL disposing(const EventObject& Source) override;
153};
154
155}
156
157AttacherAllListener_Impl::AttacherAllListener_Impl
158(
159 ImplEventAttacherManager* pManager_,
160 OUString aScriptType_,
161 OUString aScriptCode_
162)
163 : mxManager( pManager_ )
164 , aScriptType(std::move( aScriptType_ ))
165 , aScriptCode(std::move( aScriptCode_ ))
166{
167}
168
169
170// Methods of XAllListener
171void SAL_CALL AttacherAllListener_Impl::firing(const AllEventObject& Event)
172{
173 ScriptEvent aScriptEvent;
174 aScriptEvent.Source = static_cast<OWeakObject *>(mxManager.get()); // get correct XInterface
175 aScriptEvent.ListenerType = Event.ListenerType;
176 aScriptEvent.MethodName = Event.MethodName;
177 aScriptEvent.Arguments = Event.Arguments;
178 aScriptEvent.Helper = Event.Helper;
179 aScriptEvent.ScriptType = aScriptType;
180 aScriptEvent.ScriptCode = aScriptCode;
181
182 // Iterate over all listeners and pass events.
183 mxManager->aScriptListeners.notifyEach( &XScriptListener::firing, aScriptEvent );
184}
185
186
187// Convert to the standard event return
188void AttacherAllListener_Impl::convertToEventReturn( Any & rRet, const Type & rRetType )
189{
190 // no return value? Set to the specified values
191 if( rRet.getValueType().getTypeClass() == TypeClass_VOID )
192 {
193 switch( rRetType.getTypeClass() )
194 {
195 case TypeClass_INTERFACE:
196 {
197 rRet <<= Reference< XInterface >();
198 }
199 break;
200
201 case TypeClass_BOOLEAN:
202 rRet <<= true;
203 break;
204
205 case TypeClass_STRING:
206 rRet <<= OUString();
207 break;
208
209 case TypeClass_FLOAT: rRet <<= float(0); break;
210 case TypeClass_DOUBLE: rRet <<= 0.0; break;
211 case TypeClass_BYTE: rRet <<= sal_uInt8(0); break;
212 case TypeClass_SHORT: rRet <<= sal_Int16( 0 ); break;
213 case TypeClass_LONG: rRet <<= sal_Int32( 0 ); break;
214 case TypeClass_UNSIGNED_SHORT: rRet <<= sal_uInt16( 0 ); break;
215 case TypeClass_UNSIGNED_LONG: rRet <<= sal_uInt32( 0 ); break;
216
217 default:
218 OSL_ASSERT(false);
219 break;
220 }
221 }
222 else if( !rRet.getValueType().equals( rRetType ) )
223 {
224 if( !mxManager->xConverter.is() )
225 throw CannotConvertException();
226 rRet = mxManager->xConverter->convertTo( rRet, rRetType );
227 }
228}
229
230// Methods of XAllListener
231Any SAL_CALL AttacherAllListener_Impl::approveFiring( const AllEventObject& Event )
232{
233 ScriptEvent aScriptEvent;
234 aScriptEvent.Source = static_cast<OWeakObject *>(mxManager.get()); // get correct XInterface
235 aScriptEvent.ListenerType = Event.ListenerType;
236 aScriptEvent.MethodName = Event.MethodName;
237 aScriptEvent.Arguments = Event.Arguments;
238 aScriptEvent.Helper = Event.Helper;
239 aScriptEvent.ScriptType = aScriptType;
240 aScriptEvent.ScriptCode = aScriptCode;
241
242 Any aRet;
243 // Iterate over all listeners and pass events.
244 OInterfaceIteratorHelper3 aIt( mxManager->aScriptListeners );
245 while( aIt.hasMoreElements() )
246 {
247 aRet = aIt.next()->approveFiring( aScriptEvent );
248 try
249 {
250 Reference< XIdlClass > xListenerType = mxManager->getReflection()->
251 forName( Event.ListenerType.getTypeName() );
252 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
253 if( xMeth.is() )
254 {
255 Reference< XIdlClass > xRetType = xMeth->getReturnType();
256 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
257 convertToEventReturn( aRet, aRetType );
258 }
259
260 switch( aRet.getValueType().getTypeClass() )
261 {
262 case TypeClass_INTERFACE:
263 {
264 // Interface not null, return
266 aRet >>= x;
267 if( x.is() )
268 return aRet;
269 }
270 break;
271
272 case TypeClass_BOOLEAN:
273 // FALSE -> Return
274 if( !(*o3tl::forceAccess<bool>(aRet)) )
275 return aRet;
276 break;
277
278 case TypeClass_STRING:
279 // none empty string -> return
280 if( !o3tl::forceAccess<OUString>(aRet)->isEmpty() )
281 return aRet;
282 break;
283
284 // none zero number -> return
285 case TypeClass_FLOAT: if( *o3tl::forceAccess<float>(aRet) ) return aRet; break;
286 case TypeClass_DOUBLE: if( *o3tl::forceAccess<double>(aRet) ) return aRet; break;
287 case TypeClass_BYTE: if( *o3tl::forceAccess<sal_Int8>(aRet) ) return aRet; break;
288 case TypeClass_SHORT: if( *o3tl::forceAccess<sal_Int16>(aRet) ) return aRet; break;
289 case TypeClass_LONG: if( *o3tl::forceAccess<sal_Int32>(aRet) ) return aRet; break;
290 case TypeClass_UNSIGNED_SHORT: if( *o3tl::forceAccess<sal_uInt16>(aRet) ) return aRet; break;
291 case TypeClass_UNSIGNED_LONG: if( *o3tl::forceAccess<sal_uInt32>(aRet) ) return aRet; break;
292
293 default:
294 OSL_ASSERT(false);
295 break;
296 }
297 }
298 catch (const CannotConvertException&)
299 {
300 // silent ignore conversions errors from a script call
301 Reference< XIdlClass > xListenerType = mxManager->getReflection()->
302 forName( Event.ListenerType.getTypeName() );
303 Reference< XIdlMethod > xMeth = xListenerType->getMethod( Event.MethodName );
304 if( xMeth.is() )
305 {
306 Reference< XIdlClass > xRetType = xMeth->getReturnType();
307 Type aRetType(xRetType->getTypeClass(), xRetType->getName());
308 aRet.clear();
309 try
310 {
311 convertToEventReturn( aRet, aRetType );
312 }
313 catch (const CannotConvertException& e)
314 {
315 css::uno::Any anyEx = cppu::getCaughtException();
316 throw css::lang::WrappedTargetRuntimeException(
317 "wrapped CannotConvertException " + e.Message,
318 css::uno::Reference<css::uno::XInterface>(), anyEx);
319 }
320 }
321 }
322 }
323 return aRet;
324}
325
326// Methods of XEventListener
327void SAL_CALL AttacherAllListener_Impl::disposing(const EventObject& )
328{
329 // It is up to the container to release the object
330}
331
332// Constructor method for EventAttacherManager
334{
335 Reference< XIntrospection > xIntrospection = theIntrospection::get( rxContext );
336 return new ImplEventAttacherManager( xIntrospection, rxContext );
337}
338
339
340ImplEventAttacherManager::ImplEventAttacherManager( const Reference< XIntrospection > & rIntrospection,
341 const Reference< XComponentContext >& rContext )
343 , mxContext( rContext )
344 , nVersion(0)
345{
346 if ( rContext.is() )
347 {
348 Reference< XInterface > xIFace( rContext->getServiceManager()->createInstanceWithContext(
349 "com.sun.star.script.EventAttacher", rContext) );
350 if ( xIFace.is() )
351 {
352 xAttacher.set( xIFace, UNO_QUERY );
353 }
354 xConverter = Converter::create(rContext);
355 }
356
357 Reference< XInitialization > xInit( xAttacher, UNO_QUERY );
358 if( xInit.is() )
359 {
360 xInit->initialize({ Any(rIntrospection) });
361 }
362}
363
364Reference< XIdlReflection > ImplEventAttacherManager::getReflection()
365{
366 Guard< Mutex > aGuard( aLock );
367 // Do we already have a service? If not, create one.
368 if( !mxCoreReflection.is() )
369 {
370 mxCoreReflection = theCoreReflection::get(mxContext);
371 }
372 return mxCoreReflection;
373}
374
375
376std::deque< AttacherIndex_Impl >::iterator ImplEventAttacherManager::implCheckIndex( sal_Int32 _nIndex )
377{
378 if ( (_nIndex < 0) || (o3tl::make_unsigned(_nIndex) >= aIndex.size()) )
379 throw IllegalArgumentException("wrong index", static_cast<cppu::OWeakObject*>(this), 1);
380
381 std::deque<AttacherIndex_Impl>::iterator aIt = aIndex.begin() + _nIndex;
382 return aIt;
383}
384
385// Methods of XEventAttacherManager
386void SAL_CALL ImplEventAttacherManager::registerScriptEvent
387(
388 sal_Int32 nIndex,
389 const ScriptEventDescriptor& ScriptEvent
390)
391{
392 Guard< Mutex > aGuard( aLock );
393
394 // Examine the index and apply the array
395 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
396
397 ScriptEventDescriptor aEvt = ScriptEvent;
398 sal_Int32 nLastDot = aEvt.ListenerType.lastIndexOf('.');
399 if (nLastDot != -1)
400 aEvt.ListenerType = aEvt.ListenerType.copy(nLastDot+1);
401 aIt->aEventList.push_back( aEvt );
402
403 // register new Event
404 for( auto& rObj : aIt->aObjList )
405 {
407 new AttacherAllListener_Impl( this, ScriptEvent.ScriptType, ScriptEvent.ScriptCode );
408 try
409 {
410 rObj.aAttachedListenerSeq.push_back( xAttacher->attachSingleEventListener( rObj.xTarget, xAll,
411 rObj.aHelper, ScriptEvent.ListenerType,
412 ScriptEvent.AddListenerParam, ScriptEvent.EventMethod ) );
413 }
414 catch( Exception& )
415 {
416 }
417 }
418}
419
420
421void SAL_CALL ImplEventAttacherManager::registerScriptEvents
422(
423 sal_Int32 nIndex,
424 const Sequence< ScriptEventDescriptor >& ScriptEvents
425)
426{
427 Guard< Mutex > aGuard( aLock );
428
429 // Examine the index and apply the array
430 std::deque< AttachedObject_Impl > aList = implCheckIndex( nIndex )->aObjList;
431 for( const auto& rObj : aList )
432 detach( nIndex, rObj.xTarget );
433
434 const ScriptEventDescriptor* pArray = ScriptEvents.getConstArray();
435 sal_Int32 nLen = ScriptEvents.getLength();
436 for( sal_Int32 i = 0 ; i < nLen ; i++ )
437 registerScriptEvent( nIndex, pArray[ i ] );
438
439 for( const auto& rObj : aList )
440 attach( nIndex, rObj.xTarget, rObj.aHelper );
441}
442
443
444void SAL_CALL ImplEventAttacherManager::revokeScriptEvent
445(
446 sal_Int32 nIndex,
447 const OUString& ListenerType,
448 const OUString& EventMethod,
449 const OUString& ToRemoveListenerParam
450)
451{
452 Guard< Mutex > aGuard( aLock );
453
454 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
455
456 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
457 for( const auto& rObj : aList )
458 detach( nIndex, rObj.xTarget );
459
460 std::u16string_view aLstType = ListenerType;
461 size_t nLastDot = aLstType.rfind('.');
462 if (nLastDot != std::u16string_view::npos)
463 aLstType = aLstType.substr(nLastDot+1);
464
465 auto aEvtIt = std::find_if(aIt->aEventList.begin(), aIt->aEventList.end(),
466 [&aLstType, &EventMethod, &ToRemoveListenerParam](const ScriptEventDescriptor& rEvent) {
467 return aLstType == rEvent.ListenerType
468 && EventMethod == rEvent.EventMethod
469 && ToRemoveListenerParam == rEvent.AddListenerParam;
470 });
471 if (aEvtIt != aIt->aEventList.end())
472 aIt->aEventList.erase( aEvtIt );
473
474 for( const auto& rObj : aList )
475 attach( nIndex, rObj.xTarget, rObj.aHelper );
476}
477
478
479void SAL_CALL ImplEventAttacherManager::revokeScriptEvents(sal_Int32 nIndex )
480{
481 Guard< Mutex > aGuard( aLock );
482 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
483
484 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
485 for( const auto& rObj : aList )
486 detach( nIndex, rObj.xTarget );
487 aIt->aEventList.clear();
488 for( const auto& rObj : aList )
489 attach( nIndex, rObj.xTarget, rObj.aHelper );
490}
491
492
493void SAL_CALL ImplEventAttacherManager::insertEntry(sal_Int32 nIndex)
494{
495 Guard< Mutex > aGuard( aLock );
496 if( nIndex < 0 )
497 throw IllegalArgumentException("negative index", static_cast<cppu::OWeakObject*>(this), 1);
498
499 if ( o3tl::make_unsigned(nIndex) >= aIndex.size() )
500 aIndex.resize(nIndex+1);
501
502 AttacherIndex_Impl aTmp;
503 aIndex.insert( aIndex.begin() + nIndex, aTmp );
504}
505
506
507void SAL_CALL ImplEventAttacherManager::removeEntry(sal_Int32 nIndex)
508{
509 Guard< Mutex > aGuard( aLock );
510 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
511
512 std::deque< AttachedObject_Impl > aList = aIt->aObjList;
513 for( const auto& rObj : aList )
514 detach( nIndex, rObj.xTarget );
515
516 aIndex.erase( aIt );
517}
518
519
520Sequence< ScriptEventDescriptor > SAL_CALL ImplEventAttacherManager::getScriptEvents(sal_Int32 nIndex)
521{
522 Guard< Mutex > aGuard( aLock );
523 std::deque<AttacherIndex_Impl>::iterator aIt = implCheckIndex( nIndex );
524 return comphelper::containerToSequence(aIt->aEventList);
525}
526
527
528void SAL_CALL ImplEventAttacherManager::attach(sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any & Helper)
529{
530 Guard< Mutex > aGuard( aLock );
531 if( nIndex < 0 || !xObject.is() )
532 throw IllegalArgumentException("negative index, or null object", static_cast<cppu::OWeakObject*>(this), -1);
533
534 if( o3tl::make_unsigned(nIndex) >= aIndex.size() )
535 {
536 // read older files
537 if( nVersion != 1 )
538 throw IllegalArgumentException();
539 insertEntry( nIndex );
540 attach( nIndex, xObject, Helper );
541 return;
542 }
543
544 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
545
546 AttachedObject_Impl aTmp;
547 aTmp.xTarget = xObject;
548 aTmp.aHelper = Helper;
549 aCurrentPosition->aObjList.push_back( aTmp );
550
551 AttachedObject_Impl & rCurObj = aCurrentPosition->aObjList.back();
552 rCurObj.aAttachedListenerSeq = std::vector< Reference< XEventListener > >( aCurrentPosition->aEventList.size() );
553
554 if (aCurrentPosition->aEventList.empty())
555 return;
556
557 Sequence<css::script::EventListener> aEvents(aCurrentPosition->aEventList.size());
558 css::script::EventListener* p = aEvents.getArray();
559 size_t i = 0;
560 for (const auto& rEvent : aCurrentPosition->aEventList)
561 {
562 css::script::EventListener aListener;
563 aListener.AllListener =
564 new AttacherAllListener_Impl(this, rEvent.ScriptType, rEvent.ScriptCode);
565 aListener.Helper = rCurObj.aHelper;
566 aListener.ListenerType = rEvent.ListenerType;
567 aListener.EventMethod = rEvent.EventMethod;
568 aListener.AddListenerParam = rEvent.AddListenerParam;
569 p[i++] = aListener;
570 }
571
572 try
573 {
574 rCurObj.aAttachedListenerSeq = comphelper::sequenceToContainer<std::vector<Reference< XEventListener >>>(
575 xAttacher->attachMultipleEventListeners(rCurObj.xTarget, aEvents));
576 }
577 catch (const Exception&)
578 {
579 // Fail gracefully.
580 }
581}
582
583
584void SAL_CALL ImplEventAttacherManager::detach(sal_Int32 nIndex, const Reference< XInterface >& xObject)
585{
586 Guard< Mutex > aGuard( aLock );
587 //return;
588 if( nIndex < 0 || o3tl::make_unsigned(nIndex) >= aIndex.size() || !xObject.is() )
589 throw IllegalArgumentException("bad index or null object", static_cast<cppu::OWeakObject*>(this), 1);
590
591 std::deque< AttacherIndex_Impl >::iterator aCurrentPosition = aIndex.begin() + nIndex;
592 auto aObjIt = std::find_if(aCurrentPosition->aObjList.begin(), aCurrentPosition->aObjList.end(),
593 [&xObject](const AttachedObject_Impl& rObj) { return rObj.xTarget == xObject; });
594 if (aObjIt == aCurrentPosition->aObjList.end())
595 return;
596
597 sal_Int32 i = 0;
598 for( const auto& rEvt : aCurrentPosition->aEventList )
599 {
600 if( aObjIt->aAttachedListenerSeq[i].is() )
601 {
602 try
603 {
604 xAttacher->removeListener( aObjIt->xTarget, rEvt.ListenerType,
605 rEvt.AddListenerParam, aObjIt->aAttachedListenerSeq[i] );
606 }
607 catch( Exception& )
608 {
609 }
610 }
611 ++i;
612 }
613 aCurrentPosition->aObjList.erase( aObjIt );
614}
615
616void SAL_CALL ImplEventAttacherManager::addScriptListener(const Reference< XScriptListener >& aListener)
617{
618 Guard< Mutex > aGuard( aLock );
619 aScriptListeners.addInterface( aListener );
620}
621
622void SAL_CALL ImplEventAttacherManager::removeScriptListener(const Reference< XScriptListener >& aListener)
623{
624 Guard< Mutex > aGuard( aLock );
625 aScriptListeners.removeInterface( aListener );
626}
627
628
629// Methods of XPersistObject
630OUString SAL_CALL ImplEventAttacherManager::getServiceName()
631{
632 return "com.sun.star.uno.script.EventAttacherManager";
633}
634
635void SAL_CALL ImplEventAttacherManager::write(const Reference< XObjectOutputStream >& OutStream)
636{
637 Guard< Mutex > aGuard( aLock );
638 // Don't run without XMarkableStream
639 Reference< XMarkableStream > xMarkStream( OutStream, UNO_QUERY );
640 if( !xMarkStream.is() )
641 return;
642
643 // Write out the version
644 OutStream->writeShort( 2 );
645
646 // Remember position for length
647 sal_Int32 nObjLenMark = xMarkStream->createMark();
648 OutStream->writeLong( 0 );
649
650 OutStream->writeLong( aIndex.size() );
651
652 // Write out sequences
653 for( const auto& rIx : aIndex )
654 {
655 OutStream->writeLong( rIx.aEventList.size() );
656 for( const auto& rDesc : rIx.aEventList )
657 {
658 OutStream->writeUTF( rDesc.ListenerType );
659 OutStream->writeUTF( rDesc.EventMethod );
660 OutStream->writeUTF( rDesc.AddListenerParam );
661 OutStream->writeUTF( rDesc.ScriptType );
662 OutStream->writeUTF( rDesc.ScriptCode );
663 }
664 }
665
666 // The length is now known
667 sal_Int32 nObjLen = xMarkStream->offsetToMark( nObjLenMark ) -4;
668 xMarkStream->jumpToMark( nObjLenMark );
669 OutStream->writeLong( nObjLen );
670 xMarkStream->jumpToFurthest();
671 xMarkStream->deleteMark( nObjLenMark );
672}
673
674void SAL_CALL ImplEventAttacherManager::read(const Reference< XObjectInputStream >& InStream)
675{
676 Guard< Mutex > aGuard( aLock );
677 // Don't run without XMarkableStream
678 Reference< XMarkableStream > xMarkStream( InStream, UNO_QUERY );
679 if( !xMarkStream.is() )
680 return;
681
682 // Read in the version
683 nVersion = InStream->readShort();
684
685 // At first there's the data according to version 1 --
686 // this part needs to be kept in later versions.
687 sal_Int32 nLen = InStream->readLong();
688
689 // Position for comparative purposes
690 sal_Int32 nObjLenMark = xMarkStream->createMark();
691
692 // Number of read sequences
693 sal_Int32 nItemCount = InStream->readLong();
694
695 for( sal_Int32 i = 0 ; i < nItemCount ; i++ )
696 {
697 insertEntry( i );
698 // Read the length of the sequence
699 sal_Int32 nSeqLen = InStream->readLong();
700
701 // Display the sequences and read the descriptions
702 Sequence< ScriptEventDescriptor > aSEDSeq( nSeqLen );
703 ScriptEventDescriptor* pArray = aSEDSeq.getArray();
704 for( sal_Int32 j = 0 ; j < nSeqLen ; j++ )
705 {
706 ScriptEventDescriptor& rDesc = pArray[ j ];
707 rDesc.ListenerType = InStream->readUTF();
708 rDesc.EventMethod = InStream->readUTF();
709 rDesc.AddListenerParam = InStream->readUTF();
710 rDesc.ScriptType = InStream->readUTF();
711 rDesc.ScriptCode = InStream->readUTF();
712 }
713 registerScriptEvents( i, aSEDSeq );
714 }
715
716 // Have we read the specified length?
717 sal_Int32 nRealLen = xMarkStream->offsetToMark( nObjLenMark );
718 if( nRealLen != nLen )
719 {
720 // Only if the StreamVersion is > 1 and the date still follows, can
721 // this be true. Otherwise, something is completely gone.
722 if( nRealLen > nLen || nVersion == 1 )
723 {
724 OSL_FAIL( "ImplEventAttacherManager::read(): Fatal Error, wrong object length" );
725 }
726 else
727 { // TODO: Examine if caching the dates would be useful
728 // But for now, it's easier to skip it.
729 sal_Int32 nSkipCount = nLen - nRealLen;
730 InStream->skipBytes( nSkipCount );
731 }
732 }
733 xMarkStream->jumpToFurthest();
734 xMarkStream->deleteMark( nObjLenMark );
735}
736
737} // namespace comphelper
738
739
740/* vim:set shiftwidth=4 softtabstop=4 expandtab: */
struct _ADOIndex Index
float x
rtl::Reference< ImplEventAttacherManager > mxManager
std::deque< AttachedObject_Impl > aObjList
OUString const aScriptCode
Any aHelper
Reference< XComponentContext > mxContext
std::vector< Reference< XEventListener > > aAttachedListenerSeq
sal_Int16 nVersion
Reference< XEventAttacher2 > xAttacher
Mutex aLock
Reference< XInterface > xTarget
OInterfaceContainerHelper3< XScriptListener > aScriptListeners
std::deque< AttacherIndex_Impl > aIndex
OUString const aScriptType
Reference< XIdlReflection > mxCoreReflection
Reference< XTypeConverter > xConverter
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)
unsigned char sal_uInt8