From 07eb2619911b15a1083113978a5d57a93d487952 Mon Sep 17 00:00:00 2001 From: thalieht Date: Wed, 18 Sep 2019 19:26:01 +0300 Subject: [PATCH] Add "Remove torrent and its files" option to share ratio limiting --- src/base/bittorrent/session.cpp | 45 ++++++++++++-------- src/base/bittorrent/session.h | 19 ++++++--- src/gui/optionsdialog.cpp | 18 +++++++- src/gui/optionsdialog.ui | 11 +++-- src/gui/transferlistwidget.cpp | 10 +++-- src/webui/api/torrentscontroller.cpp | 7 +-- src/webui/www/private/views/preferences.html | 26 ++++++++--- 7 files changed, 97 insertions(+), 39 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 5eb5e1841..8edd480fb 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -1508,6 +1508,10 @@ void Session::processShareLimits() LogMsg(tr("'%1' reached the maximum ratio you set. Removed.").arg(torrent->name())); deleteTorrent(torrent->hash()); } + else if (m_maxRatioAction == DeleteFiles) { + LogMsg(tr("'%1' reached the maximum ratio you set. Removed torrent and its files.").arg(torrent->name())); + deleteTorrent(torrent->hash(), TorrentAndFiles); + } else if ((m_maxRatioAction == Pause) && !torrent->isPaused()) { torrent->pause(); LogMsg(tr("'%1' reached the maximum ratio you set. Paused.").arg(torrent->name())); @@ -1535,6 +1539,10 @@ void Session::processShareLimits() LogMsg(tr("'%1' reached the maximum seeding time you set. Removed.").arg(torrent->name())); deleteTorrent(torrent->hash()); } + else if (m_maxRatioAction == DeleteFiles) { + LogMsg(tr("'%1' reached the maximum seeding time you set. Removed torrent and its files.").arg(torrent->name())); + deleteTorrent(torrent->hash(), TorrentAndFiles); + } else if ((m_maxRatioAction == Pause) && !torrent->isPaused()) { torrent->pause(); LogMsg(tr("'%1' reached the maximum seeding time you set. Paused.").arg(torrent->name())); @@ -1616,8 +1624,8 @@ void Session::banIP(const QString &ip) } // Delete a torrent from the session, given its hash -// deleteLocalFiles = true means that the torrent will be removed from the hard-drive too -bool Session::deleteTorrent(const QString &hash, const bool deleteLocalFiles) +// and from the disk, if the corresponding deleteOption is chosen +bool Session::deleteTorrent(const QString &hash, const DeleteOption deleteOption) { TorrentHandle *const torrent = m_torrents.take(hash); if (!torrent) return false; @@ -1626,20 +1634,8 @@ bool Session::deleteTorrent(const QString &hash, const bool deleteLocalFiles) emit torrentAboutToBeRemoved(torrent); // Remove it from session - if (deleteLocalFiles) { - const QString rootPath = torrent->rootPath(true); - if (!rootPath.isEmpty()) - // torrent with root folder - m_removingTorrents[torrent->hash()] = {torrent->name(), rootPath, deleteLocalFiles}; - else if (torrent->useTempPath()) - // torrent without root folder still has it in its temporary save path - m_removingTorrents[torrent->hash()] = {torrent->name(), torrent->savePath(true), deleteLocalFiles}; - else - m_removingTorrents[torrent->hash()] = {torrent->name(), "", deleteLocalFiles}; - m_nativeSession->remove_torrent(torrent->nativeHandle(), lt::session::delete_files); - } - else { - m_removingTorrents[torrent->hash()] = {torrent->name(), "", deleteLocalFiles}; + if (deleteOption == Torrent) { + m_removingTorrents[torrent->hash()] = {torrent->name(), "", deleteOption}; QStringList unwantedFiles; if (torrent->hasMetadata()) unwantedFiles = torrent->absoluteFilePathsUnwanted(); @@ -1653,6 +1649,21 @@ bool Session::deleteTorrent(const QString &hash, const bool deleteLocalFiles) QDir().rmdir(parentFolder); } } + else { + const QString rootPath = torrent->rootPath(true); + if (!rootPath.isEmpty()) { + // torrent with root folder + m_removingTorrents[torrent->hash()] = {torrent->name(), rootPath, deleteOption}; + } + else if (torrent->useTempPath()) { + // torrent without root folder still has it in its temporary save path + m_removingTorrents[torrent->hash()] = {torrent->name(), torrent->savePath(true), deleteOption}; + } + else { + m_removingTorrents[torrent->hash()] = {torrent->name(), "", deleteOption}; + } + m_nativeSession->remove_torrent(torrent->nativeHandle(), lt::session::delete_files); + } // Remove it from torrent resume directory const QDir resumeDataDir(m_resumeFolderPath); @@ -4007,7 +4018,7 @@ void Session::handleTorrentRemovedAlert(const lt::torrent_removed_alert *p) if (m_removingTorrents.contains(infoHash)) { const RemovingTorrentData tmpRemovingTorrentData = m_removingTorrents[infoHash]; - if (!tmpRemovingTorrentData.requestedFileDeletion) { + if (tmpRemovingTorrentData.deleteOption == Torrent) { LogMsg(tr("'%1' was removed from the transfer list.", "'xxx.avi' was removed...").arg(tmpRemovingTorrentData.name)); m_removingTorrents.remove(infoHash); } diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index f6a03e508..4e37c1377 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -59,11 +59,20 @@ class BandwidthScheduler; class Statistics; class ResumeDataSavingManager; +// These values should remain unchanged when adding new items +// so as not to break the existing user settings. enum MaxRatioAction { - Pause, - Remove, - EnableSuperSeeding + Pause = 0, + Remove = 1, + DeleteFiles = 3, + EnableSuperSeeding = 2 +}; + +enum DeleteOption +{ + Torrent, + TorrentAndFiles }; enum TorrentExportFolder @@ -413,7 +422,7 @@ namespace BitTorrent bool isKnownTorrent(const InfoHash &hash) const; bool addTorrent(const QString &source, const AddTorrentParams ¶ms = AddTorrentParams()); bool addTorrent(const TorrentInfo &torrentInfo, const AddTorrentParams ¶ms = AddTorrentParams()); - bool deleteTorrent(const QString &hash, bool deleteLocalFiles = false); + bool deleteTorrent(const QString &hash, DeleteOption deleteOption = Torrent); bool loadMetadata(const MagnetUri &magnetUri); bool cancelLoadMetadata(const InfoHash &hash); @@ -506,7 +515,7 @@ namespace BitTorrent { QString name; QString savePathToRemove; - bool requestedFileDeletion; + DeleteOption deleteOption; }; explicit Session(QObject *parent = nullptr); diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index 02bd69fe6..95cb6c604 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -747,7 +747,14 @@ void OptionsDialog::saveOptions() session->setAdditionalTrackers(m_ui->textTrackers->toPlainText()); session->setGlobalMaxRatio(getMaxRatio()); session->setGlobalMaxSeedingMinutes(getMaxSeedingMinutes()); - session->setMaxRatioAction(static_cast(m_ui->comboRatioLimitAct->currentIndex())); + + const QVector actIndex = { + Pause, + Remove, + DeleteFiles, + EnableSuperSeeding + }; + session->setMaxRatioAction(actIndex.value(m_ui->comboRatioLimitAct->currentIndex())); // End Bittorrent preferences // Misc preferences @@ -1130,7 +1137,14 @@ void OptionsDialog::loadOptions() m_ui->spinMaxSeedingMinutes->setEnabled(false); } m_ui->comboRatioLimitAct->setEnabled((session->globalMaxSeedingMinutes() >= 0) || (session->globalMaxRatio() >= 0.)); - m_ui->comboRatioLimitAct->setCurrentIndex(session->maxRatioAction()); + + const QHash actIndex = { + {Pause, 0}, + {Remove, 1}, + {DeleteFiles, 2}, + {EnableSuperSeeding, 3} + }; + m_ui->comboRatioLimitAct->setCurrentIndex(actIndex.value(session->maxRatioAction())); // End Bittorrent preferences // Web UI preferences diff --git a/src/gui/optionsdialog.ui b/src/gui/optionsdialog.ui index 28b3eafaf..91890270c 100644 --- a/src/gui/optionsdialog.ui +++ b/src/gui/optionsdialog.ui @@ -2485,17 +2485,22 @@ - Pause them + Pause torrent - Remove them + Remove torrent - Enable super seeding for them + Remove torrent and its files + + + + + Enable super seeding for torrent diff --git a/src/gui/transferlistwidget.cpp b/src/gui/transferlistwidget.cpp index 5f250b947..64073a2bd 100644 --- a/src/gui/transferlistwidget.cpp +++ b/src/gui/transferlistwidget.cpp @@ -371,8 +371,9 @@ void TransferListWidget::deleteSelectedTorrents(bool deleteLocalFiles) if (Preferences::instance()->confirmTorrentDeletion() && !DeletionConfirmationDialog::askForDeletionConfirmation(this, deleteLocalFiles, torrents.size(), torrents[0]->name())) return; - for (BitTorrent::TorrentHandle *const torrent : torrents) - BitTorrent::Session::instance()->deleteTorrent(torrent->hash(), deleteLocalFiles); + const DeleteOption deleteOption = deleteLocalFiles ? TorrentAndFiles : Torrent; + for (const BitTorrent::TorrentHandle *torrent : torrents) + BitTorrent::Session::instance()->deleteTorrent(torrent->hash(), deleteOption); } void TransferListWidget::deleteVisibleTorrents() @@ -388,8 +389,9 @@ void TransferListWidget::deleteVisibleTorrents() && !DeletionConfirmationDialog::askForDeletionConfirmation(this, deleteLocalFiles, torrents.size(), torrents[0]->name())) return; - for (BitTorrent::TorrentHandle *const torrent : asConst(torrents)) - BitTorrent::Session::instance()->deleteTorrent(torrent->hash(), deleteLocalFiles); + const DeleteOption deleteOption = deleteLocalFiles ? TorrentAndFiles : Torrent; + for (const BitTorrent::TorrentHandle *torrent : asConst(torrents)) + BitTorrent::Session::instance()->deleteTorrent(torrent->hash(), deleteOption); } void TransferListWidget::increaseQueuePosSelectedTorrents() diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 553cf3da0..f75c894f6 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -885,10 +885,11 @@ void TorrentsController::deleteAction() checkParams({"hashes", "deleteFiles"}); const QStringList hashes {params()["hashes"].split('|')}; - const bool deleteFiles {parseBool(params()["deleteFiles"], false)}; - applyToTorrents(hashes, [deleteFiles](BitTorrent::TorrentHandle *const torrent) + const DeleteOption deleteOption = parseBool(params()["deleteFiles"], false) + ? TorrentAndFiles : Torrent; + applyToTorrents(hashes, [deleteOption](const BitTorrent::TorrentHandle *torrent) { - BitTorrent::Session::instance()->deleteTorrent(torrent->hash(), deleteFiles); + BitTorrent::Session::instance()->deleteTorrent(torrent->hash(), deleteOption); }); } diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index 80032e135..27b0ede88 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -577,9 +577,10 @@ @@ -1660,8 +1661,23 @@ $('max_seeding_time_value').setProperty('value', pref.max_seeding_time.toInt()); else $('max_seeding_time_value').setProperty('value', 1440); - const max_ratio_act = pref.max_ratio_act.toInt(); - $('max_ratio_act').getChildren('option')[max_ratio_act].setAttribute('selected', ''); + let maxRatioAct = 0; + switch (pref.max_ratio_act.toInt()) { + case 0: // Pause + default: + maxRatioAct = 0; + break; + case 1: // Remove + maxRatioAct = 1; + break; + case 2: // Enable super seeding + maxRatioAct = 3; + break; + case 3: // Remove torrent and files + maxRatioAct = 2; + break; + } + $('max_ratio_act').getChildren('option')[maxRatioAct].setAttribute('selected', ''); updateMaxRatioTimeEnabled(); // Add trackers