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