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

0001 // SPDX-FileCopyrightText: 2023 James Graham <james.h.graham@protonmail.com>
0002 // SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
0003 
0004 #include "reactionmodel.h"
0005 
0006 #include <QDebug>
0007 
0008 #include <KLocalizedString>
0009 
0010 #include "neochatuser.h"
0011 
0012 ReactionModel::ReactionModel(QObject *parent, QList<Reaction> reactions, NeoChatUser *localUser)
0013     : QAbstractListModel(parent)
0014     , m_localUser(localUser)
0015 {
0016     setReactions(reactions);
0017 }
0018 
0019 QVariant ReactionModel::data(const QModelIndex &index, int role) const
0020 {
0021     if (!index.isValid()) {
0022         return {};
0023     }
0024 
0025     if (index.row() >= rowCount()) {
0026         qDebug() << "ReactionModel, something's wrong: index.row() >= rowCount()";
0027         return {};
0028     }
0029 
0030     const auto &reaction = m_reactions.at(index.row());
0031 
0032     if (role == TextRole) {
0033         if (reaction.authors.count() > 1) {
0034             return reaction.reaction + QStringLiteral("  %1").arg(reaction.authors.count());
0035         } else {
0036             return reaction.reaction;
0037         }
0038     }
0039 
0040     if (role == ReactionRole) {
0041         return reaction.reaction;
0042     }
0043 
0044     if (role == ToolTipRole) {
0045         QString text;
0046 
0047         for (int i = 0; i < reaction.authors.count() && i < 3; i++) {
0048             if (i != 0) {
0049                 if (i < reaction.authors.count() - 1) {
0050                     text += QStringLiteral(", ");
0051                 } else {
0052                     text += i18nc("Separate the usernames of users", " and ");
0053                 }
0054             }
0055             text += reaction.authors.at(i).toMap()["displayName"].toString();
0056         }
0057 
0058         if (reaction.authors.count() > 3) {
0059             text += i18ncp("%1 is the number of other users", " and %1 other", " and %1 others", reaction.authors.count() - 3);
0060         }
0061 
0062         text = i18ncp("%2 is the users who reacted and %3 the emoji that was given",
0063                       "%2 reacted with %3",
0064                       "%2 reacted with %3",
0065                       reaction.authors.count(),
0066                       text,
0067                       reaction.reaction);
0068         return text;
0069     }
0070 
0071     if (role == AuthorsRole) {
0072         return reaction.authors;
0073     }
0074 
0075     if (role == HasLocalUser) {
0076         for (auto author : reaction.authors) {
0077             if (author.toMap()["id"] == m_localUser->id()) {
0078                 return true;
0079             }
0080         }
0081         return false;
0082     }
0083 
0084     return {};
0085 }
0086 
0087 int ReactionModel::rowCount(const QModelIndex &parent) const
0088 {
0089     Q_UNUSED(parent)
0090     return m_reactions.count();
0091 }
0092 
0093 void ReactionModel::setReactions(QList<Reaction> reactions)
0094 {
0095     beginResetModel();
0096     m_reactions.clear();
0097     m_reactions = reactions;
0098     endResetModel();
0099 }
0100 
0101 QHash<int, QByteArray> ReactionModel::roleNames() const
0102 {
0103     return {
0104         {TextRole, "text"},
0105         {ReactionRole, "reaction"},
0106         {ToolTipRole, "toolTip"},
0107         {AuthorsRole, "authors"},
0108         {HasLocalUser, "hasLocalUser"},
0109     };
0110 }
0111 
0112 #include "moc_reactionmodel.cpp"