summaryrefslogtreecommitdiff
path: root/topicmodel.cpp
blob: abfd04b134dcf22d88fecdf0ef88f8d5031d1239 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
#include <QtCore/QDebug>
#include <QtSql/QSqlError>

#include "global.h"
#include "board.h"
#include "xmlrpcinterface.h"
#include "fetchpostsaction.h"
#include "topicmodel.h"

TopicModel::TopicModel(QObject *parent) :
    QAbstractListModel(parent), _board(0), _topicId(-1), _eof(false), _firstUnread(-1)
{
	QHash<int, QByteArray> roles = roleNames();
	roles[TitleRole] = QByteArray("title");
	roles[ContentRole] = QByteArray("content");
	roles[IconRole] = QByteArray("icon");
	roles[PostIdRole] = QByteArray("postId");
	roles[UserIdRole] = QByteArray("userId");
	roles[UserNameRole] = QByteArray("userName");
	roles[DateTimeRole] = QByteArray("dateTime");
	roles[HumanDateRole] = QByteArray("humanDate");
	roles[HumanTimeRole] = QByteArray("humanTime");
	roles[UnreadRole] = QByteArray("unread");
	setRoleNames(roles);
}

Board * TopicModel::board() const
{
	return _board;
}

void TopicModel::setBoard(Board *board)
{
	if (_board != board) {
		disconnect(this, SLOT(handleTopicPostsChanged(int,int,int)));
		disconnect(this, SLOT(handleTopicPostsUnread(int,int)));
		clearModel();

		_board = board;

		if (_board) {
			connect(_board, SIGNAL(topicPostsChanged(int,int,int)),
			        SLOT(handleTopicPostsChanged(int,int,int)));
			connect(_board, SIGNAL(topicPostsUnread(int,int)),
			        SLOT(handleTopicPostsUnread(int,int)));
			if (_topicId >= 0) {
				update();
				reload();
			}
		}
		emit boardChanged();
	}
}

int TopicModel::topicId() const
{
	return _topicId;
}

void TopicModel::setTopicId(const int id)
{
	if (_topicId != id) {
		clearModel();

		_topicId = id;

		if (_topicId >= 0 && _board) {
			update();
			reload();
		}
		emit topicIdChanged();
	}
}

int TopicModel::firstUnreadPost() const
{
	return _firstUnread;
}

int TopicModel::rowCount(const QModelIndex &parent) const
{
	return parent.isValid() ? 0 : _data.size();
}

QVariant TopicModel::data(const QModelIndex &index, int role) const
{
	if (!index.isValid()) return QVariant();
	if (!_board) return QVariant();

	const int row = index.row();
	if (row >= _data.size()) {
		qWarning() << "Could not seek to" << row;
		return QVariant();
	}

	if (_data[row].post_id < 0) {
		// This post is a unfetched stub
		switch (role) {
		case PostIdRole:
			this->fetchPost(row);
			return QVariant::fromValue(-1);
		case TitleRole:
		case ContentRole:
		case UserNameRole:
		case HumanDateRole:
		case HumanTimeRole:
			return QVariant::fromValue(QString());
		case IconRole:
			return QVariant::fromValue(QUrl());
		case DateTimeRole:
			return QVariant::fromValue(QDateTime());
		case UnreadRole:
			return QVariant::fromValue(_firstUnread >= 0 && row >= _firstUnread);
		default:
			return QVariant();
		}
	}

	switch (role) { // Mind the lack of break statements
	case TitleRole:
		return _data[row].title;
	case ContentRole:
		return _data[row].content;
	case IconRole:
		return _data[row].icon;
	case PostIdRole:
		return _data[row].post_id;
	case UserIdRole:
		return _data[row].user_id;
	case UserNameRole:
		return _data[row].user_name;
	case DateTimeRole:
		return _data[row].time;
	case HumanDateRole:
		return _board->formatDateTime(_data[row].time,
		                              Board::RelativeDate);
	case HumanTimeRole:
		return _board->formatDateTime(_data[row].time,
		                              Board::ShowTime | Board::RelativeTime |
		                              Board::TodayYesterday);
	case UnreadRole:
		return _firstUnread >= 0 && row >= _firstUnread;
	}

	return QVariant();
}

bool TopicModel::canFetchMore(const QModelIndex &parent) const
{
	if (parent.isValid() || !_board || _topicId < 0) return false; // Invalid state
	return !_eof;
}

void TopicModel::fetchMore(const QModelIndex &parent)
{
	if (parent.isValid()) return;
	if (!_board || _topicId < 0) return;
	if (_eof) return;

	const int start = _data.size();
	QList<Post> posts = loadPosts(start, start + TOPIC_PAGE_SIZE - 1);
	const int new_end = start + posts.size() - 1;

	if (posts.empty()) {
		// We could not load anything more from DB!
		_eof = true;
	} else {
		beginInsertRows(QModelIndex(), start, new_end);
		_data.append(posts);
		_eof = posts.size() < TOPIC_PAGE_SIZE; // If short read, we reached EOF.
		endInsertRows();
	}

	if (_board->service()->isAccessible()) {
		if (!posts.empty()) {
			QDateTime last = oldestPostUpdate(posts);
			// If the posts we got from DB are too old, refresh online.
			if (last.secsTo(QDateTime::currentDateTimeUtc()) > TOPIC_POSTS_TLL) {
				qDebug() << "Fetching posts because of old";
				Q_ASSERT(new_end >= 0);
				_board->enqueueAction(new FetchPostsAction(_topicId,
				                                           start,
				                                           new_end,
				                                           _board));
			}
		}

		// Try to fetch more posts if board is online and we reached the end of DB
		if (_eof) {
			qDebug() << "Fetching posts because of EOF";
			_board->enqueueAction(new FetchPostsAction(_topicId,
			                                           _data.size(),
			                                           _data.size() + FORUM_PAGE_SIZE - 1,
			                                           _board));
		}
	}
}

void TopicModel::refresh()
{
	// Forcefully refresh all posts on this topic
	_board->enqueueAction(new FetchPostsAction(_topicId,
	                                           0,
	                                           FetchPostsAction::FetchAllPosts,
	                                           _board));
}

void TopicModel::markAsRead()
{
	_board->markTopicAsRead(_topicId);
}

QDateTime TopicModel::parseDbDateTime(const QVariant &v)
{
	QString s = v.toString();
	QDateTime dt = QDateTime::fromString(s, Qt::ISODate);
	dt.setTimeSpec(Qt::UTC);
	return dt;
}

QDateTime TopicModel::oldestPostUpdate(const QList<Post> &posts)
{
	if (posts.empty()) return QDateTime::currentDateTimeUtc();
	QDateTime min = posts.first().last_update_time;
	foreach (const Post& post, posts) {
		if (min < post.last_update_time) min = post.last_update_time;
	}
	return min;
}

QDateTime TopicModel::lastTopPostUpdate()
{
	if (!_board) return QDateTime();
	QSqlDatabase db = _board->database();
	QSqlQuery query(db);
	query.prepare("SELECT last_update_time FROM posts "
	              "WHERE topic_id = :topic_id "
	              "ORDER BY position ASC "
	              "LIMIT 1");
	query.bindValue(":topic_id", _topicId);
	if (query.exec()) {
		if (query.next()) {
			return parseDbDateTime(query.value(0));
		}
	} else {
		qWarning() << "Could not load top posts:" << query.lastError().text();
	}
	return QDateTime();
}

QList<TopicModel::Post> TopicModel::loadPosts(int start, int end)
{
	Q_ASSERT(_board);
	QList<Post> posts;
	QSqlQuery query(_board->database());
	query.prepare("SELECT post_id, post_title, post_content, icon_url, post_author_id, post_author_name, post_time, last_update_time FROM posts "
	              "WHERE topic_id = :topic_id AND position BETWEEN :start AND :end "
	              "ORDER by position ASC ");
	query.bindValue(":topic_id", _topicId);
	query.bindValue(":start", start);
	query.bindValue(":end", end);
	if (query.exec()) {
		int loaded = 0;
		posts.reserve(end - start + 1);
		while (query.next()) {
			Post post;
			post.post_id = query.value(0).toInt();
			post.title = query.value(1).toString();
			post.content = query.value(2).toString();
			post.icon = query.value(3).toUrl();
			post.user_id = query.value(4).toInt();
			post.user_name = query.value(5).toString();
			post.time = parseDbDateTime(query.value(6));
			post.last_update_time = parseDbDateTime(query.value(7));
			posts.append(post);
			loaded++;
		}
	} else {
		qWarning() << "Could not load posts:" << query.lastError().text();
	}
	return posts;
}

void TopicModel::fetchPost(int position) const
{
	if (_board->service()->isAccessible()) {
		// There are less posts on the DB than we wanted
		qDebug() << "Fetching post" << position << "because of unfetched";
		// Always fetch one page at least.
		int start = (position / TOPIC_PAGE_SIZE) * TOPIC_PAGE_SIZE;
		int end = (start + TOPIC_PAGE_SIZE) - 1;
		qDebug() << "From" << start << "to" << end;
		_board->enqueueAction(new FetchPostsAction(_topicId, start, end, _board));
	}
}

void TopicModel::enlargeModel(int end)
{
	int start = _data.size();
	if (end > start) {
		qDebug() << "Call insert rows (enlarge):" << start << end;
		beginInsertRows(QModelIndex(), start, end);
		Post post;
		post.post_id = -1;
		for (int i = start; i <= end; i++) {
			Q_ASSERT(_data.size() == i);
			_data.append(post);
		}
		endInsertRows();
		Q_ASSERT(_data.size() == end + 1);
	}
}

void TopicModel::clearModel()
{
	beginResetModel();
	_eof = false;
	_data.clear();
	endResetModel();
}

void TopicModel::handleTopicPostsChanged(int topicId, int start, int end)
{
	if (topicId == _topicId) {
		// Yep, our posts list changed.
		if (end > _data.size()) {
			// If for any reason we have more posts now, it means we might
			// no longer be EOF...
			_eof = false;
		}
		if (start > _data.size() + 1) {
			enlargeModel(start);
		}

		QList<Post> posts = loadPosts(start, end);
		if (posts.size() < end - start + 1) {
			// Short read
			qWarning() << "Short read while handling changed posts";
			return; // This should not happen, really.
		}

		Q_ASSERT(start <= _data.size());
		Q_ASSERT(end >= 0);

		if (end >= _data.size()) {
			qDebug() << "Call insert rows (changed):" << _data.size() << end;
			beginInsertRows(QModelIndex(), _data.size(), end);
			_data.reserve(end + 1);
			for (int i = start; i < _data.size(); i++) {
				_data[i] = posts[i - start];
			}
			for (int i = _data.size(); i <= end; i++) {
				Q_ASSERT(i >= start);
				_data.append(posts[i - start]);
			}
			endInsertRows();
			emit dataChanged(createIndex(start, 0), createIndex(_data.size() - 1, 0));
		} else {
			qDebug() << "Just refresh the data";
			for (int i = start; i <= end; i++) {
				_data[i] = posts[i - start];
			}
			emit dataChanged(createIndex(start, 0), createIndex(end, 0));
		}
	}
}

void TopicModel::handleTopicPostsUnread(int topicId, int position)
{
	if (topicId == _topicId) {
		if (position != _firstUnread) {
			enlargeModel(position);
			_firstUnread = position;
			qDebug() << "Changing unread post to" << position;
			emit firstUnreadPostChanged();
		}
	}
}

void TopicModel::update()
{
	if (!_board || _topicId < 0) return;
	// Start by requesting an update of the first 20 topics
	if (!_board->service()->isAccessible()) return;
	QDateTime last = lastTopPostUpdate();
	if (!last.isValid() ||
	        last.secsTo(QDateTime::currentDateTimeUtc()) > TOPIC_TOP_TLL) {
		qDebug() << "Fetching posts because the top are old";
		// Outdated or empty, refresh.
		if (_board->loggedIn() && _board->getConfig("goto_unread") == "1") {
			_board->enqueueAction(new FetchPostsAction(_topicId,
			                                           FetchPostsAction::FetchUnreadPosts,
			                                           TOPIC_PAGE_SIZE,
			                                           _board));
		} else {
			_board->enqueueAction(new FetchPostsAction(_topicId,
			                                           0,
			                                           TOPIC_PAGE_SIZE - 1,
			                                           _board));
		}
	} else {
		qDebug() << "Topics not outdated";
	}
}

void TopicModel::reload()
{
	Q_ASSERT(_data.empty());
	Q_ASSERT(!_eof);
	fetchMore();
}