From 772028106e17eb278f42487b16537d0d2eb19b2e Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Tue, 6 Apr 2010 09:17:33 +0000 Subject: [PATCH] FEATURE: Simplified torrent root folder renaming/truncating (< v2.3.0 is no longer forward compatible) --- Changelog | 1 + src/bittorrent.cpp | 46 ++++++++++++++++++++++++++++++---------- src/bittorrent.h | 2 +- src/misc.cpp | 31 +++++++++++++++++++++++++++ src/misc.h | 5 +++++ src/torrentadditiondlg.h | 16 +++++++++++++- src/torrentfilesmodel.h | 18 +++++++++------- 7 files changed, 98 insertions(+), 21 deletions(-) diff --git a/Changelog b/Changelog index 6ac911dea..f452f2000 100644 --- a/Changelog +++ b/Changelog @@ -1,4 +1,5 @@ * Unreleased - Christophe Dumez - v2.3.0 + - FEATURE: Simplified torrent root folder renaming/truncating (< v2.3.0 is no longer forward compatible) - FEATURE: Max number of half-open connections can now be edited - FEATURE: Added support for strict super seeding diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index c3dd4c5e1..beb30bc65 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -839,7 +839,8 @@ QTorrentHandle Bittorrent::addMagnetUri(QString magnet_uri, bool resumed) { qDebug("Successfuly loaded"); } } - const QString &savePath = getSavePath(hash); + QString torrent_name = misc::magnetUriToName(magnet_uri); + const QString &savePath = getSavePath(hash, false, QString::null, torrent_name); if(!defaultTempPath.isEmpty() && resumed && !TorrentPersistentData::isSeed(hash)) { qDebug("addMagnetURI: Temp folder is enabled."); p.save_path = defaultTempPath.toLocal8Bit().constData(); @@ -1035,6 +1036,7 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr } return h; } + QString root_folder = misc::truncateRootFolder(t); add_torrent_params p; //Getting fast resume data if existing std::vector buf; @@ -1052,7 +1054,7 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr // Enforcing the save path defined before URL download (from RSS for example) savePath = savepath_fromurl.take(QUrl::fromEncoded(from_url.toLocal8Bit())); } else { - savePath = getSavePath(hash, fromScanDir, path); + savePath = getSavePath(hash, fromScanDir, path, root_folder); } if(!defaultTempPath.isEmpty() && resumed && !TorrentPersistentData::isSeed(hash)) { qDebug("addTorrent::Temp folder is enabled."); @@ -1938,12 +1940,6 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { QTorrentHandle h(p->handle); if(h.is_valid()) { qDebug("Received metadata for %s", qPrintable(h.hash())); -#ifdef LIBTORRENT_0_15 - // Append .!qB to incomplete files - if(appendqBExtension) - appendqBextensionToTorrent(h, true); -#endif - emit metadataReceived(h); // Save metadata const QDir torrentBackup(misc::BTBackupLocation()); if(!QFile::exists(torrentBackup.path()+QDir::separator()+h.hash()+QString(".torrent"))) @@ -1968,11 +1964,20 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { } } } +#ifdef LIBTORRENT_0_15 + // Append .!qB to incomplete files + if(appendqBExtension) + appendqBextensionToTorrent(h, true); +#endif + // Truncate root folder + misc::truncateRootFolder(p->handle); + emit metadataReceived(h); if(h.is_paused()) { // XXX: Unfortunately libtorrent-rasterbar does not send a torrent_paused_alert // and the torrent can be paused when metadata is received emit pausedTorrent(h); } + } } else if (file_error_alert* p = dynamic_cast(a.get())) { @@ -2148,7 +2153,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { return s->status(); } - QString Bittorrent::getSavePath(QString hash, bool fromScanDir, QString filePath) { + QString Bittorrent::getSavePath(QString hash, bool fromScanDir, QString filePath, QString root_folder) { QString savePath; if(TorrentTempData::hasTempData(hash)) { savePath = TorrentTempData::getSavePath(hash); @@ -2168,11 +2173,19 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { qDebug("getSavePath, got save_path from temp data: %s", qPrintable(savePath)); } else { savePath = TorrentPersistentData::getSavePath(hash); + bool append_root_folder = false; if(savePath.isEmpty()) { if(fromScanDir && m_scanFolders->downloadInTorrentFolder(filePath)) savePath = QFileInfo(filePath).dir().path(); - else + else { savePath = defaultSavePath; + append_root_folder = true; + } + } else { + QSettings settings("qBittorrent", "qBittorrent"); + if(!settings.value("ported_to_new_savepath_system", false).toBool()) { + append_root_folder = true; + } } if(!fromScanDir && appendLabelToSavePath) { const QString &label = TorrentPersistentData::getLabel(hash); @@ -2183,6 +2196,13 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { } } } + if(append_root_folder && !root_folder.isEmpty()) { + // Append torrent root folder to the save path + if(!savePath.endsWith(QDir::separator())) + savePath += QDir::separator(); + savePath += root_folder; + TorrentPersistentData::saveSavePath(hash, savePath); + } qDebug("getSavePath, got save_path from persistent data: %s", qPrintable(savePath)); } // Clean path @@ -2296,6 +2316,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { filters << "*.torrent"; const QStringList &torrents_on_hd = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted); foreach(QString hash, torrents_on_hd) { + qDebug("found torrent with hash: %s on hard disk", qPrintable(hash)); hash.chop(8); // remove trailing .torrent if(!known_torrents.contains(hash)) { std::cerr << "ERROR Detected!!! Adding back torrent " << qPrintable(hash) << " which got lost for some reason." << std::endl; @@ -2317,9 +2338,10 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { const int prio = TorrentPersistentData::getPriority(hash); torrent_queue.push(qMakePair(prio, hash)); } + qDebug("Priority_queue size: %ld", (long)torrent_queue.size()); // Resume downloads while(!torrent_queue.empty()) { - const QString &hash = torrent_queue.top().second; + const QString hash = torrent_queue.top().second; torrent_queue.pop(); qDebug("Starting up torrent %s", qPrintable(hash)); if(TorrentPersistentData::isMagnet(hash)) { @@ -2338,5 +2360,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { addTorrent(torrentBackup.path()+QDir::separator()+hash+".torrent", false, QString(), true); } } + QSettings settings("qBittorrent", "qBittorrent"); + settings.setValue("ported_to_new_savepath_system", true); qDebug("Unfinished torrents resumed"); } diff --git a/src/bittorrent.h b/src/bittorrent.h index bb7175855..9fe9af925 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -186,7 +186,7 @@ public slots: void recursiveTorrentDownload(const QTorrentHandle &h); protected: - QString getSavePath(QString hash, bool fromScanDir = false, QString filePath = QString()); + QString getSavePath(QString hash, bool fromScanDir = false, QString filePath = QString::null, QString root_folder=QString::null); bool initWebUi(QString username, QString password, int port); protected slots: diff --git a/src/misc.cpp b/src/misc.cpp index 63bc9a184..89bad3a74 100644 --- a/src/misc.cpp +++ b/src/misc.cpp @@ -166,6 +166,37 @@ long long misc::freeDiskSpaceOnPath(QString path) { #endif } +QString misc::truncateRootFolder(boost::intrusive_ptr t) { + QString root_folder; + int i = 0; + for(torrent_info::file_iterator it = t->begin_files(); it < t->end_files(); it++) { + QString path = QString::fromUtf8(it->path.string().c_str()); + QStringList path_parts = path.split("/", QString::SkipEmptyParts); + if(path_parts.size() > 1) { + root_folder = path_parts.takeFirst(); + t->rename_file(i, std::string(path_parts.join("/").toUtf8())); + } + ++i; + } + return root_folder; +} + +QString misc::truncateRootFolder(libtorrent::torrent_handle h) { + QString root_folder; + int i = 0; + torrent_info t = h.get_torrent_info(); + for(torrent_info::file_iterator it = t.begin_files(); it < t.end_files(); it++) { + QString path = QString::fromUtf8(it->path.string().c_str()); + QStringList path_parts = path.split("/", QString::SkipEmptyParts); + if(path_parts.size() > 1) { + root_folder = path_parts.takeFirst(); + h.rename_file(i, std::string(path_parts.join("/").toUtf8())); + } + ++i; + } + return root_folder; +} + bool misc::sameFiles(QString path1, QString path2) { QFile f1(path1); if(!f1.exists()) return false; diff --git a/src/misc.h b/src/misc.h index 0a007b57e..28c4ea25e 100644 --- a/src/misc.h +++ b/src/misc.h @@ -40,6 +40,8 @@ #include #include +#include + using namespace libtorrent; /* Miscellaneaous functions that can be useful */ @@ -68,6 +70,9 @@ public: return x; } + static QString truncateRootFolder(boost::intrusive_ptr t); + static QString truncateRootFolder(torrent_handle h); + static bool sameFiles(QString path1, QString path2); static void copyDir(QString src_path, QString dst_path); // Introduced in v2.1.0 for backward compatibility diff --git a/src/torrentadditiondlg.h b/src/torrentadditiondlg.h index c3b84b05f..3fd8aa70e 100644 --- a/src/torrentadditiondlg.h +++ b/src/torrentadditiondlg.h @@ -157,7 +157,14 @@ public: return; } fileName = misc::magnetUriToName(magnet_uri); - if(fileName.isEmpty()) fileName = tr("Magnet Link"); + if(fileName.isEmpty()) { + fileName = tr("Magnet Link"); + } else { + QString save_path = savePathTxt->text(); + if(!save_path.endsWith(QDir::separator())) + save_path += QDir::separator(); + savePathTxt->setText(save_path + fileName); + } fileNameLbl->setText(QString::fromUtf8("
")+fileName+QString::fromUtf8("
")); // Update display updateDiskSpaceLabels(); @@ -199,6 +206,13 @@ public: close(); return; } + QString root_folder = misc::truncateRootFolder(t); + if(!root_folder.isEmpty()) { + QString save_path = savePathTxt->text(); + if(!save_path.endsWith(QDir::separator())) + save_path += QDir::separator(); + savePathTxt->setText(save_path + root_folder); + } nbFiles = t->num_files(); // Setting file name fileName = misc::toQString(t->name()); diff --git a/src/torrentfilesmodel.h b/src/torrentfilesmodel.h index 1e798dacb..af535fbbf 100644 --- a/src/torrentfilesmodel.h +++ b/src/torrentfilesmodel.h @@ -505,7 +505,7 @@ public: files_index = new TreeItem*[t.num_files()]; TreeItem *parent = this->rootItem; - if(t.num_files() == 1) { + /*if(t.num_files() == 1) { // Create possible parent folder QStringList path_parts = misc::toQString(t.file_at(0).path.string()).split("/"); path_parts.removeLast(); @@ -518,12 +518,14 @@ public: files_index[0] = f; emit layoutChanged(); return; - } + }*/ // Create parent folder - QString root_name = misc::toQString(t.file_at(0).path.string()).split("/").first(); - TreeItem *current_parent = new TreeItem(root_name, parent); + //QString root_name = misc::toQString(t.file_at(0).path.string()).split("/").first(); + //TreeItem *current_parent = new TreeItem(root_name, parent); //parent->appendChild(current_parent); - TreeItem *root_folder = current_parent; + //TreeItem *root_folder = current_parent; + TreeItem *root_folder = parent; + TreeItem *current_parent; // Iterate over files int i = 0; @@ -533,10 +535,10 @@ public: QString path = QDir::cleanPath(misc::toQString(fi->path.string())); // Iterate of parts of the path to create necessary folders QStringList pathFolders = path.split("/"); - Q_ASSERT(pathFolders.size() >= 2); + //Q_ASSERT(pathFolders.size() >= 2); QString fileName = pathFolders.takeLast(); - QString currentFolderName = pathFolders.takeFirst(); - Q_ASSERT(currentFolderName == current_parent->getName()); + //QString currentFolderName = pathFolders.takeFirst(); + //Q_ASSERT(currentFolderName == current_parent->getName()); foreach(const QString &pathPart, pathFolders) { TreeItem *new_parent = current_parent->childWithName(pathPart); if(!new_parent) {