summary refs log tree commit diff
path: root/src/ImagePackListModel.cpp
blob: 6245acbed283ebf7984cfa365fe305847055275b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#include "ImagePackListModel.h"

#include <QQmlEngine>

#include "Cache.h"
#include "SingleImagePackModel.h"

ImagePackListModel::ImagePackListModel(const std::string &roomId, QObject *parent)
  : QAbstractListModel(parent)
  , room_id(roomId)
{
    auto packs_ = cache::getImagePacks(room_id, std::nullopt);

    packs.reserve(packs_.size());
    for (const auto &pack : packs_) {
        packs.push_back(QSharedPointer<SingleImagePackModel>(new SingleImagePackModel(pack)));
    }
}

int
ImagePackListModel::rowCount(const QModelIndex &) const
{
    return (int)packs.size();
}

QHash<int, QByteArray>
ImagePackListModel::roleNames() const
{
    return {
      {Roles::DisplayName, "displayName"},
      {Roles::AvatarUrl, "avatarUrl"},
      {Roles::FromAccountData, "fromAccountData"},
      {Roles::FromCurrentRoom, "fromCurrentRoom"},
      {Roles::FromSpace, "fromSpace"},
      {Roles::StateKey, "statekey"},
      {Roles::RoomId, "roomid"},
    };
}

QVariant
ImagePackListModel::data(const QModelIndex &index, int role) const
{
    if (hasIndex(index.row(), index.column(), index.parent())) {
        const auto &pack = packs.at(index.row());
        switch (role) {
        case Roles::DisplayName:
            return pack->packname();
        case Roles::AvatarUrl:
            return pack->avatarUrl();
        case Roles::FromAccountData:
            return pack->roomid().isEmpty();
        case Roles::FromCurrentRoom:
            return pack->roomid().toStdString() == this->room_id;
        case Roles::FromSpace:
            return pack->fromSpace();
        case Roles::StateKey:
            return pack->statekey();
        case Roles::RoomId:
            return pack->roomid();
        default:
            return {};
        }
    }
    return {};
}

SingleImagePackModel *
ImagePackListModel::packAt(int row)
{
    if (row < 0 || static_cast<size_t>(row) >= packs.size())
        return {};
    auto e = packs.at(row).get();
    QQmlEngine::setObjectOwnership(e, QQmlEngine::CppOwnership);
    return e;
}

SingleImagePackModel *
ImagePackListModel::newPack(bool inRoom)
{
    ImagePackInfo info{};
    if (inRoom) {
        info.source_room = room_id;
        info.state_key   = SingleImagePackModel::unconflictingStatekey(room_id, "");
    }
    return new SingleImagePackModel(info);
}

bool
ImagePackListModel::containsAccountPack() const
{
    for (const auto &p : packs)
        if (p->roomid().isEmpty())
            return true;
    return false;
}

#include "moc_ImagePackListModel.cpp"