File indexing completed on 2024-02-18 05:01:14

0001 /*
0002    SPDX-FileCopyrightText: 2018-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "receivetypingnotificationmanager.h"
0008 #include <KLocalizedString>
0009 
0010 ReceiveTypingNotificationManager::ReceiveTypingNotificationManager(QObject *parent)
0011     : QObject(parent)
0012 {
0013 }
0014 
0015 ReceiveTypingNotificationManager::~ReceiveTypingNotificationManager() = default;
0016 
0017 void ReceiveTypingNotificationManager::clearTypingNotification()
0018 {
0019     Q_EMIT clearNotification();
0020 }
0021 
0022 void ReceiveTypingNotificationManager::insertTypingNotification(const QString &roomId, const QString &userName, bool onTyping)
0023 {
0024     if (mMapTypingNotifications.contains(roomId)) {
0025         QStringList lst = mMapTypingNotifications.value(roomId);
0026         if (onTyping) {
0027             if (!lst.contains(userName)) {
0028                 lst.append(userName);
0029                 mMapTypingNotifications[roomId] = lst;
0030                 Q_EMIT notificationChanged(roomId, generateNotification(lst));
0031             }
0032         } else {
0033             const int removedUserCount = lst.removeAll(userName);
0034             if (removedUserCount > 0) {
0035                 if (lst.isEmpty()) {
0036                     // remove roomId
0037                     mMapTypingNotifications.remove(roomId);
0038                 } else {
0039                     mMapTypingNotifications[roomId] = lst;
0040                 }
0041                 Q_EMIT notificationChanged(roomId, generateNotification(lst));
0042             }
0043         }
0044     } else {
0045         if (onTyping) {
0046             mMapTypingNotifications.insert(roomId, {userName});
0047             Q_EMIT notificationChanged(roomId, generateNotification({userName}));
0048         }
0049     }
0050 }
0051 
0052 QString ReceiveTypingNotificationManager::generateNotification(const QStringList &userNames) const
0053 {
0054     if (userNames.isEmpty()) {
0055         return {};
0056     }
0057     if (userNames.count() == 1) {
0058         return i18n("<strong>%1</strong> is typing...", userNames[0]);
0059     }
0060 
0061     QString notificationStr;
0062     for (int i = 0; i < userNames.count(); ++i) {
0063         const QString &user = userNames.at(i);
0064         if (i == 0) {
0065             notificationStr = user;
0066         } else if (i < userNames.count() - 1) {
0067             notificationStr = i18n("%1, %2", notificationStr, user);
0068         } else {
0069             notificationStr = i18n("%1 and %2", notificationStr, user);
0070         }
0071     }
0072     return i18n("<strong>%1</strong> are typing...", notificationStr);
0073 }
0074 
0075 QString ReceiveTypingNotificationManager::typingNotification(const QString &roomId) const
0076 {
0077     return generateNotification(mMapTypingNotifications.value(roomId));
0078 }
0079 
0080 #include "moc_receivetypingnotificationmanager.cpp"