2010-10-25 23:34:42 +04:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <libtorrent/version.hpp>
|
|
|
|
#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>
|
|
|
|
#include <libtorrent/create_torrent.hpp>
|
|
|
|
#include <QFile>
|
|
|
|
|
|
|
|
#include "torrentcreatorthread.h"
|
2012-05-16 22:19:05 +04:00
|
|
|
#include "fs_utils.h"
|
2010-10-25 23:34:42 +04:00
|
|
|
|
2011-10-25 21:56:54 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR < 16
|
|
|
|
#include <boost/filesystem/operations.hpp>
|
|
|
|
#include <boost/filesystem/path.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
|
|
|
#endif
|
|
|
|
#include <boost/bind.hpp>
|
2012-10-07 16:42:32 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <fstream>
|
2011-10-25 21:56:54 +04:00
|
|
|
|
2010-10-25 23:34:42 +04:00
|
|
|
using namespace libtorrent;
|
2011-10-25 21:56:54 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR < 16
|
2010-10-25 23:34:42 +04:00
|
|
|
using namespace boost::filesystem;
|
2011-10-25 21:56:54 +04:00
|
|
|
#endif
|
2010-10-25 23:34:42 +04:00
|
|
|
|
|
|
|
// do not include files and folders whose
|
|
|
|
// name starts with a .
|
2011-10-25 21:56:54 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR >= 16
|
|
|
|
bool file_filter(std::string const& f)
|
|
|
|
{
|
|
|
|
if (filename(f)[0] == '.') return false;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
#else
|
2010-10-25 23:34:42 +04:00
|
|
|
bool file_filter(boost::filesystem::path const& filename)
|
|
|
|
{
|
|
|
|
if (filename.leaf()[0] == '.') return false;
|
|
|
|
std::cerr << filename << std::endl;
|
|
|
|
return true;
|
|
|
|
}
|
2011-10-25 21:56:54 +04:00
|
|
|
#endif
|
2010-10-25 23:34:42 +04:00
|
|
|
|
|
|
|
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;
|
2012-02-20 21:30:53 +04:00
|
|
|
if (QFile(save_path).exists())
|
2012-07-05 20:10:13 +04:00
|
|
|
fsutils::forceRemove(save_path);
|
2010-10-25 23:34:42 +04:00
|
|
|
trackers = _trackers;
|
|
|
|
url_seeds = _url_seeds;
|
|
|
|
comment = _comment;
|
|
|
|
is_private = _is_private;
|
|
|
|
piece_size = _piece_size;
|
2011-10-25 21:56:54 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR < 16
|
2010-10-25 23:34:42 +04:00
|
|
|
path::default_name_check(no_check);
|
2011-10-25 21:56:54 +04:00
|
|
|
#endif
|
2010-10-25 23:34:42 +04:00
|
|
|
abort = false;
|
|
|
|
start();
|
|
|
|
}
|
|
|
|
|
2012-02-20 21:56:07 +04:00
|
|
|
void sendProgressUpdateSignal(int i, int num, TorrentCreatorThread *parent) {
|
2010-10-25 23:34:42 +04:00
|
|
|
parent->sendProgressSignal((int)(i*100./(float)num));
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorThread::sendProgressSignal(int progress) {
|
|
|
|
emit updateProgress(progress);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TorrentCreatorThread::run() {
|
|
|
|
emit updateProgress(0);
|
2011-02-26 18:04:15 +03:00
|
|
|
QString creator_str("qBittorrent "VERSION);
|
2010-10-25 23:34:42 +04:00
|
|
|
try {
|
|
|
|
file_storage fs;
|
|
|
|
// Adding files to the torrent
|
2011-03-10 20:42:17 +03:00
|
|
|
libtorrent::add_files(fs, input_path.toUtf8().constData(), file_filter);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (abort) return;
|
2010-10-25 23:34:42 +04:00
|
|
|
create_torrent t(fs, piece_size);
|
|
|
|
|
|
|
|
// Add url seeds
|
2012-02-20 21:56:07 +04:00
|
|
|
foreach (const QString &seed, url_seeds) {
|
2011-03-10 20:42:17 +03:00
|
|
|
t.add_url_seed(seed.trimmed().toStdString());
|
2010-10-25 23:34:42 +04:00
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
foreach (const QString &tracker, trackers) {
|
2011-03-10 20:42:17 +03:00
|
|
|
t.add_tracker(tracker.trimmed().toStdString());
|
2010-10-25 23:34:42 +04:00
|
|
|
}
|
2012-02-20 21:30:53 +04:00
|
|
|
if (abort) return;
|
2010-10-25 23:34:42 +04:00
|
|
|
// calculate the hash for all pieces
|
2012-05-16 22:19:05 +04:00
|
|
|
const QString parent_path = fsutils::branchPath(input_path);
|
2010-11-28 13:29:59 +03:00
|
|
|
set_piece_hashes(t, parent_path.toUtf8().constData(), boost::bind<void>(&sendProgressUpdateSignal, _1, t.num_pieces(), this));
|
2010-10-25 23:34:42 +04:00
|
|
|
// Set qBittorrent as creator and add user comment to
|
|
|
|
// torrent_info structure
|
2011-02-26 18:04:15 +03:00
|
|
|
t.set_creator(creator_str.toUtf8().constData());
|
|
|
|
t.set_comment(comment.toUtf8().constData());
|
2010-10-25 23:34:42 +04:00
|
|
|
// Is private ?
|
|
|
|
t.set_priv(is_private);
|
2012-02-20 21:30:53 +04:00
|
|
|
if (abort) return;
|
2010-10-25 23:34:42 +04:00
|
|
|
// create the torrent and print it to out
|
2011-01-01 21:45:38 +03:00
|
|
|
qDebug("Saving to %s", qPrintable(save_path));
|
2012-10-17 20:21:27 +04:00
|
|
|
std::ofstream outfile(save_path.toLocal8Bit().constData(), std::ios_base::out|std::ios_base::binary);
|
2012-10-07 16:42:32 +04:00
|
|
|
if (outfile.fail())
|
2011-01-02 12:37:56 +03:00
|
|
|
throw std::exception();
|
2012-10-07 16:42:32 +04:00
|
|
|
bencode(std::ostream_iterator<char>(outfile), t.generate());
|
|
|
|
outfile.close();
|
|
|
|
emit updateProgress(100);
|
|
|
|
emit creationSuccess(save_path, parent_path);
|
2012-02-20 21:56:07 +04:00
|
|
|
} catch (std::exception& e) {
|
2011-01-01 21:45:38 +03:00
|
|
|
emit creationFailure(QString::fromLocal8Bit(e.what()));
|
2010-10-25 23:34:42 +04:00
|
|
|
}
|
|
|
|
}
|