2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-07-21 14:37:57 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
|
|
|
#include "ImagePackListModel.h"
|
|
|
|
|
|
|
|
#include <QQmlEngine>
|
|
|
|
|
2023-10-31 18:38:15 +03:00
|
|
|
#include "Cache.h"
|
2021-07-21 14:37:57 +03:00
|
|
|
#include "SingleImagePackModel.h"
|
|
|
|
|
|
|
|
ImagePackListModel::ImagePackListModel(const std::string &roomId, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
|
|
|
, room_id(roomId)
|
|
|
|
{
|
2023-10-31 18:38:15 +03:00
|
|
|
auto packs_ = cache::getImagePacks(room_id, std::nullopt);
|
2021-07-21 14:37:57 +03:00
|
|
|
|
2021-12-29 08:01:38 +03:00
|
|
|
packs.reserve(packs_.size());
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &pack : packs_) {
|
|
|
|
packs.push_back(QSharedPointer<SingleImagePackModel>(new SingleImagePackModel(pack)));
|
|
|
|
}
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int
|
|
|
|
ImagePackListModel::rowCount(const QModelIndex &) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return (int)packs.size();
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
ImagePackListModel::roleNames() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return {
|
|
|
|
{Roles::DisplayName, "displayName"},
|
|
|
|
{Roles::AvatarUrl, "avatarUrl"},
|
|
|
|
{Roles::FromAccountData, "fromAccountData"},
|
|
|
|
{Roles::FromCurrentRoom, "fromCurrentRoom"},
|
2022-09-01 14:25:11 +03:00
|
|
|
{Roles::FromSpace, "fromSpace"},
|
2021-09-18 01:22:33 +03:00
|
|
|
{Roles::StateKey, "statekey"},
|
|
|
|
{Roles::RoomId, "roomid"},
|
|
|
|
};
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
ImagePackListModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
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;
|
2022-09-01 14:25:11 +03:00
|
|
|
case Roles::FromSpace:
|
|
|
|
return pack->fromSpace();
|
2021-09-18 01:22:33 +03:00
|
|
|
case Roles::StateKey:
|
|
|
|
return pack->statekey();
|
|
|
|
case Roles::RoomId:
|
|
|
|
return pack->roomid();
|
|
|
|
default:
|
|
|
|
return {};
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return {};
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
SingleImagePackModel *
|
|
|
|
ImagePackListModel::packAt(int row)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (row < 0 || static_cast<size_t>(row) >= packs.size())
|
|
|
|
return {};
|
|
|
|
auto e = packs.at(row).get();
|
|
|
|
QQmlEngine::setObjectOwnership(e, QQmlEngine::CppOwnership);
|
|
|
|
return e;
|
2021-07-21 14:37:57 +03:00
|
|
|
}
|
2021-08-06 05:31:30 +03:00
|
|
|
|
|
|
|
SingleImagePackModel *
|
|
|
|
ImagePackListModel::newPack(bool inRoom)
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
ImagePackInfo info{};
|
2023-05-20 00:06:14 +03:00
|
|
|
if (inRoom) {
|
2021-09-18 01:22:33 +03:00
|
|
|
info.source_room = room_id;
|
2023-05-20 00:06:14 +03:00
|
|
|
info.state_key = SingleImagePackModel::unconflictingStatekey(room_id, "");
|
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
return new SingleImagePackModel(info);
|
2021-08-06 05:31:30 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool
|
|
|
|
ImagePackListModel::containsAccountPack() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &p : packs)
|
|
|
|
if (p->roomid().isEmpty())
|
|
|
|
return true;
|
|
|
|
return false;
|
2021-08-06 05:31:30 +03:00
|
|
|
}
|
2024-03-16 03:24:33 +03:00
|
|
|
|
|
|
|
#include "moc_ImagePackListModel.cpp"
|