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 :
21 : #include <tools/debug.hxx>
22 :
23 : #include <svl/hint.hxx>
24 : #include <svl/brdcst.hxx>
25 :
26 : #include <svl/lstner.hxx>
27 : #include <algorithm>
28 :
29 0 : TYPEINIT0(SfxListener);
30 :
31 : // simple ctor of class SfxListener
32 :
33 0 : SfxListener::SfxListener()
34 : {
35 0 : }
36 :
37 : // copy ctor of class SfxListener
38 :
39 0 : SfxListener::SfxListener( const SfxListener &rListener )
40 : {
41 0 : for ( sal_uInt16 n = 0; n < rListener.aBCs.size(); ++n )
42 0 : StartListening( *rListener.aBCs[n] );
43 0 : }
44 :
45 : // unregisters the SfxListener from its SfxBroadcasters
46 :
47 0 : SfxListener::~SfxListener()
48 : {
49 : // unregister at all remaining broadcasters
50 0 : for ( sal_uInt16 nPos = 0; nPos < aBCs.size(); ++nPos )
51 : {
52 0 : SfxBroadcaster *pBC = aBCs[nPos];
53 0 : pBC->RemoveListener(*this);
54 : }
55 0 : }
56 :
57 :
58 : // unregisters a specific SfxBroadcaster
59 :
60 0 : void SfxListener::RemoveBroadcaster_Impl( SfxBroadcaster& rBroadcaster )
61 : {
62 0 : aBCs.erase( std::find( aBCs.begin(), aBCs.end(), &rBroadcaster ) );
63 0 : }
64 :
65 :
66 : // registers a specific SfxBroadcaster
67 :
68 0 : bool SfxListener::StartListening( SfxBroadcaster& rBroadcaster, bool bPreventDups )
69 : {
70 0 : if ( !bPreventDups || !IsListening( rBroadcaster ) )
71 : {
72 0 : rBroadcaster.AddListener(*this);
73 0 : aBCs.push_back( &rBroadcaster );
74 :
75 : DBG_ASSERT( IsListening(rBroadcaster), "StartListening failed" );
76 0 : return true;
77 : }
78 :
79 0 : return false;
80 : }
81 :
82 :
83 : // unregisters a specific SfxBroadcaster
84 :
85 0 : bool SfxListener::EndListening( SfxBroadcaster& rBroadcaster, bool bAllDups )
86 : {
87 0 : if ( !IsListening( rBroadcaster ) )
88 0 : return false;
89 :
90 0 : do
91 : {
92 0 : rBroadcaster.RemoveListener(*this);
93 0 : aBCs.erase( std::find( aBCs.begin(), aBCs.end(), &rBroadcaster ) );
94 : }
95 0 : while ( bAllDups && IsListening( rBroadcaster ) );
96 0 : return true;
97 : }
98 :
99 :
100 : // unregisters all Broadcasters
101 :
102 0 : void SfxListener::EndListeningAll()
103 : {
104 : // MI: bei Optimierung beachten: Seiteneffekte von RemoveListener beachten!
105 0 : while ( !aBCs.empty() )
106 : {
107 0 : SfxBroadcaster *pBC = aBCs.front();
108 0 : pBC->RemoveListener(*this);
109 0 : aBCs.pop_front();
110 : }
111 0 : }
112 :
113 :
114 0 : bool SfxListener::IsListening( SfxBroadcaster& rBroadcaster ) const
115 : {
116 0 : return aBCs.end() != std::find( aBCs.begin(), aBCs.end(), &rBroadcaster );
117 : }
118 :
119 :
120 : // base implementation of notification handler
121 :
122 : #ifdef DBG_UTIL
123 : void SfxListener::Notify( SfxBroadcaster& rBroadcaster, const SfxHint& )
124 : #else
125 0 : void SfxListener::Notify( SfxBroadcaster&, const SfxHint& )
126 : #endif
127 : {
128 : #ifdef DBG_UTIL
129 : DBG_ASSERT(aBCs.end() != std::find(aBCs.begin(), aBCs.end(), &rBroadcaster),
130 : "notification from unregistered broadcaster" );
131 : #endif
132 0 : }
133 :
134 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|