Skip to content

Commit

Permalink
Allow to remove tracker from tracker filter widget menu
Browse files Browse the repository at this point in the history
PR #19681.
Closes #11100.
  • Loading branch information
glassez authored Oct 11, 2023
1 parent ec90d16 commit 30189ae
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 5 deletions.
13 changes: 13 additions & 0 deletions src/base/preferences.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1580,6 +1580,19 @@ void Preferences::setConfirmMergeTrackers(const bool enabled)
setValue(u"GUI/ConfirmActions/MergeTrackers"_s, enabled);
}

bool Preferences::confirmRemoveTrackerFromAllTorrents() const
{
return value(u"GUI/ConfirmActions/RemoveTrackerFromAllTorrents"_s, true);
}

void Preferences::setConfirmRemoveTrackerFromAllTorrents(const bool enabled)
{
if (enabled == confirmRemoveTrackerFromAllTorrents())
return;

setValue(u"GUI/ConfirmActions/RemoveTrackerFromAllTorrents"_s, enabled);
}

#ifndef Q_OS_MACOS
TrayIcon::Style Preferences::trayIconStyle() const
{
Expand Down
2 changes: 2 additions & 0 deletions src/base/preferences.h
Original file line number Diff line number Diff line change
Expand Up @@ -321,6 +321,8 @@ class Preferences final : public QObject
void setConfirmPauseAndResumeAll(bool enabled);
bool confirmMergeTrackers() const;
void setConfirmMergeTrackers(bool enabled);
bool confirmRemoveTrackerFromAllTorrents() const;
void setConfirmRemoveTrackerFromAllTorrents(bool enabled);
#ifndef Q_OS_MACOS
bool systemTrayEnabled() const;
void setSystemTrayEnabled(bool enabled);
Expand Down
7 changes: 7 additions & 0 deletions src/gui/advancedsettings.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ namespace
NOTIFICATION_TIMEOUT,
#endif
CONFIRM_REMOVE_ALL_TAGS,
CONFIRM_REMOVE_TRACKER_FROM_ALL_TORRENTS,
REANNOUNCE_WHEN_ADDRESS_CHANGED,
DOWNLOAD_TRACKER_FAVICON,
SAVE_PATH_HISTORY_LENGTH,
Expand All @@ -100,6 +101,7 @@ namespace
TRACKER_PORT,
TRACKER_PORT_FORWARDING,
PYTHON_EXECUTABLE_PATH,

// libtorrent section
LIBTORRENT_HEADER,
BDECODE_DEPTH_LIMIT,
Expand Down Expand Up @@ -327,6 +329,7 @@ void AdvancedSettings::saveAdvancedSettings() const
pref->setConfirmTorrentRecheck(m_checkBoxConfirmTorrentRecheck.isChecked());

pref->setConfirmRemoveAllTags(m_checkBoxConfirmRemoveAllTags.isChecked());
pref->setConfirmRemoveTrackerFromAllTorrents(m_checkBoxConfirmRemoveTrackerFromAllTorrents.isChecked());

session->setAnnounceToAllTrackers(m_checkBoxAnnounceAllTrackers.isChecked());
session->setAnnounceToAllTiers(m_checkBoxAnnounceAllTiers.isChecked());
Expand Down Expand Up @@ -838,6 +841,10 @@ void AdvancedSettings::loadAdvancedSettings()
m_checkBoxConfirmRemoveAllTags.setChecked(pref->confirmRemoveAllTags());
addRow(CONFIRM_REMOVE_ALL_TAGS, tr("Confirm removal of all tags"), &m_checkBoxConfirmRemoveAllTags);

// Remove tracker from all torrents confirmation
m_checkBoxConfirmRemoveTrackerFromAllTorrents.setChecked(pref->confirmRemoveTrackerFromAllTorrents());
addRow(CONFIRM_REMOVE_TRACKER_FROM_ALL_TORRENTS, tr("Confirm removal of tracker from all torrents"), &m_checkBoxConfirmRemoveTrackerFromAllTorrents);

// Announce to all trackers in a tier
m_checkBoxAnnounceAllTrackers.setChecked(session->announceToAllTrackers());
addRow(ANNOUNCE_ALL_TRACKERS, (tr("Always announce to all trackers in a tier")
Expand Down
2 changes: 1 addition & 1 deletion src/gui/advancedsettings.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ private slots:
m_checkBoxProgramNotifications, m_checkBoxTorrentAddedNotifications, m_checkBoxReannounceWhenAddressChanged, m_checkBoxTrackerFavicon, m_checkBoxTrackerStatus,
m_checkBoxTrackerPortForwarding, m_checkBoxConfirmTorrentRecheck, m_checkBoxConfirmRemoveAllTags, m_checkBoxAnnounceAllTrackers, m_checkBoxAnnounceAllTiers,
m_checkBoxMultiConnectionsPerIp, m_checkBoxValidateHTTPSTrackerCertificate, m_checkBoxSSRFMitigation, m_checkBoxBlockPeersOnPrivilegedPorts, m_checkBoxPieceExtentAffinity,
m_checkBoxSuggestMode, m_checkBoxSpeedWidgetEnabled, m_checkBoxIDNSupport;
m_checkBoxSuggestMode, m_checkBoxSpeedWidgetEnabled, m_checkBoxIDNSupport, m_checkBoxConfirmRemoveTrackerFromAllTorrents;
QComboBox m_comboBoxInterface, m_comboBoxInterfaceAddress, m_comboBoxDiskIOReadMode, m_comboBoxDiskIOWriteMode, m_comboBoxUtpMixedMode, m_comboBoxChokingAlgorithm,
m_comboBoxSeedChokingAlgorithm, m_comboBoxResumeDataStorage;
QLineEdit m_pythonExecutablePath, m_lineEditAnnounceIP, m_lineEditDHTBootstrapNodes;
Expand Down
67 changes: 63 additions & 4 deletions src/gui/transferlistfilters/trackersfilterwidget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,11 @@

#include "trackersfilterwidget.h"

#include <QCheckBox>
#include <QIcon>
#include <QListWidgetItem>
#include <QMenu>
#include <QMessageBox>
#include <QUrl>

#include "base/algorithm.h"
Expand All @@ -54,7 +56,7 @@ namespace
OTHERERROR_ROW,
WARNING_ROW,

NUM_ROWS
NUM_SPECIAL_ROWS
};

const QString NULL_HOST = u""_s;
Expand Down Expand Up @@ -262,10 +264,10 @@ void TrackersFilterWidget::addItems(const QString &trackerURL, const QVector<Bit
return;
}

Q_ASSERT(count() >= NUM_ROWS);
Q_ASSERT(count() >= NUM_SPECIAL_ROWS);
const Utils::Compare::NaturalLessThan<Qt::CaseSensitive> naturalLessThan {};
int insPos = count();
for (int i = NUM_ROWS; i < count(); ++i)
for (int i = NUM_SPECIAL_ROWS; i < count(); ++i)
{
if (naturalLessThan(host, item(i)->text()))
{
Expand Down Expand Up @@ -469,6 +471,28 @@ void TrackersFilterWidget::downloadFavicon(const QString &trackerHost, const QSt
downloadingFaviconNode.insert(trackerHost);
}

void TrackersFilterWidget::removeTracker(const QString &tracker)
{
for (const BitTorrent::TorrentID &torrentID : asConst(m_trackers.value(tracker).torrents))
{
auto *torrent = BitTorrent::Session::instance()->getTorrent(torrentID);
Q_ASSERT(torrent);
if (!torrent) [[unlikely]]
continue;

QStringList trackersToRemove;
for (const BitTorrent::TrackerEntry &trackerEntry : asConst(torrent->trackers()))
{
if ((trackerEntry.url == tracker) || (QUrl(trackerEntry.url).host() == tracker))
trackersToRemove.append(trackerEntry.url);
}

torrent->removeTrackers({trackersToRemove});
}

updateGeometry();
}

void TrackersFilterWidget::handleFavicoDownloadFinished(const Net::DownloadResult &result)
{
const QSet<QString> trackerHosts = m_downloadingFavicons.take(result.url);
Expand Down Expand Up @@ -537,6 +561,13 @@ void TrackersFilterWidget::showMenu()
QMenu *menu = new QMenu(this);
menu->setAttribute(Qt::WA_DeleteOnClose);

if (currentRow() >= NUM_SPECIAL_ROWS)
{
menu->addAction(UIThemeManager::instance()->getIcon(u"edit-clear"_s, u"list-remove"_s), tr("Remove tracker")
, this, &TrackersFilterWidget::onRemoveTrackerTriggered);
menu->addSeparator();
}

menu->addAction(UIThemeManager::instance()->getIcon(u"torrent-start"_s, u"media-playback-start"_s), tr("Resume torrents")
, transferList(), &TransferListWidget::startVisibleTorrents);
menu->addAction(UIThemeManager::instance()->getIcon(u"torrent-stop"_s, u"media-playback-pause"_s), tr("Pause torrents")
Expand Down Expand Up @@ -593,6 +624,34 @@ void TrackersFilterWidget::torrentAboutToBeDeleted(BitTorrent::Torrent *const to
item(ALL_ROW)->setText(formatItemText(ALL_ROW, --m_totalTorrents));
}

void TrackersFilterWidget::onRemoveTrackerTriggered()
{
const int row = currentRow();
if (row < NUM_SPECIAL_ROWS)
return;

const QString &tracker = trackerFromRow(row);
if (!Preferences::instance()->confirmRemoveTrackerFromAllTorrents())
{
removeTracker(tracker);
return;
}

auto *confirmBox = new QMessageBox(QMessageBox::Question, tr("Removal confirmation")
, tr("Are you sure you want to remove tracker \"%1\" from all torrents?").arg(tracker)
, (QMessageBox::Yes | QMessageBox::No), this);
confirmBox->setCheckBox(new QCheckBox(tr("Don't ask me again.")));
confirmBox->setAttribute(Qt::WA_DeleteOnClose);
connect(confirmBox, &QDialog::accepted, this, [this, confirmBox, tracker]
{
removeTracker(tracker);

if (confirmBox->checkBox()->isChecked())
Preferences::instance()->setConfirmRemoveTrackerFromAllTorrents(false);
});
confirmBox->open();
}

QString TrackersFilterWidget::trackerFromRow(int row) const
{
Q_ASSERT(row > 1);
Expand All @@ -606,7 +665,7 @@ QString TrackersFilterWidget::trackerFromRow(int row) const
int TrackersFilterWidget::rowFromTracker(const QString &tracker) const
{
Q_ASSERT(!tracker.isEmpty());
for (int i = NUM_ROWS; i < count(); ++i)
for (int i = NUM_SPECIAL_ROWS; i < count(); ++i)
{
if (tracker == trackerFromRow(i))
return i;
Expand Down
3 changes: 3 additions & 0 deletions src/gui/transferlistfilters/trackersfilterwidget.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,15 @@ private slots:
void handleTorrentsLoaded(const QVector<BitTorrent::Torrent *> &torrents) override;
void torrentAboutToBeDeleted(BitTorrent::Torrent *torrent) override;

void onRemoveTrackerTriggered();

void addItems(const QString &trackerURL, const QVector<BitTorrent::TorrentID> &torrents);
void removeItem(const QString &trackerURL, const BitTorrent::TorrentID &id);
QString trackerFromRow(int row) const;
int rowFromTracker(const QString &tracker) const;
QSet<BitTorrent::TorrentID> getTorrentIDs(int row) const;
void downloadFavicon(const QString &trackerHost, const QString &faviconURL);
void removeTracker(const QString &tracker);

struct TrackerData
{
Expand Down

0 comments on commit 30189ae

Please sign in to comment.