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"
|
|
|
|
#include "qtorrenthandle.h"
|
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>
|
2007-08-20 10:29:18 +04:00
|
|
|
|
2007-08-26 22:41:45 +04:00
|
|
|
QTorrentHandle::QTorrentHandle(torrent_handle h): h(h) {}
|
2007-08-20 10:29:18 +04:00
|
|
|
|
|
|
|
//
|
|
|
|
// Getters
|
|
|
|
//
|
|
|
|
|
|
|
|
torrent_handle QTorrentHandle::get_torrent_handle() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return h;
|
|
|
|
}
|
|
|
|
|
|
|
|
torrent_info QTorrentHandle::get_torrent_info() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.get_torrent_info();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::hash() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2009-08-17 09:14:03 +04:00
|
|
|
return misc::toQString(h.info_hash());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::name() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return misc::toQString(h.name());
|
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
QString QTorrentHandle::creation_date() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
boost::optional<boost::posix_time::ptime> boostDate = h.get_torrent_info().creation_date();
|
|
|
|
return misc::boostTimeToQString(boostDate);
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
float QTorrentHandle::progress() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2008-07-17 01:54:01 +04:00
|
|
|
if(!h.status().total_wanted)
|
|
|
|
return 0.;
|
2008-08-17 07:47:52 +04:00
|
|
|
if (h.status().total_wanted_done == h.status().total_wanted)
|
|
|
|
return 1.;
|
2008-07-17 01:54:01 +04:00
|
|
|
float progress = (float)h.status().total_wanted_done/(float)h.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 {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().pieces;
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 22:44:05 +03:00
|
|
|
void QTorrentHandle::piece_availability(std::vector<int>& avail) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.piece_availability(avail);
|
|
|
|
}
|
|
|
|
|
2007-11-18 21:06:44 +03:00
|
|
|
void QTorrentHandle::get_download_queue(std::vector<partial_piece_info>& queue) const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.get_download_queue(queue);
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::current_tracker() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return misc::toQString(h.status().current_tracker);
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_valid() const {
|
|
|
|
return h.is_valid();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_paused() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2008-11-02 14:43:20 +03:00
|
|
|
return h.is_paused() && !h.is_auto_managed();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::is_queued() const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.is_paused() && h.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 {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.get_torrent_info().total_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::piece_length() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2009-08-17 09:14:03 +04:00
|
|
|
return h.get_torrent_info().piece_length();
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_pieces() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2009-08-17 09:14:03 +04:00
|
|
|
return h.get_torrent_info().num_pieces();
|
2007-11-18 21:06:44 +03:00
|
|
|
}
|
2007-08-20 10:29:18 +04:00
|
|
|
|
2009-11-14 13:37:45 +03:00
|
|
|
void QTorrentHandle::get_peer_info(std::vector<peer_info>& v) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.get_peer_info(v);
|
|
|
|
}
|
|
|
|
|
2009-11-21 13:17:57 +03:00
|
|
|
bool QTorrentHandle::first_last_piece_first() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2009-11-21 15:35:26 +03:00
|
|
|
// Detect main file
|
|
|
|
int rank=0;
|
|
|
|
int main_file_index = 0;
|
|
|
|
file_entry main_file = h.get_torrent_info().file_at(0);
|
|
|
|
torrent_info::file_iterator it = h.get_torrent_info().begin_files();
|
|
|
|
it++; ++rank;
|
|
|
|
while(it != h.get_torrent_info().end_files()) {
|
|
|
|
if(it->size > main_file.size) {
|
|
|
|
main_file = *it;
|
|
|
|
main_file_index = rank;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
++rank;
|
|
|
|
}
|
|
|
|
qDebug("Main file in the torrent is %s", main_file.path.string().c_str());
|
|
|
|
int piece_size = h.get_torrent_info().piece_length();
|
|
|
|
Q_ASSERT(piece_size>0);
|
|
|
|
int first_piece = floor((main_file.offset+1)/(double)piece_size);
|
|
|
|
Q_ASSERT(first_piece >= 0 && first_piece < h.get_torrent_info().num_pieces());
|
|
|
|
qDebug("First piece of the file is %d/%d", first_piece, h.get_torrent_info().num_pieces()-1);
|
|
|
|
int num_pieces_in_file = ceil(main_file.size/(double)piece_size);
|
|
|
|
int last_piece = first_piece+num_pieces_in_file-1;
|
|
|
|
Q_ASSERT(last_piece >= 0 && last_piece < h.get_torrent_info().num_pieces());
|
|
|
|
qDebug("last piece of the file is %d/%d", last_piece, h.get_torrent_info().num_pieces()-1);
|
|
|
|
return (h.piece_priority(first_piece) == 7) && (h.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 {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-10-10 23:10:34 +04:00
|
|
|
return h.status().total_wanted_done;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float QTorrentHandle::download_payload_rate() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().download_payload_rate;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
float QTorrentHandle::upload_payload_rate() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().upload_payload_rate;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_peers() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().num_peers;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_seeds() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().num_seeds;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-07-12 09:37:09 +04:00
|
|
|
int QTorrentHandle::num_complete() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().num_complete;
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::scrape_tracker() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.scrape_tracker();
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_incomplete() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().num_incomplete;
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::save_path() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return misc::toQString(h.save_path().string());
|
|
|
|
}
|
|
|
|
|
2009-11-18 13:29:20 +03:00
|
|
|
#ifdef LIBTORRENT_0_15
|
2009-10-22 00:47:46 +04:00
|
|
|
bool QTorrentHandle::super_seeding() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.super_seeding();
|
|
|
|
}
|
2009-11-18 13:29:20 +03:00
|
|
|
#endif
|
2009-10-22 00:47:46 +04:00
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QStringList QTorrentHandle::url_seeds() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
QStringList res;
|
2009-08-17 09:14:03 +04:00
|
|
|
try {
|
|
|
|
std::vector<std::string> existing_seeds = h.get_torrent_info().url_seeds();
|
|
|
|
unsigned int nbSeeds = existing_seeds.size();
|
|
|
|
QString existing_seed;
|
|
|
|
for(unsigned int i=0; i<nbSeeds; ++i) {
|
|
|
|
res << misc::toQString(existing_seeds[i]);
|
|
|
|
}
|
|
|
|
} catch(std::exception e) {}
|
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{
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-10-01 23:43:24 +04:00
|
|
|
return h.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 {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
std::vector<int> piece_priorities = h.piece_priorities();
|
|
|
|
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::download_limit() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return h.download_limit();
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::upload_limit() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return h.upload_limit();
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_files() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.get_torrent_info().num_files();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::has_metadata() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return h.has_metadata();
|
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
void QTorrentHandle::save_resume_data() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2008-11-02 00:42:56 +03:00
|
|
|
return h.save_resume_data();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-08-29 20:07:37 +04:00
|
|
|
QString QTorrentHandle::file_at(unsigned int index) const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-29 20:07:37 +04:00
|
|
|
Q_ASSERT(index < (unsigned int)h.get_torrent_info().num_files());
|
2007-08-26 22:41:45 +04:00
|
|
|
return misc::toQString(h.get_torrent_info().file_at(index).path.leaf());
|
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 {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-29 20:07:37 +04:00
|
|
|
Q_ASSERT(index < (unsigned int)h.get_torrent_info().num_files());
|
2007-08-26 22:41:45 +04:00
|
|
|
return h.get_torrent_info().file_at(index).size;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-10-30 17:58:17 +03:00
|
|
|
std::vector<announce_entry> QTorrentHandle::trackers() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2009-10-30 17:58:17 +03:00
|
|
|
return h.trackers();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
torrent_status::state_t QTorrentHandle::state() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().state;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2009-08-16 07:09:20 +04:00
|
|
|
std::vector<int> QTorrentHandle::file_priorities() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.file_priorities();
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
QString QTorrentHandle::creator() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return misc::toQString(h.get_torrent_info().creator());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
QString QTorrentHandle::comment() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return misc::toQString(h.get_torrent_info().comment());
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::total_failed_bytes() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.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 {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().total_redundant_bytes;
|
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
void QTorrentHandle::file_progress(std::vector<size_type>& fp) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
return h.file_progress(fp);
|
|
|
|
}
|
|
|
|
|
2009-11-17 14:46:43 +03:00
|
|
|
bool QTorrentHandle::is_checking() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().state == torrent_status::checking_files || h.status().state == torrent_status::checking_resume_data;
|
|
|
|
}
|
|
|
|
|
2008-11-02 01:07:14 +03:00
|
|
|
size_type QTorrentHandle::all_time_download() {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().all_time_download;
|
2008-11-02 01:07:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::all_time_upload() {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().all_time_upload;
|
2008-11-02 01:07:14 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
size_type QTorrentHandle::total_payload_download() {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().total_payload_download;
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
size_type QTorrentHandle::total_payload_upload() {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.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 {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-23 18:04:53 +04:00
|
|
|
QString saveDir = misc::toQString(h.save_path().string()) + QDir::separator();
|
|
|
|
QStringList res;
|
2007-08-26 22:41:45 +04:00
|
|
|
torrent_info::file_iterator fi = h.get_torrent_info().begin_files();
|
|
|
|
while(fi != h.get_torrent_info().end_files()) {
|
2007-08-23 18:04:53 +04:00
|
|
|
res << QDir::cleanPath(saveDir + misc::toQString(fi->path.string()));
|
|
|
|
fi++;
|
|
|
|
}
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
|
2008-11-02 13:47:59 +03:00
|
|
|
int QTorrentHandle::queue_position() const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.queue_position();
|
2008-11-02 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
2007-08-24 16:25:52 +04:00
|
|
|
int QTorrentHandle::num_uploads() const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().num_uploads;
|
2007-08-24 16:25:52 +04:00
|
|
|
}
|
|
|
|
|
2008-09-03 22:19:17 +04:00
|
|
|
bool QTorrentHandle::is_seed() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2008-12-26 22:14:19 +03:00
|
|
|
// Affected by bug http://code.rasterbar.com/libtorrent/ticket/402
|
|
|
|
//return h.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 {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.is_auto_managed();
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
qlonglong QTorrentHandle::active_time() const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().active_time;
|
2008-11-02 16:07:38 +03:00
|
|
|
}
|
|
|
|
|
2009-11-12 10:42:21 +03:00
|
|
|
qlonglong QTorrentHandle::seeding_time() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().seeding_time;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::num_connections() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().num_connections;
|
|
|
|
}
|
|
|
|
|
|
|
|
int QTorrentHandle::connections_limit() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.status().connections_limit;
|
|
|
|
}
|
|
|
|
|
2009-08-16 07:09:20 +04:00
|
|
|
bool QTorrentHandle::is_sequential_download() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.is_sequential_download();
|
|
|
|
}
|
|
|
|
|
2009-11-15 13:59:11 +03:00
|
|
|
bool QTorrentHandle::resolve_countries() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.resolve_countries();
|
|
|
|
}
|
|
|
|
|
2009-11-19 18:43:00 +03:00
|
|
|
bool QTorrentHandle::priv() const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
return h.get_torrent_info().priv();
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
//
|
|
|
|
// Setters
|
|
|
|
//
|
|
|
|
|
|
|
|
void QTorrentHandle::set_download_limit(int limit) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.set_download_limit(limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::set_upload_limit(int limit) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.set_upload_limit(limit);
|
|
|
|
}
|
|
|
|
|
2008-12-27 20:05:26 +03:00
|
|
|
void QTorrentHandle::pause() {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2008-11-02 14:43:20 +03:00
|
|
|
h.auto_managed(false);
|
2007-08-20 10:29:18 +04:00
|
|
|
h.pause();
|
2009-01-06 01:16:08 +03:00
|
|
|
h.save_resume_data();
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::resume() {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2008-11-02 14:43:20 +03:00
|
|
|
h.auto_managed(true);
|
2007-08-20 10:29:18 +04:00
|
|
|
h.resume();
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::remove_url_seed(QString seed) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2009-07-12 12:13:00 +04:00
|
|
|
h.remove_url_seed(misc::toString((const char*)seed.toLocal8Bit()));
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::add_url_seed(QString seed) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2009-07-12 12:13:00 +04:00
|
|
|
h.add_url_seed(misc::toString((const char*)seed.toLocal8Bit()));
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-09-09 11:44:22 +04:00
|
|
|
void QTorrentHandle::set_max_uploads(int val) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.set_max_uploads(val);
|
|
|
|
}
|
|
|
|
|
2007-09-09 11:44:22 +04:00
|
|
|
void QTorrentHandle::set_max_connections(int val) {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.set_max_connections(val);
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
void QTorrentHandle::prioritize_files(std::vector<int> v) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.prioritize_files(v);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::set_ratio(float ratio) const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.set_ratio(ratio);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::replace_trackers(std::vector<announce_entry> const& v) const {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.replace_trackers(v);
|
|
|
|
}
|
|
|
|
|
2008-11-02 14:43:20 +03:00
|
|
|
void QTorrentHandle::auto_managed(bool b) const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.auto_managed(b);
|
2008-11-02 14:43:20 +03:00
|
|
|
}
|
|
|
|
|
2008-11-02 13:47:59 +03:00
|
|
|
void QTorrentHandle::queue_position_down() const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.queue_position_down();
|
2008-11-02 13:47:59 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::queue_position_up() const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2009-11-17 11:15:26 +03:00
|
|
|
if(h.queue_position() > 0)
|
|
|
|
h.queue_position_up();
|
2008-11-02 13:47:59 +03:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
void QTorrentHandle::force_reannounce() {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2007-08-20 10:29:18 +04:00
|
|
|
h.force_reannounce();
|
|
|
|
}
|
|
|
|
|
2008-11-02 00:42:56 +03:00
|
|
|
void QTorrentHandle::set_sequential_download(bool b) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2008-11-02 00:42:56 +03:00
|
|
|
h.set_sequential_download(b);
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2007-09-09 11:44:22 +04:00
|
|
|
void QTorrentHandle::set_tracker_login(QString username, QString password) {
|
2007-08-26 22:41:45 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
2009-07-12 12:13:00 +04:00
|
|
|
h.set_tracker_login(std::string(username.toLocal8Bit().data()), std::string(password.toLocal8Bit().data()));
|
2007-08-20 10:29:18 +04:00
|
|
|
}
|
|
|
|
|
2008-11-02 16:53:45 +03:00
|
|
|
void QTorrentHandle::force_recheck() const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.force_recheck();
|
2008-11-02 16:53:45 +03:00
|
|
|
}
|
|
|
|
|
2008-11-02 22:55:55 +03:00
|
|
|
void QTorrentHandle::move_storage(QString new_path) const {
|
2009-08-17 09:14:03 +04:00
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.move_storage(new_path.toLocal8Bit().data());
|
2008-11-02 22:55:55 +03:00
|
|
|
}
|
|
|
|
|
2009-08-16 07:09:20 +04:00
|
|
|
void QTorrentHandle::file_priority(int index, int priority) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2009-08-17 09:14:03 +04:00
|
|
|
h.file_priority(index, priority);
|
2009-08-16 07:09:20 +04:00
|
|
|
}
|
|
|
|
|
2009-11-18 13:29:20 +03:00
|
|
|
#ifdef LIBTORRENT_0_15
|
2009-10-22 00:47:46 +04:00
|
|
|
void QTorrentHandle::super_seeding(bool on) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.super_seeding(on);
|
|
|
|
}
|
2009-11-18 13:29:20 +03:00
|
|
|
#endif
|
2009-10-22 00:47:46 +04:00
|
|
|
|
2009-11-15 13:59:11 +03:00
|
|
|
void QTorrentHandle::resolve_countries(bool r) {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.resolve_countries(r);
|
|
|
|
}
|
|
|
|
|
2009-11-17 14:46:43 +03:00
|
|
|
void QTorrentHandle::connect_peer(asio::ip::tcp::endpoint const& adr, int source) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.connect_peer(adr, source);
|
|
|
|
}
|
|
|
|
|
2009-11-17 17:19:50 +03:00
|
|
|
void QTorrentHandle::set_peer_upload_limit(asio::ip::tcp::endpoint ip, int limit) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.set_peer_upload_limit(ip, limit);
|
|
|
|
}
|
|
|
|
|
|
|
|
void QTorrentHandle::set_peer_download_limit(asio::ip::tcp::endpoint ip, int limit) const {
|
|
|
|
Q_ASSERT(h.is_valid());
|
|
|
|
h.set_peer_download_limit(ip, limit);
|
|
|
|
}
|
|
|
|
|
2009-11-19 14:09:03 +03:00
|
|
|
void QTorrentHandle::add_tracker(announce_entry const& url) {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2009-11-22 13:40:54 +03:00
|
|
|
#ifdef LIBTORRENT_0_15
|
2009-11-19 14:09:03 +03:00
|
|
|
h.add_tracker(url);
|
2009-11-22 13:40:54 +03:00
|
|
|
#else
|
|
|
|
std::vector<announce_entry> trackers = h.trackers();
|
|
|
|
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);
|
|
|
|
h.replace_trackers(trackers);
|
|
|
|
}
|
|
|
|
#endif
|
2009-11-19 14:09:03 +03:00
|
|
|
}
|
|
|
|
|
2009-11-21 13:17:57 +03:00
|
|
|
void QTorrentHandle::prioritize_first_last_piece(bool b) {
|
|
|
|
Q_ASSERT(h.is_valid());
|
2009-11-21 15:35:26 +03:00
|
|
|
// Detect main file
|
|
|
|
int rank=0;
|
|
|
|
int main_file_index = 0;
|
|
|
|
file_entry main_file = h.get_torrent_info().file_at(0);
|
|
|
|
torrent_info::file_iterator it = h.get_torrent_info().begin_files();
|
|
|
|
it++; ++rank;
|
|
|
|
while(it != h.get_torrent_info().end_files()) {
|
|
|
|
if(it->size > main_file.size) {
|
|
|
|
main_file = *it;
|
|
|
|
main_file_index = rank;
|
|
|
|
}
|
|
|
|
it++;
|
|
|
|
++rank;
|
|
|
|
}
|
|
|
|
qDebug("Main file in the torrent is %s", main_file.path.string().c_str());
|
|
|
|
// Determine the priority to set
|
|
|
|
int prio = 7; // MAX
|
|
|
|
if(!b) prio = h.file_priority(main_file_index);
|
|
|
|
// Determine the first and last piece of the main file
|
|
|
|
int piece_size = h.get_torrent_info().piece_length();
|
|
|
|
Q_ASSERT(piece_size>0);
|
|
|
|
int first_piece = floor((main_file.offset+1)/(double)piece_size);
|
|
|
|
Q_ASSERT(first_piece >= 0 && first_piece < h.get_torrent_info().num_pieces());
|
|
|
|
qDebug("First piece of the file is %d/%d", first_piece, h.get_torrent_info().num_pieces()-1);
|
|
|
|
int num_pieces_in_file = ceil(main_file.size/(double)piece_size);
|
|
|
|
int last_piece = first_piece+num_pieces_in_file-1;
|
|
|
|
Q_ASSERT(last_piece >= 0 && last_piece < h.get_torrent_info().num_pieces());
|
|
|
|
qDebug("last piece of the file is %d/%d", last_piece, h.get_torrent_info().num_pieces()-1);
|
|
|
|
h.piece_priority(first_piece, prio);
|
|
|
|
h.piece_priority(last_piece, prio);
|
2009-11-21 13:17:57 +03:00
|
|
|
}
|
|
|
|
|
2007-08-20 10:29:18 +04:00
|
|
|
//
|
|
|
|
// Operators
|
|
|
|
//
|
|
|
|
|
|
|
|
QTorrentHandle& QTorrentHandle::operator =(const torrent_handle& new_h) {
|
|
|
|
h = new_h;
|
|
|
|
return *this;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool QTorrentHandle::operator ==(const QTorrentHandle& new_h) const{
|
2009-08-17 09:14:03 +04:00
|
|
|
QString hash = misc::toQString(h.info_hash());
|
2007-08-20 10:29:18 +04:00
|
|
|
return (hash == new_h.hash());
|
|
|
|
}
|