File indexing completed on 2024-05-19 05:05:18
0001 /*************************************************************************** 0002 * SPDX-License-Identifier: GPL-2.0-or-later 0003 * * 0004 * SPDX-FileCopyrightText: 2004-2019 Thomas Fischer <fischer@unix-ag.uni-kl.de> 0005 * * 0006 * This program is free software; you can redistribute it and/or modify * 0007 * it under the terms of the GNU General Public License as published by * 0008 * the Free Software Foundation; either version 2 of the License, or * 0009 * (at your option) any later version. * 0010 * * 0011 * This program is distributed in the hope that it will be useful, * 0012 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 0013 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 0014 * GNU General Public License for more details. * 0015 * * 0016 * You should have received a copy of the GNU General Public License * 0017 * along with this program; if not, see <https://www.gnu.org/licenses/>. * 0018 ***************************************************************************/ 0019 0020 #include "notificationhub.h" 0021 0022 #include <QSet> 0023 0024 #include "logging_config.h" 0025 0026 NotificationListener::~NotificationListener() 0027 { 0028 NotificationHub::unregisterNotificationListener(this); 0029 } 0030 0031 class NotificationHub::NotificationHubPrivate 0032 { 0033 public: 0034 QHash<int, QSet<NotificationListener *> > listenersPerEventId; 0035 QSet<NotificationListener *> listenersAnyEvent; 0036 0037 static const QHash<int, QString> eventIdToName; 0038 0039 NotificationHubPrivate(NotificationHub *) 0040 { 0041 /// nothing 0042 } 0043 }; 0044 0045 const QHash<int, QString> NotificationHub::NotificationHubPrivate::eventIdToName{ 0046 {NotificationHub::EventAny, QStringLiteral("Any")}, 0047 {NotificationHub::EventConfigurationChanged, QStringLiteral("ConfigurationChanged")}, 0048 {NotificationHub::EventBibliographySystemChanged, QStringLiteral("BibliographySystemChanged")}, 0049 {NotificationHub::EventUserDefined, QStringLiteral("UserDefined")} 0050 }; 0051 0052 NotificationHub::NotificationHub() 0053 : d(new NotificationHubPrivate(this)) 0054 { 0055 /// nothing 0056 } 0057 0058 NotificationHub::~NotificationHub() 0059 { 0060 delete d; 0061 } 0062 0063 NotificationHub &NotificationHub::instance() 0064 { 0065 static NotificationHub singleton; 0066 return singleton; 0067 } 0068 0069 void NotificationHub::registerNotificationListener(NotificationListener *listener, int eventId) 0070 { 0071 NotificationHub::NotificationHubPrivate *d = instance().d; 0072 if (eventId == EventAny) 0073 d->listenersAnyEvent.insert(listener); 0074 else { 0075 QSet<NotificationListener *> set = d->listenersPerEventId.value(eventId, QSet<NotificationListener *>()); 0076 set.insert(listener); 0077 d->listenersPerEventId.insert(eventId, set); 0078 } 0079 } 0080 0081 void NotificationHub::unregisterNotificationListener(NotificationListener *listener, int eventId) 0082 { 0083 NotificationHub::NotificationHubPrivate *d = instance().d; 0084 if (eventId == EventAny) { 0085 for (QHash<int, QSet<NotificationListener *> >::Iterator it = d->listenersPerEventId.begin(); it != d->listenersPerEventId.end(); ++it) 0086 it.value().remove(listener); 0087 } else { 0088 QSet<NotificationListener *> set = d->listenersPerEventId.value(eventId, QSet<NotificationListener *>()); 0089 set.remove(listener); 0090 d->listenersPerEventId.insert(eventId, set); 0091 } 0092 d->listenersAnyEvent.remove(listener); 0093 } 0094 0095 void NotificationHub::publishEvent(int eventId) 0096 { 0097 if (eventId >= 0) { 0098 NotificationHub::NotificationHubPrivate *d = instance().d; 0099 QSet<NotificationListener *> set(d->listenersPerEventId.value(eventId, QSet<NotificationListener *>())); 0100 for (NotificationListener *listener : const_cast<const QSet<NotificationListener *> &>(d->listenersAnyEvent)) 0101 set.insert(listener); 0102 qCDebug(LOG_KBIBTEX_CONFIG) << "Notifying about event" << NotificationHubPrivate::eventIdToName.value(eventId, QString::number(eventId)) << "having" << set.count() << "receivers"; 0103 for (NotificationListener *listener : const_cast<const QSet<NotificationListener *> &>(set)) 0104 listener->notificationEvent(eventId); 0105 } 0106 } 0107 0108 const int NotificationHub::EventAny = -1; 0109 const int NotificationHub::EventConfigurationChanged = 0; 0110 const int NotificationHub::EventBibliographySystemChanged = 1; 0111 const int NotificationHub::EventUserDefined = 1024;