2021-03-05 02:35:15 +03:00
|
|
|
// SPDX-FileCopyrightText: 2021 Nheko Contributors
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: GPL-3.0-or-later
|
|
|
|
|
2020-09-07 12:51:28 +03:00
|
|
|
#include "UsersModel.h"
|
|
|
|
|
2021-04-24 10:12:50 +03:00
|
|
|
#include <QUrl>
|
|
|
|
|
2020-09-07 12:51:28 +03:00
|
|
|
#include "Cache.h"
|
|
|
|
#include "CompletionModelRoles.h"
|
2021-04-24 10:12:50 +03:00
|
|
|
#include "UserSettingsPage.h"
|
2020-09-07 12:51:28 +03:00
|
|
|
|
|
|
|
UsersModel::UsersModel(const std::string &roomId, QObject *parent)
|
|
|
|
: QAbstractListModel(parent)
|
2020-11-20 04:38:08 +03:00
|
|
|
, room_id(roomId)
|
|
|
|
{
|
|
|
|
roomMembers_ = cache::roomMembers(roomId);
|
2020-11-20 06:05:31 +03:00
|
|
|
for (const auto &m : roomMembers_) {
|
|
|
|
displayNames.push_back(QString::fromStdString(cache::displayName(room_id, m)));
|
|
|
|
userids.push_back(QString::fromStdString(m));
|
|
|
|
}
|
2020-11-20 04:38:08 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QHash<int, QByteArray>
|
|
|
|
UsersModel::roleNames() const
|
2020-09-07 12:51:28 +03:00
|
|
|
{
|
2020-11-20 04:38:08 +03:00
|
|
|
return {
|
|
|
|
{CompletionModel::CompletionRole, "completionRole"},
|
|
|
|
{CompletionModel::SearchRole, "searchRole"},
|
|
|
|
{CompletionModel::SearchRole2, "searchRole2"},
|
|
|
|
{Roles::DisplayName, "displayName"},
|
|
|
|
{Roles::AvatarUrl, "avatarUrl"},
|
2020-11-24 21:06:31 +03:00
|
|
|
{Roles::UserID, "userid"},
|
2020-11-20 04:38:08 +03:00
|
|
|
};
|
2020-09-07 12:51:28 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
QVariant
|
|
|
|
UsersModel::data(const QModelIndex &index, int role) const
|
|
|
|
{
|
|
|
|
if (hasIndex(index.row(), index.column(), index.parent())) {
|
|
|
|
switch (role) {
|
|
|
|
case CompletionModel::CompletionRole:
|
2021-04-24 10:12:50 +03:00
|
|
|
if (UserSettings::instance()->markdown())
|
|
|
|
return QString("[%1](https://matrix.to/#/%2)")
|
|
|
|
.arg(displayNames[index.row()])
|
|
|
|
.arg(QString(QUrl::toPercentEncoding(userids[index.row()])));
|
|
|
|
else
|
|
|
|
return displayNames[index.row()];
|
2020-09-07 12:51:28 +03:00
|
|
|
case CompletionModel::SearchRole:
|
|
|
|
case Qt::DisplayRole:
|
2020-11-20 04:38:08 +03:00
|
|
|
case Roles::DisplayName:
|
2020-11-20 06:05:31 +03:00
|
|
|
return displayNames[index.row()];
|
2020-11-20 04:38:08 +03:00
|
|
|
case CompletionModel::SearchRole2:
|
2020-11-20 06:05:31 +03:00
|
|
|
return userids[index.row()];
|
2020-11-20 04:38:08 +03:00
|
|
|
case Roles::AvatarUrl:
|
|
|
|
return cache::avatarUrl(QString::fromStdString(room_id),
|
|
|
|
QString::fromStdString(roomMembers_[index.row()]));
|
2020-11-24 21:06:31 +03:00
|
|
|
case Roles::UserID:
|
|
|
|
return userids[index.row()];
|
2020-09-07 12:51:28 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|