mirror of
https://github.com/qbittorrent/qBittorrent.git
synced 2026-01-03 14:12:30 -06:00
Revise 'Add trackers' dialog
* Now it allow user to add tracker to different tier. * The downloaded trackers are now displayed as is (without modifying). * Now the dialog remember dialog size and last used URL. Closes #17692.
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
/*
|
||||
* Bittorrent Client using Qt and libtorrent.
|
||||
* Copyright (C) 2022 Mike Tzou (Chocobo1)
|
||||
* Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
|
||||
*
|
||||
* This program is free software; you can redistribute it and/or
|
||||
@@ -28,9 +29,10 @@
|
||||
|
||||
#include "trackersadditiondialog.h"
|
||||
|
||||
#include <QBuffer>
|
||||
#include <QMessageBox>
|
||||
#include <QStringList>
|
||||
#include <QSize>
|
||||
#include <QStringView>
|
||||
#include <QVector>
|
||||
|
||||
#include "base/bittorrent/torrent.h"
|
||||
#include "base/bittorrent/trackerentry.h"
|
||||
@@ -39,102 +41,87 @@
|
||||
#include "gui/uithememanager.h"
|
||||
#include "ui_trackersadditiondialog.h"
|
||||
|
||||
#define SETTINGS_KEY(name) u"AddTrackersDialog/" name
|
||||
|
||||
TrackersAdditionDialog::TrackersAdditionDialog(QWidget *parent, BitTorrent::Torrent *const torrent)
|
||||
: QDialog(parent)
|
||||
, m_ui(new Ui::TrackersAdditionDialog())
|
||||
, m_ui(new Ui::TrackersAdditionDialog)
|
||||
, m_torrent(torrent)
|
||||
, m_storeDialogSize(SETTINGS_KEY(u"Size"_qs))
|
||||
, m_storeTrackersListURL(SETTINGS_KEY(u"TrackersListURL"_qs))
|
||||
{
|
||||
m_ui->setupUi(this);
|
||||
// Icons
|
||||
m_ui->uTorrentListButton->setIcon(UIThemeManager::instance()->getIcon(u"downloading"_qs));
|
||||
|
||||
m_ui->downloadButton->setIcon(UIThemeManager::instance()->getIcon(u"downloading"_qs));
|
||||
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Add"));
|
||||
|
||||
connect(m_ui->downloadButton, &QAbstractButton::clicked, this, &TrackersAdditionDialog::onDownloadButtonClicked);
|
||||
connect(this, &QDialog::accepted, this, &TrackersAdditionDialog::onAccepted);
|
||||
|
||||
loadSettings();
|
||||
}
|
||||
|
||||
TrackersAdditionDialog::~TrackersAdditionDialog()
|
||||
{
|
||||
saveSettings();
|
||||
|
||||
delete m_ui;
|
||||
}
|
||||
|
||||
QStringList TrackersAdditionDialog::newTrackers() const
|
||||
void TrackersAdditionDialog::onAccepted() const
|
||||
{
|
||||
const QString plainText = m_ui->textEditTrackersList->toPlainText();
|
||||
|
||||
QStringList cleanTrackers;
|
||||
for (QStringView url : asConst(QStringView(plainText).split(u'\n')))
|
||||
{
|
||||
url = url.trimmed();
|
||||
if (!url.isEmpty())
|
||||
cleanTrackers << url.toString();
|
||||
}
|
||||
return cleanTrackers;
|
||||
const QVector<BitTorrent::TrackerEntry> entries = BitTorrent::parseTrackerEntries(m_ui->textEditTrackersList->toPlainText());
|
||||
m_torrent->addTrackers(entries);
|
||||
}
|
||||
|
||||
void TrackersAdditionDialog::on_uTorrentListButton_clicked()
|
||||
void TrackersAdditionDialog::onDownloadButtonClicked()
|
||||
{
|
||||
m_ui->uTorrentListButton->setEnabled(false);
|
||||
Net::DownloadManager::instance()->download(m_ui->lineEditListURL->text()
|
||||
, this, &TrackersAdditionDialog::torrentListDownloadFinished);
|
||||
// Just to show that it takes times
|
||||
setCursor(Qt::WaitCursor);
|
||||
}
|
||||
|
||||
void TrackersAdditionDialog::torrentListDownloadFinished(const Net::DownloadResult &result)
|
||||
{
|
||||
if (result.status != Net::DownloadStatus::Success)
|
||||
const QString url = m_ui->lineEditListURL->text();
|
||||
if (url.isEmpty())
|
||||
{
|
||||
// To restore the cursor ...
|
||||
setCursor(Qt::ArrowCursor);
|
||||
m_ui->uTorrentListButton->setEnabled(true);
|
||||
QMessageBox::warning(
|
||||
this, tr("Download error")
|
||||
, tr("The trackers list could not be downloaded, reason: %1")
|
||||
.arg(result.errorString), QMessageBox::Ok);
|
||||
QMessageBox::warning(this, tr("Trackers list URL error"), tr("The trackers list URL cannot be empty"));
|
||||
return;
|
||||
}
|
||||
|
||||
const QStringList trackersFromUser = m_ui->textEditTrackersList->toPlainText().split(u'\n');
|
||||
QVector<BitTorrent::TrackerEntry> existingTrackers = m_torrent->trackers();
|
||||
existingTrackers.reserve(trackersFromUser.size());
|
||||
for (const QString &userURL : trackersFromUser)
|
||||
{
|
||||
const BitTorrent::TrackerEntry userTracker {userURL};
|
||||
if (!existingTrackers.contains(userTracker))
|
||||
existingTrackers << userTracker;
|
||||
}
|
||||
// Just to show that it takes times
|
||||
m_ui->downloadButton->setEnabled(false);
|
||||
setCursor(Qt::WaitCursor);
|
||||
|
||||
// Add new trackers to the list
|
||||
if (!m_ui->textEditTrackersList->toPlainText().isEmpty() && !m_ui->textEditTrackersList->toPlainText().endsWith(u'\n'))
|
||||
m_ui->textEditTrackersList->insertPlainText(u"\n"_qs);
|
||||
int nb = 0;
|
||||
QBuffer buffer;
|
||||
buffer.setData(result.data);
|
||||
buffer.open(QBuffer::ReadOnly);
|
||||
while (!buffer.atEnd())
|
||||
{
|
||||
const auto line = QString::fromUtf8(buffer.readLine().trimmed());
|
||||
if (line.isEmpty()) continue;
|
||||
|
||||
BitTorrent::TrackerEntry newTracker {line};
|
||||
if (!existingTrackers.contains(newTracker))
|
||||
{
|
||||
m_ui->textEditTrackersList->insertPlainText(line + u'\n');
|
||||
++nb;
|
||||
}
|
||||
}
|
||||
|
||||
// To restore the cursor ...
|
||||
setCursor(Qt::ArrowCursor);
|
||||
m_ui->uTorrentListButton->setEnabled(true);
|
||||
// Display information message if necessary
|
||||
if (nb == 0)
|
||||
QMessageBox::information(this, tr("No change"), tr("No additional trackers were found."), QMessageBox::Ok);
|
||||
Net::DownloadManager::instance()->download(url, this, &TrackersAdditionDialog::onTorrentListDownloadFinished);
|
||||
}
|
||||
|
||||
QStringList TrackersAdditionDialog::askForTrackers(QWidget *parent, BitTorrent::Torrent *const torrent)
|
||||
void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadResult &result)
|
||||
{
|
||||
QStringList trackers;
|
||||
TrackersAdditionDialog dlg(parent, torrent);
|
||||
if (dlg.exec() == QDialog::Accepted)
|
||||
return dlg.newTrackers();
|
||||
// Restore the cursor, buttons...
|
||||
m_ui->downloadButton->setEnabled(true);
|
||||
setCursor(Qt::ArrowCursor);
|
||||
|
||||
return trackers;
|
||||
if (result.status != Net::DownloadStatus::Success)
|
||||
{
|
||||
QMessageBox::warning(this, tr("Download trackers list error")
|
||||
, tr("Error occurred when downloading the trackers list. Reason: \"%1\"").arg(result.errorString));
|
||||
return;
|
||||
}
|
||||
|
||||
// Add fetched trackers to the list
|
||||
const QString existingText = m_ui->textEditTrackersList->toPlainText();
|
||||
if (!existingText.isEmpty() && !existingText.endsWith(u'\n'))
|
||||
m_ui->textEditTrackersList->insertPlainText(u"\n"_qs);
|
||||
|
||||
// append the data as-is
|
||||
const auto trackers = QString::fromUtf8(result.data).trimmed();
|
||||
m_ui->textEditTrackersList->insertPlainText(trackers);
|
||||
}
|
||||
|
||||
void TrackersAdditionDialog::saveSettings()
|
||||
{
|
||||
m_storeDialogSize = size();
|
||||
m_storeTrackersListURL = m_ui->lineEditListURL->text();
|
||||
}
|
||||
|
||||
void TrackersAdditionDialog::loadSettings()
|
||||
{
|
||||
if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
|
||||
resize(dialogSize);
|
||||
m_ui->lineEditListURL->setText(m_storeTrackersListURL);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user