summaryrefslogtreecommitdiff
path: root/topicmodel.cpp
diff options
context:
space:
mode:
authorJavier S. Pedro <maemo@javispedro.com>2013-04-04 15:49:31 +0200
committerJavier S. Pedro <maemo@javispedro.com>2013-04-04 15:49:31 +0200
commitd8fcff1a2d6eb61c97c44790dbdb920ba9f52980 (patch)
tree49df7f1e07e34061301ad5944a1807feba24b526 /topicmodel.cpp
parent11b4152301b408c7a4f02a8b202fed9f5e1ee1e7 (diff)
downloadtapasboard-d8fcff1a2d6eb61c97c44790dbdb920ba9f52980.tar.gz
tapasboard-d8fcff1a2d6eb61c97c44790dbdb920ba9f52980.zip
add showing unread posts
Diffstat (limited to 'topicmodel.cpp')
-rw-r--r--topicmodel.cpp67
1 files changed, 62 insertions, 5 deletions
diff --git a/topicmodel.cpp b/topicmodel.cpp
index c058a6a..6b4991d 100644
--- a/topicmodel.cpp
+++ b/topicmodel.cpp
@@ -8,7 +8,7 @@
#include "topicmodel.h"
TopicModel::TopicModel(QObject *parent) :
- QAbstractListModel(parent), _board(0), _topicId(-1), _eof(false)
+ QAbstractListModel(parent), _board(0), _topicId(-1), _eof(false), _firstUnread(-1)
{
QHash<int, QByteArray> roles = roleNames();
roles[TitleRole] = QByteArray("title");
@@ -32,6 +32,7 @@ void TopicModel::setBoard(Board *board)
{
if (_board != board) {
disconnect(this, SLOT(handleTopicPostsChanged(int,int,int)));
+ disconnect(this, SLOT(handleTopicPostsUnread(int,int)));
clearModel();
_board = board;
@@ -39,6 +40,8 @@ void TopicModel::setBoard(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();
@@ -68,6 +71,11 @@ void TopicModel::setTopicId(const int id)
}
}
+int TopicModel::firstUnreadPost() const
+{
+ return _firstUnread;
+}
+
int TopicModel::rowCount(const QModelIndex &parent) const
{
return parent.isValid() ? 0 : _data.size();
@@ -84,6 +92,12 @@ QVariant TopicModel::data(const QModelIndex &index, int role) const
return QVariant();
}
+ if (_data[row].post_id < 0) {
+ // This post is a unfetched stub
+ this->fetchPost(row);
+ return QVariant(); // Will update the model once the post arrives
+ }
+
switch (role) { // Mind the lack of break statements
case TitleRole:
return _data[row].title;
@@ -120,7 +134,7 @@ void TopicModel::fetchMore(const QModelIndex &parent)
const int start = _data.size();
QList<Post> posts = loadPosts(start, start + TOPIC_PAGE_SIZE - 1);
- const int new_end = start + _data.size() - 1;
+ const int new_end = start + posts.size() - 1;
if (posts.empty()) {
// We could not load anything more from DB!
@@ -133,11 +147,12 @@ void TopicModel::fetchMore(const QModelIndex &parent)
}
if (_board->service()->isAccessible()) {
- if (!_data.empty()) {
+ if (!posts.empty()) {
QDateTime last = oldestPostUpdate(posts);
// If the posts we got from DB are too old, refresh online.
if (last.secsTo(QDateTime::currentDateTime()) > TOPIC_POSTS_TLL) {
qDebug() << "Fetching posts because of old";
+ Q_ASSERT(new_end > 0);
_board->enqueueAction(new FetchPostsAction(_topicId,
start,
new_end,
@@ -215,6 +230,7 @@ QList<TopicModel::Post> TopicModel::loadPosts(int start, int end)
query.bindValue(":start", start);
query.bindValue(":limit", rows);
if (query.exec()) {
+ int loaded = 0;
posts.reserve(rows);
while (query.next()) {
Post post;
@@ -226,6 +242,7 @@ QList<TopicModel::Post> TopicModel::loadPosts(int start, int end)
post.time = parseDbDateTime(query.value(5));
post.last_update_time = parseDbDateTime(query.value(6));
posts.append(post);
+ loaded++;
}
} else {
qWarning() << "Could not load posts:" << query.lastError().text();
@@ -233,6 +250,36 @@ QList<TopicModel::Post> TopicModel::loadPosts(int start, int end)
return posts;
}
+void TopicModel::fetchPost(int position) const
+{
+ if (_board->service()->isAccessible()) {
+ // There are lest posts on the DB than we wanted
+ qDebug() << "Fetching posts because of unfetched";
+ // Always fetch one page at least.
+ int fetch_start = position % FORUM_PAGE_SIZE;
+ int fetch_end = fetch_start + FORUM_PAGE_SIZE;
+ _board->enqueueAction(new FetchPostsAction(_topicId,
+ fetch_start, fetch_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();
+ }
+}
+
void TopicModel::clearModel()
{
beginResetModel();
@@ -245,7 +292,6 @@ void TopicModel::handleTopicPostsChanged(int topicId, int start, int end)
{
if (topicId == _topicId) {
// Yep, our posts list changed.
- qDebug() << "My posts changed" << start << end;
if (end > _data.size()) {
// If for any reason we have more posts now, it means we might
// no longer be EOF...
@@ -264,7 +310,7 @@ void TopicModel::handleTopicPostsChanged(int topicId, int start, int end)
}
if (end >= _data.size()) {
- qDebug() << "Call insert rows" << _data.size() << end;
+ 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++) {
@@ -286,6 +332,17 @@ void TopicModel::handleTopicPostsChanged(int topicId, int start, int end)
}
}
+void TopicModel::handleTopicPostsUnread(int topicId, int position)
+{
+ if (topicId == _topicId) {
+ if (position != _firstUnread) {
+ enlargeModel(position);
+ _firstUnread = position;
+ emit firstUnreadPostChanged();
+ }
+ }
+}
+
void TopicModel::update()
{
if (!_board || _topicId < 0) return;