1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: listenernotification.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 // MARKER(update_precomp.py): autogen include statement, do not remove
32 #include "precompiled_comphelper.hxx"
33 #include <comphelper/listenernotification.hxx>
35 /** === begin UNO includes === **/
36 #include <com/sun/star/lang/DisposedException.hpp>
37 /** === end UNO includes === **/
39 //........................................................................
42 //........................................................................
44 using namespace ::com::sun::star::uno
;
45 using namespace ::com::sun::star::lang
;
47 //====================================================================
48 //= OListenerContainer
49 //====================================================================
50 //--------------------------------------------------------------------
51 OListenerContainer::OListenerContainer( ::osl::Mutex
& _rMutex
)
52 :m_aListeners( _rMutex
)
56 OListenerContainer::~OListenerContainer() {}
58 //--------------------------------------------------------------------
59 void OListenerContainer::impl_addListener( const Reference
< XEventListener
>& _rxListener
)
61 OSL_PRECOND( _rxListener
.is(), "OListenerContainer::impl_addListener: a NULL listener?!" );
62 if ( _rxListener
.is() )
63 m_aListeners
.addInterface( _rxListener
);
66 //--------------------------------------------------------------------
67 void OListenerContainer::impl_removeListener( const Reference
< XEventListener
>& _rxListener
)
69 #if OSL_DEBUG_LEVEL > 0
70 ::cppu::OInterfaceIteratorHelper
aIter( m_aListeners
);
72 while ( aIter
.hasMoreElements() && !bFound
)
74 bFound
= ( Reference
< XInterface
>( aIter
.next() ) == _rxListener
);
76 OSL_ENSURE( bFound
, "OListenerContainer::impl_removeListener: sure your listener handling is correct? The given listener is not registered!" );
78 m_aListeners
.removeInterface( _rxListener
);
81 //--------------------------------------------------------------------
82 void OListenerContainer::disposing( const EventObject
& _rEventSource
)
84 m_aListeners
.disposeAndClear( _rEventSource
);
87 //--------------------------------------------------------------------
88 void OListenerContainer::clear()
93 //--------------------------------------------------------------------
94 bool OListenerContainer::impl_notify( const EventObject
& _rEvent
) SAL_THROW(( Exception
))
96 ::cppu::OInterfaceIteratorHelper
aIter( m_aListeners
);
97 bool bCancelled
= false;
98 while ( aIter
.hasMoreElements() && !bCancelled
)
100 Reference
< XEventListener
> xListener( static_cast< XEventListener
* >( aIter
.next() ) );
101 if ( !xListener
.is() )
106 bCancelled
= !implNotify( xListener
, _rEvent
);
108 catch( const DisposedException
& e
)
110 // DisposedExceptions from the listener might indicate a
111 // broken connection to a different environment.
113 OSL_ENSURE( e
.Context
.is(), "OListenerContainer::impl_notify: caught dispose exception with empty Context field" );
115 // If the exception stems from the listener then remove it
116 // from the list of listeners. If the Context field of the
117 // exception is empty this is interpreted to indicate the
119 if ( e
.Context
== xListener
|| !e
.Context
.is() )
127 //........................................................................
128 } // namespace comphelper
129 //........................................................................