matrixion/src/RoomsModel.cpp

81 lines
2.8 KiB
C++
Raw Normal View History

2021-03-05 02:35:15 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#include "RoomsModel.h"
#include <QUrl>
#include "Cache_p.h"
#include "CompletionModelRoles.h"
2021-04-24 10:12:50 +03:00
#include "UserSettingsPage.h"
RoomsModel::RoomsModel(bool showOnlyRoomWithAliases, QObject *parent)
: QAbstractListModel(parent)
, showOnlyRoomWithAliases_(showOnlyRoomWithAliases)
{
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-09-18 01:22:33 +03:00
for (const auto &r : rooms_) {
auto roomAliasesList = cache::client()->getRoomAliases(r);
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-09-18 01:22:33 +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"}};
}
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-09-18 01:22:33 +03:00
}
return {};
}