File indexing completed on 2024-05-05 05:01:25

0001 // SPDX-FileCopyrightText: 2022 Tobias Fella <tobias.fella@kde.org>
0002 // SPDX-License-Identifier: LGPL-2.0-or-later
0003 
0004 #pragma once
0005 
0006 #include <QAbstractListModel>
0007 #include <QQmlEngine>
0008 #include <QString>
0009 
0010 #include <Quotient/csapi/search.h>
0011 
0012 namespace Quotient
0013 {
0014 class Connection;
0015 }
0016 
0017 class NeoChatRoom;
0018 
0019 /**
0020  * @class SearchModel
0021  *
0022  * This class defines the model for visualising the results of a room message search.
0023  */
0024 class SearchModel : public QAbstractListModel
0025 {
0026     Q_OBJECT
0027     QML_ELEMENT
0028 
0029     /**
0030      * @brief The text to search messages for.
0031      */
0032     Q_PROPERTY(QString searchText READ searchText WRITE setSearchText NOTIFY searchTextChanged)
0033 
0034     /**
0035      * @brief The current room that the search is being done from.
0036      */
0037     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0038 
0039     /**
0040      * @brief Whether the model is currently searching for messages.
0041      */
0042     Q_PROPERTY(bool searching READ searching NOTIFY searchingChanged)
0043 
0044 public:
0045     /**
0046      * @brief Defines the model roles.
0047      *
0048      * For documentation of the roles, see MessageEventModel.
0049      *
0050      * Some of the roles exist only for compatibility with the MessageEventModel,
0051      * since the same delegates are used.
0052      */
0053     enum Roles {
0054         DisplayRole = Qt::DisplayRole,
0055         DelegateTypeRole,
0056         ShowAuthorRole,
0057         AuthorRole,
0058         ShowSectionRole,
0059         SectionRole,
0060         TimeRole,
0061         TimeStringRole,
0062         EventIdRole,
0063         ExcessReadMarkersRole,
0064         HighlightRole,
0065         ReadMarkersString,
0066         PlainTextRole,
0067         VerifiedRole,
0068         ProgressInfoRole,
0069         ShowReactionsRole,
0070         IsReplyRole,
0071         ReplyAuthorRole,
0072         ReplyIdRole,
0073         ReplyDelegateTypeRole,
0074         ReplyDisplayRole,
0075         ReplyMediaInfoRole,
0076         ReactionRole,
0077         ReadMarkersRole,
0078         IsPendingRole,
0079         ShowReadMarkersRole,
0080         MimeTypeRole,
0081         ShowLinkPreviewRole,
0082         LinkPreviewRole,
0083         IsThreadedRole,
0084         ThreadRootRole,
0085     };
0086     Q_ENUM(Roles)
0087     explicit SearchModel(QObject *parent = nullptr);
0088 
0089     QString searchText() const;
0090     void setSearchText(const QString &searchText);
0091 
0092     NeoChatRoom *room() const;
0093     void setRoom(NeoChatRoom *room);
0094 
0095     bool searching() const;
0096 
0097     /**
0098      * @brief Get the given role value at the given index.
0099      *
0100      * @sa QAbstractItemModel::data
0101      */
0102     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0103 
0104     /**
0105      * @brief Number of rows in the model.
0106      *
0107      * @sa QAbstractItemModel::rowCount
0108      */
0109     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0110 
0111     /**
0112      * @brief Returns a mapping from Role enum values to role names.
0113      *
0114      * @sa Roles, QAbstractItemModel::roleNames()
0115      */
0116     QHash<int, QByteArray> roleNames() const override;
0117 
0118     /**
0119      * @brief Start searching for messages.
0120      */
0121     Q_INVOKABLE void search();
0122 
0123 Q_SIGNALS:
0124     void searchTextChanged();
0125     void roomChanged();
0126     void searchingChanged();
0127 
0128 protected:
0129     bool event(QEvent *event) override;
0130 
0131 private:
0132     void setSearching(bool searching);
0133 
0134     QString m_searchText;
0135     NeoChatRoom *m_room = nullptr;
0136     Quotient::Omittable<Quotient::SearchJob::ResultRoomEvents> m_result = Quotient::none;
0137     Quotient::SearchJob *m_job = nullptr;
0138     bool m_searching = false;
0139 };