diff options
author | Javier S. Pedro <maemo@javispedro.com> | 2013-04-01 15:04:58 +0200 |
---|---|---|
committer | Javier S. Pedro <maemo@javispedro.com> | 2013-04-01 15:04:58 +0200 |
commit | 5ef8b38e55c1883224fe1f01f47aba45b7b42666 (patch) | |
tree | 67a873c6a7c5263d202793314c3b3a61543fbb40 /fetchforumsaction.cpp | |
download | tapasboard-5ef8b38e55c1883224fe1f01f47aba45b7b42666.tar.gz tapasboard-5ef8b38e55c1883224fe1f01f47aba45b7b42666.zip |
initial import
Diffstat (limited to 'fetchforumsaction.cpp')
-rw-r--r-- | fetchforumsaction.cpp | 107 |
1 files changed, 107 insertions, 0 deletions
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 <QtCore/QDateTime> +#include <QtCore/QDebug> +#include <QtSql/QSqlDatabase> +#include <QtSql/QSqlQuery> + +#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<QVariantList> result(_call); + if (result.isValid()) { + int order = 0; + QList<QVariantMap> 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<QVariantMap> FetchForumsAction::flattenForumList(const QVariantList &list, int *order) +{ + QList<QVariantMap> 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()); +} |