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>
34 #include <QStringView>
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
)
49 , m_ui(new Ui::TrackersAdditionDialog
)
51 , m_storeDialogSize(SETTINGS_KEY(u
"Size"_s
))
52 , m_storeTrackersListURL(SETTINGS_KEY(u
"TrackersListURL"_s
))
56 m_ui
->buttonBox
->button(QDialogButtonBox::Ok
)->setText(tr("Add"));
57 connect(m_ui
->buttonBox
, &QDialogButtonBox::accepted
, this, &QDialog::accept
);
58 connect(m_ui
->buttonBox
, &QDialogButtonBox::rejected
, this, &QDialog::reject
);
60 m_ui
->downloadButton
->setIcon(UIThemeManager::instance()->getIcon(u
"downloading"_s
, u
"download"_s
));
61 connect(m_ui
->downloadButton
, &QAbstractButton::clicked
, this, &TrackersAdditionDialog::onDownloadButtonClicked
);
63 connect(this, &QDialog::accepted
, this, &TrackersAdditionDialog::onAccepted
);
68 TrackersAdditionDialog::~TrackersAdditionDialog()
75 void TrackersAdditionDialog::onAccepted() const
77 const QVector
<BitTorrent::TrackerEntry
> entries
= BitTorrent::parseTrackerEntries(m_ui
->textEditTrackersList
->toPlainText());
78 m_torrent
->addTrackers(entries
);
81 void TrackersAdditionDialog::onDownloadButtonClicked()
83 const QString url
= m_ui
->lineEditListURL
->text();
86 QMessageBox::warning(this, tr("Trackers list URL error"), tr("The trackers list URL cannot be empty"));
90 // Just to show that it takes times
91 m_ui
->downloadButton
->setEnabled(false);
92 setCursor(Qt::WaitCursor
);
94 Net::DownloadManager::instance()->download(url
, Preferences::instance()->useProxyForGeneralPurposes()
95 , this, &TrackersAdditionDialog::onTorrentListDownloadFinished
);
98 void TrackersAdditionDialog::onTorrentListDownloadFinished(const Net::DownloadResult
&result
)
100 // Restore the cursor, buttons...
101 m_ui
->downloadButton
->setEnabled(true);
102 setCursor(Qt::ArrowCursor
);
104 if (result
.status
!= Net::DownloadStatus::Success
)
106 QMessageBox::warning(this, tr("Download trackers list error")
107 , tr("Error occurred when downloading the trackers list. Reason: \"%1\"").arg(result
.errorString
));
111 // Add fetched trackers to the list
112 const QString existingText
= m_ui
->textEditTrackersList
->toPlainText();
113 if (!existingText
.isEmpty() && !existingText
.endsWith(u
'\n'))
114 m_ui
->textEditTrackersList
->insertPlainText(u
"\n"_s
);
116 // append the data as-is
117 const auto trackers
= QString::fromUtf8(result
.data
).trimmed();
118 m_ui
->textEditTrackersList
->insertPlainText(trackers
);
121 void TrackersAdditionDialog::saveSettings()
123 m_storeDialogSize
= size();
124 m_storeTrackersListURL
= m_ui
->lineEditListURL
->text();
127 void TrackersAdditionDialog::loadSettings()
129 if (const QSize dialogSize
= m_storeDialogSize
; dialogSize
.isValid())
131 m_ui
->lineEditListURL
->setText(m_storeTrackersListURL
);