Port to Integral
This commit is contained in:
@@ -1,23 +1,45 @@
|
||||
|
||||
// SPDX-FileCopyrightText: 2023 Tobias Fella <tobias.fella@kde.org>
|
||||
// SPDX-License-Identifier: LGPL-2.0-or-later
|
||||
|
||||
#include "roomtreemodel.h"
|
||||
|
||||
#include <Quotient/room.h>
|
||||
|
||||
#include "eventhandler.h"
|
||||
#include "neochatconnection.h"
|
||||
// #include "eventhandler.h"
|
||||
#include "neochatroomtype.h"
|
||||
#include "spacehierarchycache.h"
|
||||
#include "rust/cxx.h"
|
||||
#include <Integral/lib.rs.h>
|
||||
// #include "spacehierarchycache.h"
|
||||
#include <Integral/RoomStream>
|
||||
#include <Integral/Utils>
|
||||
|
||||
using namespace Quotient;
|
||||
using namespace Integral;
|
||||
|
||||
class RoomTreeModel::Private
|
||||
{
|
||||
public:
|
||||
QPointer<Integral::Connection> connection;
|
||||
std::unique_ptr<RoomStream> roomStream = nullptr;
|
||||
std::unique_ptr<RoomTreeItem> rootItem;
|
||||
// Since the rooms are streamed as vector diffs we need to keep track of them
|
||||
// for things like the index value of insert to make sense.
|
||||
QList<QPersistentModelIndex> roomIndexes;
|
||||
|
||||
void roomsUpdate();
|
||||
void resetTree();
|
||||
|
||||
RoomTreeModel *q = nullptr;
|
||||
};
|
||||
|
||||
RoomTreeModel::RoomTreeModel(QObject *parent)
|
||||
: QAbstractItemModel(parent)
|
||||
, m_rootItem(new RoomTreeItem(nullptr))
|
||||
, d(std::make_unique<Private>())
|
||||
{
|
||||
d->q = this;
|
||||
}
|
||||
|
||||
RoomTreeModel::~RoomTreeModel() = default;
|
||||
|
||||
RoomTreeItem *RoomTreeModel::getItem(const QModelIndex &index) const
|
||||
{
|
||||
if (index.isValid()) {
|
||||
@@ -26,179 +48,226 @@ RoomTreeItem *RoomTreeModel::getItem(const QModelIndex &index) const
|
||||
return item;
|
||||
}
|
||||
}
|
||||
return m_rootItem.get();
|
||||
return d->rootItem.get();
|
||||
}
|
||||
|
||||
void RoomTreeModel::resetModel()
|
||||
{
|
||||
if (m_connection == nullptr) {
|
||||
if (d->connection == nullptr) {
|
||||
beginResetModel();
|
||||
m_rootItem.reset();
|
||||
d->rootItem.reset();
|
||||
d->roomStream.reset();
|
||||
endResetModel();
|
||||
return;
|
||||
}
|
||||
|
||||
beginResetModel();
|
||||
m_rootItem.reset(new RoomTreeItem(nullptr));
|
||||
d->resetTree();
|
||||
|
||||
for (int i = 0; i < NeoChatRoomType::TypesCount; i++) {
|
||||
m_rootItem->insertChild(std::make_unique<RoomTreeItem>(NeoChatRoomType::Types(i), m_rootItem.get()));
|
||||
}
|
||||
d->roomStream = d->connection->roomStream();
|
||||
connect(d->roomStream.get(), &RoomStream::roomsUpdate, this, [this]() {
|
||||
d->roomsUpdate();
|
||||
});
|
||||
|
||||
for (const auto &r : m_connection->allRooms()) {
|
||||
const auto room = dynamic_cast<NeoChatRoom *>(r);
|
||||
const auto type = NeoChatRoomType::typeForRoom(room);
|
||||
const auto categoryItem = m_rootItem->child(type);
|
||||
if (categoryItem->insertChild(std::make_unique<RoomTreeItem>(room, categoryItem))) {
|
||||
connectRoomSignals(room);
|
||||
}
|
||||
}
|
||||
d->roomStream->startStream();
|
||||
|
||||
endResetModel();
|
||||
}
|
||||
|
||||
void RoomTreeModel::setConnection(NeoChatConnection *connection)
|
||||
void RoomTreeModel::Private::resetTree()
|
||||
{
|
||||
if (m_connection == connection) {
|
||||
rootItem.reset(new RoomTreeItem(nullptr));
|
||||
for (int i = 0; i < NeoChatRoomType::TypesCount; i++) {
|
||||
rootItem->insertChild(std::make_unique<RoomTreeItem>(NeoChatRoomType::Type(i), rootItem.get()));
|
||||
}
|
||||
}
|
||||
|
||||
void RoomTreeModel::setConnection(Connection *connection)
|
||||
{
|
||||
if (d->connection == connection) {
|
||||
return;
|
||||
}
|
||||
if (m_connection) {
|
||||
disconnect(m_connection.get(), nullptr, this, nullptr);
|
||||
if (d->connection) {
|
||||
d->connection->disconnect(this);
|
||||
}
|
||||
m_connection = connection;
|
||||
d->connection = connection;
|
||||
|
||||
resetModel();
|
||||
|
||||
connect(connection, &Connection::newRoom, this, &RoomTreeModel::newRoom);
|
||||
connect(connection, &Connection::leftRoom, this, &RoomTreeModel::leftRoom);
|
||||
connect(connection, &Connection::aboutToDeleteRoom, this, &RoomTreeModel::leftRoom);
|
||||
|
||||
Q_EMIT connectionChanged();
|
||||
}
|
||||
|
||||
void RoomTreeModel::newRoom(Room *r)
|
||||
void RoomTreeModel::Private::roomsUpdate()
|
||||
{
|
||||
const auto room = dynamic_cast<NeoChatRoom *>(r);
|
||||
const auto type = NeoChatRoomType::typeForRoom(room);
|
||||
// Check if the room is already in the model.
|
||||
const auto checkRoomIndex = indexForRoom(room);
|
||||
if (checkRoomIndex.isValid()) {
|
||||
// If the room is in the wrong type category for whatever reason, move it.
|
||||
if (checkRoomIndex.parent().row() != type) {
|
||||
moveRoom(room);
|
||||
const auto diff = roomStream->next();
|
||||
|
||||
switch (diff->op()) {
|
||||
case 0: { // Append
|
||||
for (const auto &it : diff->items_vec()) {
|
||||
const auto type = NeoChatRoomType::typeForRoom(it.box_me());
|
||||
const auto parentItem = rootItem->child(type);
|
||||
q->beginInsertRows(q->index(parentItem->row(), 0), parentItem->childCount(), parentItem->childCount());
|
||||
if (parentItem->insertChild(std::make_unique<RoomTreeItem>(new RoomWrapper{it.box_me()}, parentItem))) {
|
||||
// connectRoomSignals(room);
|
||||
}
|
||||
q->endInsertRows();
|
||||
roomIndexes.append(q->indexForRoom(it.box_me()));
|
||||
}
|
||||
return;
|
||||
break;
|
||||
}
|
||||
|
||||
const auto parentItem = m_rootItem->child(type);
|
||||
beginInsertRows(index(parentItem->row(), 0), parentItem->childCount(), parentItem->childCount());
|
||||
parentItem->insertChild(std::make_unique<RoomTreeItem>(room, parentItem));
|
||||
connectRoomSignals(room);
|
||||
endInsertRows();
|
||||
}
|
||||
|
||||
void RoomTreeModel::leftRoom(Room *r)
|
||||
{
|
||||
const auto room = dynamic_cast<NeoChatRoom *>(r);
|
||||
auto index = indexForRoom(room);
|
||||
if (!index.isValid()) {
|
||||
return;
|
||||
case 1: { // Clear
|
||||
q->beginResetModel();
|
||||
resetTree();
|
||||
roomIndexes.clear();
|
||||
q->endResetModel();
|
||||
break;
|
||||
}
|
||||
|
||||
const auto parentItem = getItem(index.parent());
|
||||
Q_ASSERT(parentItem);
|
||||
|
||||
beginRemoveRows(index.parent(), index.row(), index.row());
|
||||
parentItem->removeChild(index.row());
|
||||
room->disconnect(this);
|
||||
endRemoveRows();
|
||||
}
|
||||
|
||||
void RoomTreeModel::moveRoom(Quotient::Room *room)
|
||||
{
|
||||
// We can't assume the type as it has changed so currently the return of
|
||||
// NeoChatRoomType::typeForRoom doesn't match it's current location. So find the room.
|
||||
NeoChatRoomType::Types oldType;
|
||||
int oldRow = -1;
|
||||
for (int i = 0; i < NeoChatRoomType::TypesCount; i++) {
|
||||
const auto categoryItem = m_rootItem->child(i);
|
||||
const auto row = categoryItem->rowForRoom(room);
|
||||
if (row) {
|
||||
oldType = static_cast<NeoChatRoomType::Types>(i);
|
||||
oldRow = *row;
|
||||
case 2: { // Push Front
|
||||
const auto type = NeoChatRoomType::typeForRoom(diff->item());
|
||||
const auto parentItem = rootItem->child(type);
|
||||
q->beginInsertRows(q->index(parentItem->row(), 0), 0, 0);
|
||||
if (parentItem->insertChild(std::make_unique<RoomTreeItem>(new RoomWrapper{diff->item()}, parentItem))) {
|
||||
// connectRoomSignals(room);
|
||||
}
|
||||
q->endInsertRows();
|
||||
roomIndexes.prepend(q->indexForRoom(diff->item()));
|
||||
break;
|
||||
}
|
||||
|
||||
if (oldRow == -1) {
|
||||
return;
|
||||
case 3: { // Push Back
|
||||
const auto type = NeoChatRoomType::typeForRoom(diff->item());
|
||||
const auto parentItem = rootItem->child(type);
|
||||
q->beginInsertRows(q->index(parentItem->row(), 0), parentItem->childCount(), parentItem->childCount());
|
||||
if (parentItem->insertChild(std::make_unique<RoomTreeItem>(new RoomWrapper{diff->item()}, parentItem))) {
|
||||
// connectRoomSignals(room);
|
||||
}
|
||||
q->endInsertRows();
|
||||
roomIndexes.append(q->indexForRoom(diff->item()));
|
||||
break;
|
||||
}
|
||||
auto neochatRoom = dynamic_cast<NeoChatRoom *>(room);
|
||||
const auto newType = NeoChatRoomType::typeForRoom(neochatRoom);
|
||||
if (newType == oldType) {
|
||||
return;
|
||||
case 4: { // Pop Front
|
||||
const auto index = roomIndexes.front();
|
||||
q->beginRemoveRows(index.parent(), index.row(), index.row());
|
||||
const auto parentItem = q->getItem(index.parent());
|
||||
parentItem->removeChild(index.row());
|
||||
roomIndexes.removeFirst();
|
||||
q->endRemoveRows();
|
||||
break;
|
||||
}
|
||||
case 5: { // Pop Back
|
||||
const auto index = roomIndexes.back();
|
||||
q->beginRemoveRows(index.parent(), index.row(), index.row());
|
||||
const auto parentItem = q->getItem(index.parent());
|
||||
parentItem->removeChild(index.row());
|
||||
roomIndexes.removeLast();
|
||||
q->endRemoveRows();
|
||||
break;
|
||||
}
|
||||
case 6: { // Insert
|
||||
const auto type = NeoChatRoomType::typeForRoom(diff->item());
|
||||
const auto parentItem = rootItem->child(type);
|
||||
q->beginInsertRows(q->index(parentItem->row(), 0), parentItem->childCount(), parentItem->childCount());
|
||||
if (parentItem->insertChild(std::make_unique<RoomTreeItem>(new RoomWrapper{diff->item()}, parentItem))) {
|
||||
// connectRoomSignals(room);
|
||||
}
|
||||
q->endInsertRows();
|
||||
roomIndexes.insert(diff->index(), q->indexForRoom(diff->item()));
|
||||
break;
|
||||
}
|
||||
case 7: { // Set
|
||||
const auto index = roomIndexes.at(diff->index());
|
||||
q->beginRemoveRows(index.parent(), index.row(), index.row());
|
||||
q->getItem(index.parent())->removeChild(index.row());
|
||||
q->endRemoveRows();
|
||||
|
||||
const auto oldParent = index(oldType, 0, {});
|
||||
auto oldParentItem = getItem(oldParent);
|
||||
Q_ASSERT(oldParentItem);
|
||||
const auto type = NeoChatRoomType::typeForRoom(diff->item());
|
||||
const auto parentItem = rootItem->child(type);
|
||||
q->beginInsertRows(q->index(parentItem->row(), 0), parentItem->childCount(), parentItem->childCount());
|
||||
if (parentItem->insertChild(std::make_unique<RoomTreeItem>(new RoomWrapper{diff->item()}, parentItem))) {
|
||||
// connectRoomSignals(room);
|
||||
}
|
||||
q->endInsertRows();
|
||||
roomIndexes[diff->index()] = q->indexForRoom(diff->item());
|
||||
break;
|
||||
}
|
||||
case 8: { // Remove
|
||||
const auto index = roomIndexes.at(diff->index());
|
||||
q->beginRemoveRows(index.parent(), index.row(), index.row());
|
||||
q->getItem(index.parent())->removeChild(index.row());
|
||||
q->endRemoveRows();
|
||||
roomIndexes.removeAt(diff->index());
|
||||
break;
|
||||
}
|
||||
case 9: { // Truncate
|
||||
for (int i = q->rowCount({}) - 1; i >= int(diff->index()); i--) {
|
||||
const auto index = roomIndexes.at(i);
|
||||
q->beginRemoveRows(index.parent(), index.row(), index.row());
|
||||
q->getItem(index.parent())->removeChild(index.row());
|
||||
q->endRemoveRows();
|
||||
roomIndexes.removeAt(i);
|
||||
}
|
||||
break;
|
||||
}
|
||||
case 10: { // Reset
|
||||
q->beginResetModel();
|
||||
resetTree();
|
||||
roomIndexes.clear();
|
||||
q->endResetModel();
|
||||
|
||||
const auto newParent = index(newType, 0, {});
|
||||
auto newParentItem = getItem(newParent);
|
||||
Q_ASSERT(newParentItem);
|
||||
|
||||
// HACK: We're doing this as a remove then insert because moving doesn't work
|
||||
// properly with DelegateChooser for whatever reason.
|
||||
Q_ASSERT(checkIndex(index(oldRow, 0, oldParent), QAbstractItemModel::CheckIndexOption::IndexIsValid));
|
||||
beginRemoveRows(oldParent, oldRow, oldRow);
|
||||
const bool success = oldParentItem->removeChild(oldRow);
|
||||
Q_ASSERT(success);
|
||||
endRemoveRows();
|
||||
beginInsertRows(newParent, newParentItem->childCount(), newParentItem->childCount());
|
||||
newParentItem->insertChild(std::make_unique<RoomTreeItem>(neochatRoom, newParentItem));
|
||||
endInsertRows();
|
||||
for (const auto &it : diff->items_vec()) {
|
||||
const auto type = NeoChatRoomType::typeForRoom(it.box_me());
|
||||
const auto parentItem = rootItem->child(type);
|
||||
q->beginInsertRows(q->index(parentItem->row(), 0), parentItem->childCount(), parentItem->childCount());
|
||||
if (parentItem->insertChild(std::make_unique<RoomTreeItem>(new RoomWrapper{it.box_me()}, parentItem))) {
|
||||
// connectRoomSignals(room);
|
||||
}
|
||||
q->endInsertRows();
|
||||
roomIndexes.append(q->indexForRoom(it.box_me()));
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void RoomTreeModel::connectRoomSignals(NeoChatRoom *room)
|
||||
{
|
||||
connect(room, &Room::displaynameChanged, this, [this, room] {
|
||||
refreshRoomRoles(room, {DisplayNameRole});
|
||||
});
|
||||
connect(room, &Room::unreadStatsChanged, this, [this, room] {
|
||||
refreshRoomRoles(room, {ContextNotificationCountRole, HasHighlightNotificationsRole});
|
||||
});
|
||||
connect(room, &Room::avatarChanged, this, [this, room] {
|
||||
refreshRoomRoles(room, {AvatarRole});
|
||||
});
|
||||
connect(room, &Room::tagsChanged, this, [this, room] {
|
||||
moveRoom(room);
|
||||
});
|
||||
connect(room, &Room::joinStateChanged, this, [this, room] {
|
||||
refreshRoomRoles(room);
|
||||
});
|
||||
connect(room, &Room::addedMessages, this, [this, room] {
|
||||
refreshRoomRoles(room, {SubtitleTextRole});
|
||||
});
|
||||
connect(room, &Room::pendingEventMerged, this, [this, room] {
|
||||
refreshRoomRoles(room, {SubtitleTextRole});
|
||||
});
|
||||
connect(room, &NeoChatRoom::pushNotificationStateChanged, this, [this, room] {
|
||||
refreshRoomRoles(room, {ContextNotificationCountRole, HasHighlightNotificationsRole});
|
||||
});
|
||||
}
|
||||
// void RoomTreeModel::connectRoomSignals(NeoChatRoom *room)
|
||||
// {
|
||||
// connect(room, &Room::displaynameChanged, this, [this, room] {
|
||||
// refreshRoomRoles(room, {DisplayNameRole});
|
||||
// });
|
||||
// connect(room, &Room::unreadStatsChanged, this, [this, room] {
|
||||
// refreshRoomRoles(room, {ContextNotificationCountRole, HasHighlightNotificationsRole});
|
||||
// });
|
||||
// connect(room, &Room::avatarChanged, this, [this, room] {
|
||||
// refreshRoomRoles(room, {AvatarRole});
|
||||
// });
|
||||
// connect(room, &Room::tagsChanged, this, [this, room] {
|
||||
// moveRoom(room);
|
||||
// });
|
||||
// connect(room, &Room::joinStateChanged, this, [this, room] {
|
||||
// refreshRoomRoles(room);
|
||||
// });
|
||||
// connect(room, &Room::addedMessages, this, [this, room] {
|
||||
// refreshRoomRoles(room, {SubtitleTextRole});
|
||||
// });
|
||||
// connect(room, &Room::pendingEventMerged, this, [this, room] {
|
||||
// refreshRoomRoles(room, {SubtitleTextRole});
|
||||
// });
|
||||
// connect(room, &NeoChatRoom::pushNotificationStateChanged, this, [this, room] {
|
||||
// refreshRoomRoles(room, {ContextNotificationCountRole, HasHighlightNotificationsRole});
|
||||
// });
|
||||
// }
|
||||
|
||||
void RoomTreeModel::refreshRoomRoles(NeoChatRoom *room, const QList<int> &roles)
|
||||
{
|
||||
const auto index = indexForRoom(room);
|
||||
if (!index.isValid()) {
|
||||
qCritical() << "Room" << room->id() << "not found in the room list";
|
||||
return;
|
||||
}
|
||||
Q_EMIT dataChanged(index, index, roles);
|
||||
}
|
||||
// void RoomTreeModel::refreshRoomRoles(NeoChatRoom *room, const QList<int> &roles)
|
||||
// {
|
||||
// const auto index = indexForRoom(room);
|
||||
// if (!index.isValid()) {
|
||||
// qCritical() << "Room" << room->id() << "not found in the room list";
|
||||
// return;
|
||||
// }
|
||||
// Q_EMIT dataChanged(index, index, roles);
|
||||
// }
|
||||
|
||||
NeoChatConnection *RoomTreeModel::connection() const
|
||||
Connection *RoomTreeModel::connection() const
|
||||
{
|
||||
return m_connection;
|
||||
return d->connection;
|
||||
}
|
||||
|
||||
int RoomTreeModel::columnCount(const QModelIndex &parent) const
|
||||
@@ -215,7 +284,7 @@ int RoomTreeModel::rowCount(const QModelIndex &parent) const
|
||||
}
|
||||
|
||||
if (!parent.isValid()) {
|
||||
parentItem = m_rootItem.get();
|
||||
parentItem = d->rootItem.get();
|
||||
} else {
|
||||
parentItem = static_cast<RoomTreeItem *>(parent.internalPointer());
|
||||
}
|
||||
@@ -239,7 +308,7 @@ QModelIndex RoomTreeModel::parent(const QModelIndex &index) const
|
||||
}
|
||||
RoomTreeItem *parentItem = childItem->parentItem();
|
||||
|
||||
if (parentItem == m_rootItem.get()) {
|
||||
if (parentItem == d->rootItem.get()) {
|
||||
return QModelIndex();
|
||||
}
|
||||
|
||||
@@ -295,7 +364,7 @@ QVariant RoomTreeModel::data(const QModelIndex &index, int role) const
|
||||
}
|
||||
|
||||
RoomTreeItem *child = getItem(index);
|
||||
if (std::holds_alternative<NeoChatRoomType::Types>(child->data())) {
|
||||
if (std::holds_alternative<NeoChatRoomType::Type>(child->data())) {
|
||||
if (role == DisplayNameRole) {
|
||||
return NeoChatRoomType::typeName(index.row());
|
||||
}
|
||||
@@ -314,98 +383,91 @@ QVariant RoomTreeModel::data(const QModelIndex &index, int role) const
|
||||
return {};
|
||||
}
|
||||
|
||||
const auto room = std::get<NeoChatRoom *>(child->data());
|
||||
const auto room = std::get<RoomWrapper *>(child->data());
|
||||
Q_ASSERT(room);
|
||||
|
||||
if (role == DisplayNameRole) {
|
||||
return room->displayName();
|
||||
return stringFromRust((*room->item)->display_name()).toHtmlEscaped();
|
||||
}
|
||||
if (role == AvatarRole) {
|
||||
return room->avatarMediaUrl();
|
||||
return u"%1?user_id=%2"_s.arg(stringFromRust((*room->item)->avatar_url()), d->connection->matrixId());
|
||||
}
|
||||
if (role == CanonicalAliasRole) {
|
||||
return room->canonicalAlias();
|
||||
return stringFromRust((*room->item)->canonical_alias()).toHtmlEscaped();
|
||||
}
|
||||
if (role == TopicRole) {
|
||||
return room->topic();
|
||||
return stringFromRust((*room->item)->topic()).toHtmlEscaped();
|
||||
}
|
||||
if (role == CategoryRole) {
|
||||
return NeoChatRoomType::typeForRoom(room);
|
||||
return NeoChatRoomType::typeForRoom((*room->item)->box_me());
|
||||
}
|
||||
if (role == ContextNotificationCountRole) {
|
||||
return int(room->contextAwareNotificationCount());
|
||||
return int((*room->item)->num_unread_messages());
|
||||
}
|
||||
if (role == HasHighlightNotificationsRole) {
|
||||
return room->highlightCount() > 0 && room->contextAwareNotificationCount() > 0;
|
||||
return (*room->item)->num_unread_mentions() > 0 && (*room->item)->num_unread_messages() > 0;
|
||||
}
|
||||
if (role == JoinStateRole) {
|
||||
if (!room->successorId().isEmpty()) {
|
||||
if (!(*room->item)->tombstone()->replacement_room().empty()) {
|
||||
return u"upgraded"_s;
|
||||
}
|
||||
return QVariant::fromValue(room->joinState());
|
||||
return QVariant::fromValue((*room->item)->state());
|
||||
}
|
||||
if (role == CurrentRoomRole) {
|
||||
return QVariant::fromValue(room);
|
||||
return {};
|
||||
// return QVariant::fromValue(room);
|
||||
}
|
||||
if (role == SubtitleTextRole) {
|
||||
if (room->isInvite()) {
|
||||
if (room->isDirectChat()) {
|
||||
return i18nc("@info:label", "Invited you to chat");
|
||||
}
|
||||
return i18nc("@info:label", "%1 invited you", room->member(room->invitingUserId()).displayName());
|
||||
}
|
||||
if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) {
|
||||
return QString();
|
||||
}
|
||||
return EventHandler::subtitleText(room, room->lastEvent());
|
||||
return {};
|
||||
// if (room->lastEvent() == nullptr || room->lastEventIsSpoiler()) {
|
||||
// return QString();
|
||||
// }
|
||||
// return EventHandler::subtitleText(room, room->lastEvent());
|
||||
}
|
||||
if (role == AvatarImageRole) {
|
||||
return room->avatar(128);
|
||||
return {};
|
||||
// return room->avatar(128);
|
||||
}
|
||||
if (role == RoomIdRole) {
|
||||
return room->id();
|
||||
return stringFromRust((*room->item)->id()).toHtmlEscaped();
|
||||
}
|
||||
if (role == IsSpaceRole) {
|
||||
return room->isSpace();
|
||||
return (*room->item)->is_space();
|
||||
}
|
||||
if (role == IsChildSpaceRole) {
|
||||
return SpaceHierarchyCache::instance().isChild(room->id());
|
||||
return false;
|
||||
// return SpaceHierarchyCache::instance().isChild(room->id());
|
||||
}
|
||||
if (role == ReplacementIdRole) {
|
||||
return room->successorId();
|
||||
return stringFromRust((*room->item)->tombstone()->replacement_room()).toHtmlEscaped();
|
||||
}
|
||||
if (role == IsDirectChat) {
|
||||
return room->isDirectChat();
|
||||
return false;
|
||||
// return room->isDirectChat();
|
||||
}
|
||||
if (role == DelegateTypeRole) {
|
||||
return u"normal"_s;
|
||||
}
|
||||
if (role == RoomTypeRole) {
|
||||
if (room->creation()) {
|
||||
return room->creation()->contentPart<QString>("type"_L1);
|
||||
}
|
||||
return stringFromRust((*room->item)->room_type()).toHtmlEscaped();
|
||||
}
|
||||
|
||||
return {};
|
||||
}
|
||||
|
||||
QModelIndex RoomTreeModel::indexForRoom(NeoChatRoom *room) const
|
||||
QModelIndex RoomTreeModel::indexForRoom(rust::Box<sdk::RoomListRoom> room) const
|
||||
{
|
||||
if (room == nullptr) {
|
||||
return {};
|
||||
}
|
||||
|
||||
// Try and find by checking type.
|
||||
const auto type = NeoChatRoomType::typeForRoom(room);
|
||||
const auto parentItem = m_rootItem->child(type);
|
||||
const auto row = parentItem->rowForRoom(room);
|
||||
const auto type = NeoChatRoomType::typeForRoom(room->box_me());
|
||||
const auto parentItem = d->rootItem->child(type);
|
||||
const auto row = parentItem->rowForRoom(room->box_me());
|
||||
if (row) {
|
||||
return index(*row, 0, index(type, 0));
|
||||
}
|
||||
// Double check that the room isn't in the wrong category.
|
||||
for (int i = 0; i < NeoChatRoomType::TypesCount; i++) {
|
||||
const auto parentItem = m_rootItem->child(i);
|
||||
const auto row = parentItem->rowForRoom(room);
|
||||
const auto parentItem = d->rootItem->child(i);
|
||||
const auto row = parentItem->rowForRoom(room->box_me());
|
||||
if (row) {
|
||||
return index(*row, 0, index(i, 0));
|
||||
}
|
||||
@@ -414,4 +476,13 @@ QModelIndex RoomTreeModel::indexForRoom(NeoChatRoom *room) const
|
||||
return {};
|
||||
}
|
||||
|
||||
std::optional<rust::Box<sdk::RoomListRoom>> RoomTreeModel::roomForIndex(QModelIndex index) const
|
||||
{
|
||||
RoomTreeItem *child = getItem(index);
|
||||
if (std::holds_alternative<NeoChatRoomType::Type>(child->data())) {
|
||||
return std::nullopt;
|
||||
}
|
||||
return (*std::get<RoomWrapper *>(child->data())->item)->box_me();
|
||||
}
|
||||
|
||||
#include "moc_roomtreemodel.cpp"
|
||||
|
||||
Reference in New Issue
Block a user