2007-08-20 10:29:18 +04:00
|
|
|
/*
|
|
|
|
* Bittorrent Client using Qt4 and libtorrent.
|
|
|
|
* Copyright (C) 2006 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.
|
|
|
|
*
|
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-08-20 10:29:18 +04:00
|
|
|
* Contact : chris@qbittorrent.org
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QString>
|
2007-08-23 18:04:53 +04:00
|
|
|
#include <QStringList>
|
2007-08-20 10:29:18 +04:00
|
|
|
#include <QFile>
|
2007-08-23 18:04:53 +04:00
|
|
|
#include <QDir>
|
2007-08-20 10:29:18 +04:00
|
|
|
#include <QByteArray>
|
2009-11-21 15:35:26 +03:00
|
|
|
#include <math.h>
|
2007-08-20 10:29:18 +04:00
|
|
|
#include "misc.h"
|
2010-07-25 18:55:30 +04:00
|
|
|
#include "preferences.h"
|
2007-08-20 10:29:18 +04:00
|
|
|
#include "qtorrenthandle.h"
|
2010-01-01 16:25:59 +03:00
|
|
|
#include "torrentpersistentdata.h"
|
2010-06-07 12:32:41 +04:00
|
|
|
#include <libtorrent/version.hpp>
|
2009-08-17 09:14:03 +04:00
|
|
|
#include <libtorrent/magnet_uri.hpp>
|
2009-11-21 15:35:26 +03:00
|
|
|
#include <libtorrent/torrent_info.hpp>
|
2010-01-20 00:47:53 +03:00
|
|
|
#include <libtorrent/bencode.hpp>
|
|
|
|
#include <libtorrent/entry.hpp>
|
|
|
|
#include <boost/filesystem/fstream.hpp>
|
2007-08-20 10:29:18 +04:00
|
|
|
|
2010-11-23 00:55:32 +03:00
|
|
|
using namespace libtorrent;
|
|
|
|
|
2010-10-20 00:00:50 +04:00
|
|
|
QTorrentHandle::QTorrentHandle(torrent_handle h): torrent_handle(h) {}
|
2007-08-20 10:29:18 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Getters
|
|
|
|
//
|
|
|
|
|
|
|
|
QString QTorrentHandle::hash() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQString(torrent_handle::info_hash());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::name() const {
|
2010-01-01 16:25:59 +03:00
|
|
|
QString name = TorrentPersistentData::getName(hash());
|
|
|
|
if(name.isEmpty()) {
|
2010-10-20 00:00:50 +04:00
|
|
|
name = misc::toQStringU(torrent_handle::name());
|
2010-01-01 16:25:59 +03:00
|
|
|
}
|
|
|
|
return name;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
QString QTorrentHandle::creation_date() const {
|
2010-11-28 13:29:59 +03:00
|
|
|
#if LIBTORRENT_VERSION_MINOR >= 16
|
|
|
|
boost::optional<time_t> t = torrent_handle::get_torrent_info().creation_date();
|
|
|
|
return misc::time_tToQString(t);
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
boost::optional<boost::posix_time::ptime> boostDate = torrent_handle::get_torrent_info().creation_date();
|
2009-11-12 10:42:21 +03:00
|
|
|
return misc::boostTimeToQString(boostDate);
|
2010-11-28 13:29:59 +03:00
|
|
|
#endif
|
2009-11-12 10:42:21 +03:00
|
|
|
}
|
|
|
|
|
2010-05-18 13:35:55 +04:00
|
|
|
QString QTorrentHandle::next_announce() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::userFriendlyDuration(torrent_handle::status().next_announce.total_seconds());
|
2010-05-18 13:35:55 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
qlonglong QTorrentHandle::next_announce_s() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().next_announce.total_seconds();
|
2010-05-18 13:35:55 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
float QTorrentHandle::progress() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
if(!torrent_handle::status().total_wanted)
|
2008-07-17 01:54:01 +04:00
|
|
|
return 0.;
|
2010-10-20 00:00:50 +04:00
|
|
|
if (torrent_handle::status().total_wanted_done == torrent_handle::status().total_wanted)
|
2008-08-17 07:47:52 +04:00
|
|
|
return 1.;
|
2010-10-20 00:00:50 +04:00
|
|
|
float progress = (float)torrent_handle::status().total_wanted_done/(float)torrent_handle::status().total_wanted;
|
2007-08-28 23:22:05 +04:00
|
|
|
Q_ASSERT(progress >= 0. && progress <= 1.);
|
|
|
|
return progress;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
bitfield QTorrentHandle::pieces() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().pieces;
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::current_tracker() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQString(torrent_handle::status().current_tracker);
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_paused() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_paused() && !torrent_handle::is_auto_managed();
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_queued() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_paused() && torrent_handle::is_auto_managed();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:44 +03:00
|
|
|
size_type QTorrentHandle::total_size() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().total_size();
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::piece_length() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().piece_length();
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_pieces() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().num_pieces();
|
2009-11-14 13:37:45 +03:00
|
|
|
}
|
|
|
|
|
2009-11-21 13:17:57 +03:00
|
|
|
bool QTorrentHandle::first_last_piece_first() const {
|
2010-12-04 23:47:20 +03:00
|
|
|
// Detect first media file
|
|
|
|
torrent_info::file_iterator it;
|
|
|
|
int index = 0;
|
|
|
|
for(it = get_torrent_info().begin_files(); it != get_torrent_info().end_files(); it++) {
|
|
|
|
const QString ext = misc::toQStringU(it->path.string()).split(".").last();
|
|
|
|
if(misc::isPreviewable(ext) && torrent_handle::file_priority(index) > 0) {
|
|
|
|
break;
|
2009-11-21 15:35:26 +03:00
|
|
|
}
|
2010-12-04 23:47:20 +03:00
|
|
|
++index;
|
2009-11-21 15:35:26 +03:00
|
|
|
}
|
2010-12-05 13:13:08 +03:00
|
|
|
if(index >= torrent_handle::get_torrent_info().num_files()) return false;
|
2010-12-04 23:47:20 +03:00
|
|
|
file_entry media_file = torrent_handle::get_torrent_info().file_at(index);
|
2010-10-20 00:00:50 +04:00
|
|
|
int piece_size = torrent_handle::get_torrent_info().piece_length();
|
2009-11-21 15:35:26 +03:00
|
|
|
Q_ASSERT(piece_size>0);
|
2010-12-04 23:47:20 +03:00
|
|
|
int first_piece = floor((media_file.offset+1)/(double)piece_size);
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(first_piece >= 0 && first_piece < torrent_handle::get_torrent_info().num_pieces());
|
|
|
|
qDebug("First piece of the file is %d/%d", first_piece, torrent_handle::get_torrent_info().num_pieces()-1);
|
2010-12-04 23:47:20 +03:00
|
|
|
int num_pieces_in_file = ceil(media_file.size/(double)piece_size);
|
2009-11-21 15:35:26 +03:00
|
|
|
int last_piece = first_piece+num_pieces_in_file-1;
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(last_piece >= 0 && last_piece < torrent_handle::get_torrent_info().num_pieces());
|
|
|
|
qDebug("last piece of the file is %d/%d", last_piece, torrent_handle::get_torrent_info().num_pieces()-1);
|
|
|
|
return (torrent_handle::piece_priority(first_piece) == 7) && (torrent_handle::piece_priority(last_piece) == 7);
|
2009-11-21 13:17:57 +03:00
|
|
|
}
|
|
|
|
|
2007-10-10 23:10:34 +04:00
|
|
|
size_type QTorrentHandle::total_wanted_done() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_wanted_done;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-16 20:43:54 +03:00
|
|
|
size_type QTorrentHandle::total_wanted() const {
|
|
|
|
return torrent_handle::status().total_wanted;
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
float QTorrentHandle::download_payload_rate() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().download_payload_rate;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float QTorrentHandle::upload_payload_rate() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().upload_payload_rate;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_peers() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_peers;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_seeds() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_seeds;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-07-12 09:37:09 +04:00
|
|
|
int QTorrentHandle::num_complete() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_complete;
|
2009-07-12 09:37:09 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_incomplete() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_incomplete;
|
2009-07-12 09:37:09 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::save_path() const {
|
2010-11-28 13:29:59 +03:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
return misc::toQStringU(torrent_handle::save_path()).replace("\\", "/");
|
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::save_path().string()).replace("\\", "/");
|
2010-11-28 13:29:59 +03:00
|
|
|
#endif
|
2009-10-22 00:47:46 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QStringList QTorrentHandle::url_seeds() const {
|
|
|
|
QStringList res;
|
2009-08-17 09:14:03 +04:00
|
|
|
try {
|
2010-10-20 00:00:50 +04:00
|
|
|
const std::set<std::string> existing_seeds = torrent_handle::url_seeds();
|
2010-10-10 19:39:08 +04:00
|
|
|
std::set<std::string>::const_iterator it;
|
|
|
|
for(it = existing_seeds.begin(); it != existing_seeds.end(); it++) {
|
|
|
|
qDebug("URL Seed: %s", it->c_str());
|
|
|
|
res << misc::toQString(*it);
|
2009-08-17 09:14:03 +04:00
|
|
|
}
|
2010-10-10 19:39:08 +04:00
|
|
|
} catch(std::exception e) {
|
|
|
|
std::cout << "ERROR: Failed to convert the URL seed" << std::endl;
|
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
|
|
|
// get the size of the torrent without the filtered files
|
|
|
|
size_type QTorrentHandle::actual_size() const{
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_wanted;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 18:30:30 +04:00
|
|
|
bool QTorrentHandle::has_filtered_pieces() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
std::vector<int> piece_priorities = torrent_handle::piece_priorities();
|
2007-08-29 18:30:30 +04:00
|
|
|
for(unsigned int i = 0; i<piece_priorities.size(); ++i) {
|
|
|
|
if(!piece_priorities[i]) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
int QTorrentHandle::num_files() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().num_files();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-10-23 20:21:56 +04:00
|
|
|
QString QTorrentHandle::filename_at(unsigned int index) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files());
|
2010-11-28 13:29:59 +03:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
2010-12-30 22:38:53 +03:00
|
|
|
return misc::fileName(filepath_at(index));
|
2010-11-28 13:29:59 +03:00
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path.leaf());
|
2010-11-28 13:29:59 +03:00
|
|
|
#endif
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 20:07:37 +04:00
|
|
|
size_type QTorrentHandle::filesize_at(unsigned int index) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files());
|
|
|
|
return torrent_handle::get_torrent_info().file_at(index).size;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-28 13:29:59 +03:00
|
|
|
QString QTorrentHandle::filepath(const libtorrent::file_entry &fe) const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
file_storage fs = torrent_handle::get_torrent_info().files();
|
|
|
|
return misc::toQStringU(fs.file_path(fe));
|
|
|
|
#else
|
|
|
|
return misc::toQStringU(fe.path.string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::filepath_at(unsigned int index) const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
file_storage fs = torrent_handle::get_torrent_info().files();
|
|
|
|
return misc::toQStringU(fs.file_path(fs.at(index)));
|
|
|
|
#else
|
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path.string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::orig_filepath_at(unsigned int index) const {
|
|
|
|
#if LIBTORRENT_VERSION_MINOR > 15
|
|
|
|
file_storage fs = torrent_handle::get_torrent_info().orig_files();
|
|
|
|
return misc::toQStringU(fs.file_path(fs.at(index)));
|
|
|
|
#else
|
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().orig_files().at(index).path.string());
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
torrent_status::state_t QTorrentHandle::state() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().state;
|
2009-08-16 07:09:20 +04:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::creator() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().creator());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::comment() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQStringU(torrent_handle::get_torrent_info().comment());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::total_failed_bytes() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_failed_bytes;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
size_type QTorrentHandle::total_redundant_bytes() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_redundant_bytes;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-11-17 14:46:43 +03:00
|
|
|
bool QTorrentHandle::is_checking() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().state == torrent_status::checking_files || torrent_handle::status().state == torrent_status::checking_resume_data;
|
2009-11-17 14:46:43 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::total_done() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_done;
|
2010-02-17 11:10:57 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::all_time_download() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().all_time_download;
|
2008-11-02 01:07:14 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::all_time_upload() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().all_time_upload;
|
2008-11-02 01:07:14 +03:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::total_payload_download() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_payload_download;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-03-03 20:27:25 +03:00
|
|
|
size_type QTorrentHandle::total_payload_upload() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().total_payload_upload;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-23 18:04:53 +04:00
|
|
|
// Return a list of absolute paths corresponding
|
|
|
|
// to all files in a torrent
|
|
|
|
QStringList QTorrentHandle::files_path() const {
|
2010-10-18 21:44:37 +04:00
|
|
|
QDir saveDir(save_path());
|
2007-08-23 18:04:53 +04:00
|
|
|
QStringList res;
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_info::file_iterator fi = torrent_handle::get_torrent_info().begin_files();
|
|
|
|
while(fi != torrent_handle::get_torrent_info().end_files()) {
|
2010-11-28 13:29:59 +03:00
|
|
|
res << QDir::cleanPath(saveDir.absoluteFilePath(filepath(*fi)));
|
2007-08-23 18:04:53 +04:00
|
|
|
fi++;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-10-18 21:44:37 +04:00
|
|
|
QStringList QTorrentHandle::uneeded_files_path() const {
|
|
|
|
QDir saveDir(save_path());
|
|
|
|
QStringList res;
|
2010-10-20 00:00:50 +04:00
|
|
|
std::vector<int> fp = torrent_handle::file_priorities();
|
|
|
|
torrent_info::file_iterator fi = torrent_handle::get_torrent_info().begin_files();
|
2010-10-18 21:44:37 +04:00
|
|
|
int i = 0;
|
2010-10-20 00:00:50 +04:00
|
|
|
while(fi != torrent_handle::get_torrent_info().end_files()) {
|
2010-10-18 21:44:37 +04:00
|
|
|
if(fp[i] == 0)
|
2010-11-28 13:29:59 +03:00
|
|
|
res << QDir::cleanPath(saveDir.absoluteFilePath(filepath(*fi)));
|
2010-10-18 21:44:37 +04:00
|
|
|
fi++;
|
|
|
|
++i;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2010-08-22 00:22:59 +04:00
|
|
|
bool QTorrentHandle::has_missing_files() const {
|
|
|
|
const QStringList paths = files_path();
|
|
|
|
foreach(const QString &path, paths) {
|
|
|
|
if(!QFile::exists(path)) return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2008-11-02 13:47:59 +03:00
|
|
|
int QTorrentHandle::queue_position() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
if(torrent_handle::queue_position() < 0)
|
2010-05-21 15:58:14 +04:00
|
|
|
return -1;
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::queue_position()+1;
|
2008-11-02 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2007-08-24 16:25:52 +04:00
|
|
|
int QTorrentHandle::num_uploads() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_uploads;
|
2007-08-24 16:25:52 +04:00
|
|
|
}
|
|
|
|
|
2008-09-03 22:19:17 +04:00
|
|
|
bool QTorrentHandle::is_seed() const {
|
2008-12-26 22:14:19 +03:00
|
|
|
// Affected by bug http://code.rasterbar.com/libtorrent/ticket/402
|
2010-10-20 00:00:50 +04:00
|
|
|
//return torrent_handle::is_seed();
|
2008-12-27 12:10:07 +03:00
|
|
|
// May suffer from approximation problems
|
|
|
|
//return (progress() == 1.);
|
|
|
|
// This looks safe
|
|
|
|
return (state() == torrent_status::finished || state() == torrent_status::seeding);
|
2008-09-03 22:19:17 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 14:43:20 +03:00
|
|
|
bool QTorrentHandle::is_auto_managed() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_auto_managed();
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
qlonglong QTorrentHandle::active_time() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().active_time;
|
2008-11-02 16:07:38 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
qlonglong QTorrentHandle::seeding_time() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().seeding_time;
|
2009-11-12 10:42:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_connections() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().num_connections;
|
2009-11-12 10:42:21 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::connections_limit() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::status().connections_limit;
|
2009-11-15 13:59:11 +03:00
|
|
|
}
|
|
|
|
|
2009-11-19 18:43:00 +03:00
|
|
|
bool QTorrentHandle::priv() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::get_torrent_info().priv();
|
2009-11-19 18:43:00 +03:00
|
|
|
}
|
|
|
|
|
2010-06-27 21:58:08 +04:00
|
|
|
QString QTorrentHandle::firstFileSavePath() const {
|
|
|
|
Q_ASSERT(has_metadata());
|
|
|
|
QString fsave_path = TorrentPersistentData::getSavePath(hash());
|
|
|
|
if(fsave_path.isEmpty())
|
|
|
|
fsave_path = save_path();
|
|
|
|
fsave_path = fsave_path.replace("\\", "/");
|
|
|
|
if(!fsave_path.endsWith("/"))
|
|
|
|
fsave_path += "/";
|
2010-11-28 13:29:59 +03:00
|
|
|
fsave_path += filepath_at(0);
|
2010-07-25 18:00:35 +04:00
|
|
|
// Remove .!qB extension
|
|
|
|
if(fsave_path.endsWith(".!qB", Qt::CaseInsensitive))
|
|
|
|
fsave_path.chop(4);
|
2010-06-27 21:58:08 +04:00
|
|
|
return fsave_path;
|
|
|
|
}
|
|
|
|
|
2010-01-31 22:03:34 +03:00
|
|
|
bool QTorrentHandle::has_error() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return torrent_handle::is_paused() && !torrent_handle::status().error.empty();
|
2010-05-24 18:19:25 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::error() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
return misc::toQString(torrent_handle::status().error);
|
2010-01-31 22:03:34 +03:00
|
|
|
}
|
|
|
|
|
2010-03-19 01:58:32 +03:00
|
|
|
void QTorrentHandle::downloading_pieces(bitfield &bf) const {
|
|
|
|
std::vector<partial_piece_info> queue;
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::get_download_queue(queue);
|
2010-03-19 01:58:32 +03:00
|
|
|
for(std::vector<partial_piece_info>::iterator it=queue.begin(); it!= queue.end(); it++) {
|
|
|
|
bf.set_bit(it->piece_index);
|
|
|
|
}
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
//
|
|
|
|
// Setters
|
|
|
|
//
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::pause() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::auto_managed(false);
|
|
|
|
torrent_handle::pause();
|
|
|
|
torrent_handle::save_resume_data();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::resume() const {
|
2010-10-20 00:00:50 +04:00
|
|
|
if(has_error()) torrent_handle::clear_error();
|
2010-07-25 18:55:30 +04:00
|
|
|
const QString torrent_hash = hash();
|
|
|
|
bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash);
|
|
|
|
TorrentPersistentData::setErrorState(torrent_hash, false);
|
2010-11-16 23:34:31 +03:00
|
|
|
bool temp_path_enabled = Preferences().isTempPathEnabled();
|
2010-07-25 18:55:30 +04:00
|
|
|
if(has_persistant_error && temp_path_enabled) {
|
|
|
|
// Torrent was supposed to be seeding, checking again in final destination
|
|
|
|
qDebug("Resuming a torrent with error...");
|
|
|
|
const QString final_save_path = TorrentPersistentData::getSavePath(torrent_hash);
|
|
|
|
qDebug("Torrent final path is: %s", qPrintable(final_save_path));
|
|
|
|
if(!final_save_path.isEmpty())
|
|
|
|
move_storage(final_save_path);
|
|
|
|
}
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::auto_managed(true);
|
|
|
|
torrent_handle::resume();
|
2010-07-25 18:55:30 +04:00
|
|
|
if(has_persistant_error && temp_path_enabled) {
|
|
|
|
// Force recheck
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::force_recheck();
|
2010-07-25 18:55:30 +04:00
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::remove_url_seed(QString seed) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::remove_url_seed(seed.toStdString());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::add_url_seed(QString seed) const {
|
2010-10-10 19:39:08 +04:00
|
|
|
const std::string str_seed = seed.toStdString();
|
2010-10-20 00:00:50 +04:00
|
|
|
qDebug("calling torrent_handle::add_url_seed(%s)", str_seed.c_str());
|
|
|
|
torrent_handle::add_url_seed(str_seed);
|
2007-09-09 11:44:22 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::prioritize_files(const std::vector<int> &v) const {
|
2009-11-24 18:38:47 +03:00
|
|
|
// Does not do anything for seeding torrents
|
2010-10-20 00:00:50 +04:00
|
|
|
if(v.size() != (unsigned int)torrent_handle::get_torrent_info().num_files())
|
2009-12-08 20:20:28 +03:00
|
|
|
return;
|
2010-10-18 22:20:44 +04:00
|
|
|
bool was_seed = is_seed();
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::prioritize_files(v);
|
2010-10-18 22:20:44 +04:00
|
|
|
if(was_seed && !is_seed()) {
|
|
|
|
// Reset seed status
|
|
|
|
TorrentPersistentData::saveSeedStatus(*this);
|
2010-10-24 12:06:56 +04:00
|
|
|
// Move to temp folder if necessary
|
2010-11-16 23:34:31 +03:00
|
|
|
const Preferences pref;
|
|
|
|
if(pref.isTempPathEnabled()) {
|
|
|
|
QString tmp_path = pref.getTempPath();
|
2010-10-24 12:06:56 +04:00
|
|
|
QString root_folder = TorrentPersistentData::getRootFolder(hash());
|
|
|
|
if(!root_folder.isEmpty())
|
|
|
|
tmp_path = QDir(tmp_path).absoluteFilePath(root_folder);
|
|
|
|
move_storage(tmp_path);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::file_priority(int index, int priority) const {
|
|
|
|
bool was_seed = is_seed();
|
|
|
|
torrent_handle::file_priority(index, priority);
|
|
|
|
if(was_seed && !is_seed()) {
|
|
|
|
// Save seed status
|
|
|
|
TorrentPersistentData::saveSeedStatus(*this);
|
|
|
|
// Move to temp folder if necessary
|
2010-11-16 23:34:31 +03:00
|
|
|
const Preferences pref;
|
|
|
|
if(pref.isTempPathEnabled()) {
|
|
|
|
QString tmp_path = pref.getTempPath();
|
2010-10-24 12:06:56 +04:00
|
|
|
QString root_folder = TorrentPersistentData::getRootFolder(hash());
|
|
|
|
if(!root_folder.isEmpty())
|
|
|
|
tmp_path = QDir(tmp_path).absoluteFilePath(root_folder);
|
|
|
|
move_storage(tmp_path);
|
|
|
|
}
|
2010-10-18 22:20:44 +04:00
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::set_tracker_login(QString username, QString password) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::set_tracker_login(std::string(username.toLocal8Bit().constData()), std::string(password.toLocal8Bit().constData()));
|
2008-11-02 16:53:45 +03:00
|
|
|
}
|
|
|
|
|
2008-11-02 22:55:55 +03:00
|
|
|
void QTorrentHandle::move_storage(QString new_path) const {
|
2010-07-18 01:35:00 +04:00
|
|
|
if(QDir(save_path()) == QDir(new_path)) return;
|
|
|
|
TorrentPersistentData::setPreviousSavePath(hash(), save_path());
|
2010-10-17 19:17:39 +04:00
|
|
|
// Create destination directory if necessary
|
|
|
|
// or move_storage() will fail...
|
|
|
|
QDir().mkpath(new_path);
|
|
|
|
// Actually move the storage
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::move_storage(new_path.toLocal8Bit().constData());
|
2008-11-02 22:55:55 +03:00
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
bool QTorrentHandle::save_torrent_file(QString path) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
if(!torrent_handle::has_metadata()) return false;
|
2010-01-20 00:47:53 +03:00
|
|
|
QFile met_file(path);
|
|
|
|
if(met_file.open(QIODevice::WriteOnly)) {
|
2010-10-20 00:00:50 +04:00
|
|
|
entry meta = bdecode(torrent_handle::get_torrent_info().metadata().get(), torrent_handle::get_torrent_info().metadata().get()+torrent_handle::get_torrent_info().metadata_size());
|
2010-01-20 00:47:53 +03:00
|
|
|
entry torrent_file(entry::dictionary_t);
|
|
|
|
torrent_file["info"] = meta;
|
2010-10-20 00:00:50 +04:00
|
|
|
if(!torrent_handle::trackers().empty())
|
|
|
|
torrent_file["announce"] = torrent_handle::trackers().front().url;
|
2010-03-04 23:19:25 +03:00
|
|
|
boost::filesystem::ofstream out(path.toLocal8Bit().constData(), std::ios_base::binary);
|
2010-01-20 00:47:53 +03:00
|
|
|
out.unsetf(std::ios_base::skipws);
|
|
|
|
bencode(std::ostream_iterator<char>(out), torrent_file);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::add_tracker(const announce_entry& url) const {
|
2010-06-07 12:32:41 +04:00
|
|
|
#if LIBTORRENT_VERSION_MINOR > 14
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::add_tracker(url);
|
2009-11-22 13:40:54 +03:00
|
|
|
#else
|
2010-10-20 00:00:50 +04:00
|
|
|
std::vector<announce_entry> trackers = torrent_handle::trackers();
|
2009-11-22 13:40:54 +03:00
|
|
|
bool exists = false;
|
|
|
|
std::vector<announce_entry>::iterator it = trackers.begin();
|
|
|
|
while(it != trackers.end()) {
|
|
|
|
if(it->url == url.url) {
|
|
|
|
exists = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
}
|
|
|
|
if(!exists) {
|
|
|
|
trackers.push_back(url);
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::replace_trackers(trackers);
|
2009-11-22 13:40:54 +03:00
|
|
|
}
|
|
|
|
#endif
|
2009-11-19 14:09:03 +03:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:47:20 +03:00
|
|
|
void QTorrentHandle::prioritize_first_last_piece(int file_index, bool b) const {
|
2009-11-21 15:35:26 +03:00
|
|
|
// Determine the priority to set
|
|
|
|
int prio = 7; // MAX
|
2010-12-04 23:47:20 +03:00
|
|
|
if(!b) prio = torrent_handle::file_priority(file_index);
|
|
|
|
file_entry file = get_torrent_info().file_at(file_index);
|
|
|
|
// Determine the first and last piece of the file
|
2010-10-20 00:00:50 +04:00
|
|
|
int piece_size = torrent_handle::get_torrent_info().piece_length();
|
2009-11-21 15:35:26 +03:00
|
|
|
Q_ASSERT(piece_size>0);
|
2010-12-04 23:47:20 +03:00
|
|
|
int first_piece = floor((file.offset+1)/(double)piece_size);
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(first_piece >= 0 && first_piece < torrent_handle::get_torrent_info().num_pieces());
|
|
|
|
qDebug("First piece of the file is %d/%d", first_piece, torrent_handle::get_torrent_info().num_pieces()-1);
|
2010-12-04 23:47:20 +03:00
|
|
|
int num_pieces_in_file = ceil(file.size/(double)piece_size);
|
2009-11-21 15:35:26 +03:00
|
|
|
int last_piece = first_piece+num_pieces_in_file-1;
|
2010-10-20 00:00:50 +04:00
|
|
|
Q_ASSERT(last_piece >= 0 && last_piece < torrent_handle::get_torrent_info().num_pieces());
|
|
|
|
qDebug("last piece of the file is %d/%d", last_piece, torrent_handle::get_torrent_info().num_pieces()-1);
|
|
|
|
torrent_handle::piece_priority(first_piece, prio);
|
|
|
|
torrent_handle::piece_priority(last_piece, prio);
|
2009-11-21 13:17:57 +03:00
|
|
|
}
|
|
|
|
|
2010-12-04 23:47:20 +03:00
|
|
|
void QTorrentHandle::prioritize_first_last_piece(bool b) const {
|
|
|
|
// Download first and last pieces first for all media files in the torrent
|
|
|
|
torrent_info::file_iterator it;
|
|
|
|
int index = 0;
|
|
|
|
for(it = get_torrent_info().begin_files(); it != get_torrent_info().end_files(); it++) {
|
|
|
|
const QString ext = misc::toQStringU(it->path.string()).split(".").last();
|
|
|
|
if(misc::isPreviewable(ext) && torrent_handle::file_priority(index) > 0) {
|
|
|
|
qDebug() << "File" << it->path.string().c_str() << "is previewable, toggle downloading of first/last pieces first";
|
|
|
|
prioritize_first_last_piece(index, b);
|
|
|
|
}
|
|
|
|
++index;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-24 23:31:14 +03:00
|
|
|
void QTorrentHandle::rename_file(int index, QString name) const {
|
2010-10-20 00:00:50 +04:00
|
|
|
torrent_handle::rename_file(index, std::string(name.toUtf8().constData()));
|
2009-12-18 16:36:47 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
//
|
|
|
|
// Operators
|
|
|
|
//
|
|
|
|
|
|
|
|
bool QTorrentHandle::operator ==(const QTorrentHandle& new_h) const{
|
2010-11-14 21:46:16 +03:00
|
|
|
const QString hash = misc::toQString(torrent_handle::info_hash());
|
2007-08-20 10:29:18 +04:00
|
|
|
return (hash == new_h.hash());
|
|
|
|
}
|