Browse Source

Merge pull request #3197 from ngosang/webui_websources

[Web UI] Add Web Seeds (HTTP Sources) tab
adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
a8df699441
  1. 110
      src/webui/btjson.cpp
  2. 1
      src/webui/btjson.h
  3. 7
      src/webui/webapplication.cpp
  4. 1
      src/webui/webapplication.h
  5. 1
      src/webui/webui.qrc
  6. 2
      src/webui/www/public/css/style.css
  7. 1
      src/webui/www/public/properties.html
  8. 13
      src/webui/www/public/properties_content.html
  9. 19
      src/webui/www/public/scripts/client.js
  10. 5
      src/webui/www/public/scripts/prop-files.js
  11. 3
      src/webui/www/public/scripts/prop-general.js
  12. 5
      src/webui/www/public/scripts/prop-trackers.js
  13. 109
      src/webui/www/public/scripts/prop-webseeds.js

110
src/webui/btjson.cpp

@ -110,6 +110,9 @@ static const char KEY_TRACKER_STATUS[] = "status"; @@ -110,6 +110,9 @@ static const char KEY_TRACKER_STATUS[] = "status";
static const char KEY_TRACKER_MSG[] = "msg";
static const char KEY_TRACKER_PEERS[] = "num_peers";
// Web seed keys
static const char KEY_WEBSEED_URL[] = "url";
// Torrent keys (Properties)
static const char KEY_PROP_SAVE_PATH[] = "save_path";
static const char KEY_PROP_CREATION_DATE[] = "creation_date";
@ -239,15 +242,15 @@ private: @@ -239,15 +242,15 @@ private:
QByteArray btjson::getTorrents(QString filter, QString label,
QString sortedColumn, bool reverse, int limit, int offset)
{
QVariantList torrent_list;
QVariantList torrentList;
TorrentFilter torrentFilter(filter, TorrentFilter::AnyHash, label);
foreach (BitTorrent::TorrentHandle *const torrent, BitTorrent::Session::instance()->torrents()) {
if (torrentFilter.match(torrent))
torrent_list.append(toMap(torrent));
torrentList.append(toMap(torrent));
}
std::sort(torrent_list.begin(), torrent_list.end(), QTorrentCompare(sortedColumn, reverse));
int size = torrent_list.size();
std::sort(torrentList.begin(), torrentList.end(), QTorrentCompare(sortedColumn, reverse));
int size = torrentList.size();
// normalize offset
if (offset < 0)
offset = size + offset;
@ -258,9 +261,9 @@ QByteArray btjson::getTorrents(QString filter, QString label, @@ -258,9 +261,9 @@ QByteArray btjson::getTorrents(QString filter, QString label,
limit = -1; // unlimited
if ((limit > 0) || (offset > 0))
return json::toJson(torrent_list.mid(offset, limit));
return json::toJson(torrentList.mid(offset, limit));
else
return json::toJson(torrent_list);
return json::toJson(torrentList);
}
/**
@ -344,7 +347,7 @@ QByteArray btjson::getSyncMainData(int acceptedResponseId, QVariantMap &lastData @@ -344,7 +347,7 @@ QByteArray btjson::getSyncMainData(int acceptedResponseId, QVariantMap &lastData
*/
QByteArray btjson::getTrackersForTorrent(const QString& hash)
{
CACHED_VARIABLE_FOR_HASH(QVariantList, tracker_list, CACHE_DURATION_MS, hash);
CACHED_VARIABLE_FOR_HASH(QVariantList, trackerList, CACHE_DURATION_MS, hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) {
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
@ -353,8 +356,8 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash) @@ -353,8 +356,8 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash)
QHash<QString, BitTorrent::TrackerInfo> trackers_data = torrent->trackerInfos();
foreach (const BitTorrent::TrackerEntry &tracker, torrent->trackers()) {
QVariantMap tracker_dict;
tracker_dict[KEY_TRACKER_URL] = tracker.url();
QVariantMap trackerDict;
trackerDict[KEY_TRACKER_URL] = tracker.url();
const BitTorrent::TrackerInfo data = trackers_data.value(tracker.url());
QString status;
switch (tracker.status()) {
@ -367,14 +370,39 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash) @@ -367,14 +370,39 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash)
case BitTorrent::TrackerEntry::NotWorking:
status = tr("Not working"); break;
}
tracker_dict[KEY_TRACKER_STATUS] = status;
tracker_dict[KEY_TRACKER_PEERS] = data.numPeers;
tracker_dict[KEY_TRACKER_MSG] = data.lastMessage.trimmed();
trackerDict[KEY_TRACKER_STATUS] = status;
trackerDict[KEY_TRACKER_PEERS] = data.numPeers;
trackerDict[KEY_TRACKER_MSG] = data.lastMessage.trimmed();
trackerList.append(trackerDict);
}
return json::toJson(trackerList);
}
/**
* Returns the web seeds for a torrent in JSON format.
*
* The return value is a JSON-formatted list of dictionaries.
* The dictionary keys are:
* - "url": Web seed URL
*/
QByteArray btjson::getWebSeedsForTorrent(const QString& hash)
{
CACHED_VARIABLE_FOR_HASH(QVariantList, webSeedList, CACHE_DURATION_MS, hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) {
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
return QByteArray();
}
tracker_list.append(tracker_dict);
foreach (const QUrl &webseed, torrent->urlSeeds()) {
QVariantMap webSeedDict;
webSeedDict[KEY_WEBSEED_URL] = webseed.toString();
webSeedList.append(webSeedDict);
}
return json::toJson(tracker_list);
return json::toJson(webSeedList);
}
/**
@ -401,7 +429,7 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash) @@ -401,7 +429,7 @@ QByteArray btjson::getTrackersForTorrent(const QString& hash)
*/
QByteArray btjson::getPropertiesForTorrent(const QString& hash)
{
CACHED_VARIABLE_FOR_HASH(QVariantMap, data, CACHE_DURATION_MS, hash);
CACHED_VARIABLE_FOR_HASH(QVariantMap, dataDict, CACHE_DURATION_MS, hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) {
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
@ -411,25 +439,25 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash) @@ -411,25 +439,25 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash)
if (!torrent->hasMetadata())
return QByteArray();
data[KEY_PROP_SAVE_PATH] = Utils::Fs::toNativePath(torrent->savePath());
data[KEY_PROP_CREATION_DATE] = torrent->creationDate().toTime_t();
data[KEY_PROP_PIECE_SIZE] = torrent->pieceLength();
data[KEY_PROP_COMMENT] = torrent->comment();
data[KEY_PROP_WASTED] = torrent->wastedSize();
data[KEY_PROP_UPLOADED] = torrent->totalUpload();
data[KEY_PROP_UPLOADED_SESSION] = torrent->totalPayloadUpload();
data[KEY_PROP_DOWNLOADED] = torrent->totalDownload();
data[KEY_PROP_DOWNLOADED_SESSION] = torrent->totalPayloadDownload();
data[KEY_PROP_UP_LIMIT] = torrent->uploadLimit() <= 0 ? -1 : torrent->uploadLimit();
data[KEY_PROP_DL_LIMIT] = torrent->downloadLimit() <= 0 ? -1 : torrent->downloadLimit();
data[KEY_PROP_TIME_ELAPSED] = torrent->activeTime();
data[KEY_PROP_SEEDING_TIME] = torrent->seedingTime();
data[KEY_PROP_CONNECT_COUNT] = torrent->connectionsCount();
data[KEY_PROP_CONNECT_COUNT_LIMIT] = torrent->connectionsLimit();
dataDict[KEY_PROP_SAVE_PATH] = Utils::Fs::toNativePath(torrent->savePath());
dataDict[KEY_PROP_CREATION_DATE] = torrent->creationDate().toTime_t();
dataDict[KEY_PROP_PIECE_SIZE] = torrent->pieceLength();
dataDict[KEY_PROP_COMMENT] = torrent->comment();
dataDict[KEY_PROP_WASTED] = torrent->wastedSize();
dataDict[KEY_PROP_UPLOADED] = torrent->totalUpload();
dataDict[KEY_PROP_UPLOADED_SESSION] = torrent->totalPayloadUpload();
dataDict[KEY_PROP_DOWNLOADED] = torrent->totalDownload();
dataDict[KEY_PROP_DOWNLOADED_SESSION] = torrent->totalPayloadDownload();
dataDict[KEY_PROP_UP_LIMIT] = torrent->uploadLimit() <= 0 ? -1 : torrent->uploadLimit();
dataDict[KEY_PROP_DL_LIMIT] = torrent->downloadLimit() <= 0 ? -1 : torrent->downloadLimit();
dataDict[KEY_PROP_TIME_ELAPSED] = torrent->activeTime();
dataDict[KEY_PROP_SEEDING_TIME] = torrent->seedingTime();
dataDict[KEY_PROP_CONNECT_COUNT] = torrent->connectionsCount();
dataDict[KEY_PROP_CONNECT_COUNT_LIMIT] = torrent->connectionsLimit();
const qreal ratio = torrent->realRatio();
data[KEY_PROP_RATIO] = ratio > BitTorrent::TorrentHandle::MAX_RATIO ? -1 : ratio;
dataDict[KEY_PROP_RATIO] = ratio > BitTorrent::TorrentHandle::MAX_RATIO ? -1 : ratio;
return json::toJson(data);
return json::toJson(dataDict);
}
/**
@ -445,7 +473,7 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash) @@ -445,7 +473,7 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash)
*/
QByteArray btjson::getFilesForTorrent(const QString& hash)
{
CACHED_VARIABLE_FOR_HASH(QVariantList, file_list, CACHE_DURATION_MS, hash);
CACHED_VARIABLE_FOR_HASH(QVariantList, fileList, CACHE_DURATION_MS, hash);
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(hash);
if (!torrent) {
qWarning() << Q_FUNC_INFO << "Invalid torrent " << qPrintable(hash);
@ -458,22 +486,22 @@ QByteArray btjson::getFilesForTorrent(const QString& hash) @@ -458,22 +486,22 @@ QByteArray btjson::getFilesForTorrent(const QString& hash)
const QVector<int> priorities = torrent->filePriorities();
QVector<qreal> fp = torrent->filesProgress();
for (int i = 0; i < torrent->filesCount(); ++i) {
QVariantMap file_dict;
QVariantMap fileDict;
QString fileName = torrent->filePath(i);
if (fileName.endsWith(".!qB", Qt::CaseInsensitive))
fileName.chop(4);
file_dict[KEY_FILE_NAME] = Utils::Fs::toNativePath(fileName);
fileDict[KEY_FILE_NAME] = Utils::Fs::toNativePath(fileName);
const qlonglong size = torrent->fileSize(i);
file_dict[KEY_FILE_SIZE] = size;
file_dict[KEY_FILE_PROGRESS] = fp[i];
file_dict[KEY_FILE_PRIORITY] = priorities[i];
fileDict[KEY_FILE_SIZE] = size;
fileDict[KEY_FILE_PROGRESS] = fp[i];
fileDict[KEY_FILE_PRIORITY] = priorities[i];
if (i == 0)
file_dict[KEY_FILE_IS_SEED] = torrent->isSeed();
fileDict[KEY_FILE_IS_SEED] = torrent->isSeed();
file_list.append(file_dict);
fileList.append(fileDict);
}
return json::toJson(file_list);
return json::toJson(fileList);
}
/**

1
src/webui/btjson.h

@ -47,6 +47,7 @@ public: @@ -47,6 +47,7 @@ public:
QString sortedColumn = "name", bool reverse = false, int limit = 0, int offset = 0);
static QByteArray getSyncMainData(int acceptedResponseId, QVariantMap &lastData, QVariantMap &lastAcceptedData);
static QByteArray getTrackersForTorrent(const QString& hash);
static QByteArray getWebSeedsForTorrent(const QString& hash);
static QByteArray getPropertiesForTorrent(const QString& hash);
static QByteArray getFilesForTorrent(const QString& hash);
static QByteArray getTransferInfo();

7
src/webui/webapplication.cpp

@ -76,6 +76,7 @@ QMap<QString, QMap<QString, WebApplication::Action> > WebApplication::initialize @@ -76,6 +76,7 @@ QMap<QString, QMap<QString, WebApplication::Action> > WebApplication::initialize
ADD_ACTION(query, transferInfo);
ADD_ACTION(query, propertiesGeneral);
ADD_ACTION(query, propertiesTrackers);
ADD_ACTION(query, propertiesWebSeeds);
ADD_ACTION(query, propertiesFiles);
ADD_ACTION(sync, maindata);
ADD_ACTION(command, shutdown);
@ -249,6 +250,12 @@ void WebApplication::action_query_propertiesTrackers() @@ -249,6 +250,12 @@ void WebApplication::action_query_propertiesTrackers()
print(btjson::getTrackersForTorrent(args_.front()), Http::CONTENT_TYPE_JS);
}
void WebApplication::action_query_propertiesWebSeeds()
{
CHECK_URI(1);
print(btjson::getWebSeedsForTorrent(args_.front()), Http::CONTENT_TYPE_JS);
}
void WebApplication::action_query_propertiesFiles()
{
CHECK_URI(1);

1
src/webui/webapplication.h

@ -52,6 +52,7 @@ private: @@ -52,6 +52,7 @@ private:
void action_query_transferInfo();
void action_query_propertiesGeneral();
void action_query_propertiesTrackers();
void action_query_propertiesWebSeeds();
void action_query_propertiesFiles();
void action_sync_maindata();
void action_command_shutdown();

1
src/webui/webui.qrc

@ -33,6 +33,7 @@ @@ -33,6 +33,7 @@
<file>www/public/properties_content.html</file>
<file>www/public/scripts/prop-general.js</file>
<file>www/public/scripts/prop-trackers.js</file>
<file>www/public/scripts/prop-webseeds.js</file>
<file>www/public/scripts/prop-files.js</file>
<file>www/public/transferlist.html</file>
<file>www/public/upload.html</file>

2
src/webui/www/public/css/style.css

@ -362,7 +362,7 @@ ul.filterList li:hover a { @@ -362,7 +362,7 @@ ul.filterList li:hover a {
line-height: 20px;
}
#trackersTable {
#trackersTable, #webseedsTable {
line-height: 25px;
}

1
src/webui/www/public/properties.html

@ -2,6 +2,7 @@ @@ -2,6 +2,7 @@
<ul id="propertiesTabs" class="tab-menu">
<li id="PropGeneralLink" class="selected"><a>QBT_TR(General)QBT_TR</a></li>
<li id="PropTrackersLink"><a>QBT_TR(Trackers)QBT_TR</a></li>
<li id="PropWebSeedsLink"><a>QBT_TR(HTTP Sources)QBT_TR</a></li>
<li id="PropFilesLink"><a>QBT_TR(Content)QBT_TR</a></li>
</ul>
<div class="clear"></div>

13
src/webui/www/public/properties_content.html

@ -38,6 +38,19 @@ @@ -38,6 +38,19 @@
</div>
</div>
<div id="prop_webseeds" class="invisible">
<div id="webseeds">
<table class="torrentTable" cellpadding="0" cellspacing="0" style="width: 100%">
<thead>
<tr>
<th>QBT_TR(URL)QBT_TR</th>
</tr>
</thead>
<tbody id="webseedsTable"></tbody>
</table>
</div>
</div>
<div id="prop_files" class="invisible">
<div id="torrentFiles">
<table class="torrentTable" cellpadding="0" cellspacing="0" style="width: 100%">

19
src/webui/www/public/scripts/client.js

@ -344,7 +344,7 @@ window.addEvent('load', function () { @@ -344,7 +344,7 @@ window.addEvent('load', function () {
contentURL : 'properties_content.html',
require : {
css : ['css/Tabs.css'],
js : ['scripts/prop-general.js', 'scripts/prop-trackers.js', 'scripts/prop-files.js'],
js : ['scripts/prop-general.js', 'scripts/prop-trackers.js', 'scripts/prop-webseeds.js', 'scripts/prop-files.js'],
},
tabsURL : 'properties.html',
tabsOnload : function() {
@ -355,6 +355,8 @@ window.addEvent('load', function () { @@ -355,6 +355,8 @@ window.addEvent('load', function () {
updateTorrentData();
else if (!$('prop_trackers').hasClass('invisible'))
updateTrackersData();
else if (!$('prop_webseeds').hasClass('invisible'))
updateWebSeedsData();
else if (!$('prop_files').hasClass('invisible'))
updateTorrentFilesData();
}
@ -362,6 +364,7 @@ window.addEvent('load', function () { @@ -362,6 +364,7 @@ window.addEvent('load', function () {
$('PropGeneralLink').addEvent('click', function(e){
$('prop_general').removeClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_webseeds').addClass("invisible");
$('prop_files').addClass("invisible");
updatePropertiesPanel();
});
@ -369,6 +372,15 @@ window.addEvent('load', function () { @@ -369,6 +372,15 @@ window.addEvent('load', function () {
$('PropTrackersLink').addEvent('click', function(e){
$('prop_trackers').removeClass("invisible");
$('prop_general').addClass("invisible");
$('prop_webseeds').addClass("invisible");
$('prop_files').addClass("invisible");
updatePropertiesPanel();
});
$('PropWebSeedsLink').addEvent('click', function(e){
$('prop_webseeds').removeClass("invisible");
$('prop_general').addClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_files').addClass("invisible");
updatePropertiesPanel();
});
@ -377,6 +389,11 @@ window.addEvent('load', function () { @@ -377,6 +389,11 @@ window.addEvent('load', function () {
$('prop_files').removeClass("invisible");
$('prop_general').addClass("invisible");
$('prop_trackers').addClass("invisible");
$('prop_webseeds').addClass("invisible");
updatePropertiesPanel();
});
$('propertiesPanel_collapseToggle').addEvent('click', function(e){
updatePropertiesPanel();
});
},

5
src/webui/www/public/scripts/prop-files.js

@ -273,7 +273,8 @@ var filesDynTable = new Class({ @@ -273,7 +273,8 @@ var filesDynTable = new Class({
var loadTorrentFilesDataTimer;
var loadTorrentFilesData = function() {
if ($('prop_files').hasClass('invisible')) {
if ($('prop_files').hasClass('invisible') ||
$('propertiesPanel_collapseToggle').hasClass('panel-expand')) {
// Tab changed, don't do anything
return;
}
@ -347,4 +348,4 @@ var updateTorrentFilesData = function() { @@ -347,4 +348,4 @@ var updateTorrentFilesData = function() {
}
fTable = new filesDynTable();
fTable.setup($('filesTable'));
fTable.setup($('filesTable'));

3
src/webui/www/public/scripts/prop-general.js

@ -16,7 +16,8 @@ var clearData = function() { @@ -16,7 +16,8 @@ var clearData = function() {
var loadTorrentDataTimer;
var loadTorrentData = function() {
if ($('prop_general').hasClass('invisible')) {
if ($('prop_general').hasClass('invisible') ||
$('propertiesPanel_collapseToggle').hasClass('panel-expand')) {
// Tab changed, don't do anything
return;
}

5
src/webui/www/public/scripts/prop-trackers.js

@ -54,7 +54,8 @@ var current_hash = ""; @@ -54,7 +54,8 @@ var current_hash = "";
var loadTrackersDataTimer;
var loadTrackersData = function() {
if ($('prop_trackers').hasClass('invisible')) {
if ($('prop_trackers').hasClass('invisible') ||
$('propertiesPanel_collapseToggle').hasClass('panel-expand')) {
// Tab changed, don't do anything
return;
}
@ -127,4 +128,4 @@ $('addTrackersPlus').addEvent('click', function addTrackerDlg() { @@ -127,4 +128,4 @@ $('addTrackersPlus').addEvent('click', function addTrackerDlg() {
width: 500,
height: 250
});
});
});

109
src/webui/www/public/scripts/prop-webseeds.js

@ -0,0 +1,109 @@ @@ -0,0 +1,109 @@
var webseedsDynTable = new Class({
initialize: function() {},
setup: function(table) {
this.table = $(table);
this.rows = new Hash();
},
removeRow: function(url) {
if (this.rows.has(url)) {
var tr = this.rows.get(url);
tr.dispose();
this.rows.erase(url);
return true;
}
return false;
},
removeAllRows: function() {
this.rows.each(function(tr, url) {
this.removeRow(url);
}.bind(this));
},
updateRow: function(tr, row) {
var tds = tr.getElements('td');
for (var i = 0; i < row.length; i++) {
tds[i].set('html', row[i]);
}
return true;
},
insertRow: function(row) {
var url = row[0];
if (this.rows.has(url)) {
var tr = this.rows.get(url);
this.updateRow(tr, row);
return;
}
//this.removeRow(id);
var tr = new Element('tr');
this.rows.set(url, tr);
for (var i = 0; i < row.length; i++) {
var td = new Element('td');
td.set('html', row[i]);
td.injectInside(tr);
}
tr.injectInside(this.table);
},
});
var current_hash = "";
var loadWebSeedsDataTimer;
var loadWebSeedsData = function() {
if ($('prop_webseeds').hasClass('invisible') ||
$('propertiesPanel_collapseToggle').hasClass('panel-expand')) {
// Tab changed, don't do anything
return;
}
var new_hash = myTable.getCurrentTorrentHash();
if (new_hash == "") {
wsTable.removeAllRows();
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
return;
}
if (new_hash != current_hash) {
wsTable.removeAllRows();
current_hash = new_hash;
}
var url = 'query/propertiesWebSeeds/' + current_hash;
var request = new Request.JSON({
url: url,
noCache: true,
method: 'get',
onFailure: function() {
$('error_div').set('html', 'QBT_TR(qBittorrent client is not reachable)QBT_TR');
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(20000);
},
onSuccess: function(webseeds) {
$('error_div').set('html', '');
if (webseeds) {
// Update WebSeeds data
webseeds.each(function(webseed) {
var row = new Array();
row.length = 1;
row[0] = webseed.url;
wsTable.insertRow(row);
});
}
else {
wsTable.removeAllRows();
}
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsDataTimer = loadWebSeedsData.delay(10000);
}
}).send();
}
var updateWebSeedsData = function() {
clearTimeout(loadWebSeedsDataTimer);
loadWebSeedsData();
}
wsTable = new webseedsDynTable();
wsTable.setup($('webseedsTable'));
Loading…
Cancel
Save