1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: ConfigurationControllerBroadcaster.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #include "precompiled_sd.hxx"
33 #include "ConfigurationControllerBroadcaster.hxx"
34 #include <com/sun/star/lang/IllegalArgumentException.hpp>
35 #include <com/sun/star/lang/DisposedException.hpp>
36 #include <tools/debug.hxx>
38 using namespace ::com::sun::star
;
39 using namespace ::com::sun::star::uno
;
40 using namespace ::com::sun::star::drawing::framework
;
43 namespace sd
{ namespace framework
{
45 ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster (
46 const Reference
<XConfigurationController
>& rxController
)
47 : mxConfigurationController(rxController
),
55 void ConfigurationControllerBroadcaster::AddListener(
56 const Reference
<XConfigurationChangeListener
>& rxListener
,
57 const ::rtl::OUString
& rsEventType
,
60 if ( ! rxListener
.is())
61 throw lang::IllegalArgumentException(
62 OUString::createFromAscii("invalid listener"),
63 mxConfigurationController
,
66 if (maListenerMap
.find(rsEventType
) == maListenerMap
.end())
67 maListenerMap
[rsEventType
] = ListenerList();
68 ListenerDescriptor aDescriptor
;
69 aDescriptor
.mxListener
= rxListener
;
70 aDescriptor
.maUserData
= rUserData
;
71 maListenerMap
[rsEventType
].push_back(aDescriptor
);
77 void ConfigurationControllerBroadcaster::RemoveListener(
78 const Reference
<XConfigurationChangeListener
>& rxListener
)
80 if ( ! rxListener
.is())
81 throw lang::IllegalArgumentException(
82 OUString::createFromAscii("invalid listener"),
83 mxConfigurationController
,
86 ListenerMap::iterator iMap
;
87 ListenerList::iterator iList
;
88 for (iMap
=maListenerMap
.begin(); iMap
!=maListenerMap
.end(); ++iMap
)
90 for (iList
=iMap
->second
.begin(); iList
!=iMap
->second
.end(); ++iList
)
92 if (iList
->mxListener
== rxListener
)
94 iMap
->second
.erase(iList
);
104 void ConfigurationControllerBroadcaster::NotifyListeners (
105 const ListenerList
& rList
,
106 const ConfigurationChangeEvent
& rEvent
)
108 // Create a local copy of the event in which the user data is modified
109 // for every listener.
110 ConfigurationChangeEvent
aEvent (rEvent
);
112 ListenerList::const_iterator iListener
;
113 for (iListener
=rList
.begin(); iListener
!=rList
.end(); ++iListener
)
117 aEvent
.UserData
= iListener
->maUserData
;
118 iListener
->mxListener
->notifyConfigurationChange(aEvent
);
120 catch (lang::DisposedException
& rException
)
122 // When the exception comes from the listener itself then
124 if (rException
.Context
== iListener
->mxListener
)
125 RemoveListener(iListener
->mxListener
);
127 catch(RuntimeException
&)
130 "ConfigurationController: caught exception while notifying listeners");
138 void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent
& rEvent
)
140 // Notify the specialized listeners.
141 ListenerMap::const_iterator
iMap (maListenerMap
.find(rEvent
.Type
));
142 if (iMap
!= maListenerMap
.end())
144 // Create a local list of the listeners to avoid problems with
145 // concurrent changes and to be able to remove disposed listeners.
146 ListenerList
aList (iMap
->second
.begin(), iMap
->second
.end());
147 NotifyListeners(aList
,rEvent
);
150 // Notify the universal listeners.
151 iMap
= maListenerMap
.find(OUString());
152 if (iMap
!= maListenerMap
.end())
154 // Create a local list of the listeners to avoid problems with
155 // concurrent changes and to be able to remove disposed listeners.
156 ListenerList
aList (iMap
->second
.begin(), iMap
->second
.end());
157 NotifyListeners(aList
,rEvent
);
164 void ConfigurationControllerBroadcaster::NotifyListeners (
165 const OUString
& rsEventType
,
166 const Reference
<XResourceId
>& rxResourceId
,
167 const Reference
<XResource
>& rxResourceObject
)
169 ConfigurationChangeEvent aEvent
;
170 aEvent
.Type
= rsEventType
;
171 aEvent
.ResourceId
= rxResourceId
;
172 aEvent
.ResourceObject
= rxResourceObject
;
175 NotifyListeners(aEvent
);
177 catch (lang::DisposedException
)
186 void ConfigurationControllerBroadcaster::DisposeAndClear (void)
188 lang::EventObject aEvent
;
189 aEvent
.Source
= mxConfigurationController
;
190 while (maListenerMap
.size() > 0)
192 ListenerMap::iterator
iMap (maListenerMap
.begin());
193 if (iMap
== maListenerMap
.end())
196 // When the first vector is empty then remove it from the map.
197 if (iMap
->second
.size() == 0)
199 maListenerMap
.erase(iMap
);
204 Reference
<lang::XEventListener
> xListener (
205 iMap
->second
.front().mxListener
, UNO_QUERY
);
208 // Tell the listener that the configuration controller is
209 // being disposed and remove the listener (for all event
213 RemoveListener(iMap
->second
.front().mxListener
);
214 xListener
->disposing(aEvent
);
216 catch (RuntimeException
&)
219 "ConfigurationController: caught exception while notifying dispose");
224 // Remove just this reference to the listener.
225 iMap
->second
.erase(iMap
->second
.begin());
234 } } // end of namespace sd::framework