From 697c275d833837d397ded5dda1f5551ffbe2761c Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 4 Oct 2008 15:21:52 +0000 Subject: [PATCH] - Fixed torrent queueing system (finally) --- src/bittorrent.cpp | 130 ++++++++++++++++++++++++--------------------- src/bittorrent.h | 1 + 2 files changed, 70 insertions(+), 61 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index ece966068..9877b20b7 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -260,11 +260,63 @@ void bittorrent::saveTorrentPriority(QString hash, int prio) { prio_file.close(); } +void bittorrent::fixTorrentPriorities() { + Q_ASSERT(queueingEnabled); + // Load priorities + QList > tmp_list; + QStringList noprio; + foreach(QString hash, unfinishedTorrents) { + int prio = loadTorrentPriority(hash); + if(prio != -1) { + misc::insertSort2(tmp_list, QPair(prio,hash), Qt::AscendingOrder); + } else { + noprio << hash; + } + } + downloadQueue->clear(); + QPair couple; + foreach(couple, tmp_list) { + downloadQueue->append(couple.second); + } + (*downloadQueue)<clear(); + updateDownloadQueue(); + foreach(QString hash, finishedTorrents) { + int prio = loadTorrentPriority(hash); + if(prio != -1) { + misc::insertSort2(tmp_list, QPair(prio,hash), Qt::AscendingOrder); + } else { + noprio << hash; + } + } + uploadQueue->clear(); + foreach(couple, tmp_list) { + uploadQueue->append(couple.second); + } + (*uploadQueue)<clear(); + updateUploadQueue(); +} + int bittorrent::loadTorrentPriority(QString hash) { - if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio")) { - // Read .queued file - QFile prio_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio"); - prio_file.open(QIODevice::ReadOnly | QIODevice::Text); + // Read .prio file + QFile prio_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio"); + if(prio_file.open(QIODevice::ReadOnly | QIODevice::Text)) { bool ok = false; int prio = prio_file.readAll().toInt(&ok); prio_file.close(); @@ -318,57 +370,11 @@ void bittorrent::setQueueingEnabled(bool enable) { qDebug("Queueing system is changing state..."); queueingEnabled = enable; if(enable) { - // Load priorities - QList > tmp_list; - QStringList noprio; - QStringList unfinished = getUnfinishedTorrents(); - foreach(QString hash, unfinished) { - int prio = loadTorrentPriority(hash); - if(prio != -1) { - misc::insertSort2(tmp_list, QPair(prio,hash), Qt::AscendingOrder); - } else { - noprio << hash; - } - } downloadQueue = new QStringList(); - QPair couple; - foreach(couple, tmp_list) { - downloadQueue->append(couple.second); - } - (*downloadQueue)< > tmp_list2; - QStringList noprio2; - QStringList finished = getFinishedTorrents(); - foreach(QString hash, finished) { - int prio = loadTorrentPriority(hash); - if(prio != -1) { - misc::insertSort2(tmp_list2, QPair(prio,hash), Qt::AscendingOrder); - } else { - noprio2 << hash; - } - } uploadQueue = new QStringList(); - QPair couple2; - foreach(couple2, tmp_list2) { - uploadQueue->append(couple2.second); - } - (*uploadQueue)<contains(hash)) { uploadQueue->removeAll(hash); - queuedDownloads->removeAll(hash); - if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio")) + queuedUploads->removeAll(hash); + /*if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio")) QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio"); if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".queued")) - QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".queued"); + QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".queued");*/ updateUploadQueue(); } // Add it to downloadQueue @@ -747,10 +753,10 @@ void bittorrent::setFinishedTorrent(QString hash) { if(queueingEnabled) { downloadQueue->removeAll(hash); queuedDownloads->removeAll(hash); - if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio")) + /*if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio")) QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".prio"); if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".queued")) - QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".queued"); + QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".queued");*/ updateDownloadQueue(); if(!uploadQueue->contains(hash)) { uploadQueue->append(hash); @@ -886,7 +892,7 @@ void bittorrent::loadWebSeeds(QString hash) { } // Add a torrent to the bittorrent session -void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url, bool) { +void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url, bool resumed) { QTorrentHandle h; entry resume_data; bool fastResume=false; @@ -1017,15 +1023,14 @@ void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url, bo } if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")) { finishedTorrents << hash; - if(queueingEnabled) { + if(!resumed && queueingEnabled) { uploadQueue->append(hash); saveTorrentPriority(hash, uploadQueue->size()-1); updateUploadQueue(); } }else{ unfinishedTorrents << hash; - // Add it to downloadQueue - if(queueingEnabled) { + if(!resumed && queueingEnabled) { downloadQueue->append(hash); saveTorrentPriority(hash, downloadQueue->size()-1); updateDownloadQueue(); @@ -2041,5 +2046,8 @@ void bittorrent::resumeUnfinishedTorrents() { foreach(fileName, filePaths) { addTorrent(fileName, false, QString(), true); } + if(queueingEnabled) { + fixTorrentPriorities(); + } qDebug("Unfinished torrents resumed"); } diff --git a/src/bittorrent.h b/src/bittorrent.h index a2d3f413d..e7339fcdc 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -187,6 +187,7 @@ class bittorrent : public QObject { void setETACalculation(bool enable); void addConsoleMessage(QString msg, QColor color=QApplication::palette().color(QPalette::WindowText)); void addPeerBanMessage(QString msg, bool from_ipfilter); + void fixTorrentPriorities(); protected slots: void scanDirectory();