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. 3
      src/httpconnection.cpp
  4. 6
      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 @@
#include "eventmanager.h" #include "eventmanager.h"
#include "bittorrent.h" #include "bittorrent.h"
#include "json.h"
#include <QDebug> #include <QDebug>
EventManager::EventManager(QObject *parent, bittorrent *BTSession) EventManager::EventManager(QObject *parent, bittorrent *BTSession)
@ -29,12 +28,8 @@ EventManager::EventManager(QObject *parent, bittorrent *BTSession)
{ {
} }
QVariant EventManager::getEventList() const { QList<QVariantMap> EventManager::getEventList() const {
QVariantList list; return event_list.values();
foreach(QVariantMap event, event_list.values()) {
list << QVariant(event);
}
return QVariant(list);
} }
void EventManager::addedTorrent(QTorrentHandle& h) void EventManager::addedTorrent(QTorrentHandle& h)

2
src/eventmanager.h

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

3
src/httpconnection.cpp

@ -161,8 +161,7 @@ void HttpConnection::respondNotFound()
void HttpConnection::respondJson() void HttpConnection::respondJson()
{ {
EventManager* manager = parent->eventManager(); EventManager* manager = parent->eventManager();
QVariant data = manager->getEventList(); QString string = json::toJson(manager->getEventList());
QString string = toJson(data);
generator.setStatusLine(200, "OK"); generator.setStatusLine(200, "OK");
generator.setContentTypeByExt("js"); generator.setContentTypeByExt("js");
generator.setMessage(string); generator.setMessage(string);

6
src/httpserver.cpp

@ -25,11 +25,11 @@
#include "bittorrent.h" #include "bittorrent.h"
#include <QTimer> #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(); base64 = QByteArray(":").toBase64();
connect(this, SIGNAL(newConnection()), this, SLOT(newHttpConnection())); connect(this, SIGNAL(newConnection()), this, SLOT(newHttpConnection()));
HttpServer::BTSession = BTSession; BTSession = _BTSession;
manager = new EventManager(this, BTSession); manager = new EventManager(this, BTSession);
//add torrents //add torrents
QStringList list = BTSession->getUnfinishedTorrents(); QStringList list = BTSession->getUnfinishedTorrents();
@ -99,7 +99,7 @@ bool HttpServer::isAuthorized(QByteArray auth) const
return (auth == base64); return (auth == base64);
} }
EventManager *HttpServer::eventManager() const EventManager* HttpServer::eventManager() const
{ {
return manager; return manager;
} }

103
src/json.cpp

@ -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 @@
#include <QVariant> #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 #endif

1
src/src.pro

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

Loading…
Cancel
Save