qBittorrent/src/gui/torrentcreatordlg.cpp

378 lines
13 KiB
C++
Raw Normal View History

/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2010 Christophe Dumez
*
* 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.
*
* 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
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* 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.
*
* Contact : chris@qbittorrent.org
*/
2016-04-28 14:02:05 +03:00
#include "torrentcreatordlg.h"
#include <QCloseEvent>
#include <QDebug>
#include <QFileDialog>
#include <QMessageBox>
2016-04-28 14:02:05 +03:00
#include <QMimeData>
#include <QUrl>
2015-09-25 11:10:05 +03:00
#include "base/bittorrent/session.h"
#include "base/bittorrent/torrentinfo.h"
#include "base/bittorrent/torrentcreatorthread.h"
2016-04-28 14:02:05 +03:00
#include "base/settingsstorage.h"
#include "base/utils/fs.h"
#include "guiiconprovider.h"
2016-04-28 14:02:05 +03:00
#include "ui_torrentcreatordlg.h"
2016-04-28 14:02:05 +03:00
namespace
{
2016-04-28 14:02:05 +03:00
#define SETTINGS_KEY(name) "TorrentCreator/" name
const QString KEY_DIALOG_SIZE = SETTINGS_KEY("Dimension");
const QString KEY_PIECE_SIZE = SETTINGS_KEY("PieceSize");
const QString KEY_PRIVATE_TORRENT = SETTINGS_KEY("PrivateTorrent");
const QString KEY_START_SEEDING = SETTINGS_KEY("StartSeeding");
const QString KEY_SETTING_IGNORE_RATIO = SETTINGS_KEY("IgnoreRatio");
const QString KEY_LAST_ADD_PATH = SETTINGS_KEY("LastAddPath");
const QString KEY_TRACKER_LIST = SETTINGS_KEY("TrackerList");
const QString KEY_WEB_SEED_LIST = SETTINGS_KEY("WebSeedList");
const QString KEY_COMMENTS = SETTINGS_KEY("Comments");
const QString KEY_LAST_SAVE_PATH = SETTINGS_KEY("LastSavePath");
2016-04-28 14:02:05 +03:00
SettingsStorage *settings() { return SettingsStorage::instance(); }
}
TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent, const QString &defaultPath)
: QDialog(parent)
, m_ui(new Ui::TorrentCreatorDlg)
, m_creatorThread(nullptr)
, m_defaultPath(Utils::Fs::toNativePath(defaultPath))
{
m_ui->setupUi(this);
setAttribute(Qt::WA_DeleteOnClose);
setModal(true);
2016-04-28 14:02:05 +03:00
showProgressBar(false);
2016-04-28 14:02:05 +03:00
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setText(tr("Create Torrent"));
connect(m_ui->addFile_button, SIGNAL(clicked(bool)), SLOT(onAddFileButtonClicked()));
connect(m_ui->addFolder_button, SIGNAL(clicked(bool)), SLOT(onAddFolderButtonClicked()));
connect(m_ui->buttonBox, SIGNAL(accepted()), SLOT(onCreateButtonClicked()));
loadSettings();
show();
}
TorrentCreatorDlg::~TorrentCreatorDlg()
{
2016-04-28 14:02:05 +03:00
saveSettings();
// End torrent creation thread
if (m_creatorThread) {
if (m_creatorThread->isRunning()) {
m_creatorThread->abortCreation();
m_creatorThread->terminate();
// Wait for termination
m_creatorThread->wait();
}
delete m_creatorThread;
2016-04-28 14:02:05 +03:00
}
delete m_ui;
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::onAddFolderButtonClicked()
{
2016-04-28 14:02:05 +03:00
QString oldPath = m_ui->textInputPath->text();
QString path = QFileDialog::getExistingDirectory(this, tr("Select folder"), oldPath);
if (!path.isEmpty())
m_ui->textInputPath->setText(Utils::Fs::toNativePath(path));
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::onAddFileButtonClicked()
{
2016-04-28 14:02:05 +03:00
QString oldPath = m_ui->textInputPath->text();
QString path = QFileDialog::getOpenFileName(this, tr("Select file"), oldPath);
if (!path.isEmpty())
m_ui->textInputPath->setText(Utils::Fs::toNativePath(path));
}
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();
m_ui->textInputPath->setText(Utils::Fs::toNativePath(path));
}
}
void TorrentCreatorDlg::dragEnterEvent(QDragEnterEvent *event)
{
if (event->mimeData()->hasFormat("text/plain") || event->mimeData()->hasFormat("text/uri-list"))
event->acceptProposedAction();
}
// Main function that create a .torrent file
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::onCreateButtonClicked()
{
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."));
return;
}
2016-04-28 14:02:05 +03:00
input = fi.canonicalFilePath();
2016-04-28 14:02:05 +03:00
// get save path
QString lastPath = getLastSavePath();
QString destination = QFileDialog::getSaveFileName(this, tr("Select where to save the new torrent"), lastPath, tr("Torrent Files (*.torrent)"));
if (destination.isEmpty())
return;
2016-04-28 14:02:05 +03:00
if (!destination.endsWith(".torrent", Qt::CaseInsensitive))
destination += ".torrent";
setLastSavePath(Utils::Fs::branchPath(destination));
2016-04-28 14:02:05 +03:00
// Disable dialog & set busy cursor
setInteractionEnabled(false);
showProgressBar(true);
setCursor(QCursor(Qt::WaitCursor));
2016-04-28 14:02:05 +03:00
QStringList trackers = m_ui->trackers_list->toPlainText().split("\n");
QStringList urlSeeds = m_ui->URLSeeds_list->toPlainText().split("\n");
QString comment = m_ui->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)));
2016-04-28 14:02:05 +03:00
m_creatorThread->create(input, destination, trackers, urlSeeds, comment, m_ui->check_private->isChecked(), getPieceSize());
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::handleCreationFailure(const QString &msg)
{
// 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));
setInteractionEnabled(true);
showProgressBar(false);
}
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)
{
// Remove busy cursor
setCursor(QCursor(Qt::ArrowCursor));
2016-04-28 14:02:05 +03:00
if (m_ui->checkStartSeeding->isChecked()) {
// 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."));
return;
}
2015-04-19 18:17:47 +03:00
BitTorrent::AddTorrentParams params;
2016-04-28 14:02:05 +03:00
params.savePath = branchPath;
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
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)));
close();
}
void TorrentCreatorDlg::updateProgressBar(int progress)
{
2016-04-28 14:02:05 +03:00
m_ui->progressBar->setValue(progress);
}
void TorrentCreatorDlg::setInteractionEnabled(bool enabled)
{
2016-04-28 14:02:05 +03:00
m_ui->textInputPath->setEnabled(enabled);
m_ui->addFile_button->setEnabled(enabled);
m_ui->addFolder_button->setEnabled(enabled);
m_ui->trackers_list->setEnabled(enabled);
m_ui->URLSeeds_list->setEnabled(enabled);
m_ui->txt_comment->setEnabled(enabled);
m_ui->comboPieceSize->setEnabled(enabled);
m_ui->check_private->setEnabled(enabled);
m_ui->checkStartSeeding->setEnabled(enabled);
m_ui->buttonBox->button(QDialogButtonBox::Ok)->setEnabled(enabled);
m_ui->checkIgnoreShareLimits->setEnabled(enabled && m_ui->checkStartSeeding->isChecked());
}
void TorrentCreatorDlg::showProgressBar(bool show)
{
2016-04-28 14:02:05 +03:00
m_ui->progressLbl->setVisible(show);
m_ui->progressBar->setVisible(show);
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::saveSettings()
{
2016-04-28 14:02:05 +03:00
setLastAddPath(m_ui->textInputPath->text().trimmed());
setSettingPieceSize(m_ui->comboPieceSize->currentIndex());
setSettingPrivateTorrent(m_ui->check_private->isChecked());
setSettingStartSeeding(m_ui->checkStartSeeding->isChecked());
setSettingIgnoreRatio(m_ui->checkIgnoreShareLimits->isChecked());
setTrackerList(m_ui->trackers_list->toPlainText());
setWebSeedList(m_ui->URLSeeds_list->toPlainText());
setComments(m_ui->txt_comment->toPlainText());
setDialogSize(size());
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::loadSettings()
{
2016-04-28 14:02:05 +03:00
m_ui->textInputPath->setText(!m_defaultPath.isEmpty() ? m_defaultPath : getLastAddPath());
m_ui->comboPieceSize->setCurrentIndex(getSettingPieceSize());
m_ui->check_private->setChecked(getSettingPrivateTorrent());
m_ui->checkStartSeeding->setChecked(getSettingStartSeeding());
m_ui->checkIgnoreShareLimits->setChecked(getSettingIgnoreRatio());
m_ui->checkIgnoreShareLimits->setEnabled(m_ui->checkStartSeeding->isChecked());
m_ui->trackers_list->setPlainText(getTrackerList());
m_ui->URLSeeds_list->setPlainText(getWebSeedList());
m_ui->txt_comment->setPlainText(getComments());
resize(getDialogSize());
}
2016-04-28 14:02:05 +03:00
QSize TorrentCreatorDlg::getDialogSize() const
{
2016-04-28 14:02:05 +03:00
return settings()->loadValue(KEY_DIALOG_SIZE, size()).toSize();
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::setDialogSize(const QSize &size)
{
2016-04-28 14:02:05 +03:00
settings()->storeValue(KEY_DIALOG_SIZE, size);
}
2010-12-04 13:31:14 +03:00
2016-04-28 14:02:05 +03:00
int TorrentCreatorDlg::getSettingPieceSize() const
2010-12-04 13:31:14 +03:00
{
2016-04-28 14:02:05 +03:00
return settings()->loadValue(KEY_PIECE_SIZE).toInt();
2010-12-04 13:31:14 +03:00
}
2016-04-28 14:02:05 +03:00
void TorrentCreatorDlg::setSettingPieceSize(const int size)
2010-12-04 13:31:14 +03:00
{
2016-04-28 14:02:05 +03:00
settings()->storeValue(KEY_PIECE_SIZE, size);
2010-12-04 13:31:14 +03:00
}
2016-04-28 14:02:05 +03:00
bool TorrentCreatorDlg::getSettingPrivateTorrent() const
2010-12-04 13:31:14 +03:00
{
2016-04-28 14:02:05 +03:00
return settings()->loadValue(KEY_PRIVATE_TORRENT).toBool();
}
void TorrentCreatorDlg::setSettingPrivateTorrent(const bool b)
{
settings()->storeValue(KEY_PRIVATE_TORRENT, b);
}
bool TorrentCreatorDlg::getSettingStartSeeding() const
{
return settings()->loadValue(KEY_START_SEEDING).toBool();
}
void TorrentCreatorDlg::setSettingStartSeeding(const bool b)
{
settings()->storeValue(KEY_START_SEEDING, b);
}
bool TorrentCreatorDlg::getSettingIgnoreRatio() const
{
return settings()->loadValue(KEY_SETTING_IGNORE_RATIO).toBool();
}
void TorrentCreatorDlg::setSettingIgnoreRatio(const bool ignore)
{
settings()->storeValue(KEY_SETTING_IGNORE_RATIO, ignore);
}
QString TorrentCreatorDlg::getLastAddPath() const
{
return settings()->loadValue(KEY_LAST_ADD_PATH, QDir::homePath()).toString();
}
void TorrentCreatorDlg::setLastAddPath(const QString &path)
{
settings()->storeValue(KEY_LAST_ADD_PATH, path);
}
QString TorrentCreatorDlg::getTrackerList() const
{
return settings()->loadValue(KEY_TRACKER_LIST).toString();
}
void TorrentCreatorDlg::setTrackerList(const QString &list)
{
settings()->storeValue(KEY_TRACKER_LIST, list);
}
QString TorrentCreatorDlg::getWebSeedList() const
{
return settings()->loadValue(KEY_WEB_SEED_LIST).toString();
}
void TorrentCreatorDlg::setWebSeedList(const QString &list)
{
settings()->storeValue(KEY_WEB_SEED_LIST, list);
}
QString TorrentCreatorDlg::getComments() const
{
return settings()->loadValue(KEY_COMMENTS).toString();
}
void TorrentCreatorDlg::setComments(const QString &str)
{
settings()->storeValue(KEY_COMMENTS, str);
}
QString TorrentCreatorDlg::getLastSavePath() const
{
return settings()->loadValue(KEY_LAST_SAVE_PATH, QDir::homePath()).toString();
}
void TorrentCreatorDlg::setLastSavePath(const QString &path)
{
settings()->storeValue(KEY_LAST_SAVE_PATH, path);
2010-12-04 13:31:14 +03:00
}