From 5ef8b38e55c1883224fe1f01f47aba45b7b42666 Mon Sep 17 00:00:00 2001 From: "Javier S. Pedro" Date: Mon, 1 Apr 2013 15:04:58 +0200 Subject: initial import --- fetchforumsaction.cpp | 107 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 107 insertions(+) create mode 100644 fetchforumsaction.cpp (limited to 'fetchforumsaction.cpp') diff --git a/fetchforumsaction.cpp b/fetchforumsaction.cpp new file mode 100644 index 0000000..8b3e4c8 --- /dev/null +++ b/fetchforumsaction.cpp @@ -0,0 +1,107 @@ +#include +#include +#include +#include + +#include "board.h" +#include "xmlrpcinterface.h" +#include "xmlrpcreply.h" +#include "fetchforumsaction.h" + +FetchForumsAction::FetchForumsAction(Board *board) : + Action(board) +{ +} + +void FetchForumsAction::execute() +{ + _call = _board->service()->asyncCall("get_forum"); + connect(_call, SIGNAL(finished(XmlRpcPendingCall*)), SLOT(handleFinishedCall())); +} + +void FetchForumsAction::handleFinishedCall() +{ + XmlRpcReply result(_call); + if (result.isValid()) { + int order = 0; + QList list = flattenForumList(result, &order); + QSqlDatabase db = _board->database(); + db.transaction(); + db.exec("DELETE FROM forums"); + if (db.lastError().isValid()) { + handleDatabaseError("truncating forums table", db.lastError()); + } + QSqlQuery query(db); + query.prepare("INSERT INTO forums (forum_id, forum_name, description, parent_id, logo_url, new_post, is_protected, is_subscribed, can_subscribe, url, sub_only, sort_index)" + "VALUES (:forum_id, :forum_name, :description, :parent_id, :logo_url, :new_post, :is_protected, :is_subscribed, :can_subscribe, :url, :sub_only, :sort_index)"); + + foreach (const QVariant& list_element, list) { + QVariantMap map = list_element.toMap(); + bool ok = false; + int forum_id = map["forum_id"].toInt(&ok); + if (!ok) { + qWarning() << "No forum_id in" << map; + continue; + } + int parent_id = map["parent_id"].toInt(&ok); + if (!ok) { + qWarning() << "No parent_id in" << map; + continue; + } + + query.bindValue(":forum_id", forum_id); + query.bindValue(":parent_id", parent_id); + query.bindValue(":forum_name", unencodeForumText(map["forum_name"])); + query.bindValue(":description", unencodeForumText(map["description"])); + query.bindValue(":logo_url", map["logo_url"].toString()); + query.bindValue(":new_post", map["new_post"].toBool() ? 1 : 0); + query.bindValue(":is_protected", map["is_protected"].toBool() ? 1 : 0); + query.bindValue(":is_subscribed", map["is_subscribed"].toBool() ? 1 : 0); + query.bindValue(":can_subscribe", map["can_subscribed"].toBool() ? 1 : 0); + query.bindValue(":url", map["url"].toString()); + query.bindValue(":sub_only", map["sub_only"].toBool() ? 1 : 0); + query.bindValue(":sort_index", map["sort_index"]); + + if (!query.exec()) { + qWarning() << "Failed to store forum info for:" << forum_id; + handleDatabaseError("storing forum info", query); + continue; + } + } + + _board->setConfig("last_forums_fetch", + QDateTime::currentDateTimeUtc().toString(Qt::ISODate)); + db.commit(); + _board->notifyForumsChanged(); + } else { + qWarning() << "Could not fetch board forums"; + // TODO emit error ... + } + emit finished(this); + _call->deleteLater(); +} + +QList FetchForumsAction::flattenForumList(const QVariantList &list, int *order) +{ + QList flattened; + foreach (const QVariant& list_element, list) { + QVariantMap map = list_element.toMap(); + map["sort_index"] = (*order)++; + QVariantMap::iterator child_key = map.find("child"); + if (child_key != map.end()) { + // There are children, so flatten them too. + QVariantList sublist = child_key.value().toList(); + map.erase(child_key); + flattened << map << flattenForumList(sublist, order); + } else { + flattened << map; + } + } + return flattened; +} + +QString FetchForumsAction::unencodeForumText(const QVariant &v) +{ + QByteArray ba = v.toByteArray(); + return QString::fromUtf8(ba.constData(), ba.length()); +} -- cgit v1.2.3