1
0
mirror of https://github.com/d47081/qBittorrent.git synced 2025-01-22 20:44:15 +00:00

Performance optimization and code clean up

This commit is contained in:
Christophe Dumez 2010-10-19 20:00:50 +00:00
parent 3664134e02
commit f617b74bac
6 changed files with 176 additions and 425 deletions

View File

@ -365,7 +365,7 @@ void PropertiesWidget::loadDynamicData() {
std::vector<int> avail; std::vector<int> avail;
h.piece_availability(avail); h.piece_availability(avail);
pieces_availability->setAvailability(avail); pieces_availability->setAvailability(avail);
avail_average_lbl->setText(QString::number(h.get_torrent_handle().status().distributed_copies, 'f', 1)); avail_average_lbl->setText(QString::number(h.status().distributed_copies, 'f', 1));
} else { } else {
showPiecesAvailability(false); showPiecesAvailability(false);
} }

View File

@ -149,7 +149,7 @@ public:
if(index.column() != PRIORITY) return 0; if(index.column() != PRIORITY) return 0;
if(properties) { if(properties) {
QTorrentHandle h = properties->getCurrentTorrent(); QTorrentHandle h = properties->getCurrentTorrent();
if(!h.is_valid() || h.get_torrent_handle().is_seed() || !h.has_metadata()) return 0; if(!h.is_valid() || static_cast<torrent_handle>(h).is_seed() || !h.has_metadata()) return 0;
} }
if(index.data().toInt() == 0) { if(index.data().toInt() == 0) {
// IGNORED // IGNORED

View File

@ -772,10 +772,10 @@ void QBtSession::deleteTorrent(QString hash, bool delete_local_files) {
QDir save_dir(h.save_path()); QDir save_dir(h.save_path());
if(save_dir != QDir(defaultSavePath) && (defaultTempPath.isEmpty() || save_dir != QDir(defaultTempPath))) if(save_dir != QDir(defaultSavePath) && (defaultTempPath.isEmpty() || save_dir != QDir(defaultTempPath)))
savePathsToRemove[hash] = save_dir.absolutePath(); savePathsToRemove[hash] = save_dir.absolutePath();
s->remove_torrent(h.get_torrent_handle(), session::delete_files); s->remove_torrent(h, session::delete_files);
} else { } else {
QStringList uneeded_files = h.uneeded_files_path(); QStringList uneeded_files = h.uneeded_files_path();
s->remove_torrent(h.get_torrent_handle()); s->remove_torrent(h);
// Remove unneeded files // Remove unneeded files
foreach(const QString &uneeded_file, uneeded_files) { foreach(const QString &uneeded_file, uneeded_files) {
qDebug("Removing uneeded file: %s", qPrintable(uneeded_file)); qDebug("Removing uneeded file: %s", qPrintable(uneeded_file));

View File

@ -45,126 +45,97 @@
#include <libtorrent/entry.hpp> #include <libtorrent/entry.hpp>
#include <boost/filesystem/fstream.hpp> #include <boost/filesystem/fstream.hpp>
QTorrentHandle::QTorrentHandle(torrent_handle h): h(h) {} QTorrentHandle::QTorrentHandle(torrent_handle h): torrent_handle(h) {}
// //
// Getters // Getters
// //
const torrent_handle& QTorrentHandle::get_torrent_handle() const {
Q_ASSERT(h.is_valid());
return h;
}
const torrent_info& QTorrentHandle::get_torrent_info() const {
Q_ASSERT(h.is_valid());
return h.get_torrent_info();
}
QString QTorrentHandle::hash() const { QString QTorrentHandle::hash() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::toQString(h.info_hash()); return misc::toQString(torrent_handle::info_hash());
} }
QString QTorrentHandle::name() const { QString QTorrentHandle::name() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
QString name = TorrentPersistentData::getName(hash()); QString name = TorrentPersistentData::getName(hash());
if(name.isEmpty()) { if(name.isEmpty()) {
name = misc::toQStringU(h.name()); name = misc::toQStringU(torrent_handle::name());
} }
return name; return name;
} }
QString QTorrentHandle::creation_date() const { QString QTorrentHandle::creation_date() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
boost::optional<boost::posix_time::ptime> boostDate = h.get_torrent_info().creation_date(); boost::optional<boost::posix_time::ptime> boostDate = torrent_handle::get_torrent_info().creation_date();
return misc::boostTimeToQString(boostDate); return misc::boostTimeToQString(boostDate);
} }
QString QTorrentHandle::next_announce() const { QString QTorrentHandle::next_announce() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::userFriendlyDuration(h.status().next_announce.total_seconds()); return misc::userFriendlyDuration(torrent_handle::status().next_announce.total_seconds());
} }
qlonglong QTorrentHandle::next_announce_s() const { qlonglong QTorrentHandle::next_announce_s() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().next_announce.total_seconds(); return torrent_handle::status().next_announce.total_seconds();
} }
float QTorrentHandle::progress() const { float QTorrentHandle::progress() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
if(!h.status().total_wanted) if(!torrent_handle::status().total_wanted)
return 0.; return 0.;
if (h.status().total_wanted_done == h.status().total_wanted) if (torrent_handle::status().total_wanted_done == torrent_handle::status().total_wanted)
return 1.; return 1.;
float progress = (float)h.status().total_wanted_done/(float)h.status().total_wanted; float progress = (float)torrent_handle::status().total_wanted_done/(float)torrent_handle::status().total_wanted;
Q_ASSERT(progress >= 0. && progress <= 1.); Q_ASSERT(progress >= 0. && progress <= 1.);
return progress; return progress;
} }
bitfield QTorrentHandle::pieces() const { bitfield QTorrentHandle::pieces() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().pieces; return torrent_handle::status().pieces;
}
void QTorrentHandle::piece_availability(std::vector<int>& avail) const {
Q_ASSERT(h.is_valid());
h.piece_availability(avail);
}
void QTorrentHandle::get_download_queue(std::vector<partial_piece_info>& queue) const {
Q_ASSERT(h.is_valid());
h.get_download_queue(queue);
} }
QString QTorrentHandle::current_tracker() const { QString QTorrentHandle::current_tracker() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::toQString(h.status().current_tracker); return misc::toQString(torrent_handle::status().current_tracker);
}
bool QTorrentHandle::is_valid() const {
return h.is_valid();
} }
bool QTorrentHandle::is_paused() const { bool QTorrentHandle::is_paused() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.is_paused() && !h.is_auto_managed(); return torrent_handle::is_paused() && !torrent_handle::is_auto_managed();
} }
bool QTorrentHandle::is_queued() const { bool QTorrentHandle::is_queued() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.is_paused() && h.is_auto_managed(); return torrent_handle::is_paused() && torrent_handle::is_auto_managed();
} }
size_type QTorrentHandle::total_size() const { size_type QTorrentHandle::total_size() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.get_torrent_info().total_size(); return torrent_handle::get_torrent_info().total_size();
} }
size_type QTorrentHandle::piece_length() const { size_type QTorrentHandle::piece_length() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.get_torrent_info().piece_length(); return torrent_handle::get_torrent_info().piece_length();
} }
int QTorrentHandle::num_pieces() const { int QTorrentHandle::num_pieces() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.get_torrent_info().num_pieces(); return torrent_handle::get_torrent_info().num_pieces();
}
void QTorrentHandle::get_peer_info(std::vector<peer_info>& v) const {
Q_ASSERT(h.is_valid());
h.get_peer_info(v);
} }
bool QTorrentHandle::first_last_piece_first() const { bool QTorrentHandle::first_last_piece_first() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
// Detect main file // Detect main file
int rank=0; int rank=0;
int main_file_index = 0; int main_file_index = 0;
file_entry main_file = h.get_torrent_info().file_at(0); file_entry main_file = torrent_handle::get_torrent_info().file_at(0);
torrent_info::file_iterator it = h.get_torrent_info().begin_files(); torrent_info::file_iterator it = torrent_handle::get_torrent_info().begin_files();
it++; ++rank; it++; ++rank;
while(it != h.get_torrent_info().end_files()) { while(it != torrent_handle::get_torrent_info().end_files()) {
if(it->size > main_file.size) { if(it->size > main_file.size) {
main_file = *it; main_file = *it;
main_file_index = rank; main_file_index = rank;
@ -173,75 +144,63 @@ bool QTorrentHandle::first_last_piece_first() const {
++rank; ++rank;
} }
qDebug("Main file in the torrent is %s", main_file.path.string().c_str()); qDebug("Main file in the torrent is %s", main_file.path.string().c_str());
int piece_size = h.get_torrent_info().piece_length(); int piece_size = torrent_handle::get_torrent_info().piece_length();
Q_ASSERT(piece_size>0); Q_ASSERT(piece_size>0);
int first_piece = floor((main_file.offset+1)/(double)piece_size); int first_piece = floor((main_file.offset+1)/(double)piece_size);
Q_ASSERT(first_piece >= 0 && first_piece < h.get_torrent_info().num_pieces()); 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, h.get_torrent_info().num_pieces()-1); qDebug("First piece of the file is %d/%d", first_piece, torrent_handle::get_torrent_info().num_pieces()-1);
int num_pieces_in_file = ceil(main_file.size/(double)piece_size); int num_pieces_in_file = ceil(main_file.size/(double)piece_size);
int last_piece = first_piece+num_pieces_in_file-1; int last_piece = first_piece+num_pieces_in_file-1;
Q_ASSERT(last_piece >= 0 && last_piece < h.get_torrent_info().num_pieces()); 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, h.get_torrent_info().num_pieces()-1); qDebug("last piece of the file is %d/%d", last_piece, torrent_handle::get_torrent_info().num_pieces()-1);
return (h.piece_priority(first_piece) == 7) && (h.piece_priority(last_piece) == 7); return (torrent_handle::piece_priority(first_piece) == 7) && (torrent_handle::piece_priority(last_piece) == 7);
} }
size_type QTorrentHandle::total_wanted_done() const { size_type QTorrentHandle::total_wanted_done() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_wanted_done; return torrent_handle::status().total_wanted_done;
} }
float QTorrentHandle::download_payload_rate() const { float QTorrentHandle::download_payload_rate() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().download_payload_rate; return torrent_handle::status().download_payload_rate;
} }
float QTorrentHandle::upload_payload_rate() const { float QTorrentHandle::upload_payload_rate() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().upload_payload_rate; return torrent_handle::status().upload_payload_rate;
} }
int QTorrentHandle::num_peers() const { int QTorrentHandle::num_peers() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().num_peers; return torrent_handle::status().num_peers;
} }
int QTorrentHandle::num_seeds() const { int QTorrentHandle::num_seeds() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().num_seeds; return torrent_handle::status().num_seeds;
} }
int QTorrentHandle::num_complete() const { int QTorrentHandle::num_complete() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().num_complete; return torrent_handle::status().num_complete;
}
void QTorrentHandle::scrape_tracker() const {
Q_ASSERT(h.is_valid());
h.scrape_tracker();
} }
int QTorrentHandle::num_incomplete() const { int QTorrentHandle::num_incomplete() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().num_incomplete; return torrent_handle::status().num_incomplete;
} }
QString QTorrentHandle::save_path() const { QString QTorrentHandle::save_path() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::toQStringU(h.save_path().string()).replace("\\", "/"); return misc::toQStringU(torrent_handle::save_path().string()).replace("\\", "/");
} }
#if LIBTORRENT_VERSION_MINOR > 14
bool QTorrentHandle::super_seeding() const {
Q_ASSERT(h.is_valid());
return h.super_seeding();
}
#endif
QStringList QTorrentHandle::url_seeds() const { QStringList QTorrentHandle::url_seeds() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
QStringList res; QStringList res;
try { try {
const std::set<std::string> existing_seeds = h.url_seeds(); const std::set<std::string> existing_seeds = torrent_handle::url_seeds();
std::set<std::string>::const_iterator it; std::set<std::string>::const_iterator it;
for(it = existing_seeds.begin(); it != existing_seeds.end(); it++) { for(it = existing_seeds.begin(); it != existing_seeds.end(); it++) {
qDebug("URL Seed: %s", it->c_str()); qDebug("URL Seed: %s", it->c_str());
@ -255,134 +214,99 @@ QStringList QTorrentHandle::url_seeds() const {
// get the size of the torrent without the filtered files // get the size of the torrent without the filtered files
size_type QTorrentHandle::actual_size() const{ size_type QTorrentHandle::actual_size() const{
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_wanted; return torrent_handle::status().total_wanted;
} }
bool QTorrentHandle::has_filtered_pieces() const { bool QTorrentHandle::has_filtered_pieces() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
std::vector<int> piece_priorities = h.piece_priorities(); std::vector<int> piece_priorities = torrent_handle::piece_priorities();
for(unsigned int i = 0; i<piece_priorities.size(); ++i) { for(unsigned int i = 0; i<piece_priorities.size(); ++i) {
if(!piece_priorities[i]) return true; if(!piece_priorities[i]) return true;
} }
return false; return false;
} }
int QTorrentHandle::download_limit() const {
Q_ASSERT(h.is_valid());
return h.download_limit();
}
int QTorrentHandle::upload_limit() const {
Q_ASSERT(h.is_valid());
return h.upload_limit();
}
int QTorrentHandle::num_files() const { int QTorrentHandle::num_files() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.get_torrent_info().num_files(); return torrent_handle::get_torrent_info().num_files();
}
bool QTorrentHandle::has_metadata() const {
Q_ASSERT(h.is_valid());
return h.has_metadata();
}
void QTorrentHandle::save_resume_data() const {
Q_ASSERT(h.is_valid());
return h.save_resume_data();
} }
QString QTorrentHandle::file_at(unsigned int index) const { QString QTorrentHandle::file_at(unsigned int index) const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
Q_ASSERT(index < (unsigned int)h.get_torrent_info().num_files()); Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files());
return misc::toQStringU(h.get_torrent_info().file_at(index).path.leaf()); return misc::toQStringU(torrent_handle::get_torrent_info().file_at(index).path.leaf());
} }
size_type QTorrentHandle::filesize_at(unsigned int index) const { size_type QTorrentHandle::filesize_at(unsigned int index) const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
Q_ASSERT(index < (unsigned int)h.get_torrent_info().num_files()); Q_ASSERT(index < (unsigned int)torrent_handle::get_torrent_info().num_files());
return h.get_torrent_info().file_at(index).size; return torrent_handle::get_torrent_info().file_at(index).size;
}
const std::vector<announce_entry> QTorrentHandle::trackers() const {
Q_ASSERT(h.is_valid());
return h.trackers();
} }
torrent_status::state_t QTorrentHandle::state() const { torrent_status::state_t QTorrentHandle::state() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().state; return torrent_handle::status().state;
}
std::vector<int> QTorrentHandle::file_priorities() const {
Q_ASSERT(h.is_valid());
return h.file_priorities();
} }
QString QTorrentHandle::creator() const { QString QTorrentHandle::creator() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::toQStringU(h.get_torrent_info().creator()); return misc::toQStringU(torrent_handle::get_torrent_info().creator());
} }
QString QTorrentHandle::comment() const { QString QTorrentHandle::comment() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::toQStringU(h.get_torrent_info().comment()); return misc::toQStringU(torrent_handle::get_torrent_info().comment());
} }
size_type QTorrentHandle::total_failed_bytes() const { size_type QTorrentHandle::total_failed_bytes() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_failed_bytes; return torrent_handle::status().total_failed_bytes;
} }
size_type QTorrentHandle::total_redundant_bytes() const { size_type QTorrentHandle::total_redundant_bytes() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_redundant_bytes; return torrent_handle::status().total_redundant_bytes;
}
void QTorrentHandle::file_progress(std::vector<size_type>& fp) const {
Q_ASSERT(h.is_valid());
return h.file_progress(fp);
} }
bool QTorrentHandle::is_checking() const { bool QTorrentHandle::is_checking() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().state == torrent_status::checking_files || h.status().state == torrent_status::checking_resume_data; return torrent_handle::status().state == torrent_status::checking_files || torrent_handle::status().state == torrent_status::checking_resume_data;
} }
size_type QTorrentHandle::total_done() const { size_type QTorrentHandle::total_done() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_done; return torrent_handle::status().total_done;
} }
size_type QTorrentHandle::all_time_download() const { size_type QTorrentHandle::all_time_download() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().all_time_download; return torrent_handle::status().all_time_download;
} }
size_type QTorrentHandle::all_time_upload() const { size_type QTorrentHandle::all_time_upload() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().all_time_upload; return torrent_handle::status().all_time_upload;
} }
size_type QTorrentHandle::total_payload_download() const { size_type QTorrentHandle::total_payload_download() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_payload_download; return torrent_handle::status().total_payload_download;
} }
size_type QTorrentHandle::total_payload_upload() const { size_type QTorrentHandle::total_payload_upload() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().total_payload_upload; return torrent_handle::status().total_payload_upload;
} }
// Return a list of absolute paths corresponding // Return a list of absolute paths corresponding
// to all files in a torrent // to all files in a torrent
QStringList QTorrentHandle::files_path() const { QStringList QTorrentHandle::files_path() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
QDir saveDir(save_path()); QDir saveDir(save_path());
QStringList res; QStringList res;
torrent_info::file_iterator fi = h.get_torrent_info().begin_files(); torrent_info::file_iterator fi = torrent_handle::get_torrent_info().begin_files();
while(fi != h.get_torrent_info().end_files()) { while(fi != torrent_handle::get_torrent_info().end_files()) {
res << QDir::cleanPath(saveDir.absoluteFilePath(misc::toQStringU(fi->path.string()))); res << QDir::cleanPath(saveDir.absoluteFilePath(misc::toQStringU(fi->path.string())));
fi++; fi++;
} }
@ -390,13 +314,13 @@ QStringList QTorrentHandle::files_path() const {
} }
QStringList QTorrentHandle::uneeded_files_path() const { QStringList QTorrentHandle::uneeded_files_path() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
QDir saveDir(save_path()); QDir saveDir(save_path());
QStringList res; QStringList res;
std::vector<int> fp = h.file_priorities(); std::vector<int> fp = torrent_handle::file_priorities();
torrent_info::file_iterator fi = h.get_torrent_info().begin_files(); torrent_info::file_iterator fi = torrent_handle::get_torrent_info().begin_files();
int i = 0; int i = 0;
while(fi != h.get_torrent_info().end_files()) { while(fi != torrent_handle::get_torrent_info().end_files()) {
if(fp[i] == 0) if(fp[i] == 0)
res << QDir::cleanPath(saveDir.absoluteFilePath(misc::toQStringU(fi->path.string()))); res << QDir::cleanPath(saveDir.absoluteFilePath(misc::toQStringU(fi->path.string())));
fi++; fi++;
@ -414,21 +338,21 @@ bool QTorrentHandle::has_missing_files() const {
} }
int QTorrentHandle::queue_position() const { int QTorrentHandle::queue_position() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
if(h.queue_position() < 0) if(torrent_handle::queue_position() < 0)
return -1; return -1;
return h.queue_position()+1; return torrent_handle::queue_position()+1;
} }
int QTorrentHandle::num_uploads() const { int QTorrentHandle::num_uploads() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().num_uploads; return torrent_handle::status().num_uploads;
} }
bool QTorrentHandle::is_seed() const { bool QTorrentHandle::is_seed() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
// Affected by bug http://code.rasterbar.com/libtorrent/ticket/402 // Affected by bug http://code.rasterbar.com/libtorrent/ticket/402
//return h.is_seed(); //return torrent_handle::is_seed();
// May suffer from approximation problems // May suffer from approximation problems
//return (progress() == 1.); //return (progress() == 1.);
// This looks safe // This looks safe
@ -436,49 +360,37 @@ bool QTorrentHandle::is_seed() const {
} }
bool QTorrentHandle::is_auto_managed() const { bool QTorrentHandle::is_auto_managed() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.is_auto_managed(); return torrent_handle::is_auto_managed();
} }
qlonglong QTorrentHandle::active_time() const { qlonglong QTorrentHandle::active_time() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().active_time; return torrent_handle::status().active_time;
} }
qlonglong QTorrentHandle::seeding_time() const { qlonglong QTorrentHandle::seeding_time() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().seeding_time; return torrent_handle::status().seeding_time;
} }
int QTorrentHandle::num_connections() const { int QTorrentHandle::num_connections() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().num_connections; return torrent_handle::status().num_connections;
} }
int QTorrentHandle::connections_limit() const { int QTorrentHandle::connections_limit() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.status().connections_limit; return torrent_handle::status().connections_limit;
} }
bool QTorrentHandle::is_sequential_download() const {
Q_ASSERT(h.is_valid());
return h.is_sequential_download();
}
#ifndef DISABLE_GUI
bool QTorrentHandle::resolve_countries() const {
Q_ASSERT(h.is_valid());
return h.resolve_countries();
}
#endif
bool QTorrentHandle::priv() const { bool QTorrentHandle::priv() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.get_torrent_info().priv(); return torrent_handle::get_torrent_info().priv();
} }
QString QTorrentHandle::firstFileSavePath() const { QString QTorrentHandle::firstFileSavePath() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
Q_ASSERT(has_metadata()); Q_ASSERT(has_metadata());
QString fsave_path = TorrentPersistentData::getSavePath(hash()); QString fsave_path = TorrentPersistentData::getSavePath(hash());
if(fsave_path.isEmpty()) if(fsave_path.isEmpty())
@ -486,7 +398,7 @@ QString QTorrentHandle::firstFileSavePath() const {
fsave_path = fsave_path.replace("\\", "/"); fsave_path = fsave_path.replace("\\", "/");
if(!fsave_path.endsWith("/")) if(!fsave_path.endsWith("/"))
fsave_path += "/"; fsave_path += "/";
fsave_path += misc::toQStringU(h.get_torrent_info().file_at(0).path.string()); fsave_path += misc::toQStringU(torrent_handle::get_torrent_info().file_at(0).path.string());
// Remove .!qB extension // Remove .!qB extension
if(fsave_path.endsWith(".!qB", Qt::CaseInsensitive)) if(fsave_path.endsWith(".!qB", Qt::CaseInsensitive))
fsave_path.chop(4); fsave_path.chop(4);
@ -494,19 +406,19 @@ QString QTorrentHandle::firstFileSavePath() const {
} }
bool QTorrentHandle::has_error() const { bool QTorrentHandle::has_error() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return h.is_paused() && !h.status().error.empty(); return torrent_handle::is_paused() && !torrent_handle::status().error.empty();
} }
QString QTorrentHandle::error() const { QString QTorrentHandle::error() const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
return misc::toQString(h.status().error); return misc::toQString(torrent_handle::status().error);
} }
void QTorrentHandle::downloading_pieces(bitfield &bf) const { void QTorrentHandle::downloading_pieces(bitfield &bf) const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
std::vector<partial_piece_info> queue; std::vector<partial_piece_info> queue;
h.get_download_queue(queue); torrent_handle::get_download_queue(queue);
for(std::vector<partial_piece_info>::iterator it=queue.begin(); it!= queue.end(); it++) { for(std::vector<partial_piece_info>::iterator it=queue.begin(); it!= queue.end(); it++) {
bf.set_bit(it->piece_index); bf.set_bit(it->piece_index);
} }
@ -517,26 +429,16 @@ void QTorrentHandle::downloading_pieces(bitfield &bf) const {
// Setters // Setters
// //
void QTorrentHandle::set_download_limit(int limit) {
Q_ASSERT(h.is_valid());
h.set_download_limit(limit);
}
void QTorrentHandle::set_upload_limit(int limit) {
Q_ASSERT(h.is_valid());
h.set_upload_limit(limit);
}
void QTorrentHandle::pause() { void QTorrentHandle::pause() {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
h.auto_managed(false); torrent_handle::auto_managed(false);
h.pause(); torrent_handle::pause();
h.save_resume_data(); torrent_handle::save_resume_data();
} }
void QTorrentHandle::resume() { void QTorrentHandle::resume() {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
if(has_error()) h.clear_error(); if(has_error()) torrent_handle::clear_error();
const QString torrent_hash = hash(); const QString torrent_hash = hash();
bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash); bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash);
TorrentPersistentData::setErrorState(torrent_hash, false); TorrentPersistentData::setErrorState(torrent_hash, false);
@ -549,161 +451,71 @@ void QTorrentHandle::resume() {
if(!final_save_path.isEmpty()) if(!final_save_path.isEmpty())
move_storage(final_save_path); move_storage(final_save_path);
} }
h.auto_managed(true); torrent_handle::auto_managed(true);
h.resume(); torrent_handle::resume();
if(has_persistant_error && temp_path_enabled) { if(has_persistant_error && temp_path_enabled) {
// Force recheck // Force recheck
h.force_recheck(); torrent_handle::force_recheck();
} }
} }
void QTorrentHandle::remove_url_seed(QString seed) { void QTorrentHandle::remove_url_seed(QString seed) {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
h.remove_url_seed(seed.toStdString()); torrent_handle::remove_url_seed(seed.toStdString());
} }
void QTorrentHandle::add_url_seed(QString seed) { void QTorrentHandle::add_url_seed(QString seed) {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
const std::string str_seed = seed.toStdString(); const std::string str_seed = seed.toStdString();
qDebug("calling h.add_url_seed(%s)", str_seed.c_str()); qDebug("calling torrent_handle::add_url_seed(%s)", str_seed.c_str());
h.add_url_seed(str_seed); torrent_handle::add_url_seed(str_seed);
}
void QTorrentHandle::set_max_uploads(int val) {
Q_ASSERT(h.is_valid());
h.set_max_uploads(val);
}
void QTorrentHandle::set_max_connections(int val) {
Q_ASSERT(h.is_valid());
h.set_max_connections(val);
} }
void QTorrentHandle::prioritize_files(const std::vector<int> &v) { void QTorrentHandle::prioritize_files(const std::vector<int> &v) {
// Does not do anything for seeding torrents // Does not do anything for seeding torrents
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
if(v.size() != (unsigned int)h.get_torrent_info().num_files()) if(v.size() != (unsigned int)torrent_handle::get_torrent_info().num_files())
return; return;
bool was_seed = is_seed(); bool was_seed = is_seed();
h.prioritize_files(v); torrent_handle::prioritize_files(v);
if(was_seed && !is_seed()) { if(was_seed && !is_seed()) {
// Reset seed status // Reset seed status
TorrentPersistentData::saveSeedStatus(*this); TorrentPersistentData::saveSeedStatus(*this);
} }
} }
void QTorrentHandle::set_ratio(float ratio) const {
Q_ASSERT(h.is_valid());
h.set_ratio(ratio);
}
void QTorrentHandle::replace_trackers(const std::vector<announce_entry> & v) const {
Q_ASSERT(h.is_valid());
h.replace_trackers(v);
}
void QTorrentHandle::auto_managed(bool b) const {
Q_ASSERT(h.is_valid());
h.auto_managed(b);
}
void QTorrentHandle::queue_position_down() const {
Q_ASSERT(h.is_valid());
h.queue_position_down();
}
void QTorrentHandle::queue_position_up() const {
Q_ASSERT(h.is_valid());
if(h.queue_position() > 0)
h.queue_position_up();
}
void QTorrentHandle::queue_position_top() const {
Q_ASSERT(h.is_valid());
h.queue_position_top();
}
void QTorrentHandle::queue_position_bottom() const {
Q_ASSERT(h.is_valid());
h.queue_position_bottom();
}
void QTorrentHandle::force_reannounce() {
Q_ASSERT(h.is_valid());
h.force_reannounce();
}
void QTorrentHandle::set_sequential_download(bool b) {
Q_ASSERT(h.is_valid());
h.set_sequential_download(b);
}
void QTorrentHandle::set_tracker_login(QString username, QString password) { void QTorrentHandle::set_tracker_login(QString username, QString password) {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
h.set_tracker_login(std::string(username.toLocal8Bit().constData()), std::string(password.toLocal8Bit().constData())); torrent_handle::set_tracker_login(std::string(username.toLocal8Bit().constData()), std::string(password.toLocal8Bit().constData()));
}
void QTorrentHandle::force_recheck() const {
Q_ASSERT(h.is_valid());
h.force_recheck();
} }
void QTorrentHandle::move_storage(QString new_path) const { void QTorrentHandle::move_storage(QString new_path) const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
if(QDir(save_path()) == QDir(new_path)) return; if(QDir(save_path()) == QDir(new_path)) return;
TorrentPersistentData::setPreviousSavePath(hash(), save_path()); TorrentPersistentData::setPreviousSavePath(hash(), save_path());
// Create destination directory if necessary // Create destination directory if necessary
// or move_storage() will fail... // or move_storage() will fail...
QDir().mkpath(new_path); QDir().mkpath(new_path);
// Actually move the storage // Actually move the storage
h.move_storage(new_path.toLocal8Bit().constData()); torrent_handle::move_storage(new_path.toLocal8Bit().constData());
} }
void QTorrentHandle::file_priority(int index, int priority) const { void QTorrentHandle::file_priority(int index, int priority) const {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
h.file_priority(index, priority); torrent_handle::file_priority(index, priority);
// Save seed status // Save seed status
TorrentPersistentData::saveSeedStatus(*this); TorrentPersistentData::saveSeedStatus(*this);
} }
#if LIBTORRENT_VERSION_MINOR > 14
void QTorrentHandle::super_seeding(bool on) const {
Q_ASSERT(h.is_valid());
h.super_seeding(on);
}
void QTorrentHandle::flush_cache() const {
Q_ASSERT(h.is_valid());
h.flush_cache();
}
#endif
#ifndef DISABLE_GUI
void QTorrentHandle::resolve_countries(bool r) {
Q_ASSERT(h.is_valid());
h.resolve_countries(r);
}
#endif
void QTorrentHandle::connect_peer(libtorrent::asio::ip::tcp::endpoint const& adr, int source) const {
Q_ASSERT(h.is_valid());
h.connect_peer(adr, source);
}
void QTorrentHandle::set_peer_upload_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const {
Q_ASSERT(h.is_valid());
h.set_peer_upload_limit(ip, limit);
}
bool QTorrentHandle::save_torrent_file(QString path) { bool QTorrentHandle::save_torrent_file(QString path) {
if(!h.has_metadata()) return false; if(!torrent_handle::has_metadata()) return false;
QFile met_file(path); QFile met_file(path);
if(met_file.open(QIODevice::WriteOnly)) { if(met_file.open(QIODevice::WriteOnly)) {
entry meta = bdecode(h.get_torrent_info().metadata().get(), h.get_torrent_info().metadata().get()+h.get_torrent_info().metadata_size()); entry meta = bdecode(torrent_handle::get_torrent_info().metadata().get(), torrent_handle::get_torrent_info().metadata().get()+torrent_handle::get_torrent_info().metadata_size());
entry torrent_file(entry::dictionary_t); entry torrent_file(entry::dictionary_t);
torrent_file["info"] = meta; torrent_file["info"] = meta;
if(!h.trackers().empty()) if(!torrent_handle::trackers().empty())
torrent_file["announce"] = h.trackers().front().url; torrent_file["announce"] = torrent_handle::trackers().front().url;
boost::filesystem::ofstream out(path.toLocal8Bit().constData(), std::ios_base::binary); boost::filesystem::ofstream out(path.toLocal8Bit().constData(), std::ios_base::binary);
out.unsetf(std::ios_base::skipws); out.unsetf(std::ios_base::skipws);
bencode(std::ostream_iterator<char>(out), torrent_file); bencode(std::ostream_iterator<char>(out), torrent_file);
@ -712,17 +524,12 @@ bool QTorrentHandle::save_torrent_file(QString path) {
return false; return false;
} }
void QTorrentHandle::set_peer_download_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const {
Q_ASSERT(h.is_valid());
h.set_peer_download_limit(ip, limit);
}
void QTorrentHandle::add_tracker(const announce_entry& url) { void QTorrentHandle::add_tracker(const announce_entry& url) {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
#if LIBTORRENT_VERSION_MINOR > 14 #if LIBTORRENT_VERSION_MINOR > 14
h.add_tracker(url); torrent_handle::add_tracker(url);
#else #else
std::vector<announce_entry> trackers = h.trackers(); std::vector<announce_entry> trackers = torrent_handle::trackers();
bool exists = false; bool exists = false;
std::vector<announce_entry>::iterator it = trackers.begin(); std::vector<announce_entry>::iterator it = trackers.begin();
while(it != trackers.end()) { while(it != trackers.end()) {
@ -734,20 +541,20 @@ void QTorrentHandle::add_tracker(const announce_entry& url) {
} }
if(!exists) { if(!exists) {
trackers.push_back(url); trackers.push_back(url);
h.replace_trackers(trackers); torrent_handle::replace_trackers(trackers);
} }
#endif #endif
} }
void QTorrentHandle::prioritize_first_last_piece(bool b) { void QTorrentHandle::prioritize_first_last_piece(bool b) {
Q_ASSERT(h.is_valid()); Q_ASSERT(torrent_handle::is_valid());
// Detect main file // Detect main file
int rank=0; int rank=0;
int main_file_index = 0; int main_file_index = 0;
file_entry main_file = h.get_torrent_info().file_at(0); file_entry main_file = torrent_handle::get_torrent_info().file_at(0);
torrent_info::file_iterator it = h.get_torrent_info().begin_files(); torrent_info::file_iterator it = torrent_handle::get_torrent_info().begin_files();
it++; ++rank; it++; ++rank;
while(it != h.get_torrent_info().end_files()) { while(it != torrent_handle::get_torrent_info().end_files()) {
if(it->size > main_file.size) { if(it->size > main_file.size) {
main_file = *it; main_file = *it;
main_file_index = rank; main_file_index = rank;
@ -758,35 +565,30 @@ void QTorrentHandle::prioritize_first_last_piece(bool b) {
qDebug("Main file in the torrent is %s", main_file.path.string().c_str()); qDebug("Main file in the torrent is %s", main_file.path.string().c_str());
// Determine the priority to set // Determine the priority to set
int prio = 7; // MAX int prio = 7; // MAX
if(!b) prio = h.file_priority(main_file_index); if(!b) prio = torrent_handle::file_priority(main_file_index);
// Determine the first and last piece of the main file // Determine the first and last piece of the main file
int piece_size = h.get_torrent_info().piece_length(); int piece_size = torrent_handle::get_torrent_info().piece_length();
Q_ASSERT(piece_size>0); Q_ASSERT(piece_size>0);
int first_piece = floor((main_file.offset+1)/(double)piece_size); int first_piece = floor((main_file.offset+1)/(double)piece_size);
Q_ASSERT(first_piece >= 0 && first_piece < h.get_torrent_info().num_pieces()); 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, h.get_torrent_info().num_pieces()-1); qDebug("First piece of the file is %d/%d", first_piece, torrent_handle::get_torrent_info().num_pieces()-1);
int num_pieces_in_file = ceil(main_file.size/(double)piece_size); int num_pieces_in_file = ceil(main_file.size/(double)piece_size);
int last_piece = first_piece+num_pieces_in_file-1; int last_piece = first_piece+num_pieces_in_file-1;
Q_ASSERT(last_piece >= 0 && last_piece < h.get_torrent_info().num_pieces()); 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, h.get_torrent_info().num_pieces()-1); qDebug("last piece of the file is %d/%d", last_piece, torrent_handle::get_torrent_info().num_pieces()-1);
h.piece_priority(first_piece, prio); torrent_handle::piece_priority(first_piece, prio);
h.piece_priority(last_piece, prio); torrent_handle::piece_priority(last_piece, prio);
} }
void QTorrentHandle::rename_file(int index, QString name) { void QTorrentHandle::rename_file(int index, QString name) {
h.rename_file(index, std::string(name.toUtf8().constData())); torrent_handle::rename_file(index, std::string(name.toUtf8().constData()));
} }
// //
// Operators // Operators
// //
QTorrentHandle& QTorrentHandle::operator =(const torrent_handle& new_h) {
h = new_h;
return *this;
}
bool QTorrentHandle::operator ==(const QTorrentHandle& new_h) const{ bool QTorrentHandle::operator ==(const QTorrentHandle& new_h) const{
QString hash = misc::toQString(h.info_hash()); QString hash = misc::toQString(torrent_handle::info_hash());
return (hash == new_h.hash()); return (hash == new_h.hash());
} }

View File

@ -42,10 +42,7 @@ class QStringList;
// A wrapper for torrent_handle in libtorrent // A wrapper for torrent_handle in libtorrent
// to interact well with Qt types // to interact well with Qt types
class QTorrentHandle { class QTorrentHandle : public torrent_handle {
private:
torrent_handle h;
public: public:
@ -59,17 +56,11 @@ class QTorrentHandle {
// //
// Getters // Getters
// //
const torrent_handle& get_torrent_handle() const;
const torrent_info& get_torrent_info() const;
QString hash() const; QString hash() const;
QString name() const; QString name() const;
float progress() const; float progress() const;
bitfield pieces() const; bitfield pieces() const;
void piece_availability(std::vector<int>& avail) const;
void get_download_queue(std::vector<partial_piece_info>& queue) const;
QString current_tracker() const; QString current_tracker() const;
bool is_valid() const;
bool is_paused() const; bool is_paused() const;
bool has_filtered_pieces() const; bool has_filtered_pieces() const;
size_type total_size() const; size_type total_size() const;
@ -84,26 +75,19 @@ class QTorrentHandle {
int num_seeds() const; int num_seeds() const;
int num_complete() const; int num_complete() const;
int num_incomplete() const; int num_incomplete() const;
void scrape_tracker() const;
QString save_path() const; QString save_path() const;
QStringList url_seeds() const; QStringList url_seeds() const;
size_type actual_size() const; size_type actual_size() const;
int download_limit() const;
int upload_limit() const;
int num_files() const; int num_files() const;
bool has_metadata() const;
void save_resume_data() const;
int queue_position() const; int queue_position() const;
bool is_queued() const; bool is_queued() const;
QString file_at(unsigned int index) const; QString file_at(unsigned int index) const;
size_type filesize_at(unsigned int index) const; size_type filesize_at(unsigned int index) const;
const std::vector<announce_entry> trackers() const;
torrent_status::state_t state() const; torrent_status::state_t state() const;
QString creator() const; QString creator() const;
QString comment() const; QString comment() const;
size_type total_failed_bytes() const; size_type total_failed_bytes() const;
size_type total_redundant_bytes() const; size_type total_redundant_bytes() const;
void file_progress(std::vector<size_type>& fp) const;
size_type total_payload_download() const; size_type total_payload_download() const;
size_type total_payload_upload() const; size_type total_payload_upload() const;
size_type all_time_upload() const; size_type all_time_upload() const;
@ -118,18 +102,9 @@ class QTorrentHandle {
bool is_auto_managed() const; bool is_auto_managed() const;
qlonglong active_time() const; qlonglong active_time() const;
qlonglong seeding_time() const; qlonglong seeding_time() const;
std::vector<int> file_priorities() const;
bool is_sequential_download() const;
#if LIBTORRENT_VERSION_MINOR > 14
bool super_seeding() const;
#endif
QString creation_date() const; QString creation_date() const;
QString next_announce() const; QString next_announce() const;
qlonglong next_announce_s() const; qlonglong next_announce_s() const;
void get_peer_info(std::vector<peer_info>&) const;
#ifndef DISABLE_GUI
bool resolve_countries() const;
#endif
bool priv() const; bool priv() const;
bool first_last_piece_first() const; bool first_last_piece_first() const;
QString root_path() const; QString root_path() const;
@ -141,39 +116,14 @@ class QTorrentHandle {
// //
// Setters // Setters
// //
void set_download_limit(int limit);
void set_upload_limit(int limit);
void pause(); void pause();
void resume(); void resume();
void remove_url_seed(QString seed); void remove_url_seed(QString seed);
void add_url_seed(QString seed); void add_url_seed(QString seed);
void set_max_uploads(int val);
void set_max_connections(int val);
void prioritize_files(const std::vector<int> &v); void prioritize_files(const std::vector<int> &v);
void file_priority(int index, int priority) const; void file_priority(int index, int priority) const;
void set_ratio(float ratio) const;
void replace_trackers(const std::vector<announce_entry>& trackers) const;
void force_reannounce();
void set_sequential_download(bool);
void set_tracker_login(QString username, QString password); void set_tracker_login(QString username, QString password);
void queue_position_down() const;
void queue_position_up() const;
void queue_position_top() const;
void queue_position_bottom() const;
void auto_managed(bool) const;
void force_recheck() const;
void move_storage(QString path) const; void move_storage(QString path) const;
#if LIBTORRENT_VERSION_MINOR > 14
void super_seeding(bool on) const;
void flush_cache() const;
#endif
#ifndef DISABLE_GUI
void resolve_countries(bool r);
#endif
void connect_peer(libtorrent::asio::ip::tcp::endpoint const& adr, int source = 0) const;
void set_peer_upload_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const;
void set_peer_download_limit(libtorrent::asio::ip::tcp::endpoint ip, int limit) const;
void add_tracker(const announce_entry& url); void add_tracker(const announce_entry& url);
void prioritize_first_last_piece(bool b); void prioritize_first_last_piece(bool b);
void rename_file(int index, QString name); void rename_file(int index, QString name);
@ -182,7 +132,6 @@ class QTorrentHandle {
// //
// Operators // Operators
// //
QTorrentHandle& operator =(const torrent_handle& new_h);
bool operator ==(const QTorrentHandle& new_h) const; bool operator ==(const QTorrentHandle& new_h) const;
}; };

View File

@ -291,7 +291,7 @@ public:
QHash<QString, QVariant> data; QHash<QString, QVariant> data;
data["is_magnet"] = is_magnet; data["is_magnet"] = is_magnet;
if(is_magnet) { if(is_magnet) {
data["magnet_uri"] = misc::toQString(make_magnet_uri(h.get_torrent_handle())); data["magnet_uri"] = misc::toQString(make_magnet_uri(h));
} }
data["seed"] = h.is_seed(); data["seed"] = h.is_seed();
data["priority"] = h.queue_position(); data["priority"] = h.queue_position();