| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "ImagePackListModel.h" |
| 6 | |
| 7 | #include <QQmlEngine> |
| 8 | |
| 9 | #include "Cache.h" |
| 10 | #include "SingleImagePackModel.h" |
| 11 | |
| 12 | ImagePackListModel::ImagePackListModel(const std::string &roomId, QObject *parent) |
| 13 | : QAbstractListModel(parent) |
| 14 | , room_id(roomId) |
| 15 | { |
| 16 | auto packs_ = cache::getImagePacks(room_id, std::nullopt); |
| 17 | |
| 18 | packs.reserve(packs_.size()); |
| 19 | for (const auto &pack : packs_) { |
| 20 | packs.push_back(QSharedPointer<SingleImagePackModel>(new SingleImagePackModel(pack))); |
| 21 | } |
| 22 | } |
| 23 | |
| 24 | int |
| 25 | ImagePackListModel::rowCount(const QModelIndex &) const |
| 26 | { |
| 27 | return (int)packs.size(); |
| 28 | } |
| 29 | |
| 30 | QHash<int, QByteArray> |
| 31 | ImagePackListModel::roleNames() const |
| 32 | { |
| 33 | return { |
| 34 | {Roles::DisplayName, "displayName"}, |
| 35 | {Roles::AvatarUrl, "avatarUrl"}, |
| 36 | {Roles::FromAccountData, "fromAccountData"}, |
| 37 | {Roles::FromCurrentRoom, "fromCurrentRoom"}, |
| 38 | {Roles::FromSpace, "fromSpace"}, |
| 39 | {Roles::StateKey, "statekey"}, |
| 40 | {Roles::RoomId, "roomid"}, |
| 41 | }; |
| 42 | } |
| 43 | |
| 44 | QVariant |
| 45 | ImagePackListModel::data(const QModelIndex &index, int role) const |
| 46 | { |
| 47 | if (hasIndex(index.row(), index.column(), index.parent())) { |
| 48 | const auto &pack = packs.at(index.row()); |
| 49 | switch (role) { |
| 50 | case Roles::DisplayName: |
| 51 | return pack->packname(); |
| 52 | case Roles::AvatarUrl: |
| 53 | return pack->avatarUrl(); |
| 54 | case Roles::FromAccountData: |
| 55 | return pack->roomid().isEmpty(); |
| 56 | case Roles::FromCurrentRoom: |
| 57 | return pack->roomid().toStdString() == this->room_id; |
| 58 | case Roles::FromSpace: |
| 59 | return pack->fromSpace(); |
| 60 | case Roles::StateKey: |
| 61 | return pack->statekey(); |
| 62 | case Roles::RoomId: |
| 63 | return pack->roomid(); |
| 64 | default: |
| 65 | return {}; |
| 66 | } |
| 67 | } |
| 68 | return {}; |
| 69 | } |
| 70 | |
| 71 | SingleImagePackModel * |
| 72 | ImagePackListModel::packAt(int row) |
| 73 | { |
| 74 | if (row < 0 || static_cast<size_t>(row) >= packs.size()) |
| 75 | return {}; |
| 76 | auto e = packs.at(row).get(); |
| 77 | QQmlEngine::setObjectOwnership(e, QQmlEngine::CppOwnership); |
| 78 | return e; |
| 79 | } |
| 80 | |
| 81 | SingleImagePackModel * |
| 82 | ImagePackListModel::newPack(bool inRoom) |
| 83 | { |
| 84 | ImagePackInfo info{}; |
| 85 | if (inRoom) { |
| 86 | info.source_room = room_id; |
| 87 | info.state_key = SingleImagePackModel::unconflictingStatekey(room_id, ""); |
| 88 | } |
| 89 | return new SingleImagePackModel(info); |
| 90 | } |
| 91 | |
| 92 | bool |
| 93 | ImagePackListModel::containsAccountPack() const |
| 94 | { |
| 95 | for (const auto &p : packs) |
| 96 | if (p->roomid().isEmpty()) |
| 97 | return true; |
| 98 | return false; |
| 99 | } |
| 100 | |
| 101 | #include "moc_ImagePackListModel.cpp" |