Merge pull request #13340 from glassez/fix-magnet

Improve adding torrent using Magnet URI
This commit is contained in:
Vladimir Golovnev
2020-09-09 10:06:39 +03:00
committed by GitHub
4 changed files with 48 additions and 81 deletions

View File

@@ -39,6 +39,7 @@
#include <QVector>
#include "base/bittorrent/downloadpriority.h"
#include "base/bittorrent/infohash.h"
#include "base/bittorrent/magneturi.h"
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrenthandle.h"
@@ -276,11 +277,11 @@ bool AddNewTorrentDialog::loadTorrentFile(const QString &torrentPath)
bool AddNewTorrentDialog::loadTorrentImpl()
{
m_hasMetadata = true;
m_hash = m_torrentInfo.hash();
const BitTorrent::InfoHash infoHash = m_torrentInfo.hash();
// Prevent showing the dialog if download is already present
if (BitTorrent::Session::instance()->isKnownTorrent(m_hash)) {
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(m_hash);
if (BitTorrent::Session::instance()->isKnownTorrent(infoHash)) {
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(infoHash);
if (torrent) {
if (torrent->isPrivate() || m_torrentInfo.isPrivate()) {
RaisedMessageBox::warning(this, tr("Torrent is already present"), tr("Torrent '%1' is already in the transfer list. Trackers haven't been merged because it is a private torrent.").arg(torrent->name()), QMessageBox::Ok);
@@ -297,7 +298,7 @@ bool AddNewTorrentDialog::loadTorrentImpl()
return false;
}
m_ui->labelHashData->setText(m_hash);
m_ui->labelHashData->setText(infoHash);
setupTreeview();
TMMChanged(m_ui->comboTTM->currentIndex());
m_ui->keepTopLevelFolderCheckBox->setEnabled(m_torrentInfo.hasRootFolder());
@@ -312,10 +313,11 @@ bool AddNewTorrentDialog::loadMagnet(const BitTorrent::MagnetUri &magnetUri)
}
m_torrentGuard = std::make_unique<TorrentFileGuard>();
m_hash = magnetUri.hash();
const BitTorrent::InfoHash infoHash = magnetUri.hash();
// Prevent showing the dialog if download is already present
if (BitTorrent::Session::instance()->isKnownTorrent(m_hash)) {
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(m_hash);
if (BitTorrent::Session::instance()->isKnownTorrent(infoHash)) {
BitTorrent::TorrentHandle *const torrent = BitTorrent::Session::instance()->findTorrent(infoHash);
if (torrent) {
if (torrent->isPrivate()) {
RaisedMessageBox::warning(this, tr("Torrent is already present"), tr("Torrent '%1' is already in the transfer list. Trackers haven't been merged because it is a private torrent.").arg(torrent->name()), QMessageBox::Ok);
@@ -335,7 +337,7 @@ bool AddNewTorrentDialog::loadMagnet(const BitTorrent::MagnetUri &magnetUri)
connect(BitTorrent::Session::instance(), &BitTorrent::Session::metadataLoaded, this, &AddNewTorrentDialog::updateMetadata);
// Set dialog title
QString torrentName = magnetUri.name();
const QString torrentName = magnetUri.name();
setWindowTitle(torrentName.isEmpty() ? tr("Magnet link") : torrentName);
setupTreeview();
@@ -343,8 +345,9 @@ bool AddNewTorrentDialog::loadMagnet(const BitTorrent::MagnetUri &magnetUri)
BitTorrent::Session::instance()->loadMetadata(magnetUri);
setMetadataProgressIndicator(true, tr("Retrieving metadata..."));
m_ui->labelHashData->setText(m_hash);
m_ui->labelHashData->setText(infoHash);
m_magnetURI = magnetUri;
return true;
}
@@ -573,7 +576,7 @@ void AddNewTorrentDialog::accept()
// Add torrent
if (!m_hasMetadata)
BitTorrent::Session::instance()->addTorrent(m_hash, m_torrentParams);
BitTorrent::Session::instance()->addTorrent(m_magnetURI, m_torrentParams);
else
BitTorrent::Session::instance()->addTorrent(m_torrentInfo, m_torrentParams);
@@ -585,26 +588,26 @@ void AddNewTorrentDialog::reject()
{
if (!m_hasMetadata) {
setMetadataProgressIndicator(false);
BitTorrent::Session::instance()->cancelLoadMetadata(m_hash);
BitTorrent::Session::instance()->cancelLoadMetadata(m_magnetURI.hash());
}
QDialog::reject();
}
void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &info)
void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &metadata)
{
if (info.hash() != m_hash) return;
if (metadata.hash() != m_magnetURI.hash()) return;
disconnect(BitTorrent::Session::instance(), &BitTorrent::Session::metadataLoaded, this, &AddNewTorrentDialog::updateMetadata);
if (!info.isValid()) {
if (!metadata.isValid()) {
RaisedMessageBox::critical(this, tr("I/O Error"), ("Invalid metadata."));
setMetadataProgressIndicator(false, tr("Invalid metadata"));
return;
}
// Good to go
m_torrentInfo = info;
m_torrentInfo = metadata;
m_hasMetadata = true;
setMetadataProgressIndicator(true, tr("Parsing metadata..."));

View File

@@ -34,13 +34,13 @@
#include <QDialog>
#include "base/bittorrent/addtorrentparams.h"
#include "base/bittorrent/infohash.h"
#include "base/bittorrent/magneturi.h"
#include "base/bittorrent/torrentinfo.h"
#include "base/settingvalue.h"
namespace BitTorrent
{
class MagnetUri;
class InfoHash;
}
namespace Net
@@ -82,7 +82,7 @@ private slots:
void displayContentTreeMenu(const QPoint &);
void updateDiskSpaceLabel();
void onSavePathChanged(const QString &newPath);
void updateMetadata(const BitTorrent::TorrentInfo &info);
void updateMetadata(const BitTorrent::TorrentInfo &metadata);
void handleDownloadFinished(const Net::DownloadResult &result);
void TMMChanged(int index);
void categoryChanged(int index);
@@ -112,7 +112,7 @@ private:
TorrentContentFilterModel *m_contentModel;
PropListDelegate *m_contentDelegate;
bool m_hasMetadata;
BitTorrent::InfoHash m_hash;
BitTorrent::MagnetUri m_magnetURI;
BitTorrent::TorrentInfo m_torrentInfo;
QByteArray m_headerState;
int m_oldIndex;