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 "ConfigurationControllerBroadcaster.hxx"
22 : #include <com/sun/star/lang/IllegalArgumentException.hpp>
23 : #include <com/sun/star/lang/DisposedException.hpp>
24 : #include <tools/debug.hxx>
25 : #include <tools/diagnose_ex.h>
26 :
27 : using namespace ::com::sun::star;
28 : using namespace ::com::sun::star::uno;
29 : using namespace ::com::sun::star::drawing::framework;
30 : using rtl::OUString;
31 :
32 : namespace sd { namespace framework {
33 :
34 0 : ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster (
35 : const Reference<XConfigurationController>& rxController)
36 : : mxConfigurationController(rxController),
37 0 : maListenerMap()
38 : {
39 0 : }
40 :
41 :
42 :
43 :
44 0 : void ConfigurationControllerBroadcaster::AddListener(
45 : const Reference<XConfigurationChangeListener>& rxListener,
46 : const ::rtl::OUString& rsEventType,
47 : const Any& rUserData)
48 : {
49 0 : if ( ! rxListener.is())
50 : throw lang::IllegalArgumentException("invalid listener",
51 : mxConfigurationController,
52 0 : 0);
53 :
54 0 : if (maListenerMap.find(rsEventType) == maListenerMap.end())
55 0 : maListenerMap[rsEventType] = ListenerList();
56 0 : ListenerDescriptor aDescriptor;
57 0 : aDescriptor.mxListener = rxListener;
58 0 : aDescriptor.maUserData = rUserData;
59 0 : maListenerMap[rsEventType].push_back(aDescriptor);
60 0 : }
61 :
62 :
63 :
64 :
65 0 : void ConfigurationControllerBroadcaster::RemoveListener(
66 : const Reference<XConfigurationChangeListener>& rxListener)
67 : {
68 0 : if ( ! rxListener.is())
69 : throw lang::IllegalArgumentException("invalid listener",
70 : mxConfigurationController,
71 0 : 0);
72 :
73 0 : ListenerMap::iterator iMap;
74 0 : ListenerList::iterator iList;
75 0 : for (iMap=maListenerMap.begin(); iMap!=maListenerMap.end(); ++iMap)
76 : {
77 0 : for (iList=iMap->second.begin(); iList!=iMap->second.end(); ++iList)
78 : {
79 0 : if (iList->mxListener == rxListener)
80 : {
81 0 : iMap->second.erase(iList);
82 0 : break;
83 : }
84 : }
85 : }
86 0 : }
87 :
88 :
89 :
90 :
91 0 : void ConfigurationControllerBroadcaster::NotifyListeners (
92 : const ListenerList& rList,
93 : const ConfigurationChangeEvent& rEvent)
94 : {
95 : // Create a local copy of the event in which the user data is modified
96 : // for every listener.
97 0 : ConfigurationChangeEvent aEvent (rEvent);
98 :
99 0 : ListenerList::const_iterator iListener;
100 0 : for (iListener=rList.begin(); iListener!=rList.end(); ++iListener)
101 : {
102 : try
103 : {
104 0 : aEvent.UserData = iListener->maUserData;
105 0 : iListener->mxListener->notifyConfigurationChange(aEvent);
106 : }
107 0 : catch (const lang::DisposedException& rException)
108 : {
109 : // When the exception comes from the listener itself then
110 : // unregister it.
111 0 : if (rException.Context == iListener->mxListener)
112 0 : RemoveListener(iListener->mxListener);
113 : }
114 0 : catch (const RuntimeException&)
115 : {
116 : DBG_UNHANDLED_EXCEPTION();
117 : }
118 0 : }
119 0 : }
120 :
121 :
122 :
123 :
124 0 : void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent& rEvent)
125 : {
126 : // Notify the specialized listeners.
127 0 : ListenerMap::const_iterator iMap (maListenerMap.find(rEvent.Type));
128 0 : if (iMap != maListenerMap.end())
129 : {
130 : // Create a local list of the listeners to avoid problems with
131 : // concurrent changes and to be able to remove disposed listeners.
132 0 : ListenerList aList (iMap->second.begin(), iMap->second.end());
133 0 : NotifyListeners(aList,rEvent);
134 : }
135 :
136 : // Notify the universal listeners.
137 0 : iMap = maListenerMap.find(OUString());
138 0 : if (iMap != maListenerMap.end())
139 : {
140 : // Create a local list of the listeners to avoid problems with
141 : // concurrent changes and to be able to remove disposed listeners.
142 0 : ListenerList aList (iMap->second.begin(), iMap->second.end());
143 0 : NotifyListeners(aList,rEvent);
144 : }
145 0 : }
146 :
147 :
148 :
149 :
150 0 : void ConfigurationControllerBroadcaster::NotifyListeners (
151 : const OUString& rsEventType,
152 : const Reference<XResourceId>& rxResourceId,
153 : const Reference<XResource>& rxResourceObject)
154 : {
155 0 : ConfigurationChangeEvent aEvent;
156 0 : aEvent.Type = rsEventType;
157 0 : aEvent.ResourceId = rxResourceId;
158 0 : aEvent.ResourceObject = rxResourceObject;
159 : try
160 : {
161 0 : NotifyListeners(aEvent);
162 : }
163 0 : catch (const lang::DisposedException&)
164 : {
165 0 : }
166 0 : }
167 :
168 :
169 :
170 :
171 :
172 0 : void ConfigurationControllerBroadcaster::DisposeAndClear (void)
173 : {
174 0 : lang::EventObject aEvent;
175 0 : aEvent.Source = mxConfigurationController;
176 0 : while (!maListenerMap.empty())
177 : {
178 0 : ListenerMap::iterator iMap (maListenerMap.begin());
179 0 : if (iMap == maListenerMap.end())
180 : break;
181 :
182 : // When the first vector is empty then remove it from the map.
183 0 : if (iMap->second.empty())
184 : {
185 0 : maListenerMap.erase(iMap);
186 0 : continue;
187 : }
188 : else
189 : {
190 : Reference<lang::XEventListener> xListener (
191 0 : iMap->second.front().mxListener, UNO_QUERY);
192 0 : if (xListener.is())
193 : {
194 : // Tell the listener that the configuration controller is
195 : // being disposed and remove the listener (for all event
196 : // types).
197 : try
198 : {
199 0 : RemoveListener(iMap->second.front().mxListener);
200 0 : xListener->disposing(aEvent);
201 : }
202 0 : catch (const RuntimeException&)
203 : {
204 : DBG_UNHANDLED_EXCEPTION();
205 : }
206 : }
207 : else
208 : {
209 : // Remove just this reference to the listener.
210 0 : iMap->second.erase(iMap->second.begin());
211 0 : }
212 : }
213 0 : }
214 0 : }
215 :
216 :
217 :
218 :
219 : } } // end of namespace sd::framework
220 :
221 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|