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