From b8c79e0b5f99be908218af312abef2a2b3014d0d Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 25 Dec 2008 14:33:37 +0000 Subject: [PATCH] - Simplified and optimized json code --- src/eventmanager.cpp | 9 +--- src/eventmanager.h | 2 +- src/httpconnection.cpp | 5 +- src/httpserver.cpp | 14 +++--- src/json.cpp | 103 ----------------------------------------- src/json.h | 69 ++++++++++++++++++++++++++- src/src.pro | 1 - 7 files changed, 80 insertions(+), 123 deletions(-) delete mode 100644 src/json.cpp diff --git a/src/eventmanager.cpp b/src/eventmanager.cpp index 09c4b378d..1d564b1f9 100644 --- a/src/eventmanager.cpp +++ b/src/eventmanager.cpp @@ -21,7 +21,6 @@ #include "eventmanager.h" #include "bittorrent.h" -#include "json.h" #include EventManager::EventManager(QObject *parent, bittorrent *BTSession) @@ -29,12 +28,8 @@ EventManager::EventManager(QObject *parent, bittorrent *BTSession) { } -QVariant EventManager::getEventList() const { - QVariantList list; - foreach(QVariantMap event, event_list.values()) { - list << QVariant(event); - } - return QVariant(list); +QList EventManager::getEventList() const { + return event_list.values(); } void EventManager::addedTorrent(QTorrentHandle& h) diff --git a/src/eventmanager.h b/src/eventmanager.h index edd40ad6b..70a65c988 100644 --- a/src/eventmanager.h +++ b/src/eventmanager.h @@ -40,7 +40,7 @@ class EventManager : public QObject public: EventManager(QObject *parent, bittorrent* BTSession); - QVariant getEventList() const; + QList getEventList() const; public slots: void addedTorrent(QTorrentHandle& h); diff --git a/src/httpconnection.cpp b/src/httpconnection.cpp index 074a18dd2..558faae47 100644 --- a/src/httpconnection.cpp +++ b/src/httpconnection.cpp @@ -160,9 +160,8 @@ void HttpConnection::respondNotFound() void HttpConnection::respondJson() { - EventManager* manager = parent->eventManager(); - QVariant data = manager->getEventList(); - QString string = toJson(data); + EventManager* manager = parent->eventManager(); + QString string = json::toJson(manager->getEventList()); generator.setStatusLine(200, "OK"); generator.setContentTypeByExt("js"); generator.setMessage(string); diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 4e68fbe94..74a63d1f6 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -25,11 +25,11 @@ #include "bittorrent.h" #include -HttpServer::HttpServer(bittorrent *BTSession, int msec, QObject* parent) : QTcpServer(parent) +HttpServer::HttpServer(bittorrent *_BTSession, int msec, QObject* parent) : QTcpServer(parent) { base64 = QByteArray(":").toBase64(); connect(this, SIGNAL(newConnection()), this, SLOT(newHttpConnection())); - HttpServer::BTSession = BTSession; + BTSession = _BTSession; manager = new EventManager(this, BTSession); //add torrents QStringList list = BTSession->getUnfinishedTorrents(); @@ -37,11 +37,11 @@ HttpServer::HttpServer(bittorrent *BTSession, int msec, QObject* parent) : QTcpS QTorrentHandle h = BTSession->getTorrentHandle(hash); if(h.is_valid()) manager->addedTorrent(h); } - list = BTSession->getFinishedTorrents(); + list = BTSession->getFinishedTorrents(); foreach(QString hash, list) { - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(h.is_valid()) manager->addedTorrent(h); - } + QTorrentHandle h = BTSession->getTorrentHandle(hash); + if(h.is_valid()) manager->addedTorrent(h); + } //connect BTSession to manager connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), manager, SLOT(addedTorrent(QTorrentHandle&))); connect(BTSession, SIGNAL(deletedTorrent(QString)), manager, SLOT(deletedTorrent(QString))); @@ -99,7 +99,7 @@ bool HttpServer::isAuthorized(QByteArray auth) const return (auth == base64); } -EventManager *HttpServer::eventManager() const +EventManager* HttpServer::eventManager() const { return manager; } diff --git a/src/json.cpp b/src/json.cpp deleted file mode 100644 index 1aad2fd18..000000000 --- a/src/json.cpp +++ /dev/null @@ -1,103 +0,0 @@ -/* - * Copyright (C) 2007 by Ishan Arora - * ishanarora@gmail.com - * - * This program is free software; you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 2 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program; if not, write to the - * Free Software Foundation, Inc., - * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. - */ - - -#include "json.h" - -QString toJson(QVariant v) -{ - if (v.isNull()) - return "null"; - QString result; - switch(v.type()) - { - case QVariant::Bool: - case QVariant::Double: - case QVariant::Int: - case QVariant::LongLong: - case QVariant::UInt: - case QVariant::ULongLong: - return v.value(); - case QVariant::String: - { - QString s = v.value(); - result = "\""; - for(int i=0; i it(v.value()); - while (it.hasNext()) - result += toJson(it.next()) + ","; - if(result.size() > 1) - result.chop(1); - result += "]"; - return result; - } - case QVariant::Map: - { - result = "{"; - QMapIterator it(v.value()); - while (it.hasNext()) { - it.next(); - if(it.value().isValid()) - result += toJson(QVariant(it.key())) + ":" + toJson(it.value()) + ","; - } - if(result.size() > 1) - result.chop(1); - result += "}"; - return result; - } - default: - return "undefined"; - } -} diff --git a/src/json.h b/src/json.h index e89c9f289..eb1e322ef 100644 --- a/src/json.h +++ b/src/json.h @@ -24,6 +24,73 @@ #include -QString toJson(QVariant v); +namespace json { + + QString toJson(QVariant v) { + if (v.isNull()) + return "null"; + switch(v.type()) + { + case QVariant::Bool: + case QVariant::Double: + case QVariant::Int: + case QVariant::LongLong: + case QVariant::UInt: + case QVariant::ULongLong: + return v.value(); + case QVariant::String: + { + QString s = v.value(); + QString result = "\""; + for(int i=0; i v) { + QStringList res; + foreach(QVariantMap m, v) { + QStringList vlist; + foreach(QString key, m.keys()) { + vlist << toJson(key)+":"+toJson(m[key]); + } + res << "{"+vlist.join(",")+"}"; + } + return "["+res.join(",")+"]"; + } +} #endif diff --git a/src/src.pro b/src/src.pro index bb01e6a95..b185cc1ca 100644 --- a/src/src.pro +++ b/src/src.pro @@ -224,7 +224,6 @@ SOURCES += GUI.cpp \ httpconnection.cpp \ httprequestparser.cpp \ httpresponsegenerator.cpp \ - json.cpp \ eventmanager.cpp \ SearchTab.cpp \ ico.cpp