From 9a0a26dae12cf00231ec563e70ccf56cdc93a43e Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sat, 4 May 2013 15:01:24 +0300 Subject: [PATCH 1/2] Store TorrentTempData only in memory. Partially implements #583. --- src/qtlibtorrent/qbtsession.cpp | 3 + src/torrentpersistentdata.h | 113 ++++++++------------------------ 2 files changed, 32 insertions(+), 84 deletions(-) diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index 2249f61a4..ff885a731 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -82,6 +82,9 @@ #include #include "dnsupdater.h" +//initialize static member variables +QHash TorrentTempData::data = QHash(); + using namespace libtorrent; QBtSession* QBtSession::m_instance = 0; diff --git a/src/torrentpersistentdata.h b/src/torrentpersistentdata.h index 23365edf2..c60e897ea 100644 --- a/src/torrentpersistentdata.h +++ b/src/torrentpersistentdata.h @@ -44,129 +44,74 @@ class TorrentTempData { public: - static bool hasTempData(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - return all_data.contains(hash); + static bool hasTempData(QString hash) { + return data.contains(hash); } static void deleteTempData(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - if (all_data.contains(hash)) { - all_data.remove(hash); - settings.setValue("torrents-tmp", all_data); - } + data.remove(hash); } static void setFilesPriority(QString hash, const std::vector &pp) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - QHash data = all_data.value(hash).toHash(); - QStringList pieces_priority; - - std::vector::const_iterator pp_it = pp.begin(); - std::vector::const_iterator pp_itend = pp.end(); - while(pp_it != pp_itend) { - pieces_priority << QString::number(*pp_it); - ++pp_it; - } - data["files_priority"] = pieces_priority; - all_data[hash] = data; - settings.setValue("torrents-tmp", all_data); + data[hash].files_priority = pp; } static void setFilesPath(QString hash, const QStringList &path_list) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - QHash data = all_data.value(hash).toHash(); - data["files_path"] = path_list; - all_data[hash] = data; - settings.setValue("torrents-tmp", all_data); + data[hash].path_list = path_list; } static void setSavePath(QString hash, QString save_path) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - QHash data = all_data.value(hash).toHash(); - data["save_path"] = save_path; - all_data[hash] = data; - settings.setValue("torrents-tmp", all_data); + data[hash].save_path = save_path; } static void setLabel(QString hash, QString label) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - QHash data = all_data.value(hash).toHash(); - qDebug("Saving label %s to tmp data", label.toLocal8Bit().data()); - data["label"] = label; - all_data[hash] = data; - settings.setValue("torrents-tmp", all_data); + data[hash].label = label; } static void setSequential(QString hash, bool sequential) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - QHash data = all_data.value(hash).toHash(); - data["sequential"] = sequential; - all_data[hash] = data; - settings.setValue("torrents-tmp", all_data); + data[hash].sequential = sequential; } static bool isSequential(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - const QHash data = all_data.value(hash).toHash(); - return data.value("sequential", false).toBool(); + return data.value(hash).sequential; } - static void setSeedingMode(QString hash,bool seed) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents-tmp").toHash(); - QHash data = all_data.value(hash).toHash(); - data["seeding"] = seed; - all_data[hash] = data; - settings.setValue("torrents-tmp", all_data); + static void setSeedingMode(QString hash, bool seed) { + data[hash].seed = seed; } static bool isSeedingMode(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - const QHash data = all_data.value(hash).toHash(); - return data.value("seeding", false).toBool(); + return data.value(hash).seed; } static QString getSavePath(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - const QHash data = all_data.value(hash).toHash(); - return data.value("save_path").toString(); + return data.value(hash).save_path; } static QStringList getFilesPath(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - const QHash data = all_data.value(hash).toHash(); - return data.value("files_path").toStringList(); + return data.value(hash).path_list; } static QString getLabel(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - const QHash data = all_data.value(hash).toHash(); - qDebug("Got label %s from tmp data", data.value("label", "").toString().toLocal8Bit().data()); - return data.value("label", "").toString(); + return data.value(hash).label; } static void getFilesPriority(QString hash, std::vector &fp) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents-tmp").toHash(); - const QHash data = all_data.value(hash).toHash(); - const QList list_var = misc::intListfromStringList(data.value("files_priority").toStringList()); - foreach (const int &var, list_var) { - fp.push_back(var); - } + fp = data.value(hash).files_priority; } + +private: + struct TorrentData { + TorrentData(): sequential(false), seed(false) {} + std::vector files_priority; + QStringList path_list; + QString save_path; + QString label; + bool sequential; + bool seed; + }; + + static QHash data; }; class TorrentPersistentData { From 7aa3eff845c647585200d65dfe0ea62e6735f375 Mon Sep 17 00:00:00 2001 From: sledgehammer999 Date: Sat, 4 May 2013 21:29:18 +0300 Subject: [PATCH 2/2] Add constness and value by reference in TorrentTempData. --- src/torrentpersistentdata.h | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/torrentpersistentdata.h b/src/torrentpersistentdata.h index c60e897ea..4d0ed1c0b 100644 --- a/src/torrentpersistentdata.h +++ b/src/torrentpersistentdata.h @@ -44,59 +44,59 @@ class TorrentTempData { public: - static bool hasTempData(QString hash) { + static bool hasTempData(const QString &hash) { return data.contains(hash); } - static void deleteTempData(QString hash) { + static void deleteTempData(const QString &hash) { data.remove(hash); } - static void setFilesPriority(QString hash, const std::vector &pp) { + static void setFilesPriority(const QString &hash, const std::vector &pp) { data[hash].files_priority = pp; } - static void setFilesPath(QString hash, const QStringList &path_list) { + static void setFilesPath(const QString &hash, const QStringList &path_list) { data[hash].path_list = path_list; } - static void setSavePath(QString hash, QString save_path) { + static void setSavePath(const QString &hash, const QString &save_path) { data[hash].save_path = save_path; } - static void setLabel(QString hash, QString label) { + static void setLabel(const QString &hash, const QString &label) { data[hash].label = label; } - static void setSequential(QString hash, bool sequential) { + static void setSequential(const QString &hash, const bool &sequential) { data[hash].sequential = sequential; } - static bool isSequential(QString hash) { + static bool isSequential(const QString &hash) { return data.value(hash).sequential; } - static void setSeedingMode(QString hash, bool seed) { + static void setSeedingMode(const QString &hash, const bool &seed) { data[hash].seed = seed; } - static bool isSeedingMode(QString hash) { + static bool isSeedingMode(const QString &hash) { return data.value(hash).seed; } - static QString getSavePath(QString hash) { + static QString getSavePath(const QString &hash) { return data.value(hash).save_path; } - static QStringList getFilesPath(QString hash) { + static QStringList getFilesPath(const QString &hash) { return data.value(hash).path_list; } - static QString getLabel(QString hash) { + static QString getLabel(const QString &hash) { return data.value(hash).label; } - static void getFilesPriority(QString hash, std::vector &fp) { + static void getFilesPriority(const QString &hash, std::vector &fp) { fp = data.value(hash).files_priority; }