File indexing completed on 2024-05-12 05:02:05
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 "emoticonmodel.h" 0008 #include "emoticons/emojimanager.h" 0009 #include "rocketchataccount.h" 0010 #include <KLocalizedString> 0011 #include <QIcon> 0012 #include <QUrl> 0013 0014 EmoticonModel::EmoticonModel(RocketChatAccount *account, QObject *parent) 0015 : QAbstractListModel(parent) 0016 , mRocketChatAccount(account) 0017 { 0018 } 0019 0020 EmoticonModel::~EmoticonModel() = default; 0021 0022 int EmoticonModel::rowCount(const QModelIndex &parent) const 0023 { 0024 if (parent.isValid()) { 0025 return 0; // flat model 0026 } 0027 return mUnicodeRows.count() + mCustomRows.count(); 0028 } 0029 0030 QIcon EmoticonModel::createCustomIcon(const QString &name) const 0031 { 0032 if (mRocketChatAccount) { 0033 const QString fileName = mRocketChatAccount->emojiManager()->customEmojiFileName(name); 0034 if (!fileName.isEmpty()) { 0035 const QUrl emojiUrl = mRocketChatAccount->attachmentUrlFromLocalCache(fileName); 0036 if (!emojiUrl.isEmpty()) { 0037 const QIcon icon(emojiUrl.toLocalFile()); 0038 return icon; 0039 } 0040 } 0041 } 0042 return {}; 0043 } 0044 0045 QVariant EmoticonModel::data(const QModelIndex &index, int role) const 0046 { 0047 if (index.row() < 0 || index.row() >= (mUnicodeRows.count() + mCustomRows.count())) { 0048 return {}; 0049 } 0050 0051 if (index.row() < mUnicodeRows.count()) { 0052 const auto &row = mUnicodeRows.at(index.row()); 0053 if (row.first < mEmoticons.count()) { 0054 const TextEmoticonsCore::UnicodeEmoticon &unicodeEmoti = mEmoticons.at(row.first); 0055 0056 switch (role) { 0057 case CompleterName: 0058 return unicodeEmoti.identifier().mid(1); 0059 case Qt::DisplayRole: // for the completion popup (until we have a delegate) 0060 case UnicodeEmojiRole: 0061 return unicodeEmoti.unicode(); 0062 case Category: 0063 return unicodeEmoti.category(); 0064 case IconRole: 0065 return {}; 0066 case Qt::ToolTipRole: 0067 case IdentifierRole: 0068 if (row.second == -1) { 0069 return unicodeEmoti.identifier(); 0070 } 0071 return unicodeEmoti.aliases().value(row.second); 0072 } 0073 } 0074 } else { 0075 const auto &row = mCustomRows.at(index.row() - mUnicodeRows.count()); 0076 if (row.first < mUnicodeRows.count()) { 0077 const CustomEmoji &customEmoti = mCustomEmojiList.at(row.first); 0078 switch (role) { 0079 case CompleterName: 0080 return customEmoti.emojiIdentifier().mid(1); 0081 case Qt::DisplayRole: 0082 case UnicodeEmojiRole: 0083 return customEmoti.emojiIdentifier(); 0084 case Category: 0085 return i18n("Custom"); 0086 case IconRole: 0087 case Qt::DecorationRole: 0088 return createCustomIcon(customEmoti.emojiIdentifier()); 0089 case Qt::ToolTipRole: 0090 case IdentifierRole: 0091 if (row.second == -1) { 0092 return customEmoti.emojiIdentifier(); 0093 } 0094 return customEmoti.aliases().value(row.second); 0095 } 0096 } 0097 } 0098 return {}; 0099 } 0100 0101 QList<TextEmoticonsCore::UnicodeEmoticon> EmoticonModel::unicodeEmoticons() const 0102 { 0103 return mEmoticons; 0104 } 0105 0106 void EmoticonModel::setUnicodeEmoticons(const QList<TextEmoticonsCore::UnicodeEmoticon> &emoticons) 0107 { 0108 beginResetModel(); 0109 mEmoticons = emoticons; 0110 mUnicodeRows.clear(); 0111 int row = 0; 0112 for (const auto &emoticon : emoticons) { 0113 mUnicodeRows.append({row, -1}); 0114 const auto numAliases = emoticon.aliases().size(); 0115 for (int i = 0; i < numAliases; ++i) { 0116 mUnicodeRows.append({row, i}); 0117 } 0118 ++row; 0119 } 0120 endResetModel(); 0121 } 0122 0123 const QVector<CustomEmoji> &EmoticonModel::customEmojiList() const 0124 { 0125 return mCustomEmojiList; 0126 } 0127 0128 void EmoticonModel::setCustomEmojiList(const QVector<CustomEmoji> &newCustomEmojiList) 0129 { 0130 beginResetModel(); 0131 mCustomEmojiList = newCustomEmojiList; 0132 mCustomRows.clear(); 0133 int row = 0; 0134 for (const auto &emoticon : newCustomEmojiList) { 0135 mCustomRows.append({row, -1}); 0136 const auto numAliases = emoticon.aliases().size(); 0137 for (int i = 0; i < numAliases; ++i) { 0138 mCustomRows.append({row, i}); 0139 } 0140 ++row; 0141 } 0142 endResetModel(); 0143 } 0144 0145 #include "moc_emoticonmodel.cpp"