File indexing completed on 2024-05-12 09:02:07

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 connection that the model is using to search for messages.
0036      */
0037     Q_PROPERTY(Quotient::Connection *connection READ connection WRITE setConnection NOTIFY connectionChanged)
0038 
0039     /**
0040      * @brief The current room that the search is being done from.
0041      */
0042     Q_PROPERTY(NeoChatRoom *room READ room WRITE setRoom NOTIFY roomChanged)
0043 
0044     /**
0045      * @brief Whether the model is currently searching for messages.
0046      */
0047     Q_PROPERTY(bool searching READ searching NOTIFY searchingChanged)
0048 
0049 public:
0050     /**
0051      * @brief Defines the model roles.
0052      *
0053      * For documentation of the roles, see MessageEventModel.
0054      *
0055      * Some of the roles exist only for compatibility with the MessageEventModel,
0056      * since the same delegates are used.
0057      */
0058     enum Roles {
0059         DisplayRole = Qt::DisplayRole,
0060         DelegateTypeRole,
0061         ShowAuthorRole,
0062         AuthorRole,
0063         ShowSectionRole,
0064         SectionRole,
0065         TimeRole,
0066         TimeStringRole,
0067         EventIdRole,
0068         ExcessReadMarkersRole,
0069         HighlightRole,
0070         ReadMarkersString,
0071         PlainTextRole,
0072         VerifiedRole,
0073         ProgressInfoRole,
0074         ShowReactionsRole,
0075         IsReplyRole,
0076         ReplyAuthorRole,
0077         ReplyIdRole,
0078         ReplyDelegateTypeRole,
0079         ReplyDisplayRole,
0080         ReplyMediaInfoRole,
0081         ReactionRole,
0082         ReadMarkersRole,
0083         IsPendingRole,
0084         ShowReadMarkersRole,
0085         MimeTypeRole,
0086         ShowLinkPreviewRole,
0087         LinkPreviewRole,
0088         IsThreadedRole,
0089         ThreadRootRole,
0090     };
0091     Q_ENUM(Roles)
0092     explicit SearchModel(QObject *parent = nullptr);
0093 
0094     QString searchText() const;
0095     void setSearchText(const QString &searchText);
0096 
0097     Quotient::Connection *connection() const;
0098     void setConnection(Quotient::Connection *connection);
0099 
0100     NeoChatRoom *room() const;
0101     void setRoom(NeoChatRoom *room);
0102 
0103     bool searching() const;
0104 
0105     /**
0106      * @brief Get the given role value at the given index.
0107      *
0108      * @sa QAbstractItemModel::data
0109      */
0110     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0111 
0112     /**
0113      * @brief Number of rows in the model.
0114      *
0115      * @sa QAbstractItemModel::rowCount
0116      */
0117     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0118 
0119     /**
0120      * @brief Returns a mapping from Role enum values to role names.
0121      *
0122      * @sa Roles, QAbstractItemModel::roleNames()
0123      */
0124     QHash<int, QByteArray> roleNames() const override;
0125 
0126     /**
0127      * @brief Start searching for messages.
0128      */
0129     Q_INVOKABLE void search();
0130 
0131 Q_SIGNALS:
0132     void searchTextChanged();
0133     void connectionChanged();
0134     void roomChanged();
0135     void searchingChanged();
0136 
0137 protected:
0138     bool event(QEvent *event) override;
0139 
0140 private:
0141     void setSearching(bool searching);
0142 
0143     QString m_searchText;
0144     Quotient::Connection *m_connection = nullptr;
0145     NeoChatRoom *m_room = nullptr;
0146     Quotient::Omittable<Quotient::SearchJob::ResultRoomEvents> m_result = Quotient::none;
0147     Quotient::SearchJob *m_job = nullptr;
0148     bool m_searching = false;
0149 };