2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// 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"
|
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
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
std::vector<std::string> rooms_ = cache::joinedRooms();
|
|
|
|
roomInfos = cache::getRoomInfo(rooms_);
|
|
|
|
if (!showOnlyRoomWithAliases_) {
|
|
|
|
roomids.reserve(rooms_.size());
|
|
|
|
roomAliases.reserve(rooms_.size());
|
|
|
|
}
|
2021-02-15 22:17:17 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
for (const auto &r : rooms_) {
|
|
|
|
auto roomAliasesList = cache::client()->getRoomAliases(r);
|
2021-02-15 22:17:17 +03:00
|
|
|
|
2021-09-18 01:22:33 +03:00
|
|
|
if (showOnlyRoomWithAliases_) {
|
|
|
|
if (roomAliasesList && !roomAliasesList->alias.empty()) {
|
|
|
|
roomids.push_back(QString::fromStdString(r));
|
|
|
|
roomAliases.push_back(QString::fromStdString(roomAliasesList->alias));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
roomids.push_back(QString::fromStdString(r));
|
|
|
|
roomAliases.push_back(roomAliasesList ? QString::fromStdString(roomAliasesList->alias)
|
|
|
|
: "");
|
2021-02-15 22:17:17 +03:00
|
|
|
}
|
2021-09-18 01:22:33 +03:00
|
|
|
}
|
2021-02-15 22:17:17 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
RoomsModel::roleNames() const
|
|
|
|
{
|
2021-09-18 01:22:33 +03:00
|
|
|
return {{CompletionModel::CompletionRole, "completionRole"},
|
|
|
|
{CompletionModel::SearchRole, "searchRole"},
|
|
|
|
{CompletionModel::SearchRole2, "searchRole2"},
|
|
|
|
{Roles::RoomAlias, "roomAlias"},
|
|
|
|
{Roles::AvatarUrl, "avatarUrl"},
|
|
|
|
{Roles::RoomID, "roomid"},
|
|
|
|
{Roles::RoomName, "roomName"}};
|
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: {
|
|
|
|
if (UserSettings::instance()->markdown()) {
|
|
|
|
QString percentEncoding = QUrl::toPercentEncoding(roomAliases[index.row()]);
|
|
|
|
return QString("[%1](https://matrix.to/#/%2)")
|
|
|
|
.arg(roomAliases[index.row()], percentEncoding);
|
|
|
|
} else {
|
|
|
|
return roomAliases[index.row()];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case CompletionModel::SearchRole:
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
case Roles::RoomAlias:
|
|
|
|
return roomAliases[index.row()].toHtmlEscaped();
|
|
|
|
case CompletionModel::SearchRole2:
|
|
|
|
case Roles::RoomName:
|
|
|
|
return QString::fromStdString(roomInfos.at(roomids[index.row()]).name).toHtmlEscaped();
|
|
|
|
case Roles::AvatarUrl:
|
|
|
|
return QString::fromStdString(roomInfos.at(roomids[index.row()]).avatar_url);
|
|
|
|
case Roles::RoomID:
|
|
|
|
return roomids[index.row()].toHtmlEscaped();
|
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
|
|
|
}
|