mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2024-12-02 23:46:18 +00:00
- Simplified and optimized json code
This commit is contained in:
parent
0d078f660a
commit
b8c79e0b5f
@ -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)
|
||||
{
|
||||
}
|
||||
|
||||
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)
|
||||
|
@ -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);
|
||||
|
@ -161,8 +161,7 @@ void HttpConnection::respondNotFound()
|
||||
void HttpConnection::respondJson()
|
||||
{
|
||||
EventManager* manager = parent->eventManager();
|
||||
QVariant data = manager->getEventList();
|
||||
QString string = toJson(data);
|
||||
QString string = json::toJson(manager->getEventList());
|
||||
generator.setStatusLine(200, "OK");
|
||||
generator.setContentTypeByExt("js");
|
||||
generator.setMessage(string);
|
||||
|
@ -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();
|
||||
@ -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
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
69
src/json.h
@ -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
|
||||
|
@ -224,7 +224,6 @@ SOURCES += GUI.cpp \
|
||||
httpconnection.cpp \
|
||||
httprequestparser.cpp \
|
||||
httpresponsegenerator.cpp \
|
||||
json.cpp \
|
||||
eventmanager.cpp \
|
||||
SearchTab.cpp \
|
||||
ico.cpp
|
||||
|
Loading…
Reference in New Issue
Block a user