Improve public room list and add search functionalities.
This commit is contained in:
@@ -1,7 +1,5 @@
|
||||
#include "publicroomlistmodel.h"
|
||||
|
||||
#include "csapi/list_public_rooms.h"
|
||||
|
||||
PublicRoomListModel::PublicRoomListModel(QObject* parent)
|
||||
: QAbstractListModel(parent) {}
|
||||
|
||||
@@ -14,6 +12,7 @@ void PublicRoomListModel::setConnection(Connection* conn) {
|
||||
nextBatch = "";
|
||||
attempted = false;
|
||||
rooms.clear();
|
||||
m_server.clear();
|
||||
|
||||
if (m_connection) {
|
||||
m_connection->disconnect(this);
|
||||
@@ -23,33 +22,109 @@ void PublicRoomListModel::setConnection(Connection* conn) {
|
||||
|
||||
m_connection = conn;
|
||||
|
||||
if (job) {
|
||||
job->abandon();
|
||||
job = nullptr;
|
||||
}
|
||||
|
||||
if (m_connection) {
|
||||
next();
|
||||
}
|
||||
|
||||
emit connectionChanged();
|
||||
emit serverChanged();
|
||||
emit hasMoreChanged();
|
||||
}
|
||||
|
||||
void PublicRoomListModel::setServer(const QString& value) {
|
||||
if (m_server == value)
|
||||
return;
|
||||
|
||||
m_server = value;
|
||||
|
||||
beginResetModel();
|
||||
|
||||
nextBatch = "";
|
||||
attempted = false;
|
||||
rooms.clear();
|
||||
|
||||
endResetModel();
|
||||
|
||||
if (job) {
|
||||
job->abandon();
|
||||
job = nullptr;
|
||||
}
|
||||
|
||||
if (m_connection) {
|
||||
next();
|
||||
}
|
||||
|
||||
emit serverChanged();
|
||||
emit hasMoreChanged();
|
||||
}
|
||||
|
||||
void PublicRoomListModel::setKeyword(const QString& value) {
|
||||
if (m_keyword == value)
|
||||
return;
|
||||
|
||||
m_keyword = value;
|
||||
|
||||
beginResetModel();
|
||||
|
||||
nextBatch = "";
|
||||
attempted = false;
|
||||
rooms.clear();
|
||||
|
||||
endResetModel();
|
||||
|
||||
if (job) {
|
||||
job->abandon();
|
||||
job = nullptr;
|
||||
}
|
||||
|
||||
if (m_connection) {
|
||||
next();
|
||||
}
|
||||
|
||||
emit keywordChanged();
|
||||
emit hasMoreChanged();
|
||||
}
|
||||
|
||||
void PublicRoomListModel::next(int count) {
|
||||
if (count < 1)
|
||||
return;
|
||||
|
||||
if (attempted && nextBatch.isEmpty())
|
||||
if (job) {
|
||||
qDebug() << "PublicRoomListModel: Other jobs running, ignore";
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
if (!hasMore())
|
||||
return;
|
||||
|
||||
auto job = m_connection->callApi<GetPublicRoomsJob>(count, nextBatch);
|
||||
job = m_connection->callApi<QueryPublicRoomsJob>(
|
||||
m_server, count, nextBatch, QueryPublicRoomsJob::Filter{m_keyword});
|
||||
|
||||
connect(job, &BaseJob::success, this, [=] {
|
||||
auto resp = job->data();
|
||||
nextBatch = resp.nextBatch;
|
||||
connect(job, &BaseJob::finished, this, [=] {
|
||||
attempted = true;
|
||||
|
||||
this->beginInsertRows({}, rooms.count(),
|
||||
rooms.count() + resp.chunk.count());
|
||||
rooms.append(resp.chunk);
|
||||
this->endInsertRows();
|
||||
if (job->status() == BaseJob::Success) {
|
||||
auto resp = job->data();
|
||||
nextBatch = resp.nextBatch;
|
||||
|
||||
this->beginInsertRows({}, rooms.count(),
|
||||
rooms.count() + resp.chunk.count() - 1);
|
||||
rooms.append(resp.chunk);
|
||||
this->endInsertRows();
|
||||
|
||||
if (resp.nextBatch.isEmpty()) {
|
||||
emit hasMoreChanged();
|
||||
}
|
||||
}
|
||||
|
||||
this->job = nullptr;
|
||||
});
|
||||
|
||||
connect(job, &BaseJob::finished, this, [=] { attempted = true; });
|
||||
}
|
||||
|
||||
QVariant PublicRoomListModel::data(const QModelIndex& index, int role) const {
|
||||
@@ -63,7 +138,31 @@ QVariant PublicRoomListModel::data(const QModelIndex& index, int role) const {
|
||||
}
|
||||
auto room = rooms.at(index.row());
|
||||
if (role == NameRole) {
|
||||
return room.name;
|
||||
auto displayName = room.name;
|
||||
if (!displayName.isEmpty()) {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
displayName = room.canonicalAlias;
|
||||
if (!displayName.isEmpty()) {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
displayName = room.aliases.front();
|
||||
if (!displayName.isEmpty()) {
|
||||
return displayName;
|
||||
}
|
||||
|
||||
return room.roomId;
|
||||
}
|
||||
if (role == AvatarRole) {
|
||||
auto avatarUrl = room.avatarUrl;
|
||||
|
||||
if (avatarUrl.isEmpty()) {
|
||||
return "";
|
||||
}
|
||||
|
||||
return avatarUrl.remove(0, 6);
|
||||
}
|
||||
if (role == TopicRole) {
|
||||
return room.topic;
|
||||
@@ -76,6 +175,7 @@ QHash<int, QByteArray> PublicRoomListModel::roleNames() const {
|
||||
QHash<int, QByteArray> roles;
|
||||
|
||||
roles[NameRole] = "name";
|
||||
roles[AvatarRole] = "avatar";
|
||||
roles[TopicRole] = "topic";
|
||||
|
||||
return roles;
|
||||
@@ -87,3 +187,7 @@ int PublicRoomListModel::rowCount(const QModelIndex& parent) const {
|
||||
|
||||
return rooms.count();
|
||||
}
|
||||
|
||||
bool PublicRoomListModel::hasMore() const {
|
||||
return !(attempted && nextBatch.isEmpty());
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user