From bf6d51039d061b33aade5b1951271034fe3f7e32 Mon Sep 17 00:00:00 2001 From: "Vladimir Golovnev (Glassez)" Date: Mon, 28 Jun 2021 16:21:50 +0300 Subject: [PATCH] Use torrent info with hashes for creating .torrent file --- src/base/bittorrent/session.cpp | 42 ++++++++++++++++++++++----------- src/base/bittorrent/session.h | 8 +------ src/gui/addnewtorrentdialog.cpp | 10 ++++++-- src/gui/addnewtorrentdialog.ui | 2 +- 4 files changed, 38 insertions(+), 24 deletions(-) diff --git a/src/base/bittorrent/session.cpp b/src/base/bittorrent/session.cpp index 72444f43a17..f8f8feaefc6 100644 --- a/src/base/bittorrent/session.cpp +++ b/src/base/bittorrent/session.cpp @@ -2296,28 +2296,25 @@ bool Session::downloadMetadata(const MagnetUri &magnetUri) return true; } -void Session::exportTorrentFile(const Torrent *torrent, TorrentExportFolder folder) +void Session::exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName) { - Q_ASSERT(((folder == TorrentExportFolder::Regular) && !torrentExportDirectory().isEmpty()) || - ((folder == TorrentExportFolder::Finished) && !finishedTorrentExportDirectory().isEmpty())); - - const QString validName = Utils::Fs::toValidFileSystemName(torrent->name()); + const QString validName = Utils::Fs::toValidFileSystemName(baseName); QString torrentExportFilename = QString::fromLatin1("%1.torrent").arg(validName); - const QDir exportPath(folder == TorrentExportFolder::Regular ? torrentExportDirectory() : finishedTorrentExportDirectory()); - if (exportPath.exists() || exportPath.mkpath(exportPath.absolutePath())) + const QDir exportDir {folderPath}; + if (exportDir.exists() || exportDir.mkpath(exportDir.absolutePath())) { - QString newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename); + QString newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename); int counter = 0; while (QFile::exists(newTorrentPath)) { // Append number to torrent name to make it unique torrentExportFilename = QString::fromLatin1("%1 %2.torrent").arg(validName).arg(++counter); - newTorrentPath = exportPath.absoluteFilePath(torrentExportFilename); + newTorrentPath = exportDir.absoluteFilePath(torrentExportFilename); } try { - torrent->info().saveToFile(newTorrentPath); + torrentInfo.saveToFile(newTorrentPath); } catch (const RuntimeError &err) { @@ -3893,7 +3890,14 @@ void Session::handleTorrentMetadataReceived(TorrentImpl *const torrent) { // Copy the torrent file to the export folder if (!torrentExportDirectory().isEmpty()) - exportTorrentFile(torrent); + { +#if (LIBTORRENT_VERSION_NUM >= 20000) + const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file_with_hashes()}; +#else + const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file()}; +#endif + exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name()); + } emit torrentMetadataReceived(torrent); } @@ -3944,7 +3948,14 @@ void Session::handleTorrentFinished(TorrentImpl *const torrent) // Move .torrent file to another folder if (!finishedTorrentExportDirectory().isEmpty()) - exportTorrentFile(torrent, TorrentExportFolder::Finished); + { +#if (LIBTORRENT_VERSION_NUM >= 20000) + const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file_with_hashes()}; +#else + const TorrentInfo torrentInfo {torrent->nativeHandle().torrent_file()}; +#endif + exportTorrentFile(torrentInfo, finishedTorrentExportDirectory(), torrent->name()); + } if (!hasUnfinishedTorrents()) emit allTorrentsFinished(); @@ -4452,7 +4463,10 @@ void Session::createTorrent(const lt::torrent_handle &nativeHandle) { // Copy the torrent file to the export folder if (!torrentExportDirectory().isEmpty()) - exportTorrentFile(torrent); + { + const TorrentInfo torrentInfo {params.ltAddTorrentParams.ti}; + exportTorrentFile(torrentInfo, torrentExportDirectory(), torrent->name()); + } } if (isAddTrackersEnabled() && !torrent->isPrivate()) @@ -4579,7 +4593,7 @@ void Session::handleMetadataReceivedAlert(const lt::metadata_received_alert *p) if (downloadedMetadataIter != m_downloadedMetadata.end()) { - TorrentInfo metadata {p->handle.torrent_file()}; + const TorrentInfo metadata {p->handle.torrent_file()}; m_downloadedMetadata.erase(downloadedMetadataIter); --m_extraLimit; diff --git a/src/base/bittorrent/session.h b/src/base/bittorrent/session.h index 05d4c9f47ea..bff355c59ad 100644 --- a/src/base/bittorrent/session.h +++ b/src/base/bittorrent/session.h @@ -82,12 +82,6 @@ enum DeleteOption DeleteTorrentAndFiles }; -enum TorrentExportFolder -{ - Regular, - Finished -}; - namespace Net { struct DownloadResult; @@ -613,7 +607,7 @@ namespace BitTorrent bool addTorrent_impl(const std::variant &source, const AddTorrentParams &addTorrentParams); void updateSeedingLimitTimer(); - void exportTorrentFile(const Torrent *torrent, TorrentExportFolder folder = TorrentExportFolder::Regular); + void exportTorrentFile(const TorrentInfo &torrentInfo, const QString &folderPath, const QString &baseName); void handleAlert(const lt::alert *a); void dispatchTorrentAlert(const lt::alert *a); diff --git a/src/gui/addnewtorrentdialog.cpp b/src/gui/addnewtorrentdialog.cpp index 63af9c6be20..c9d46631319 100644 --- a/src/gui/addnewtorrentdialog.cpp +++ b/src/gui/addnewtorrentdialog.cpp @@ -460,7 +460,7 @@ void AddNewTorrentDialog::saveTorrentFile() Q_ASSERT(m_hasMetadata); const QString torrentFileExtension {C_TORRENT_FILE_EXTENSION}; - const QString filter {QString{"Torrent file (*%1)"}.arg(torrentFileExtension)}; + const QString filter {tr("Torrent file (*%1)").arg(torrentFileExtension)}; QString path = QFileDialog::getSaveFileName( this, tr("Save as torrent file") @@ -679,6 +679,13 @@ void AddNewTorrentDialog::updateMetadata(const BitTorrent::TorrentInfo &metadata // Update UI setupTreeview(); setMetadataProgressIndicator(false, tr("Metadata retrieval complete")); + + m_ui->buttonSave->setVisible(true); + if (m_torrentInfo.infoHash().v2().isValid()) + { + m_ui->buttonSave->setEnabled(false); + m_ui->buttonSave->setToolTip(tr("Cannot create v2 torrent until its data is fully downloaded.")); + } } void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, const QString &labelText) @@ -687,7 +694,6 @@ void AddNewTorrentDialog::setMetadataProgressIndicator(bool visibleIndicator, co m_ui->lblMetaLoading->setVisible(true); m_ui->lblMetaLoading->setText(labelText); m_ui->progMetaLoading->setVisible(visibleIndicator); - m_ui->buttonSave->setVisible(!visibleIndicator); } void AddNewTorrentDialog::setupTreeview() diff --git a/src/gui/addnewtorrentdialog.ui b/src/gui/addnewtorrentdialog.ui index 64321122a11..9cb7c7150c2 100644 --- a/src/gui/addnewtorrentdialog.ui +++ b/src/gui/addnewtorrentdialog.ui @@ -355,7 +355,7 @@ - Info hash v2 + Info hash v2: