matrixion/src/RoomDirectoryModel.h

108 lines
3 KiB
C
Raw Normal View History

2021-08-17 15:22:37 +03:00
// SPDX-FileCopyrightText: 2021 Nheko Contributors
// SPDX-FileCopyrightText: 2022 Nheko Contributors
// SPDX-FileCopyrightText: 2023 Nheko Contributors
2021-08-17 15:22:37 +03:00
//
// SPDX-License-Identifier: GPL-3.0-or-later
#pragma once
#include <QAbstractListModel>
#include <QString>
#include <string>
#include <vector>
#include <mtx/responses/public_rooms.hpp>
class FetchRoomsChunkFromDirectoryJob final : public QObject
{
Q_OBJECT
public:
explicit FetchRoomsChunkFromDirectoryJob(QObject *p = nullptr)
: QObject(p)
{
}
signals:
void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms,
const std::string &next_batch,
const std::string &search_term,
const std::string &server,
const std::string &since);
};
2021-08-17 15:22:37 +03:00
class RoomDirectoryModel : public QAbstractListModel
{
2021-09-18 01:22:33 +03:00
Q_OBJECT
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged)
Q_PROPERTY(
bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY reachedEndOfPaginationChanged)
2021-08-17 15:22:37 +03:00
public:
2021-09-18 01:22:33 +03:00
explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = "");
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
enum Roles
{
Name = Qt::UserRole,
Id,
AvatarUrl,
Topic,
MemberCount,
Previewable,
CanJoin,
};
QHash<int, QByteArray> roleNames() const override;
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
QVariant data(const QModelIndex &index, int role) const override;
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
inline int rowCount(const QModelIndex &parent = QModelIndex()) const override
{
(void)parent;
return static_cast<int>(publicRoomsData_.size());
}
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; }
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
bool loadingMoreRooms() const { return loadingMoreRooms_; }
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
bool reachedEndOfPagination() const { return reachedEndOfPagination_; }
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
void fetchMore(const QModelIndex &) override;
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
Q_INVOKABLE void joinRoom(const int &index = -1);
2021-08-17 15:22:37 +03:00
signals:
2021-09-18 01:22:33 +03:00
void loadingMoreRoomsChanged();
void reachedEndOfPaginationChanged();
2021-08-17 15:22:37 +03:00
public slots:
void setMatrixServer(const QString &s = QLatin1String(""));
2021-09-18 01:22:33 +03:00
void setSearchTerm(const QString &f);
2021-08-17 15:22:37 +03:00
private slots:
2021-09-18 01:22:33 +03:00
void displayRooms(std::vector<mtx::responses::PublicRoomsChunk> rooms,
const std::string &next_batch,
const std::string &search_term,
const std::string &server,
const std::string &since);
2021-08-17 15:22:37 +03:00
private:
2021-09-18 01:22:33 +03:00
bool canJoinRoom(const QString &room) const;
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
static constexpr size_t limit_ = 50;
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
std::string server_;
std::string userSearchString_;
std::string prevBatch_;
std::string nextBatch_;
bool canFetchMore_{true};
bool loadingMoreRooms_{false};
bool reachedEndOfPagination_{false};
std::vector<mtx::responses::PublicRoomsChunk> publicRoomsData_;
2021-08-17 15:22:37 +03:00
2021-09-18 01:22:33 +03:00
std::vector<std::string> getViasForRoom(const std::vector<std::string> &room);
void resetDisplayedData();
2021-08-17 15:22:37 +03:00
};