Load torrent from data directly

This commit is contained in:
Chocobo1 2019-02-13 22:41:38 +08:00
parent 78ab0e4ba9
commit 5c0d9b7eab
No known key found for this signature in database
GPG key ID: 210D9C873253A68C
2 changed files with 41 additions and 39 deletions

View file

@ -102,6 +102,10 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
m_ui->savePath->setDialogCaption(tr("Choose save path"));
m_ui->savePath->setMaxVisibleItems(20);
#ifdef Q_OS_MAC
setModal(true);
#endif
auto session = BitTorrent::Session::instance();
if (m_torrentParams.addPaused == TriStateBool::True)
@ -233,10 +237,9 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
if (Net::DownloadManager::hasSupportedScheme(source)) {
// Launch downloader
// TODO: Don't save loaded torrent to file, just use downloaded data!
Net::DownloadHandler *handler = Net::DownloadManager::instance()->download(
Net::DownloadRequest(source).limit(10485760 /* 10MB */).handleRedirectToMagnet(true).saveToFile(true));
connect(handler, static_cast<void (Net::DownloadHandler::*)(const QString &, const QString &)>(&Net::DownloadHandler::downloadFinished)
Net::DownloadRequest(source).limit(10485760 /* 10MB */).handleRedirectToMagnet(true));
connect(handler, static_cast<void (Net::DownloadHandler::*)(const QString &, const QByteArray &)>(&Net::DownloadHandler::downloadFinished)
, dlg, &AddNewTorrentDialog::handleDownloadFinished);
connect(handler, &Net::DownloadHandler::downloadFailed, dlg, &AddNewTorrentDialog::handleDownloadFailed);
connect(handler, &Net::DownloadHandler::redirectedToMagnet, dlg, &AddNewTorrentDialog::handleRedirectedToMagnet);
@ -246,18 +249,12 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
const BitTorrent::MagnetUri magnetUri(source);
const bool isLoaded = magnetUri.isValid()
? dlg->loadMagnet(magnetUri)
: dlg->loadTorrent(source);
: dlg->loadTorrentFile(source);
if (isLoaded) {
#ifdef Q_OS_MAC
dlg->exec();
#else
if (isLoaded)
dlg->open();
#endif
}
else {
else
delete dlg;
}
}
void AddNewTorrentDialog::show(const QString &source, QWidget *parent)
@ -265,34 +262,29 @@ void AddNewTorrentDialog::show(const QString &source, QWidget *parent)
show(source, BitTorrent::AddTorrentParams(), parent);
}
bool AddNewTorrentDialog::loadTorrent(const QString &torrentPath)
bool AddNewTorrentDialog::loadTorrentFile(const QString &torrentPath)
{
if (torrentPath.startsWith("file://", Qt::CaseInsensitive))
m_filePath = QUrl::fromEncoded(torrentPath.toLocal8Bit()).toLocalFile();
else
m_filePath = torrentPath;
const QString decodedPath = torrentPath.startsWith("file://", Qt::CaseInsensitive)
? QUrl::fromEncoded(torrentPath.toLocal8Bit()).toLocalFile()
: torrentPath;
if (!QFile::exists(m_filePath)) {
RaisedMessageBox::critical(this, tr("I/O Error"), tr("The torrent file '%1' does not exist.").arg(Utils::Fs::toNativePath(m_filePath)));
return false;
}
QFileInfo fileinfo(m_filePath);
if (!fileinfo.isReadable()) {
RaisedMessageBox::critical(this, tr("I/O Error"), tr("The torrent file '%1' cannot be read from the disk. Probably you don't have enough permissions.").arg(Utils::Fs::toNativePath(m_filePath)));
return false;
}
m_hasMetadata = true;
QString error;
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(m_filePath, &error);
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(decodedPath, &error);
if (!m_torrentInfo.isValid()) {
RaisedMessageBox::critical(this, tr("Invalid torrent"), tr("Failed to load the torrent: %1.\nError: %2", "Don't remove the '\n' characters. They insert a newline.")
.arg(Utils::Fs::toNativePath(m_filePath), error));
RaisedMessageBox::critical(this, tr("Invalid torrent")
, tr("Failed to load the torrent: %1.\nError: %2", "Don't remove the '\n' characters. They insert a newline.")
.arg(Utils::Fs::toNativePath(decodedPath), error));
return false;
}
m_torrentGuard.reset(new TorrentFileGuard(m_filePath));
m_torrentGuard.reset(new TorrentFileGuard(decodedPath));
return loadTorrentImpl();
}
bool AddNewTorrentDialog::loadTorrentImpl()
{
m_hasMetadata = true;
m_hash = m_torrentInfo.hash();
// Prevent showing the dialog if download is already present
@ -779,16 +771,26 @@ void AddNewTorrentDialog::handleDownloadFailed(const QString &url, const QString
void AddNewTorrentDialog::handleRedirectedToMagnet(const QString &url, const QString &magnetUri)
{
Q_UNUSED(url)
if (loadMagnet(BitTorrent::MagnetUri(magnetUri)))
open();
else
this->deleteLater();
}
void AddNewTorrentDialog::handleDownloadFinished(const QString &url, const QString &filePath)
void AddNewTorrentDialog::handleDownloadFinished(const QString &url, const QByteArray &data)
{
Q_UNUSED(url)
if (loadTorrent(filePath))
QString error;
m_torrentInfo = BitTorrent::TorrentInfo::load(data, &error);
if (!m_torrentInfo.isValid()) {
RaisedMessageBox::critical(this, tr("Invalid torrent"), tr("Failed to load from URL: %1.\nError: %2")
.arg(url, error));
return;
}
m_torrentGuard.reset(new TorrentFileGuard);
if (loadTorrentImpl())
open();
else
this->deleteLater();

View file

@ -80,7 +80,7 @@ private slots:
void updateMetadata(const BitTorrent::TorrentInfo &info);
void handleDownloadFailed(const QString &url, const QString &reason);
void handleRedirectedToMagnet(const QString &url, const QString &magnetUri);
void handleDownloadFinished(const QString &url, const QString &filePath);
void handleDownloadFinished(const QString &url, const QByteArray &data);
void TMMChanged(int index);
void categoryChanged(int index);
void doNotDeleteTorrentClicked(bool checked);
@ -90,7 +90,8 @@ private slots:
private:
explicit AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inParams, QWidget *parent);
bool loadTorrent(const QString &torrentPath);
bool loadTorrentFile(const QString &torrentPath);
bool loadTorrentImpl();
bool loadMagnet(const BitTorrent::MagnetUri &magnetUri);
void populateSavePathComboBox();
void saveSavePathHistory() const;
@ -108,7 +109,6 @@ private:
TorrentContentFilterModel *m_contentModel;
PropListDelegate *m_contentDelegate;
bool m_hasMetadata;
QString m_filePath;
BitTorrent::InfoHash m_hash;
BitTorrent::TorrentInfo m_torrentInfo;
QByteArray m_headerState;