Rewrite code for returning information about torrents as JSON (Web UI)

- The new code is simpler, cleaner and more efficient
This commit is contained in:
Christophe Dumez
2012-05-26 20:42:44 +03:00
parent fc4989d738
commit 9a964d871d
19 changed files with 825 additions and 412 deletions

View File

@@ -1,6 +1,6 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2006 Ishan Arora and Christophe Dumez
* Copyright (C) 2006-2012 Ishan Arora and Christophe Dumez
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
@@ -36,150 +36,11 @@
namespace json {
QString toJson(const 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::StringList:
case QVariant::List: {
QStringList strList;
foreach (const QVariant &var, v.toList()) {
strList << toJson(var);
}
return "["+strList.join(",")+"]";
}
case QVariant::String: {
QString s = v.value<QString>();
QString result = "\"";
for (int i=0; i<s.size(); ++i) {
const 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:
qDebug("Unknown QVariantType: %d", (int)v.type());
return "undefined";
}
}
QString toJson(const QVariant& v);
QString toJson(const QVariantMap& m); // TODO: Remove
QString toJson(const QList<QVariantMap>& v); // TODO: Remove
QVariantMap fromJson(const QString& json);
QString toJson(const QVariantMap& m) {
QStringList vlist;
QVariantMap::ConstIterator it;
for (it = m.constBegin(); it != m.constEnd(); it++) {
vlist << toJson(it.key())+":"+toJson(it.value());
}
return "{"+vlist.join(",")+"}";
}
QVariantMap fromJson(const QString& json) {
qDebug("JSON is %s", qPrintable(json));
QVariantMap m;
if (json.startsWith("{") && json.endsWith("}")) {
QStringList couples;
QString tmp = "";
bool in_list = false;
foreach (const QChar &c, json.mid(1, json.length()-2)) {
if (c == ',' && !in_list) {
couples << tmp;
tmp = "";
} else {
if (c == '[')
in_list = true;
else if (c == ']')
in_list = false;
tmp += c;
}
}
if (!tmp.isEmpty()) couples << tmp;
foreach (const QString &couple, couples) {
QStringList parts = couple.split(":");
if (parts.size() != 2) continue;
QString key = parts.first();
if (key.startsWith("\"") && key.endsWith("\"")) {
key = key.mid(1, key.length()-2);
}
QString value_str = parts.last();
QVariant value;
if (value_str.startsWith("[") && value_str.endsWith("]")) {
value_str = value_str.mid(1, value_str.length()-2);
QStringList list_elems = value_str.split(",", QString::SkipEmptyParts);
QVariantList varlist;
foreach (const QString &list_val, list_elems) {
if (list_val.startsWith("\"") && list_val.endsWith("\"")) {
varlist << list_val.mid(1, list_val.length()-2).replace("\\n", "\n");
} else {
varlist << list_val.toInt();
}
}
value = varlist;
} else {
if (value_str.startsWith("\"") && value_str.endsWith("\"")) {
value_str = value_str.mid(1, value_str.length()-2).replace("\\n", "\n");
value = value_str;
} else {
if (value_str.compare("false", Qt::CaseInsensitive) == 0)
value = false;
else if (value_str.compare("true", Qt::CaseInsensitive) == 0)
value = true;
else
value = value_str.toInt();
}
}
m.insert(key, value);
qDebug("%s:%s", key.toLocal8Bit().data(), value_str.toLocal8Bit().data());
}
}
return m;
}
QString toJson(const QList<QVariantMap>& v) {
QStringList res;
foreach (QVariantMap m, v) {
QStringList vlist;
QVariantMap::ConstIterator it;
for (it = m.constBegin(); it != m.constEnd(); it++) {
vlist << toJson(it.key())+":"+toJson(it.value());
}
res << "{"+vlist.join(",")+"}";
}
return "["+res.join(",")+"]";
}
}
} // namespace json
#endif