2023-02-22 01:48:49 +03:00
|
|
|
// SPDX-FileCopyrightText: Nheko Contributors
|
2021-03-05 02:35:15 +03:00
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2021-02-15 22:17:17 +03:00
|
|
|
#include "RoomsModel.h"
|
|
|
|
|
2021-02-19 14:34:31 +03:00
|
|
|
#include <QUrl>
|
|
|
|
|
2021-02-15 22:17:17 +03:00
|
|
|
#include "Cache_p.h"
|
|
|
|
#include "CompletionModelRoles.h"
|
2021-04-24 10:12:50 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2023-01-07 19:39:32 +03:00
|
|
|
#include "Utils.h"
|
2021-02-15 22:17:17 +03:00
|
|
|
|
2021-02-17 17:21:35 +03:00
|
|
|
RoomsModel::RoomsModel(bool showOnlyRoomWithAliases, QObject *parent)
|
2021-02-15 22:17:17 +03:00
|
|
|
: QAbstractListModel(parent)
|
2021-02-17 17:21:35 +03:00
|
|
|
, showOnlyRoomWithAliases_(showOnlyRoomWithAliases)
|
2021-02-15 22:17:17 +03:00
|
|
|
{
|
2023-01-07 19:39:32 +03:00
|
|
|
rooms = cache::client()->roomNamesAndAliases();
|
2021-02-15 22:17:17 +03:00
|
|
|
|
2023-01-07 19:39:32 +03:00
|
|
|
if (showOnlyRoomWithAliases_)
|
|
|
|
utils::erase_if(rooms, [](auto &r) { return r.alias.empty(); });
|
2023-05-06 20:30:29 +03:00
|
|
|
|
|
|
|
std::ranges::sort(rooms,
|
|
|
|
[](auto &a, auto &b) { return a.recent_activity > b.recent_activity; });
|
2021-02-15 22:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
RoomsModel::roleNames() const
|
|
|
|
{
|
2023-05-06 20:30:29 +03:00
|
|
|
return {
|
|
|
|
{CompletionModel::CompletionRole, "completionRole"},
|
|
|
|
{CompletionModel::SearchRole, "searchRole"},
|
|
|
|
{CompletionModel::SearchRole2, "searchRole2"},
|
|
|
|
{Roles::RoomAlias, "roomAlias"},
|
|
|
|
{Roles::AvatarUrl, "avatarUrl"},
|
|
|
|
{Roles::RoomID, "roomid"},
|
|
|
|
{Roles::RoomName, "roomName"},
|
|
|
|
{Roles::IsTombstoned, "isTombstoned"},
|
|
|
|
};
|
2021-02-15 22:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
RoomsModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
switch (role) {
|
|
|
|
case CompletionModel::CompletionRole: {
|
2023-01-07 19:39:32 +03:00
|
|
|
auto alias = QString::fromStdString(rooms[index.row()].alias);
|
2021-09-18 01:22:33 +03:00
|
|
|
if (UserSettings::instance()->markdown()) {
|
2023-01-07 19:39:32 +03:00
|
|
|
QString percentEncoding = QUrl::toPercentEncoding(alias);
|
2021-12-29 06:28:08 +03:00
|
|
|
return QStringLiteral("[%1](https://matrix.to/#/%2)")
|
2023-01-07 19:39:32 +03:00
|
|
|
.arg(alias.replace("[", "\\[").replace("]", "\\]").toHtmlEscaped(),
|
2022-07-20 14:52:13 +03:00
|
|
|
percentEncoding);
|
2021-09-18 01:22:33 +03:00
|
|
|
} else {
|
2023-01-07 19:39:32 +03:00
|
|
|
return alias;
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
case CompletionModel::SearchRole:
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Roles::RoomAlias:
|
2023-01-07 19:39:32 +03:00
|
|
|
return QString::fromStdString(rooms[index.row()].alias).toHtmlEscaped();
|
2021-09-18 01:22:33 +03:00
|
|
|
case CompletionModel::SearchRole2:
|
|
|
|
case Roles::RoomName:
|
2023-01-07 19:39:32 +03:00
|
|
|
return QString::fromStdString(rooms[index.row()].name);
|
2021-09-18 01:22:33 +03:00
|
|
|
case Roles::AvatarUrl:
|
2023-01-07 19:39:32 +03:00
|
|
|
return QString::fromStdString(
|
|
|
|
cache::client()->singleRoomInfo(rooms[index.row()].id).avatar_url);
|
2021-09-18 01:22:33 +03:00
|
|
|
case Roles::RoomID:
|
2023-01-07 19:39:32 +03:00
|
|
|
return QString::fromStdString(rooms[index.row()].id).toHtmlEscaped();
|
2023-05-06 20:30:29 +03:00
|
|
|
case Roles::IsTombstoned:
|
|
|
|
return rooms[index.row()].is_tombstoned;
|
2021-02-15 22:17:17 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
|
|
|
return {};
|
2021-02-15 22:17:17 +03:00
|
|
|
}
|