From f0353e50b2061de61ba5d46deb2cf9157b647ef7 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 14 Jul 2007 13:38:29 +0000 Subject: [PATCH] - Upload/download limits per torrent are now saved on restart. I can't test this feature yet though because libtorrent is crashing when I try to use it :) --- TODO | 4 +++- src/allocationDlg.h | 29 +++++++++----------------- src/bittorrent.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++- src/bittorrent.h | 4 ++++ 4 files changed, 67 insertions(+), 21 deletions(-) diff --git a/TODO b/TODO index ecaa67519..18b8e0a8c 100644 --- a/TODO +++ b/TODO @@ -44,4 +44,6 @@ - Fix sorting with Qt 4.3 - Reported to Trolltech, waiting for fix - update sorting when a new torrent is added? * beta2 -- Save bandwidth limits per torrent on hard disk \ No newline at end of file +- Wait for some bug fixes in libtorrent : + - upload/download limit per torrent + - ipfilter crash \ No newline at end of file diff --git a/src/allocationDlg.h b/src/allocationDlg.h index 1ddef25e4..83bf8af52 100644 --- a/src/allocationDlg.h +++ b/src/allocationDlg.h @@ -32,7 +32,7 @@ class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg { Q_OBJECT public: - BandwidthAllocationDialog(QWidget *parent, bool uploadMode, bittorrent *BTSession, QStringList hashes): QDialog(parent), uploadMode(uploadMode){ + BandwidthAllocationDialog(QWidget *parent, bool uploadMode, bittorrent *BTSession, QStringList hashes): QDialog(parent), uploadMode(uploadMode), hashes(hashes){ setupUi(this); setAttribute(Qt::WA_DeleteOnClose); qDebug("Bandwidth allocation dialog creation"); @@ -47,21 +47,11 @@ class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg { lblTitle->setText(tr("Download limit:")); connect(bandwidthSlider, SIGNAL(valueChanged(int)), this, SLOT(updateBandwidthLabel(int))); if(!global){ - QString hash; - foreach(hash, hashes){ - torrent_handle h = BTSession->getTorrentHandle(hash); - if(!h.is_valid()){ - qDebug("Error: Invalid Handle!"); - continue; - }else{ - handles << h; - } - } - unsigned int nbTorrents = handles.size(); + unsigned int nbTorrents = hashes.size(); if(!nbTorrents) close(); int val; if(nbTorrents == 1){ - torrent_handle h = handles.at(0); + torrent_handle h = BTSession->getTorrentHandle(hashes.at(0)); if(uploadMode) val = h.upload_limit(); else @@ -118,17 +108,18 @@ class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg { } void setBandwidth(){ + qDebug("setBandwidth called"); int val = bandwidthSlider->value(); if(!global){ - torrent_handle h; + QString hash; if(uploadMode) { - foreach(h, handles) { - h.set_upload_limit(val*1024); + foreach(hash, hashes) { + BTSession->setUploadLimit(hash, val*1024); qDebug("Setting upload limit"); } } else { - foreach(h, handles) { - h.set_download_limit(val*1024); + foreach(hash, hashes) { + BTSession->setDownloadLimit(hash, val*1024); qDebug("Setting download limit"); } } @@ -150,7 +141,7 @@ class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg { bool uploadMode; bool global; bittorrent *BTSession; - QList handles; + QStringList hashes; }; #endif diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 4cc6b56e5..83c44cdf8 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -62,6 +62,19 @@ void bittorrent::resumeUnfinishedTorrents(){ resumeUnfinished(); } +void bittorrent::setDownloadLimit(QString hash, int val){ + torrent_handle h = getTorrentHandle(hash); + h.set_download_limit(val); + saveTorrentSpeedLimits(hash); +} + +void bittorrent::setUploadLimit(QString hash, int val){ + qDebug("Set upload limit rate to %d", val); + torrent_handle h = getTorrentHandle(hash); + h.set_upload_limit(val); + saveTorrentSpeedLimits(hash); +} + void bittorrent::updateETAs(){ std::vector handles = s->get_torrents(); for(unsigned int i=0; i speeds = speed_limits.split(' '); + if(speeds.size() != 2){ + std::cerr << "Invalid .speedLimits file for " << hash.toStdString() << '\n'; + return; + } + h.set_download_limit(speeds.at(0).toInt()); + h.set_upload_limit(speeds.at(1).toInt()); +} + // Read pieces priorities from .priorities file // and ask torrent_handle to consider them void bittorrent::loadFilesPriorities(torrent_handle &h){ @@ -816,7 +864,8 @@ void bittorrent::reloadTorrent(const torrent_handle &h){ new_h.set_max_uploads(-1); // Load filtered Files loadFilesPriorities(new_h); - + // Load speed limit from hard drive + loadTorrentSpeedLimits(fileHash); // Pause torrent if it was paused last time if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+fileHash+".paused")){ new_h.pause(); diff --git a/src/bittorrent.h b/src/bittorrent.h index daf4c1d56..b4217cca1 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -109,6 +109,8 @@ class bittorrent : public QObject{ void setTorrentFinishedChecking(QString hash); void resumeUnfinishedTorrents(); void updateETAs(); + void saveTorrentSpeedLimits(QString hash); + void loadTorrentSpeedLimits(QString hash); // Session configuration - Setters void setListeningPortsRange(std::pair ports); void setMaxConnections(int maxConnec); @@ -121,6 +123,8 @@ class bittorrent : public QObject{ void setDefaultSavePath(const QString& savepath); void applyEncryptionSettings(pe_settings se); void loadFilesPriorities(torrent_handle& h); + void setDownloadLimit(QString hash, int val); + void setUploadLimit(QString hash, int val); protected slots: void cleanDeleter(deleteThread* deleter);