Browse Source

Load torrent from data directly

adaptive-webui-19844
Chocobo1 6 years ago
parent
commit
5c0d9b7eab
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 72
      src/gui/addnewtorrentdialog.cpp
  2. 6
      src/gui/addnewtorrentdialog.h

72
src/gui/addnewtorrentdialog.cpp

@ -102,6 +102,10 @@ AddNewTorrentDialog::AddNewTorrentDialog(const BitTorrent::AddTorrentParams &inP
m_ui->savePath->setDialogCaption(tr("Choose save path")); m_ui->savePath->setDialogCaption(tr("Choose save path"));
m_ui->savePath->setMaxVisibleItems(20); m_ui->savePath->setMaxVisibleItems(20);
#ifdef Q_OS_MAC
setModal(true);
#endif
auto session = BitTorrent::Session::instance(); auto session = BitTorrent::Session::instance();
if (m_torrentParams.addPaused == TriStateBool::True) if (m_torrentParams.addPaused == TriStateBool::True)
@ -233,10 +237,9 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
if (Net::DownloadManager::hasSupportedScheme(source)) { if (Net::DownloadManager::hasSupportedScheme(source)) {
// Launch downloader // Launch downloader
// TODO: Don't save loaded torrent to file, just use downloaded data!
Net::DownloadHandler *handler = Net::DownloadManager::instance()->download( Net::DownloadHandler *handler = Net::DownloadManager::instance()->download(
Net::DownloadRequest(source).limit(10485760 /* 10MB */).handleRedirectToMagnet(true).saveToFile(true)); Net::DownloadRequest(source).limit(10485760 /* 10MB */).handleRedirectToMagnet(true));
connect(handler, static_cast<void (Net::DownloadHandler::*)(const QString &, const QString &)>(&Net::DownloadHandler::downloadFinished) connect(handler, static_cast<void (Net::DownloadHandler::*)(const QString &, const QByteArray &)>(&Net::DownloadHandler::downloadFinished)
, dlg, &AddNewTorrentDialog::handleDownloadFinished); , dlg, &AddNewTorrentDialog::handleDownloadFinished);
connect(handler, &Net::DownloadHandler::downloadFailed, dlg, &AddNewTorrentDialog::handleDownloadFailed); connect(handler, &Net::DownloadHandler::downloadFailed, dlg, &AddNewTorrentDialog::handleDownloadFailed);
connect(handler, &Net::DownloadHandler::redirectedToMagnet, dlg, &AddNewTorrentDialog::handleRedirectedToMagnet); connect(handler, &Net::DownloadHandler::redirectedToMagnet, dlg, &AddNewTorrentDialog::handleRedirectedToMagnet);
@ -246,53 +249,42 @@ void AddNewTorrentDialog::show(const QString &source, const BitTorrent::AddTorre
const BitTorrent::MagnetUri magnetUri(source); const BitTorrent::MagnetUri magnetUri(source);
const bool isLoaded = magnetUri.isValid() const bool isLoaded = magnetUri.isValid()
? dlg->loadMagnet(magnetUri) ? dlg->loadMagnet(magnetUri)
: dlg->loadTorrent(source); : dlg->loadTorrentFile(source);
if (isLoaded) { if (isLoaded)
#ifdef Q_OS_MAC
dlg->exec();
#else
dlg->open(); dlg->open();
#endif else
}
else {
delete dlg; delete dlg;
} }
}
void AddNewTorrentDialog::show(const QString &source, QWidget *parent) void AddNewTorrentDialog::show(const QString &source, QWidget *parent)
{ {
show(source, BitTorrent::AddTorrentParams(), parent); show(source, BitTorrent::AddTorrentParams(), parent);
} }
bool AddNewTorrentDialog::loadTorrent(const QString &torrentPath) bool AddNewTorrentDialog::loadTorrentFile(const QString &torrentPath)
{ {
if (torrentPath.startsWith("file://", Qt::CaseInsensitive)) const QString decodedPath = torrentPath.startsWith("file://", Qt::CaseInsensitive)
m_filePath = QUrl::fromEncoded(torrentPath.toLocal8Bit()).toLocalFile(); ? QUrl::fromEncoded(torrentPath.toLocal8Bit()).toLocalFile()
else : torrentPath;
m_filePath = torrentPath;
if (!QFile::exists(m_filePath)) { QString error;
RaisedMessageBox::critical(this, tr("I/O Error"), tr("The torrent file '%1' does not exist.").arg(Utils::Fs::toNativePath(m_filePath))); 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(decodedPath), error));
return false; return false;
} }
QFileInfo fileinfo(m_filePath); m_torrentGuard.reset(new TorrentFileGuard(decodedPath));
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; return loadTorrentImpl();
QString error;
m_torrentInfo = BitTorrent::TorrentInfo::loadFromFile(m_filePath, &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));
return false;
} }
m_torrentGuard.reset(new TorrentFileGuard(m_filePath)); bool AddNewTorrentDialog::loadTorrentImpl()
{
m_hasMetadata = true;
m_hash = m_torrentInfo.hash(); m_hash = m_torrentInfo.hash();
// Prevent showing the dialog if download is already present // 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) void AddNewTorrentDialog::handleRedirectedToMagnet(const QString &url, const QString &magnetUri)
{ {
Q_UNUSED(url) Q_UNUSED(url)
if (loadMagnet(BitTorrent::MagnetUri(magnetUri))) if (loadMagnet(BitTorrent::MagnetUri(magnetUri)))
open(); open();
else else
this->deleteLater(); this->deleteLater();
} }
void AddNewTorrentDialog::handleDownloadFinished(const QString &url, const QString &filePath) void AddNewTorrentDialog::handleDownloadFinished(const QString &url, const QByteArray &data)
{ {
Q_UNUSED(url) QString error;
if (loadTorrent(filePath)) 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(); open();
else else
this->deleteLater(); this->deleteLater();

6
src/gui/addnewtorrentdialog.h

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

Loading…
Cancel
Save