Changeset View
Standalone View
src/core/commentsmodel.cpp
- This file was added.
1 | /* | ||||
---|---|---|---|---|---|
2 | * Copyright (C) 2019 Dan Leinir Turthra Jensen <admin@leinir.dk> | ||||
3 | * | ||||
4 | * This library is free software; you can redistribute it and/or | ||||
5 | * modify it under the terms of the GNU Lesser General Public | ||||
6 | * License as published by the Free Software Foundation; either | ||||
7 | * version 2.1 of the License, or (at your option) version 3, or any | ||||
8 | * later version accepted by the membership of KDE e.V. (or its | ||||
9 | * successor approved by the membership of KDE e.V.), which shall | ||||
10 | * act as a proxy defined in Section 6 of version 3 of the license. | ||||
11 | * | ||||
12 | * This library is distributed in the hope that it will be useful, | ||||
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | ||||
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||||
15 | * Lesser General Public License for more details. | ||||
16 | * | ||||
17 | * You should have received a copy of the GNU Lesser General Public | ||||
18 | * License along with this library. If not, see <http://www.gnu.org/licenses/>. | ||||
19 | * | ||||
20 | */ | ||||
21 | | ||||
22 | #include "commentsmodel.h" | ||||
23 | | ||||
24 | #include "entryinternal.h" | ||||
25 | #include "engine.h" | ||||
26 | #include "knewstuffcore_debug.h" | ||||
27 | | ||||
28 | #include <KLocalizedString> | ||||
29 | | ||||
30 | #include <QDateTime> | ||||
31 | | ||||
32 | namespace KNSCore { | ||||
33 | class CommentsModel::Private { | ||||
34 | public: | ||||
35 | Private(CommentsModel *qq) | ||||
36 | : q(qq) | ||||
37 | {} | ||||
38 | CommentsModel* q = nullptr; | ||||
39 | Engine* engine = nullptr; | ||||
40 | | ||||
41 | EntryInternal entry; | ||||
42 | | ||||
43 | QList<std::shared_ptr<KNSCore::Comment>> comments; | ||||
44 | | ||||
45 | enum FetchOptions { | ||||
46 | NoOption, | ||||
47 | ClearModel | ||||
48 | }; | ||||
49 | void fetch(FetchOptions option = NoOption) { | ||||
50 | // Sanity checks, because we need a few things to be correct before we can actually fetch comments... | ||||
51 | if (!engine) { | ||||
52 | qCWarning(KNEWSTUFFCORE) << "CommentsModel must be parented on a KNSCore::Engine instance to be able to fetch comments"; | ||||
53 | } | ||||
54 | if (!entry.isValid()) { | ||||
55 | qCWarning(KNEWSTUFFCORE) << "Without an entry to fetch comments for, CommentsModel cannot fetch comments for it"; | ||||
56 | } | ||||
57 | | ||||
58 | if (engine && entry.isValid()) { | ||||
59 | QSharedPointer<Provider> provider = engine->provider(entry.providerId()); | ||||
60 | if (option == ClearModel) { | ||||
61 | emit q->beginResetModel(); | ||||
62 | comments.clear(); | ||||
63 | provider->disconnect(q); | ||||
64 | q->connect(provider.data(), &Provider::commentsLoaded, q, [=](const QList<std::shared_ptr<KNSCore::Comment>> &newComments){ | ||||
65 | QList<std::shared_ptr<KNSCore::Comment>> actualNewComments; | ||||
66 | for (std::shared_ptr<KNSCore::Comment> comment : newComments) { | ||||
67 | bool commentIsKnown = false; | ||||
68 | for (std::shared_ptr<KNSCore::Comment> existingComment : comments) { | ||||
69 | if (existingComment->id == comment->id) { | ||||
70 | commentIsKnown = true; | ||||
71 | break; | ||||
72 | } | ||||
73 | } | ||||
74 | if (commentIsKnown) { | ||||
75 | continue; | ||||
76 | } | ||||
77 | actualNewComments << comment; | ||||
78 | } | ||||
79 | if (actualNewComments.count() > 0) { | ||||
80 | q->beginInsertRows(QModelIndex(), comments.count(), comments.count() + actualNewComments.count() - 1); | ||||
81 | qDebug() << "Appending" << actualNewComments.count() << "new comments"; | ||||
82 | comments.append(actualNewComments); | ||||
83 | q->endInsertRows(); | ||||
84 | } | ||||
85 | }); | ||||
86 | emit q->endResetModel(); | ||||
87 | } | ||||
88 | int commentsPerPage = 100; | ||||
89 | int pageToLoad = comments.count() / commentsPerPage; | ||||
90 | qDebug() << "Loading comments, page" << pageToLoad << "with current comment count" << comments.count() << "out of a total of" << entry.numberOfComments(); | ||||
91 | provider->loadComments(entry, commentsPerPage, pageToLoad); | ||||
92 | } | ||||
93 | } | ||||
94 | }; | ||||
95 | } | ||||
96 | | ||||
97 | KNSCore::CommentsModel::CommentsModel(Engine* parent) | ||||
98 | : QAbstractListModel(parent) | ||||
99 | , d(new Private(this)) | ||||
100 | { | ||||
ahiemstra: Categorise or drop :) | |||||
101 | d->engine = parent; | ||||
102 | } | ||||
103 | | ||||
104 | KNSCore::CommentsModel::~CommentsModel() | ||||
105 | { | ||||
106 | delete d; | ||||
107 | } | ||||
108 | | ||||
109 | QHash<int, QByteArray> KNSCore::CommentsModel::roleNames() const | ||||
110 | { | ||||
111 | QHash<int, QByteArray> roles; | ||||
112 | roles[IdRole] = "id"; | ||||
113 | roles[SubjectRole] = "subject"; | ||||
114 | roles[TextRole] = "text"; | ||||
115 | roles[ChildCountRole] = "childCound"; | ||||
116 | roles[UsernameRole] = "username"; | ||||
117 | roles[DateRole] = "date"; | ||||
118 | roles[ScoreRole] = "score"; | ||||
119 | roles[ParentIndexRole] = "parentIndex"; | ||||
120 | roles[DepthRole] = "depth"; | ||||
121 | return roles; | ||||
122 | } | ||||
123 | | ||||
124 | QVariant KNSCore::CommentsModel::data(const QModelIndex& index, int role) const | ||||
125 | { | ||||
126 | QVariant value; | ||||
127 | if (index.isValid() && index.row() < d->comments.count()) { | ||||
128 | std::shared_ptr<KNSCore::Comment> comment = d->comments[index.row()]; | ||||
129 | switch (role) | ||||
130 | { | ||||
131 | case IdRole: | ||||
132 | value.setValue(comment->id); | ||||
133 | break; | ||||
134 | case SubjectRole: | ||||
135 | value.setValue(comment->subject); | ||||
136 | break; | ||||
Might want to add a call to checkIndex here (https://doc.qt.io/qt-5/qabstractitemmodel.html#checkIndex), it would obsolete some additional checking you do later. ahiemstra: Might want to add a call to checkIndex here (https://doc.qt.io/qt-5/qabstractitemmodel. | |||||
After confirming that 5.11 (which introduces that function) is indeed acceptable for Frameworks, i'm aaaall over that ;) Very handy little function, that :) leinir: After confirming that 5.11 (which introduces that function) is indeed acceptable for Frameworks… | |||||
137 | case TextRole: | ||||
138 | value.setValue(comment->text); | ||||
139 | break; | ||||
140 | case ChildCountRole: | ||||
141 | value.setValue(comment->childCount); | ||||
142 | break; | ||||
143 | case UsernameRole: | ||||
144 | value.setValue(comment->username); | ||||
145 | break; | ||||
146 | case DateRole: | ||||
147 | value.setValue(comment->date); | ||||
148 | break; | ||||
149 | case ScoreRole: | ||||
150 | value.setValue(comment->score); | ||||
151 | break; | ||||
152 | case ParentIndexRole: | ||||
153 | { | ||||
154 | int idx{-1}; | ||||
155 | if(comment->parent) { | ||||
156 | d->comments.indexOf(std::shared_ptr<KNSCore::Comment>(comment->parent)); | ||||
157 | } | ||||
158 | value.setValue(idx); | ||||
159 | } | ||||
160 | break; | ||||
161 | case DepthRole: | ||||
162 | { | ||||
163 | int depth = 0; | ||||
164 | Comment* child = comment.get(); | ||||
165 | while (child->parent) { | ||||
166 | ++depth; | ||||
167 | child = child->parent; | ||||
168 | } | ||||
169 | value.setValue(depth); | ||||
170 | } | ||||
171 | break; | ||||
172 | default: | ||||
173 | value.setValue(i18nc("The value returned for an unknown role when requesting data from the model.", "")); | ||||
174 | break; | ||||
175 | } | ||||
176 | } | ||||
177 | return value; | ||||
178 | } | ||||
Since you're already checking for child being true (aka not-null), this if becomes rather superfluous. ahiemstra: Since you're already checking for `child` being true (aka not-null), this if becomes rather… | |||||
leinir: Hmm... leftovers from earlier versions of that function, there :) | |||||
179 | | ||||
180 | QVariant KNSCore::CommentsModel::data(int index, int role) const | ||||
181 | { | ||||
182 | return data(CommentsModel::index(index), role); | ||||
183 | } | ||||
184 | | ||||
185 | int KNSCore::CommentsModel::rowCount(const QModelIndex& parent) const | ||||
broulik: Any particular reason not do just return a null `QVariant()`? | |||||
Hmm... This actually is supposed to be "Unknown model role"... It's kind of corner-casey, but i've noticed in the past that something other than just an invalid QVariant is occasionally useful for those times where you've, say, missed the h in "depth" ;) Usually this wouldn't be hit, of course, so it's not actually expensive in any real way. But yes, compared to this, QVariant() would be the better option. leinir: Hmm... This actually is supposed to be "Unknown model role"... It's kind of corner-casey, but… | |||||
186 | { | ||||
187 | if (parent.isValid()) | ||||
188 | return 0; | ||||
189 | return d->comments.count(); | ||||
190 | } | ||||
191 | | ||||
192 | bool KNSCore::CommentsModel::canFetchMore(const QModelIndex& parent) const | ||||
193 | { | ||||
194 | if (parent.isValid()) | ||||
195 | return false; | ||||
196 | if (d->entry.numberOfComments() > d->comments.count()) | ||||
197 | return true; | ||||
198 | return false; | ||||
199 | } | ||||
200 | | ||||
201 | void KNSCore::CommentsModel::fetchMore(const QModelIndex& parent) | ||||
202 | { | ||||
203 | if(parent.isValid()) | ||||
204 | return; | ||||
205 | d->fetch(); | ||||
206 | } | ||||
207 | | ||||
208 | const KNSCore::EntryInternal & KNSCore::CommentsModel::entry() const | ||||
209 | { | ||||
210 | return d->entry; | ||||
211 | } | ||||
212 | | ||||
213 | void KNSCore::CommentsModel::setEntry(const KNSCore::EntryInternal& newEntry) | ||||
214 | { | ||||
215 | d->entry = newEntry; | ||||
216 | d->fetch(Private::ClearModel); | ||||
217 | emit entryChanged(); | ||||
218 | } |
Categorise or drop :)