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 127 : ConfigurationControllerBroadcaster::ConfigurationControllerBroadcaster (
32 : const Reference<XConfigurationController>& rxController)
33 : : mxConfigurationController(rxController),
34 127 : maListenerMap()
35 : {
36 127 : }
37 :
38 2106 : void ConfigurationControllerBroadcaster::AddListener(
39 : const Reference<XConfigurationChangeListener>& rxListener,
40 : const OUString& rsEventType,
41 : const Any& rUserData)
42 : {
43 2106 : if ( ! rxListener.is())
44 : throw lang::IllegalArgumentException("invalid listener",
45 : mxConfigurationController,
46 0 : 0);
47 :
48 2106 : if (maListenerMap.find(rsEventType) == maListenerMap.end())
49 762 : maListenerMap[rsEventType] = ListenerList();
50 2106 : ListenerDescriptor aDescriptor;
51 2106 : aDescriptor.mxListener = rxListener;
52 2106 : aDescriptor.maUserData = rUserData;
53 2106 : maListenerMap[rsEventType].push_back(aDescriptor);
54 2106 : }
55 :
56 1053 : void ConfigurationControllerBroadcaster::RemoveListener(
57 : const Reference<XConfigurationChangeListener>& rxListener)
58 : {
59 1053 : if ( ! rxListener.is())
60 : throw lang::IllegalArgumentException("invalid listener",
61 : mxConfigurationController,
62 0 : 0);
63 :
64 1053 : ListenerMap::iterator iMap;
65 1053 : ListenerList::iterator iList;
66 5683 : for (iMap=maListenerMap.begin(); iMap!=maListenerMap.end(); ++iMap)
67 : {
68 12457 : for (iList=iMap->second.begin(); iList!=iMap->second.end(); ++iList)
69 : {
70 9933 : if (iList->mxListener == rxListener)
71 : {
72 2106 : iMap->second.erase(iList);
73 2106 : break;
74 : }
75 : }
76 : }
77 1053 : }
78 :
79 3106 : 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 3106 : ConfigurationChangeEvent aEvent (rEvent);
86 :
87 3106 : ListenerList::const_iterator iListener;
88 11688 : for (iListener=rList.begin(); iListener!=rList.end(); ++iListener)
89 : {
90 : try
91 : {
92 8582 : aEvent.UserData = iListener->maUserData;
93 8582 : 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 3106 : }
107 3106 : }
108 :
109 3665 : void ConfigurationControllerBroadcaster::NotifyListeners (const ConfigurationChangeEvent& rEvent)
110 : {
111 : // Notify the specialized listeners.
112 3665 : ListenerMap::const_iterator iMap (maListenerMap.find(rEvent.Type));
113 3665 : 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 3106 : ListenerList aList (iMap->second.begin(), iMap->second.end());
118 3106 : NotifyListeners(aList,rEvent);
119 : }
120 :
121 : // Notify the universal listeners.
122 3665 : iMap = maListenerMap.find(OUString());
123 3665 : 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 3665 : }
131 :
132 1677 : void ConfigurationControllerBroadcaster::NotifyListeners (
133 : const OUString& rsEventType,
134 : const Reference<XResourceId>& rxResourceId,
135 : const Reference<XResource>& rxResourceObject)
136 : {
137 1677 : ConfigurationChangeEvent aEvent;
138 1677 : aEvent.Type = rsEventType;
139 1677 : aEvent.ResourceId = rxResourceId;
140 1677 : aEvent.ResourceObject = rxResourceObject;
141 : try
142 : {
143 1677 : NotifyListeners(aEvent);
144 : }
145 0 : catch (const lang::DisposedException&)
146 : {
147 1677 : }
148 1677 : }
149 :
150 127 : void ConfigurationControllerBroadcaster::DisposeAndClear()
151 : {
152 127 : lang::EventObject aEvent;
153 127 : aEvent.Source = mxConfigurationController;
154 2028 : while (!maListenerMap.empty())
155 : {
156 1774 : ListenerMap::iterator iMap (maListenerMap.begin());
157 1774 : if (iMap == maListenerMap.end())
158 0 : break;
159 :
160 : // When the first vector is empty then remove it from the map.
161 1774 : if (iMap->second.empty())
162 : {
163 762 : maListenerMap.erase(iMap);
164 1524 : continue;
165 : }
166 : else
167 : {
168 : Reference<lang::XEventListener> xListener (
169 1012 : iMap->second.front().mxListener, UNO_QUERY);
170 1012 : 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 1012 : RemoveListener(iMap->second.front().mxListener);
178 1012 : 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 1012 : }
190 : }
191 127 : }
192 127 : }
193 :
194 : } } // end of namespace sd::framework
195 :
196 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|