From c1378450fc6ed9870e367605883a9d1a53f766e8 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Thu, 27 Aug 2009 03:13:39 +0000 Subject: [PATCH] - Import torrents from v1.4.x (or earlier) in order not to lose them when updating --- src/bittorrent.cpp | 124 ++++++++++++++++++++++++++++++++++++++++++++- src/bittorrent.h | 2 + src/src.pro | 2 +- 3 files changed, 126 insertions(+), 2 deletions(-) diff --git a/src/bittorrent.cpp b/src/bittorrent.cpp index 3d0d95126..16c2f5dc4 100644 --- a/src/bittorrent.cpp +++ b/src/bittorrent.cpp @@ -60,7 +60,7 @@ bittorrent::bittorrent() : DHTEnabled(false), preAllocateAll(false), addInPause( // To avoid some exceptions fs::path::default_name_check(fs::no_check); // Creating bittorrent session - // Check if we should spoof azureus + // Check if we should spoof utorrent QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); if(settings.value(QString::fromUtf8("AzureusSpoof"), false).toBool()) { s = new session(fingerprint("UT", 1, 8, 3, 0), 0); @@ -574,6 +574,11 @@ QTorrentHandle bittorrent::addTorrent(QString path, bool fromScanDir, QString fr } else { p.save_path = defaultTempPath.toLocal8Bit().data(); } + // TODO: Remove in v1.6.0: For backward compatibility only + if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished")) { + p.save_path = savePath.toLocal8Bit().data(); + QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".finished"); + } p.ti = t; // Preallocate all? if(preAllocateAll) @@ -659,6 +664,117 @@ QTorrentHandle bittorrent::addTorrent(QString path, bool fromScanDir, QString fr return h; } +// Import torrents temp data from v1.4.0 or earlier: save_path, filtered pieces +// TODO: Remove in qBittorrent v1.6.0 +void bittorrent::importOldTempData(QString torrent_path) { + // Create torrent hash + boost::intrusive_ptr t; + try { + t = new torrent_info(torrent_path.toLocal8Bit().data()); + QString hash = misc::toQString(t->info_hash()); + // Load save path + QFile savepath_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".savepath"); + QByteArray line; + QString savePath; + if(savepath_file.open(QIODevice::ReadOnly | QIODevice::Text)) { + line = savepath_file.readAll(); + savepath_file.close(); + qDebug(" -> Save path: %s", line.data()); + savePath = QString::fromUtf8(line.data()); + qDebug("Imported the following save path: %s", savePath.toLocal8Bit().data()); + TorrentTempData::setSavePath(hash, savePath); + // Clean up + savepath_file.remove(); + } + // Load pieces priority + QFile pieces_file(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".priorities"); + if(pieces_file.exists()){ + // Read saved file + if(pieces_file.open(QIODevice::ReadOnly | QIODevice::Text)) { + QByteArray pieces_priorities = pieces_file.readAll(); + pieces_file.close(); + QList pieces_priorities_list = pieces_priorities.split('\n'); + std::vector pp; + for(int i=0; inum_files(); ++i) { + int priority = pieces_priorities_list.at(i).toInt(); + if( priority < 0 || priority > 7) { + priority = 1; + } + qDebug("Setting piece piority to %d", priority); + pp.push_back(priority); + } + TorrentTempData::setFilesPriority(hash, pp); + qDebug("Successfuly imported pieces_priority"); + } + // Clean up + pieces_file.remove(); + } + // Load sequential + if(QFile::exists(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".incremental")) { + qDebug("Imported torrent was sequential"); + TorrentTempData::setSequential(hash, true); + // Cleanup + QFile::remove(misc::qBittorrentPath()+"BT_backup"+QDir::separator()+hash+".incremental"); + } + } catch(std::exception&) { + } +} + +// Import torrents from v1.4.0 or earlier +// TODO: Remove in qBittorrent v1.6.0 +void bittorrent::importOldTorrents() { + QSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent")); + if(!settings.value("v1_4_x_torrent_imported", false).toBool()) { + // Import old torrent + QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); + QStringList fileNames; + QStringList filters; + filters << "*.torrent"; + fileNames = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted); + if(isQueueingEnabled()) { + QList > filePaths; + foreach(const QString &fileName, fileNames) { + QString filePath = torrentBackup.path()+QDir::separator()+fileName; + int prio = 99999; + // Get priority + QString prioPath = filePath; + prioPath.replace(".torrent", ".prio"); + if(QFile::exists(prioPath)) { + QFile prio_file(prioPath); + if(prio_file.open(QIODevice::ReadOnly | QIODevice::Text)) { + bool ok = false; + prio = prio_file.readAll().toInt(&ok); + if(!ok) + prio = 99999; + prio_file.close(); + // Clean up + prio_file.remove(); + } + } + misc::insertSort2(filePaths, qMakePair(prio, filePath)); + } + // Resume downloads + QPair fileName; + foreach(fileName, filePaths) { + importOldTempData(fileName.second); + addTorrent(fileName.second, false, QString(), true); + } + } else { + QStringList filePaths; + foreach(const QString &fileName, fileNames) { + filePaths.append(torrentBackup.path()+QDir::separator()+fileName); + } + // Resume downloads + foreach(const QString &fileName, filePaths) { + importOldTempData(fileName); + addTorrent(fileName, false, QString(), true); + } + } + settings.setValue("v1_4_x_torrent_imported", true); + std::cout << "Successfully imported torrents from v1.4.x (or previous) instance" << std::endl; + } +} + // Check if the user filtered files in this torrent. bool bittorrent::has_filtered_files(QString hash) const{ QVariantList files_priority = TorrentPersistentData::getFilesPriority(hash); @@ -1515,6 +1631,12 @@ void bittorrent::startUpTorrents() { QDir torrentBackup(misc::qBittorrentPath() + "BT_backup"); QStringList fileNames; QStringList known_torrents = TorrentPersistentData::knownTorrents(); + if(known_torrents.empty()) { + qDebug("No known torrent, importing old torrents"); + importOldTorrents(); + return; + } + qDebug("Starting up torrents"); if(isQueueingEnabled()) { QList > hashes; foreach(const QString &hash, known_torrents) { diff --git a/src/bittorrent.h b/src/bittorrent.h index fb60900d2..cd891bc27 100644 --- a/src/bittorrent.h +++ b/src/bittorrent.h @@ -114,6 +114,8 @@ class bittorrent : public QObject { public slots: QTorrentHandle addTorrent(QString path, bool fromScanDir = false, QString from_url = QString(), bool resumed = false); QTorrentHandle addMagnetUri(QString magnet_uri, bool resumed=false); + void importOldTorrents(); + void importOldTempData(QString torrent_path); void loadSessionState(); void saveSessionState(); void downloadFromUrl(QString url); diff --git a/src/src.pro b/src/src.pro index 5b4ffabdc..6e0f1cf18 100644 --- a/src/src.pro +++ b/src/src.pro @@ -3,7 +3,7 @@ LANG_PATH = lang ICONS_PATH = Icons # Set the following variable to 1 to enable debug -DEBUG_MODE = 0 +DEBUG_MODE = 1 # Global TEMPLATE = app