diff --git a/src/base/bittorrent/torrentcreatorthread.cpp b/src/base/bittorrent/torrentcreatorthread.cpp index 3a3d7fce9..38643235b 100644 --- a/src/base/bittorrent/torrentcreatorthread.cpp +++ b/src/base/bittorrent/torrentcreatorthread.cpp @@ -160,3 +160,13 @@ void TorrentCreatorThread::run() emit creationFailure(QString::fromStdString(e.what())); } } + +int TorrentCreatorThread::calculateTotalPieces(const QString &inputPath, const int pieceSize) +{ + if (inputPath.isEmpty()) + return 0; + + libt::file_storage fs; + libt::add_files(fs, Utils::Fs::toNativePath(inputPath).toStdString(), fileFilter); + return libt::create_torrent(fs, pieceSize).num_pieces(); +} diff --git a/src/base/bittorrent/torrentcreatorthread.h b/src/base/bittorrent/torrentcreatorthread.h index d62af8011..25d8d83ed 100644 --- a/src/base/bittorrent/torrentcreatorthread.h +++ b/src/base/bittorrent/torrentcreatorthread.h @@ -47,6 +47,8 @@ namespace BitTorrent void create(const QString &inputPath, const QString &savePath, const QStringList &trackers, const QStringList &urlSeeds, const QString &comment, bool isPrivate, int pieceSize); + static int calculateTotalPieces(const QString &inputPath, const int pieceSize); + protected: void run(); diff --git a/src/gui/torrentcreatordlg.cpp b/src/gui/torrentcreatordlg.cpp index 4eb58966d..fb72e792b 100644 --- a/src/gui/torrentcreatordlg.cpp +++ b/src/gui/torrentcreatordlg.cpp @@ -70,6 +70,7 @@ TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent, const QString &defaultPath connect(m_ui->addFileButton, SIGNAL(clicked(bool)), SLOT(onAddFileButtonClicked())); connect(m_ui->addFolderButton, SIGNAL(clicked(bool)), SLOT(onAddFolderButtonClicked())); connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(onCreateButtonClicked())); + connect(m_ui->buttonCalcTotalPieces, &QAbstractButton::clicked, this, &TorrentCreatorDlg::updatePiecesCount); connect(m_creatorThread, SIGNAL(creationSuccess(QString, QString)), this, SLOT(handleCreationSuccess(QString, QString))); connect(m_creatorThread, SIGNAL(creationFailure(QString)), this, SLOT(handleCreationFailure(QString))); @@ -207,6 +208,14 @@ void TorrentCreatorDlg::updateProgressBar(int progress) m_ui->progressBar->setValue(progress); } +void TorrentCreatorDlg::updatePiecesCount() +{ + const QString path = m_ui->textInputPath->text().trimmed(); + + const int count = BitTorrent::TorrentCreatorThread::calculateTotalPieces(path, getPieceSize()); + m_ui->labelTotalPieces->setText(QString::number(count)); +} + void TorrentCreatorDlg::setInteractionEnabled(bool enabled) { m_ui->textInputPath->setEnabled(enabled); diff --git a/src/gui/torrentcreatordlg.h b/src/gui/torrentcreatordlg.h index 41fd0a8fe..f8235fe67 100644 --- a/src/gui/torrentcreatordlg.h +++ b/src/gui/torrentcreatordlg.h @@ -55,6 +55,7 @@ public: private slots: void updateProgressBar(int progress); + void updatePiecesCount(); void onCreateButtonClicked(); void onAddFileButtonClicked(); void onAddFolderButtonClicked(); diff --git a/src/gui/torrentcreatordlg.ui b/src/gui/torrentcreatordlg.ui index 2145bb939..29b6162ee 100644 --- a/src/gui/torrentcreatordlg.ui +++ b/src/gui/torrentcreatordlg.ui @@ -166,6 +166,20 @@ + + + + Calculate total pieces: + + + + + + + 0 + + +