From fb443bb6e755ed522bc17621f14b16e1333719db Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 6 Jan 2011 12:12:07 +0000 Subject: [PATCH] Hide unwanted files that have to be partly downloaded --- Changelog | 1 + src/misc.cpp | 9 ++- src/qtlibtorrent/qtorrenthandle.cpp | 93 +++++++++++++++++------------ src/qtlibtorrent/qtorrenthandle.h | 4 +- src/torrentfilesmodel.h | 1 + 5 files changed, 66 insertions(+), 42 deletions(-) diff --git a/Changelog b/Changelog index eb4ca149b..ecce3b01c 100644 --- a/Changelog +++ b/Changelog @@ -11,6 +11,7 @@ - FEATURE: Added a search engine plugin to extratorrent.com - FEATURE: Added a search engine plugin for kickasstorrents.com - FEATURE: Added auto-suspend upon downloads completion feature + - BUGFIX: Hide unwanted files that have to be partly downloaded - I18N: Added Galician translation - COSMETIC: Same deletion confirmation dialog in the GUI and Web UI - COSMETIC: Simplified the top toolbar diff --git a/src/misc.cpp b/src/misc.cpp index 2bf4c0fd4..faf847883 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -35,6 +35,7 @@ #include #include #include +#include #ifdef DISABLE_GUI #include @@ -772,7 +773,13 @@ QString misc::branchPath(QString file_path, bool uses_slashes) { if(!uses_slashes) file_path.replace("\\", "/"); - return file_path.left(file_path.lastIndexOf('/')); + Q_ASSERT(!file_path.contains("\\")); + if(file_path.endsWith("/")) + file_path.chop(1); // Remove trailing slash + qDebug() << Q_FUNC_INFO << "before:" << file_path; + if(file_path.contains("/")) + return file_path.left(file_path.lastIndexOf('/')); + return ""; } bool misc::isUrl(const QString &s) diff --git a/src/qtlibtorrent/qtorrenthandle.cpp b/src/qtlibtorrent/qtorrenthandle.cpp index 9d92b4158..ff50f0120 100644 --- a/src/qtlibtorrent/qtorrenthandle.cpp +++ b/src/qtlibtorrent/qtorrenthandle.cpp @@ -46,6 +46,7 @@ #include using namespace libtorrent; +using namespace std; QTorrentHandle::QTorrentHandle(torrent_handle h): torrent_handle(h) {} @@ -456,45 +457,6 @@ void QTorrentHandle::add_url_seed(QString seed) const { torrent_handle::add_url_seed(str_seed); } -void QTorrentHandle::prioritize_files(const std::vector &v) const { - // Does not do anything for seeding torrents - if(v.size() != (unsigned int)torrent_handle::get_torrent_info().num_files()) - return; - bool was_seed = is_seed(); - torrent_handle::prioritize_files(v); - if(was_seed && !is_seed()) { - // Reset seed status - TorrentPersistentData::saveSeedStatus(*this); - // Move to temp folder if necessary - const Preferences pref; - if(pref.isTempPathEnabled()) { - QString tmp_path = pref.getTempPath(); - 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 - const Preferences pref; - if(pref.isTempPathEnabled()) { - QString tmp_path = pref.getTempPath(); - QString root_folder = TorrentPersistentData::getRootFolder(hash()); - if(!root_folder.isEmpty()) - tmp_path = QDir(tmp_path).absoluteFilePath(root_folder); - move_storage(tmp_path); - } - } -} - void QTorrentHandle::set_tracker_login(QString username, QString password) const { torrent_handle::set_tracker_login(std::string(username.toLocal8Bit().constData()), std::string(password.toLocal8Bit().constData())); } @@ -526,6 +488,59 @@ bool QTorrentHandle::save_torrent_file(QString path) const { return false; } +void QTorrentHandle::file_priority(int index, int priority) const { + vector priorities = torrent_handle::file_priorities(); + if(priorities[index] != priority) { + priorities[index] = priority; + prioritize_files(priorities); + } +} + +// TODO: Also hide the folder on Windows +void QTorrentHandle::prioritize_files(const vector &files) const { + if((int)files.size() != torrent_handle::get_torrent_info().num_files()) return; + const vector prev_priorities = torrent_handle::file_priorities(); + bool was_seed = is_seed(); + torrent_handle::prioritize_files(files); + for(uint i=0; i 0 && files[i] == 0) { + QString old_path = filepath_at(i); + QString old_name = filename_at(i); + QDir parent_path(misc::branchPath(old_path)); + if(parent_path.dirName() != ".unwanted") { + parent_path.mkdir(".unwanted"); + rename_file(i, parent_path.filePath(".unwanted/"+old_name)); + } + } + // Move wanted files back to their original folder + if(prev_priorities[i] == 0 && files[i] > 0) { + QString old_path = filepath_at(i); + QString old_name = filename_at(i); + QDir parent_path(misc::branchPath(old_path)); + if(parent_path.dirName() == ".unwanted") { + QDir new_path(misc::branchPath(parent_path.path())); + rename_file(i, new_path.filePath(old_name)); + // Remove .unwanted directory if empty + new_path.rmdir(".unwanted"); + } + } + } + if(was_seed && !is_seed()) { + // Save seed status + TorrentPersistentData::saveSeedStatus(*this); + // Move to temp folder if necessary + const Preferences pref; + if(pref.isTempPathEnabled()) { + QString tmp_path = pref.getTempPath(); + QString root_folder = TorrentPersistentData::getRootFolder(hash()); + if(!root_folder.isEmpty()) + tmp_path = QDir(tmp_path).absoluteFilePath(root_folder); + move_storage(tmp_path); + } + } +} + void QTorrentHandle::add_tracker(const announce_entry& url) const { #if LIBTORRENT_VERSION_MINOR > 14 torrent_handle::add_tracker(url); diff --git a/src/qtlibtorrent/qtorrenthandle.h b/src/qtlibtorrent/qtorrenthandle.h index 6aa45e40e..701bad9b9 100644 --- a/src/qtlibtorrent/qtorrenthandle.h +++ b/src/qtlibtorrent/qtorrenthandle.h @@ -122,14 +122,14 @@ public: void resume() const; void remove_url_seed(QString seed) const; void add_url_seed(QString seed) const; - void prioritize_files(const std::vector &v) const; - void file_priority(int index, int priority) const; void set_tracker_login(QString username, QString password) const; void move_storage(QString path) const; void add_tracker(const libtorrent::announce_entry& url) const; void prioritize_first_last_piece(bool b) const; void rename_file(int index, QString name) const; bool save_torrent_file(QString path) const; + void prioritize_files(const std::vector &files) const; + void file_priority(int index, int priority) const; // // Operators diff --git a/src/torrentfilesmodel.h b/src/torrentfilesmodel.h index c4de7cde9..a29cb12a5 100644 --- a/src/torrentfilesmodel.h +++ b/src/torrentfilesmodel.h @@ -546,6 +546,7 @@ public: #endif // Iterate of parts of the path to create necessary folders QStringList pathFolders = path.split("/"); + pathFolders.removeAll(".unwanted"); pathFolders.takeLast(); foreach(const QString &pathPart, pathFolders) { TreeItem *new_parent = current_parent->childWithName(pathPart);