File indexing completed on 2024-04-28 16:13:20

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 "timeline/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     Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged)
0030 
0031 public:
0032     /// The search result type
0033     enum ResultType {
0034         Status, ///< A status (for full-text search)
0035         Account, ///< An account
0036         Hashtag, ///< A hashtag
0037     };
0038     Q_ENUM(ResultType);
0039 
0040     explicit SearchModel(QObject *parent = nullptr);
0041     ~SearchModel() override;
0042 
0043     /// Check if the search has finished loading
0044     /// \see setLoaded
0045     bool loaded() const;
0046 
0047     /// Sets the search loading status
0048     /// \see laoded
0049     void setLoaded(bool loaded);
0050 
0051     /// Start searching for \p queryString
0052     Q_INVOKABLE void search(const QString &queryString);
0053 
0054     /// Get a localized label for a result type
0055     Q_INVOKABLE QString labelForType(SearchModel::ResultType sectionType);
0056 
0057     /// Clear the fetched search results
0058     Q_INVOKABLE void clear();
0059 
0060     int rowCount(const QModelIndex &parent) const override;
0061     QVariant data(const QModelIndex &index, int role) const override;
0062 
0063 Q_SIGNALS:
0064     /// Emitted if the loading status has changed
0065     /// \see setLoaded
0066     void loadedChanged();
0067 
0068 private:
0069     QList<std::shared_ptr<Identity>> m_accounts;
0070     QList<Post *> m_statuses;
0071     QList<SearchHashtag> m_hashtags;
0072     bool m_loaded = false;
0073 };