From 19d77b0881dc777b7930106869854067e5b2faba Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 9 Sep 2020 15:08:14 +0800 Subject: [PATCH] Add support for creating v2 torrents --- src/base/bittorrent/torrentcreatorthread.cpp | 27 +++++++++ src/base/bittorrent/torrentcreatorthread.h | 21 ++++++- src/gui/torrentcreatordialog.cpp | 51 +++++++++++++++- src/gui/torrentcreatordialog.h | 16 +++-- src/gui/torrentcreatordialog.ui | 61 +++++++++++++++++++- 5 files changed, 165 insertions(+), 11 deletions(-) diff --git a/src/base/bittorrent/torrentcreatorthread.cpp b/src/base/bittorrent/torrentcreatorthread.cpp index 12a1d1e7e..87b7fd086 100644 --- a/src/base/bittorrent/torrentcreatorthread.cpp +++ b/src/base/bittorrent/torrentcreatorthread.cpp @@ -55,6 +55,21 @@ namespace { return !Utils::Fs::fileName(QString::fromStdString(f)).startsWith('.'); } + +#if (LIBTORRENT_VERSION_NUM >= 20000) + lt::create_flags_t toNativeTorrentFormatFlag(const BitTorrent::TorrentFormat torrentFormat) + { + switch (torrentFormat) { + case BitTorrent::TorrentFormat::V1: + return lt::create_torrent::v1_only; + case BitTorrent::TorrentFormat::Hybrid: + return {}; + case BitTorrent::TorrentFormat::V2: + return lt::create_torrent::v2_only; + } + return {}; + } +#endif } using namespace BitTorrent; @@ -131,8 +146,12 @@ void TorrentCreatorThread::run() if (isInterruptionRequested()) return; +#if (LIBTORRENT_VERSION_NUM >= 20000) + lt::create_torrent newTorrent {fs, m_params.pieceSize, toNativeTorrentFormatFlag(m_params.torrentFormat)}; +#else lt::create_torrent newTorrent(fs, m_params.pieceSize, m_params.paddedFileSizeLimit , (m_params.isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})); +#endif // Add url seeds for (QString seed : asConst(m_params.urlSeeds)) { @@ -198,7 +217,11 @@ void TorrentCreatorThread::run() } } +#if (LIBTORRENT_VERSION_NUM >= 20000) +int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const int pieceSize, const TorrentFormat torrentFormat) +#else int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const int pieceSize, const bool isAlignmentOptimized, const int paddedFileSizeLimit) +#endif { if (inputPath.isEmpty()) return 0; @@ -206,6 +229,10 @@ int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const i lt::file_storage fs; lt::add_files(fs, Utils::Fs::toNativePath(inputPath).toStdString(), fileFilter); +#if (LIBTORRENT_VERSION_NUM >= 20000) + return lt::create_torrent {fs, pieceSize, toNativeTorrentFormatFlag(torrentFormat)}.num_pieces(); +#else return lt::create_torrent(fs, pieceSize, paddedFileSizeLimit , (isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})).num_pieces(); +#endif } diff --git a/src/base/bittorrent/torrentcreatorthread.h b/src/base/bittorrent/torrentcreatorthread.h index 50c3779b3..5b6d3bc38 100644 --- a/src/base/bittorrent/torrentcreatorthread.h +++ b/src/base/bittorrent/torrentcreatorthread.h @@ -29,17 +29,32 @@ #ifndef BITTORRENT_TORRENTCREATORTHREAD_H #define BITTORRENT_TORRENTCREATORTHREAD_H +#include + #include #include namespace BitTorrent { +#if (LIBTORRENT_VERSION_NUM >= 20000) + enum class TorrentFormat + { + V1, + V2, + Hybrid + }; +#endif + struct TorrentCreatorParams { bool isPrivate; +#if (LIBTORRENT_VERSION_NUM >= 20000) + TorrentFormat torrentFormat; +#else bool isAlignmentOptimized; - int pieceSize; int paddedFileSizeLimit; +#endif + int pieceSize; QString inputPath; QString savePath; QString comment; @@ -58,8 +73,12 @@ namespace BitTorrent void create(const TorrentCreatorParams ¶ms); +#if (LIBTORRENT_VERSION_NUM >= 20000) + static int calculateTotalPieces(const QString &inputPath, const int pieceSize, const TorrentFormat torrentFormat); +#else static int calculateTotalPieces(const QString &inputPath , const int pieceSize, const bool isAlignmentOptimized, int paddedFileSizeLimit); +#endif protected: void run() override; diff --git a/src/gui/torrentcreatordialog.cpp b/src/gui/torrentcreatordialog.cpp index 20e324fa8..53a9a07fb 100644 --- a/src/gui/torrentcreatordialog.cpp +++ b/src/gui/torrentcreatordialog.cpp @@ -36,7 +36,6 @@ #include #include "base/bittorrent/session.h" -#include "base/bittorrent/torrentcreatorthread.h" #include "base/bittorrent/torrentinfo.h" #include "base/global.h" #include "base/utils/fs.h" @@ -54,8 +53,12 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defau , m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent")) , m_storeStartSeeding(SETTINGS_KEY("StartSeeding")) , m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio")) +#if (LIBTORRENT_VERSION_NUM >= 20000) + , m_storeTorrentFormat(SETTINGS_KEY("TorrentFormat"), 1) +#else , m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"), true) , m_paddedFileSizeLimit(SETTINGS_KEY("PaddedFileSizeLimit"), -1) +#endif , m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath()) , m_storeTrackerList(SETTINGS_KEY("TrackerList")) , m_storeWebSeedList(SETTINGS_KEY("WebSeedList")) @@ -81,6 +84,12 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defau loadSettings(); updateInputPath(defaultPath); +#if (LIBTORRENT_VERSION_NUM >= 20000) + m_ui->checkOptimizeAlignment->hide(); +#else + m_ui->widgetTorrentFormat->hide(); +#endif + show(); } @@ -118,11 +127,26 @@ int TorrentCreatorDialog::getPieceSize() const return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024; } +#if (LIBTORRENT_VERSION_NUM >= 20000) +BitTorrent::TorrentFormat TorrentCreatorDialog::getTorrentFormat() const +{ + switch (m_ui->comboTorrentFormat->currentIndex()) { + case 0: + return BitTorrent::TorrentFormat::V2; + case 1: + return BitTorrent::TorrentFormat::Hybrid; + case 2: + return BitTorrent::TorrentFormat::V1; + } + return BitTorrent::TorrentFormat::Hybrid; +} +#else int TorrentCreatorDialog::getPaddedFileSizeLimit() const { const int value = m_ui->spinPaddedFileSizeLimit->value(); return ((value >= 0) ? (value * 1024) : -1); } +#endif void TorrentCreatorDialog::dropEvent(QDropEvent *event) { @@ -173,8 +197,13 @@ void TorrentCreatorDialog::onCreateButtonClicked() .replace(QRegularExpression("\n\n[\n]+"), "\n\n").split('\n'); const BitTorrent::TorrentCreatorParams params { m_ui->checkPrivate->isChecked() +#if (LIBTORRENT_VERSION_NUM >= 20000) + , getTorrentFormat() +#else , m_ui->checkOptimizeAlignment->isChecked() - , getPieceSize(), getPaddedFileSizeLimit() + , getPaddedFileSizeLimit() +#endif + , getPieceSize() , input, destination , m_ui->txtComment->toPlainText() , m_ui->lineEditSource->text() @@ -230,10 +259,14 @@ void TorrentCreatorDialog::updateProgressBar(int progress) void TorrentCreatorDialog::updatePiecesCount() { const QString path = m_ui->textInputPath->text().trimmed(); +#if (LIBTORRENT_VERSION_NUM >= 20000) + const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces( + path, getPieceSize(), getTorrentFormat()); +#else const bool isAlignmentOptimized = m_ui->checkOptimizeAlignment->isChecked(); - const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path , getPieceSize(), isAlignmentOptimized, getPaddedFileSizeLimit()); +#endif m_ui->labelTotalPieces->setText(QString::number(count)); } @@ -251,8 +284,12 @@ void TorrentCreatorDialog::setInteractionEnabled(const bool enabled) const m_ui->checkStartSeeding->setEnabled(enabled); m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled); m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked()); +#if (LIBTORRENT_VERSION_NUM >= 20000) + m_ui->widgetTorrentFormat->setEnabled(enabled); +#else m_ui->checkOptimizeAlignment->setEnabled(enabled); m_ui->spinPaddedFileSizeLimit->setEnabled(enabled); +#endif } void TorrentCreatorDialog::saveSettings() @@ -263,8 +300,12 @@ void TorrentCreatorDialog::saveSettings() m_storePrivateTorrent = m_ui->checkPrivate->isChecked(); m_storeStartSeeding = m_ui->checkStartSeeding->isChecked(); m_storeIgnoreRatio = m_ui->checkIgnoreShareLimits->isChecked(); +#if (LIBTORRENT_VERSION_NUM >= 20000) + m_storeTorrentFormat = m_ui->comboTorrentFormat->currentIndex(); +#else m_storeOptimizeAlignment = m_ui->checkOptimizeAlignment->isChecked(); m_paddedFileSizeLimit = m_ui->spinPaddedFileSizeLimit->value(); +#endif m_storeTrackerList = m_ui->trackersList->toPlainText(); m_storeWebSeedList = m_ui->URLSeedsList->toPlainText(); @@ -283,8 +324,12 @@ void TorrentCreatorDialog::loadSettings() m_ui->checkStartSeeding->setChecked(m_storeStartSeeding); m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio); m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked()); +#if (LIBTORRENT_VERSION_NUM >= 20000) + m_ui->comboTorrentFormat->setCurrentIndex(m_storeTorrentFormat); +#else m_ui->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment); m_ui->spinPaddedFileSizeLimit->setValue(m_paddedFileSizeLimit); +#endif m_ui->trackersList->setPlainText(m_storeTrackerList); m_ui->URLSeedsList->setPlainText(m_storeWebSeedList); diff --git a/src/gui/torrentcreatordialog.h b/src/gui/torrentcreatordialog.h index b008056ab..f51130959 100644 --- a/src/gui/torrentcreatordialog.h +++ b/src/gui/torrentcreatordialog.h @@ -30,15 +30,13 @@ #ifndef TORRENTCREATORDIALOG_H #define TORRENTCREATORDIALOG_H +#include + #include +#include "base/bittorrent/torrentcreatorthread.h" #include "base/settingvalue.h" -namespace BitTorrent -{ - class TorrentCreatorThread; -} - namespace Ui { class TorrentCreatorDialog; @@ -71,7 +69,11 @@ private: void setInteractionEnabled(bool enabled) const; int getPieceSize() const; +#if (LIBTORRENT_VERSION_NUM >= 20000) + BitTorrent::TorrentFormat getTorrentFormat() const; +#else int getPaddedFileSizeLimit() const; +#endif Ui::TorrentCreatorDialog *m_ui; BitTorrent::TorrentCreatorThread *m_creatorThread; @@ -82,8 +84,12 @@ private: CachedSettingValue m_storePrivateTorrent; CachedSettingValue m_storeStartSeeding; CachedSettingValue m_storeIgnoreRatio; +#if (LIBTORRENT_VERSION_NUM >= 20000) + CachedSettingValue m_storeTorrentFormat; +#else CachedSettingValue m_storeOptimizeAlignment; CachedSettingValue m_paddedFileSizeLimit; +#endif CachedSettingValue m_storeLastAddPath; CachedSettingValue m_storeTrackerList; CachedSettingValue m_storeWebSeedList; diff --git a/src/gui/torrentcreatordialog.ui b/src/gui/torrentcreatordialog.ui index 3daca4c7f..90b931872 100644 --- a/src/gui/torrentcreatordialog.ui +++ b/src/gui/torrentcreatordialog.ui @@ -30,8 +30,8 @@ 0 0 - 574 - 649 + 560 + 668 @@ -120,6 +120,63 @@ Settings + + + + + 0 + + + 0 + + + 0 + + + 0 + + + + + Torrent format: + + + + + + + + V2 + + + + + Hybrid + + + + + V1 + + + + + + + + Qt::Horizontal + + + + 40 + 20 + + + + + + +