Avoid potential negative array index access to cached text.
[LibreOffice.git] / configmgr / source / broadcaster.cxx
blob365e25f9eb028a4a6e38b68a10464f84ef8f6492
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
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/.
9 * This file incorporates work covered by the following license notice:
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 .
20 #include <sal/config.h>
22 #include <cassert>
24 #include <com/sun/star/beans/XPropertiesChangeListener.hpp>
25 #include <com/sun/star/beans/XPropertyChangeListener.hpp>
26 #include <com/sun/star/container/XContainerListener.hpp>
27 #include <com/sun/star/lang/DisposedException.hpp>
28 #include <com/sun/star/lang/WrappedTargetRuntimeException.hpp>
29 #include <com/sun/star/lang/XEventListener.hpp>
30 #include <com/sun/star/uno/Any.hxx>
31 #include <com/sun/star/uno/Exception.hpp>
32 #include <com/sun/star/uno/Reference.hxx>
33 #include <com/sun/star/uno/XInterface.hpp>
34 #include <com/sun/star/util/XChangesListener.hpp>
35 #include <cppuhelper/exc_hlp.hxx>
36 #include <rtl/ustrbuf.hxx>
37 #include <rtl/ustring.hxx>
38 #include <utility>
40 #include "broadcaster.hxx"
42 namespace configmgr {
44 namespace {
46 void appendMessage(
47 OUStringBuffer & buffer, css::uno::Exception const & exception)
49 buffer.append("; ");
50 buffer.append(exception.Message);
55 void Broadcaster::addDisposeNotification(
56 css::uno::Reference< css::lang::XEventListener > const & listener,
57 css::lang::EventObject const & event)
59 disposeNotifications_.emplace_back(listener, event);
62 void Broadcaster::addContainerElementReplacedNotification(
63 css::uno::Reference< css::container::XContainerListener > const & listener,
64 css::container::ContainerEvent const & event)
66 containerElementReplacedNotifications_.emplace_back(listener, event);
69 void Broadcaster::addContainerElementInsertedNotification(
70 css::uno::Reference< css::container::XContainerListener > const & listener,
71 css::container::ContainerEvent const & event)
73 containerElementInsertedNotifications_.emplace_back(listener, event);
76 void Broadcaster::addContainerElementRemovedNotification(
77 css::uno::Reference< css::container::XContainerListener > const & listener,
78 css::container::ContainerEvent const & event)
80 containerElementRemovedNotifications_.emplace_back(listener, event);
83 void Broadcaster::addPropertyChangeNotification(
84 css::uno::Reference< css::beans::XPropertyChangeListener > const & listener,
85 css::beans::PropertyChangeEvent const & event)
87 propertyChangeNotifications_.emplace_back(listener, event);
90 void Broadcaster::addPropertiesChangeNotification(
91 css::uno::Reference< css::beans::XPropertiesChangeListener > const &
92 listener,
93 css::uno::Sequence< css::beans::PropertyChangeEvent > const & event)
95 propertiesChangeNotifications_.emplace_back(listener, event);
98 void Broadcaster::addChangesNotification(
99 css::uno::Reference< css::util::XChangesListener > const & listener,
100 css::util::ChangesEvent const & event, bool bRootListener)
102 if (bRootListener)
103 rootChangesNotifications_.emplace_back(listener, event);
104 else
105 changesNotifications_.emplace_back(listener, event);
108 void Broadcaster::send() {
109 css::uno::Any exception;
110 OUStringBuffer messages;
111 for (auto& rNotification : disposeNotifications_) {
112 try {
113 rNotification.listener->disposing(rNotification.event);
114 } catch (css::lang::DisposedException &) {
115 } catch (css::uno::Exception & e) {
116 exception = cppu::getCaughtException();
117 appendMessage(messages, e);
120 for (auto& rNotification : containerElementInsertedNotifications_)
122 try {
123 rNotification.listener->elementInserted(rNotification.event);
124 } catch (css::lang::DisposedException &) {
125 } catch (css::uno::Exception & e) {
126 exception = cppu::getCaughtException();
127 appendMessage(messages, e);
130 for (auto& rNotification : containerElementRemovedNotifications_)
132 try {
133 rNotification.listener->elementRemoved(rNotification.event);
134 } catch (css::lang::DisposedException &) {
135 } catch (css::uno::Exception & e) {
136 exception = cppu::getCaughtException();
137 appendMessage(messages, e);
140 for (auto& rNotification : containerElementReplacedNotifications_)
142 try {
143 rNotification.listener->elementReplaced(rNotification.event);
144 } catch (css::lang::DisposedException &) {
145 } catch (css::uno::Exception & e) {
146 exception = cppu::getCaughtException();
147 appendMessage(messages, e);
150 for (auto& rNotification : propertyChangeNotifications_)
152 try {
153 rNotification.listener->propertyChange(rNotification.event);
154 } catch (css::lang::DisposedException &) {
155 } catch (css::uno::Exception & e) {
156 exception = cppu::getCaughtException();
157 appendMessage(messages, e);
160 for (auto& rNotification : propertiesChangeNotifications_)
162 try {
163 rNotification.listener->propertiesChange(rNotification.event);
164 } catch (css::lang::DisposedException &) {
165 } catch (css::uno::Exception & e) {
166 exception = cppu::getCaughtException();
167 appendMessage(messages, e);
170 // First root listeners, then the rest
171 for (const auto& container : { rootChangesNotifications_ , changesNotifications_})
173 for (auto& rNotification : container) {
174 try {
175 rNotification.listener->changesOccurred(rNotification.event);
176 } catch (css::lang::DisposedException &) {
177 } catch (css::uno::Exception & e) {
178 exception = cppu::getCaughtException();
179 appendMessage(messages, e);
183 if (exception.hasValue()) {
184 throw css::lang::WrappedTargetRuntimeException(
185 ("configmgr exceptions during listener notification" +
186 messages),
187 css::uno::Reference< css::uno::XInterface >(),
188 exception);
192 Broadcaster::DisposeNotification::DisposeNotification(
193 css::uno::Reference< css::lang::XEventListener > const & theListener,
194 css::lang::EventObject theEvent):
195 listener(theListener), event(std::move(theEvent))
197 assert(theListener.is());
200 Broadcaster::ContainerNotification::ContainerNotification(
201 css::uno::Reference< css::container::XContainerListener > const &
202 theListener,
203 css::container::ContainerEvent theEvent):
204 listener(theListener), event(std::move(theEvent))
206 assert(theListener.is());
209 Broadcaster::PropertyChangeNotification::PropertyChangeNotification(
210 css::uno::Reference< css::beans::XPropertyChangeListener > const &
211 theListener,
212 css::beans::PropertyChangeEvent theEvent):
213 listener(theListener), event(std::move(theEvent))
215 assert(theListener.is());
218 Broadcaster::PropertiesChangeNotification::PropertiesChangeNotification(
219 css::uno::Reference< css::beans::XPropertiesChangeListener > const &
220 theListener,
221 css::uno::Sequence< css::beans::PropertyChangeEvent > const & theEvent):
222 listener(theListener), event(theEvent)
224 assert(theListener.is());
227 Broadcaster::ChangesNotification::ChangesNotification(
228 css::uno::Reference< css::util::XChangesListener > const & theListener,
229 css::util::ChangesEvent theEvent):
230 listener(theListener), event(std::move(theEvent))
232 assert(theListener.is());
237 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */