nheko/src/RoomDirectoryModel.h

99 lines
2.9 KiB
C
Raw Normal View History

// SPDX-FileCopyrightText: 2021 Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <QAbstractListModel>
#include <QHash>
#include <QString>
#include <string>
2021-07-29 04:33:23 +02:00
#include <vector>
#include "MatrixClient.h"
#include <mtx/responses/public_rooms.hpp>
2021-07-29 04:33:23 +02:00
#include <mtxclient/http/errors.hpp>
#include "Logging.h"
namespace mtx::http {
using RequestErr = const std::optional<mtx::http::ClientError> &;
}
namespace mtx::responses {
struct PublicRooms;
}
class RoomDirectoryModel : public QAbstractListModel
{
2021-07-29 04:33:23 +02:00
Q_OBJECT
2021-08-12 16:45:42 +02:00
Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged)
Q_PROPERTY(bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY
reachedEndOfPaginationChanged)
2021-08-12 15:50:52 +02:00
2021-07-29 04:33:23 +02:00
public:
explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = "");
2021-07-29 04:33:23 +02:00
enum Roles
{
Name = Qt::UserRole,
Id,
AvatarUrl,
Topic,
MemberCount,
Previewable,
CanJoin,
2021-07-29 04:33:23 +02:00
};
QHash<int, QByteArray> roleNames() const override;
QVariant data(const QModelIndex &index, int role) const override;
inline int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
(void)parent;
return static_cast<int>(publicRoomsData_.size());
}
2021-08-12 16:45:42 +02:00
bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; }
2021-08-12 15:50:52 +02:00
2021-08-12 16:45:42 +02:00
bool loadingMoreRooms() const { return loadingMoreRooms_; }
2021-08-12 15:50:52 +02:00
2021-08-12 16:45:42 +02:00
bool reachedEndOfPagination() const { return reachedEndOfPagination_; }
2021-08-12 15:50:52 +02:00
2021-07-29 04:33:23 +02:00
void fetchMore(const QModelIndex &) override;
Q_INVOKABLE void joinRoom(const int &index = -1);
signals:
2021-07-29 04:33:23 +02:00
void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms,
const std::string &next_batch);
2021-08-12 16:45:42 +02:00
void loadingMoreRoomsChanged();
void reachedEndOfPaginationChanged();
public slots:
2021-07-29 04:33:23 +02:00
void setMatrixServer(const QString &s = "");
void setSearchTerm(const QString &f);
2021-08-14 15:44:34 +02:00
private slots:
void displayRooms(std::vector<mtx::responses::PublicRoomsChunk> rooms,
const std::string &next_batch);
private:
bool canJoinRoom(const QString &room) const;
2021-07-29 04:33:23 +02:00
static constexpr size_t limit_ = 50;
std::string server_;
std::string userSearchString_;
std::string prevBatch_;
std::string nextBatch_;
2021-08-12 16:45:42 +02:00
bool canFetchMore_{true};
bool loadingMoreRooms_{false};
bool reachedEndOfPagination_{false};
2021-07-29 04:33:23 +02:00
std::vector<mtx::responses::PublicRoomsChunk> publicRoomsData_;
std::vector<std::string> getViasForRoom(const std::vector<std::string> &room);
void resetDisplayedData();
2021-08-04 00:25:11 +02:00
};