diff --git a/src/transferlistwidget.cpp b/src/transferlistwidget.cpp index 5e21945ea..430e3fcc4 100644 --- a/src/transferlistwidget.cpp +++ b/src/transferlistwidget.cpp @@ -53,9 +53,12 @@ #include #include #include +#include #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, std::vector >, std::greater > > 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, std::vector >, std::less > > 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(); } } diff --git a/src/webui/httpconnection.cpp b/src/webui/httpconnection.cpp index 6ef6244e8..7e6e1d959 100644 --- a/src/webui/httpconnection.cpp +++ b/src/webui/httpconnection.cpp @@ -45,6 +45,8 @@ #include #include #include +#include +#include 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, std::vector >, std::less > > 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, std::vector >, std::greater > > 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(); + } +} diff --git a/src/webui/httpconnection.h b/src/webui/httpconnection.h index 17be39cc6..e5878e80f 100644 --- a/src/webui/httpconnection.h +++ b/src/webui/httpconnection.h @@ -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); diff --git a/src/webui/httpserver.cpp b/src/webui/httpserver.cpp index 79078262e..2e7ee508e 100644 --- a/src/webui/httpserver.cpp +++ b/src/webui/httpserver.cpp @@ -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(prop_response == response)); + //qDebug("AUTH: comparing reponses: (%d)", static_cast(prop_response == response)); return prop_response == response; } diff --git a/src/webui/scripts/mocha-init.js b/src/webui/scripts/mocha-init.js index c3d5c58da..4d55a561f 100644 --- a/src/webui/scripts/mocha-init.js +++ b/src/webui/scripts/mocha-init.js @@ -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(); @@ -219,13 +219,18 @@ initializeWindows = function(){ new Request({url: '/command/'+item+'all'}).send(); }); }); + + ['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(); } }