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 "enginebase.h"
14
15#include "knewstuffcore_export.h"
16
17#include <memory>
18
19namespace KNSCore
20{
21class Entry;
22
23struct Comment {
24 QString id;
25 QString subject;
26 QString text;
27 int childCount = 0;
28 QString username;
29 QDateTime date;
30 int score = 0;
31 std::shared_ptr<KNSCore::Comment> parent;
32};
33class CommentsModelPrivate;
34
35/*!
36 * \class KNSCore::CommentsModel
37 * \inmodule KNewStuffCore
38 *
39 * \brief A model which takes care of the comments for a single Entry.
40 *
41 * \note This model should preferably be constructed by asking the Engine to give a model
42 * instance to you for a specific entry using the commentsForEntry function. If you
43 * insist, you can construct an instance yourself as well, but this is not recommended.
44 *
45 * \sa Engine::commentsForEntry(KNSCore::Entry)
46 * \since 5.63
47 */
48class KNEWSTUFFCORE_EXPORT CommentsModel : public QAbstractListModel
49{
50 Q_OBJECT
51 /*!
52 * \qmlproperty Entry CommentsModel::entry
53 * The Entry for which this model should handle comments
54 */
55 /*!
56 * \property KNSCore::CommentsModel::entry
57 * The Entry for which this model should handle comments
58 */
59 Q_PROPERTY(KNSCore::Entry entry READ entry WRITE setEntry NOTIFY entryChanged)
60public:
61 /*!
62 * Construct a new CommentsModel instance.
63 * \note The class is intended to be constructed using the Engine::commentsForEntry function
64 * \sa Engine::commentsForEntry(KNSCore::Entry)
65 */
66 explicit CommentsModel(EngineBase *parent = nullptr);
67 ~CommentsModel() override;
68
69 /*!
70 * \enum KNSCore::CommentsModel::Roles
71 *
72 * \value SubjectRole
73 * \value IdRole
74 * \value TextRole
75 * \value ChildCountRole
76 * \value UsernameRole
77 * \value DateRole
78 * \value ScoreRole
79 * \value ParentIndexRole
80 * \value DepthRole
81 */
82 enum Roles {
83 SubjectRole = Qt::DisplayRole,
84 IdRole = Qt::UserRole + 1,
85 TextRole,
86 ChildCountRole,
87 UsernameRole,
88 DateRole,
89 ScoreRole,
90 ParentIndexRole,
91 DepthRole,
92 };
93 Q_ENUM(Roles)
94
95 QHash<int, QByteArray> roleNames() const override;
96 QVariant data(const QModelIndex &index, int role = Qt::DisplayRole) const override;
97 int rowCount(const QModelIndex &parent = QModelIndex()) const override;
98 bool canFetchMore(const QModelIndex &parent) const override;
99 void fetchMore(const QModelIndex &parent) override;
100
101 const KNSCore::Entry &entry() const;
102 void setEntry(const KNSCore::Entry &newEntry);
103 Q_SIGNAL void entryChanged();
104
105private:
106 friend class CommentsModelPrivate; // For beginResetModel and beginInsertRows method calls
107 const std::unique_ptr<CommentsModelPrivate> d;
108};
109}
110
111#endif // KNSCORE_COMMENTSMODEL_H
112

source code of knewstuff/src/core/commentsmodel.h