Fix priority up/down for multiple torrents at the same time (closes #692184)

This commit is contained in:
Christophe Dumez 2010-12-19 13:37:12 +00:00
parent 89abde61d6
commit c3b7aeadd7
5 changed files with 113 additions and 26 deletions

View File

@ -53,9 +53,12 @@
#include <QRegExp>
#include <QFileDialog>
#include <vector>
#include <queue>
#include "qinisettings.h"
using namespace libtorrent;
TransferListWidget::TransferListWidget(QWidget *parent, MainWindow *main_window, QBtSession *_BTSession):
QTreeView(parent), BTSession(_BTSession), main_window(main_window) {
// Create and apply delegate
@ -312,24 +315,50 @@ void TransferListWidget::deleteVisibleTorrents() {
}
void TransferListWidget::increasePrioSelectedTorrents() {
qDebug() << Q_FUNC_INFO;
if(main_window->getCurrentTabWidget() != this) return;
const QStringList hashes = getSelectedTorrentsHashes();
std::priority_queue<QPair<int, QTorrentHandle>, std::vector<QPair<int, QTorrentHandle> >, std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && !h.is_seed()) {
try {
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(!h.is_seed()) {
torrent_queue.push(qMakePair(h.queue_position(), h));
}
}catch(invalid_handle&){}
}
// Increase torrents priority (starting with the ones with highest priority)
while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_up();
}
} catch(invalid_handle& h) {}
torrent_queue.pop();
}
}
void TransferListWidget::decreasePrioSelectedTorrents() {
qDebug() << Q_FUNC_INFO;
if(main_window->getCurrentTabWidget() != this) return;
const QStringList hashes = getSelectedTorrentsHashes();
std::priority_queue<QPair<int, QTorrentHandle>, std::vector<QPair<int, QTorrentHandle> >, std::less<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach(const QString &hash, hashes) {
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(h.is_valid() && !h.is_seed()) {
try {
QTorrentHandle h = BTSession->getTorrentHandle(hash);
if(!h.is_seed()) {
torrent_queue.push(qMakePair(h.queue_position(), h));
}
}catch(invalid_handle&){}
}
// Decrease torrents priority (starting with the ones with lowest priority)
while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_down();
}
} catch(invalid_handle& h) {}
torrent_queue.pop();
}
}

View File

@ -45,6 +45,8 @@
#include <QDebug>
#include <QRegExp>
#include <QTemporaryFile>
#include <queue>
#include <vector>
using namespace libtorrent;
@ -505,23 +507,25 @@ void HttpConnection::respondCommand(QString command)
return;
}
if(command == "increasePrio") {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(parser.post("hash"));
if(h.is_valid()) h.queue_position_up();
increaseTorrentsPriority(parser.post("hashes").split("|"));
return;
}
if(command == "decreasePrio") {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(parser.post("hash"));
if(h.is_valid()) h.queue_position_down();
decreaseTorrentsPriority(parser.post("hashes").split("|"));
return;
}
if(command == "topPrio") {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(parser.post("hash"));
if(h.is_valid()) h.queue_position_top();
foreach(const QString &hash, parser.post("hashes").split("|")) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if(h.is_valid()) h.queue_position_top();
}
return;
}
if(command == "bottomPrio") {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(parser.post("hash"));
if(h.is_valid()) h.queue_position_bottom();
foreach(const QString &hash, parser.post("hashes").split("|")) {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if(h.is_valid()) h.queue_position_bottom();
}
return;
}
if(command == "recheck"){
@ -550,3 +554,49 @@ void HttpConnection::recheckAllTorrents() {
QBtSession::instance()->recheckTorrent(h.hash());
}
}
void HttpConnection::decreaseTorrentsPriority(const QStringList &hashes)
{
qDebug() << Q_FUNC_INFO << hashes;
std::priority_queue<QPair<int, QTorrentHandle>, std::vector<QPair<int, QTorrentHandle> >, std::less<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach(const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if(!h.is_seed()) {
torrent_queue.push(qMakePair(h.queue_position(), h));
}
}catch(invalid_handle&){}
}
// Decrease torrents priority (starting with the ones with lowest priority)
while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_down();
} catch(invalid_handle& h) {}
torrent_queue.pop();
}
}
void HttpConnection::increaseTorrentsPriority(const QStringList &hashes)
{
qDebug() << Q_FUNC_INFO << hashes;
std::priority_queue<QPair<int, QTorrentHandle>, std::vector<QPair<int, QTorrentHandle> >, std::greater<QPair<int, QTorrentHandle> > > torrent_queue;
// Sort torrents by priority
foreach(const QString &hash, hashes) {
try {
QTorrentHandle h = QBtSession::instance()->getTorrentHandle(hash);
if(!h.is_seed()) {
torrent_queue.push(qMakePair(h.queue_position(), h));
}
}catch(invalid_handle&){}
}
// Increase torrents priority (starting with the ones with highest priority)
while(!torrent_queue.empty()) {
QTorrentHandle h = torrent_queue.top().second;
try {
h.queue_position_up();
} catch(invalid_handle& h) {}
torrent_queue.pop();
}
}

View File

@ -67,6 +67,9 @@ protected slots:
void handleDownloadFailure(QString, QString);
void recheckTorrent(QString hash);
void recheckAllTorrents();
void decreaseTorrentsPriority(const QStringList& hashes);
void increaseTorrentsPriority(const QStringList& hashes);
public:
HttpConnection(QTcpSocket *socket, HttpServer *httpserver);

View File

@ -208,7 +208,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
return false;
}
QByteArray prop_nonce = regex_nonce.cap(1).toLocal8Bit();
qDebug("prop nonce is: %s", prop_nonce.data());
//qDebug("prop nonce is: %s", prop_nonce.data());
// get uri
QRegExp regex_uri(".*uri=\"([^\"]+)\".*");
if(regex_uri.indexIn(auth) < 0) {
@ -216,7 +216,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
return false;
}
QByteArray prop_uri = regex_uri.cap(1).toLocal8Bit();
qDebug("prop uri is: %s", prop_uri.data());
//qDebug("prop uri is: %s", prop_uri.data());
// get response
QRegExp regex_response(".*response=[\"]?([\\w=]+)[\"]?.*");
if(regex_response.indexIn(auth) < 0) {
@ -224,7 +224,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
return false;
}
QByteArray prop_response = regex_response.cap(1).toLocal8Bit();
qDebug("prop response is: %s", prop_response.data());
//qDebug("prop response is: %s", prop_response.data());
// Compute correct reponse
QCryptographicHash md5_ha2(QCryptographicHash::Md5);
md5_ha2.addData(method.toLocal8Bit() + ":" + prop_uri);
@ -239,21 +239,21 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
return false;
}
QByteArray prop_nc = regex_nc.cap(1).toLocal8Bit();
qDebug("prop nc is: %s", prop_nc.data());
//qDebug("prop nc is: %s", prop_nc.data());
QRegExp regex_cnonce(".*cnonce=[\"]?([\\w=]+)[\"]?.*");
if(regex_cnonce.indexIn(auth) < 0) {
qDebug("AUTH-PROB: qop but missing cnonce");
return false;
}
QByteArray prop_cnonce = regex_cnonce.cap(1).toLocal8Bit();
qDebug("prop cnonce is: %s", prop_cnonce.data());
//qDebug("prop cnonce is: %s", prop_cnonce.data());
QRegExp regex_qop(".*qop=[\"]?(\\w+)[\"]?.*");
if(regex_qop.indexIn(auth) < 0) {
qDebug("AUTH-PROB: missing qop");
return false;
}
QByteArray prop_qop = regex_qop.cap(1).toLocal8Bit();
qDebug("prop qop is: %s", prop_qop.data());
//qDebug("prop qop is: %s", prop_qop.data());
md5_ha.addData(password_ha1+":"+prop_nonce+":"+prop_nc+":"+prop_cnonce+":"+prop_qop+":"+ha2);
response = md5_ha.result().toHex();
} else {
@ -261,7 +261,7 @@ bool HttpServer::isAuthorized(QByteArray auth, QString method) const {
md5_ha.addData(password_ha1+":"+prop_nonce+":"+ha2);
response = md5_ha.result().toHex();
}
qDebug("AUTH: comparing reponses: (%d)", static_cast<int>(prop_response == response));
//qDebug("AUTH: comparing reponses: (%d)", static_cast<int>(prop_response == response));
return prop_response == response;
}

View File

@ -203,7 +203,7 @@ initializeWindows = function(){
}
};
['pause','resume','decreasePrio','increasePrio', 'topPrio', 'bottomPrio', 'recheck'].each(function(item) {
['pause','resume', 'recheck'].each(function(item) {
addClickEvent(item, function(e){
new Event(e).stop();
var h = myTable.selectedIds();
@ -220,12 +220,17 @@ initializeWindows = function(){
});
});
['decreasePrio','increasePrio', 'topPrio', 'bottomPrio'].each(function(item) {
addClickEvent(item, function(e){
new Event(e).stop();
setPriorityFN(item);
});
});
setPriorityFN = function(cmd) {
var h = myTable.selectedIds();
if(h.length){
h.each(function(hash, index){
new Request({url: '/command/'+cmd, method: 'post', data: {hash: hash}}).send();
});
if(h.length) {
new Request({url: '/command/'+cmd, method: 'post', data: {hashes: h.join("|")}}).send();
}
}