2006-09-30 20:02:39 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2010-10-25 23:34:42 +04:00
|
|
|
* Copyright (C) 2010 Christophe Dumez
|
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.
|
|
|
|
*
|
2007-07-14 18:31:59 +04:00
|
|
|
* Contact : chris@qbittorrent.org
|
2006-09-30 20:02:39 +04:00
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QFileDialog>
|
|
|
|
#include <QMessageBox>
|
2007-04-04 22:55:38 +04:00
|
|
|
#include <QInputDialog>
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2009-11-29 12:01:33 +03:00
|
|
|
#include "torrentpersistentdata.h"
|
2010-10-23 00:13:22 +04:00
|
|
|
#include "torrentcreatordlg.h"
|
2012-05-16 22:19:05 +04:00
|
|
|
#include "fs_utils.h"
|
2007-12-30 21:06:51 +03:00
|
|
|
#include "misc.h"
|
2010-08-13 18:02:19 +04:00
|
|
|
#include "qinisettings.h"
|
2010-10-25 23:34:42 +04:00
|
|
|
#include "torrentcreatorthread.h"
|
2011-01-01 16:05:28 +03:00
|
|
|
#include "iconprovider.h"
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
const uint NB_PIECES_MIN = 1200;
|
|
|
|
const uint NB_PIECES_MAX = 2200;
|
2008-11-02 00:42:56 +03:00
|
|
|
|
2010-11-23 00:55:32 +03:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
TorrentCreatorDlg::TorrentCreatorDlg(QWidget *parent): QDialog(parent), creatorThread(0) {
|
2006-09-30 20:02:39 +04:00
|
|
|
setupUi(this);
|
2010-12-12 22:37:59 +03:00
|
|
|
// Icons
|
2011-01-01 16:05:28 +03:00
|
|
|
addFile_button->setIcon(IconProvider::instance()->getIcon("document-new"));
|
|
|
|
addFolder_button->setIcon(IconProvider::instance()->getIcon("folder-new"));
|
|
|
|
createButton->setIcon(IconProvider::instance()->getIcon("document-save"));
|
|
|
|
cancelButton->setIcon(IconProvider::instance()->getIcon("dialog-cancel"));
|
2010-12-12 22:37:59 +03:00
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2010-03-10 23:21:56 +03:00
|
|
|
setModal(true);
|
2010-10-25 23:34:42 +04:00
|
|
|
showProgressBar(false);
|
|
|
|
loadTrackerList();
|
|
|
|
// Piece sizes
|
|
|
|
m_piece_sizes << 32 << 64 << 128 << 256 << 512 << 1024 << 2048 << 4096;
|
2010-12-04 13:31:14 +03:00
|
|
|
loadSettings();
|
2006-09-30 20:02:39 +04:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2010-10-23 00:13:22 +04:00
|
|
|
TorrentCreatorDlg::~TorrentCreatorDlg() {
|
2012-02-20 21:30:53 +04:00
|
|
|
if (creatorThread)
|
2010-10-25 23:34:42 +04:00
|
|
|
delete creatorThread;
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
void TorrentCreatorDlg::on_addFolder_button_clicked() {
|
2010-08-13 18:02:19 +04:00
|
|
|
QIniSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
QString last_path = settings.value("CreateTorrent/last_add_path", QDir::homePath()).toString();
|
|
|
|
QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), last_path, QFileDialog::ShowDirsOnly);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!dir.isEmpty()) {
|
2010-08-13 18:02:19 +04:00
|
|
|
settings.setValue("CreateTorrent/last_add_path", dir);
|
2010-06-22 23:17:47 +04:00
|
|
|
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
|
2011-09-25 16:04:51 +04:00
|
|
|
dir.replace("/", "\\");
|
2010-06-04 01:08:28 +04:00
|
|
|
#endif
|
2007-09-04 08:18:51 +04:00
|
|
|
textInputPath->setText(dir);
|
2010-10-25 23:34:42 +04:00
|
|
|
// Update piece size
|
2012-02-20 21:30:53 +04:00
|
|
|
if (checkAutoPieceSize->isChecked())
|
2010-10-25 23:34:42 +04:00
|
|
|
updateOptimalPieceSize();
|
2010-06-04 01:08:28 +04:00
|
|
|
}
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
void TorrentCreatorDlg::on_addFile_button_clicked() {
|
2010-08-13 18:02:19 +04:00
|
|
|
QIniSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
QString last_path = settings.value("CreateTorrent/last_add_path", QDir::homePath()).toString();
|
|
|
|
QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), last_path);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!file.isEmpty()) {
|
2012-05-16 22:19:05 +04:00
|
|
|
settings.setValue("CreateTorrent/last_add_path", fsutils::branchPath(file));
|
2010-06-22 23:17:47 +04:00
|
|
|
#if defined(Q_WS_WIN) || defined(Q_OS_OS2)
|
2011-09-25 16:04:51 +04:00
|
|
|
file.replace("/", "\\");
|
2010-06-04 01:08:28 +04:00
|
|
|
#endif
|
2007-09-04 08:18:51 +04:00
|
|
|
textInputPath->setText(file);
|
2010-10-25 23:34:42 +04:00
|
|
|
// Update piece size
|
2012-02-20 21:30:53 +04:00
|
|
|
if (checkAutoPieceSize->isChecked())
|
2010-10-25 23:34:42 +04:00
|
|
|
updateOptimalPieceSize();
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-10-23 00:13:22 +04:00
|
|
|
int TorrentCreatorDlg::getPieceSize() const {
|
2010-10-25 23:34:42 +04:00
|
|
|
return m_piece_sizes.at(comboPieceSize->currentIndex())*1024;
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
// Main function that create a .torrent file
|
2012-02-20 21:56:07 +04:00
|
|
|
void TorrentCreatorDlg::on_createButton_clicked() {
|
2007-09-04 08:18:51 +04:00
|
|
|
QString input = textInputPath->text().trimmed();
|
2007-12-30 21:06:51 +03:00
|
|
|
if (input.endsWith(QDir::separator()))
|
|
|
|
input.chop(1);
|
2012-02-20 21:56:07 +04:00
|
|
|
if (input.isEmpty()) {
|
2007-09-04 08:18:51 +04:00
|
|
|
QMessageBox::critical(0, tr("No input path set"), tr("Please type an input path first"));
|
2006-09-30 20:02:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-10-25 23:34:42 +04:00
|
|
|
QStringList trackers = trackers_list->toPlainText().split("\n");
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!trackers_list->toPlainText().trimmed().isEmpty())
|
2010-10-25 23:34:42 +04:00
|
|
|
saveTrackerList();
|
2010-08-13 18:02:19 +04:00
|
|
|
|
|
|
|
QIniSettings settings("qBittorrent", "qBittorrent");
|
|
|
|
QString last_path = settings.value("CreateTorrent/last_save_path", QDir::homePath()).toString();
|
|
|
|
|
|
|
|
QString destination = QFileDialog::getSaveFileName(this, tr("Select destination torrent file"), last_path, tr("Torrent Files")+QString::fromUtf8(" (*.torrent)"));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!destination.isEmpty()) {
|
2012-05-16 22:19:05 +04:00
|
|
|
settings.setValue("CreateTorrent/last_save_path", fsutils::branchPath(destination));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!destination.toUpper().endsWith(".TORRENT"))
|
2010-09-25 14:46:29 +04:00
|
|
|
destination += QString::fromUtf8(".torrent");
|
2007-09-04 08:18:51 +04:00
|
|
|
} else {
|
2006-09-30 20:02:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2010-06-17 21:52:14 +04:00
|
|
|
// Disable dialog
|
2010-10-25 23:34:42 +04:00
|
|
|
setInteractionEnabled(false);
|
|
|
|
showProgressBar(true);
|
2010-06-17 21:52:14 +04:00
|
|
|
// Set busy cursor
|
|
|
|
setCursor(QCursor(Qt::WaitCursor));
|
|
|
|
// Actually create the torrent
|
2010-10-25 23:34:42 +04:00
|
|
|
QStringList url_seeds = URLSeeds_list->toPlainText().split("\n");
|
2007-12-31 19:57:35 +03:00
|
|
|
QString comment = txt_comment->toPlainText();
|
2010-10-25 23:34:42 +04:00
|
|
|
// Create the creator thread
|
|
|
|
creatorThread = new TorrentCreatorThread(this);
|
|
|
|
connect(creatorThread, SIGNAL(creationSuccess(QString, QString)), this, SLOT(handleCreationSuccess(QString, QString)));
|
|
|
|
connect(creatorThread, SIGNAL(creationFailure(QString)), this, SLOT(handleCreationFailure(QString)));
|
|
|
|
connect(creatorThread, SIGNAL(updateProgress(int)), this, SLOT(updateProgressBar(int)));
|
2007-12-31 19:57:35 +03:00
|
|
|
creatorThread->create(input, destination, trackers, url_seeds, comment, check_private->isChecked(), getPieceSize());
|
|
|
|
}
|
|
|
|
|
2010-10-23 00:13:22 +04:00
|
|
|
void TorrentCreatorDlg::handleCreationFailure(QString msg) {
|
2010-06-17 21:52:14 +04:00
|
|
|
// Remove busy cursor
|
|
|
|
setCursor(QCursor(Qt::ArrowCursor));
|
2007-12-31 19:57:35 +03:00
|
|
|
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(msg));
|
2010-10-25 23:34:42 +04:00
|
|
|
setInteractionEnabled(true);
|
|
|
|
showProgressBar(false);
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:13:22 +04:00
|
|
|
void TorrentCreatorDlg::handleCreationSuccess(QString path, QString branch_path) {
|
2010-06-17 21:52:14 +04:00
|
|
|
// Remove busy cursor
|
|
|
|
setCursor(QCursor(Qt::ArrowCursor));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (checkStartSeeding->isChecked()) {
|
2009-11-29 12:11:39 +03:00
|
|
|
// Create save path temp data
|
|
|
|
boost::intrusive_ptr<torrent_info> t;
|
|
|
|
try {
|
2010-06-06 17:58:43 +04:00
|
|
|
t = new torrent_info(path.toUtf8().data());
|
2009-11-29 12:11:39 +03:00
|
|
|
} catch(std::exception&) {
|
|
|
|
QMessageBox::critical(0, tr("Torrent creation"), tr("Created torrent file is invalid. It won't be added to download list."));
|
|
|
|
return;
|
2008-11-02 00:42:56 +03:00
|
|
|
}
|
2009-11-29 12:11:39 +03:00
|
|
|
QString hash = misc::toQString(t->info_hash());
|
2010-10-17 12:41:45 +04:00
|
|
|
QString save_path = branch_path;
|
|
|
|
TorrentTempData::setSavePath(hash, save_path);
|
2009-11-29 12:11:39 +03:00
|
|
|
// Enable seeding mode (do not recheck the files)
|
|
|
|
TorrentTempData::setSeedingMode(hash, true);
|
|
|
|
emit torrent_to_seed(path);
|
|
|
|
}
|
|
|
|
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent was created successfully:")+" "+path);
|
|
|
|
close();
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:13:22 +04:00
|
|
|
void TorrentCreatorDlg::on_cancelButton_clicked() {
|
2010-06-17 21:52:14 +04:00
|
|
|
// End torrent creation thread
|
2012-02-20 21:30:53 +04:00
|
|
|
if (creatorThread && creatorThread->isRunning()) {
|
2010-06-17 21:52:14 +04:00
|
|
|
creatorThread->abortCreation();
|
|
|
|
creatorThread->terminate();
|
|
|
|
// Wait for termination
|
|
|
|
creatorThread->wait();
|
|
|
|
}
|
|
|
|
// Close the dialog
|
2010-12-04 13:31:14 +03:00
|
|
|
close();
|
2010-06-17 21:52:14 +04:00
|
|
|
}
|
|
|
|
|
2010-10-23 00:13:22 +04:00
|
|
|
void TorrentCreatorDlg::updateProgressBar(int progress) {
|
2007-12-31 19:57:35 +03:00
|
|
|
progressBar->setValue(progress);
|
|
|
|
}
|
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
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);
|
|
|
|
check_private->setEnabled(enabled);
|
|
|
|
checkStartSeeding->setEnabled(enabled);
|
|
|
|
createButton->setEnabled(enabled);
|
|
|
|
//cancelButton->setEnabled(!enabled);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorDlg::showProgressBar(bool show)
|
|
|
|
{
|
|
|
|
progressLbl->setVisible(show);
|
|
|
|
progressBar->setVisible(show);
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
void TorrentCreatorDlg::on_checkAutoPieceSize_clicked(bool checked)
|
|
|
|
{
|
|
|
|
comboPieceSize->setEnabled(!checked);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (checked) {
|
2010-10-25 23:34:42 +04:00
|
|
|
updateOptimalPieceSize();
|
|
|
|
}
|
2009-07-23 13:11:05 +04:00
|
|
|
}
|
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
void TorrentCreatorDlg::updateOptimalPieceSize()
|
|
|
|
{
|
2012-05-16 22:19:05 +04:00
|
|
|
quint64 torrent_size = fsutils::computePathSize(textInputPath->text());
|
2010-10-25 23:34:42 +04:00
|
|
|
qDebug("Torrent size is %lld", torrent_size);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (torrent_size == 0) return;
|
2010-10-25 23:34:42 +04:00
|
|
|
int i = 0;
|
|
|
|
qulonglong nb_pieces = 0;
|
|
|
|
do {
|
|
|
|
nb_pieces = (double)torrent_size/(m_piece_sizes.at(i)*1024.);
|
|
|
|
qDebug("nb_pieces=%lld with piece_size=%s", nb_pieces, qPrintable(comboPieceSize->itemText(i)));
|
2012-02-20 21:30:53 +04:00
|
|
|
if (nb_pieces <= NB_PIECES_MIN) {
|
|
|
|
if (i > 1)
|
2010-10-25 23:34:42 +04:00
|
|
|
--i;
|
|
|
|
break;
|
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (nb_pieces < NB_PIECES_MAX) {
|
2010-10-25 23:34:42 +04:00
|
|
|
qDebug("Good, nb_pieces=%lld < %d", nb_pieces, NB_PIECES_MAX);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
++i;
|
|
|
|
}while(i<m_piece_sizes.size());
|
|
|
|
comboPieceSize->setCurrentIndex(i);
|
2008-11-02 00:42:56 +03:00
|
|
|
}
|
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
void TorrentCreatorDlg::saveTrackerList()
|
|
|
|
{
|
|
|
|
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
settings.setValue("CreateTorrent/TrackerList", trackers_list->toPlainText());
|
|
|
|
}
|
2008-11-02 00:42:56 +03:00
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
void TorrentCreatorDlg::loadTrackerList()
|
|
|
|
{
|
|
|
|
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
trackers_list->setPlainText(settings.value("CreateTorrent/TrackerList", "").toString());
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2010-12-04 13:31:14 +03:00
|
|
|
|
|
|
|
void TorrentCreatorDlg::saveSettings()
|
|
|
|
{
|
|
|
|
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
settings.setValue("CreateTorrent/dimensions", saveGeometry());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorDlg::loadSettings()
|
|
|
|
{
|
|
|
|
QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent"));
|
|
|
|
restoreGeometry(settings.value("CreateTorrent/dimensions").toByteArray());
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorDlg::closeEvent(QCloseEvent *event)
|
|
|
|
{
|
|
|
|
qDebug() << Q_FUNC_INFO;
|
|
|
|
saveSettings();
|
|
|
|
QDialog::closeEvent(event);
|
|
|
|
}
|