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.
|
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
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>
|
2006-09-30 20:02:39 +04:00
|
|
|
|
|
|
|
#include "createtorrent_imp.h"
|
|
|
|
|
|
|
|
using namespace libtorrent;
|
|
|
|
using namespace boost::filesystem;
|
|
|
|
|
|
|
|
createtorrent::createtorrent(QWidget *parent): QDialog(parent){
|
|
|
|
setupUi(this);
|
|
|
|
setAttribute(Qt::WA_DeleteOnClose);
|
|
|
|
show();
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Subfunction to add files to a torrent_info structure
|
|
|
|
// Written by Arvid Norberg (libtorrent Author)
|
|
|
|
void add_files(torrent_info& t, path const& p, path const& l){
|
|
|
|
path f(p / l);
|
|
|
|
if (is_directory(f)){
|
|
|
|
for (directory_iterator i(f), end; i != end; ++i)
|
|
|
|
add_files(t, p, l / i->leaf());
|
|
|
|
}else{
|
|
|
|
t.add_file(l, file_size(f));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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-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-04-04 22:55:38 +04:00
|
|
|
char const* creator_str = "qBittorrent "VERSION;
|
2006-09-30 20:02:39 +04:00
|
|
|
try {
|
2007-09-04 08:18:51 +04:00
|
|
|
boost::intrusive_ptr<torrent_info> t(new torrent_info);
|
2006-10-26 00:35:41 +04:00
|
|
|
ofstream out(complete(path((const char*)destination.toUtf8())), std::ios_base::binary);
|
2007-09-01 14:23:39 +04:00
|
|
|
path full_path;
|
|
|
|
// Adding files to the torrent
|
2007-09-04 08:18:51 +04:00
|
|
|
full_path = complete(path(input.toUtf8().data()));
|
|
|
|
add_files(*t, full_path.branch_path(), full_path.leaf());
|
|
|
|
// Set piece size
|
|
|
|
int piece_size = getPieceSize();
|
|
|
|
t->set_piece_size(piece_size);
|
2007-04-04 22:55:38 +04:00
|
|
|
// Add url seeds
|
|
|
|
QStringList urlSeeds = allItems(URLSeeds_list);
|
|
|
|
QString seed;
|
|
|
|
foreach(seed, urlSeeds){
|
2007-09-04 08:18:51 +04:00
|
|
|
t->add_url_seed(seed.toUtf8().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){
|
2007-09-04 08:18:51 +04:00
|
|
|
t->add_tracker(trackers.at(i).toUtf8().data());
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
// calculate the hash for all pieces
|
2007-04-04 22:55:38 +04:00
|
|
|
file_pool fp;
|
2007-04-12 21:17:04 +04:00
|
|
|
boost::scoped_ptr<storage_interface> st(default_storage_constructor(t, full_path.branch_path(), fp));
|
2007-09-04 08:18:51 +04:00
|
|
|
int num = t->num_pieces();
|
2006-09-30 20:02:39 +04:00
|
|
|
std::vector<char> buf(piece_size);
|
2007-04-04 22:55:38 +04:00
|
|
|
for (int i = 0; i < num; ++i) {
|
2007-09-04 08:18:51 +04:00
|
|
|
st->read(&buf[0], i, 0, t->piece_size(i));
|
|
|
|
hasher h(&buf[0], t->piece_size(i));
|
|
|
|
t->set_hash(i, h.final());
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// torrent_info structure
|
2007-09-04 08:18:51 +04:00
|
|
|
t->set_creator(creator_str);
|
|
|
|
t->set_comment((const char*)txt_comment->toPlainText().toUtf8());
|
2007-04-04 22:55:38 +04:00
|
|
|
// Is private ?
|
|
|
|
if(check_private->isChecked()){
|
2007-09-04 08:18:51 +04:00
|
|
|
t->set_priv(true);
|
2007-04-04 22:55:38 +04:00
|
|
|
}
|
2006-09-30 20:02:39 +04:00
|
|
|
// create the torrent and print it to out
|
2007-09-04 08:18:51 +04:00
|
|
|
entry e = t->create_torrent();
|
2006-09-30 20:02:39 +04:00
|
|
|
libtorrent::bencode(std::ostream_iterator<char>(out), e);
|
2007-09-04 08:18:51 +04:00
|
|
|
if(checkStartSeeding->isChecked())
|
|
|
|
emit torrent_to_seed(destination);
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
catch (std::exception& e){
|
|
|
|
std::cerr << e.what() << "\n";
|
2007-08-20 11:03:41 +04:00
|
|
|
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent creation was unsuccessful, reason: %1").arg(QString::fromUtf8(e.what())));
|
2007-03-26 03:22:10 +04:00
|
|
|
hide();
|
|
|
|
return;
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|
|
|
|
hide();
|
2007-03-12 16:41:02 +03:00
|
|
|
QMessageBox::information(0, tr("Torrent creation"), tr("Torrent was created successfully:")+" "+destination);
|
2006-09-30 20:02:39 +04:00
|
|
|
}
|