xref: /aoo41x/main/svl/source/notify/brdcst.cxx (revision cdf0e10c)
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 
31 #ifndef GCC
32 #endif
33 #include <tools/debug.hxx>
34 
35 #include <svl/hint.hxx>
36 #include <svl/smplhint.hxx>
37 #include <svl/lstner.hxx>
38 
39 SV_DECL_PTRARR( SfxListenerArr_Impl, SfxListener*, 0, 2 )
40 
41 #define _SFX_BRDCST_CXX
42 #include <svl/brdcst.hxx>
43 
44 //====================================================================
45 DBG_NAME(SfxBroadcaster)
46 TYPEINIT0(SfxBroadcaster);
47 
48 //====================================================================
49 
50 //====================================================================
51 // broadcast immediately
52 
53 
54 void SfxBroadcaster::Broadcast( const SfxHint &rHint )
55 {
56 	DBG_CHKTHIS(SfxBroadcaster, 0);
57 
58 	// is anybody to notify?
59 	if ( aListeners.Count() /*! || aGlobListeners.Count() */ )
60 	{
61 		// notify all registered listeners exactly once
62 		for ( sal_uInt16 n = 0; n < aListeners.Count(); ++n )
63 		{
64 			SfxListener* pListener = aListeners[n];
65 			if ( pListener )
66 				pListener->Notify( *this, rHint );
67 		}
68 	}
69 }
70 
71 //--------------------------------------------------------------------
72 
73 // broadcast after a timeout
74 
75 
76 void SfxBroadcaster::BroadcastDelayed( const SfxHint& rHint )
77 {
78 	DBG_WARNING( "not implemented" );
79 	Broadcast(rHint);
80 }
81 //--------------------------------------------------------------------
82 
83 // broadcast in idle-handler
84 
85 void SfxBroadcaster::BroadcastInIdle( const SfxHint& rHint )
86 {
87 	DBG_WARNING( "not implemented" );
88 	Broadcast(rHint);
89 }
90 //--------------------------------------------------------------------
91 
92 // unregister all listeners
93 
94 SfxBroadcaster::~SfxBroadcaster()
95 {
96 	DBG_DTOR(SfxBroadcaster, 0);
97 
98 	Broadcast( SfxSimpleHint(SFX_HINT_DYING) );
99 
100 	// remove all still registered listeners
101 	for ( sal_uInt16 nPos = 0; nPos < aListeners.Count(); ++nPos )
102 	{
103 		SfxListener *pListener = aListeners[nPos];
104 		if ( pListener )
105 			pListener->RemoveBroadcaster_Impl(*this);
106 	}
107 }
108 
109 //--------------------------------------------------------------------
110 
111 // simple ctor of class SfxBroadcaster
112 
113 SfxBroadcaster::SfxBroadcaster()
114 {
115 	DBG_CTOR(SfxBroadcaster, 0);
116 }
117 
118 //--------------------------------------------------------------------
119 
120 // copy ctor of class SfxBroadcaster
121 
122 
123 SfxBroadcaster::SfxBroadcaster( const SfxBroadcaster &rBC )
124 {
125 	DBG_CTOR(SfxBroadcaster, 0);
126 
127 	for ( sal_uInt16 n = 0; n < rBC.aListeners.Count(); ++n )
128 	{
129 		SfxListener *pListener = rBC.aListeners[n];
130 		if ( pListener )
131 			pListener->StartListening( *this );
132 	}
133 }
134 
135 //--------------------------------------------------------------------
136 
137 // add a new SfxListener to the list
138 
139 sal_Bool SfxBroadcaster::AddListener( SfxListener& rListener )
140 {
141 	DBG_CHKTHIS(SfxBroadcaster, 0);
142 	const SfxListener *pListener = &rListener;
143 	const SfxListener *pNull = 0;
144 	sal_uInt16 nFreePos = aListeners.GetPos( pNull );
145 	if ( nFreePos < aListeners.Count() )
146 		aListeners.GetData()[nFreePos] = pListener;
147 	else if ( aListeners.Count() < (USHRT_MAX-1) )
148 		aListeners.Insert( pListener, aListeners.Count() );
149 	else
150 	{
151 		DBG_ERROR( "array overflow" );
152 		return sal_False;
153 	}
154 
155 	DBG_ASSERT( USHRT_MAX != aListeners.GetPos(pListener),
156 				"AddListener failed" );
157 	return sal_True;
158 }
159 
160 //--------------------------------------------------------------------
161 
162 // called, if no more listeners exists
163 
164 void SfxBroadcaster::ListenersGone()
165 {
166 	DBG_CHKTHIS(SfxBroadcaster,0);
167 }
168 
169 //--------------------------------------------------------------------
170 
171 // forward a notification to all registered listeners
172 
173 void SfxBroadcaster::Forward(SfxBroadcaster& rBC, const SfxHint& rHint)
174 {
175 	const sal_uInt16 nCount = aListeners.Count();
176 	for ( sal_uInt16 i = 0; i < nCount; ++i )
177 	{
178 		SfxListener *pListener = aListeners[i];
179 		if ( pListener )
180 			pListener->Notify( rBC, rHint );
181 	}
182 }
183 
184 //--------------------------------------------------------------------
185 
186 // remove one SfxListener from the list
187 
188 void SfxBroadcaster::RemoveListener( SfxListener& rListener )
189 {
190 	{DBG_CHKTHIS(SfxBroadcaster, 0);}
191 	const SfxListener *pListener = &rListener;
192 	sal_uInt16 nPos = aListeners.GetPos(pListener);
193 	DBG_ASSERT( nPos != USHRT_MAX, "RemoveListener: Listener unknown" );
194 	aListeners.GetData()[nPos] = 0;
195 	if ( !HasListeners() )
196 		ListenersGone();
197 }
198 
199 //--------------------------------------------------------------------
200 
201 sal_Bool SfxBroadcaster::HasListeners() const
202 {
203 	for ( sal_uInt16 n = 0; n < aListeners.Count(); ++n )
204 		if ( aListeners.GetObject(n) != 0 )
205 			return sal_True;
206 	return sal_False;
207 }
208 
209 //--------------------------------------------------------------------
210