Skip to content

Commit

Permalink
Support creating .torrent with larger piece size
Browse files Browse the repository at this point in the history
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 qbittorrent#19527.
PR qbittorrent#19535.
  • Loading branch information
Chocobo1 authored and rcarpa committed Dec 1, 2023
1 parent 46667e2 commit 02782b2
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 76 deletions.
6 changes: 4 additions & 2 deletions src/base/utils/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -262,12 +262,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<SplitToFriendlyUnitResult> 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);
}
Expand Down
2 changes: 1 addition & 1 deletion src/base/utils/misc.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
30 changes: 22 additions & 8 deletions src/gui/torrentcreatordialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down Expand Up @@ -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);

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
}

Expand All @@ -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)
Expand Down Expand Up @@ -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);
Expand Down
65 changes: 0 additions & 65 deletions src/gui/torrentcreatordialog.ui
Original file line number Diff line number Diff line change
Expand Up @@ -194,71 +194,6 @@
<property name="minimumContentsLength">
<number>7</number>
</property>
<item>
<property name="text">
<string>Auto</string>
</property>
</item>
<item>
<property name="text">
<string>16 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>32 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>64 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>128 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>256 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>512 KiB</string>
</property>
</item>
<item>
<property name="text">
<string>1 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>2 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>4 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>8 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>16 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>32 MiB</string>
</property>
</item>
</widget>
</item>
<item>
Expand Down

0 comments on commit 02782b2

Please sign in to comment.