2006-09-30 20:02:39 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
2007-07-14 18:31:59 +04:00
|
|
|
* Copyright (C) 2006 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
|
|
|
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2008-11-02 00:42:56 +03:00
|
|
|
#include <boost/bind.hpp>
|
2006-09-30 20:02:39 +04:00
|
|
|
|
2006-12-28 02:56:30 +03:00
|
|
|
#include <libtorrent/entry.hpp>
|
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include <libtorrent/torrent_info.hpp>
|
|
|
|
#include <libtorrent/file.hpp>
|
|
|
|
#include <libtorrent/storage.hpp>
|
|
|
|
#include <libtorrent/hasher.hpp>
|
|
|
|
#include <libtorrent/file_pool.hpp>
|
2008-11-02 00:42:56 +03:00
|
|
|
#include <libtorrent/create_torrent.hpp>
|
2006-09-30 20:02:39 +04:00
|
|
|
|
|
|
|
#include "createtorrent_imp.h"
|
2007-12-30 21:06:51 +03:00
|
|
|
#include "misc.h"
|
2006-09-30 20:02:39 +04:00
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
// do not include files and folders whose
|
|
|
|
// name starts with a .
|
|
|
|
bool file_filter(boost::filesystem::path const& filename)
|
|
|
|
{
|
|
|
|
if (filename.leaf()[0] == '.') return false;
|
|
|
|
std::cerr << filename << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
createtorrent::createtorrent(QWidget *parent): QDialog(parent){
|
|
|
|
setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
2008-11-02 00:42:56 +03:00
|
|
|
creatorThread = new torrentCreatorThread(this);
|
|
|
|
connect(creatorThread, SIGNAL(creationSuccess(QString, const char*)), this, SLOT(handleCreationSuccess(QString, const char*)));
|
2007-12-31 19:57:35 +03:00
|
|
|
connect(creatorThread, SIGNAL(creationFailure(QString)), this, SLOT(handleCreationFailure(QString)));
|
|
|
|
connect(creatorThread, SIGNAL(updateProgress(int)), this, SLOT(updateProgressBar(int)));
|
2008-11-02 00:42:56 +03:00
|
|
|
path::default_name_check(no_check);
|
2006-09-30 20:02:39 +04:00
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
2007-12-31 19:57:35 +03:00
|
|
|
createtorrent::~createtorrent() {
|
|
|
|
delete creatorThread;
|
|
|
|
}
|
|
|
|
|
2007-04-04 22:55:38 +04:00
|
|
|
void createtorrent::on_addFolder_button_clicked(){
|
|
|
|
QString dir = QFileDialog::getExistingDirectory(this, tr("Select a folder to add to the torrent"), QDir::homePath(), QFileDialog::ShowDirsOnly);
|
2007-09-04 08:18:51 +04:00
|
|
|
if(!dir.isEmpty())
|
|
|
|
textInputPath->setText(dir);
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void createtorrent::on_addFile_button_clicked(){
|
2007-09-02 21:03:33 +04:00
|
|
|
QString file = QFileDialog::getOpenFileName(this, tr("Select a file to add to the torrent"), QDir::homePath(), QString(), 0, QFileDialog::ShowDirsOnly);
|
2007-09-04 08:18:51 +04:00
|
|
|
if(!file.isEmpty())
|
|
|
|
textInputPath->setText(file);
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
2007-09-04 08:18:51 +04:00
|
|
|
void createtorrent::on_removeTracker_button_clicked() {
|
2007-04-04 22:55:38 +04:00
|
|
|
QModelIndexList selectedIndexes = trackers_list->selectionModel()->selectedIndexes();
|
|
|
|
for(int i=selectedIndexes.size()-1; i>=0; --i){
|
|
|
|
QListWidgetItem *item = trackers_list->takeItem(selectedIndexes.at(i).row());
|
|
|
|
delete item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-09-04 08:18:51 +04:00
|
|
|
int createtorrent::getPieceSize() const {
|
|
|
|
switch(comboPieceSize->currentIndex()) {
|
|
|
|
case 0:
|
|
|
|
return 32*1024;
|
|
|
|
case 1:
|
|
|
|
return 64*1024;
|
|
|
|
case 2:
|
|
|
|
return 128*1024;
|
|
|
|
case 3:
|
|
|
|
return 256*1024;
|
|
|
|
case 4:
|
|
|
|
return 512*1024;
|
|
|
|
case 5:
|
|
|
|
return 1024*1024;
|
|
|
|
case 6:
|
|
|
|
return 2048*1024;
|
|
|
|
default:
|
|
|
|
return 256*1024;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void createtorrent::on_addTracker_button_clicked() {
|
2007-04-04 22:55:38 +04:00
|
|
|
bool ok;
|
|
|
|
QString URL = QInputDialog::getText(this, tr("Please type an announce URL"),
|
2007-06-16 03:40:33 +04:00
|
|
|
tr("Announce URL:", "Tracker URL"), QLineEdit::Normal,
|
2007-04-04 22:55:38 +04:00
|
|
|
"http://", &ok);
|
|
|
|
if(ok){
|
|
|
|
if(trackers_list->findItems(URL, Qt::MatchFixedString).size() == 0)
|
|
|
|
trackers_list->addItem(URL);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void createtorrent::on_removeURLSeed_button_clicked(){
|
|
|
|
QModelIndexList selectedIndexes = URLSeeds_list->selectionModel()->selectedIndexes();
|
|
|
|
for(int i=selectedIndexes.size()-1; i>=0; --i){
|
|
|
|
QListWidgetItem *item = URLSeeds_list->takeItem(selectedIndexes.at(i).row());
|
|
|
|
delete item;
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void createtorrent::on_addURLSeed_button_clicked(){
|
|
|
|
bool ok;
|
2007-06-16 03:40:33 +04:00
|
|
|
QString URL = QInputDialog::getText(this, tr("Please type a web seed url"),
|
|
|
|
tr("Web seed URL:"), QLineEdit::Normal,
|
2007-04-04 22:55:38 +04:00
|
|
|
"http://", &ok);
|
|
|
|
if(ok){
|
|
|
|
if(URLSeeds_list->findItems(URL, Qt::MatchFixedString).size() == 0)
|
|
|
|
URLSeeds_list->addItem(URL);
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2007-04-04 22:55:38 +04:00
|
|
|
QStringList createtorrent::allItems(QListWidget *list){
|
|
|
|
QStringList res;
|
|
|
|
unsigned int nbItems = list->count();
|
|
|
|
for(unsigned int i=0; i< nbItems; ++i){
|
|
|
|
res << list->item(i)->text();
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2006-09-30 20:02:39 +04:00
|
|
|
// Main function that create a .torrent file
|
|
|
|
void createtorrent::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);
|
2007-10-12 15:50:02 +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;
|
|
|
|
}
|
2007-09-04 08:18:51 +04:00
|
|
|
QStringList trackers = allItems(trackers_list);
|
|
|
|
if(!trackers.size()){
|
|
|
|
QMessageBox::critical(0, tr("No tracker path set"), tr("Please set at least one tracker"));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
QString destination = QFileDialog::getSaveFileName(this, tr("Select destination torrent file"), QDir::homePath(), tr("Torrent Files")+QString::fromUtf8(" (*.torrent)"));
|
|
|
|
if(!destination.isEmpty()) {
|
|
|
|
if(!destination.endsWith(QString::fromUtf8(".torrent")))
|
|
|
|
destination += QString::fromUtf8(".torrent");
|
|
|
|
} else {
|
2006-09-30 20:02:39 +04:00
|
|
|
return;
|
|
|
|
}
|
2007-12-31 19:57:35 +03:00
|
|
|
QStringList url_seeds = allItems(URLSeeds_list);
|
|
|
|
QString comment = txt_comment->toPlainText();
|
|
|
|
creatorThread->create(input, destination, trackers, url_seeds, comment, check_private->isChecked(), getPieceSize());
|
|
|
|
}
|
|
|
|
|
|
|
|
void createtorrent::handleCreationFailure(QString msg) {
|
|
|
|
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(msg));
|
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
void createtorrent::handleCreationSuccess(QString path, const char* branch_path) {
|
|
|
|
if(checkStartSeeding->isChecked()) {
|
|
|
|
// Create save path file
|
|
|
|
boost::intrusive_ptr<torrent_info> t;
|
|
|
|
try {
|
2009-07-12 12:13:00 +04:00
|
|
|
t = new torrent_info(path.toLocal8Bit().data());
|
2008-11-02 00:42:56 +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;
|
|
|
|
}
|
|
|
|
QString hash = misc::toQString(t->info_hash());
|
|
|
|
QFile savepath_file(misc::qBittorrentPath()+QString::fromUtf8("BT_backup")+QDir::separator()+hash+QString::fromUtf8(".savepath"));
|
|
|
|
savepath_file.open(QIODevice::WriteOnly | QIODevice::Text);
|
|
|
|
savepath_file.write(branch_path);
|
|
|
|
savepath_file.close();
|
|
|
|
emit torrent_to_seed(path);
|
|
|
|
}
|
|
|
|
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent was created successfully:")+" "+path);
|
2009-07-23 13:11:05 +04:00
|
|
|
close();
|
2007-12-31 19:57:35 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void createtorrent::updateProgressBar(int progress) {
|
|
|
|
progressBar->setValue(progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
//
|
|
|
|
// Torrent Creator Thread
|
|
|
|
//
|
|
|
|
|
|
|
|
void torrentCreatorThread::create(QString _input_path, QString _save_path, QStringList _trackers, QStringList _url_seeds, QString _comment, bool _is_private, int _piece_size) {
|
|
|
|
input_path = _input_path;
|
|
|
|
save_path = _save_path;
|
|
|
|
trackers = _trackers;
|
|
|
|
url_seeds = _url_seeds;
|
|
|
|
comment = _comment;
|
|
|
|
is_private = _is_private;
|
|
|
|
piece_size = _piece_size;
|
|
|
|
abort = false;
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2009-07-23 13:11:05 +04:00
|
|
|
void sendProgressUpdateSignal(int i, int num, torrentCreatorThread *parent){
|
|
|
|
parent->sendProgressSignal((int)(i*100./(float)num));
|
|
|
|
}
|
|
|
|
|
|
|
|
void torrentCreatorThread::sendProgressSignal(int progress) {
|
|
|
|
emit updateProgress(progress);
|
2008-11-02 00:42:56 +03:00
|
|
|
}
|
|
|
|
|
2007-12-31 19:57:35 +03:00
|
|
|
void torrentCreatorThread::run() {
|
|
|
|
emit updateProgress(0);
|
2007-04-04 22:55:38 +04:00
|
|
|
char const* creator_str = "qBittorrent "VERSION;
|
2006-09-30 20:02:39 +04:00
|
|
|
try {
|
2008-11-02 00:42:56 +03:00
|
|
|
file_storage fs;
|
|
|
|
file_pool fp;
|
2009-07-12 12:13:00 +04:00
|
|
|
path full_path = complete(path(input_path.toLocal8Bit().data()));
|
2008-11-02 00:42:56 +03:00
|
|
|
// Adding files to the torrent
|
|
|
|
add_files(fs, full_path, file_filter);
|
2007-12-31 19:57:35 +03:00
|
|
|
if(abort) return;
|
2008-11-02 00:42:56 +03:00
|
|
|
create_torrent t(fs, piece_size);
|
|
|
|
|
2007-04-04 22:55:38 +04:00
|
|
|
// Add url seeds
|
|
|
|
QString seed;
|
2007-12-31 19:57:35 +03:00
|
|
|
foreach(seed, url_seeds){
|
2009-07-12 12:13:00 +04:00
|
|
|
t.add_url_seed(seed.toLocal8Bit().data());
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
2006-09-30 20:02:39 +04:00
|
|
|
for(int i=0; i<trackers.size(); ++i){
|
2009-07-12 12:13:00 +04:00
|
|
|
t.add_tracker(trackers.at(i).toLocal8Bit().data());
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
2007-12-31 19:57:35 +03:00
|
|
|
if(abort) return;
|
2006-09-30 20:02:39 +04:00
|
|
|
// calculate the hash for all pieces
|
2009-07-23 13:11:05 +04:00
|
|
|
set_piece_hashes(t, full_path.branch_path(), boost::bind<void>(&sendProgressUpdateSignal, _1, t.num_pieces(), this));
|
2006-09-30 20:02:39 +04:00
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// torrent_info structure
|
2008-11-02 00:42:56 +03:00
|
|
|
t.set_creator(creator_str);
|
2009-07-12 12:13:00 +04:00
|
|
|
t.set_comment((const char*)comment.toLocal8Bit());
|
2007-04-04 22:55:38 +04:00
|
|
|
// Is private ?
|
2009-07-24 06:40:36 +04:00
|
|
|
t.set_priv(is_private);
|
2007-12-31 19:57:35 +03:00
|
|
|
if(abort) return;
|
2006-09-30 20:02:39 +04:00
|
|
|
// create the torrent and print it to out
|
2009-07-12 12:13:00 +04:00
|
|
|
ofstream out(complete(path((const char*)save_path.toLocal8Bit())), std::ios_base::binary);
|
2008-11-02 00:42:56 +03:00
|
|
|
bencode(std::ostream_iterator<char>(out), t.generate());
|
2007-12-31 19:57:35 +03:00
|
|
|
emit updateProgress(100);
|
2008-11-02 00:42:56 +03:00
|
|
|
emit creationSuccess(save_path, full_path.branch_path().string().c_str());
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
catch (std::exception& e){
|
2007-12-31 19:57:35 +03:00
|
|
|
emit creationFailure(QString::fromUtf8(e.what()));
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
}
|