xref: /trunk/main/forms/source/misc/InterfaceContainer.cxx (revision 46c69c84579d1f1df536015e7d54c9965db90b69)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_forms.hxx"
26 
27 #include "frm_resource.hrc"
28 #include "frm_resource.hxx"
29 #include "InterfaceContainer.hxx"
30 #include "componenttools.hxx"
31 #include "property.hrc"
32 #include "services.hxx"
33 
34 #include <com/sun/star/beans/XPropertySet.hpp>
35 #include <com/sun/star/container/XNamed.hpp>
36 #include <com/sun/star/io/WrongFormatException.hpp>
37 #include <com/sun/star/io/XMarkableStream.hpp>
38 #include <com/sun/star/lang/XComponent.hpp>
39 #include <com/sun/star/util/XCloneable.hpp>
40 #include <com/sun/star/form/XForm.hpp>
41 
42 #include <comphelper/container.hxx>
43 #include <comphelper/enumhelper.hxx>
44 #include <comphelper/eventattachermgr.hxx>
45 #include <comphelper/property.hxx>
46 #include <comphelper/sequence.hxx>
47 #include <comphelper/types.hxx>
48 #include <cppuhelper/exc_hlp.hxx>
49 #include <cppuhelper/queryinterface.hxx>
50 #include <rtl/logfile.hxx>
51 #include <tools/debug.hxx>
52 #include <tools/diagnose_ex.h>
53 
54 #include <algorithm>
55 #include <memory>
56 
57 //.........................................................................
58 #include <com/sun/star/frame/XModel.hpp>
59 #include <com/sun/star/document/XCodeNameQuery.hpp>
60 #include <ooo/vba/XVBAToOOEventDescGen.hpp>
61 #include <comphelper/processfactory.hxx>
62 
63 namespace frm
64 {
65 //.........................................................................
66 
67 using namespace ::com::sun::star::frame;
68 using namespace ::com::sun::star::lang;
69 using namespace ::com::sun::star::uno;
70 using namespace ::com::sun::star::beans;
71 using namespace ::com::sun::star::document;
72 using namespace ::com::sun::star::container;
73 using namespace ::com::sun::star::script;
74 using namespace ::com::sun::star::io;
75 using namespace ::com::sun::star::form;
76 using namespace ::com::sun::star::util;
77 
78 namespace
79 {
80     //---------------------------------------------------------------------
lcl_throwIllegalArgumentException()81     static void lcl_throwIllegalArgumentException()
82     {
83         throw IllegalArgumentException();
84     }
85 }
86 
87 bool
lcl_hasVbaEvents(const Sequence<ScriptEventDescriptor> & sEvents)88 lcl_hasVbaEvents( const Sequence< ScriptEventDescriptor >& sEvents  )
89 {
90     const ScriptEventDescriptor* pDesc = sEvents.getConstArray();
91     const ScriptEventDescriptor* pEnd = ( pDesc + sEvents.getLength() );
92     for ( ; pDesc != pEnd; ++pDesc )
93     {
94         if ( pDesc->ScriptType.equals( rtl::OUString::createFromAscii( "VBAInterop" ) ) )
95             return true;
96     }
97     return false;
98 }
99 
100 Sequence< ScriptEventDescriptor >
lcl_stripVbaEvents(const Sequence<ScriptEventDescriptor> & sEvents)101 lcl_stripVbaEvents( const Sequence< ScriptEventDescriptor >& sEvents )
102 {
103     Sequence< ScriptEventDescriptor > sStripped( sEvents.getLength() );
104 
105     const ScriptEventDescriptor* pDesc = sEvents.getConstArray();
106     const ScriptEventDescriptor* pEnd = ( pDesc + sEvents.getLength() );
107     sal_Int32 nCopied = 0;
108     for ( ; pDesc != pEnd; ++pDesc )
109     {
110         if ( !pDesc->ScriptType.equals( rtl::OUString::createFromAscii( "VBAInterop" ) ) )
111         {
112             sStripped[ nCopied++ ] = *pDesc;
113         }
114     }
115     if ( nCopied )
116         sStripped.realloc( nCopied );
117     return sStripped;
118 }
119 
impl_addVbEvents_nolck_nothrow(const sal_Int32 i_nIndex)120 void OInterfaceContainer::impl_addVbEvents_nolck_nothrow(  const sal_Int32 i_nIndex )
121 {
122     // we are dealing with form controls
123     try
124     {
125         do
126         {
127             Reference< XModel > xDoc( getXModel( static_cast< XContainer *> ( this ) ) );
128             if ( !xDoc.is() )
129                 break;
130 
131             Reference< XMultiServiceFactory > xDocFac( xDoc, UNO_QUERY_THROW );
132             Reference< XCodeNameQuery > xNameQuery( xDocFac->createInstance( rtl::OUString::createFromAscii( "ooo.vba.VBACodeNameProvider" ) ), UNO_QUERY );
133             if ( !xNameQuery.is() )
134                 break;
135 
136             ::osl::MutexGuard aGuard( m_rMutex );
137             bool hasVBABindings = lcl_hasVbaEvents( m_xEventAttacher->getScriptEvents( i_nIndex ) );
138             if ( hasVBABindings )
139                 break;
140 
141             Reference< XInterface > xElement( getByIndex( i_nIndex ) , UNO_QUERY_THROW );
142             Reference< XForm > xElementAsForm( xElement, UNO_QUERY );
143             if ( xElementAsForm.is() )
144                 break;
145 
146             ::rtl::OUString sCodeName( xNameQuery->getCodeNameForObject( xElement ) );
147 
148             Reference< XPropertySet > xProps( xElement, UNO_QUERY_THROW );
149             ::rtl::OUString sServiceName;
150             xProps->getPropertyValue( rtl::OUString::createFromAscii("DefaultControl" ) ) >>= sServiceName;
151 
152             Reference< ooo::vba::XVBAToOOEventDescGen > xDescSupplier( m_xServiceFactory->createInstance( rtl::OUString::createFromAscii( "ooo.vba.VBAToOOEventDesc" ) ), UNO_QUERY_THROW );
153             Sequence< ScriptEventDescriptor > vbaEvents = xDescSupplier->getEventDescriptions( m_xServiceFactory->createInstance( sServiceName ), sCodeName );
154             // register the vba script events
155             m_xEventAttacher->registerScriptEvents( i_nIndex, vbaEvents );
156         }
157         while ( false );
158     }
159     catch ( const ServiceNotRegisteredException& )
160     {
161         // silence this, not all document types support the ooo.vba.VBACodeNameProvider service
162     }
163     catch( const Exception& )
164     {
165         DBG_UNHANDLED_EXCEPTION();
166     }
167 
168 }
169 //==================================================================
170 //= ElementDescription
171 //==================================================================
172 //------------------------------------------------------------------
ElementDescription()173 ElementDescription::ElementDescription( )
174 {
175 }
176 
177 //------------------------------------------------------------------
~ElementDescription()178 ElementDescription::~ElementDescription()
179 {
180 }
181 
182 //==================================================================
183 //= OInterfaceContainer
184 //==================================================================
185 //------------------------------------------------------------------
OInterfaceContainer(const Reference<XMultiServiceFactory> & _rxFactory,::osl::Mutex & _rMutex,const Type & _rElementType)186 OInterfaceContainer::OInterfaceContainer(
187                 const Reference<XMultiServiceFactory>& _rxFactory,
188                 ::osl::Mutex& _rMutex,
189                 const Type& _rElementType)
190     :OInterfaceContainer_BASE()
191     ,m_rMutex(_rMutex)
192     ,m_aContainerListeners(_rMutex)
193     ,m_aElementType(_rElementType)
194     ,m_xServiceFactory(_rxFactory)
195 {
196     impl_createEventAttacher_nothrow();
197 }
198 
199 //------------------------------------------------------------------------------
OInterfaceContainer(::osl::Mutex & _rMutex,const OInterfaceContainer & _cloneSource)200 OInterfaceContainer::OInterfaceContainer( ::osl::Mutex& _rMutex, const OInterfaceContainer& _cloneSource )
201     :OInterfaceContainer_BASE()
202     ,m_rMutex( _rMutex )
203     ,m_aContainerListeners( _rMutex )
204     ,m_aElementType( _cloneSource.m_aElementType )
205     ,m_xServiceFactory( _cloneSource.m_xServiceFactory )
206 {
207     impl_createEventAttacher_nothrow();
208 }
209 
210 //------------------------------------------------------------------------------
clonedFrom(const OInterfaceContainer & _cloneSource)211 void OInterfaceContainer::clonedFrom( const OInterfaceContainer& _cloneSource )
212 {
213     try
214     {
215         const Reference< XIndexAccess > xSourceHierarchy( const_cast< OInterfaceContainer* >( &_cloneSource ) );
216         const sal_Int32 nCount = xSourceHierarchy->getCount();
217         for ( sal_Int32 i=0; i<nCount; ++i )
218         {
219             Reference< XCloneable > xCloneable( xSourceHierarchy->getByIndex( i ), UNO_QUERY_THROW );
220             Reference< XInterface > xClone( xCloneable->createClone() );
221             insertByIndex( i, makeAny( xClone ) );
222         }
223     }
224     catch( const Exception& )
225     {
226         throw WrappedTargetException(
227             ::rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Could not clone the given interface hierarchy." ) ),
228             static_cast< XIndexContainer* >( const_cast< OInterfaceContainer* >( &_cloneSource ) ),
229             ::cppu::getCaughtException()
230         );
231     }
232 }
233 
234 //------------------------------------------------------------------------------
impl_createEventAttacher_nothrow()235 void OInterfaceContainer::impl_createEventAttacher_nothrow()
236 {
237     try
238     {
239         m_xEventAttacher.set( ::comphelper::createEventAttacherManager( m_xServiceFactory ), UNO_SET_THROW );
240     }
241     catch( const Exception& )
242     {
243         DBG_UNHANDLED_EXCEPTION();
244     }
245 }
246 
247 //------------------------------------------------------------------------------
~OInterfaceContainer()248 OInterfaceContainer::~OInterfaceContainer()
249 {
250 }
251 
252 //------------------------------------------------------------------------------
disposing()253 void OInterfaceContainer::disposing()
254 {
255     // dispose all elements
256     for (sal_Int32 i = m_aItems.size(); i > 0; --i)
257     {
258         Reference<XPropertySet>  xSet(m_aItems[i - 1], UNO_QUERY);
259         if (xSet.is())
260             xSet->removePropertyChangeListener(PROPERTY_NAME, this);
261 
262         // revoke event knittings
263         if ( m_xEventAttacher.is() )
264         {
265             Reference< XInterface > xIfc( xSet, UNO_QUERY );
266             m_xEventAttacher->detach( i - 1, xIfc );
267             m_xEventAttacher->removeEntry( i - 1 );
268         }
269 
270         Reference<XComponent>  xComponent(xSet, UNO_QUERY);
271         if (xComponent.is())
272             xComponent->dispose();
273     }
274     m_aMap.clear();
275     m_aItems.clear();
276 
277     EventObject aEvt(static_cast<XContainer*>(this));
278     m_aContainerListeners.disposeAndClear(aEvt);
279 }
280 
281 // XPersistObject
282 //------------------------------------------------------------------------------
283 namespace
284 {
285     //..........................................................................
lcl_saveEvents(::std::vector<Sequence<ScriptEventDescriptor>> & _rSave,const Reference<XEventAttacherManager> & _rxManager,const sal_Int32 _nItemCount)286     void lcl_saveEvents( ::std::vector< Sequence< ScriptEventDescriptor > >& _rSave,
287         const Reference< XEventAttacherManager >& _rxManager, const sal_Int32 _nItemCount )
288     {
289         OSL_ENSURE( _rxManager.is(), "lcl_saveEvents: invalid event attacher manager!" );
290         if ( !_rxManager.is() )
291             return;
292 
293         // reserve the space needed
294         _rSave.reserve( _nItemCount );
295 
296         // copy the events
297         for (sal_Int32 i=0; i<_nItemCount; ++i)
298             _rSave.push_back(_rxManager->getScriptEvents( i ));
299     }
300 
301     //..........................................................................
lcl_restoreEvents(const::std::vector<Sequence<ScriptEventDescriptor>> & _rSave,const Reference<XEventAttacherManager> & _rxManager)302     void lcl_restoreEvents( const ::std::vector< Sequence< ScriptEventDescriptor > >& _rSave,
303         const Reference< XEventAttacherManager >& _rxManager )
304     {
305         OSL_ENSURE( _rxManager.is(), "lcl_restoreEvents: invalid event attacher manager!" );
306         if ( !_rxManager.is() )
307             return;
308 
309         ::std::vector< Sequence< ScriptEventDescriptor > >::const_iterator aLoop = _rSave.begin();
310         ::std::vector< Sequence< ScriptEventDescriptor > >::const_iterator aEnd = _rSave.end();
311         for ( sal_Int32 i=0; aLoop != aEnd; ++aLoop, ++i )
312         {
313             _rxManager->revokeScriptEvents( i );
314             _rxManager->registerScriptEvents( i, *aLoop );
315         }
316     }
317 }
318 
319 //------------------------------------------------------------------------------
writeEvents(const Reference<XObjectOutputStream> & _rxOutStream)320 void SAL_CALL OInterfaceContainer::writeEvents(const Reference<XObjectOutputStream>& _rxOutStream)
321 {
322     // We're writing a document in SO 5.2 format (or even from earlier versions)
323     // -> convert the events from the new runtime format to the format of the 5.2 files
324     // but before, remember the current script events set for our children
325     ::std::vector< Sequence< ScriptEventDescriptor > > aSave;
326     if ( m_xEventAttacher.is() )
327         lcl_saveEvents( aSave, m_xEventAttacher, m_aItems.size() );
328 
329     transformEvents( efVersionSO5x );
330 
331     try
332     {
333         Reference<XMarkableStream>  xMark(_rxOutStream, UNO_QUERY);
334         sal_Int32 nMark = xMark->createMark();
335 
336         sal_Int32 nObjLen = 0;
337         _rxOutStream->writeLong(nObjLen);
338 
339         Reference<XPersistObject>  xScripts(m_xEventAttacher, UNO_QUERY);
340         if (xScripts.is())
341             xScripts->write(_rxOutStream);
342 
343         // feststellen der Laenge
344         nObjLen = xMark->offsetToMark(nMark) - 4;
345         xMark->jumpToMark(nMark);
346         _rxOutStream->writeLong(nObjLen);
347         xMark->jumpToFurthest();
348         xMark->deleteMark(nMark);
349     }
350     catch( const Exception& )
351     {
352         // restore the events
353         if ( m_xEventAttacher.is() )
354             lcl_restoreEvents( aSave, m_xEventAttacher );
355         throw;
356     }
357 
358     // restore the events
359     if ( m_xEventAttacher.is() )
360         lcl_restoreEvents( aSave, m_xEventAttacher );
361 }
362 
363 //------------------------------------------------------------------------------
364 struct TransformEventTo52Format
365 {
366     typedef ScriptEventDescriptor argument_type;
367     typedef void result_type;
operator ()frm::TransformEventTo52Format368     void operator()( ScriptEventDescriptor& _rDescriptor )
369     {
370         if ( 0 == _rDescriptor.ScriptType.compareToAscii( "StarBasic" ) )
371         {   // it's a starbasic macro
372             sal_Int32 nPrefixLength = _rDescriptor.ScriptCode.indexOf( ':' );
373             if ( 0 <= nPrefixLength )
374             {   // the macro name does not already contain a :
375 #ifdef DBG_UTIL
376                 const ::rtl::OUString sPrefix = _rDescriptor.ScriptCode.copy( 0, nPrefixLength );
377                 DBG_ASSERT( 0 == sPrefix.compareToAscii( "document" )
378                         ||  0 == sPrefix.compareToAscii( "application" ),
379                         "TransformEventTo52Format: invalid (unknown) prefix!" );
380 #endif
381                 // cut the prefix
382                 _rDescriptor.ScriptCode = _rDescriptor.ScriptCode.copy( nPrefixLength + 1 );
383             }
384         }
385     }
386 };
387 
388 //------------------------------------------------------------------------------
389 struct TransformEventTo60Format
390 {
391     typedef ScriptEventDescriptor argument_type;
392     typedef void result_type;
operator ()frm::TransformEventTo60Format393     void operator()( ScriptEventDescriptor& _rDescriptor )
394     {
395         if ( 0 == _rDescriptor.ScriptType.compareToAscii( "StarBasic" ) )
396         {   // it's a starbasic macro
397             if ( _rDescriptor.ScriptCode.indexOf( ':' ) < 0 )
398             {   // the macro name does not already contain a :
399                 // -> default the type to "document"
400                 ::rtl::OUString sNewScriptCode( RTL_CONSTASCII_USTRINGPARAM( "document:" ) );
401                 sNewScriptCode += _rDescriptor.ScriptCode;
402                 _rDescriptor.ScriptCode = sNewScriptCode;
403             }
404         }
405     }
406 };
407 
408 //------------------------------------------------------------------------------
transformEvents(const EventFormat _eTargetFormat)409 void OInterfaceContainer::transformEvents( const EventFormat _eTargetFormat )
410 {
411     OSL_ENSURE( m_xEventAttacher.is(), "OInterfaceContainer::transformEvents: no event attacher manager!" );
412     if ( !m_xEventAttacher.is() )
413         return;
414 
415     try
416     {
417         // loop through all our children
418         sal_Int32 nItems = m_aItems.size();
419         Sequence< ScriptEventDescriptor > aChildEvents;
420 
421         for (sal_Int32 i=0; i<nItems; ++i)
422         {
423             // get the script events for this object
424             aChildEvents = m_xEventAttacher->getScriptEvents( i );
425 
426             if ( aChildEvents.getLength() )
427             {
428                 // the "iterators" for the events for this child
429                 ScriptEventDescriptor* pChildEvents     =                       aChildEvents.getArray();
430                 ScriptEventDescriptor* pChildEventsEnd  =   pChildEvents    +   aChildEvents.getLength();
431 
432                 // do the transformation
433                 if ( efVersionSO6x == _eTargetFormat )
434                     ::std::for_each( pChildEvents, pChildEventsEnd, TransformEventTo60Format() );
435                 else
436                     ::std::for_each( pChildEvents, pChildEventsEnd, TransformEventTo52Format() );
437 
438                 // revoke the script events
439                 m_xEventAttacher->revokeScriptEvents( i );
440                 // and re-register them
441                 m_xEventAttacher->registerScriptEvents( i, aChildEvents );
442             }
443         }
444     }
445     catch( const Exception& )
446     {
447         DBG_UNHANDLED_EXCEPTION();
448     }
449 }
450 
451 //------------------------------------------------------------------------------
readEvents(const Reference<XObjectInputStream> & _rxInStream)452 void SAL_CALL OInterfaceContainer::readEvents(const Reference<XObjectInputStream>& _rxInStream)
453 {
454     ::osl::MutexGuard aGuard( m_rMutex );
455 
456     // Scripting Info lesen
457     Reference<XMarkableStream>  xMark(_rxInStream, UNO_QUERY);
458     sal_Int32 nObjLen = _rxInStream->readLong();
459     if (nObjLen)
460     {
461         sal_Int32 nMark = xMark->createMark();
462         Reference<XPersistObject>  xObj(m_xEventAttacher, UNO_QUERY);
463         if (xObj.is())
464             xObj->read(_rxInStream);
465         xMark->jumpToMark(nMark);
466         _rxInStream->skipBytes(nObjLen);
467         xMark->deleteMark(nMark);
468     }
469 
470     // Attachement lesen
471     if ( m_xEventAttacher.is() )
472     {
473         OInterfaceArray::const_iterator aAttach = m_aItems.begin();
474         OInterfaceArray::const_iterator aAttachEnd = m_aItems.end();
475         for ( sal_Int32 i=0; aAttach != aAttachEnd; ++aAttach, ++i )
476         {
477             Reference< XInterface > xAsIFace( *aAttach, UNO_QUERY );    // important to normalize this ....
478             Reference< XPropertySet > xAsSet( xAsIFace, UNO_QUERY );
479             m_xEventAttacher->attach( i, xAsIFace, makeAny( xAsSet ) );
480         }
481     }
482 }
483 
484 //------------------------------------------------------------------------------
write(const Reference<XObjectOutputStream> & _rxOutStream)485 void SAL_CALL OInterfaceContainer::write( const Reference< XObjectOutputStream >& _rxOutStream )
486 {
487     ::osl::MutexGuard aGuard( m_rMutex );
488     sal_Int32 nLen = m_aItems.size();
489 
490     // schreiben der laenge
491     _rxOutStream->writeLong(nLen);
492 
493     if (nLen)
494     {
495         // 1. Version
496         _rxOutStream->writeShort(0x0001);
497 
498         // 2. Objekte
499         for (sal_Int32 i = 0; i < nLen; i++)
500         {
501             Reference<XPersistObject>  xObj(m_aItems[i], UNO_QUERY);
502             if (xObj.is())
503                 _rxOutStream->writeObject(xObj);
504             else
505             {
506                 // ::com::sun::star::chaos::Error
507             }
508         }
509 
510         // 3. Scripts
511         writeEvents(_rxOutStream);
512     }
513 }
514 
515 //------------------------------------------------------------------------------
516 namespace
517 {
lcl_createPlaceHolder(const Reference<XMultiServiceFactory> & _rxORB)518     Reference< XPersistObject > lcl_createPlaceHolder( const Reference< XMultiServiceFactory >& _rxORB )
519     {
520         Reference< XPersistObject > xObject( _rxORB->createInstance( FRM_COMPONENT_HIDDENCONTROL ), UNO_QUERY );
521         DBG_ASSERT( xObject.is(), "lcl_createPlaceHolder: could not create a substitute for the unknown object!" );
522         if ( xObject.is() )
523         {
524             // set some properties describing what we did
525             Reference< XPropertySet > xObjProps( xObject, UNO_QUERY );
526             if ( xObject.is()  )
527             {
528                 try
529                 {
530                     xObjProps->setPropertyValue( PROPERTY_NAME, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_NAME ) ) );
531                     xObjProps->setPropertyValue( PROPERTY_TAG, makeAny( FRM_RES_STRING( RID_STR_CONTROL_SUBSTITUTED_EPXPLAIN ) ) );
532                 }
533                 catch(Exception&)
534                 {
535                 }
536             }
537         }
538         return xObject;
539     }
540 }
541 
542 //------------------------------------------------------------------------------
read(const Reference<XObjectInputStream> & _rxInStream)543 void SAL_CALL OInterfaceContainer::read( const Reference< XObjectInputStream >& _rxInStream )
544 {
545     ::osl::MutexGuard aGuard( m_rMutex );
546 
547     // after ::read the object is expected to be in the state it was when ::write was called, so we have
548     // to empty ourself here
549     // FS - 71598 - 12.01.00
550     while (getCount())
551         removeByIndex(0);
552 
553     // Schreibt nur in Abhaengigkeit der Laenge
554     sal_Int32 nLen = _rxInStream->readLong();
555 
556     if (nLen)
557     {
558         // 1. Version
559         sal_uInt16 nVersion = _rxInStream->readShort(); (void)nVersion;
560 
561         // 2. Objekte
562         for (sal_Int32 i = 0; i < nLen; i++)
563         {
564             Reference<XPersistObject>  xObj;
565             try
566             {
567                 xObj = _rxInStream->readObject();
568             }
569             catch(WrongFormatException& e)
570             {
571                 (void)e;    // make compiler happy
572                 // the object could not be read
573                 // create a object (so the readEvents below will assign the events to the right controls)
574                 xObj = lcl_createPlaceHolder( m_xServiceFactory );
575                 if ( !xObj.is() )
576                     // couldn't handle it
577                     throw;
578                 // 72133 - 09.02.00 - FS
579             }
580             catch(Exception&)
581             {
582                 // unsere Map leeren
583                 while (!m_aItems.empty())
584                     removeElementsNoEvents(0);
585 
586                 // und die Exception nach aussen
587                 throw;
588             }
589 
590             if ( xObj.is() )
591             {
592                 Reference< XPropertySet > xElement( xObj, UNO_QUERY );
593                 try
594                 {
595                     implInsert(
596                         m_aItems.size(),    // position
597                         xElement,           // element to insert
598                         sal_False,          // no event attacher manager handling
599                         NULL,               // not yet approved - let implInsert do it
600                         sal_True            // fire the event
601                     );
602                 }
603                 catch( const Exception& )
604                 {
605                     DBG_ERROR( "OInterfaceContainerHelper::read: reading succeeded, but not inserting!" );
606                     // create a placeholder
607                     xElement = xElement.query( lcl_createPlaceHolder( m_xServiceFactory ) );
608                     if ( !xElement.is() )
609                         // couldn't handle it
610                         throw;
611                     // insert the placeholder
612                     implInsert( m_aItems.size(), xElement, sal_False, NULL, sal_True );
613                 }
614             }
615         }
616 
617         readEvents(_rxInStream);
618     }
619     else
620     {
621         try
622         {
623             m_xEventAttacher = ::comphelper::createEventAttacherManager( m_xServiceFactory );
624             OSL_ENSURE( m_xEventAttacher.is(), "OInterfaceContainer::read: could not create an event attacher manager!" );
625         }
626         catch( const Exception& )
627         {
628             DBG_UNHANDLED_EXCEPTION();
629         }
630     }
631 }
632 
633 // XContainer
634 //------------------------------------------------------------------------------
addContainerListener(const Reference<XContainerListener> & _rxListener)635 void SAL_CALL OInterfaceContainer::addContainerListener(const Reference<XContainerListener>& _rxListener)
636 {
637     m_aContainerListeners.addInterface(_rxListener);
638 }
639 
640 //------------------------------------------------------------------------------
removeContainerListener(const Reference<XContainerListener> & _rxListener)641 void SAL_CALL OInterfaceContainer::removeContainerListener(const Reference<XContainerListener>& _rxListener)
642 {
643     m_aContainerListeners.removeInterface(_rxListener);
644 }
645 
646 // XEventListener
647 //------------------------------------------------------------------------------
disposing(const EventObject & _rSource)648 void SAL_CALL OInterfaceContainer::disposing(const EventObject& _rSource)
649 {
650     ::osl::MutexGuard aGuard( m_rMutex );
651 
652     Reference< XInterface > xSource( _rSource.Source, UNO_QUERY );
653         // normalized source
654 
655     OInterfaceArray::iterator j;
656     for ( j = m_aItems.begin(); j != m_aItems.end(); ++j )
657     {
658         DBG_ASSERT( j->get() == Reference< XInterface >( *j, UNO_QUERY ).get(),
659             "OInterfaceContainer::disposing: vector element not normalized!" );
660 
661         if ( xSource.get() == j->get() )
662             // found the element
663             break;
664     }
665 
666     if ( m_aItems.end() != j )
667     {
668         m_aItems.erase(j);
669 
670         // look up in, and erase from, m_aMap, too
671         OInterfaceMap::iterator i = m_aMap.begin();
672         while ( i != m_aMap.end() )
673         {
674             DBG_ASSERT( i->second.get() == Reference< XInterface >( i->second, UNO_QUERY ).get(),
675                 "OInterfaceContainer::disposing: map element not normalized!" );
676 
677             if ( i->second.get() == xSource.get() )
678             {
679                 // found it
680                 m_aMap.erase(i);
681                 break;
682             }
683 
684             ++i;
685 
686             DBG_ASSERT( i != m_aMap.end(), "OInterfaceContainer::disposing: inconsistency: the element was in m_aItems, but not in m_aMap!" );
687         }
688     }
689 }
690 
691 // XPropertyChangeListener
692 //------------------------------------------------------------------------------
propertyChange(const PropertyChangeEvent & evt)693 void OInterfaceContainer::propertyChange(const PropertyChangeEvent& evt) {
694     if (evt.PropertyName == PROPERTY_NAME)
695     {
696         ::osl::MutexGuard aGuard( m_rMutex );
697         OInterfaceMap::iterator i = ::std::find(m_aMap.begin(), m_aMap.end(),
698             ::std::pair<const ::rtl::OUString, InterfaceRef >(::comphelper::getString(evt.OldValue),evt.Source));
699         if (i != m_aMap.end())
700         {
701             InterfaceRef  xCorrectType((*i).second);
702             m_aMap.erase(i);
703             m_aMap.insert(::std::pair<const ::rtl::OUString, InterfaceRef >(::comphelper::getString(evt.NewValue),xCorrectType));
704         }
705     }
706 }
707 
708 // XElementAccess
709 //------------------------------------------------------------------------------
hasElements()710 sal_Bool SAL_CALL OInterfaceContainer::hasElements()
711 {
712     return !m_aMap.empty();
713 }
714 
715 //------------------------------------------------------------------------------
getElementType()716 Type SAL_CALL OInterfaceContainer::getElementType()
717 {
718     return m_aElementType;
719 }
720 
721 // XEnumerationAccess
722 //------------------------------------------------------------------------------
createEnumeration()723 Reference<XEnumeration> SAL_CALL OInterfaceContainer::createEnumeration()
724 {
725     ::osl::MutexGuard aGuard( m_rMutex );
726     return new ::comphelper::OEnumerationByIndex(static_cast<XIndexAccess*>(this));
727 }
728 
729 // XNameAccess
730 //------------------------------------------------------------------------------
getByName(const::rtl::OUString & _rName)731 Any SAL_CALL OInterfaceContainer::getByName( const ::rtl::OUString& _rName )
732 {
733     ::std::pair <OInterfaceMap::iterator,
734           OInterfaceMap::iterator> aPair = m_aMap.equal_range(_rName);
735 
736     if (aPair.first == aPair.second)
737         throw NoSuchElementException();
738 
739     return (*aPair.first).second->queryInterface( m_aElementType );
740 }
741 
742 //------------------------------------------------------------------------------
getElementNames()743 StringSequence SAL_CALL OInterfaceContainer::getElementNames()
744 {
745     StringSequence aNameList(m_aItems.size());
746     ::rtl::OUString* pStringArray = aNameList.getArray();
747 
748     for (OInterfaceMap::const_iterator i = m_aMap.begin(); i != m_aMap.end(); ++i, ++pStringArray)
749     {
750         *pStringArray = (*i).first;
751     }
752     return aNameList;
753 }
754 
755 //------------------------------------------------------------------------------
hasByName(const::rtl::OUString & _rName)756 sal_Bool SAL_CALL OInterfaceContainer::hasByName( const ::rtl::OUString& _rName )
757 {
758     ::std::pair <OInterfaceMap::iterator,
759           OInterfaceMap::iterator> aPair = m_aMap.equal_range(_rName);
760     return aPair.first != aPair.second;
761 }
762 
763 // XIndexAccess
764 //------------------------------------------------------------------------------
getCount()765 sal_Int32 OInterfaceContainer::getCount()
766 {
767     return m_aItems.size();
768 }
769 
770 //------------------------------------------------------------------------------
getByIndex(sal_Int32 _nIndex)771 Any OInterfaceContainer::getByIndex(sal_Int32 _nIndex)
772 {
773     if (_nIndex < 0 || (_nIndex >= (sal_Int32)m_aItems.size()))
774         throw IndexOutOfBoundsException();
775 
776     return m_aItems[_nIndex]->queryInterface( m_aElementType );
777 }
778 
779 //------------------------------------------------------------------------------
approveNewElement(const Reference<XPropertySet> & _rxObject,ElementDescription * _pElement)780 void OInterfaceContainer::approveNewElement( const Reference< XPropertySet >& _rxObject, ElementDescription* _pElement )
781 {
782     // it has to be non-NULL
783     if ( !_rxObject.is() )
784         throw IllegalArgumentException(FRM_RES_STRING(RID_STR_NEED_NON_NULL_OBJECT), static_cast<XContainer*>(this), 1);
785 
786     // it has to support our element type interface
787     Any aCorrectType = _rxObject->queryInterface( m_aElementType );
788     if ( !aCorrectType.hasValue() )
789         lcl_throwIllegalArgumentException();
790 
791     // it has to have a "Name" property
792     if ( !hasProperty( PROPERTY_NAME, _rxObject ) )
793         lcl_throwIllegalArgumentException();
794 
795     // it has to be a child, and it must not have a parent already
796     Reference< XChild > xChild( _rxObject, UNO_QUERY );
797     if ( !xChild.is() || xChild->getParent().is() )
798     {
799 #ifdef FS_PRIV_DEBUG
800         ::rtl::OUString sChildName, sParentName;
801         Reference< XNamed > xNamed( xChild, UNO_QUERY );
802         if ( xNamed.is() )
803             sChildName = xNamed->getName();
804         if ( xChild.is() )
805         {
806             xNamed = xNamed.query( xChild->getParent() );
807             if ( xNamed.is() )
808                 sParentName = xNamed->getName();
809         }
810 #endif
811         lcl_throwIllegalArgumentException();
812     }
813 
814     // passed all tests. cache the information we have so far
815     DBG_ASSERT( _pElement, "OInterfaceContainer::approveNewElement: invalid event descriptor!" );
816     if ( _pElement )
817     {
818         _pElement->xPropertySet = _rxObject;
819         _pElement->xChild = xChild;
820         _pElement->aElementTypeInterface = aCorrectType;
821         _pElement->xInterface = Reference< XInterface >( _rxObject, UNO_QUERY );    // normalized XInterface
822     }
823 }
824 
825 //------------------------------------------------------------------------------
implInsert(sal_Int32 _nIndex,const Reference<XPropertySet> & _rxElement,sal_Bool _bEvents,ElementDescription * _pApprovalResult,sal_Bool _bFire)826 void OInterfaceContainer::implInsert(sal_Int32 _nIndex, const Reference< XPropertySet >& _rxElement,
827     sal_Bool _bEvents, ElementDescription* _pApprovalResult, sal_Bool _bFire )
828 {
829     const bool bHandleEvents = _bEvents && m_xEventAttacher.is();
830 
831     // SYNCHRONIZED ----->
832     ::osl::ClearableMutexGuard aGuard( m_rMutex );
833 
834     ::std::unique_ptr< ElementDescription > aAutoDeleteMetaData;
835     ElementDescription* pElementMetaData = _pApprovalResult;
836     if ( !pElementMetaData )
837     {   // not yet approved by the caller -> do ourself
838         pElementMetaData = createElementMetaData();
839         DBG_ASSERT( pElementMetaData, "OInterfaceContainer::implInsert: createElementMetaData returned nonsense!" );
840 
841         // ensure that the meta data structure will be deleted later on
842         aAutoDeleteMetaData = ::std::unique_ptr< ElementDescription >( pElementMetaData );
843 
844         // will throw an exception if necessary
845         approveNewElement( _rxElement, pElementMetaData );
846     }
847 
848 
849     // approveNewElement (no matter if called here or outside) has ensure that all relevant interfaces
850     // exist
851 
852     // set the name, and add as change listener for the name
853     ::rtl::OUString sName;
854     _rxElement->getPropertyValue(PROPERTY_NAME) >>= sName;
855     _rxElement->addPropertyChangeListener(PROPERTY_NAME, this);
856 
857     // insert the object into our internal structures
858     if (_nIndex > (sal_Int32)m_aItems.size()) // ermitteln des tatsaechlichen Indexs
859     {
860         _nIndex = m_aItems.size();
861         m_aItems.push_back( pElementMetaData->xInterface );
862     }
863     else
864         m_aItems.insert( m_aItems.begin() + _nIndex, pElementMetaData->xInterface );
865 
866     m_aMap.insert( ::std::pair< const ::rtl::OUString, InterfaceRef >( sName, pElementMetaData->xInterface ) );
867 
868     // announce ourself as parent to the new element
869     pElementMetaData->xChild->setParent(static_cast<XContainer*>(this));
870 
871     // handle the events
872     if ( bHandleEvents )
873     {
874         m_xEventAttacher->insertEntry(_nIndex);
875         m_xEventAttacher->attach( _nIndex, pElementMetaData->xInterface, makeAny( _rxElement ) );
876     }
877 
878     // notify derived classes
879     implInserted( pElementMetaData );
880 
881     aGuard.clear();
882     // <----- SYNCHRONIZED
883 
884     // insert faked VBA events?
885     if ( bHandleEvents )
886     {
887         Reference< XEventAttacherManager > xMgr ( pElementMetaData->xInterface, UNO_QUERY );
888         if ( xMgr.is() )
889         {
890             OInterfaceContainer* pIfcMgr = dynamic_cast< OInterfaceContainer* >( xMgr.get() );
891             sal_Int32 nLen = pIfcMgr->getCount();
892             for ( sal_Int32 i = 0; (i < nLen) && pIfcMgr ; ++i )
893             {
894                 // add fake events to the control at index i
895                 pIfcMgr->impl_addVbEvents_nolck_nothrow( i );
896             }
897         }
898         else
899         {
900             // add fake events to the control at index i
901             impl_addVbEvents_nolck_nothrow( _nIndex );
902         }
903     }
904 
905     // fire the notification about the change
906     if ( _bFire )
907     {
908         // notify listeners
909         ContainerEvent aEvt;
910         aEvt.Source   = static_cast<XContainer*>(this);
911         aEvt.Accessor <<= _nIndex;
912         aEvt.Element  = pElementMetaData->aElementTypeInterface;
913 
914         aGuard.clear();
915         m_aContainerListeners.notifyEach( &XContainerListener::elementInserted, aEvt );
916     }
917 }
918 
919 //------------------------------------------------------------------------------
removeElementsNoEvents(sal_Int32 nIndex)920 void OInterfaceContainer::removeElementsNoEvents(sal_Int32 nIndex)
921 {
922     OInterfaceArray::iterator i = m_aItems.begin() + nIndex;
923     InterfaceRef  xElement(*i);
924 
925     OInterfaceMap::iterator j = m_aMap.begin();
926     while (j != m_aMap.end() && (*j).second != xElement) ++j;
927 
928     m_aItems.erase(i);
929     m_aMap.erase(j);
930 
931     Reference<XPropertySet>  xSet(xElement, UNO_QUERY);
932     if (xSet.is())
933         xSet->removePropertyChangeListener(PROPERTY_NAME, this);
934 
935     Reference<XChild>  xChild(xElement, UNO_QUERY);
936     if (xChild.is())
937         xChild->setParent(InterfaceRef ());
938 }
939 
940 //------------------------------------------------------------------------------
implInserted(const ElementDescription *)941 void OInterfaceContainer::implInserted( const ElementDescription* /*_pElement*/ )
942 {
943     // not inrerested in
944 }
945 
946 //------------------------------------------------------------------------------
implRemoved(const InterfaceRef &)947 void OInterfaceContainer::implRemoved( const InterfaceRef& /*_rxObject*/ )
948 {
949     // not inrerested in
950 }
951 
952 //------------------------------------------------------------------------------
impl_replacedElement(const ContainerEvent & _rEvent,::osl::ClearableMutexGuard & _rInstanceLock)953 void OInterfaceContainer::impl_replacedElement( const ContainerEvent& _rEvent, ::osl::ClearableMutexGuard& _rInstanceLock )
954 {
955     _rInstanceLock.clear();
956     m_aContainerListeners.notifyEach( &XContainerListener::elementReplaced, _rEvent );
957 }
958 
959 // XIndexContainer
960 //------------------------------------------------------------------------------
insertByIndex(sal_Int32 _nIndex,const Any & _rElement)961 void SAL_CALL OInterfaceContainer::insertByIndex( sal_Int32 _nIndex, const Any& _rElement )
962 {
963     Reference< XPropertySet > xElement;
964     _rElement >>= xElement;
965     implInsert( _nIndex, xElement, sal_True /* event handling */ , NULL /* not yet approved */ , sal_True /* notification */ );
966 }
967 
968 //------------------------------------------------------------------------------
implReplaceByIndex(const sal_Int32 _nIndex,const Any & _rNewElement,::osl::ClearableMutexGuard & _rClearBeforeNotify)969 void OInterfaceContainer::implReplaceByIndex( const sal_Int32 _nIndex, const Any& _rNewElement, ::osl::ClearableMutexGuard& _rClearBeforeNotify )
970 {
971     OSL_PRECOND( ( _nIndex >= 0 ) && ( _nIndex < (sal_Int32)m_aItems.size() ), "OInterfaceContainer::implReplaceByIndex: precondition not met (index)!" );
972 
973     // approve the new object
974     ::std::unique_ptr< ElementDescription > aElementMetaData( createElementMetaData() );
975     DBG_ASSERT( aElementMetaData.get(), "OInterfaceContainer::implReplaceByIndex: createElementMetaData returned nonsense!" );
976     {
977         Reference< XPropertySet > xElementProps;
978         _rNewElement >>= xElementProps;
979         approveNewElement( xElementProps, aElementMetaData.get() );
980     }
981 
982     // get the old element
983     InterfaceRef  xOldElement( m_aItems[ _nIndex ] );
984     DBG_ASSERT( xOldElement.get() == Reference< XInterface >( xOldElement, UNO_QUERY ).get(),
985         "OInterfaceContainer::implReplaceByIndex: elements should be held normalized!" );
986 
987     // locate the old element in the map
988     OInterfaceMap::iterator j = m_aMap.begin();
989     while ( ( j != m_aMap.end() ) && ( j->second.get() != xOldElement.get() ) )
990         ++j;
991 
992     // remove event knittings
993     if ( m_xEventAttacher.is() )
994     {
995         InterfaceRef xNormalized( xOldElement, UNO_QUERY );
996         m_xEventAttacher->detach( _nIndex, xNormalized );
997         m_xEventAttacher->removeEntry( _nIndex );
998     }
999 
1000     // don't listen for property changes anymore
1001     Reference<XPropertySet>  xSet( xOldElement, UNO_QUERY );
1002     if (xSet.is())
1003         xSet->removePropertyChangeListener(PROPERTY_NAME, this);
1004 
1005     // give the old element a new (void) parent
1006     Reference<XChild>  xChild(xOldElement, UNO_QUERY);
1007     if (xChild.is())
1008         xChild->setParent(InterfaceRef ());
1009 
1010     // remove the old one
1011     m_aMap.erase(j);
1012 
1013     // examine the new element
1014     ::rtl::OUString sName;
1015     DBG_ASSERT( aElementMetaData.get()->xPropertySet.is(), "OInterfaceContainer::implReplaceByIndex: what did approveNewElement do?" );
1016 
1017     aElementMetaData.get()->xPropertySet->getPropertyValue(PROPERTY_NAME) >>= sName;
1018     aElementMetaData.get()->xPropertySet->addPropertyChangeListener(PROPERTY_NAME, this);
1019 
1020     // insert the new one
1021     m_aMap.insert( ::std::pair<const ::rtl::OUString, InterfaceRef  >( sName, aElementMetaData.get()->xInterface ) );
1022     m_aItems[ _nIndex ] = aElementMetaData.get()->xInterface;
1023 
1024     aElementMetaData.get()->xChild->setParent(static_cast<XContainer*>(this));
1025 
1026     if ( m_xEventAttacher.is() )
1027     {
1028         m_xEventAttacher->insertEntry( _nIndex );
1029         m_xEventAttacher->attach( _nIndex, aElementMetaData.get()->xInterface, makeAny( aElementMetaData.get()->xPropertySet ) );
1030     }
1031 
1032     ContainerEvent aReplaceEvent;
1033     aReplaceEvent.Source   = static_cast< XContainer* >( this );
1034     aReplaceEvent.Accessor <<= _nIndex;
1035     aReplaceEvent.Element  = aElementMetaData.get()->xInterface->queryInterface( m_aElementType );
1036     aReplaceEvent.ReplacedElement = xOldElement->queryInterface( m_aElementType );
1037 
1038     impl_replacedElement( aReplaceEvent, _rClearBeforeNotify );
1039 }
1040 
1041 //------------------------------------------------------------------------------
implCheckIndex(const sal_Int32 _nIndex)1042 void OInterfaceContainer::implCheckIndex( const sal_Int32 _nIndex )
1043 {
1044     if (_nIndex < 0 || _nIndex >= (sal_Int32)m_aItems.size())
1045         throw IndexOutOfBoundsException();
1046 }
1047 
1048 //------------------------------------------------------------------------------
replaceByIndex(sal_Int32 _nIndex,const Any & Element)1049 void SAL_CALL OInterfaceContainer::replaceByIndex(sal_Int32 _nIndex, const Any& Element)
1050 {
1051     ::osl::ClearableMutexGuard aGuard( m_rMutex );
1052     // check the index
1053     implCheckIndex( _nIndex );
1054     // do the replace
1055     implReplaceByIndex( _nIndex, Element, aGuard );
1056 }
1057 
1058 //------------------------------------------------------------------------------
implRemoveByIndex(const sal_Int32 _nIndex,::osl::ClearableMutexGuard & _rClearBeforeNotify)1059 void OInterfaceContainer::implRemoveByIndex( const sal_Int32 _nIndex, ::osl::ClearableMutexGuard& _rClearBeforeNotify )
1060 {
1061     OSL_PRECOND( ( _nIndex >= 0 ) && ( _nIndex < (sal_Int32)m_aItems.size() ), "OInterfaceContainer::implRemoveByIndex: precondition not met (index)!" );
1062 
1063     OInterfaceArray::iterator i = m_aItems.begin() + _nIndex;
1064     InterfaceRef  xElement(*i);
1065 
1066     OInterfaceMap::iterator j = m_aMap.begin();
1067     while (j != m_aMap.end() && (*j).second != xElement) ++j;
1068 
1069     m_aItems.erase(i);
1070     m_aMap.erase(j);
1071 
1072     // remove event knittings
1073     if ( m_xEventAttacher.is() )
1074     {
1075         InterfaceRef xNormalized( xElement, UNO_QUERY );
1076         m_xEventAttacher->detach( _nIndex, xNormalized );
1077         m_xEventAttacher->removeEntry( _nIndex );
1078     }
1079 
1080     Reference<XPropertySet>  xSet(xElement, UNO_QUERY);
1081     if (xSet.is())
1082         xSet->removePropertyChangeListener(PROPERTY_NAME, this);
1083 
1084     Reference<XChild>  xChild(xElement, UNO_QUERY);
1085     if (xChild.is())
1086         xChild->setParent(InterfaceRef ());
1087 
1088     // notify derived classes
1089     implRemoved(xElement);
1090 
1091     // notify listeners
1092     ContainerEvent aEvt;
1093     aEvt.Source     = static_cast<XContainer*>(this);
1094     aEvt.Element    = xElement->queryInterface( m_aElementType );
1095     aEvt.Accessor   <<= _nIndex;
1096 
1097     _rClearBeforeNotify.clear();
1098     m_aContainerListeners.notifyEach( &XContainerListener::elementRemoved, aEvt );
1099 }
1100 
1101 //------------------------------------------------------------------------------
removeByIndex(sal_Int32 _nIndex)1102 void SAL_CALL OInterfaceContainer::removeByIndex(sal_Int32 _nIndex)
1103 {
1104     ::osl::ClearableMutexGuard aGuard( m_rMutex );
1105     // check the index
1106     implCheckIndex( _nIndex );
1107     // do the removal
1108     implRemoveByIndex( _nIndex, aGuard );
1109 }
1110 
1111 //------------------------------------------------------------------------
createElementMetaData()1112 ElementDescription* OInterfaceContainer::createElementMetaData( )
1113 {
1114     return new ElementDescription;
1115 }
1116 
1117 //------------------------------------------------------------------------
insertByName(const::rtl::OUString & _rName,const Any & _rElement)1118 void SAL_CALL OInterfaceContainer::insertByName(const ::rtl::OUString& _rName, const Any& _rElement)
1119 {
1120     Reference< XPropertySet > xElementProps;
1121 
1122     ::std::unique_ptr< ElementDescription > aElementMetaData( createElementMetaData() );
1123     DBG_ASSERT( aElementMetaData.get(), "OInterfaceContainer::insertByName: createElementMetaData returned nonsense!" );
1124 
1125     // ensure the correct name of the element
1126     try
1127     {
1128         _rElement >>= xElementProps;
1129         approveNewElement( xElementProps, aElementMetaData.get() );
1130 
1131         xElementProps->setPropertyValue( PROPERTY_NAME, makeAny( _rName ) );
1132     }
1133     catch( const IllegalArgumentException& )
1134     {
1135         throw;  // allowed to leave
1136     }
1137     catch( const ElementExistException& )
1138     {
1139         throw;  // allowed to leave
1140     }
1141     catch( const Exception& )
1142     {
1143         DBG_ERROR( "OInterfaceContainer::insertByName: caught an exception!" );
1144     }
1145     implInsert( m_aItems.size(), xElementProps, sal_True, aElementMetaData.get(), sal_True );
1146 }
1147 
1148 //------------------------------------------------------------------------
replaceByName(const::rtl::OUString & Name,const Any & Element)1149 void SAL_CALL OInterfaceContainer::replaceByName(const ::rtl::OUString& Name, const Any& Element)
1150 {
1151     ::osl::ClearableMutexGuard aGuard( m_rMutex );
1152     ::std::pair <OInterfaceMap::iterator,
1153           OInterfaceMap::iterator> aPair = m_aMap.equal_range(Name);
1154     if (aPair.first == aPair.second)
1155         throw NoSuchElementException();
1156 
1157     if (Element.getValueType().getTypeClass() != TypeClass_INTERFACE)
1158         lcl_throwIllegalArgumentException();
1159 
1160     Reference<XPropertySet> xSet;
1161     Element >>= xSet;
1162     if (xSet.is())
1163     {
1164         if (!hasProperty(PROPERTY_NAME, xSet))
1165             lcl_throwIllegalArgumentException();
1166 
1167         xSet->setPropertyValue(PROPERTY_NAME, makeAny(Name));
1168     }
1169 
1170     // determine the element pos
1171     sal_Int32 nPos = ::std::find(m_aItems.begin(), m_aItems.end(), (*aPair.first).second) - m_aItems.begin();
1172 
1173     implReplaceByIndex( nPos, Element, aGuard );
1174 }
1175 
1176 //------------------------------------------------------------------------
removeByName(const::rtl::OUString & Name)1177 void SAL_CALL OInterfaceContainer::removeByName(const ::rtl::OUString& Name)
1178 {
1179     ::osl::MutexGuard aGuard( m_rMutex );
1180     ::std::pair <OInterfaceMap::iterator,
1181           OInterfaceMap::iterator> aPair = m_aMap.equal_range(Name);
1182     if (aPair.first == aPair.second)
1183         throw NoSuchElementException();
1184 
1185     sal_Int32 nPos = ::std::find(m_aItems.begin(), m_aItems.end(), (*aPair.first).second) - m_aItems.begin();
1186     removeByIndex(nPos);
1187 }
1188 
1189 
1190 // XEventAttacherManager
1191 //------------------------------------------------------------------------
registerScriptEvent(sal_Int32 nIndex,const ScriptEventDescriptor & aScriptEvent)1192 void SAL_CALL OInterfaceContainer::registerScriptEvent( sal_Int32 nIndex, const ScriptEventDescriptor& aScriptEvent )
1193 {
1194     ::osl::ClearableMutexGuard aGuard( m_rMutex );
1195     if ( m_xEventAttacher.is() )
1196     {
1197         m_xEventAttacher->registerScriptEvent( nIndex, aScriptEvent );
1198         aGuard.clear();
1199         impl_addVbEvents_nolck_nothrow( nIndex ); // add fake vba events
1200     }
1201 }
1202 
1203 //------------------------------------------------------------------------
registerScriptEvents(sal_Int32 nIndex,const Sequence<ScriptEventDescriptor> & aScriptEvents)1204 void SAL_CALL OInterfaceContainer::registerScriptEvents( sal_Int32 nIndex, const Sequence< ScriptEventDescriptor >& aScriptEvents )
1205 {
1206     ::osl::ClearableMutexGuard aGuard( m_rMutex );
1207     if ( m_xEventAttacher.is() )
1208     {
1209         m_xEventAttacher->registerScriptEvents( nIndex, aScriptEvents );
1210         aGuard.clear();
1211         impl_addVbEvents_nolck_nothrow( nIndex ); // add fake vba events
1212     }
1213 }
1214 
1215 //------------------------------------------------------------------------
revokeScriptEvent(sal_Int32 nIndex,const::rtl::OUString & aListenerType,const::rtl::OUString & aEventMethod,const::rtl::OUString & aRemoveListenerParam)1216 void SAL_CALL OInterfaceContainer::revokeScriptEvent( sal_Int32 nIndex, const ::rtl::OUString& aListenerType, const ::rtl::OUString& aEventMethod, const ::rtl::OUString& aRemoveListenerParam )
1217 {
1218     if ( m_xEventAttacher.is() )
1219         m_xEventAttacher->revokeScriptEvent( nIndex, aListenerType, aEventMethod, aRemoveListenerParam );
1220 }
1221 
1222 //------------------------------------------------------------------------
revokeScriptEvents(sal_Int32 nIndex)1223 void SAL_CALL OInterfaceContainer::revokeScriptEvents( sal_Int32 nIndex )
1224 {
1225     if ( m_xEventAttacher.is() )
1226         m_xEventAttacher->revokeScriptEvents( nIndex );
1227 }
1228 
1229 //------------------------------------------------------------------------
insertEntry(sal_Int32 nIndex)1230 void SAL_CALL OInterfaceContainer::insertEntry( sal_Int32 nIndex )
1231 {
1232     if ( m_xEventAttacher.is() )
1233         m_xEventAttacher->insertEntry( nIndex );
1234 }
1235 
1236 //------------------------------------------------------------------------
removeEntry(sal_Int32 nIndex)1237 void SAL_CALL OInterfaceContainer::removeEntry( sal_Int32 nIndex )
1238 {
1239     if ( m_xEventAttacher.is() )
1240         m_xEventAttacher->removeEntry( nIndex );
1241 }
1242 
1243 //------------------------------------------------------------------------
getScriptEvents(sal_Int32 nIndex)1244 Sequence< ScriptEventDescriptor > SAL_CALL OInterfaceContainer::getScriptEvents( sal_Int32 nIndex )
1245 {
1246     Sequence< ScriptEventDescriptor > aReturn;
1247     if ( m_xEventAttacher.is() )
1248     {
1249         aReturn = m_xEventAttacher->getScriptEvents( nIndex );
1250             if ( lcl_hasVbaEvents( aReturn ) )
1251             {
1252                 aReturn = lcl_stripVbaEvents( aReturn );
1253             }
1254     }
1255     return aReturn;
1256 }
1257 
1258 //------------------------------------------------------------------------
attach(sal_Int32 nIndex,const Reference<XInterface> & xObject,const Any & aHelper)1259 void SAL_CALL OInterfaceContainer::attach( sal_Int32 nIndex, const Reference< XInterface >& xObject, const Any& aHelper )
1260 {
1261     if ( m_xEventAttacher.is() )
1262         m_xEventAttacher->attach( nIndex, xObject, aHelper );
1263 }
1264 
1265 //------------------------------------------------------------------------
detach(sal_Int32 nIndex,const Reference<XInterface> & xObject)1266 void SAL_CALL OInterfaceContainer::detach( sal_Int32 nIndex, const Reference< XInterface >& xObject )
1267 {
1268     if ( m_xEventAttacher.is() )
1269         m_xEventAttacher->detach( nIndex, xObject );
1270 }
1271 
1272 //------------------------------------------------------------------------
addScriptListener(const Reference<XScriptListener> & xListener)1273 void SAL_CALL OInterfaceContainer::addScriptListener( const Reference< XScriptListener >& xListener )
1274 {
1275     if ( m_xEventAttacher.is() )
1276         m_xEventAttacher->addScriptListener( xListener );
1277 }
1278 
1279 //------------------------------------------------------------------------
removeScriptListener(const Reference<XScriptListener> & xListener)1280 void SAL_CALL OInterfaceContainer::removeScriptListener( const Reference< XScriptListener >& xListener )
1281 {
1282     if ( m_xEventAttacher.is() )
1283         m_xEventAttacher->removeScriptListener( xListener );
1284 }
1285 
1286 //==================================================================
1287 //= OFormComponents
1288 //==================================================================
1289 //------------------------------------------------------------------------------
queryAggregation(const Type & _rType)1290 Any SAL_CALL OFormComponents::queryAggregation(const Type& _rType)
1291 {
1292     Any aReturn = OFormComponents_BASE::queryInterface(_rType);
1293     if (!aReturn.hasValue())
1294     {
1295         aReturn = OInterfaceContainer::queryInterface(_rType);
1296 
1297         if (!aReturn.hasValue())
1298             aReturn = FormComponentsBase::queryAggregation(_rType);
1299     }
1300 
1301     return aReturn;
1302 }
1303 
1304 //------------------------------------------------------------------
getTypes()1305 Sequence<Type> SAL_CALL OFormComponents::getTypes()
1306 {
1307     return ::comphelper::concatSequences(OInterfaceContainer::getTypes(), FormComponentsBase::getTypes(), OFormComponents_BASE::getTypes());
1308 }
1309 
1310 //------------------------------------------------------------------------------
OFormComponents(const Reference<XMultiServiceFactory> & _rxFactory)1311 OFormComponents::OFormComponents(const Reference<XMultiServiceFactory>& _rxFactory)
1312     :FormComponentsBase( m_aMutex )
1313     ,OInterfaceContainer( _rxFactory, m_aMutex, XFormComponent::static_type() )
1314     ,OFormComponents_BASE()
1315 {
1316 }
1317 
1318 //------------------------------------------------------------------------------
OFormComponents(const OFormComponents & _cloneSource)1319 OFormComponents::OFormComponents( const OFormComponents& _cloneSource )
1320     :FormComponentsBase( m_aMutex )
1321     ,OInterfaceContainer( m_aMutex, _cloneSource )
1322     ,OFormComponents_BASE()
1323 {
1324 }
1325 
1326 //------------------------------------------------------------------------------
~OFormComponents()1327 OFormComponents::~OFormComponents()
1328 {
1329     if (!FormComponentsBase::rBHelper.bDisposed)
1330     {
1331         acquire();
1332         dispose();
1333     }
1334 }
1335 
1336 // OComponentHelper
1337 //------------------------------------------------------------------------------
disposing()1338 void OFormComponents::disposing()
1339 {
1340     OInterfaceContainer::disposing();
1341     FormComponentsBase::disposing();
1342     m_xParent = NULL;
1343 }
1344 
1345 //XChild
1346 //------------------------------------------------------------------------------
setParent(const InterfaceRef & Parent)1347 void OFormComponents::setParent(const InterfaceRef& Parent)
1348 {
1349     ::osl::MutexGuard aGuard( m_aMutex );
1350     m_xParent = Parent;
1351 }
1352 
1353 //------------------------------------------------------------------------------
getParent()1354 InterfaceRef OFormComponents::getParent()
1355 {
1356     return m_xParent;
1357 }
1358 
1359 //.........................................................................
1360 }   // namespace frm
1361 //.........................................................................
1362