| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "GridImagePackModel.h" |
| 6 | |
| 7 | #include <QCoreApplication> |
| 8 | #include <QTextBoundaryFinder> |
| 9 | |
| 10 | #include <algorithm> |
| 11 | |
| 12 | #include "Cache.h" |
| 13 | #include "emoji/Provider.h" |
| 14 | |
| 15 | static QString |
| 16 | categoryToIcon(emoji::Emoji::Category cat) |
| 17 | { |
| 18 | switch (cat) { |
| 19 | case emoji::Emoji::Category::People: |
| 20 | return QStringLiteral(":/icons/icons/emoji-categories/people.svg"); |
| 21 | case emoji::Emoji::Category::Nature: |
| 22 | return QStringLiteral(":/icons/icons/emoji-categories/nature.svg"); |
| 23 | case emoji::Emoji::Category::Food: |
| 24 | return QStringLiteral(":/icons/icons/emoji-categories/foods.svg"); |
| 25 | case emoji::Emoji::Category::Activity: |
| 26 | return QStringLiteral(":/icons/icons/emoji-categories/activity.svg"); |
| 27 | case emoji::Emoji::Category::Travel: |
| 28 | return QStringLiteral(":/icons/icons/emoji-categories/travel.svg"); |
| 29 | case emoji::Emoji::Category::Objects: |
| 30 | return QStringLiteral(":/icons/icons/emoji-categories/objects.svg"); |
| 31 | case emoji::Emoji::Category::Symbols: |
| 32 | return QStringLiteral(":/icons/icons/emoji-categories/symbols.svg"); |
| 33 | case emoji::Emoji::Category::Flags: |
| 34 | return QStringLiteral(":/icons/icons/emoji-categories/flags.svg"); |
| 35 | default: |
| 36 | return ""; |
| 37 | } |
| 38 | } |
| 39 | |
| 40 | GridImagePackModel::GridImagePackModel(const std::string &roomId, bool stickers, QObject *parent) |
| 41 | : QAbstractListModel(parent) |
| 42 | , room_id(roomId) |
| 43 | , columns(stickers ? 3 : 7) |
| 44 | { |
| 45 | if (!stickers) { |
| 46 | for (const auto &category : { |
| 47 | emoji::Emoji::Category::People, |
| 48 | emoji::Emoji::Category::Nature, |
| 49 | emoji::Emoji::Category::Food, |
| 50 | emoji::Emoji::Category::Activity, |
| 51 | emoji::Emoji::Category::Travel, |
| 52 | emoji::Emoji::Category::Objects, |
| 53 | emoji::Emoji::Category::Symbols, |
| 54 | emoji::Emoji::Category::Flags, |
| 55 | }) { |
| 56 | PackDesc newPack{}; |
| 57 | newPack.packname = categoryToName(category); |
| 58 | newPack.packavatar = categoryToIcon(category); |
| 59 | |
| 60 | auto emojisInCategory = std::ranges::equal_range( |
| 61 | emoji::Provider::emoji, category, {}, &emoji::Emoji::category); |
| 62 | newPack.emojis.reserve(emojisInCategory.size()); |
| 63 | |
| 64 | for (const auto &e : emojisInCategory) { |
| 65 | newPack.emojis.push_back(TextEmoji{.unicode = e.unicode(), |
| 66 | .unicodeName = e.unicodeName(), |
| 67 | .shortcode = e.shortName()}); |
| 68 | } |
| 69 | |
| 70 | size_t packRowCount = |
| 71 | (newPack.emojis.size() / columns) + (newPack.emojis.size() % columns ? 1 : 0); |
| 72 | newPack.firstRow = rowToPack.size(); |
| 73 | for (size_t i = 0; i < packRowCount; i++) |
| 74 | rowToPack.push_back(packs.size()); |
| 75 | packs.push_back(std::move(newPack)); |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | auto originalPacks = cache::getImagePacks(room_id, stickers); |
| 80 | |
| 81 | for (auto &pack : originalPacks) { |
| 82 | PackDesc newPack{}; |
| 83 | newPack.packname = |
| 84 | pack.pack.pack ? QString::fromStdString(pack.pack.pack->display_name) : QString(); |
| 85 | newPack.packavatar = |
| 86 | pack.pack.pack ? QString::fromStdString(pack.pack.pack->avatar_url) : QString(); |
| 87 | newPack.room_id = pack.source_room; |
| 88 | newPack.state_key = pack.state_key; |
| 89 | |
| 90 | newPack.images.resize(pack.pack.images.size()); |
| 91 | std::ranges::transform(std::move(pack.pack.images), newPack.images.begin(), [](auto &&img) { |
| 92 | return std::pair(std::move(img.second), QString::fromStdString(img.first)); |
| 93 | }); |
| 94 | |
| 95 | size_t packRowCount = |
| 96 | (newPack.images.size() / columns) + (newPack.images.size() % columns ? 1 : 0); |
| 97 | newPack.firstRow = rowToPack.size(); |
| 98 | for (size_t i = 0; i < packRowCount; i++) |
| 99 | rowToPack.push_back(packs.size()); |
| 100 | packs.push_back(std::move(newPack)); |
| 101 | } |
| 102 | |
| 103 | // prepare search index |
| 104 | |
| 105 | auto insertParts = [this](const QString &str, std::pair<std::uint32_t, std::uint32_t> id) { |
| 106 | QTextBoundaryFinder finder(QTextBoundaryFinder::BoundaryType::Word, str); |
| 107 | finder.toStart(); |
| 108 | do { |
| 109 | auto start = finder.position(); |
| 110 | finder.toNextBoundary(); |
| 111 | auto end = finder.position(); |
| 112 | |
| 113 | auto ref = QStringView(str).mid(start, end - start).trimmed(); |
| 114 | if (!ref.isEmpty()) |
| 115 | trie_.insert<ElementRank::second>(ref.toUcs4(), id); |
| 116 | } while (finder.position() < str.size()); |
| 117 | }; |
| 118 | |
| 119 | std::uint32_t packIndex = 0; |
| 120 | for (const auto &pack : packs) { |
| 121 | std::uint32_t emojiIndex = 0; |
| 122 | for (const auto &emoji : pack.emojis) { |
| 123 | std::pair<std::uint32_t, std::uint32_t> key{packIndex, emojiIndex}; |
| 124 | |
| 125 | QString string1 = emoji.shortcode.toCaseFolded(); |
| 126 | QString string2 = emoji.unicodeName.toCaseFolded(); |
| 127 | |
| 128 | if (!string1.isEmpty()) { |
| 129 | trie_.insert<ElementRank::first>(string1.toUcs4(), key); |
| 130 | insertParts(string1, key); |
| 131 | } |
| 132 | if (!string2.isEmpty()) { |
| 133 | trie_.insert<ElementRank::first>(string2.toUcs4(), key); |
| 134 | insertParts(string2, key); |
| 135 | } |
| 136 | |
| 137 | emojiIndex++; |
| 138 | } |
| 139 | |
| 140 | std::uint32_t imgIndex = 0; |
| 141 | for (const auto &img : pack.images) { |
| 142 | std::pair<std::uint32_t, std::uint32_t> key{packIndex, imgIndex}; |
| 143 | |
| 144 | QString string1 = img.second.toCaseFolded(); |
| 145 | QString string2 = QString::fromStdString(img.first.body).toCaseFolded(); |
| 146 | |
| 147 | if (!string1.isEmpty()) { |
| 148 | trie_.insert<ElementRank::first>(string1.toUcs4(), key); |
| 149 | insertParts(string1, key); |
| 150 | } |
| 151 | if (!string2.isEmpty()) { |
| 152 | trie_.insert<ElementRank::first>(string2.toUcs4(), key); |
| 153 | insertParts(string2, key); |
| 154 | } |
| 155 | |
| 156 | imgIndex++; |
| 157 | } |
| 158 | packIndex++; |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | int |
| 163 | GridImagePackModel::rowCount(const QModelIndex &) const |
| 164 | { |
| 165 | return static_cast<int>(searchString_.isEmpty() ? rowToPack.size() |
| 166 | : rowToFirstRowEntryFromSearch.size()); |
| 167 | } |
| 168 | |
| 169 | QHash<int, QByteArray> |
| 170 | GridImagePackModel::roleNames() const |
| 171 | { |
| 172 | return { |
| 173 | {Roles::PackName, "packname"}, |
| 174 | {Roles::Row, "row"}, |
| 175 | }; |
| 176 | } |
| 177 | |
| 178 | QVariant |
| 179 | GridImagePackModel::data(const QModelIndex &index, int role) const |
| 180 | { |
| 181 | if (index.row() < rowCount() && index.row() >= 0) { |
| 182 | if (searchString_.isEmpty()) { |
| 183 | const auto &pack = packs[rowToPack[index.row()]]; |
| 184 | switch (role) { |
| 185 | case Roles::PackName: |
| 186 | return nameFromPack(pack); |
| 187 | case Roles::Row: { |
| 188 | std::size_t offset = static_cast<std::size_t>(index.row()) - pack.firstRow; |
| 189 | if (pack.emojis.empty()) { |
| 190 | QList<StickerImage> imgs; |
| 191 | auto endOffset = std::min((offset + 1) * columns, pack.images.size()); |
| 192 | for (std::size_t img = offset * columns; img < endOffset; img++) { |
| 193 | const auto &data = pack.images.at(img); |
| 194 | // See |
| 195 | // https://developercommunity.visualstudio.com/t/Internal-compile-error-while-compiling-c/1227337 |
| 196 | imgs.push_back({/*.url =*/QString::fromStdString(data.first.url), |
| 197 | /*.shortcode =*/data.second, |
| 198 | /*.body =*/QString::fromStdString(data.first.body), |
| 199 | /*.descriptor_=*/ |
| 200 | std::vector{ |
| 201 | pack.room_id, |
| 202 | pack.state_key, |
| 203 | data.second.toStdString(), |
| 204 | }}); |
| 205 | } |
| 206 | return QVariant::fromValue(imgs); |
| 207 | } else { |
| 208 | auto endOffset = std::min((offset + 1) * columns, pack.emojis.size()); |
| 209 | QList<TextEmoji> imgs(pack.emojis.begin() + offset * columns, |
| 210 | pack.emojis.begin() + endOffset); |
| 211 | |
| 212 | return QVariant::fromValue(imgs); |
| 213 | } |
| 214 | } |
| 215 | default: |
| 216 | return {}; |
| 217 | } |
| 218 | } else { |
| 219 | if (static_cast<size_t>(index.row()) >= rowToFirstRowEntryFromSearch.size()) |
| 220 | return {}; |
| 221 | |
| 222 | const auto firstIndex = rowToFirstRowEntryFromSearch[index.row()]; |
| 223 | const auto firstEntry = currentSearchResult[firstIndex]; |
| 224 | const auto &pack = packs[firstEntry.first]; |
| 225 | |
| 226 | switch (role) { |
| 227 | case Roles::PackName: |
| 228 | return nameFromPack(pack); |
| 229 | case Roles::Row: { |
| 230 | if (pack.emojis.empty()) { |
| 231 | QList<StickerImage> imgs; |
| 232 | for (auto img = firstIndex; |
| 233 | imgs.size() < columns && img < currentSearchResult.size() && |
| 234 | currentSearchResult[img].first == firstEntry.first; |
| 235 | img++) { |
| 236 | const auto &data = pack.images.at(currentSearchResult[img].second); |
| 237 | // See |
| 238 | // https://developercommunity.visualstudio.com/t/Internal-compile-error-while-compiling-c/1227337 |
| 239 | imgs.push_back({/*.url = */ QString::fromStdString(data.first.url), |
| 240 | /*.shortcode = */ data.second, |
| 241 | /*.body = */ QString::fromStdString(data.first.body), |
| 242 | /*.descriptor_ = */ |
| 243 | std::vector{ |
| 244 | pack.room_id, |
| 245 | pack.state_key, |
| 246 | data.second.toStdString(), |
| 247 | }}); |
| 248 | } |
| 249 | return QVariant::fromValue(imgs); |
| 250 | } else { |
| 251 | QList<TextEmoji> emojis; |
| 252 | for (auto emoji = firstIndex; |
| 253 | emojis.size() < columns && emoji < currentSearchResult.size() && |
| 254 | currentSearchResult[emoji].first == firstEntry.first; |
| 255 | emoji++) { |
| 256 | emojis.push_back(pack.emojis.at(currentSearchResult[emoji].second)); |
| 257 | } |
| 258 | return QVariant::fromValue(emojis); |
| 259 | } |
| 260 | } |
| 261 | default: |
| 262 | return {}; |
| 263 | } |
| 264 | } |
| 265 | } |
| 266 | return {}; |
| 267 | } |
| 268 | |
| 269 | QString |
| 270 | GridImagePackModel::nameFromPack(const PackDesc &pack) const |
| 271 | { |
| 272 | if (!pack.packname.isEmpty()) { |
| 273 | return pack.packname; |
| 274 | } |
| 275 | |
| 276 | if (!pack.state_key.empty()) { |
| 277 | return QString::fromStdString(pack.state_key); |
| 278 | } |
| 279 | |
| 280 | if (!pack.room_id.empty()) { |
| 281 | auto info = cache::singleRoomInfo(pack.room_id); |
| 282 | return QString::fromStdString(info.name); |
| 283 | } |
| 284 | |
| 285 | return tr("Account Pack"); |
| 286 | } |
| 287 | |
| 288 | QString |
| 289 | GridImagePackModel::avatarFromPack(const PackDesc &pack) const |
| 290 | { |
| 291 | if (!pack.packavatar.isEmpty()) { |
| 292 | return pack.packavatar; |
| 293 | } |
| 294 | |
| 295 | if (!pack.images.empty()) { |
| 296 | return QString::fromStdString(pack.images.begin()->first.url); |
| 297 | } |
| 298 | |
| 299 | return ""; |
| 300 | } |
| 301 | |
| 302 | QList<SectionDescription> |
| 303 | GridImagePackModel::sections() const |
| 304 | { |
| 305 | QList<SectionDescription> sectionNames; |
| 306 | if (searchString_.isEmpty()) { |
| 307 | std::size_t packIdx = -1; |
| 308 | for (std::size_t i = 0; i < rowToPack.size(); i++) { |
| 309 | if (rowToPack[i] != packIdx) { |
| 310 | const auto &pack = packs[rowToPack[i]]; |
| 311 | sectionNames.push_back({ |
| 312 | .name = nameFromPack(pack), |
| 313 | .url = avatarFromPack(pack), |
| 314 | .firstRowWith = static_cast<int>(i), |
| 315 | }); |
| 316 | packIdx = rowToPack[i]; |
| 317 | } |
| 318 | } |
| 319 | } else { |
| 320 | std::uint32_t packIdx = -1; |
| 321 | int row = 0; |
| 322 | for (const auto &i : rowToFirstRowEntryFromSearch) { |
| 323 | const auto res = currentSearchResult[i]; |
| 324 | if (res.first != packIdx) { |
| 325 | packIdx = res.first; |
| 326 | const auto &pack = packs[packIdx]; |
| 327 | sectionNames.push_back({ |
| 328 | .name = nameFromPack(pack), |
| 329 | .url = avatarFromPack(pack), |
| 330 | .firstRowWith = row, |
| 331 | }); |
| 332 | } |
| 333 | row++; |
| 334 | } |
| 335 | } |
| 336 | |
| 337 | return sectionNames; |
| 338 | } |
| 339 | |
| 340 | void |
| 341 | GridImagePackModel::setSearchString(QString key) |
| 342 | { |
| 343 | beginResetModel(); |
| 344 | currentSearchResult.clear(); |
| 345 | rowToFirstRowEntryFromSearch.clear(); |
| 346 | searchString_ = key; |
| 347 | |
| 348 | if (!key.isEmpty()) { |
| 349 | auto searchParts = key.toCaseFolded().toUcs4(); |
| 350 | auto tempResults = |
| 351 | trie_.search(searchParts, static_cast<std::size_t>(columns * columns * 4)); |
| 352 | |
| 353 | std::map<std::uint32_t, std::size_t> firstPositionOfPack; |
| 354 | for (const auto &e : tempResults) |
| 355 | firstPositionOfPack.emplace(e.first, firstPositionOfPack.size()); |
| 356 | |
| 357 | std::ranges::stable_sort(tempResults, [&firstPositionOfPack](auto a, auto b) { |
| 358 | return firstPositionOfPack[a.first] < firstPositionOfPack[b.first]; |
| 359 | }); |
| 360 | currentSearchResult = std::move(tempResults); |
| 361 | |
| 362 | std::size_t lastPack = -1; |
| 363 | int columnIndex = 0; |
| 364 | for (std::size_t i = 0; i < currentSearchResult.size(); i++) { |
| 365 | auto elem = currentSearchResult[i]; |
| 366 | if (elem.first != lastPack || columnIndex == columns) { |
| 367 | columnIndex = 0; |
| 368 | lastPack = elem.first; |
| 369 | rowToFirstRowEntryFromSearch.push_back(i); |
| 370 | } |
| 371 | columnIndex++; |
| 372 | } |
| 373 | } |
| 374 | |
| 375 | endResetModel(); |
| 376 | emit newSearchString(); |
| 377 | } |
| 378 | |
| 379 | #include "moc_GridImagePackModel.cpp" |