mirror of
https://github.com/qbittorrent/qBittorrent
synced 2025-07-16 02:03:07 -07:00
Load torrent from data directly
This commit is contained in:
parent
78ab0e4ba9
commit
5c0d9b7eab
2 changed files with 41 additions and 39 deletions
|
@ -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();
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue