From 427004acc6a80004f69c6ce059667465023269a9 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Tue, 2 Apr 2013 00:51:58 +0200 Subject: parse bbcode, show usernames, .... --- topicmodel.cpp | 38 +++++++++++++++++++++++++++----------- 1 file changed, 27 insertions(+), 11 deletions(-) (limited to 'topicmodel.cpp') diff --git a/topicmodel.cpp b/topicmodel.cpp index bbc2314..0c7541e 100644 --- a/topicmodel.cpp +++ b/topicmodel.cpp @@ -14,7 +14,12 @@ TopicModel::TopicModel(QObject *parent) : roles[TitleRole] = QByteArray("title"); roles[ContentRole] = QByteArray("content"); roles[IconRole] = QByteArray("icon"); - roles[PostIdRole] = QByteArray("postcId"); + roles[PostIdRole] = QByteArray("postId"); + roles[UserIdRole] = QByteArray("userId"); + roles[UserNameRole] = QByteArray("userName"); + roles[DateTimeRole] = QByteArray("dateTime"); + roles[HumanDateRole] = QByteArray("humanDate"); + roles[HumanTimeRole] = QByteArray("humanTime"); setRoleNames(roles); } @@ -72,22 +77,31 @@ int TopicModel::rowCount(const QModelIndex &parent) const 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(); } - switch (role) { + switch (role) { // Mind the lack of break statements case TitleRole: return _data[row].title; - break; case ContentRole: return _data[row].content; - break; case PostIdRole: return _data[row].post_id; - break; + case UserIdRole: + return _data[row].user_id; + case UserNameRole: + return _data[row].user_name; + case DateTimeRole: + return _data[row].time; + case HumanDateRole: + return _board->renderHumanDate(_data[row].time); + case HumanTimeRole: + return _board->renderHumanTime(_data[row].time); } return QVariant(); @@ -143,7 +157,7 @@ void TopicModel::fetchMore(const QModelIndex &parent) } } -QDateTime TopicModel::parseDateTime(const QVariant &v) +QDateTime TopicModel::parseDbDateTime(const QVariant &v) { QString s = v.toString(); return QDateTime::fromString(s, Qt::ISODate); @@ -171,10 +185,10 @@ QDateTime TopicModel::lastTopPostUpdate() query.bindValue(":topic_id", _topicId); if (query.exec()) { if (query.next()) { - return parseDateTime(query.value(0)); + return parseDbDateTime(query.value(0)); } } else { - qWarning() << "Could not fetch posts:" << query.lastError().text(); + qWarning() << "Could not load top posts:" << query.lastError().text(); } return QDateTime(); } @@ -185,7 +199,7 @@ QList TopicModel::loadPosts(int start, int end) const int rows = end - start + 1; QList posts; QSqlQuery query(_board->database()); - query.prepare("SELECT post_id, post_title, post_content, post_time, last_update_time FROM posts " + query.prepare("SELECT post_id, post_title, post_content, post_author_id, post_author_name, post_time, last_update_time FROM posts " "WHERE topic_id = :topic_id " "ORDER by post_time ASC " "LIMIT :start, :limit"); @@ -199,8 +213,10 @@ QList TopicModel::loadPosts(int start, int end) post.post_id = query.value(0).toInt(); post.title = query.value(1).toString(); post.content = query.value(2).toString(); - post.time = parseDateTime(query.value(3)); - post.last_update_time = parseDateTime(query.value(4)); + post.user_id = query.value(3).toInt(); + post.user_name = query.value(4).toString(); + post.time = parseDbDateTime(query.value(5)); + post.last_update_time = parseDbDateTime(query.value(6)); posts.append(post); } } else { -- cgit v1.2.3