summary refs log tree commit diff
path: root/src/RoomDirectoryModel.h
blob: a51031120a353e97695c3ff91b207dba33644221 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
// SPDX-FileCopyrightText: Nheko Contributors
//
// SPDX-License-Identifier: GPL-3.0-or-later

#pragma once

#include <QAbstractListModel>
#include <QQmlEngine>
#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);
};

class RoomDirectoryModel : public QAbstractListModel
{
    Q_OBJECT
    QML_ELEMENT

    Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged)
    Q_PROPERTY(
      bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY reachedEndOfPaginationChanged)

public:
    explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = "");

    enum Roles
    {
        Name = Qt::UserRole,
        Id,
        AvatarUrl,
        Topic,
        MemberCount,
        Previewable,
        CanJoin,
    };
    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());
    }

    bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; }

    bool loadingMoreRooms() const { return loadingMoreRooms_; }

    bool reachedEndOfPagination() const { return reachedEndOfPagination_; }

    void fetchMore(const QModelIndex &) override;

    Q_INVOKABLE void joinRoom(const int &index = -1);

signals:
    void loadingMoreRoomsChanged();
    void reachedEndOfPaginationChanged();

public slots:
    void setMatrixServer(const QString &s = QLatin1String(""));
    void setSearchTerm(const QString &f);

private slots:

    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);

private:
    bool canJoinRoom(const QString &room) const;

    static constexpr size_t limit_ = 50;

    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_;

    std::vector<std::string> getViasForRoom(const std::vector<std::string> &room);
    void resetDisplayedData();
};