From 577ace4971b37613a2a60d12fbc69dca9a8caa41 Mon Sep 17 00:00:00 2001 From: ngosang Date: Wed, 24 Jun 2015 12:19:42 +0200 Subject: [PATCH 1/2] Follow project coding style. Issue #2192. --- src/gui/gui.pri | 2 +- src/gui/torrentcreatordlg.cpp | 334 +++++++++--------- src/gui/torrentcreatordlg.h | 49 +-- ...{createtorrent.ui => torrentcreatordlg.ui} | 0 4 files changed, 198 insertions(+), 187 deletions(-) rename src/gui/{createtorrent.ui => torrentcreatordlg.ui} (100%) diff --git a/src/gui/gui.pri b/src/gui/gui.pri index f3c72769d..43474c28c 100644 --- a/src/gui/gui.pri +++ b/src/gui/gui.pri @@ -95,6 +95,6 @@ FORMS += \ $$PWD/autoexpandabledialog.ui \ $$PWD/statsdialog.ui \ $$PWD/options.ui \ - $$PWD/createtorrent.ui + $$PWD/torrentcreatordlg.ui RESOURCES += $$PWD/about.qrc diff --git a/src/gui/torrentcreatordlg.cpp b/src/gui/torrentcreatordlg.cpp index 2c49864f3..6d3fd8172 100644 --- a/src/gui/torrentcreatordlg.cpp +++ b/src/gui/torrentcreatordlg.cpp @@ -44,228 +44,238 @@ const uint NB_PIECES_MIN = 1200; const uint NB_PIECES_MAX = 2200; -TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent): QDialog(parent), m_creatorThread(0) { - setupUi(this); - // Icons - 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")); +TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent) + : QDialog(parent) + , m_creatorThread(0) +{ + setupUi(this); + // Icons + 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); - setModal(true); - showProgressBar(false); - loadTrackerList(); - // Piece sizes - m_pieceSizes << 32 << 64 << 128 << 256 << 512 << 1024 << 2048 << 4096; - loadSettings(); - show(); + setAttribute(Qt::WA_DeleteOnClose); + setModal(true); + showProgressBar(false); + loadTrackerList(); + // Piece sizes + m_pieceSizes << 32 << 64 << 128 << 256 << 512 << 1024 << 2048 << 4096; + loadSettings(); + show(); } -TorrentCreatorDlg::~TorrentCreatorDlg() { - if (m_creatorThread) - delete m_creatorThread; +TorrentCreatorDlg::~TorrentCreatorDlg() +{ + if (m_creatorThread) + delete m_creatorThread; } -void TorrentCreatorDlg::on_addFolder_button_clicked() { - Preferences* const pref = Preferences::instance(); - QString last_path = pref->getCreateTorLastAddPath(); - QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), last_path, QFileDialog::ShowDirsOnly); - if (!dir.isEmpty()) { - pref->setCreateTorLastAddPath(dir); - textInputPath->setText(Utils::Fs::toNativePath(dir)); - // Update piece size - if (checkAutoPieceSize->isChecked()) - updateOptimalPieceSize(); - } +void TorrentCreatorDlg::on_addFolder_button_clicked() +{ + Preferences* const pref = Preferences::instance(); + QString lastPath = pref->getCreateTorLastAddPath(); + QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), lastPath, QFileDialog::ShowDirsOnly); + if (!dir.isEmpty()) { + pref->setCreateTorLastAddPath(dir); + textInputPath->setText(Utils::Fs::toNativePath(dir)); + // Update piece size + if (checkAutoPieceSize->isChecked()) + updateOptimalPieceSize(); + } } -void TorrentCreatorDlg::on_addFile_button_clicked() { - Preferences* const pref = Preferences::instance(); - QString last_path = pref->getCreateTorLastAddPath(); - QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), last_path); - if (!file.isEmpty()) { - pref->setCreateTorLastAddPath(Utils::Fs::branchPath(file)); - textInputPath->setText(Utils::Fs::toNativePath(file)); - // Update piece size - if (checkAutoPieceSize->isChecked()) - updateOptimalPieceSize(); - } +void TorrentCreatorDlg::on_addFile_button_clicked() +{ + Preferences* const pref = Preferences::instance(); + QString lastPath = pref->getCreateTorLastAddPath(); + QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), lastPath); + if (!file.isEmpty()) { + pref->setCreateTorLastAddPath(Utils::Fs::branchPath(file)); + textInputPath->setText(Utils::Fs::toNativePath(file)); + // Update piece size + if (checkAutoPieceSize->isChecked()) + updateOptimalPieceSize(); + } } -int TorrentCreatorDlg::getPieceSize() const { - return m_pieceSizes.at(comboPieceSize->currentIndex())*1024; +int TorrentCreatorDlg::getPieceSize() const +{ + return m_pieceSizes.at(comboPieceSize->currentIndex()) * 1024; } // Main function that create a .torrent file -void TorrentCreatorDlg::on_createButton_clicked() { - QString input = Utils::Fs::fromNativePath(textInputPath->text()).trimmed(); - if (input.endsWith("/")) - input.chop(1); - if (input.isEmpty()) { - 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()) - saveTrackerList(); +void TorrentCreatorDlg::on_createButton_clicked() +{ + QString input = Utils::Fs::fromNativePath(textInputPath->text()).trimmed(); + if (input.endsWith("/")) + input.chop(1); + if (input.isEmpty()) { + 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()) + saveTrackerList(); - Preferences* const pref = Preferences::instance(); - QString last_path = pref->getCreateTorLastSavePath(); + Preferences* const pref = Preferences::instance(); + 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)); if (!destination.toUpper().endsWith(".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()); -} + destination += QString::fromUtf8(".torrent"); -void TorrentCreatorDlg::handleCreationFailure(QString msg) { - // Remove busy cursor - setCursor(QCursor(Qt::ArrowCursor)); - QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(msg)); - setInteractionEnabled(true); - showProgressBar(false); + // Disable dialog + setInteractionEnabled(false); + showProgressBar(true); + // Set busy cursor + setCursor(QCursor(Qt::WaitCursor)); + // 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) { - // Remove busy cursor - 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; - } +void TorrentCreatorDlg::handleCreationFailure(QString msg) +{ + // Remove busy cursor + setCursor(QCursor(Qt::ArrowCursor)); + QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(msg)); + setInteractionEnabled(true); + showProgressBar(false); +} - BitTorrent::AddTorrentParams params; - params.savePath = branch_path; - params.skipChecking = true; - params.ignoreShareRatio = checkIgnoreShareLimits->isChecked(); +void TorrentCreatorDlg::handleCreationSuccess(QString path, QString branch_path) +{ + // Remove busy cursor + 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)); - close(); + BitTorrent::Session::instance()->addTorrent(t, params); + } + QMessageBox::information(0, tr("Torrent creation"), tr("Torrent was created successfully:")+" "+Utils::Fs::toNativePath(path)); + close(); } -void TorrentCreatorDlg::on_cancelButton_clicked() { - // End torrent creation thread - if (m_creatorThread && m_creatorThread->isRunning()) { - m_creatorThread->abortCreation(); - m_creatorThread->terminate(); - // Wait for termination - m_creatorThread->wait(); - } - // Close the dialog - close(); +void TorrentCreatorDlg::on_cancelButton_clicked() +{ + // End torrent creation thread + if (m_creatorThread && m_creatorThread->isRunning()) { + m_creatorThread->abortCreation(); + m_creatorThread->terminate(); + // Wait for termination + m_creatorThread->wait(); + } + // Close the dialog + close(); } -void TorrentCreatorDlg::updateProgressBar(int progress) { - progressBar->setValue(progress); +void TorrentCreatorDlg::updateProgressBar(int progress) +{ + progressBar->setValue(progress); } void TorrentCreatorDlg::setInteractionEnabled(bool enabled) { - textInputPath->setEnabled(enabled); - addFile_button->setEnabled(enabled); - addFolder_button->setEnabled(enabled); - trackers_list->setEnabled(enabled); - URLSeeds_list->setEnabled(enabled); - txt_comment->setEnabled(enabled); - comboPieceSize->setEnabled(enabled); - checkAutoPieceSize->setEnabled(enabled); - check_private->setEnabled(enabled); - checkStartSeeding->setEnabled(enabled); - createButton->setEnabled(enabled); - checkIgnoreShareLimits->setEnabled(enabled && checkStartSeeding->isChecked()); - //cancelButton->setEnabled(!enabled); + textInputPath->setEnabled(enabled); + addFile_button->setEnabled(enabled); + addFolder_button->setEnabled(enabled); + trackers_list->setEnabled(enabled); + URLSeeds_list->setEnabled(enabled); + txt_comment->setEnabled(enabled); + comboPieceSize->setEnabled(enabled); + checkAutoPieceSize->setEnabled(enabled); + check_private->setEnabled(enabled); + checkStartSeeding->setEnabled(enabled); + createButton->setEnabled(enabled); + checkIgnoreShareLimits->setEnabled(enabled && checkStartSeeding->isChecked()); } void TorrentCreatorDlg::showProgressBar(bool show) { - progressLbl->setVisible(show); - progressBar->setVisible(show); + progressLbl->setVisible(show); + progressBar->setVisible(show); } void TorrentCreatorDlg::on_checkAutoPieceSize_clicked(bool checked) { - comboPieceSize->setEnabled(!checked); - if (checked) { - updateOptimalPieceSize(); - } + comboPieceSize->setEnabled(!checked); + if (checked) + updateOptimalPieceSize(); } void TorrentCreatorDlg::updateOptimalPieceSize() { - qint64 torrent_size = Utils::Fs::computePathSize(textInputPath->text()); - qDebug("Torrent size is %lld", torrent_size); - if (torrent_size < 0) return; - int i = 0; - qulonglong nb_pieces = 0; - do { - nb_pieces = (double)torrent_size/(m_pieceSizes.at(i)*1024.); - qDebug("nb_pieces=%lld with piece_size=%s", nb_pieces, qPrintable(comboPieceSize->itemText(i))); - if (nb_pieces <= NB_PIECES_MIN) { - if (i > 1) - --i; - break; - } - if (nb_pieces < NB_PIECES_MAX) { - qDebug("Good, nb_pieces=%lld < %d", nb_pieces, NB_PIECES_MAX); - break; - } - ++i; - }while(i<(m_pieceSizes.size()-1)); - comboPieceSize->setCurrentIndex(i); + qint64 torrentSize = Utils::Fs::computePathSize(textInputPath->text()); + qDebug("Torrent size is %lld", torrentSize); + if (torrentSize < 0) + return; + int i = 0; + qulonglong nb_pieces = 0; + do { + nb_pieces = (double)torrentSize/(m_pieceSizes.at(i) * 1024.); + qDebug("nb_pieces=%lld with piece_size=%s", nb_pieces, qPrintable(comboPieceSize->itemText(i))); + if (nb_pieces <= NB_PIECES_MIN) { + if (i > 1) + --i; + break; + } + else if (nb_pieces < NB_PIECES_MAX) { + qDebug("Good, nb_pieces=%lld < %d", nb_pieces + 1, NB_PIECES_MAX); + break; + } + ++i; + } while (i < (m_pieceSizes.size() - 1)); + comboPieceSize->setCurrentIndex(i); } void TorrentCreatorDlg::saveTrackerList() { - Preferences::instance()->setCreateTorTrackers(trackers_list->toPlainText()); + Preferences::instance()->setCreateTorTrackers(trackers_list->toPlainText()); } void TorrentCreatorDlg::loadTrackerList() { - trackers_list->setPlainText(Preferences::instance()->getCreateTorTrackers()); + trackers_list->setPlainText(Preferences::instance()->getCreateTorTrackers()); } void TorrentCreatorDlg::saveSettings() { - Preferences* const pref = Preferences::instance(); - pref->setCreateTorGeometry(saveGeometry()); - pref->setCreateTorIgnoreRatio(checkIgnoreShareLimits->isChecked()); + Preferences* const pref = Preferences::instance(); + pref->setCreateTorGeometry(saveGeometry()); + pref->setCreateTorIgnoreRatio(checkIgnoreShareLimits->isChecked()); } void TorrentCreatorDlg::loadSettings() { - const Preferences* const pref = Preferences::instance(); - restoreGeometry(pref->getCreateTorGeometry()); - checkIgnoreShareLimits->setChecked(pref->getCreateTorIgnoreRatio()); + const Preferences* const pref = Preferences::instance(); + restoreGeometry(pref->getCreateTorGeometry()); + checkIgnoreShareLimits->setChecked(pref->getCreateTorIgnoreRatio()); } void TorrentCreatorDlg::closeEvent(QCloseEvent *event) { - qDebug() << Q_FUNC_INFO; - saveSettings(); - QDialog::closeEvent(event); + qDebug() << Q_FUNC_INFO; + saveSettings(); + QDialog::closeEvent(event); } diff --git a/src/gui/torrentcreatordlg.h b/src/gui/torrentcreatordlg.h index 44a7ecab0..d681a90d9 100644 --- a/src/gui/torrentcreatordlg.h +++ b/src/gui/torrentcreatordlg.h @@ -31,48 +31,49 @@ #ifndef CREATE_TORRENT_IMP_H #define CREATE_TORRENT_IMP_H -#include "ui_createtorrent.h" +#include "ui_torrentcreatordlg.h" namespace BitTorrent { class TorrentCreatorThread; } -class TorrentCreatorDlg : public QDialog, private Ui::createTorrentDialog{ - Q_OBJECT +class TorrentCreatorDlg : public QDialog, private Ui::createTorrentDialog +{ + Q_OBJECT public: - TorrentCreatorDlg(QWidget *parent = 0); - ~TorrentCreatorDlg(); - int getPieceSize() const; + TorrentCreatorDlg(QWidget *parent = 0); + ~TorrentCreatorDlg(); + int getPieceSize() const; public slots: - void updateProgressBar(int progress); - void on_cancelButton_clicked(); + void updateProgressBar(int progress); + void on_cancelButton_clicked(); protected slots: - void on_createButton_clicked(); - void on_addFile_button_clicked(); - void on_addFolder_button_clicked(); - void handleCreationFailure(QString msg); - void handleCreationSuccess(QString path, QString branch_path); - void setInteractionEnabled(bool enabled); - void showProgressBar(bool show); - void on_checkAutoPieceSize_clicked(bool checked); - void updateOptimalPieceSize(); - void saveTrackerList(); - void loadTrackerList(); + void on_createButton_clicked(); + void on_addFile_button_clicked(); + void on_addFolder_button_clicked(); + void handleCreationFailure(QString msg); + void handleCreationSuccess(QString path, QString branch_path); + void setInteractionEnabled(bool enabled); + void showProgressBar(bool show); + void on_checkAutoPieceSize_clicked(bool checked); + void updateOptimalPieceSize(); + void saveTrackerList(); + void loadTrackerList(); protected: - void closeEvent(QCloseEvent *event); + void closeEvent(QCloseEvent *event); private: - void saveSettings(); - void loadSettings(); + void saveSettings(); + void loadSettings(); private: - BitTorrent::TorrentCreatorThread *m_creatorThread; - QList m_pieceSizes; + BitTorrent::TorrentCreatorThread *m_creatorThread; + QList m_pieceSizes; }; #endif diff --git a/src/gui/createtorrent.ui b/src/gui/torrentcreatordlg.ui similarity index 100% rename from src/gui/createtorrent.ui rename to src/gui/torrentcreatordlg.ui From 91818417be124ab330c216bcd8971a7fbc56787e Mon Sep 17 00:00:00 2001 From: ngosang Date: Wed, 24 Jun 2015 13:04:11 +0200 Subject: [PATCH 2/2] Add 16 KiB, 8 MiB and 16 MiB piece sizes in Torrent Creator --- src/gui/torrentcreatordlg.cpp | 2 +- src/gui/torrentcreatordlg.ui | 17 ++++++++++++++++- 2 files changed, 17 insertions(+), 2 deletions(-) diff --git a/src/gui/torrentcreatordlg.cpp b/src/gui/torrentcreatordlg.cpp index 6d3fd8172..ae1e91abb 100644 --- a/src/gui/torrentcreatordlg.cpp +++ b/src/gui/torrentcreatordlg.cpp @@ -60,7 +60,7 @@ TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent) showProgressBar(false); loadTrackerList(); // 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(); show(); } diff --git a/src/gui/torrentcreatordlg.ui b/src/gui/torrentcreatordlg.ui index 876accdd1..524fc2db8 100644 --- a/src/gui/torrentcreatordlg.ui +++ b/src/gui/torrentcreatordlg.ui @@ -158,8 +158,13 @@ false - 3 + 4 + + + 16 KiB + + 32 KiB @@ -200,6 +205,16 @@ 4 MiB + + + 8 MiB + + + + + 16 MiB + +