Browse Source

- Simplified and optimized json code

adaptive-webui-19844
Christophe Dumez 16 years ago
parent
commit
b8c79e0b5f
  1. 9
      src/eventmanager.cpp
  2. 2
      src/eventmanager.h
  3. 5
      src/httpconnection.cpp
  4. 14
      src/httpserver.cpp
  5. 103
      src/json.cpp
  6. 69
      src/json.h
  7. 1
      src/src.pro

9
src/eventmanager.cpp

@ -21,7 +21,6 @@ @@ -21,7 +21,6 @@
#include "eventmanager.h"
#include "bittorrent.h"
#include "json.h"
#include <QDebug>
EventManager::EventManager(QObject *parent, bittorrent *BTSession)
@ -29,12 +28,8 @@ 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<QVariantMap> EventManager::getEventList() const {
return event_list.values();
}
void EventManager::addedTorrent(QTorrentHandle& h)

2
src/eventmanager.h

@ -40,7 +40,7 @@ class EventManager : public QObject @@ -40,7 +40,7 @@ class EventManager : public QObject
public:
EventManager(QObject *parent, bittorrent* BTSession);
QVariant getEventList() const;
QList<QVariantMap> getEventList() const;
public slots:
void addedTorrent(QTorrentHandle& h);

5
src/httpconnection.cpp

@ -160,9 +160,8 @@ void HttpConnection::respondNotFound() @@ -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);

14
src/httpserver.cpp

@ -25,11 +25,11 @@ @@ -25,11 +25,11 @@
#include "bittorrent.h"
#include <QTimer>
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 @@ -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 @@ -99,7 +99,7 @@ bool HttpServer::isAuthorized(QByteArray auth) const
return (auth == base64);
}
EventManager *HttpServer::eventManager() const
EventManager* HttpServer::eventManager() const
{
return manager;
}

103
src/json.cpp

@ -1,103 +0,0 @@ @@ -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<QString>();
case QVariant::String:
{
QString s = v.value<QString>();
result = "\"";
for(int i=0; i<s.size(); i++)
{
QChar ch = s[i];
switch(ch.toAscii())
{
case '\b':
result += "\\b";
break;
case '\f':
result += "\\f";
break;
case '\n':
result += "\\n";
break;
case '\r':
result += "\\r";
break;
case '\t':
result += "\\t";
break;
case '\"':
case '\'':
case '\\':
case '&':
result += '\\';
case '\0':
default:
result += ch;
}
}
result += "\"";
return result;
}
case QVariant::List:
{
result = "[";
QListIterator<QVariant> it(v.value<QVariantList>());
while (it.hasNext())
result += toJson(it.next()) + ",";
if(result.size() > 1)
result.chop(1);
result += "]";
return result;
}
case QVariant::Map:
{
result = "{";
QMapIterator<QString, QVariant> it(v.value<QVariantMap>());
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";
}
}

69
src/json.h

@ -24,6 +24,73 @@ @@ -24,6 +24,73 @@
#include <QVariant>
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<QString>();
case QVariant::String:
{
QString s = v.value<QString>();
QString result = "\"";
for(int i=0; i<s.size(); i++)
{
QChar ch = s[i];
switch(ch.toAscii())
{
case '\b':
result += "\\b";
break;
case '\f':
result += "\\f";
break;
case '\n':
result += "\\n";
break;
case '\r':
result += "\\r";
break;
case '\t':
result += "\\t";
break;
case '\"':
case '\'':
case '\\':
case '&':
result += '\\';
case '\0':
default:
result += ch;
}
}
result += "\"";
return result;
}
default:
return "undefined";
}
}
QString toJson(QList<QVariantMap> 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

1
src/src.pro

@ -224,7 +224,6 @@ SOURCES += GUI.cpp \ @@ -224,7 +224,6 @@ SOURCES += GUI.cpp \
httpconnection.cpp \
httprequestparser.cpp \
httpresponsegenerator.cpp \
json.cpp \
eventmanager.cpp \
SearchTab.cpp \
ico.cpp

Loading…
Cancel
Save