Browse Source

FEATURE: Simplified torrent root folder renaming/truncating (< v2.3.0 is no longer forward compatible)

adaptive-webui-19844
Christophe Dumez 15 years ago
parent
commit
772028106e
  1. 1
      Changelog
  2. 46
      src/bittorrent.cpp
  3. 2
      src/bittorrent.h
  4. 31
      src/misc.cpp
  5. 5
      src/misc.h
  6. 16
      src/torrentadditiondlg.h
  7. 18
      src/torrentfilesmodel.h

1
Changelog

@ -1,4 +1,5 @@ @@ -1,4 +1,5 @@
* Unreleased - Christophe Dumez <chris@qbittorrent.org> - 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

46
src/bittorrent.cpp

@ -839,7 +839,8 @@ QTorrentHandle Bittorrent::addMagnetUri(QString magnet_uri, bool resumed) { @@ -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 @@ -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<char> buf;
@ -1052,7 +1054,7 @@ QTorrentHandle Bittorrent::addTorrent(QString path, bool fromScanDir, QString fr @@ -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) { @@ -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) { @@ -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<file_error_alert*>(a.get())) {
@ -2148,7 +2153,7 @@ void Bittorrent::addConsoleMessage(QString msg, QString) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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) { @@ -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");
}

2
src/bittorrent.h

@ -186,7 +186,7 @@ public slots: @@ -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:

31
src/misc.cpp

@ -166,6 +166,37 @@ long long misc::freeDiskSpaceOnPath(QString path) { @@ -166,6 +166,37 @@ long long misc::freeDiskSpaceOnPath(QString path) {
#endif
}
QString misc::truncateRootFolder(boost::intrusive_ptr<torrent_info> 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;

5
src/misc.h

@ -40,6 +40,8 @@ @@ -40,6 +40,8 @@
#include <QPoint>
#include <libtorrent/torrent_info.hpp>
#include <libtorrent/torrent_handle.hpp>
using namespace libtorrent;
/* Miscellaneaous functions that can be useful */
@ -68,6 +70,9 @@ public: @@ -68,6 +70,9 @@ public:
return x;
}
static QString truncateRootFolder(boost::intrusive_ptr<torrent_info> 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

16
src/torrentadditiondlg.h

@ -157,7 +157,14 @@ public: @@ -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("<center><b>")+fileName+QString::fromUtf8("</b></center>"));
// Update display
updateDiskSpaceLabels();
@ -199,6 +206,13 @@ public: @@ -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());

18
src/torrentfilesmodel.h

@ -505,7 +505,7 @@ public: @@ -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: @@ -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: @@ -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) {

Loading…
Cancel
Save