| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
#pragma once |
| 6 |
|
| 7 |
#include <QAbstractListModel> |
| 8 |
#include <QQmlEngine> |
| 9 |
#include <QString> |
| 10 |
#include <string> |
| 11 |
#include <vector> |
| 12 |
|
| 13 |
#include <mtx/responses/public_rooms.hpp> |
| 14 |
|
| 15 |
class FetchRoomsChunkFromDirectoryJob final : public QObject |
| 16 |
{ |
| 17 |
Q_OBJECT |
| 18 |
|
| 19 |
public: |
| 20 |
explicit FetchRoomsChunkFromDirectoryJob(QObject *p = nullptr) |
| 21 |
: QObject(p) |
| 22 |
{ |
| 23 |
} |
| 24 |
|
| 25 |
signals: |
| 26 |
void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms, |
| 27 |
const std::string &next_batch, |
| 28 |
const std::string &search_term, |
| 29 |
const std::string &server, |
| 30 |
const std::string &since); |
| 31 |
}; |
| 32 |
|
| 33 |
class RoomDirectoryModel : public QAbstractListModel |
| 34 |
{ |
| 35 |
Q_OBJECT |
| 36 |
QML_ELEMENT |
| 37 |
|
| 38 |
Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged) |
| 39 |
Q_PROPERTY( |
| 40 |
bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY reachedEndOfPaginationChanged) |
| 41 |
|
| 42 |
public: |
| 43 |
explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = ""); |
| 44 |
|
| 45 |
enum Roles |
| 46 |
{ |
| 47 |
Name = Qt::UserRole, |
| 48 |
Id, |
| 49 |
AvatarUrl, |
| 50 |
Topic, |
| 51 |
MemberCount, |
| 52 |
Previewable, |
| 53 |
CanJoin, |
| 54 |
}; |
| 55 |
QHash<int, QByteArray> roleNames() const override; |
| 56 |
|
| 57 |
QVariant data(const QModelIndex &index, int role) const override; |
| 58 |
|
| 59 |
inline int rowCount(const QModelIndex &parent = QModelIndex()) const override |
| 60 |
{ |
| 61 |
(void)parent; |
| 62 |
return static_cast<int>(publicRoomsData_.size()); |
| 63 |
} |
| 64 |
|
| 65 |
bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; } |
| 66 |
|
| 67 |
bool loadingMoreRooms() const { return loadingMoreRooms_; } |
| 68 |
|
| 69 |
bool reachedEndOfPagination() const { return reachedEndOfPagination_; } |
| 70 |
|
| 71 |
void fetchMore(const QModelIndex &) override; |
| 72 |
|
| 73 |
Q_INVOKABLE void joinRoom(const int &index = -1); |
| 74 |
|
| 75 |
signals: |
| 76 |
void loadingMoreRoomsChanged(); |
| 77 |
void reachedEndOfPaginationChanged(); |
| 78 |
|
| 79 |
public slots: |
| 80 |
void setMatrixServer(const QString &s = QLatin1String("")); |
| 81 |
void setSearchTerm(const QString &f); |
| 82 |
|
| 83 |
private slots: |
| 84 |
|
| 85 |
void displayRooms(std::vector<mtx::responses::PublicRoomsChunk> rooms, |
| 86 |
const std::string &next_batch, |
| 87 |
const std::string &search_term, |
| 88 |
const std::string &server, |
| 89 |
const std::string &since); |
| 90 |
|
| 91 |
private: |
| 92 |
bool canJoinRoom(const QString &room) const; |
| 93 |
|
| 94 |
static constexpr size_t limit_ = 50; |
| 95 |
|
| 96 |
std::string server_; |
| 97 |
std::string userSearchString_; |
| 98 |
std::string prevBatch_; |
| 99 |
std::string nextBatch_; |
| 100 |
bool canFetchMore_{true}; |
| 101 |
bool loadingMoreRooms_{false}; |
| 102 |
bool reachedEndOfPagination_{false}; |
| 103 |
std::vector<mtx::responses::PublicRoomsChunk> publicRoomsData_; |
| 104 |
|
| 105 |
std::vector<std::string> getViasForRoom(const std::vector<std::string> &room); |
| 106 |
void resetDisplayedData(); |
| 107 |
}; |