FEATURE: Download first/last pieces first now applies to all media files in the torrent (Thanks Ahmad)

This commit is contained in:
Christophe Dumez 2010-12-04 20:47:20 +00:00
commit 6d1ad28d8c
4 changed files with 299 additions and 299 deletions

View file

@ -14,6 +14,8 @@
- FEATURE: Optimized and improved the peer country resolution code - FEATURE: Optimized and improved the peer country resolution code
- FEATURE: Download first/last pieces first when sequential download is - FEATURE: Download first/last pieces first when sequential download is
enabled (Thanks Ahmad) enabled (Thanks Ahmad)
- FEATURE: Download first/last pieces first now applies to all media files
in the torrent (Thanks Ahmad)
- BUGFIX: Fix SOCKS5 proxy authentication in search engine(closes #680072) - BUGFIX: Fix SOCKS5 proxy authentication in search engine(closes #680072)
- BUGFIX: Fix two advanced settings (ignore limits on LAN and protocol - BUGFIX: Fix two advanced settings (ignore limits on LAN and protocol
overhead inclusion in rate limiter) overhead inclusion in rate limiter)

View file

@ -639,9 +639,9 @@ void PropertiesWidget::renameSelectedFile() {
} }
} }
} }
} }
void PropertiesWidget::askWebSeed(){ void PropertiesWidget::askWebSeed(){
bool ok; bool ok;
// Ask user for a new url seed // Ask user for a new url seed
const QString url_seed = QInputDialog::getText(this, tr("New url seed", "New HTTP source"), const QString url_seed = QInputDialog::getText(this, tr("New url seed", "New HTTP source"),
@ -658,9 +658,9 @@ void PropertiesWidget::renameSelectedFile() {
h.add_url_seed(url_seed); h.add_url_seed(url_seed);
// Refresh the seeds list // Refresh the seeds list
loadUrlSeeds(); loadUrlSeeds();
} }
void PropertiesWidget::deleteSelectedUrlSeeds(){ void PropertiesWidget::deleteSelectedUrlSeeds(){
const QList<QListWidgetItem *> selectedItems = listWebSeeds->selectedItems(); const QList<QListWidgetItem *> selectedItems = listWebSeeds->selectedItems();
bool change = false; bool change = false;
foreach(const QListWidgetItem *item, selectedItems){ foreach(const QListWidgetItem *item, selectedItems){
@ -672,15 +672,13 @@ void PropertiesWidget::renameSelectedFile() {
// Refresh list // Refresh list
loadUrlSeeds(); loadUrlSeeds();
} }
} }
bool PropertiesWidget::applyPriorities() { bool PropertiesWidget::applyPriorities() {
qDebug("Saving files priorities"); qDebug("Saving files priorities");
const std::vector<int> priorities = PropListModel->getFilesPriorities(h.get_torrent_info().num_files()); const std::vector<int> priorities = PropListModel->getFilesPriorities(h.get_torrent_info().num_files());
bool first_last_piece_first = false;
// Save first/last piece first option state // Save first/last piece first option state
if(h.first_last_piece_first()) bool first_last_piece_first = h.first_last_piece_first();
first_last_piece_first = true;
// Prioritize the files // Prioritize the files
qDebug("prioritize files: %d", priorities[0]); qDebug("prioritize files: %d", priorities[0]);
h.prioritize_files(priorities); h.prioritize_files(priorities);
@ -688,10 +686,10 @@ void PropertiesWidget::renameSelectedFile() {
if(first_last_piece_first) if(first_last_piece_first)
h.prioritize_first_last_piece(true); h.prioritize_first_last_piece(true);
return true; return true;
} }
void PropertiesWidget::on_changeSavePathButton_clicked() { void PropertiesWidget::on_changeSavePathButton_clicked() {
if(!h.is_valid()) return; if(!h.is_valid()) return;
QString new_path; QString new_path;
if(h.has_metadata() && h.num_files() == 1) { if(h.has_metadata() && h.num_files() == 1) {
@ -747,10 +745,10 @@ void PropertiesWidget::renameSelectedFile() {
#endif #endif
save_path->setText(display_path); save_path->setText(display_path);
} }
} }
void PropertiesWidget::filteredFilesChanged() { void PropertiesWidget::filteredFilesChanged() {
if(h.is_valid()) { if(h.is_valid()) {
applyPriorities(); applyPriorities();
} }
} }

View file

@ -122,27 +122,23 @@ int QTorrentHandle::num_pieces() const {
} }
bool QTorrentHandle::first_last_piece_first() const { bool QTorrentHandle::first_last_piece_first() const {
// Detect main file // Detect first media file
int rank=0; torrent_info::file_iterator it;
int main_file_index = 0; int index = 0;
file_entry main_file = torrent_handle::get_torrent_info().file_at(0); for(it = get_torrent_info().begin_files(); it != get_torrent_info().end_files(); it++) {
torrent_info::file_iterator it = torrent_handle::get_torrent_info().begin_files(); const QString ext = misc::toQStringU(it->path.string()).split(".").last();
it++; ++rank; if(misc::isPreviewable(ext) && torrent_handle::file_priority(index) > 0) {
while(it != torrent_handle::get_torrent_info().end_files()) { break;
if(it->size > main_file.size) {
main_file = *it;
main_file_index = rank;
} }
it++; ++index;
++rank;
} }
qDebug() << "Main file in the torrent is" << filepath(main_file); file_entry media_file = torrent_handle::get_torrent_info().file_at(index);
int piece_size = torrent_handle::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((media_file.offset+1)/(double)piece_size);
Q_ASSERT(first_piece >= 0 && first_piece < torrent_handle::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, torrent_handle::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(media_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 < torrent_handle::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, torrent_handle::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);
@ -550,32 +546,18 @@ void QTorrentHandle::add_tracker(const announce_entry& url) const {
#endif #endif
} }
void QTorrentHandle::prioritize_first_last_piece(bool b) const { void QTorrentHandle::prioritize_first_last_piece(int file_index, bool b) const {
// Detect main file
int rank=0;
int main_file_index = 0;
file_entry main_file = torrent_handle::get_torrent_info().file_at(0);
torrent_info::file_iterator it = torrent_handle::get_torrent_info().begin_files();
it++; ++rank;
while(it != torrent_handle::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" << filepath(main_file);
// Determine the priority to set // Determine the priority to set
int prio = 7; // MAX int prio = 7; // MAX
if(!b) prio = torrent_handle::file_priority(main_file_index); if(!b) prio = torrent_handle::file_priority(file_index);
// Determine the first and last piece of the main file file_entry file = get_torrent_info().file_at(file_index);
// Determine the first and last piece of the file
int piece_size = torrent_handle::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((file.offset+1)/(double)piece_size);
Q_ASSERT(first_piece >= 0 && first_piece < torrent_handle::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, torrent_handle::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(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 < torrent_handle::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, torrent_handle::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);
@ -583,6 +565,20 @@ void QTorrentHandle::prioritize_first_last_piece(bool b) const {
torrent_handle::piece_priority(last_piece, prio); torrent_handle::piece_priority(last_piece, prio);
} }
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;
}
}
void QTorrentHandle::rename_file(int index, QString name) const { void QTorrentHandle::rename_file(int index, QString name) const {
torrent_handle::rename_file(index, std::string(name.toUtf8().constData())); torrent_handle::rename_file(index, std::string(name.toUtf8().constData()));
} }

View file

@ -42,7 +42,7 @@ class QStringList;
// to interact well with Qt types // to interact well with Qt types
class QTorrentHandle : public libtorrent::torrent_handle { class QTorrentHandle : public libtorrent::torrent_handle {
public: public:
// //
// Constructors // Constructors
@ -135,6 +135,10 @@ class QTorrentHandle : public libtorrent::torrent_handle {
// Operators // Operators
// //
bool operator ==(const QTorrentHandle& new_h) const; bool operator ==(const QTorrentHandle& new_h) const;
private:
void prioritize_first_last_piece(int file_index, bool b) const;
}; };
#endif #endif