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