File indexing completed on 2024-12-08 04:33:17

0001 /*
0002    SPDX-FileCopyrightText: 2022-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "notificationhistorymodel.h"
0008 #include <KLocalizedString>
0009 
0010 NotificationHistoryModel::NotificationHistoryModel(QObject *parent)
0011     : QAbstractListModel{parent}
0012 {
0013 }
0014 
0015 NotificationHistoryModel::~NotificationHistoryModel() = default;
0016 
0017 int NotificationHistoryModel::rowCount(const QModelIndex &parent) const
0018 {
0019     Q_UNUSED(parent)
0020     return mNotificationInfo.count();
0021 }
0022 
0023 QVariant NotificationHistoryModel::data(const QModelIndex &index, int role) const
0024 {
0025     if (index.row() < 0 || index.row() >= mNotificationInfo.count()) {
0026         return {};
0027     }
0028     const auto info = mNotificationInfo.at(index.row());
0029     switch (role) {
0030     case Qt::DisplayRole:
0031     case AccountName:
0032         return info.accountName();
0033     case DateTime:
0034         return info.dateTime();
0035     case MessageStr:
0036         return generateMessage(info);
0037     case RoomId:
0038         return info.roomId();
0039     case ChannelType:
0040         return info.channelType();
0041     case Pixmap:
0042         return info.pixmap();
0043     case SenderName:
0044         return info.senderName();
0045     case SenderUserName:
0046         return info.senderUserName();
0047     case MessageId:
0048         return info.messageId();
0049     case RoomName:
0050         return info.roomName();
0051     }
0052     return {};
0053 }
0054 
0055 QString NotificationHistoryModel::generateMessage(const NotificationInfo &info) const
0056 {
0057     switch (info.notificationType()) {
0058     case NotificationInfo::StandardMessage:
0059         return info.message();
0060     case NotificationInfo::ConferenceCall:
0061         return i18n("Conference Call");
0062     }
0063     return {};
0064 }
0065 
0066 void NotificationHistoryModel::clear()
0067 {
0068     if (!mNotificationInfo.isEmpty()) {
0069         beginResetModel();
0070         mNotificationInfo.clear();
0071         endResetModel();
0072     }
0073 }
0074 
0075 void NotificationHistoryModel::insertNotifications(const QVector<NotificationInfo> &infos)
0076 {
0077     clear();
0078     if (!infos.isEmpty()) {
0079         beginInsertRows(QModelIndex(), 0, infos.count() - 1);
0080         mNotificationInfo = infos;
0081         endInsertRows();
0082     }
0083 }
0084 
0085 void NotificationHistoryModel::addNotification(const NotificationInfo &info)
0086 {
0087     if (mNotificationInfo.contains(info)) {
0088         return;
0089     }
0090     const int numberOfElement = mNotificationInfo.count();
0091     mNotificationInfo.append(info);
0092     beginInsertRows(QModelIndex(), numberOfElement, mNotificationInfo.count() - 1);
0093     endInsertRows();
0094 }
0095 
0096 #include "moc_notificationhistorymodel.cpp"