File indexing completed on 2024-05-12 16:25:54

0001 /*
0002 
0003  * SPDX-FileCopyrightText: 2016 Riccardo Iaconelli <riccardo@kde.org>
0004  * SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0005  *
0006  * SPDX-License-Identifier: LGPL-2.0-or-later
0007  *
0008  */
0009 
0010 #include "notification.h"
0011 #include "ruqola_notification_debug.h"
0012 #ifdef UNITY_SUPPORT
0013 #include "unityservicemanager.h"
0014 #endif
0015 
0016 #include <KLocalizedString>
0017 #include <QIcon>
0018 
0019 Notification::Notification(QObject *parent)
0020     : KStatusNotifierItem(parent)
0021 {
0022     createTrayIcon();
0023 }
0024 
0025 Notification::~Notification()
0026 {
0027 #ifdef UNITY_SUPPORT
0028     delete mUnityServiceManager;
0029 #endif
0030 }
0031 
0032 void Notification::createTrayIcon()
0033 {
0034     setToolTipTitle(QStringLiteral("Ruqola"));
0035     setIconByPixmap(QIcon(QStringLiteral(":/icons/systray.png")));
0036     setCategory(KStatusNotifierItem::Communications);
0037 }
0038 
0039 void Notification::clearNotification(const QString &account)
0040 {
0041     mListTrayIcon.remove(account);
0042     createToolTip();
0043 }
0044 
0045 void Notification::roomNeedAttention()
0046 {
0047     qCDebug(RUQOLA_NOTIFICATION_LOG) << " emit alert";
0048     Q_EMIT alert();
0049 }
0050 
0051 void Notification::updateNotification(bool hasAlert, int unreadNumber, const QString &account)
0052 {
0053     qCDebug(RUQOLA_NOTIFICATION_LOG) << " hasAlert " << hasAlert << " unreadNumber " << unreadNumber << " account" << account;
0054     const TrayInfo info(unreadNumber, hasAlert);
0055     if (info.hasNotification()) {
0056         mListTrayIcon.insert(account, info);
0057     } else {
0058         mListTrayIcon.remove(account);
0059     }
0060     createToolTip();
0061 }
0062 
0063 void Notification::createToolTip()
0064 {
0065     QMapIterator<QString, TrayInfo> i(mListTrayIcon);
0066     QString str;
0067     bool firstElement = true;
0068     bool hasAlert = false;
0069     int unreadMessage = 0;
0070     while (i.hasNext()) {
0071         i.next();
0072         if (firstElement && !str.isEmpty()) {
0073             firstElement = false;
0074             str += QLatin1Char('\n');
0075         }
0076         const TrayInfo trayInfo = i.value();
0077         if (trayInfo.hasAlert) {
0078             hasAlert = trayInfo.hasAlert;
0079         }
0080         if (trayInfo.unreadMessage != 0) {
0081             str += i18n("%1 has %2 Unread Message", i.key(), trayInfo.unreadMessage);
0082             unreadMessage += trayInfo.unreadMessage;
0083         }
0084     }
0085     setToolTipSubTitle(str);
0086     updateUnityService(unreadMessage);
0087     if (status() == KStatusNotifierItem::Passive && (!str.isEmpty() || hasAlert)) {
0088         setStatus(KStatusNotifierItem::Active);
0089     } else if (status() == KStatusNotifierItem::Active && (str.isEmpty() && !hasAlert)) {
0090         setStatus(KStatusNotifierItem::Passive);
0091     }
0092 }
0093 
0094 void Notification::updateUnityService(int unreadMessage)
0095 {
0096 #ifdef UNITY_SUPPORT
0097     unityServiceManager()->setCount(unreadMessage);
0098 #else
0099     Q_UNUSED(unreadMessage)
0100 #endif
0101 }
0102 
0103 #ifdef UNITY_SUPPORT
0104 UnityServiceManager *Notification::unityServiceManager()
0105 {
0106     if (!mUnityServiceManager) {
0107         mUnityServiceManager = new UnityServiceManager();
0108     }
0109     return mUnityServiceManager;
0110 }
0111 
0112 #endif
0113 
0114 #include "moc_notification.cpp"