Browse Source

- 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 :)

adaptive-webui-19844
Christophe Dumez 17 years ago
parent
commit
f0353e50b2
  1. 4
      TODO
  2. 29
      src/allocationDlg.h
  3. 51
      src/bittorrent.cpp
  4. 4
      src/bittorrent.h

4
TODO

@ -44,4 +44,6 @@ @@ -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
- Wait for some bug fixes in libtorrent :
- upload/download limit per torrent
- ipfilter crash

29
src/allocationDlg.h

@ -32,7 +32,7 @@ class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg { @@ -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 { @@ -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 { @@ -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 { @@ -150,7 +141,7 @@ class BandwidthAllocationDialog : public QDialog, private Ui_bandwidth_dlg {
bool uploadMode;
bool global;
bittorrent *BTSession;
QList<torrent_handle> handles;
QStringList hashes;
};
#endif

51
src/bittorrent.cpp

@ -62,6 +62,19 @@ void bittorrent::resumeUnfinishedTorrents(){ @@ -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<torrent_handle> handles = s->get_torrents();
for(unsigned int i=0; i<handles.size(); ++i){
@ -129,6 +142,7 @@ void bittorrent::deleteTorrent(const QString& hash, bool permanent){ @@ -129,6 +142,7 @@ void bittorrent::deleteTorrent(const QString& hash, bool permanent){
torrentBackup.remove(hash+".priorities");
torrentBackup.remove(hash+".savepath");
torrentBackup.remove(hash+".trackers");
torrentBackup.remove(hash+".speedLimits");
// Remove it from ETAs hash tables
ETAstats.take(hash);
ETAs.take(hash);
@ -271,6 +285,8 @@ void bittorrent::addTorrent(const QString& path, bool fromScanDir, bool onStartu @@ -271,6 +285,8 @@ void bittorrent::addTorrent(const QString& path, bool fromScanDir, bool onStartu
qDebug("Torrent hash is " + hash.toUtf8());
// Load filtered files
loadFilesPriorities(h);
// Load speed limit from hard drive
loadTorrentSpeedLimits(hash);
// Load trackers
bool loaded_trackers = loadTrackerFile(hash);
// Doing this to order trackers well
@ -442,6 +458,38 @@ void bittorrent::disableDHT(){ @@ -442,6 +458,38 @@ void bittorrent::disableDHT(){
}
}
void bittorrent::saveTorrentSpeedLimits(QString hash){
qDebug("Saving speedLimits file for %s", (const char*)hash.toUtf8());
torrent_handle h = getTorrentHandle(hash);
int download_limit = h.download_limit();
int upload_limit = h.upload_limit();
QFile speeds_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".speedLimits");
if(!speeds_file.open(QIODevice::WriteOnly | QIODevice::Text)){
qDebug("* Error: Couldn't open speed limits file for torrent: %s", (const char*)hash.toUtf8());
return;
}
speeds_file.write(QByteArray(misc::toString(download_limit).c_str())+QByteArray(" ")+QByteArray(misc::toString(upload_limit).c_str()));
speeds_file.close();
}
void bittorrent::loadTorrentSpeedLimits(QString hash){
qDebug("Loading speedLimits file for %s", (const char*)hash.toUtf8());
torrent_handle h = getTorrentHandle(hash);
QFile speeds_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".speedLimits");
if(!speeds_file.open(QIODevice::ReadOnly | QIODevice::Text)){
return;
}
QByteArray speed_limits = speeds_file.readAll();
speeds_file.close();
QList<QByteArray> 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){ @@ -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();

4
src/bittorrent.h

@ -109,6 +109,8 @@ class bittorrent : public QObject{ @@ -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<unsigned short, unsigned short> ports);
void setMaxConnections(int maxConnec);
@ -121,6 +123,8 @@ class bittorrent : public QObject{ @@ -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);

Loading…
Cancel
Save