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