diff options
Diffstat (limited to 'fetchpostsaction.cpp')
-rw-r--r-- | fetchpostsaction.cpp | 98 |
1 files changed, 98 insertions, 0 deletions
diff --git a/fetchpostsaction.cpp b/fetchpostsaction.cpp new file mode 100644 index 0000000..16802fa --- /dev/null +++ b/fetchpostsaction.cpp @@ -0,0 +1,98 @@ +#include <QtCore/QDateTime> +#include <QtCore/QDebug> +#include <QtSql/QSqlDatabase> +#include <QtSql/QSqlQuery> + +#include "board.h" +#include "xmlrpcinterface.h" +#include "xmlrpcreply.h" +#include "fetchpostsaction.h" + +FetchPostsAction::FetchPostsAction(int topicId, int start, int end, Board *board) : + Action(board), _topicId(topicId), _start(start), _end(end) +{ +} + +bool FetchPostsAction::isSupersetOf(Action *action) const +{ + FetchPostsAction *other = qobject_cast<FetchPostsAction*>(action); + if (other) { + if (other->_topicId == _topicId) { + if (_start <= other->_start && _end >= other->_end) { + return true; + } + } + } + return false; +} + +void FetchPostsAction::execute() +{ + _call = _board->service()->asyncCall("get_thread", + QString::number(_topicId), _start, _end); + connect(_call, SIGNAL(finished(XmlRpcPendingCall*)), SLOT(handleFinishedCall())); +} + +void FetchPostsAction::handleFinishedCall() +{ + XmlRpcReply<QVariantMap> result(_call); + if (result.isValid()) { + QVariantMap map = result; + QVariantList posts = map["posts"].toList(); + QSqlDatabase db = _board->database(); + db.transaction(); + + QSqlQuery query(db); + query.prepare("INSERT OR REPLACE INTO posts (topic_id, post_id, post_title, post_content, post_author_id, post_author_name, can_edit, icon_url, post_time, last_update_time) " + "VALUES (:topic_id, :post_id, :post_title, :post_content, :post_author_id, :post_author_name, :can_edit, :icon_url, :post_time, :last_update_time)"); + + foreach (const QVariant& post_v, posts) { + QVariantMap post = post_v.toMap(); + bool ok = false; + int topic_id = post["topic_id"].toInt(&ok); + if (!ok) { + // Not fatal, just assume it's the one we requested + topic_id = _topicId; + } + int post_id = post["post_id"].toInt(&ok); + if (!ok) { + qWarning() << "No post_id in" << post; + continue; + } + + query.bindValue(":topic_id", topic_id); + query.bindValue(":post_id", post_id); + query.bindValue(":post_title", unencodePostText(post["post_title"])); + query.bindValue(":post_content", unencodePostText(post["post_content"])); + query.bindValue(":post_author_id", post["post_author_id"].toInt()); + query.bindValue(":post_author_name", unencodePostText(post["post_author_name"])); + query.bindValue(":can_edit", post["can_edit"].toBool() ? 1 : 0); + query.bindValue(":icon_url", post["icon_url"].toString()); + query.bindValue(":post_time", post["post_time"].toDateTime()); + query.bindValue(":last_update_time", QDateTime::currentDateTime()); + + if (!query.exec()) { + qWarning() << "Failed to store topic info for:" << topic_id; + handleDatabaseError("storing topic info", query); + continue; + } + } + + db.commit(); + if (posts.size() > 0) { + _board->notifyTopicPostsChanged(_topicId, + _start, _start + posts.size() - 1); + } + } else { + qWarning() << "Could not fetch posts"; + // TODO emit error ... + } + emit finished(this); + _call->deleteLater(); +} + +QString FetchPostsAction::unencodePostText(const QVariant &v) +{ + QByteArray ba = v.toByteArray(); + return QString::fromUtf8(ba.constData(), ba.length()); +} |