Browse Source

Merge pull request #3291 from ngosang/torrent_creator

Add 16 KiB, 8 MiB and 16 MiB piece sizes in Torrent Creator. Closes #2656
adaptive-webui-19844
sledgehammer999 10 years ago
parent
commit
f94214ae14
  1. 2
      src/gui/gui.pri
  2. 334
      src/gui/torrentcreatordlg.cpp
  3. 49
      src/gui/torrentcreatordlg.h
  4. 17
      src/gui/torrentcreatordlg.ui

2
src/gui/gui.pri

@ -95,6 +95,6 @@ FORMS += \
$$PWD/autoexpandabledialog.ui \ $$PWD/autoexpandabledialog.ui \
$$PWD/statsdialog.ui \ $$PWD/statsdialog.ui \
$$PWD/options.ui \ $$PWD/options.ui \
$$PWD/createtorrent.ui $$PWD/torrentcreatordlg.ui
RESOURCES += $$PWD/about.qrc RESOURCES += $$PWD/about.qrc

334
src/gui/torrentcreatordlg.cpp

@ -44,228 +44,238 @@
const uint NB_PIECES_MIN = 1200; const uint NB_PIECES_MIN = 1200;
const uint NB_PIECES_MAX = 2200; const uint NB_PIECES_MAX = 2200;
TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent): QDialog(parent), m_creatorThread(0) { TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent)
setupUi(this); : QDialog(parent)
// Icons , m_creatorThread(0)
addFile_button->setIcon(GuiIconProvider::instance()->getIcon("document-new")); {
addFolder_button->setIcon(GuiIconProvider::instance()->getIcon("folder-new")); setupUi(this);
createButton->setIcon(GuiIconProvider::instance()->getIcon("document-save")); // Icons
cancelButton->setIcon(GuiIconProvider::instance()->getIcon("dialog-cancel")); addFile_button->setIcon(GuiIconProvider::instance()->getIcon("document-new"));
addFolder_button->setIcon(GuiIconProvider::instance()->getIcon("folder-new"));
createButton->setIcon(GuiIconProvider::instance()->getIcon("document-save"));
cancelButton->setIcon(GuiIconProvider::instance()->getIcon("dialog-cancel"));
setAttribute(Qt::WA_DeleteOnClose); setAttribute(Qt::WA_DeleteOnClose);
setModal(true); setModal(true);
showProgressBar(false); showProgressBar(false);
loadTrackerList(); loadTrackerList();
// Piece sizes // Piece sizes
m_pieceSizes << 32 << 64 << 128 << 256 << 512 << 1024 << 2048 << 4096; m_pieceSizes << 16 << 32 << 64 << 128 << 256 << 512 << 1024 << 2048 << 4096 << 8192 << 16384;
loadSettings(); loadSettings();
show(); show();
} }
TorrentCreatorDlg::~TorrentCreatorDlg() { TorrentCreatorDlg::~TorrentCreatorDlg()
if (m_creatorThread) {
delete m_creatorThread; if (m_creatorThread)
delete m_creatorThread;
} }
void TorrentCreatorDlg::on_addFolder_button_clicked() { void TorrentCreatorDlg::on_addFolder_button_clicked()
Preferences* const pref = Preferences::instance(); {
QString last_path = pref->getCreateTorLastAddPath(); Preferences* const pref = Preferences::instance();
QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), last_path, QFileDialog::ShowDirsOnly); QString lastPath = pref->getCreateTorLastAddPath();
if (!dir.isEmpty()) { QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), lastPath, QFileDialog::ShowDirsOnly);
pref->setCreateTorLastAddPath(dir); if (!dir.isEmpty()) {
textInputPath->setText(Utils::Fs::toNativePath(dir)); pref->setCreateTorLastAddPath(dir);
// Update piece size textInputPath->setText(Utils::Fs::toNativePath(dir));
if (checkAutoPieceSize->isChecked()) // Update piece size
updateOptimalPieceSize(); if (checkAutoPieceSize->isChecked())
} updateOptimalPieceSize();
}
} }
void TorrentCreatorDlg::on_addFile_button_clicked() { void TorrentCreatorDlg::on_addFile_button_clicked()
Preferences* const pref = Preferences::instance(); {
QString last_path = pref->getCreateTorLastAddPath(); Preferences* const pref = Preferences::instance();
QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), last_path); QString lastPath = pref->getCreateTorLastAddPath();
if (!file.isEmpty()) { QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), lastPath);
pref->setCreateTorLastAddPath(Utils::Fs::branchPath(file)); if (!file.isEmpty()) {
textInputPath->setText(Utils::Fs::toNativePath(file)); pref->setCreateTorLastAddPath(Utils::Fs::branchPath(file));
// Update piece size textInputPath->setText(Utils::Fs::toNativePath(file));
if (checkAutoPieceSize->isChecked()) // Update piece size
updateOptimalPieceSize(); if (checkAutoPieceSize->isChecked())
} updateOptimalPieceSize();
}
} }
int TorrentCreatorDlg::getPieceSize() const { int TorrentCreatorDlg::getPieceSize() const
return m_pieceSizes.at(comboPieceSize->currentIndex())*1024; {
return m_pieceSizes.at(comboPieceSize->currentIndex()) * 1024;
} }
// Main function that create a .torrent file // Main function that create a .torrent file
void TorrentCreatorDlg::on_createButton_clicked() { void TorrentCreatorDlg::on_createButton_clicked()
QString input = Utils::Fs::fromNativePath(textInputPath->text()).trimmed(); {
if (input.endsWith("/")) QString input = Utils::Fs::fromNativePath(textInputPath->text()).trimmed();
input.chop(1); if (input.endsWith("/"))
if (input.isEmpty()) { input.chop(1);
QMessageBox::critical(0, tr("No input path set"), tr("Please type an input path first")); if (input.isEmpty()) {
return; QMessageBox::critical(0, tr("No input path set"), tr("Please type an input path first"));
} return;
QStringList trackers = trackers_list->toPlainText().split("\n"); }
if (!trackers_list->toPlainText().trimmed().isEmpty()) QStringList trackers = trackers_list->toPlainText().split("\n");
saveTrackerList(); if (!trackers_list->toPlainText().trimmed().isEmpty())
saveTrackerList();
Preferences* const pref = Preferences::instance(); Preferences* const pref = Preferences::instance();
QString last_path = pref->getCreateTorLastSavePath(); QString lastPath = pref->getCreateTorLastSavePath();
QString destination = QFileDialog::getSaveFileName(this, tr("Select destination torrent file"), lastPath, tr("Torrent Files")+QString::fromUtf8(" (*.torrent)"));
if (destination.isEmpty())
return;
QString destination = QFileDialog::getSaveFileName(this, tr("Select destination torrent file"), last_path, tr("Torrent Files")+QString::fromUtf8(" (*.torrent)"));
if (!destination.isEmpty()) {
pref->setCreateTorLastSavePath(Utils::Fs::branchPath(destination)); pref->setCreateTorLastSavePath(Utils::Fs::branchPath(destination));
if (!destination.toUpper().endsWith(".TORRENT")) if (!destination.toUpper().endsWith(".TORRENT"))
destination += QString::fromUtf8(".torrent"); destination += QString::fromUtf8(".torrent");
} else {
return;
}
// Disable dialog
setInteractionEnabled(false);
showProgressBar(true);
// Set busy cursor
setCursor(QCursor(Qt::WaitCursor));
// Actually create the torrent
QStringList url_seeds = URLSeeds_list->toPlainText().split("\n");
QString comment = txt_comment->toPlainText();
// Create the creator thread
m_creatorThread = new BitTorrent::TorrentCreatorThread(this);
connect(m_creatorThread, SIGNAL(creationSuccess(QString, QString)), this, SLOT(handleCreationSuccess(QString, QString)));
connect(m_creatorThread, SIGNAL(creationFailure(QString)), this, SLOT(handleCreationFailure(QString)));
connect(m_creatorThread, SIGNAL(updateProgress(int)), this, SLOT(updateProgressBar(int)));
m_creatorThread->create(input, destination, trackers, url_seeds, comment, check_private->isChecked(), getPieceSize());
}
void TorrentCreatorDlg::handleCreationFailure(QString msg) { // Disable dialog
// Remove busy cursor setInteractionEnabled(false);
setCursor(QCursor(Qt::ArrowCursor)); showProgressBar(true);
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(msg)); // Set busy cursor
setInteractionEnabled(true); setCursor(QCursor(Qt::WaitCursor));
showProgressBar(false); // Actually create the torrent
QStringList urlSeeds = URLSeeds_list->toPlainText().split("\n");
QString comment = txt_comment->toPlainText();
// Create the creator thread
m_creatorThread = new BitTorrent::TorrentCreatorThread(this);
connect(m_creatorThread, SIGNAL(creationSuccess(QString, QString)), this, SLOT(handleCreationSuccess(QString, QString)));
connect(m_creatorThread, SIGNAL(creationFailure(QString)), this, SLOT(handleCreationFailure(QString)));
connect(m_creatorThread, SIGNAL(updateProgress(int)), this, SLOT(updateProgressBar(int)));
m_creatorThread->create(input, destination, trackers, urlSeeds, comment, check_private->isChecked(), getPieceSize());
} }
void TorrentCreatorDlg::handleCreationSuccess(QString path, QString branch_path) { void TorrentCreatorDlg::handleCreationFailure(QString msg)
// Remove busy cursor {
setCursor(QCursor(Qt::ArrowCursor)); // Remove busy cursor
if (checkStartSeeding->isChecked()) { setCursor(QCursor(Qt::ArrowCursor));
// Create save path temp data QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(msg));
BitTorrent::TorrentInfo t = BitTorrent::TorrentInfo::loadFromFile(Utils::Fs::toNativePath(path)); setInteractionEnabled(true);
if (!t.isValid()) { showProgressBar(false);
QMessageBox::critical(0, tr("Torrent creation"), tr("Created torrent file is invalid. It won't be added to download list.")); }
return;
}
BitTorrent::AddTorrentParams params; void TorrentCreatorDlg::handleCreationSuccess(QString path, QString branch_path)
params.savePath = branch_path; {
params.skipChecking = true; // Remove busy cursor
params.ignoreShareRatio = checkIgnoreShareLimits->isChecked(); setCursor(QCursor(Qt::ArrowCursor));
if (checkStartSeeding->isChecked()) {
// Create save path temp data
BitTorrent::TorrentInfo t = BitTorrent::TorrentInfo::loadFromFile(Utils::Fs::toNativePath(path));
if (!t.isValid()) {
QMessageBox::critical(0, tr("Torrent creation"), tr("Created torrent file is invalid. It won't be added to download list."));
return;
}
BitTorrent::Session::instance()->addTorrent(t, params); BitTorrent::AddTorrentParams params;
} params.savePath = branch_path;
params.skipChecking = true;
params.ignoreShareRatio = checkIgnoreShareLimits->isChecked();
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent was created successfully:")+" "+Utils::Fs::toNativePath(path)); BitTorrent::Session::instance()->addTorrent(t, params);
close(); }
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent was created successfully:")+" "+Utils::Fs::toNativePath(path));
close();
} }
void TorrentCreatorDlg::on_cancelButton_clicked() { void TorrentCreatorDlg::on_cancelButton_clicked()
// End torrent creation thread {
if (m_creatorThread && m_creatorThread->isRunning()) { // End torrent creation thread
m_creatorThread->abortCreation(); if (m_creatorThread && m_creatorThread->isRunning()) {
m_creatorThread->terminate(); m_creatorThread->abortCreation();
// Wait for termination m_creatorThread->terminate();
m_creatorThread->wait(); // Wait for termination
} m_creatorThread->wait();
// Close the dialog }
close(); // Close the dialog
close();
} }
void TorrentCreatorDlg::updateProgressBar(int progress) { void TorrentCreatorDlg::updateProgressBar(int progress)
progressBar->setValue(progress); {
progressBar->setValue(progress);
} }
void TorrentCreatorDlg::setInteractionEnabled(bool enabled) void TorrentCreatorDlg::setInteractionEnabled(bool enabled)
{ {
textInputPath->setEnabled(enabled); textInputPath->setEnabled(enabled);
addFile_button->setEnabled(enabled); addFile_button->setEnabled(enabled);
addFolder_button->setEnabled(enabled); addFolder_button->setEnabled(enabled);
trackers_list->setEnabled(enabled); trackers_list->setEnabled(enabled);
URLSeeds_list->setEnabled(enabled); URLSeeds_list->setEnabled(enabled);
txt_comment->setEnabled(enabled); txt_comment->setEnabled(enabled);
comboPieceSize->setEnabled(enabled); comboPieceSize->setEnabled(enabled);
checkAutoPieceSize->setEnabled(enabled); checkAutoPieceSize->setEnabled(enabled);
check_private->setEnabled(enabled); check_private->setEnabled(enabled);
checkStartSeeding->setEnabled(enabled); checkStartSeeding->setEnabled(enabled);
createButton->setEnabled(enabled); createButton->setEnabled(enabled);
checkIgnoreShareLimits->setEnabled(enabled && checkStartSeeding->isChecked()); checkIgnoreShareLimits->setEnabled(enabled && checkStartSeeding->isChecked());
//cancelButton->setEnabled(!enabled);
} }
void TorrentCreatorDlg::showProgressBar(bool show) void TorrentCreatorDlg::showProgressBar(bool show)
{ {
progressLbl->setVisible(show); progressLbl->setVisible(show);
progressBar->setVisible(show); progressBar->setVisible(show);
} }
void TorrentCreatorDlg::on_checkAutoPieceSize_clicked(bool checked) void TorrentCreatorDlg::on_checkAutoPieceSize_clicked(bool checked)
{ {
comboPieceSize->setEnabled(!checked); comboPieceSize->setEnabled(!checked);
if (checked) { if (checked)
updateOptimalPieceSize(); updateOptimalPieceSize();
}
} }
void TorrentCreatorDlg::updateOptimalPieceSize() void TorrentCreatorDlg::updateOptimalPieceSize()
{ {
qint64 torrent_size = Utils::Fs::computePathSize(textInputPath->text()); qint64 torrentSize = Utils::Fs::computePathSize(textInputPath->text());
qDebug("Torrent size is %lld", torrent_size); qDebug("Torrent size is %lld", torrentSize);
if (torrent_size < 0) return; if (torrentSize < 0)
int i = 0; return;
qulonglong nb_pieces = 0; int i = 0;
do { qulonglong nb_pieces = 0;
nb_pieces = (double)torrent_size/(m_pieceSizes.at(i)*1024.); do {
qDebug("nb_pieces=%lld with piece_size=%s", nb_pieces, qPrintable(comboPieceSize->itemText(i))); nb_pieces = (double)torrentSize/(m_pieceSizes.at(i) * 1024.);
if (nb_pieces <= NB_PIECES_MIN) { qDebug("nb_pieces=%lld with piece_size=%s", nb_pieces, qPrintable(comboPieceSize->itemText(i)));
if (i > 1) if (nb_pieces <= NB_PIECES_MIN) {
--i; if (i > 1)
break; --i;
} break;
if (nb_pieces < NB_PIECES_MAX) { }
qDebug("Good, nb_pieces=%lld < %d", nb_pieces, NB_PIECES_MAX); else if (nb_pieces < NB_PIECES_MAX) {
break; qDebug("Good, nb_pieces=%lld < %d", nb_pieces + 1, NB_PIECES_MAX);
} break;
++i; }
}while(i<(m_pieceSizes.size()-1)); ++i;
comboPieceSize->setCurrentIndex(i); } while (i < (m_pieceSizes.size() - 1));
comboPieceSize->setCurrentIndex(i);
} }
void TorrentCreatorDlg::saveTrackerList() void TorrentCreatorDlg::saveTrackerList()
{ {
Preferences::instance()->setCreateTorTrackers(trackers_list->toPlainText()); Preferences::instance()->setCreateTorTrackers(trackers_list->toPlainText());
} }
void TorrentCreatorDlg::loadTrackerList() void TorrentCreatorDlg::loadTrackerList()
{ {
trackers_list->setPlainText(Preferences::instance()->getCreateTorTrackers()); trackers_list->setPlainText(Preferences::instance()->getCreateTorTrackers());
} }
void TorrentCreatorDlg::saveSettings() void TorrentCreatorDlg::saveSettings()
{ {
Preferences* const pref = Preferences::instance(); Preferences* const pref = Preferences::instance();
pref->setCreateTorGeometry(saveGeometry()); pref->setCreateTorGeometry(saveGeometry());
pref->setCreateTorIgnoreRatio(checkIgnoreShareLimits->isChecked()); pref->setCreateTorIgnoreRatio(checkIgnoreShareLimits->isChecked());
} }
void TorrentCreatorDlg::loadSettings() void TorrentCreatorDlg::loadSettings()
{ {
const Preferences* const pref = Preferences::instance(); const Preferences* const pref = Preferences::instance();
restoreGeometry(pref->getCreateTorGeometry()); restoreGeometry(pref->getCreateTorGeometry());
checkIgnoreShareLimits->setChecked(pref->getCreateTorIgnoreRatio()); checkIgnoreShareLimits->setChecked(pref->getCreateTorIgnoreRatio());
} }
void TorrentCreatorDlg::closeEvent(QCloseEvent *event) void TorrentCreatorDlg::closeEvent(QCloseEvent *event)
{ {
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
saveSettings(); saveSettings();
QDialog::closeEvent(event); QDialog::closeEvent(event);
} }

49
src/gui/torrentcreatordlg.h

@ -31,48 +31,49 @@
#ifndef CREATE_TORRENT_IMP_H #ifndef CREATE_TORRENT_IMP_H
#define CREATE_TORRENT_IMP_H #define CREATE_TORRENT_IMP_H
#include "ui_createtorrent.h" #include "ui_torrentcreatordlg.h"
namespace BitTorrent namespace BitTorrent
{ {
class TorrentCreatorThread; class TorrentCreatorThread;
} }
class TorrentCreatorDlg : public QDialog, private Ui::createTorrentDialog{ class TorrentCreatorDlg : public QDialog, private Ui::createTorrentDialog
Q_OBJECT {
Q_OBJECT
public: public:
TorrentCreatorDlg(QWidget *parent = 0); TorrentCreatorDlg(QWidget *parent = 0);
~TorrentCreatorDlg(); ~TorrentCreatorDlg();
int getPieceSize() const; int getPieceSize() const;
public slots: public slots:
void updateProgressBar(int progress); void updateProgressBar(int progress);
void on_cancelButton_clicked(); void on_cancelButton_clicked();
protected slots: protected slots:
void on_createButton_clicked(); void on_createButton_clicked();
void on_addFile_button_clicked(); void on_addFile_button_clicked();
void on_addFolder_button_clicked(); void on_addFolder_button_clicked();
void handleCreationFailure(QString msg); void handleCreationFailure(QString msg);
void handleCreationSuccess(QString path, QString branch_path); void handleCreationSuccess(QString path, QString branch_path);
void setInteractionEnabled(bool enabled); void setInteractionEnabled(bool enabled);
void showProgressBar(bool show); void showProgressBar(bool show);
void on_checkAutoPieceSize_clicked(bool checked); void on_checkAutoPieceSize_clicked(bool checked);
void updateOptimalPieceSize(); void updateOptimalPieceSize();
void saveTrackerList(); void saveTrackerList();
void loadTrackerList(); void loadTrackerList();
protected: protected:
void closeEvent(QCloseEvent *event); void closeEvent(QCloseEvent *event);
private: private:
void saveSettings(); void saveSettings();
void loadSettings(); void loadSettings();
private: private:
BitTorrent::TorrentCreatorThread *m_creatorThread; BitTorrent::TorrentCreatorThread *m_creatorThread;
QList<int> m_pieceSizes; QList<int> m_pieceSizes;
}; };
#endif #endif

17
src/gui/createtorrent.ui → src/gui/torrentcreatordlg.ui

@ -158,8 +158,13 @@
<bool>false</bool> <bool>false</bool>
</property> </property>
<property name="currentIndex"> <property name="currentIndex">
<number>3</number> <number>4</number>
</property> </property>
<item>
<property name="text">
<string>16 KiB</string>
</property>
</item>
<item> <item>
<property name="text"> <property name="text">
<string>32 KiB</string> <string>32 KiB</string>
@ -200,6 +205,16 @@
<string>4 MiB</string> <string>4 MiB</string>
</property> </property>
</item> </item>
<item>
<property name="text">
<string>8 MiB</string>
</property>
</item>
<item>
<property name="text">
<string>16 MiB</string>
</property>
</item>
</widget> </widget>
</item> </item>
<item> <item>
Loading…
Cancel
Save