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