xref: /trunk/main/svl/source/notify/lstner.cxx (revision 1ecadb572e7010ff3b3382ad9bf179dbc6efadbb)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_svl.hxx"
30 #ifndef GCC
31 #endif
32 
33 #ifndef DEBUG_HXX
34 #include <tools/debug.hxx>
35 #endif
36 
37 #include <svl/hint.hxx>
38 #include <svl/brdcst.hxx>
39 
40 SV_DECL_PTRARR( SfxBroadcasterArr_Impl, SfxBroadcaster*, 0, 2 )
41 
42 #define _SFX_LSTNER_CXX
43 #include <svl/lstner.hxx>
44 
45 //====================================================================
46 DBG_NAME(SfxListener)
47 TYPEINIT0(SfxListener);
48 
49 //====================================================================
50 // simple ctor of class SfxListener
51 
52 SfxListener::SfxListener()
53 {
54     DBG_CTOR(SfxListener, 0);
55 }
56 //--------------------------------------------------------------------
57 
58 // copy ctor of class SfxListener
59 
60 SfxListener::SfxListener( const SfxListener &rListener )
61 {
62     DBG_CTOR(SfxListener, 0);
63 
64     for ( sal_uInt16 n = 0; n < rListener.aBCs.Count(); ++n )
65         StartListening( *rListener.aBCs[n] );
66 }
67 //--------------------------------------------------------------------
68 
69 // unregisteres the SfxListener from its SfxBroadcasters
70 
71 SfxListener::~SfxListener()
72 {
73     DBG_DTOR(SfxListener, 0);
74 
75     // unregister at all remainding broadcasters
76     for ( sal_uInt16 nPos = 0; nPos < aBCs.Count(); ++nPos )
77     {
78         SfxBroadcaster *pBC = aBCs[nPos];
79         pBC->RemoveListener(*this);
80     }
81 }
82 
83 //--------------------------------------------------------------------
84 
85 // unregisteres at a specific SfxBroadcaster
86 
87 void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBC )
88 {
89     DBG_CHKTHIS(SfxListener, 0);
90 
91     const SfxBroadcaster *pBC = &rBC;
92     aBCs.Remove( aBCs.GetPos(pBC), 1 );
93 }
94 
95 //--------------------------------------------------------------------
96 
97 // registeres at a specific SfxBroadcaster
98 
99 sal_Bool SfxListener::StartListening( SfxBroadcaster& rBroadcaster, sal_Bool bPreventDups )
100 {
101     DBG_CHKTHIS(SfxListener, 0);
102 
103     if ( !bPreventDups || !IsListening( rBroadcaster ) )
104     {
105         if ( rBroadcaster.AddListener(*this) )
106         {
107             const SfxBroadcaster *pBC = &rBroadcaster;
108             aBCs.Insert( pBC, aBCs.Count() );
109 
110             DBG_ASSERT( IsListening(rBroadcaster), "StartListening failed" );
111             return sal_True;
112         }
113 
114     }
115     return sal_False;
116 }
117 
118 //--------------------------------------------------------------------
119 
120 // unregisteres at a specific SfxBroadcaster
121 
122 sal_Bool SfxListener::EndListening( SfxBroadcaster& rBroadcaster, sal_Bool bAllDups )
123 {
124     DBG_CHKTHIS(SfxListener, 0);
125 
126     if ( !IsListening( rBroadcaster ) )
127         return sal_False;
128 
129     do
130     {
131         rBroadcaster.RemoveListener(*this);
132         const SfxBroadcaster *pBC = &rBroadcaster;
133         aBCs.Remove( aBCs.GetPos(pBC), 1 );
134     }
135     while ( bAllDups && IsListening( rBroadcaster ) );
136     return sal_True;
137 }
138 
139 //--------------------------------------------------------------------
140 
141 // unregisteres at a specific SfxBroadcaster by index
142 
143 void SfxListener::EndListening( sal_uInt16 nNo )
144 {
145     DBG_CHKTHIS(SfxListener, 0);
146 
147     SfxBroadcaster *pBC = aBCs.GetObject(nNo);
148     pBC->RemoveListener(*this);
149     aBCs.Remove( nNo, 1 );
150 }
151 
152 //--------------------------------------------------------------------
153 
154 // unregisteres all Broadcasters
155 
156 void SfxListener::EndListeningAll()
157 {
158     DBG_CHKTHIS(SfxListener, 0);
159 
160     // MI: bei Optimierung beachten: Seiteneffekte von RemoveListener beachten!
161     while ( aBCs.Count() )
162     {
163         SfxBroadcaster *pBC = aBCs.GetObject(0);
164         pBC->RemoveListener(*this);
165         aBCs.Remove( 0, 1 );
166     }
167 }
168 
169 //--------------------------------------------------------------------
170 
171 sal_Bool SfxListener::IsListening( SfxBroadcaster& rBroadcaster ) const
172 {
173     const SfxBroadcaster *pBC = &rBroadcaster;
174     return USHRT_MAX != aBCs.GetPos( pBC );
175 }
176 
177 //--------------------------------------------------------------------
178 
179 // base implementation of notification handler
180 
181 #ifdef DBG_UTIL
182 void SfxListener::Notify( SfxBroadcaster& rBC, const SfxHint& )
183 #else
184 void SfxListener::Notify( SfxBroadcaster&, const SfxHint& )
185 #endif
186 {
187     #ifdef DBG_UTIL
188     const SfxBroadcaster *pBC = &rBC;
189     DBG_ASSERT( USHRT_MAX != aBCs.GetPos(pBC),
190                 "notification from unregistered broadcaster" );
191     #endif
192 }
193 
194