diff --git a/src/FinishedTorrents.cpp b/src/FinishedTorrents.cpp index e0b235091..cc7642e5f 100644 --- a/src/FinishedTorrents.cpp +++ b/src/FinishedTorrents.cpp @@ -99,9 +99,6 @@ void FinishedTorrents::notifyTorrentDoubleClicked(const QModelIndex& index) { } void FinishedTorrents::addTorrent(QString hash){ - if(!BTSession->isFinished(hash)){ - BTSession->setFinishedTorrent(hash); - } int row = getRowFromHash(hash); if(row != -1) return; row = finishedListModel->rowCount(); @@ -126,13 +123,6 @@ void FinishedTorrents::addTorrent(QString hash){ emit finishedTorrentsNumberChanged(nbFinished); } -void FinishedTorrents::torrentAdded(QTorrentHandle& h) { - QString hash = h.hash(); - if(BTSession->isFinished(hash)) { - addTorrent(hash); - } -} - // Set the color of a row in data model void FinishedTorrents::setRowColor(int row, QString color){ unsigned int nbColumns = finishedListModel->columnCount()-1; @@ -239,15 +229,8 @@ void FinishedTorrents::on_actionSet_upload_limit_triggered(){ new BandwidthAllocationDialog(this, true, BTSession, hashes); } -void FinishedTorrents::updateFinishedList(){ - QString hash; - QStringList finishedSHAs = BTSession->getFinishedTorrents(); - foreach(hash, finishedSHAs){ - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(!h.is_valid()){ - qDebug("Problem: This torrent is not valid in finished list"); - continue; - } +void FinishedTorrents::updateTorrent(QTorrentHandle h) { + QString hash = h.hash(); int row = getRowFromHash(hash); if(row == -1){ qDebug("Cannot find torrent in finished list, adding it"); @@ -256,7 +239,7 @@ void FinishedTorrents::updateFinishedList(){ } Q_ASSERT(row != -1); // Update queued torrent - if(BTSession->isQueueingEnabled() && BTSession->isTorrentQueued(hash)) { + if(BTSession->isQueueingEnabled() && h.is_queued()) { if(h.state() == torrent_status::checking_files || h.state() == torrent_status::queued_for_checking){ finishedListModel->setData(finishedListModel->index(row, F_NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/time.png"))), Qt::DecorationRole); } else { @@ -266,27 +249,13 @@ void FinishedTorrents::updateFinishedList(){ finishedListModel->setData(finishedListModel->index(row, F_UPSPEED), 0.); finishedListModel->setData(finishedListModel->index(row, F_LEECH), "0"); setRowColor(row, QString::fromUtf8("grey")); + return; } - if(h.is_paused() || h.is_queued()) continue; - if(h.state() == torrent_status::downloading || (h.state() != torrent_status::checking_files && h.state() != torrent_status::queued_for_checking && h.progress() < 1.)) { - // What are you doing here? go back to download tab! - int reponse = QMessageBox::question(this, tr("Incomplete torrent in seeding list"), tr("It appears that the state of '%1' torrent changed from 'seeding' to 'downloading'. Would you like to move it back to download list? (otherwise the torrent will simply be deleted)").arg(h.name()), QMessageBox::Yes | QMessageBox::No); - if (reponse == QMessageBox::Yes) { - qDebug("Info: a torrent was moved from finished to download tab"); - deleteTorrent(hash); - BTSession->setUnfinishedTorrent(hash); - emit torrentMovedFromFinishedList(hash); - } - else if (reponse == QMessageBox::No) { - qDebug("Deleted from the finished"); - BTSession->deleteTorrent(hash, false); - } - continue; - } + if(h.is_paused()) return; if(h.state() == torrent_status::checking_files || h.state() == torrent_status::queued_for_checking){ finishedListModel->setData(finishedListModel->index(row, F_NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/time.png"))), Qt::DecorationRole); setRowColor(row, QString::fromUtf8("grey")); - continue; + return; } setRowColor(row, QString::fromUtf8("orange")); finishedListModel->setData(finishedListModel->index(row, F_NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/skin/seeding.png"))), Qt::DecorationRole); @@ -299,7 +268,6 @@ void FinishedTorrents::updateFinishedList(){ if(!finishedList->isColumnHidden(F_RATIO)) { finishedListModel->setData(finishedListModel->index(row, F_RATIO), QVariant(misc::toQString(BTSession->getRealRatio(hash)))); } - } } int FinishedTorrents::getRowFromHash(QString hash) const{ diff --git a/src/FinishedTorrents.h b/src/FinishedTorrents.h index 45e1dc304..924e527c8 100644 --- a/src/FinishedTorrents.h +++ b/src/FinishedTorrents.h @@ -65,7 +65,6 @@ class FinishedTorrents : public QWidget, public Ui::seeding { void sortFinishedListFloat(int index, Qt::SortOrder sortOrder); void sortFinishedListString(int index, Qt::SortOrder sortOrder); void updateFileSize(QString hash); - void torrentAdded(QTorrentHandle& h); void on_actionSet_upload_limit_triggered(); void notifyTorrentDoubleClicked(const QModelIndex& index); void hideOrShowColumnName(); @@ -77,7 +76,7 @@ class FinishedTorrents : public QWidget, public Ui::seeding { public slots: void addTorrent(QString hash); - void updateFinishedList(); + void updateTorrent(QTorrentHandle h); void pauseTorrent(QString hash); void resumeTorrent(QString hash); void propertiesSelection(); diff --git a/src/GUI.cpp b/src/GUI.cpp index 166b60c49..4bdff89e2 100644 --- a/src/GUI.cpp +++ b/src/GUI.cpp @@ -121,13 +121,13 @@ GUI::GUI(QWidget *parent, QStringList torrentCmdLine) : QMainWindow(parent), dis BTSession = new bittorrent(); connect(BTSession, SIGNAL(fullDiskError(QTorrentHandle&)), this, SLOT(fullDiskError(QTorrentHandle&))); connect(BTSession, SIGNAL(finishedTorrent(QTorrentHandle&)), this, SLOT(finishedTorrent(QTorrentHandle&))); + connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), this, SLOT(addedTorrent(QTorrentHandle&))); + connect(BTSession, SIGNAL(torrentFinishedChecking(QTorrentHandle&)), this, SLOT(checkedTorrent(QTorrentHandle&))); connect(BTSession, SIGNAL(trackerAuthenticationRequired(QTorrentHandle&)), this, SLOT(trackerAuthenticationRequired(QTorrentHandle&))); connect(BTSession, SIGNAL(newDownloadedTorrent(QString, QString)), this, SLOT(processDownloadedFiles(QString, QString))); connect(BTSession, SIGNAL(downloadFromUrlFailure(QString, QString)), this, SLOT(handleDownloadFromUrlFailure(QString, QString))); connect(BTSession, SIGNAL(deletedTorrent(QString)), this, SLOT(deleteTorrent(QString))); connect(BTSession, SIGNAL(pausedTorrent(QString)), this, SLOT(pauseTorrent(QString))); - connect(BTSession, SIGNAL(updateUnfinishedTorrentNumber()), this, SLOT(updateUnfinishedTorrentNumberCalc())); - connect(BTSession, SIGNAL(updateFinishedTorrentNumber()), this, SLOT(updateFinishedTorrentNumberCalc())); qDebug("create tabWidget"); tabs = new QTabWidget(); // Download torrents tab @@ -348,14 +348,12 @@ void GUI::finishedTorrent(QTorrentHandle& h) const { qDebug("In GUI, a torrent has finished"); QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); bool show_msg = true; + if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+h.hash()+".finished")) + show_msg = false; QString fileName = h.name(); bool useNotificationBalloons = settings.value(QString::fromUtf8("Preferences/General/NotificationBaloons"), true).toBool(); // Add it to finished tab QString hash = h.hash(); - if(QFile::exists(misc::qBittorrentPath()+QString::fromUtf8("BT_backup")+QDir::separator()+hash+QString::fromUtf8(".finished"))) { - show_msg = false; - qDebug("We received a finished signal for torrent %s, but it already has a .finished file", hash.toUtf8().data()); - } if(show_msg) BTSession->addConsoleMessage(tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(fileName)); downloadingTorrentTab->deleteTorrent(hash); @@ -365,6 +363,22 @@ void GUI::finishedTorrent(QTorrentHandle& h) const { } } +void GUI::addedTorrent(QTorrentHandle& h) const { + if(h.is_seed()) { + finishedTorrentTab->addTorrent(h.hash()); + } else { + downloadingTorrentTab->addTorrent(h.hash()); + } +} + +void GUI::checkedTorrent(QTorrentHandle& h) const { + if(h.is_seed()) { + // Move torrent to finished tab + downloadingTorrentTab->deleteTorrent(h.hash()); + finishedTorrentTab->addTorrent(h.hash()); + } +} + // Notification when disk is full void GUI::fullDiskError(QTorrentHandle& h) const { QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); @@ -1117,23 +1131,11 @@ void GUI::updateUnfinishedTorrentNumber(unsigned int nb) { tabs->setTabText(0, tr("Downloads") +QString::fromUtf8(" (")+misc::toQString(nb-paused)+"/"+misc::toQString(nb)+QString::fromUtf8(")")); } -void GUI::updateUnfinishedTorrentNumberCalc() { - unsigned int paused = BTSession->getUnfinishedPausedTorrentsNb(); - unsigned int nb = BTSession->getUnfinishedTorrents().size(); - tabs->setTabText(0, tr("Downloads") +QString::fromUtf8(" (")+misc::toQString(nb-paused)+"/"+misc::toQString(nb)+QString::fromUtf8(")")); -} - void GUI::updateFinishedTorrentNumber(unsigned int nb) { unsigned int paused = BTSession->getFinishedPausedTorrentsNb(); tabs->setTabText(1, tr("Finished") +QString::fromUtf8(" (")+misc::toQString(nb-paused)+"/"+misc::toQString(nb)+QString::fromUtf8(")")); } -void GUI::updateFinishedTorrentNumberCalc() { - unsigned int paused = BTSession->getFinishedPausedTorrentsNb(); - unsigned int nb = BTSession->getFinishedTorrents().size(); - tabs->setTabText(1, tr("Finished") +QString::fromUtf8(" (")+misc::toQString(nb-paused)+"/"+misc::toQString(nb)+QString::fromUtf8(")")); -} - // Allow to change action on double-click void GUI::torrentDoubleClicked(QString hash, bool finished) { int action; @@ -1196,65 +1198,43 @@ void GUI::togglePausedState(QString hash) { // Pause All Downloads in DL list void GUI::on_actionPause_All_triggered() { - bool change = false; - QStringList DL_hashes = BTSession->getUnfinishedTorrents(); - QStringList F_hashes = BTSession->getFinishedTorrents(); - QString hash; - foreach(hash, DL_hashes) { - if(BTSession->pauseTorrent(hash)){ - change = true; - downloadingTorrentTab->pauseTorrent(hash); + bool change = false; + std::vector torrents = BTSession->getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid() || h.is_paused()) continue; + change = true; + if(h.is_seed()) { + // Update in finished list + finishedTorrentTab->pauseTorrent(h.hash()); + } else { + // Update in download list + downloadingTorrentTab->pauseTorrent(h.hash()); + } } - } - foreach(hash, F_hashes) { - if(BTSession->pauseTorrent(hash)){ - change = true; - finishedTorrentTab->pauseTorrent(hash); + if(change) { + updateUnfinishedTorrentNumber(downloadingTorrentTab->getNbTorrentsInList()); + updateFinishedTorrentNumber(finishedTorrentTab->getNbTorrentsInList()); } - } - if(change) { - updateUnfinishedTorrentNumber(downloadingTorrentTab->getNbTorrentsInList()); - updateFinishedTorrentNumber(finishedTorrentTab->getNbTorrentsInList()); - } } void GUI::on_actionIncreasePriority_triggered() { - bool inDownloadList = true; - if(tabs->currentIndex() > 1) return; - if(tabs->currentIndex() == 1) - inDownloadList = false; - QStringList hashes; - if(inDownloadList) { - hashes = downloadingTorrentTab->getSelectedTorrents(); - } else { - hashes = finishedTorrentTab->getSelectedTorrents(); - } + Q_ASSERT(tabs->currentIndex() == 0); + QStringList hashes = downloadingTorrentTab->getSelectedTorrents(); foreach(QString hash, hashes) { - if(inDownloadList) { BTSession->increaseDlTorrentPriority(hash); - downloadingTorrentTab->updateDlList(); - } } + updateLists(); } void GUI::on_actionDecreasePriority_triggered() { - bool inDownloadList = true; - if(tabs->currentIndex() > 1) return; - if(tabs->currentIndex() == 1) - inDownloadList = false; - QStringList hashes; - if(inDownloadList) { - hashes = downloadingTorrentTab->getSelectedTorrents(); - } else { - hashes = finishedTorrentTab->getSelectedTorrents(); - } - QString hash; + Q_ASSERT(tabs->currentIndex() == 0); + QStringList hashes = downloadingTorrentTab->getSelectedTorrents(); foreach(QString hash, hashes) { - if(inDownloadList) { BTSession->decreaseDlTorrentPriority(hash); - downloadingTorrentTab->updateDlList(); - } } + updateLists(); } // pause selected items in the list @@ -1293,21 +1273,20 @@ void GUI::pauseTorrent(QString hash) { // Resume All Downloads in DL list void GUI::on_actionStart_All_triggered() { bool change = false; - QStringList DL_hashes = BTSession->getUnfinishedTorrents(); - QStringList F_hashes = BTSession->getFinishedTorrents(); - QString hash; - foreach(hash, DL_hashes) { - if(BTSession->resumeTorrent(hash)){ - change = true; - downloadingTorrentTab->resumeTorrent(hash); + std::vector torrents = BTSession->getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid() || !h.is_paused()) continue; + change = true; + if(h.is_seed()) { + // Update in finished list + finishedTorrentTab->resumeTorrent(h.hash()); + } else { + // Update in download list + downloadingTorrentTab->resumeTorrent(h.hash()); + } } - } - foreach(hash, F_hashes) { - if(BTSession->resumeTorrent(hash)){ - change = true; - finishedTorrentTab->resumeTorrent(hash); - } - } if(change) { updateUnfinishedTorrentNumber(downloadingTorrentTab->getNbTorrentsInList()); updateFinishedTorrentNumber(finishedTorrentTab->getNbTorrentsInList()); @@ -1360,24 +1339,34 @@ void GUI::on_actionTorrent_Properties_triggered() { } void GUI::updateLists() { - // update global informations - dlSpeedLbl->setText(tr("DL: %1 KiB/s").arg(QString(QByteArray::number(BTSession->getPayloadDownloadRate()/1024., 'f', 1)))); - upSpeedLbl->setText(tr("UP: %1 KiB/s").arg(QString(QByteArray::number(BTSession->getPayloadUploadRate()/1024., 'f', 1)))); - switch(getCurrentTabIndex()){ - case 0: - downloadingTorrentTab->updateDlList(); - break; - case 1: - finishedTorrentTab->updateFinishedList(); - break; - default: - return; - } - if(displaySpeedInTitle) { - QString dl_rate = QByteArray::number(BTSession->getSessionStatus().payload_download_rate/1024, 'f', 1); - QString up_rate = QByteArray::number(BTSession->getSessionStatus().payload_upload_rate/1024, 'f', 1); - setWindowTitle(tr("qBittorrent %1 (DL: %2KiB/s, UP: %3KiB/s)", "%1 is qBittorrent version").arg(QString::fromUtf8(VERSION)).arg(dl_rate).arg(up_rate)); - } + // update global informations + dlSpeedLbl->setText(tr("DL: %1 KiB/s").arg(QString(QByteArray::number(BTSession->getPayloadDownloadRate()/1024., 'f', 1)))); + upSpeedLbl->setText(tr("UP: %1 KiB/s").arg(QString(QByteArray::number(BTSession->getPayloadUploadRate()/1024., 'f', 1)))); + std::vector torrents = BTSession->getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid()) continue; + if(h.is_seed()) { + // Update in finished list + finishedTorrentTab->updateTorrent(h); + } else { + // Delete from finished list, if it moved back + if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+h.hash()+".finished")) { + if(h.state() != torrent_status::checking_files && h.state() != torrent_status::queued_for_checking) { + finishedTorrentTab->deleteTorrent(h.hash()); + QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+h.hash()+".finished"); + } + } + // Update in download list + downloadingTorrentTab->updateTorrent(h); + } + } + if(displaySpeedInTitle) { + QString dl_rate = QByteArray::number(BTSession->getSessionStatus().payload_download_rate/1024, 'f', 1); + QString up_rate = QByteArray::number(BTSession->getSessionStatus().payload_upload_rate/1024, 'f', 1); + setWindowTitle(tr("qBittorrent %1 (DL: %2KiB/s, UP: %3KiB/s)", "%1 is qBittorrent version").arg(QString::fromUtf8(VERSION)).arg(dl_rate).arg(up_rate)); + } } // Called when a tracker requires authentication diff --git a/src/GUI.h b/src/GUI.h index 4c572d57a..c2026c241 100644 --- a/src/GUI.h +++ b/src/GUI.h @@ -126,8 +126,6 @@ class GUI : public QMainWindow, private Ui::MainWindow{ void readSettings(); void on_actionExit_triggered(); void createTrayIcon(); - void updateUnfinishedTorrentNumberCalc(); - void updateFinishedTorrentNumberCalc(); void updateUnfinishedTorrentNumber(unsigned int nb); void updateFinishedTorrentNumber(unsigned int nb); void fullDiskError(QTorrentHandle& h) const; @@ -160,6 +158,8 @@ class GUI : public QMainWindow, private Ui::MainWindow{ void downloadFromURLList(const QStringList& urls); void deleteTorrent(QString hash); void finishedTorrent(QTorrentHandle& h) const; + void addedTorrent(QTorrentHandle& h) const; + void checkedTorrent(QTorrentHandle& h) const; void updateLists(); bool initWebUi(QString username, QString password, int port); void pauseTorrent(QString hash); diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 0ede943ff..7692c91aa 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -124,21 +124,21 @@ void bittorrent::preAllocateAllFiles(bool b) { void bittorrent::deleteBigRatios() { if(max_ratio == -1) return; - QString hash; - foreach(hash, finishedTorrents) { - QTorrentHandle h = getTorrentHandle(hash); - if(!h.is_valid()) { - qDebug("/!\\ Error: Invalid handle"); - continue; + std::vector torrents = getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid()) continue; + if(h.is_seed()) { + QString hash = h.hash(); + if(getRealRatio(hash) > max_ratio) { + QString fileName = h.name(); + addConsoleMessage(tr("%1 reached the maximum ratio you set.").arg(fileName)); + deleteTorrent(hash); + //emit torrent_ratio_deleted(fileName); + } + } } - QString hash = h.hash(); - if(getRealRatio(hash) > max_ratio) { - QString fileName = h.name(); - addConsoleMessage(tr("%1 reached the maximum ratio you set.").arg(fileName)); - deleteTorrent(hash); - //emit torrent_ratio_deleted(fileName); - } - } } void bittorrent::setDownloadLimit(QString hash, long val) { @@ -222,6 +222,10 @@ qlonglong bittorrent::getETA(QString hash) const { } } +std::vector bittorrent::getTorrents() const { + return s->get_torrents(); +} + // Return the torrent handle, given its hash QTorrentHandle bittorrent::getTorrentHandle(QString hash) const{ return QTorrentHandle(s->find_torrent(misc::fromString((hash.toStdString())))); @@ -240,21 +244,29 @@ bool bittorrent::isPaused(QString hash) const{ unsigned int bittorrent::getFinishedPausedTorrentsNb() const { unsigned int nbPaused = 0; - foreach(QString hash, finishedTorrents) { - if(isPaused(hash)) { - ++nbPaused; + std::vector torrents = getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid()) continue; + if(h.is_seed() && h.is_paused()) { + ++nbPaused; + } } - } return nbPaused; } unsigned int bittorrent::getUnfinishedPausedTorrentsNb() const { unsigned int nbPaused = 0; - foreach(QString hash, unfinishedTorrents) { - if(isPaused(hash)) { - ++nbPaused; + std::vector torrents = getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid()) continue; + if(!h.is_seed() && h.is_paused()) { + ++nbPaused; + } } - } return nbPaused; } @@ -285,17 +297,6 @@ void bittorrent::deleteTorrent(QString hash, bool permanent) { } // Remove tracker errors trackersErrors.remove(hash); - int index = finishedTorrents.indexOf(hash); - if(index != -1) { - finishedTorrents.removeAt(index); - }else{ - index = unfinishedTorrents.indexOf(hash); - if(index != -1) { - unfinishedTorrents.removeAt(index); - }else{ - std::cerr << "Error: Torrent " << hash.toStdString() << " is neither in finished or unfinished list\n"; - } - } if(permanent) addConsoleMessage(tr("'%1' was removed permanently.", "'xxx.avi' was removed permanently.").arg(fileName)); else @@ -303,50 +304,8 @@ void bittorrent::deleteTorrent(QString hash, bool permanent) { emit deletedTorrent(hash); } -// Return a list of hashes for the finished torrents -QStringList bittorrent::getFinishedTorrents() const { - return finishedTorrents; -} - -QStringList bittorrent::getUnfinishedTorrents() const { - return unfinishedTorrents; -} - bool bittorrent::isFinished(QString hash) const { - return finishedTorrents.contains(hash); -} - -// Remove the given hash from the list of finished torrents -void bittorrent::setUnfinishedTorrent(QString hash) { - if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")){ - QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished"); - } - int index = finishedTorrents.indexOf(hash); - if(index != -1) { - finishedTorrents.removeAt(index); - } - if(!unfinishedTorrents.contains(hash)) { - unfinishedTorrents << hash; - QTorrentHandle h = getTorrentHandle(hash); - } - //emit torrentSwitchedtoUnfinished(hash); -} - -// Add the given hash to the list of finished torrents -void bittorrent::setFinishedTorrent(QString hash) { - if(!QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")) { - QFile finished_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished"); - finished_file.open(QIODevice::WriteOnly | QIODevice::Text); - finished_file.close(); - } - if(!finishedTorrents.contains(hash)) { - finishedTorrents << hash; - } - int index = unfinishedTorrents.indexOf(hash); - if(index != -1) { - unfinishedTorrents.removeAt(index); - } - //emit torrentSwitchedtoFinished(hash); + return getTorrentHandle(hash).is_seed(); } // Pause a running torrent @@ -397,15 +356,27 @@ bool bittorrent::resumeTorrent(QString hash) { } void bittorrent::pauseAllTorrents() { - QStringList list = getUnfinishedTorrents() + getFinishedTorrents(); - foreach(QString hash, list) - pauseTorrent(hash); + std::vector torrents = getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid()) continue; + if(!h.is_paused()) { + h.pause(); + } + } } void bittorrent::resumeAllTorrents() { - QStringList list = getUnfinishedTorrents() + getFinishedTorrents(); - foreach(QString hash, list) - resumeTorrent(hash); + std::vector torrents = getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(!h.is_valid()) continue; + if(h.is_paused()) { + h.resume(); + } + } } void bittorrent::loadWebSeeds(QString hash) { @@ -583,11 +554,6 @@ void bittorrent::addTorrent(QString path, bool fromScanDir, QString from_url, bo // Start torrent because it was added in paused state h.resume(); } - if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")) { - finishedTorrents << hash; - }else{ - unfinishedTorrents << hash; - } // If download from url, remove temp file if(!from_url.isNull()) QFile::remove(file); // Delete from scan dir to avoid trying to download it again @@ -1197,9 +1163,13 @@ void bittorrent::readAlerts() { QTorrentHandle h(p->handle); if(h.is_valid()){ QString hash = h.hash(); + // Create .paused file if necessary + QFile finished_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished"); + finished_file.open(QIODevice::WriteOnly | QIODevice::Text); + finished_file.write(" "); + finished_file.close(); h.save_resume_data(); qDebug("Received finished alert for %s", h.name().toUtf8().data()); - setFinishedTorrent(hash); emit finishedTorrent(h); } } diff --git a/src/bittorrent.h b/src/bittorrent.h index f0c73e8be..8218ea82f 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -54,8 +54,6 @@ class bittorrent : public QObject { QHash > trackersErrors; QStringList consoleMessages; QStringList peerBanMessages; - QStringList finishedTorrents; - QStringList unfinishedTorrents; bool preAllocateAll; bool addInPause; int maxConnecsPerTorrent; @@ -77,6 +75,7 @@ class bittorrent : public QObject { bittorrent(); ~bittorrent(); QTorrentHandle getTorrentHandle(QString hash) const; + std::vector getTorrents() const; bool isPaused(QString hash) const; bool isFilePreviewPossible(QString fileHash) const; bool isDHTEnabled() const; @@ -87,8 +86,6 @@ class bittorrent : public QObject { float getRealRatio(QString hash) const; session* getSession() const; QHash getTrackersErrors(QString hash) const; - QStringList getFinishedTorrents() const; - QStringList getUnfinishedTorrents() const; bool isFinished(QString hash) const; bool has_filtered_files(QString hash) const; unsigned int getFinishedPausedTorrentsNb() const; @@ -152,8 +149,6 @@ class bittorrent : public QObject { void loadFilesPriorities(QTorrentHandle& h); void setDownloadLimit(QString hash, long val); void setUploadLimit(QString hash, long val); - void setUnfinishedTorrent(QString hash); - void setFinishedTorrent(QString hash); void enableUPnP(bool b); void enableNATPMP(bool b); void enableLSD(bool b); @@ -182,8 +177,6 @@ class bittorrent : public QObject { void updateFileSize(QString hash); void downloadFromUrlFailure(QString url, QString reason); void torrentFinishedChecking(QString hash); - void updateFinishedTorrentNumber(); - void updateUnfinishedTorrentNumber(); void forceUnfinishedListUpdate(); void forceFinishedListUpdate(); }; diff --git a/src/downloadingTorrents.cpp b/src/downloadingTorrents.cpp index 03ca57459..6601162a0 100644 --- a/src/downloadingTorrents.cpp +++ b/src/downloadingTorrents.cpp @@ -67,8 +67,6 @@ DownloadingTorrents::DownloadingTorrents(QObject *parent, bittorrent *BTSession) downloadList->hideColumn(HASH); loadHiddenColumns(); - connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), this, SLOT(torrentAdded(QTorrentHandle&))); - connect(BTSession, SIGNAL(forceUnfinishedListUpdate()), this, SLOT(updateDlList())); connect(BTSession, SIGNAL(torrentFinishedChecking(QString)), this, SLOT(sortProgressColumn(QString))); // Load last columns width for download list @@ -486,16 +484,7 @@ QStringList DownloadingTorrents::getSelectedTorrents(bool only_one) const{ // get information from torrent handles and // update download list accordingly -void DownloadingTorrents::updateDlList() { - // browse handles - QStringList unfinishedTorrents = BTSession->getUnfinishedTorrents(); - QString hash; - foreach(hash, unfinishedTorrents) { - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(!h.is_valid()){ - qDebug("We have an invalid handle for: %s", qPrintable(hash)); - continue; - } +void DownloadingTorrents::updateTorrent(QTorrentHandle h) { try{ QString hash = h.hash(); int row = getRowFromHash(hash); @@ -508,7 +497,7 @@ void DownloadingTorrents::updateDlList() { // Update Priority if(BTSession->isQueueingEnabled()) { DLListModel->setData(DLListModel->index(row, PRIORITY), QVariant((int)BTSession->getDlTorrentPriority(hash))); - if(BTSession->isTorrentQueued(hash)) { + if(h.is_queued()) { if(h.state() == torrent_status::checking_files || h.state() == torrent_status::queued_for_checking) { DLListModel->setData(DLListModel->index(row, NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/time.png"))), Qt::DecorationRole); if(!downloadList->isColumnHidden(PROGRESS)) { @@ -525,20 +514,14 @@ void DownloadingTorrents::updateDlList() { DLListModel->setData(DLListModel->index(row, UPSPEED), QVariant((double)0.)); DLListModel->setData(DLListModel->index(row, SEEDSLEECH), QVariant("0/0")); setRowColor(row, QString::fromUtf8("grey")); + return; } } // No need to update a paused torrent - if(h.is_paused() || h.is_queued()) continue; + if(h.is_paused()) return; // Parse download state // Setting download state switch(h.state()) { - case torrent_status::finished: - case torrent_status::seeding: - qDebug("A torrent that was in download tab just finished, moving it to finished tab"); - BTSession->setUnfinishedTorrent(hash); - emit torrentFinished(hash); - deleteTorrent(hash); - continue; case torrent_status::checking_files: case torrent_status::queued_for_checking: DLListModel->setData(DLListModel->index(row, NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/time.png"))), Qt::DecorationRole); @@ -583,16 +566,10 @@ void DownloadingTorrents::updateDlList() { if(!downloadList->isColumnHidden(RATIO)) { DLListModel->setData(DLListModel->index(row, RATIO), QVariant(misc::toQString(BTSession->getRealRatio(hash)))); } - }catch(invalid_handle e) { - continue; - } - } + }catch(invalid_handle e) {} } void DownloadingTorrents::addTorrent(QString hash) { - if(BTSession->isFinished(hash)){ - BTSession->setUnfinishedTorrent(hash); - } QTorrentHandle h = BTSession->getTorrentHandle(hash); int row = getRowFromHash(hash); qDebug("DL: addTorrent(): %s, row: %d", (const char*)hash.toUtf8(), row); @@ -797,37 +774,6 @@ void DownloadingTorrents::loadLastSortedColumn() { } } -// Called when a torrent is added -void DownloadingTorrents::torrentAdded(QTorrentHandle& h) { - QString hash = h.hash(); - if(BTSession->isFinished(hash)) { - return; - } - if(getRowFromHash(hash) != -1) return; - int row = DLListModel->rowCount(); - // Adding torrent to download list - DLListModel->insertRow(row); - DLListModel->setData(DLListModel->index(row, NAME), QVariant(h.name())); - DLListModel->setData(DLListModel->index(row, SIZE), QVariant((qlonglong)h.actual_size())); - DLListModel->setData(DLListModel->index(row, DLSPEED), QVariant((double)0.)); - DLListModel->setData(DLListModel->index(row, UPSPEED), QVariant((double)0.)); - DLListModel->setData(DLListModel->index(row, SEEDSLEECH), QVariant(QString::fromUtf8("0/0"))); - DLListModel->setData(DLListModel->index(row, RATIO), QVariant(misc::toQString(BTSession->getRealRatio(hash)))); - DLListModel->setData(DLListModel->index(row, ETA), QVariant((qlonglong)-1)); - DLListModel->setData(DLListModel->index(row, HASH), QVariant(hash)); - // Pause torrent if it was paused last time - if(h.is_paused()) { - DLListModel->setData(DLListModel->index(row, PROGRESS), QVariant((double)BTSession->getUncheckedTorrentProgress(hash))); - DLListModel->setData(DLListModel->index(row, NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/skin/paused.png"))), Qt::DecorationRole); - setRowColor(row, QString::fromUtf8("red")); - }else{ - DLListModel->setData(DLListModel->index(row, NAME), QVariant(QIcon(QString::fromUtf8(":/Icons/skin/stalled.png"))), Qt::DecorationRole); - setRowColor(row, QString::fromUtf8("grey")); - } - ++nbTorrents; - emit unfinishedTorrentsNumberChanged(nbTorrents); -} - void DownloadingTorrents::updateFileSizeAndProgress(QString hash) { int row = getRowFromHash(hash); Q_ASSERT(row != -1); diff --git a/src/downloadingTorrents.h b/src/downloadingTorrents.h index 01f4190f2..a6da17442 100644 --- a/src/downloadingTorrents.h +++ b/src/downloadingTorrents.h @@ -58,7 +58,6 @@ class DownloadingTorrents : public QWidget, public Ui::downloading{ signals: void unfinishedTorrentsNumberChanged(unsigned int); void torrentDoubleClicked(QString hash, bool finished); - void torrentFinished(QString hash); protected slots: void on_actionSet_download_limit_triggered(); @@ -66,13 +65,11 @@ class DownloadingTorrents : public QWidget, public Ui::downloading{ void on_actionSet_upload_limit_triggered(); void displayDLListMenu(const QPoint& pos); void displayDLHoSMenu(const QPoint&); - void addTorrent(QString hash); void sortDownloadList(int index=-1, Qt::SortOrder startSortOrder=Qt::AscendingOrder); void toggleDownloadListSortOrder(int index); void sortDownloadListFloat(int index, Qt::SortOrder sortOrder); void sortDownloadListString(int index, Qt::SortOrder sortOrder); void saveColWidthDLList() const; - void torrentAdded(QTorrentHandle& h); void setRowColor(int row, QColor color); void showProperties(const QModelIndex &index); void hideOrShowColumnName(); @@ -87,7 +84,7 @@ class DownloadingTorrents : public QWidget, public Ui::downloading{ void forceRecheck(); public slots: - void updateDlList(); + void updateTorrent(QTorrentHandle h); void pauseTorrent(QString hash); void resumeTorrent(QString hash); void deleteTorrent(QString hash); @@ -97,6 +94,7 @@ class DownloadingTorrents : public QWidget, public Ui::downloading{ void hidePriorityColumn(bool hide); void sortProgressColumn(QString hash); void loadLastSortedColumn(); + void addTorrent(QString hash); }; diff --git a/src/httpserver.cpp b/src/httpserver.cpp index 74a63d1f6..27068c11b 100644 --- a/src/httpserver.cpp +++ b/src/httpserver.cpp @@ -32,16 +32,13 @@ HttpServer::HttpServer(bittorrent *_BTSession, int msec, QObject* parent) : QTcp BTSession = _BTSession; manager = new EventManager(this, BTSession); //add torrents - QStringList list = BTSession->getUnfinishedTorrents(); - foreach(QString hash, list) { - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(h.is_valid()) manager->addedTorrent(h); - } - list = BTSession->getFinishedTorrents(); - foreach(QString hash, list) { - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(h.is_valid()) manager->addedTorrent(h); - } + std::vector torrents = BTSession->getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(h.is_valid()) + manager->addedTorrent(h); + } //connect BTSession to manager connect(BTSession, SIGNAL(addedTorrent(QTorrentHandle&)), manager, SLOT(addedTorrent(QTorrentHandle&))); connect(BTSession, SIGNAL(deletedTorrent(QString)), manager, SLOT(deletedTorrent(QString))); @@ -74,17 +71,13 @@ void HttpServer::newHttpConnection() } } -void HttpServer::onTimer() -{ - QStringList list = BTSession->getUnfinishedTorrents(); - foreach(QString hash, list) { - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(h.is_valid()) manager->modifiedTorrent(h); - } - list = BTSession->getFinishedTorrents(); - foreach(QString hash, list) { - QTorrentHandle h = BTSession->getTorrentHandle(hash); - if(h.is_valid()) manager->modifiedTorrent(h); +void HttpServer::onTimer() { + std::vector torrents = BTSession->getTorrents(); + std::vector::iterator torrentIT; + for(torrentIT = torrents.begin(); torrentIT != torrents.end(); torrentIT++) { + QTorrentHandle h = QTorrentHandle(*torrentIT); + if(h.is_valid()) + manager->modifiedTorrent(h); } }