File indexing completed on 2024-05-05 16:58:40

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