File indexing completed on 2024-05-12 05:04:14
0001 // SPDX-FileCopyrightText: 2022 Carl Schwan <carlschwan@kde.org> 0002 // SPDX-License-Identifier: LGPL-2.0-or-later 0003 0004 #pragma once 0005 0006 #include "abstracttimelinemodel.h" 0007 0008 class Identity; 0009 class Post; 0010 0011 /// Represents a search hashtag result, which only stores the name (and skips other information like occurences) for now 0012 /// \see SearchModel 0013 class SearchHashtag 0014 { 0015 public: 0016 explicit SearchHashtag(const QJsonObject &object); 0017 0018 QString getName() const; 0019 0020 private: 0021 QString m_name; 0022 }; 0023 0024 /// Model used to fetch search results 0025 /// \see AbstractTimelineModel 0026 class SearchModel : public AbstractTimelineModel 0027 { 0028 Q_OBJECT 0029 QML_ELEMENT 0030 0031 Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged) 0032 0033 public: 0034 /// The search result type 0035 enum ResultType { 0036 Status, ///< A status (for full-text search) 0037 Account, ///< An account 0038 Hashtag, ///< A hashtag 0039 }; 0040 Q_ENUM(ResultType); 0041 0042 explicit SearchModel(QObject *parent = nullptr); 0043 ~SearchModel() override; 0044 0045 /// Check if the search has finished loading 0046 /// \see setLoaded 0047 bool loaded() const; 0048 0049 /// Sets the search loading status 0050 /// \see laoded 0051 void setLoaded(bool loaded); 0052 0053 /// Start searching for \p queryString 0054 Q_INVOKABLE void search(const QString &queryString); 0055 0056 /// Get a localized label for a result type 0057 Q_INVOKABLE QString labelForType(SearchModel::ResultType sectionType); 0058 0059 /// Clear the fetched search results 0060 Q_INVOKABLE void clear(); 0061 0062 int rowCount(const QModelIndex &parent) const override; 0063 QVariant data(const QModelIndex &index, int role) const override; 0064 0065 Q_SIGNALS: 0066 /// Emitted if the loading status has changed 0067 /// \see setLoaded 0068 void loadedChanged(); 0069 0070 private: 0071 QList<std::shared_ptr<Identity>> m_accounts; 0072 QList<Post *> m_statuses; 0073 QList<SearchHashtag> m_hashtags; 0074 bool m_loaded = false; 0075 };