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 | ~Private() | ||||
39 | { | ||||
40 | qDeleteAll(comments); | ||||
41 | } | ||||
42 | CommentsModel* q = nullptr; | ||||
43 | Engine* engine = nullptr; | ||||
44 | | ||||
45 | EntryInternal entry; | ||||
46 | | ||||
47 | QList<Comment*> comments; | ||||
48 | | ||||
49 | enum FetchOptions { | ||||
50 | NoOption, | ||||
51 | ClearModel | ||||
52 | }; | ||||
53 | void fetch(FetchOptions option = NoOption) { | ||||
54 | // Sanity checks, because we need a few things to be correct before we can actually fetch comments... | ||||
55 | if (!engine) { | ||||
56 | qCWarning(KNEWSTUFFCORE) << "CommentsModel must be parented on a KNSCore::Engine instance to be able to fetch comments"; | ||||
57 | } | ||||
58 | if (!entry.isValid()) { | ||||
59 | qCWarning(KNEWSTUFFCORE) << "Without an entry to fetch comments for, CommentsModel cannot fetch comments for it"; | ||||
60 | } | ||||
61 | | ||||
62 | if (engine && entry.isValid()) { | ||||
63 | QSharedPointer<Provider> provider = engine->provider(entry.providerId()); | ||||
64 | if (option == ClearModel) { | ||||
65 | emit q->beginResetModel(); | ||||
66 | qDeleteAll(comments); | ||||
67 | comments.clear(); | ||||
68 | provider->disconnect(q); | ||||
69 | q->connect(provider.data(), &Provider::commentsLoaded, q, [=](const QList<KNSCore::Comment*> &newComments){ | ||||
70 | QList<KNSCore::Comment*> actualNewComments; | ||||
71 | for (KNSCore::Comment* comment : newComments) { | ||||
72 | bool commentIsKnown = false; | ||||
73 | for (KNSCore::Comment* existingComment : comments) { | ||||
74 | if (existingComment->id == comment->id) { | ||||
75 | commentIsKnown = true; | ||||
76 | break; | ||||
77 | } | ||||
78 | } | ||||
79 | if (commentIsKnown) { | ||||
80 | continue; | ||||
81 | } | ||||
82 | actualNewComments << comment; | ||||
83 | } | ||||
84 | if (actualNewComments.count() > 0) { | ||||
85 | q->beginInsertRows(QModelIndex(), comments.count(), comments.count() + actualNewComments.count() - 1); | ||||
86 | qDebug() << "Appending" << actualNewComments.count() << "new comments"; | ||||
87 | comments.append(actualNewComments); | ||||
88 | q->endInsertRows(); | ||||
89 | } | ||||
90 | }); | ||||
91 | emit q->endResetModel(); | ||||
92 | } | ||||
93 | int commentsPerPage = 100; | ||||
94 | int pageToLoad = comments.count() / commentsPerPage; | ||||
95 | qDebug() << "Loading comments, page" << pageToLoad << "with current comment count" << comments.count() << "out of a total of" << entry.numberOfComments(); | ||||
96 | provider->loadComments(entry, commentsPerPage, pageToLoad); | ||||
97 | } | ||||
98 | } | ||||
99 | }; | ||||
100 | } | ||||
ahiemstra: Categorise or drop :) | |||||
101 | | ||||
102 | KNSCore::CommentsModel::CommentsModel(Engine* parent) | ||||
103 | : QAbstractListModel(parent) | ||||
104 | , d(new Private(this)) | ||||
105 | { | ||||
106 | d->engine = parent; | ||||
107 | } | ||||
108 | | ||||
109 | KNSCore::CommentsModel::~CommentsModel() | ||||
110 | { | ||||
111 | delete d; | ||||
112 | } | ||||
113 | | ||||
114 | QHash<int, QByteArray> KNSCore::CommentsModel::roleNames() const | ||||
115 | { | ||||
116 | QHash<int, QByteArray> roles; | ||||
117 | roles[IdRole] = "id"; | ||||
118 | roles[SubjectRole] = "subject"; | ||||
119 | roles[TextRole] = "text"; | ||||
120 | roles[ChildCountRole] = "childCound"; | ||||
121 | roles[UsernameRole] = "username"; | ||||
122 | roles[DateRole] = "date"; | ||||
123 | roles[ScoreRole] = "score"; | ||||
124 | roles[ParentIndexRole] = "parentIndex"; | ||||
125 | roles[DepthRole] = "depth"; | ||||
126 | return roles; | ||||
127 | } | ||||
128 | | ||||
129 | QVariant KNSCore::CommentsModel::data(const QModelIndex& index, int role) const | ||||
130 | { | ||||
131 | QVariant value; | ||||
132 | if (index.isValid() && index.row() < d->comments.count()) { | ||||
133 | Comment* comment = d->comments[index.row()]; | ||||
134 | switch (role) | ||||
135 | { | ||||
136 | case IdRole: | ||||
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 | value.setValue(comment->id); | ||||
138 | break; | ||||
139 | case SubjectRole: | ||||
140 | value.setValue(comment->subject); | ||||
141 | break; | ||||
142 | case TextRole: | ||||
143 | value.setValue(comment->text); | ||||
144 | break; | ||||
145 | case ChildCountRole: | ||||
146 | value.setValue(comment->childCount); | ||||
147 | break; | ||||
148 | case UsernameRole: | ||||
149 | value.setValue(comment->username); | ||||
150 | break; | ||||
151 | case DateRole: | ||||
152 | value.setValue(comment->date); | ||||
153 | break; | ||||
154 | case ScoreRole: | ||||
155 | value.setValue(comment->score); | ||||
156 | break; | ||||
157 | case ParentIndexRole: | ||||
158 | { | ||||
159 | int idx{-1}; | ||||
160 | if(comment->parent) { | ||||
161 | d->comments.indexOf(comment->parent); | ||||
162 | } | ||||
163 | value.setValue(idx); | ||||
164 | } | ||||
165 | break; | ||||
166 | case DepthRole: | ||||
167 | { | ||||
168 | int depth = 0; | ||||
169 | Comment* child = comment; | ||||
170 | while (child->parent) { | ||||
171 | ++depth; | ||||
172 | child = child->parent; | ||||
173 | } | ||||
174 | value.setValue(depth); | ||||
175 | } | ||||
176 | break; | ||||
177 | default: | ||||
178 | value.setValue(i18nc("The value returned for an unknown role when requesting data from the model.", "")); | ||||
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 | break; | ||||
180 | } | ||||
181 | } | ||||
182 | return value; | ||||
183 | } | ||||
184 | | ||||
185 | QVariant KNSCore::CommentsModel::data(int index, int role) 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 | return data(CommentsModel::index(index), role); | ||||
188 | } | ||||
189 | | ||||
190 | int KNSCore::CommentsModel::rowCount(const QModelIndex& parent) const | ||||
191 | { | ||||
192 | if (parent.isValid()) | ||||
193 | return 0; | ||||
194 | return d->comments.count(); | ||||
195 | } | ||||
196 | | ||||
197 | bool KNSCore::CommentsModel::canFetchMore(const QModelIndex& parent) const | ||||
198 | { | ||||
199 | if (parent.isValid()) | ||||
200 | return false; | ||||
201 | if (d->entry.numberOfComments() > d->comments.count()) | ||||
202 | return true; | ||||
203 | return false; | ||||
204 | } | ||||
205 | | ||||
206 | void KNSCore::CommentsModel::fetchMore(const QModelIndex& parent) | ||||
207 | { | ||||
208 | if(parent.isValid()) | ||||
209 | return; | ||||
210 | d->fetch(); | ||||
211 | } | ||||
212 | | ||||
213 | const KNSCore::EntryInternal & KNSCore::CommentsModel::entry() const | ||||
214 | { | ||||
215 | return d->entry; | ||||
216 | } | ||||
217 | | ||||
218 | void KNSCore::CommentsModel::setEntry(const KNSCore::EntryInternal& newEntry) | ||||
219 | { | ||||
220 | d->entry = newEntry; | ||||
221 | d->fetch(Private::ClearModel); | ||||
222 | emit entryChanged(); | ||||
223 | } |
Categorise or drop :)