Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support creating .torrent with larger piece size #19535

Merged
merged 5 commits into from
Sep 3, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions src/base/utils/misc.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<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
Loading