From d1515456bc6acf2c1b41da1fb85e136e69707d08 Mon Sep 17 00:00:00 2001 From: thalieht Date: Wed, 15 Jun 2022 08:16:01 +0300 Subject: [PATCH] Add checkbox for "Excluded file names" PR #17206. --- src/base/bittorrent/session.cpp | 38 ++++++++++++++++---- src/base/bittorrent/session.h | 3 ++ src/gui/addnewtorrentdialog.cpp | 25 +++++++------ src/gui/optionsdialog.cpp | 3 ++ src/gui/optionsdialog.ui | 19 +++++----- src/webui/api/appcontroller.cpp | 5 +++ src/webui/webapplication.h | 2 +- src/webui/www/private/views/preferences.html | 18 ++++++++-- 8 files changed, 83 insertions(+), 30 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index def8e5e05..20362e46c 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -414,6 +414,7 @@ Session::Session(QObject *parent) , m_peerTurnoverCutoff(BITTORRENT_SESSION_KEY(u"PeerTurnoverCutOff"_qs), 90) , m_peerTurnoverInterval(BITTORRENT_SESSION_KEY(u"PeerTurnoverInterval"_qs), 300) , m_requestQueueSize(BITTORRENT_SESSION_KEY(u"RequestQueueSize"_qs), 500) + , m_isExcludedFileNamesEnabled(BITTORRENT_KEY(u"ExcludedFileNamesEnabled"_qs), false) , m_excludedFileNames(BITTORRENT_SESSION_KEY(u"ExcludedFileNames"_qs)) , m_bannedIPs(u"State/BannedIPs"_qs , QStringList() @@ -467,7 +468,8 @@ Session::Session(QObject *parent) enqueueRefresh(); updateSeedingLimitTimer(); populateAdditionalTrackers(); - populateExcludedFileNamesRegExpList(); + if (isExcludedFileNamesEnabled()) + populateExcludedFileNamesRegExpList(); enableTracker(isTrackerEnabled()); @@ -2256,13 +2258,16 @@ bool Session::addTorrent_impl(const std::variant &source // Use qBittorrent default priority rather than libtorrent's (4) p.file_priorities = std::vector(internalFilesCount, LT::toNative(DownloadPriority::Normal)); - if (addTorrentParams.filePriorities.size() == 0) + if (addTorrentParams.filePriorities.isEmpty()) { - // Check file name blacklist when priorities are not explicitly set - for (int i = 0; i < filePaths.size(); ++i) + if (isExcludedFileNamesEnabled()) { - if (isFilenameExcluded(filePaths.at(i).filename())) - p.file_priorities[LT::toUnderlyingType(nativeIndexes[i])] = lt::dont_download; + // Check file name blacklist when priorities are not explicitly set + for (int i = 0; i < filePaths.size(); ++i) + { + if (isFilenameExcluded(filePaths.at(i).filename())) + p.file_priorities[LT::toUnderlyingType(nativeIndexes[i])] = lt::dont_download; + } } } else @@ -3091,6 +3096,24 @@ void Session::setIPFilterFile(const Path &path) } } +bool Session::isExcludedFileNamesEnabled() const +{ + return m_isExcludedFileNamesEnabled; +} + +void Session::setExcludedFileNamesEnabled(const bool enabled) +{ + if (m_isExcludedFileNamesEnabled == enabled) + return; + + m_isExcludedFileNamesEnabled = enabled; + + if (enabled) + populateExcludedFileNamesRegExpList(); + else + m_excludedFileNamesRegExpList.clear(); +} + QStringList Session::excludedFileNames() const { return m_excludedFileNames; @@ -3122,6 +3145,9 @@ void Session::populateExcludedFileNamesRegExpList() bool Session::isFilenameExcluded(const QString &fileName) const { + if (!isExcludedFileNamesEnabled()) + return false; + return std::any_of(m_excludedFileNamesRegExpList.begin(), m_excludedFileNamesRegExpList.end(), [&fileName](const QRegularExpression &re) { return re.match(fileName).hasMatch(); diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 5406eab2e..69b74392a 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -455,6 +455,8 @@ namespace BitTorrent void setBlockPeersOnPrivilegedPorts(bool enabled); bool isTrackerFilteringEnabled() const; void setTrackerFilteringEnabled(bool enabled); + bool isExcludedFileNamesEnabled() const; + void setExcludedFileNamesEnabled(const bool enabled); QStringList excludedFileNames() const; void setExcludedFileNames(const QStringList &newList); bool isFilenameExcluded(const QString &fileName) const; @@ -782,6 +784,7 @@ namespace BitTorrent CachedSettingValue m_peerTurnoverCutoff; CachedSettingValue m_peerTurnoverInterval; CachedSettingValue m_requestQueueSize; + CachedSettingValue m_isExcludedFileNamesEnabled; CachedSettingValue m_excludedFileNames; CachedSettingValue m_bannedIPs; CachedSettingValue m_resumeDataStorageType; diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 846289ccc..bf300e987 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -976,20 +976,23 @@ void AddNewTorrentDialog::setupTreeview() m_ui->contentTreeView->setExpanded(currentIndex, true); } - // Check file name blacklist for torrents that are manually added - QVector priorities = m_contentModel->model()->getFilePriorities(); - Q_ASSERT(priorities.size() == m_torrentInfo.filesCount()); - - for (int i = 0; i < priorities.size(); ++i) + if (BitTorrent::Session::instance()->isExcludedFileNamesEnabled()) { - if (priorities[i] == BitTorrent::DownloadPriority::Ignored) - continue; + // Check file name blacklist for torrents that are manually added + QVector priorities = m_contentModel->model()->getFilePriorities(); + Q_ASSERT(priorities.size() == m_torrentInfo.filesCount()); - if (BitTorrent::Session::instance()->isFilenameExcluded(m_torrentInfo.filePath(i).filename())) - priorities[i] = BitTorrent::DownloadPriority::Ignored; + for (int i = 0; i < priorities.size(); ++i) + { + if (priorities[i] == BitTorrent::DownloadPriority::Ignored) + continue; + + if (BitTorrent::Session::instance()->isFilenameExcluded(m_torrentInfo.filePath(i).filename())) + priorities[i] = BitTorrent::DownloadPriority::Ignored; + } + + m_contentModel->model()->updateFilesPriorities(priorities); } - - m_contentModel->model()->updateFilesPriorities(priorities); } updateDiskSpaceLabel(); diff --git a/src/gui/optionsdialog.cpp b/src/gui/optionsdialog.cpp index aab94e7f2..d5b79355c 100644 --- a/src/gui/optionsdialog.cpp +++ b/src/gui/optionsdialog.cpp @@ -375,6 +375,7 @@ OptionsDialog::OptionsDialog(QWidget *parent) connect(m_ui->checkUseDownloadPath, &QAbstractButton::toggled, this, &ThisType::enableApplyButton); connect(m_ui->checkUseDownloadPath, &QAbstractButton::toggled, m_ui->textDownloadPath, &QWidget::setEnabled); connect(m_ui->addWatchedFolderButton, &QAbstractButton::clicked, this, &ThisType::enableApplyButton); + connect(m_ui->groupExcludedFileNames, &QGroupBox::toggled, this, &ThisType::enableApplyButton); connect(m_ui->textExcludedFileNames, &QPlainTextEdit::textChanged, this, &ThisType::enableApplyButton); connect(m_ui->removeWatchedFolderButton, &QAbstractButton::clicked, this, &ThisType::enableApplyButton); connect(m_ui->groupMailNotification, &QGroupBox::toggled, this, &ThisType::enableApplyButton); @@ -758,6 +759,7 @@ void OptionsDialog::saveOptions() session->setTorrentContentLayout(static_cast(m_ui->contentLayoutComboBox->currentIndex())); auto watchedFoldersModel = static_cast(m_ui->scanFoldersView->model()); watchedFoldersModel->apply(); + session->setExcludedFileNamesEnabled(m_ui->groupExcludedFileNames->isChecked()); session->setExcludedFileNames(m_ui->textExcludedFileNames->toPlainText().split(u'\n', Qt::SkipEmptyParts)); session->setTorrentExportDirectory(getTorrentExportDir()); session->setFinishedTorrentExportDirectory(getFinishedTorrentExportDir()); @@ -1018,6 +1020,7 @@ void OptionsDialog::loadOptions() m_ui->checkAppendqB->setChecked(session->isAppendExtensionEnabled()); m_ui->checkPreallocateAll->setChecked(session->isPreallocationEnabled()); m_ui->checkRecursiveDownload->setChecked(!pref->recursiveDownloadDisabled()); + m_ui->groupExcludedFileNames->setChecked(session->isExcludedFileNamesEnabled()); m_ui->textExcludedFileNames->setPlainText(session->excludedFileNames().join(u'\n')); if (session->torrentExportDirectory().isEmpty()) diff --git a/src/gui/optionsdialog.ui b/src/gui/optionsdialog.ui index 75b7867f4..cf62259dd 100644 --- a/src/gui/optionsdialog.ui +++ b/src/gui/optionsdialog.ui @@ -1112,12 +1112,12 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually - - Use Category paths in Manual Mode - Resolve relative Save Path against appropriate Category path instead of Default one + + Use Category paths in Manual Mode + @@ -1266,14 +1266,13 @@ Manual: Various torrent properties (e.g. save path) must be assigned manually Excluded file names + + true + + + false + - - - - Filters: - - - diff --git a/src/webui/api/appcontroller.cpp b/src/webui/api/appcontroller.cpp index 68351d9b6..06da30405 100644 --- a/src/webui/api/appcontroller.cpp +++ b/src/webui/api/appcontroller.cpp @@ -141,6 +141,8 @@ void AppController::preferencesAction() data[u"scan_dirs"_qs] = nativeDirs; // === END DEPRECATED CODE === // + // Excluded file names + data[u"excluded_file_names_enabled"_qs] = session->isExcludedFileNamesEnabled(); data[u"excluded_file_names"_qs] = session->excludedFileNames().join(u'\n'); // Email notification upon download completion @@ -478,6 +480,9 @@ void AppController::setPreferencesAction() } // === END DEPRECATED CODE === // + // Excluded file names + if (hasKey(u"excluded_file_names_enabled"_qs)) + session->setExcludedFileNamesEnabled(it.value().toBool()); if (hasKey(u"excluded_file_names"_qs)) session->setExcludedFileNames(it.value().toString().split(u'\n')); diff --git a/src/webui/webapplication.h b/src/webui/webapplication.h index d6581e578..d92269b2f 100644 --- a/src/webui/webapplication.h +++ b/src/webui/webapplication.h @@ -48,7 +48,7 @@ #include "base/utils/version.h" #include "api/isessionmanager.h" -inline const Utils::Version API_VERSION {2, 8, 12}; +inline const Utils::Version API_VERSION {2, 8, 13}; class APIController; class AuthController; diff --git a/src/webui/www/private/views/preferences.html b/src/webui/www/private/views/preferences.html index de274d2d5..ed00e514f 100644 --- a/src/webui/www/private/views/preferences.html +++ b/src/webui/www/private/views/preferences.html @@ -131,8 +131,10 @@
- QBT_TR(Excluded file names)QBT_TR[CONTEXT=OptionsDialog] -
+ + + +
@@ -1326,6 +1328,7 @@ updateExportDirEnabled: updateExportDirEnabled, updateExportDirFinEnabled: updateExportDirFinEnabled, addWatchFolder: addWatchFolder, + updateExcludedFileNamesEnabled: updateExcludedFileNamesEnabled, changeWatchFolderSelect: changeWatchFolderSelect, updateMailNotification: updateMailNotification, updateMailAuthSettings: updateMailAuthSettings, @@ -1423,6 +1426,11 @@ return folders; }; + const updateExcludedFileNamesEnabled = function() { + const isAExcludedFileNamesEnabled = $('excludedFileNamesCheckbox').getProperty('checked'); + $('excludedFileNamesTextarea').setProperty('disabled', !isAExcludedFileNamesEnabled); + }; + const updateExportDirEnabled = function() { const isExportDirEnabled = $('exportdir_checkbox').getProperty('checked'); $('exportdir_text').setProperty('disabled', !isExportDirEnabled); @@ -1739,6 +1747,9 @@ addWatchFolder(folder, sel, other); } addWatchFolder(); + + // Excluded file names + $('excludedFileNamesCheckbox').setProperty('checked', pref.excluded_file_names_enabled); $('excludedFileNamesTextarea').setProperty('value', pref.excluded_file_names); // Email notification upon download completion @@ -2060,6 +2071,9 @@ // Automatically add torrents from settings.set('scan_dirs', getWatchedFolders()); + + // Excluded file names + settings.set('excluded_file_names_enabled', $('excludedFileNamesCheckbox').getProperty('checked')); settings.set('excluded_file_names', $('excludedFileNamesTextarea').getProperty('value')); // Email notification upon download completion