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
{ {
return !Utils::Fs::fileName(QString::fromStdString(f)).startsWith('.'); 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; using namespace BitTorrent;
@ -131,8 +146,12 @@ void TorrentCreatorThread::run()
if (isInterruptionRequested()) return; 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 lt::create_torrent newTorrent(fs, m_params.pieceSize, m_params.paddedFileSizeLimit
, (m_params.isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})); , (m_params.isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {}));
#endif
// Add url seeds // Add url seeds
for (QString seed : asConst(m_params.urlSeeds)) { 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) int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const int pieceSize, const bool isAlignmentOptimized, const int paddedFileSizeLimit)
#endif
{ {
if (inputPath.isEmpty()) if (inputPath.isEmpty())
return 0; return 0;
@ -206,6 +229,10 @@ int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const i
lt::file_storage fs; lt::file_storage fs;
lt::add_files(fs, Utils::Fs::toNativePath(inputPath).toStdString(), fileFilter); 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 return lt::create_torrent(fs, pieceSize, paddedFileSizeLimit
, (isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})).num_pieces(); , (isAlignmentOptimized ? lt::create_torrent::optimize_alignment : lt::create_flags_t {})).num_pieces();
#endif
} }

21
src/base/bittorrent/torrentcreatorthread.h

@ -29,17 +29,32 @@
#ifndef BITTORRENT_TORRENTCREATORTHREAD_H #ifndef BITTORRENT_TORRENTCREATORTHREAD_H
#define BITTORRENT_TORRENTCREATORTHREAD_H #define BITTORRENT_TORRENTCREATORTHREAD_H
#include <libtorrent/version.hpp>
#include <QStringList> #include <QStringList>
#include <QThread> #include <QThread>
namespace BitTorrent namespace BitTorrent
{ {
#if (LIBTORRENT_VERSION_NUM >= 20000)
enum class TorrentFormat
{
V1,
V2,
Hybrid
};
#endif
struct TorrentCreatorParams struct TorrentCreatorParams
{ {
bool isPrivate; bool isPrivate;
#if (LIBTORRENT_VERSION_NUM >= 20000)
TorrentFormat torrentFormat;
#else
bool isAlignmentOptimized; bool isAlignmentOptimized;
int pieceSize;
int paddedFileSizeLimit; int paddedFileSizeLimit;
#endif
int pieceSize;
QString inputPath; QString inputPath;
QString savePath; QString savePath;
QString comment; QString comment;
@ -58,8 +73,12 @@ namespace BitTorrent
void create(const TorrentCreatorParams &params); 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 static int calculateTotalPieces(const QString &inputPath
, const int pieceSize, const bool isAlignmentOptimized, int paddedFileSizeLimit); , const int pieceSize, const bool isAlignmentOptimized, int paddedFileSizeLimit);
#endif
protected: protected:
void run() override; void run() override;

51
src/gui/torrentcreatordialog.cpp

@ -36,7 +36,6 @@
#include <QUrl> #include <QUrl>
#include "base/bittorrent/session.h" #include "base/bittorrent/session.h"
#include "base/bittorrent/torrentcreatorthread.h"
#include "base/bittorrent/torrentinfo.h" #include "base/bittorrent/torrentinfo.h"
#include "base/global.h" #include "base/global.h"
#include "base/utils/fs.h" #include "base/utils/fs.h"
@ -54,8 +53,12 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defau
, m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent")) , m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent"))
, m_storeStartSeeding(SETTINGS_KEY("StartSeeding")) , m_storeStartSeeding(SETTINGS_KEY("StartSeeding"))
, m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio")) , m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio"))
#if (LIBTORRENT_VERSION_NUM >= 20000)
, m_storeTorrentFormat(SETTINGS_KEY("TorrentFormat"), 1)
#else
, m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"), true) , m_storeOptimizeAlignment(SETTINGS_KEY("OptimizeAlignment"), true)
, m_paddedFileSizeLimit(SETTINGS_KEY("PaddedFileSizeLimit"), -1) , m_paddedFileSizeLimit(SETTINGS_KEY("PaddedFileSizeLimit"), -1)
#endif
, m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath()) , m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath())
, m_storeTrackerList(SETTINGS_KEY("TrackerList")) , m_storeTrackerList(SETTINGS_KEY("TrackerList"))
, m_storeWebSeedList(SETTINGS_KEY("WebSeedList")) , m_storeWebSeedList(SETTINGS_KEY("WebSeedList"))
@ -81,6 +84,12 @@ TorrentCreatorDialog::TorrentCreatorDialog(QWidget *parent, const QString &defau
loadSettings(); loadSettings();
updateInputPath(defaultPath); updateInputPath(defaultPath);
#if (LIBTORRENT_VERSION_NUM >= 20000)
m_ui->checkOptimizeAlignment->hide();
#else
m_ui->widgetTorrentFormat->hide();
#endif
show(); show();
} }
@ -118,11 +127,26 @@ int TorrentCreatorDialog::getPieceSize() const
return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024; 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 int TorrentCreatorDialog::getPaddedFileSizeLimit() const
{ {
const int value = m_ui->spinPaddedFileSizeLimit->value(); const int value = m_ui->spinPaddedFileSizeLimit->value();
return ((value >= 0) ? (value * 1024) : -1); return ((value >= 0) ? (value * 1024) : -1);
} }
#endif
void TorrentCreatorDialog::dropEvent(QDropEvent *event) void TorrentCreatorDialog::dropEvent(QDropEvent *event)
{ {
@ -173,8 +197,13 @@ void TorrentCreatorDialog::onCreateButtonClicked()
.replace(QRegularExpression("\n\n[\n]+"), "\n\n").split('\n'); .replace(QRegularExpression("\n\n[\n]+"), "\n\n").split('\n');
const BitTorrent::TorrentCreatorParams params { const BitTorrent::TorrentCreatorParams params {
m_ui->checkPrivate->isChecked() m_ui->checkPrivate->isChecked()
#if (LIBTORRENT_VERSION_NUM >= 20000)
, getTorrentFormat()
#else
, m_ui->checkOptimizeAlignment->isChecked() , m_ui->checkOptimizeAlignment->isChecked()
, getPieceSize(), getPaddedFileSizeLimit() , getPaddedFileSizeLimit()
#endif
, getPieceSize()
, input, destination , input, destination
, m_ui->txtComment->toPlainText() , m_ui->txtComment->toPlainText()
, m_ui->lineEditSource->text() , m_ui->lineEditSource->text()
@ -230,10 +259,14 @@ void TorrentCreatorDialog::updateProgressBar(int progress)
void TorrentCreatorDialog::updatePiecesCount() void TorrentCreatorDialog::updatePiecesCount()
{ {
const QString path = m_ui->textInputPath->text().trimmed(); 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 bool isAlignmentOptimized = m_ui->checkOptimizeAlignment->isChecked();
const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path
, getPieceSize(), isAlignmentOptimized, getPaddedFileSizeLimit()); , getPieceSize(), isAlignmentOptimized, getPaddedFileSizeLimit());
#endif
m_ui->labelTotalPieces->setText(QString::number(count)); 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->checkStartSeeding->setEnabled(enabled);
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled); m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked()); 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->checkOptimizeAlignment->setEnabled(enabled);
m_ui->spinPaddedFileSizeLimit->setEnabled(enabled); m_ui->spinPaddedFileSizeLimit->setEnabled(enabled);
#endif
} }
void TorrentCreatorDialog::saveSettings() void TorrentCreatorDialog::saveSettings()
@ -263,8 +300,12 @@ void TorrentCreatorDialog::saveSettings()
m_storePrivateTorrent = m_ui->checkPrivate->isChecked(); m_storePrivateTorrent = m_ui->checkPrivate->isChecked();
m_storeStartSeeding = m_ui->checkStartSeeding->isChecked(); m_storeStartSeeding = m_ui->checkStartSeeding->isChecked();
m_storeIgnoreRatio = m_ui->checkIgnoreShareLimits->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_storeOptimizeAlignment = m_ui->checkOptimizeAlignment->isChecked();
m_paddedFileSizeLimit = m_ui->spinPaddedFileSizeLimit->value(); m_paddedFileSizeLimit = m_ui->spinPaddedFileSizeLimit->value();
#endif
m_storeTrackerList = m_ui->trackersList->toPlainText(); m_storeTrackerList = m_ui->trackersList->toPlainText();
m_storeWebSeedList = m_ui->URLSeedsList->toPlainText(); m_storeWebSeedList = m_ui->URLSeedsList->toPlainText();
@ -283,8 +324,12 @@ void TorrentCreatorDialog::loadSettings()
m_ui->checkStartSeeding->setChecked(m_storeStartSeeding); m_ui->checkStartSeeding->setChecked(m_storeStartSeeding);
m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio); m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio);
m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked()); 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->checkOptimizeAlignment->setChecked(m_storeOptimizeAlignment);
m_ui->spinPaddedFileSizeLimit->setValue(m_paddedFileSizeLimit); m_ui->spinPaddedFileSizeLimit->setValue(m_paddedFileSizeLimit);
#endif
m_ui->trackersList->setPlainText(m_storeTrackerList); m_ui->trackersList->setPlainText(m_storeTrackerList);
m_ui->URLSeedsList->setPlainText(m_storeWebSeedList); m_ui->URLSeedsList->setPlainText(m_storeWebSeedList);

16
src/gui/torrentcreatordialog.h

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

61
src/gui/torrentcreatordialog.ui

@ -30,8 +30,8 @@
<rect> <rect>
<x>0</x> <x>0</x>
<y>0</y> <y>0</y>
<width>574</width> <width>560</width>
<height>649</height> <height>668</height>
</rect> </rect>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_5"> <layout class="QVBoxLayout" name="verticalLayout_5">
@ -120,6 +120,63 @@
<string>Settings</string> <string>Settings</string>
</property> </property>
<layout class="QVBoxLayout" name="verticalLayout_2"> <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> <item>
<layout class="QHBoxLayout" name="pieceSizeLayout"> <layout class="QHBoxLayout" name="pieceSizeLayout">
<item> <item>

Loading…
Cancel
Save