summaryrefslogtreecommitdiff
path: root/fetchtopicsaction.cpp
diff options
context:
space:
mode:
authorJavier S. Pedro <maemo@javispedro.com>2013-04-01 15:04:58 +0200
committerJavier S. Pedro <maemo@javispedro.com>2013-04-01 15:04:58 +0200
commit5ef8b38e55c1883224fe1f01f47aba45b7b42666 (patch)
tree67a873c6a7c5263d202793314c3b3a61543fbb40 /fetchtopicsaction.cpp
downloadtapasboard-5ef8b38e55c1883224fe1f01f47aba45b7b42666.tar.gz
tapasboard-5ef8b38e55c1883224fe1f01f47aba45b7b42666.zip
initial import
Diffstat (limited to 'fetchtopicsaction.cpp')
-rw-r--r--fetchtopicsaction.cpp88
1 files changed, 88 insertions, 0 deletions
diff --git a/fetchtopicsaction.cpp b/fetchtopicsaction.cpp
new file mode 100644
index 0000000..5661da8
--- /dev/null
+++ b/fetchtopicsaction.cpp
@@ -0,0 +1,88 @@
+#include <QtCore/QDateTime>
+#include <QtCore/QDebug>
+#include <QtSql/QSqlDatabase>
+#include <QtSql/QSqlQuery>
+
+#include "board.h"
+#include "xmlrpcinterface.h"
+#include "xmlrpcreply.h"
+#include "fetchtopicsaction.h"
+
+FetchTopicsAction::FetchTopicsAction(int forumId, int start, int end, Board *board) :
+ Action(board), _forumId(forumId), _start(start), _end(end)
+{
+}
+
+void FetchTopicsAction::execute()
+{
+ _call = _board->service()->asyncCall("get_topic",
+ QString::number(_forumId), _start, _end);
+ connect(_call, SIGNAL(finished(XmlRpcPendingCall*)), SLOT(handleFinishedCall()));
+}
+
+// forum_id INTEGER, topic_id INTEGER PRIMARY KEY, topic_title TEXT, topic_author_id INTEGER, topic_author_name TEXT, is_subscribed BOOL, is_closed BOOL, icon_url TEXT, last_reply_time TEXT, new_post BOOL
+
+void FetchTopicsAction::handleFinishedCall()
+{
+ XmlRpcReply<QVariantMap> result(_call);
+ if (result.isValid()) {
+ QVariantMap map = result;
+ QVariantList topics = map["topics"].toList();
+ QSqlDatabase db = _board->database();
+ db.transaction();
+
+ QSqlQuery query(db);
+ query.prepare("INSERT OR REPLACE INTO topics (forum_id, topic_id, topic_title, topic_author_id, topic_author_name, is_subscribed, is_closed, icon_url, last_reply_time, new_post, last_update_time) "
+ "VALUES (:forum_id, :topic_id, :topic_title, :topic_author_id, :topic_author_name, :is_subscribed, :is_closed, :icon_url, :last_reply_time, :new_post, :last_update_time)");
+
+ foreach (const QVariant& topic_v, topics) {
+ QVariantMap topic = topic_v.toMap();
+ bool ok = false;
+ int forum_id = topic["forum_id"].toInt(&ok);
+ if (!ok) {
+ qWarning() << "No forum_id in" << topic;
+ continue;
+ }
+ int topic_id = topic["topic_id"].toInt(&ok);
+ if (!ok) {
+ qWarning() << "No parent_id in" << topic;
+ continue;
+ }
+
+ query.bindValue(":forum_id", forum_id);
+ query.bindValue(":topic_id", topic_id);
+ query.bindValue(":topic_title", unencodeTopicText(topic["topic_title"]));
+ query.bindValue(":topic_author_id", topic["topic_author_id"].toInt());
+ query.bindValue(":topic_author_name", unencodeTopicText(topic["topic_author_name"]));
+ query.bindValue(":is_subscribed", topic["is_subscribed"].toBool() ? 1 : 0);
+ query.bindValue(":is_closed", topic["is_closed"].toBool() ? 1 : 0);
+ query.bindValue(":icon_url", topic["icon_url"].toString());
+ query.bindValue(":last_reply_time", topic["last_reply_time"].toDateTime());
+ query.bindValue(":new_post", topic["new_post"].toBool() ? 1 : 0);
+ 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 (topics.size() > 0) {
+ _board->notifyForumTopicsChanged(_forumId,
+ _start, _start + topics.size() - 1);
+ }
+ } else {
+ qWarning() << "Could not fetch topics";
+ // TODO emit error ...
+ }
+ emit finished(this);
+ _call->deleteLater();
+}
+
+QString FetchTopicsAction::unencodeTopicText(const QVariant &v)
+{
+ QByteArray ba = v.toByteArray();
+ return QString::fromUtf8(ba.constData(), ba.length());
+}