2006-09-30 20:02:39 +04:00
|
|
|
/*
|
2017-05-17 16:14:46 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
|
|
|
* Copyright (C) 2017 Mike Tzou (Chocobo1)
|
|
|
|
* Copyright (C) 2010 Christophe Dumez <chris@qbittorrent.org>
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* This program is free software; you can redistribute it and/or
|
|
|
|
* modify it under the terms of the GNU General Public License
|
|
|
|
* as published by the Free Software Foundation; either version 2
|
|
|
|
* of the License, or (at your option) any later version.
|
2006-09-30 20:02:39 +04:00
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
2007-07-14 18:31:59 +04:00
|
|
|
* along with this program; if not, write to the Free Software
|
|
|
|
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*
|
2009-04-05 21:00:55 +04:00
|
|
|
* In addition, as a special exception, the copyright holders give permission to
|
|
|
|
* link this program with the OpenSSL project's "OpenSSL" library (or with
|
|
|
|
* modified versions of it that use the same license as the "OpenSSL" library),
|
|
|
|
* and distribute the linked executables. You must obey the GNU General Public
|
|
|
|
* License in all respects for all of the code used other than "OpenSSL". If you
|
|
|
|
* modify file(s), you may extend this exception to your version of the file(s),
|
|
|
|
* but you are not obligated to do so. If you do not wish to do so, delete this
|
|
|
|
* exception statement from your version.
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
#include "torrentcreatordlg.h"
|
|
|
|
|
|
|
|
#include <QCloseEvent>
|
2014-10-25 15:49:57 +04:00
|
|
|
#include <QDebug>
|
2006-09-30 20:02:39 +04:00
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2016-04-28 14:02:05 +03:00
|
|
|
#include <QMimeData>
|
|
|
|
#include <QUrl>
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2015-09-25 11:10:05 +03:00
|
|
|
#include "base/bittorrent/session.h"
|
|
|
|
#include "base/bittorrent/torrentcreatorthread.h"
|
2016-04-29 12:25:15 +03:00
|
|
|
#include "base/bittorrent/torrentinfo.h"
|
|
|
|
#include "base/global.h"
|
2016-04-28 14:02:05 +03:00
|
|
|
#include "base/utils/fs.h"
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
#include "ui_torrentcreatordlg.h"
|
2008-11-02 00:42:56 +03:00
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
#define SETTINGS_KEY(name) "TorrentCreator/" name
|
|
|
|
|
|
|
|
TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent, const QString &defaultPath)
|
|
|
|
: QDialog(parent)
|
|
|
|
, m_ui(new Ui::TorrentCreatorDlg)
|
2016-04-30 16:02:26 +03:00
|
|
|
, m_creatorThread(new BitTorrent::TorrentCreatorThread(this))
|
2016-05-04 12:56:51 +03:00
|
|
|
, m_storeDialogSize(SETTINGS_KEY("Dimension"))
|
|
|
|
, m_storePieceSize(SETTINGS_KEY("PieceSize"))
|
|
|
|
, m_storePrivateTorrent(SETTINGS_KEY("PrivateTorrent"))
|
|
|
|
, m_storeStartSeeding(SETTINGS_KEY("StartSeeding"))
|
|
|
|
, m_storeIgnoreRatio(SETTINGS_KEY("IgnoreRatio"))
|
|
|
|
, m_storeLastAddPath(SETTINGS_KEY("LastAddPath"), QDir::homePath())
|
|
|
|
, m_storeTrackerList(SETTINGS_KEY("TrackerList"))
|
|
|
|
, m_storeWebSeedList(SETTINGS_KEY("WebSeedList"))
|
|
|
|
, m_storeComments(SETTINGS_KEY("Comments"))
|
|
|
|
, m_storeLastSavePath(SETTINGS_KEY("LastSavePath"), QDir::homePath())
|
2016-04-28 14:02:05 +03:00
|
|
|
{
|
|
|
|
m_ui->setupUi(this);
|
2015-06-24 13:19:42 +03:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2016-04-30 13:43:41 +03:00
|
|
|
setModal(false);
|
2016-04-28 14:02:05 +03:00
|
|
|
|
|
|
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent"));
|
|
|
|
|
2017-05-17 16:14:46 +03:00
|
|
|
connect(m_ui->addFileButton, SIGNAL(clicked(bool)), SLOT(onAddFileButtonClicked()));
|
|
|
|
connect(m_ui->addFolderButton, SIGNAL(clicked(bool)), SLOT(onAddFolderButtonClicked()));
|
2016-04-28 14:02:05 +03:00
|
|
|
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(onCreateButtonClicked()));
|
2017-05-25 17:48:10 +03:00
|
|
|
connect(m_ui->buttonCalcTotalPieces, &QAbstractButton::clicked, this, &TorrentCreatorDlg::updatePiecesCount);
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2016-04-30 16:02:26 +03:00
|
|
|
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)));
|
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
loadSettings();
|
2016-04-30 13:43:41 +03:00
|
|
|
updateInputPath(defaultPath);
|
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
show();
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
TorrentCreatorDlg::~TorrentCreatorDlg()
|
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
saveSettings();
|
|
|
|
|
2017-05-15 10:53:58 +03:00
|
|
|
if (m_creatorThread)
|
2015-06-24 13:19:42 +03:00
|
|
|
delete m_creatorThread;
|
2016-04-28 14:02:05 +03:00
|
|
|
|
|
|
|
delete m_ui;
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2016-04-30 13:43:41 +03:00
|
|
|
void TorrentCreatorDlg::updateInputPath(const QString &path)
|
|
|
|
{
|
|
|
|
if (path.isEmpty()) return;
|
|
|
|
m_ui->textInputPath->setText(Utils::Fs::toNativePath(path));
|
|
|
|
updateProgressBar(0);
|
|
|
|
}
|
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::onAddFolderButtonClicked()
|
2015-06-24 13:19:42 +03:00
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
QString oldPath = m_ui->textInputPath->text();
|
|
|
|
QString path = QFileDialog::getExistingDirectory(this, tr("Select folder"), oldPath);
|
2016-04-30 13:43:41 +03:00
|
|
|
updateInputPath(path);
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::onAddFileButtonClicked()
|
2015-06-24 13:19:42 +03:00
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
QString oldPath = m_ui->textInputPath->text();
|
|
|
|
QString path = QFileDialog::getOpenFileName(this, tr("Select file"), oldPath);
|
2016-04-30 13:43:41 +03:00
|
|
|
updateInputPath(path);
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
int TorrentCreatorDlg::getPieceSize() const
|
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
const int pieceSizes[] = {0, 16, 32, 64, 128, 256, 512, 1024, 2048, 4096, 8192, 16384}; // base unit in KiB
|
|
|
|
return pieceSizes[m_ui->comboPieceSize->currentIndex()] * 1024;
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorDlg::dropEvent(QDropEvent *event)
|
|
|
|
{
|
|
|
|
event->acceptProposedAction();
|
|
|
|
|
|
|
|
if (event->mimeData()->hasUrls()) {
|
|
|
|
// only take the first one
|
|
|
|
QUrl firstItem = event->mimeData()->urls().first();
|
|
|
|
QString path = (firstItem.scheme().compare("file", Qt::CaseInsensitive) == 0)
|
|
|
|
? firstItem.toLocalFile() : firstItem.toString();
|
2016-04-30 13:43:41 +03:00
|
|
|
updateInputPath(path);
|
2016-04-28 14:02:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorDlg::dragEnterEvent(QDragEnterEvent *event)
|
|
|
|
{
|
|
|
|
if (event->mimeData()->hasFormat("text/plain") || event->mimeData()->hasFormat("text/uri-list"))
|
|
|
|
event->acceptProposedAction();
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
// Main function that create a .torrent file
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::onCreateButtonClicked()
|
2015-06-24 13:19:42 +03:00
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
QString input = Utils::Fs::fromNativePath(m_ui->textInputPath->text()).trimmed();
|
|
|
|
|
|
|
|
// test if readable
|
|
|
|
QFileInfo fi(input);
|
|
|
|
if (!fi.isReadable()) {
|
|
|
|
QMessageBox::critical(this, tr("Torrent creator failed"), tr("Reason: Path to file/folder is not readable."));
|
2015-06-24 13:19:42 +03:00
|
|
|
return;
|
|
|
|
}
|
2016-04-28 14:02:05 +03:00
|
|
|
input = fi.canonicalFilePath();
|
2015-06-24 13:19:42 +03:00
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
// get save path
|
2016-05-04 12:56:51 +03:00
|
|
|
QString lastPath = m_storeLastSavePath;
|
2016-04-28 14:02:05 +03:00
|
|
|
QString destination = QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), lastPath, tr("Torrent Files (*.torrent)"));
|
2015-06-24 13:19:42 +03:00
|
|
|
if (destination.isEmpty())
|
|
|
|
return;
|
2016-04-29 12:25:15 +03:00
|
|
|
if (!destination.endsWith(C_TORRENT_FILE_EXTENSION, Qt::CaseInsensitive))
|
|
|
|
destination += C_TORRENT_FILE_EXTENSION;
|
2016-05-04 12:56:51 +03:00
|
|
|
m_storeLastSavePath = Utils::Fs::branchPath(destination);
|
2010-08-13 18:02:19 +04:00
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
// Disable dialog & set busy cursor
|
2015-06-24 13:19:42 +03:00
|
|
|
setInteractionEnabled(false);
|
|
|
|
setCursor(QCursor(Qt::WaitCursor));
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2017-05-17 16:14:46 +03:00
|
|
|
QStringList trackers = m_ui->trackersList->toPlainText().split("\n");
|
|
|
|
QStringList urlSeeds = m_ui->URLSeedsList->toPlainText().split("\n");
|
|
|
|
QString comment = m_ui->txtComment->toPlainText();
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2016-04-30 16:02:26 +03:00
|
|
|
// run the creator thread
|
2017-05-17 16:14:46 +03:00
|
|
|
m_creatorThread->create(input, destination, trackers, urlSeeds, comment, m_ui->checkPrivate->isChecked(), getPieceSize());
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::handleCreationFailure(const QString &msg)
|
2015-06-24 13:19:42 +03:00
|
|
|
{
|
|
|
|
// Remove busy cursor
|
|
|
|
setCursor(QCursor(Qt::ArrowCursor));
|
2016-04-28 14:02:05 +03:00
|
|
|
QMessageBox::information(this, tr("Torrent creator failed"), tr("Reason: %1").arg(msg));
|
2015-06-24 13:19:42 +03:00
|
|
|
setInteractionEnabled(true);
|
|
|
|
}
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::handleCreationSuccess(const QString &path, const QString &branchPath)
|
2015-06-24 13:19:42 +03:00
|
|
|
{
|
|
|
|
// Remove busy cursor
|
|
|
|
setCursor(QCursor(Qt::ArrowCursor));
|
2016-04-28 14:02:05 +03:00
|
|
|
if (m_ui->checkStartSeeding->isChecked()) {
|
2015-06-24 13:19:42 +03:00
|
|
|
// Create save path temp data
|
|
|
|
BitTorrent::TorrentInfo t = BitTorrent::TorrentInfo::loadFromFile(Utils::Fs::toNativePath(path));
|
|
|
|
if (!t.isValid()) {
|
2016-04-28 14:02:05 +03:00
|
|
|
QMessageBox::critical(this, tr("Torrent creator failed"), tr("Reason: Created torrent is invalid. It won't be added to download list."));
|
2015-06-24 13:19:42 +03:00
|
|
|
return;
|
|
|
|
}
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
BitTorrent::AddTorrentParams params;
|
2016-04-28 14:02:05 +03:00
|
|
|
params.savePath = branchPath;
|
2015-06-24 13:19:42 +03:00
|
|
|
params.skipChecking = true;
|
2016-04-28 14:02:05 +03:00
|
|
|
params.ignoreShareLimits = m_ui->checkIgnoreShareLimits->isChecked();
|
2015-04-19 18:17:47 +03:00
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
BitTorrent::Session::instance()->addTorrent(t, params);
|
|
|
|
}
|
2016-04-28 14:02:05 +03:00
|
|
|
QMessageBox::information(this, tr("Torrent creator"), QString("%1\n%2").arg(tr("Create torrent success:")).arg(Utils::Fs::toNativePath(path)));
|
2016-04-30 13:43:41 +03:00
|
|
|
setInteractionEnabled(true);
|
2010-06-17 21:52:14 +04:00
|
|
|
}
|
|
|
|
|
2015-06-24 13:19:42 +03:00
|
|
|
void TorrentCreatorDlg::updateProgressBar(int progress)
|
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
m_ui->progressBar->setValue(progress);
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2017-05-25 17:48:10 +03:00
|
|
|
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));
|
|
|
|
}
|
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
void TorrentCreatorDlg::setInteractionEnabled(bool enabled)
|
|
|
|
{
|
2016-04-28 14:02:05 +03:00
|
|
|
m_ui->textInputPath->setEnabled(enabled);
|
2017-05-17 16:14:46 +03:00
|
|
|
m_ui->addFileButton->setEnabled(enabled);
|
|
|
|
m_ui->addFolderButton->setEnabled(enabled);
|
|
|
|
m_ui->trackersList->setEnabled(enabled);
|
|
|
|
m_ui->URLSeedsList->setEnabled(enabled);
|
|
|
|
m_ui->txtComment->setEnabled(enabled);
|
2016-04-28 14:02:05 +03:00
|
|
|
m_ui->comboPieceSize->setEnabled(enabled);
|
2017-05-17 16:14:46 +03:00
|
|
|
m_ui->checkPrivate->setEnabled(enabled);
|
2016-04-28 14:02:05 +03:00
|
|
|
m_ui->checkStartSeeding->setEnabled(enabled);
|
|
|
|
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
|
|
|
|
m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked());
|
2010-10-25 23:34:42 +04:00
|
|
|
}
|
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::saveSettings()
|
2010-10-25 23:34:42 +04:00
|
|
|
{
|
2016-05-04 12:56:51 +03:00
|
|
|
m_storeLastAddPath = m_ui->textInputPath->text().trimmed();
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2016-05-04 12:56:51 +03:00
|
|
|
m_storePieceSize = m_ui->comboPieceSize->currentIndex();
|
2017-05-17 16:14:46 +03:00
|
|
|
m_storePrivateTorrent = m_ui->checkPrivate->isChecked();
|
2016-05-04 12:56:51 +03:00
|
|
|
m_storeStartSeeding = m_ui->checkStartSeeding->isChecked();
|
|
|
|
m_storeIgnoreRatio = m_ui->checkIgnoreShareLimits->isChecked();
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2017-05-17 16:14:46 +03:00
|
|
|
m_storeTrackerList = m_ui->trackersList->toPlainText();
|
|
|
|
m_storeWebSeedList = m_ui->URLSeedsList->toPlainText();
|
|
|
|
m_storeComments = m_ui->txtComment->toPlainText();
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2016-05-04 12:56:51 +03:00
|
|
|
m_storeDialogSize = size();
|
2009-07-23 13:11:05 +04:00
|
|
|
}
|
|
|
|
|
2016-04-28 14:02:05 +03:00
|
|
|
void TorrentCreatorDlg::loadSettings()
|
2010-10-25 23:34:42 +04:00
|
|
|
{
|
2016-05-04 12:56:51 +03:00
|
|
|
m_ui->textInputPath->setText(m_storeLastAddPath);
|
2016-04-28 14:02:05 +03:00
|
|
|
|
2016-05-04 12:56:51 +03:00
|
|
|
m_ui->comboPieceSize->setCurrentIndex(m_storePieceSize);
|
2017-05-17 16:14:46 +03:00
|
|
|
m_ui->checkPrivate->setChecked(m_storePrivateTorrent);
|
2016-05-04 12:56:51 +03:00
|
|
|
m_ui->checkStartSeeding->setChecked(m_storeStartSeeding);
|
|
|
|
m_ui->checkIgnoreShareLimits->setChecked(m_storeIgnoreRatio);
|
2016-04-28 14:02:05 +03:00
|
|
|
m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked());
|
|
|
|
|
2017-05-17 16:14:46 +03:00
|
|
|
m_ui->trackersList->setPlainText(m_storeTrackerList);
|
|
|
|
m_ui->URLSeedsList->setPlainText(m_storeWebSeedList);
|
|
|
|
m_ui->txtComment->setPlainText(m_storeComments);
|
2010-12-04 13:31:14 +03:00
|
|
|
|
2016-05-04 12:56:51 +03:00
|
|
|
if (m_storeDialogSize.value().isValid())
|
|
|
|
resize(m_storeDialogSize);
|
2010-12-04 13:31:14 +03:00
|
|
|
}
|