Browse Source

Add support for creating v2 torrents

adaptive-webui-19844
Chocobo1 4 years ago
parent
commit
19d77b0881
No known key found for this signature in database
GPG Key ID: 210D9C873253A68C
  1. 27
      src/base/bittorrent/torrentcreatorthread.cpp
  2. 21
      src/base/bittorrent/torrentcreatorthread.h
  3. 51
      src/gui/torrentcreatordialog.cpp
  4. 16
      src/gui/torrentcreatordialog.h
  5. 61
      src/gui/torrentcreatordialog.ui

27
src/base/bittorrent/torrentcreatorthread.cpp

@ -55,6 +55,21 @@ namespace @@ -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() @@ -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() @@ -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 @@ -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
}

21
src/base/bittorrent/torrentcreatorthread.h

@ -29,17 +29,32 @@ @@ -29,17 +29,32 @@
#ifndef BITTORRENT_TORRENTCREATORTHREAD_H
#define BITTORRENT_TORRENTCREATORTHREAD_H
#include <libtorrent/version.hpp>
#include <QStringList>
#include <QThread>
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 @@ -58,8 +73,12 @@ namespace BitTorrent
void create(const TorrentCreatorParams &params);
#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;

51
src/gui/torrentcreatordialog.cpp

@ -36,7 +36,6 @@ @@ -36,7 +36,6 @@
#include <QUrl>
#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 @@ -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 @@ -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 @@ -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() @@ -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) @@ -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 @@ -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() @@ -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() @@ -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);

16
src/gui/torrentcreatordialog.h

@ -30,15 +30,13 @@ @@ -30,15 +30,13 @@
#ifndef TORRENTCREATORDIALOG_H
#define TORRENTCREATORDIALOG_H
#include <libtorrent/version.hpp>
#include <QDialog>
#include "base/bittorrent/torrentcreatorthread.h"
#include "base/settingvalue.h"
namespace BitTorrent
{
class TorrentCreatorThread;
}
namespace Ui
{
class TorrentCreatorDialog;
@ -71,7 +69,11 @@ private: @@ -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: @@ -82,8 +84,12 @@ private:
CachedSettingValue<bool> m_storePrivateTorrent;
CachedSettingValue<bool> m_storeStartSeeding;
CachedSettingValue<bool> m_storeIgnoreRatio;
#if (LIBTORRENT_VERSION_NUM >= 20000)
CachedSettingValue<int> m_storeTorrentFormat;
#else
CachedSettingValue<bool> m_storeOptimizeAlignment;
CachedSettingValue<int> m_paddedFileSizeLimit;
#endif
CachedSettingValue<QString> m_storeLastAddPath;
CachedSettingValue<QString> m_storeTrackerList;
CachedSettingValue<QString> m_storeWebSeedList;

61
src/gui/torrentcreatordialog.ui

@ -30,8 +30,8 @@ @@ -30,8 +30,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>574</width>
<height>649</height>
<width>560</width>
<height>668</height>
</rect>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
@ -120,6 +120,63 @@ @@ -120,6 +120,63 @@
<string>Settings</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_2">
<item>
<widget class="QWidget" name="widgetTorrentFormat" native="true">
<layout class="QHBoxLayout" name="layoutTorrentFormat">
<property name="leftMargin">
<number>0</number>
</property>
<property name="topMargin">
<number>0</number>
</property>
<property name="rightMargin">
<number>0</number>
</property>
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QLabel" name="labelTorrentFormat">
<property name="text">
<string>Torrent format:</string>
</property>
</widget>
</item>
<item>
<widget class="QComboBox" name="comboTorrentFormat">
<item>
<property name="text">
<string notr="true">V2</string>
</property>
</item>
<item>
<property name="text">
<string>Hybrid</string>
</property>
</item>
<item>
<property name="text">
<string notr="true">V1</string>
</property>
</item>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="pieceSizeLayout">
<item>

Loading…
Cancel
Save