From eaf95f0c22b50c599c5713166fba03c1c13ff644 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Sun, 7 Apr 2013 18:52:23 +0200 Subject: refresh topics also when viewing outdated ones --- forummodel.cpp | 115 +++++++++++++++++++++++++++++++++++---------------------- forummodel.h | 1 + topicmodel.cpp | 6 +-- 3 files changed, 75 insertions(+), 47 deletions(-) diff --git a/forummodel.cpp b/forummodel.cpp index 3e912c6..73482e4 100644 --- a/forummodel.cpp +++ b/forummodel.cpp @@ -87,6 +87,9 @@ QVariant ForumModel::data(const QModelIndex &index, int role) const switch (role) { case TitleRole: + if (_data[row].last_update_time.secsTo(QDateTime::currentDateTimeUtc()) > FORUM_TOPICS_TLL) { + fetchTopic(row); + } return _data[row].title; case TopicIdRole: return _data[row].topic_id; @@ -134,6 +137,7 @@ void ForumModel::fetchMore(const QModelIndex &parent) // We could not load anything more from DB! _eof = true; } else { + qDebug() << "Insert rows" << normal_offset + normal_start << normal_offset + normal_new_end; beginInsertRows(QModelIndex(), normal_offset + normal_start, normal_offset + normal_new_end); _data.append(topics); @@ -141,31 +145,15 @@ void ForumModel::fetchMore(const QModelIndex &parent) endInsertRows(); } - if (_board->service()->isAccessible()) { - if (!topics.empty()) { - QDateTime last = oldestPostUpdate(topics); - // If the topics we got from DB are too old, refresh online. - if (last.secsTo(QDateTime::currentDateTimeUtc()) > FORUM_TOPICS_TLL) { - qDebug() << "Fetching topics because of old"; - Q_ASSERT(normal_new_end >= 0); - _board->enqueueAction(new FetchTopicsAction(_forumId, - Board::Normal, - normal_start, - normal_new_end, - _board)); - } - } - + if (_board->service()->isAccessible() && _eof) { // Try to fetch more topics if board is online and we reached the end of DB - if (_eof) { - qDebug() << "Fetching topics because of EOF"; - const int normal_cur_end = _data.size() - 1 - normal_offset; - _board->enqueueAction(new FetchTopicsAction(_forumId, - Board::Normal, - normal_cur_end + 1, - normal_cur_end + FORUM_PAGE_SIZE, - _board)); - } + qDebug() << "Fetching topics because of EOF"; + const int normal_cur_end = _data.size() - 1 - normal_offset; + _board->enqueueAction(new FetchTopicsAction(_forumId, + Board::Normal, + normal_cur_end + 1, + normal_cur_end + FORUM_PAGE_SIZE, + _board)); } } @@ -256,6 +244,31 @@ QList ForumModel::loadTopics(Board::TopicType type, int start return topics; } +void ForumModel::fetchTopic(int position) const +{ + if (_board->service()->isAccessible()) { + // There are less posts on the DB than we wanted + qDebug() << "Fetching topic" << position << "because of unfetched/old"; + // Figure out the topic type + Board::TopicType type; + if (position < _numAnnouncements) { + type = Board::Announcement; + } else if (position < _numAnnouncements + _numSticky) { + type = Board::Sticky; + position -= _numAnnouncements; + } else { + type = Board::Normal; + position -= _numAnnouncements + _numSticky; + } + // Always fetch one page at least. + int start = (position / FORUM_PAGE_SIZE) * FORUM_PAGE_SIZE; + int end = (start + FORUM_PAGE_SIZE) - 1; + qDebug() << "From" << start << "to" << end; + _board->enqueueAction(new FetchTopicsAction(_forumId, type, + start, end, _board)); + } +} + void ForumModel::clearModel() { beginResetModel(); @@ -270,13 +283,12 @@ void ForumModel::handleForumTopicsChanged(int forumId, Board::TopicType type, if (forumId != _forumId) { return; // Not our topics } + // TODO This probably can be simplified quite a lot. if (type == Board::Normal) { - // Yep, our topics list changed. + // Yep, our normal topics list changed. const int normal_offset = _numAnnouncements + _numSticky; int current_normal_end = _data.size() - 1 - normal_offset; - qDebug() << "My topics changed" << start << end; - if (end >= current_normal_end) { // If for any reason we have more topics now, it means we might // no longer be EOF... @@ -289,15 +301,21 @@ void ForumModel::handleForumTopicsChanged(int forumId, Board::TopicType type, return; } + const int requested = end - start + 1; QList topics = loadTopics(Board::Normal, start, end); - if (topics.size() < end - start + 1) { + + end = start + topics.size() - 1; + + // Less topics than the number of topics we requested? + if (topics.size() < requested) { _eof = true; // Short read - end = start + topics.size() - 1; + if (current_normal_end > end) { const int current_full_end = _data.size() - 1; const int new_full_end = end + normal_offset; Q_ASSERT(new_full_end < current_full_end); + qDebug() << "Remove rows" << new_full_end << current_full_end; beginRemoveRows(QModelIndex(), new_full_end, current_full_end); while (_data.size() > new_full_end) { @@ -309,32 +327,34 @@ void ForumModel::handleForumTopicsChanged(int forumId, Board::TopicType type, } } + Q_ASSERT(end == start + topics.size() - 1); + if (end > current_normal_end) { + // More topics than we currently have in the model const int current_full_end = _data.size() - 1; const int new_full_end = end + normal_offset; qDebug() << "Insert rows" << current_full_end + 1 << new_full_end; beginInsertRows(QModelIndex(), current_full_end + 1, new_full_end); _data.reserve(new_full_end + 1); const int full_start = normal_offset + start; - for (int i = full_start; i <= current_full_end; i++) { - _data[i] = topics[i - full_start]; - } Q_ASSERT(current_full_end + 1 >= full_start); for (int i = current_full_end + 1; i <= new_full_end; i++) { _data.append(topics[i - full_start]); } endInsertRows(); Q_ASSERT(new_full_end == _data.size() - 1); - emit dataChanged(createIndex(full_start, 0), createIndex(new_full_end, 0)); - } else { - qDebug() << "Just refresh the data"; - const int full_start = normal_offset + start; - const int full_end = normal_offset + end; - for (int i = full_start; i < full_end; i++) { - _data[i] = topics[i - full_start]; - } - emit dataChanged(createIndex(full_start, 0), createIndex(full_end, 0)); + end = current_normal_end; + } + + Q_ASSERT(end <= start + topics.size() - 1); + + const int full_start = normal_offset + start; + const int full_end = normal_offset + end; + for (int i = full_start; i < full_end; i++) { + _data[i] = topics[i - full_start]; } + qDebug() << "Changed rows" << full_start << full_end; + emit dataChanged(createIndex(full_start, 0), createIndex(full_end, 0)); } else if (type == Board::Sticky || type == Board::Announcement) { // We just reload these fully QList topics = loadTopics(type, 0, MAX_FORUM_PAGE_SIZE - 1); @@ -345,6 +365,7 @@ void ForumModel::handleForumTopicsChanged(int forumId, Board::TopicType type, if (topics.size() < cur_number) { const int cur_end = offset + cur_number - 1; const int new_end = offset + topics.size() - 1; + qDebug() << "Remove rows" << new_end + 1 << cur_end; beginRemoveRows(QModelIndex(), new_end + 1, cur_end); for (int i = new_end + 1; i <= cur_end; i++) { _data.removeAt(i); @@ -356,6 +377,7 @@ void ForumModel::handleForumTopicsChanged(int forumId, Board::TopicType type, const int new_end = offset + topics.size() - 1; const int new_start = cur_end + 1; Q_ASSERT(new_end - offset == topics.size() - 1); + qDebug() << "Insert rows" << new_start << new_end; beginInsertRows(QModelIndex(), new_start, new_end); for (int i = new_start; i <= new_end; i++) { _data.insert(i, topics.takeAt(new_start - offset)); @@ -367,10 +389,13 @@ void ForumModel::handleForumTopicsChanged(int forumId, Board::TopicType type, const int start = offset; const int end = offset + topics.size() - 1; - for (int i = start; i <= end; i++) { - _data.insert(i, topics.at(i - offset)); + if (end >= start) { + for (int i = start; i <= end; i++) { + _data.insert(i, topics.at(i - offset)); + } + qDebug() << "Changed rows" << start << end; + emit dataChanged(createIndex(start, 0), createIndex(end, 0)); } - emit dataChanged(createIndex(start, 0), createIndex(end, 0)); } } @@ -429,6 +454,7 @@ void ForumModel::reload() QList topics; topics = loadTopics(Board::Announcement, 0, MAX_FORUM_PAGE_SIZE - 1); if (!topics.empty()) { + qDebug() << "Insert rows" << 0 << topics.size() - 1; beginInsertRows(QModelIndex(), 0, topics.size() - 1); _data.append(topics); _numAnnouncements = topics.size(); @@ -439,6 +465,7 @@ void ForumModel::reload() topics = loadTopics(Board::Sticky, 0, MAX_FORUM_PAGE_SIZE - 1); if (!topics.empty()) { + qDebug() << "Insert rows" << _numAnnouncements << _numAnnouncements + topics.size() - 1; beginInsertRows(QModelIndex(), _numAnnouncements, _numAnnouncements + topics.size() - 1); _data.append(topics); _numSticky = topics.size(); diff --git a/forummodel.h b/forummodel.h index 76f6912..10afa86 100644 --- a/forummodel.h +++ b/forummodel.h @@ -63,6 +63,7 @@ private: static QDateTime oldestPostUpdate(const QList& topics); QDateTime lastTopPostUpdate(); QList loadTopics(Board::TopicType type, int start, int end); + void fetchTopic(int position) const; // const because data() calls this void clearModel(); private slots: diff --git a/topicmodel.cpp b/topicmodel.cpp index 4188784..36fc4c0 100644 --- a/topicmodel.cpp +++ b/topicmodel.cpp @@ -281,11 +281,11 @@ QList TopicModel::loadPosts(int start, int end) void TopicModel::fetchPost(int position) const { if (_board->service()->isAccessible()) { - // There are lest posts on the DB than we wanted + // 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 / FORUM_PAGE_SIZE) * FORUM_PAGE_SIZE; - int end = (start + FORUM_PAGE_SIZE) - 1; + 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)); } -- cgit v1.2.3