File indexing completed on 2024-04-21 03:56:22

0001 /*
0002     SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
0003 
0004     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
0005 */
0006 
0007 #ifndef KNSCORE_COMMENTSMODEL_H
0008 #define KNSCORE_COMMENTSMODEL_H
0009 
0010 #include <QAbstractListModel>
0011 #include <QDateTime>
0012 
0013 #include "enginebase.h"
0014 
0015 #include "knewstuffcore_export.h"
0016 
0017 #include <memory>
0018 
0019 namespace KNSCore
0020 {
0021 class Entry;
0022 
0023 struct Comment {
0024     QString id;
0025     QString subject;
0026     QString text;
0027     int childCount = 0;
0028     QString username;
0029     QDateTime date;
0030     int score = 0;
0031     std::shared_ptr<KNSCore::Comment> parent;
0032 };
0033 class CommentsModelPrivate;
0034 
0035 /**
0036  * @brief A model which takes care of the comments for a single Entry
0037  *
0038  * This model should preferably be constructed by asking the Engine to give a model
0039  * instance to you for a specific entry using the commentsForEntry function. If you
0040  * insist, you can construct an instance yourself as well, but this is not recommended.
0041  *
0042  * @see Engine::commentsForEntry(KNSCore::Entry)
0043  * @since 5.63
0044  */
0045 class KNEWSTUFFCORE_EXPORT CommentsModel : public QAbstractListModel
0046 {
0047     Q_OBJECT
0048     /**
0049      * The Entry for which this model should handle comments
0050      */
0051     Q_PROPERTY(KNSCore::Entry entry READ entry WRITE setEntry NOTIFY entryChanged)
0052 public:
0053     /**
0054      * Construct a new CommentsModel instance.
0055      * @note The class is intended to be constructed using the Engine::commentsForEntry function
0056      * @see Engine::commentsForEntry(KNSCore::Entry)
0057      */
0058     explicit CommentsModel(EngineBase *parent = nullptr);
0059     ~CommentsModel() override;
0060 
0061     enum Roles {
0062         SubjectRole = Qt::DisplayRole,
0063         IdRole = Qt::UserRole + 1,
0064         TextRole,
0065         ChildCountRole,
0066         UsernameRole,
0067         DateRole,
0068         ScoreRole,
0069         ParentIndexRole,
0070         DepthRole,
0071     };
0072     Q_ENUM(Roles)
0073 
0074     QHash<int, QByteArray> roleNames() const override;
0075     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
0076     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
0077     bool canFetchMore(const QModelIndex &parent) const override;
0078     void fetchMore(const QModelIndex &parent) override;
0079 
0080     const KNSCore::Entry &entry() const;
0081     void setEntry(const KNSCore::Entry &newEntry);
0082     Q_SIGNAL void entryChanged();
0083 
0084 private:
0085     friend class CommentsModelPrivate; // For beginResetModel and beginInsertRows method calls
0086     const std::unique_ptr<CommentsModelPrivate> d;
0087 };
0088 }
0089 
0090 #endif // KNSCORE_COMMENTSMODEL_H