File indexing completed on 2024-12-08 12:53:30

0001 /*
0002    SPDX-FileCopyrightText: 2021-2024 Laurent Montel <montel@kde.org>
0003 
0004    SPDX-License-Identifier: LGPL-2.0-or-later
0005 */
0006 
0007 #include "admincustomemojimodel.h"
0008 #include "emoticons/emojimanager.h"
0009 #include "rocketchataccount.h"
0010 #include <KLocalizedString>
0011 #include <QIcon>
0012 
0013 AdminCustomEmojiModel::AdminCustomEmojiModel(RocketChatAccount *account, QObject *parent)
0014     : CustomBaseModel(parent)
0015     , mRocketChatAccount(account)
0016 {
0017 }
0018 
0019 AdminCustomEmojiModel::~AdminCustomEmojiModel() = default;
0020 
0021 int AdminCustomEmojiModel::rowCount(const QModelIndex &parent) const
0022 {
0023     if (parent.isValid()) {
0024         return 0; // flat model
0025     }
0026     return mCustomEmojiList.count();
0027 }
0028 
0029 QVariant AdminCustomEmojiModel::headerData(int section, Qt::Orientation orientation, int role) const
0030 {
0031     if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
0032         switch (static_cast<CustomEmojiRoles>(section)) {
0033         case CustomEmojiRoles::Name:
0034             return i18n("Name");
0035         case CustomEmojiRoles::Identifier:
0036             return i18n("Identifier");
0037         case CustomEmojiRoles::Aliases:
0038             return i18n("Aliases");
0039         case CustomEmojiRoles::Icon:
0040             return {};
0041         }
0042     }
0043     return {};
0044 }
0045 
0046 int AdminCustomEmojiModel::columnCount(const QModelIndex &parent) const
0047 {
0048     Q_UNUSED(parent)
0049     constexpr int val = static_cast<int>(CustomEmojiRoles::LastColumn) + 1;
0050     return val;
0051 }
0052 
0053 QVariant AdminCustomEmojiModel::data(const QModelIndex &index, int role) const
0054 {
0055     if (index.row() < 0 || index.row() >= mCustomEmojiList.count()) {
0056         return {};
0057     }
0058     if (role != Qt::DisplayRole) {
0059         return {};
0060     }
0061 
0062     const CustomEmoji &customEmoji = mCustomEmojiList.at(index.row());
0063     const int col = index.column();
0064     switch (static_cast<CustomEmojiRoles>(col)) {
0065     case CustomEmojiRoles::Name:
0066         return customEmoji.name();
0067     case CustomEmojiRoles::Identifier:
0068         return customEmoji.identifier();
0069     case CustomEmojiRoles::Aliases:
0070         return customEmoji.aliases().join(QLatin1Char(','));
0071     case CustomEmojiRoles::Icon:
0072         return createCustomIcon(customEmoji.identifier());
0073     }
0074     return {};
0075 }
0076 
0077 int AdminCustomEmojiModel::total() const
0078 {
0079     return mCustomEmojiList.count();
0080 }
0081 
0082 void AdminCustomEmojiModel::clear()
0083 {
0084     if (!mCustomEmojiList.isEmpty()) {
0085         beginResetModel();
0086         mCustomEmojiList.clear();
0087         endResetModel();
0088     }
0089 }
0090 
0091 void AdminCustomEmojiModel::parseElements(const QJsonObject &obj)
0092 {
0093     clear();
0094     mCustomEmojiList.parseCustomEmojis(obj);
0095     if (!mCustomEmojiList.isEmpty()) {
0096         beginInsertRows(QModelIndex(), 0, mCustomEmojiList.count() - 1);
0097         endInsertRows();
0098     }
0099     checkFullList();
0100     Q_EMIT totalChanged();
0101 }
0102 
0103 void AdminCustomEmojiModel::checkFullList()
0104 {
0105     setHasFullList(mCustomEmojiList.count() == mCustomEmojiList.total());
0106 }
0107 
0108 const CustomEmojisInfo &AdminCustomEmojiModel::customEmojis() const
0109 {
0110     return mCustomEmojiList;
0111 }
0112 
0113 void AdminCustomEmojiModel::setCustomEmojis(const CustomEmojisInfo &newCustomEmojis)
0114 {
0115     clear();
0116     if (!mCustomEmojiList.isEmpty()) {
0117         beginInsertRows(QModelIndex(), 0, mCustomEmojiList.count() - 1);
0118         mCustomEmojiList = newCustomEmojis;
0119         endInsertRows();
0120     }
0121 }
0122 
0123 void AdminCustomEmojiModel::removeElement(const QString &identifier)
0124 {
0125     const int userCount = mCustomEmojiList.count();
0126     for (int i = 0; i < userCount; ++i) {
0127         if (mCustomEmojiList.at(i).identifier() == identifier) {
0128             beginRemoveRows(QModelIndex(), i, i);
0129             mCustomEmojiList.takeAt(i);
0130             mCustomEmojiList.setTotal(mCustomEmojiList.count()); // Update total
0131             endRemoveRows();
0132             Q_EMIT totalChanged();
0133             break;
0134         }
0135     }
0136 }
0137 
0138 void AdminCustomEmojiModel::addMoreElements(const QJsonObject &obj)
0139 {
0140     const int numberOfElement = mCustomEmojiList.count();
0141     mCustomEmojiList.parseCustomEmojis(obj);
0142     beginInsertRows(QModelIndex(), numberOfElement, mCustomEmojiList.count() - 1);
0143     endInsertRows();
0144     checkFullList();
0145 }
0146 
0147 QList<int> AdminCustomEmojiModel::hideColumns() const
0148 {
0149     return {CustomEmojiRoles::Identifier, CustomEmojiRoles::Icon};
0150 }
0151 
0152 QIcon AdminCustomEmojiModel::createCustomIcon(const QString &identifier) const
0153 {
0154     if (mRocketChatAccount) {
0155         const QString fileName = mRocketChatAccount->emojiManager()->customEmojiFileNameFromIdentifier(identifier);
0156         if (!fileName.isEmpty()) {
0157             const QUrl emojiUrl = mRocketChatAccount->attachmentUrlFromLocalCache(fileName);
0158             if (!emojiUrl.isEmpty()) {
0159                 const QIcon icon(emojiUrl.toLocalFile());
0160                 return icon;
0161             }
0162         }
0163     }
0164     return {};
0165 }
0166 
0167 #include "moc_admincustomemojimodel.cpp"