From e35d1007694cadcab0f66d262862434e1e08f1c7 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Tue, 24 Nov 2009 08:53:14 +0000 Subject: [PATCH] - Web UI: Display generation information in torrent properties --- src/eventmanager.cpp | 46 +++++++++++++++ src/eventmanager.h | 1 + src/httpconnection.cpp | 21 ++++++- src/httpconnection.h | 1 + src/json.h | 94 +++++++++++++++++-------------- src/webui/prop-general.html | 90 ++++++++++++++++++++++++++--- src/webui/scripts/client.js | 2 +- src/webui/scripts/dynamicTable.js | 8 +++ 8 files changed, 210 insertions(+), 53 deletions(-) diff --git a/src/eventmanager.cpp b/src/eventmanager.cpp index 489082578..6c71e0418 100644 --- a/src/eventmanager.cpp +++ b/src/eventmanager.cpp @@ -32,6 +32,7 @@ #include "eventmanager.h" #include "bittorrent.h" #include "misc.h" +#include "torrentpersistentdata.h" #include EventManager::EventManager(QObject *parent, Bittorrent *BTSession) @@ -43,6 +44,51 @@ QList EventManager::getEventList() const { return event_list.values(); } +QVariantMap EventManager::getPropGeneralInfo(QString hash) const { + QVariantMap data; + QTorrentHandle h = BTSession->getTorrentHandle(hash); + if(h.is_valid()) { + // Save path + data["save_path"] = TorrentPersistentData::getSavePath(hash); + // Creation date + data["creation_date"] = h.creation_date(); + // Comment + data["comment"] = h.comment(); + data["total_wasted"] = misc::friendlyUnit(h.total_failed_bytes()+h.total_redundant_bytes()); + data["total_uploaded"] = misc::friendlyUnit(h.all_time_upload()) + " ("+misc::friendlyUnit(h.total_payload_upload())+" "+tr("this session")+")"; + data["total_downloaded"] = misc::friendlyUnit(h.all_time_download()) + " ("+misc::friendlyUnit(h.total_payload_download())+" "+tr("this session")+")"; + if(h.upload_limit() <= 0) + data["up_limit"] = QString::fromUtf8("∞"); + else + data["up_limit"] = misc::friendlyUnit(h.upload_limit())+tr("/s", "/second (i.e. per second)"); + if(h.download_limit() <= 0) + data["dl_limit"] = QString::fromUtf8("∞"); + else + data["dl_limit"] = misc::friendlyUnit(h.download_limit())+tr("/s", "/second (i.e. per second)"); + QString elapsed_txt = misc::userFriendlyDuration(h.active_time()); + if(h.is_seed()) { + elapsed_txt += " ("+tr("Seeded for %1", "e.g. Seeded for 3m10s").arg(misc::userFriendlyDuration(h.seeding_time()))+")"; + } + data["time_elapsed"] = elapsed_txt; + data["nb_connections"] = QString::number(h.num_connections())+" ("+tr("%1 max", "e.g. 10 max").arg(QString::number(h.connections_limit()))+")"; + // Update ratio info + float ratio; + if(h.total_payload_download() == 0){ + if(h.total_payload_upload() == 0) + ratio = 1.; + else + ratio = 10.; // Max ratio + }else{ + ratio = (double)h.total_payload_upload()/(double)h.total_payload_download(); + if(ratio > 10.){ + ratio = 10.; + } + } + data["share_ratio"] = QString(QByteArray::number(ratio, 'f', 1)); + } + return data; +} + void EventManager::addedTorrent(QTorrentHandle& h) { modifiedTorrent(h); diff --git a/src/eventmanager.h b/src/eventmanager.h index 9d80b4e72..d431b492f 100644 --- a/src/eventmanager.h +++ b/src/eventmanager.h @@ -51,6 +51,7 @@ class EventManager : public QObject public: EventManager(QObject *parent, Bittorrent* BTSession); QList getEventList() const; + QVariantMap getPropGeneralInfo(QString hash) const; public slots: void addedTorrent(QTorrentHandle& h); diff --git a/src/httpconnection.cpp b/src/httpconnection.cpp index 7b710e50b..8a7d4b93a 100644 --- a/src/httpconnection.cpp +++ b/src/httpconnection.cpp @@ -147,7 +147,7 @@ void HttpConnection::respond() } if (list.size() == 0) list.append("index.html"); - if (list.size() == 2) + if (list.size() >= 2) { if (list[0] == "json") { @@ -156,6 +156,14 @@ void HttpConnection::respond() respondJson(); return; } + if(list.size() > 2) { + if(list[1] == "propertiesGeneral") { + qDebug("Web UI Asked for general properties data"); + QString hash = list[2]; + respondGenPropertiesJson(hash); + return; + } + } } if (list[0] == "command") { @@ -210,6 +218,17 @@ void HttpConnection::respondJson() write(); } +void HttpConnection::respondGenPropertiesJson(QString hash) { + qDebug("Torrent hash is %s", hash.toLocal8Bit().data()); + EventManager* manager = parent->eventManager(); + QString string = json::toJson(manager->getPropGeneralInfo(hash)); + qDebug("GenProperties JSON is %s", string.toLocal8Bit().data()); + generator.setStatusLine(200, "OK"); + generator.setContentTypeByExt("js"); + generator.setMessage(string); + write(); +} + void HttpConnection::respondCommand(QString command) { if(command == "download") diff --git a/src/httpconnection.h b/src/httpconnection.h index 61bb739fe..cc373328a 100644 --- a/src/httpconnection.h +++ b/src/httpconnection.h @@ -56,6 +56,7 @@ class HttpConnection : public QObject void write(); virtual void respond(); void respondJson(); + void respondGenPropertiesJson(QString hash); void respondCommand(QString command); void respondNotFound(); void processDownloadedFile(QString, QString); diff --git a/src/json.h b/src/json.h index 29d38637a..4af3f16f2 100644 --- a/src/json.h +++ b/src/json.h @@ -36,71 +36,79 @@ namespace json { - QString toJson(QVariant v) { - if (v.isNull()) - return "null"; - switch(v.type()) - { + 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(); + return v.value(); case QVariant::String: - { - QString s = v.value(); - QString result = "\""; - for(int i=0; i(); + 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(",")+"]"; + QString toJson(QVariantMap m) { + QStringList vlist; + foreach(QString key, m.keys()) { + vlist << toJson(key)+":"+toJson(m[key]); } + return "{"+vlist.join(",")+"}"; + } + + QString toJson(QList 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/webui/prop-general.html b/src/webui/prop-general.html index 40cec32c4..6dcc88b9f 100644 --- a/src/webui/prop-general.html +++ b/src/webui/prop-general.html @@ -1,26 +1,100 @@
_(Transfer) - - - + + +
_(Uploaded:)0 Kb
_(Downloaded:)0 Kb
_(Wasted:)0 Kb
_(Uploaded:)0 Kb_(UP limit:)xx_(Share ratio:)xx
_(Downloaded:)0 Kb_(DL limit:)xx_(Connections:)xx
_(Wasted:)0 Kb_(Time elapsed:)xx
_(Information) - - - + + +
_(Save path:)xxx
_(Created on:)xxx
_(Torrent hash:)xxx
_(Save path:)xxx
_(Created on:)xxx
_(Torrent hash:)xxx

_(Comment:)
-

-
\ No newline at end of file + + + + + \ No newline at end of file diff --git a/src/webui/scripts/client.js b/src/webui/scripts/client.js index d7e567897..f6800735c 100644 --- a/src/webui/scripts/client.js +++ b/src/webui/scripts/client.js @@ -160,7 +160,7 @@ window.addEvent('domready', function(){ } } waiting=false; - ajaxfn.delay(1000); + ajaxfn.delay(1500); } }).send(); } diff --git a/src/webui/scripts/dynamicTable.js b/src/webui/scripts/dynamicTable.js index 1003fb395..da6f8502b 100644 --- a/src/webui/scripts/dynamicTable.js +++ b/src/webui/scripts/dynamicTable.js @@ -43,6 +43,13 @@ var dynamicTable = new Class ({ this.priority_hidden = false; this.progressIndex = progressIndex; this.filter = 'all'; + this.current_hash = ''; + }, + + getCurrentTorrentHash: function() { + if(this.cur.length > 0) + return this.cur[0]; + return ''; }, altRow: function() @@ -200,6 +207,7 @@ var dynamicTable = new Class ({ temptr.addClass('selected'); } this.cur[0] = id; + // TODO: Warn Properties panel } } return false;