Allow to use proxy per subsystem
[qBittorrent.git] / src / gui / properties / trackersadditiondialog.cpp
blobed5f5df92cfcac05f8d91d4c71a08038ed8a9138
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2022 Mike Tzou (Chocobo1)
4 * Copyright (C) 2006 Christophe Dumez <chris@qbittorrent.org>
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "trackersadditiondialog.h"
32 #include <QMessageBox>
33 #include <QSize>
34 #include <QStringView>
35 #include <QVector>
37 #include "base/bittorrent/torrent.h"
38 #include "base/bittorrent/trackerentry.h"
39 #include "base/global.h"
40 #include "base/net/downloadmanager.h"
41 #include "base/preferences.h"
42 #include "gui/uithememanager.h"
43 #include "ui_trackersadditiondialog.h"
45 #define SETTINGS_KEY(name) u"AddTrackersDialog/" name
47 TrackersAdditionDialog::TrackersAdditionDialog(QWidget *parent, BitTorrent::Torrent *const torrent)
48 : QDialog(parent)
49 , m_ui(new Ui::TrackersAdditionDialog)
50 , m_torrent(torrent)
51 , m_storeDialogSize(SETTINGS_KEY(u"Size"_qs))
52 , m_storeTrackersListURL(SETTINGS_KEY(u"TrackersListURL"_qs))
54 m_ui->setupUi(this);
56 m_ui->downloadButton->setIcon(UIThemeManager::instance()->getIcon(u"downloading"_qs, u"download"_qs));
57 m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Add"));
59 connect(m_ui->downloadButton, &QAbstractButton::clicked, this, &TrackersAdditionDialog::onDownloadButtonClicked);
60 connect(this, &QDialog::accepted, this, &TrackersAdditionDialog::onAccepted);
62 loadSettings();
65 TrackersAdditionDialog::~TrackersAdditionDialog()
67 saveSettings();
69 delete m_ui;
72 void TrackersAdditionDialog::onAccepted() const
74 const QVector<BitTorrent::TrackerEntry> entries = BitTorrent::parseTrackerEntries(m_ui->textEditTrackersList->toPlainText());
75 m_torrent->addTrackers(entries);
78 void TrackersAdditionDialog::onDownloadButtonClicked()
80 const QString url = m_ui->lineEditListURL->text();
81 if (url.isEmpty())
83 QMessageBox::warning(this, tr("Trackers list URL error"), tr("The trackers list URL cannot be empty"));
84 return;
87 // Just to show that it takes times
88 m_ui->downloadButton->setEnabled(false);
89 setCursor(Qt::WaitCursor);
91 Net::DownloadManager::instance()->download(url, Preferences::instance()->useProxyForGeneralPurposes()
92 , this, &TrackersAdditionDialog::onTorrentListDownloadFinished);
95 void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadResult &result)
97 // Restore the cursor, buttons...
98 m_ui->downloadButton->setEnabled(true);
99 setCursor(Qt::ArrowCursor);
101 if (result.status != Net::DownloadStatus::Success)
103 QMessageBox::warning(this, tr("Download trackers list error")
104 , tr("Error occurred when downloading the trackers list. Reason: \"%1\"").arg(result.errorString));
105 return;
108 // Add fetched trackers to the list
109 const QString existingText = m_ui->textEditTrackersList->toPlainText();
110 if (!existingText.isEmpty() && !existingText.endsWith(u'\n'))
111 m_ui->textEditTrackersList->insertPlainText(u"\n"_qs);
113 // append the data as-is
114 const auto trackers = QString::fromUtf8(result.data).trimmed();
115 m_ui->textEditTrackersList->insertPlainText(trackers);
118 void TrackersAdditionDialog::saveSettings()
120 m_storeDialogSize = size();
121 m_storeTrackersListURL = m_ui->lineEditListURL->text();
124 void TrackersAdditionDialog::loadSettings()
126 if (const QSize dialogSize = m_storeDialogSize; dialogSize.isValid())
127 resize(dialogSize);
128 m_ui->lineEditListURL->setText(m_storeTrackersListURL);