From 4d2015cfeda95be0fd9f6f9628ab77d48c741d17 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Sun, 3 Sep 2023 14:31:32 +0800 Subject: [PATCH] Support creating .torrent with larger piece size Warning: users are at their own discretion to create .torrent with >= 64 MiB piece size as not every torrent client supports it. Larger piece sizes are only available when using libtorrent 2.x. libtorrent 1.x is not efficient with memory usage and in order to avoid user complaints it is limited to 128 MiB. Also note that, as of this writing, libtorrent 2.0.9 has an internal limitation that only allows loading maximum 256 MiB piece size. And therefore > 256 MiB size options are forbidden for now. Closes #19527. PR #19535. --- src/base/utils/misc.cpp | 6 ++- src/base/utils/misc.h | 2 +- src/gui/torrentcreatordialog.cpp | 30 +++++++++++---- src/gui/torrentcreatordialog.ui | 65 -------------------------------- 4 files changed, 27 insertions(+), 76 deletions(-) diff --git a/src/base/utils/misc.cpp b/src/base/utils/misc.cpp index 303d22b7f..2622ff47d 100644 --- a/src/base/utils/misc.cpp +++ b/src/base/utils/misc.cpp @@ -259,12 +259,14 @@ QString Utils::Misc::unitString(const SizeUnit unit, const bool isSpeed) return ret; } -QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed) +QString Utils::Misc::friendlyUnit(const qint64 bytes, const bool isSpeed, const int precision) { const std::optional result = splitToFriendlyUnit(bytes); if (!result) return QCoreApplication::translate("misc", "Unknown", "Unknown (size)"); - return Utils::String::fromDouble(result->value, friendlyUnitPrecision(result->unit)) + + const int digitPrecision = (precision >= 0) ? precision : friendlyUnitPrecision(result->unit); + return Utils::String::fromDouble(result->value, digitPrecision) + C_NON_BREAKING_SPACE + unitString(result->unit, isSpeed); } diff --git a/src/base/utils/misc.h b/src/base/utils/misc.h index a56e488bf..d58ae50f3 100644 --- a/src/base/utils/misc.h +++ b/src/base/utils/misc.h @@ -74,7 +74,7 @@ namespace Utils::Misc // return the best user friendly storage unit (B, KiB, MiB, GiB, TiB) // value must be given in bytes - QString friendlyUnit(qint64 bytes, bool isSpeed = false); + QString friendlyUnit(qint64 bytes, bool isSpeed = false, int precision = -1); int friendlyUnitPrecision(SizeUnit unit); qint64 sizeInBytes(qreal size, SizeUnit unit); diff --git a/src/gui/torrentcreatordialog.cpp b/src/gui/torrentcreatordialog.cpp index 749bf21d4..27dda0be0 100644 --- a/src/gui/torrentcreatordialog.cpp +++ b/src/gui/torrentcreatordialog.cpp @@ -39,6 +39,7 @@ #include "base/bittorrent/torrentdescriptor.h" #include "base/global.h" #include "base/utils/fs.h" +#include "base/utils/misc.h" #include "ui_torrentcreatordialog.h" #include "utils.h" @@ -80,6 +81,18 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const Path &defaultP { m_ui->setupUi(this); + m_ui->comboPieceSize->addItem(tr("Auto"), 0); +#ifdef QBT_USES_LIBTORRENT2 + for (int i = 4; i <= 18; ++i) +#else + for (int i = 4; i <= 17; ++i) +#endif + { + const int size = 1024 << i; + const QString displaySize = Utils::Misc::friendlyUnit(size, false, 0); + m_ui->comboPieceSize->addItem(displaySize, size); + } + m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent")); m_ui->textInputPath->setMode(FileSystemPathEdit::Mode::ReadOnly); @@ -133,8 +146,7 @@ void TorrentCreatorDialog::onAddFileButtonClicked() int TorrentCreatorDialog::getPieceSize() const { - const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384, 32768}; // base unit in KiB - return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024; + return m_ui->comboPieceSize->currentData().toInt(); } #ifdef QBT_USES_LIBTORRENT2 @@ -251,15 +263,19 @@ void TorrentCreatorDialog::handleCreationFailure(const QString &msg) void TorrentCreatorDialog::handleCreationSuccess(const Path &path, const Path &branchPath) { - // Remove busy cursor setCursor(QCursor(Qt::ArrowCursor)); + setInteractionEnabled(true); + + QMessageBox::information(this, tr("Torrent creator") + , u"%1\n%2"_s.arg(tr("Torrent created:"), path.toString())); + if (m_ui->checkStartSeeding->isChecked()) { - // Create save path temp data const auto loadResult = BitTorrent::TorrentDescriptor::loadFromFile(path); if (!loadResult) { - QMessageBox::critical(this, tr("Torrent creation failed"), tr("Reason: Created torrent is invalid. It won't be added to download list.")); + const QString message = tr("Add torrent to transfer list failed.") + u'\n' + tr("Reason: \"%1\"").arg(loadResult.error()); + QMessageBox::critical(this, tr("Add torrent failed"), message); return; } @@ -276,9 +292,6 @@ void TorrentCreatorDialog::handleCreationSuccess(const Path &path, const Path &b BitTorrent::Session::instance()->addTorrent(loadResult.value(), params); } - QMessageBox::information(this, tr("Torrent creator") - , u"%1\n%2"_s.arg(tr("Torrent created:"), path.toString())); - setInteractionEnabled(true); } void TorrentCreatorDialog::updateProgressBar(int progress) @@ -308,6 +321,7 @@ void TorrentCreatorDialog::setInteractionEnabled(const bool enabled) const m_ui->trackersList->setEnabled(enabled); m_ui->URLSeedsList->setEnabled(enabled); m_ui->txtComment->setEnabled(enabled); + m_ui->lineEditSource->setEnabled(enabled); m_ui->comboPieceSize->setEnabled(enabled); m_ui->buttonCalcTotalPieces->setEnabled(enabled); m_ui->checkPrivate->setEnabled(enabled); diff --git a/src/gui/torrentcreatordialog.ui b/src/gui/torrentcreatordialog.ui index 6b446d1ee..d25d58a51 100644 --- a/src/gui/torrentcreatordialog.ui +++ b/src/gui/torrentcreatordialog.ui @@ -194,71 +194,6 @@ 7 - - - Auto - - - - - 16 KiB - - - - - 32 KiB - - - - - 64 KiB - - - - - 128 KiB - - - - - 256 KiB - - - - - 512 KiB - - - - - 1 MiB - - - - - 2 MiB - - - - - 4 MiB - - - - - 8 MiB - - - - - 16 MiB - - - - - 32 MiB - -