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_comphelper.hxx"
26 #include <comphelper/listenernotification.hxx>
27 
28 /** === begin UNO includes === **/
29 #include <com/sun/star/lang/DisposedException.hpp>
30 /** === end UNO includes === **/
31 
32 //........................................................................
33 namespace comphelper
34 {
35 //........................................................................
36 
37     using namespace ::com::sun::star::uno;
38     using namespace ::com::sun::star::lang;
39 
40 	//====================================================================
41 	//= OListenerContainer
42 	//====================================================================
43 	//--------------------------------------------------------------------
OListenerContainer(::osl::Mutex & _rMutex)44     OListenerContainer::OListenerContainer( ::osl::Mutex& _rMutex )
45         :m_aListeners( _rMutex )
46     {
47     }
48 
~OListenerContainer()49     OListenerContainer::~OListenerContainer() {}
50 
51 	//--------------------------------------------------------------------
impl_addListener(const Reference<XEventListener> & _rxListener)52     void OListenerContainer::impl_addListener( const Reference< XEventListener >& _rxListener )
53     {
54         OSL_PRECOND( _rxListener.is(), "OListenerContainer::impl_addListener: a NULL listener?!" );
55         if ( _rxListener.is() )
56             m_aListeners.addInterface( _rxListener );
57     }
58 
59 	//--------------------------------------------------------------------
impl_removeListener(const Reference<XEventListener> & _rxListener)60     void OListenerContainer::impl_removeListener( const Reference< XEventListener >& _rxListener )
61     {
62 #if OSL_DEBUG_LEVEL > 0
63 		::cppu::OInterfaceIteratorHelper aIter( m_aListeners );
64         bool bFound = false;
65 		while ( aIter.hasMoreElements() && !bFound )
66 		{
67             bFound = ( Reference< XInterface >( aIter.next() ) == _rxListener );
68         }
69         OSL_ENSURE( bFound, "OListenerContainer::impl_removeListener: sure your listener handling is correct? The given listener is not registered!" );
70 #endif
71         m_aListeners.removeInterface( _rxListener );
72     }
73 
74 	//--------------------------------------------------------------------
disposing(const EventObject & _rEventSource)75     void OListenerContainer::disposing( const EventObject& _rEventSource )
76     {
77         m_aListeners.disposeAndClear( _rEventSource );
78     }
79 
80 	//--------------------------------------------------------------------
clear()81     void OListenerContainer::clear()
82     {
83         m_aListeners.clear();
84     }
85 
86 	//--------------------------------------------------------------------
impl_notify(const EventObject & _rEvent)87     bool OListenerContainer::impl_notify( const EventObject& _rEvent ) SAL_THROW(( Exception ))
88     {
89 		::cppu::OInterfaceIteratorHelper aIter( m_aListeners );
90         bool bCancelled = false;
91 		while ( aIter.hasMoreElements() && !bCancelled )
92 		{
93             Reference< XEventListener > xListener( static_cast< XEventListener* >( aIter.next() ) );
94             if ( !xListener.is() )
95                 continue;
96 
97             try
98             {
99                 bCancelled = !implNotify( xListener, _rEvent );
100             }
101             catch( const DisposedException& e )
102             {
103                 // DisposedExceptions from the listener might indicate a
104                 // broken connection to a different environment.
105 
106                 OSL_ENSURE( e.Context.is(), "OListenerContainer::impl_notify: caught dispose exception with empty Context field" );
107 
108                 // If the exception stems from the listener then remove it
109                 // from the list of listeners.  If the Context field of the
110                 // exception is empty this is interpreted to indicate the
111                 // listener as well.
112                 if ( e.Context == xListener || !e.Context.is() )
113                     aIter.remove();
114             }
115 		}
116 
117         return !bCancelled;
118     }
119 
120 //........................................................................
121 }   // namespace comphelper
122 //........................................................................
123 
124