1 /*
2     SPDX-FileCopyrightText: 2019 Dan Leinir Turthra Jensen <admin@leinir.dk>
3 
4     SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL
5 */
6 
7 #ifndef KNSCORE_COMMENTSMODEL_H
8 #define KNSCORE_COMMENTSMODEL_H
9 
10 #include <QAbstractListModel>
11 #include <QDateTime>
12 
13 #include "engine.h"
14 
15 #include "knewstuffcore_export.h"
16 
17 namespace KNSCore
18 {
19 class EntryInternal;
20 
21 struct Comment {
22     QString id;
23     QString subject;
24     QString text;
25     int childCount = 0;
26     QString username;
27     QDateTime date;
28     int score = 0;
29     std::shared_ptr<KNSCore::Comment> parent;
30 };
31 
32 /**
33  * @brief A model which takes care of the comments for a single EntryInternal
34  *
35  * This model should preferably be constructed by asking the Engine to give a model
36  * instance to you for a specific entry using the commentsForEntry function. If you
37  * insist, you can construct an instance yourself as well, but this is not recommended.
38  *
39  * @see Engine::commentsForEntry(KNSCore::EntryInternal)
40  * @since 5.63
41  */
42 class KNEWSTUFFCORE_EXPORT CommentsModel : public QAbstractListModel
43 {
44     Q_OBJECT
45     /**
46      * The Entry for which this model should handle comments
47      */
48     Q_PROPERTY(KNSCore::EntryInternal entry READ entry WRITE setEntry NOTIFY entryChanged)
49 public:
50     /**
51      * Construct a new CommentsModel instance.
52      * @note The class is intended to be constructed using the Engine::commentsForEntry function
53      * @see Engine::commentsForEntry(KNSCore::EntryInternal)
54      */
55     explicit CommentsModel(Engine *parent = nullptr);
56     ~CommentsModel() override;
57 
58     enum Roles {
59         SubjectRole = Qt::DisplayRole,
60         IdRole = Qt::UserRole + 1,
61         TextRole,
62         ChildCountRole,
63         UsernameRole,
64         DateRole,
65         ScoreRole,
66         ParentIndexRole,
67         DepthRole,
68     };
69     Q_ENUM(Roles)
70 
71     QHash<int, QByteArray> roleNames() const override;
72     QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
73     int rowCount(const QModelIndex &parent = QModelIndex()) const override;
74     bool canFetchMore(const QModelIndex &parent) const override;
75     void fetchMore(const QModelIndex &parent) override;
76 
77     const KNSCore::EntryInternal &entry() const;
78     void setEntry(const KNSCore::EntryInternal &newEntry);
79     Q_SIGNAL void entryChanged();
80 
81 private:
82     class Private;
83     Private *d;
84 };
85 }
86 
87 #endif // KNSCORE_COMMENTSMODEL_H
88