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"
33 #include <QMessageBox>
35 #include <QStringView>
37 #include "base/bittorrent/torrent.h"
38 #include "base/bittorrent/trackerentry.h"
39 #include "base/bittorrent/trackerentrystatus.h"
40 #include "base/global.h"
41 #include "base/net/downloadmanager.h"
42 #include "base/preferences.h"
43 #include "base/utils/number.h"
44 #include "gui/uithememanager.h"
45 #include "ui_trackersadditiondialog.h"
47 #define SETTINGS_KEY(name) u"AddTrackersDialog/" name
49 TrackersAdditionDialog::TrackersAdditionDialog(QWidget
*parent
, BitTorrent::Torrent
*const torrent
)
51 , m_ui(new Ui::TrackersAdditionDialog
)
53 , m_storeDialogSize(SETTINGS_KEY(u
"Size"_s
))
54 , m_storeTrackersListURL(SETTINGS_KEY(u
"TrackersListURL"_s
))
58 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setText(tr("Add"));
59 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &QDialog::accept
);
60 connect(m_ui
->buttonBox
, &QDialogButtonBox::rejected
, this, &QDialog::reject
);
62 m_ui
->downloadButton
->setIcon(UIThemeManager::instance()->getIcon(u
"downloading"_s
, u
"download"_s
));
63 connect(m_ui
->downloadButton
, &QAbstractButton::clicked
, this, &TrackersAdditionDialog::onDownloadButtonClicked
);
65 connect(this, &QDialog::accepted
, this, &TrackersAdditionDialog::onAccepted
);
70 TrackersAdditionDialog::~TrackersAdditionDialog()
77 void TrackersAdditionDialog::onAccepted() const
79 const QList
<BitTorrent::TrackerEntryStatus
> currentTrackers
= m_torrent
->trackers();
80 const int baseTier
= !currentTrackers
.isEmpty() ? (currentTrackers
.last().tier
+ 1) : 0;
82 QList
<BitTorrent::TrackerEntry
> entries
= BitTorrent::parseTrackerEntries(m_ui
->textEditTrackersList
->toPlainText());
83 for (BitTorrent::TrackerEntry
&entry
: entries
)
84 entry
.tier
= Utils::Number::clampingAdd(entry
.tier
, baseTier
);
86 m_torrent
->addTrackers(entries
);
89 void TrackersAdditionDialog::onDownloadButtonClicked()
91 const QString url
= m_ui
->lineEditListURL
->text();
94 QMessageBox::warning(this, tr("Trackers list URL error"), tr("The trackers list URL cannot be empty"));
98 // Just to show that it takes times
99 m_ui
->downloadButton
->setEnabled(false);
100 setCursor(Qt::WaitCursor
);
102 Net::DownloadManager::instance()->download(url
, Preferences::instance()->useProxyForGeneralPurposes()
103 , this, &TrackersAdditionDialog::onTorrentListDownloadFinished
);
106 void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadResult
&result
)
108 // Restore the cursor, buttons...
109 m_ui
->downloadButton
->setEnabled(true);
110 setCursor(Qt::ArrowCursor
);
112 if (result
.status
!= Net::DownloadStatus::Success
)
114 QMessageBox::warning(this, tr("Download trackers list error")
115 , tr("Error occurred when downloading the trackers list. Reason: \"%1\"").arg(result
.errorString
));
119 // Add fetched trackers to the list
120 const QString existingText
= m_ui
->textEditTrackersList
->toPlainText();
121 if (!existingText
.isEmpty() && !existingText
.endsWith(u
'\n'))
122 m_ui
->textEditTrackersList
->insertPlainText(u
"\n"_s
);
124 // append the data as-is
125 const auto trackers
= QString::fromUtf8(result
.data
).trimmed();
126 m_ui
->textEditTrackersList
->insertPlainText(trackers
);
129 void TrackersAdditionDialog::saveSettings()
131 m_storeDialogSize
= size();
132 m_storeTrackersListURL
= m_ui
->lineEditListURL
->text();
135 void TrackersAdditionDialog::loadSettings()
137 if (const QSize dialogSize
= m_storeDialogSize
; dialogSize
.isValid())
139 m_ui
->lineEditListURL
->setText(m_storeTrackersListURL
);