Skip to content
Snippets Groups Projects
RoomDirectoryModel.h 2.65 KiB
Newer Older
  • Learn to ignore specific revisions
  • Nicolas Werner's avatar
    Nicolas Werner committed
    // SPDX-FileCopyrightText: 2021 Nheko Contributors
    
    // SPDX-FileCopyrightText: 2022 Nheko Contributors
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    //
    // SPDX-License-Identifier: GPL-3.0-or-later
    
    #pragma once
    
    #include <QAbstractListModel>
    #include <QHash>
    #include <QString>
    #include <string>
    #include <vector>
    
    #include "MatrixClient.h"
    #include <mtx/responses/public_rooms.hpp>
    #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
    {
    
        Q_OBJECT
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        Q_PROPERTY(bool loadingMoreRooms READ loadingMoreRooms NOTIFY loadingMoreRoomsChanged)
        Q_PROPERTY(
          bool reachedEndOfPagination READ reachedEndOfPagination NOTIFY reachedEndOfPaginationChanged)
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    public:
    
        explicit RoomDirectoryModel(QObject *parent = nullptr, const std::string &server = "");
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        enum Roles
        {
            Name = Qt::UserRole,
            Id,
            AvatarUrl,
            Topic,
            MemberCount,
            Previewable,
            CanJoin,
        };
        QHash<int, QByteArray> roleNames() const override;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        QVariant data(const QModelIndex &index, int role) const override;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        inline int rowCount(const QModelIndex &parent = QModelIndex()) const override
        {
            (void)parent;
            return static_cast<int>(publicRoomsData_.size());
        }
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        bool canFetchMore(const QModelIndex &) const override { return canFetchMore_; }
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        bool loadingMoreRooms() const { return loadingMoreRooms_; }
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        bool reachedEndOfPagination() const { return reachedEndOfPagination_; }
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        void fetchMore(const QModelIndex &) override;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        Q_INVOKABLE void joinRoom(const int &index = -1);
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    signals:
    
        void fetchedRoomsBatch(std::vector<mtx::responses::PublicRoomsChunk> rooms,
                               const std::string &next_batch);
        void loadingMoreRoomsChanged();
        void reachedEndOfPaginationChanged();
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    public slots:
    
        void setMatrixServer(const QString &s = QLatin1String(""));
    
        void setSearchTerm(const QString &f);
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    private slots:
    
    
        void displayRooms(std::vector<mtx::responses::PublicRoomsChunk> rooms,
                          const std::string &next_batch);
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    private:
    
        bool canJoinRoom(const QString &room) const;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        static constexpr size_t limit_ = 50;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        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_;
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    
    
        std::vector<std::string> getViasForRoom(const std::vector<std::string> &room);
        void resetDisplayedData();
    
    Nicolas Werner's avatar
    Nicolas Werner committed
    };