diff --git a/src/mainwindow.cpp b/src/mainwindow.cpp index eb257de13..bb084985f 100755 --- a/src/mainwindow.cpp +++ b/src/mainwindow.cpp @@ -63,7 +63,6 @@ #include "preferences.h" #include "trackerlist.h" #include "peerlistwidget.h" -#include "torrentpersistentdata.h" #include "transferlistfilterswidget.h" #include "propertieswidget.h" #include "statusbar.h" @@ -668,7 +667,7 @@ void MainWindow::balloonClicked() // called when a torrent has finished void MainWindow::finishedTorrent(const QTorrentHandle& h) const { - if (TorrentPersistentData::isSeed(h.hash())) + if (TorrentPersistentData::instance().isSeed(h.hash())) showNotificationBaloon(tr("Download completion"), tr("%1 has finished downloading.", "e.g: xxx.avi has finished downloading.").arg(h.name())); } diff --git a/src/mainwindow.h b/src/mainwindow.h index b18559ba1..f25f75c8b 100644 --- a/src/mainwindow.h +++ b/src/mainwindow.h @@ -36,6 +36,7 @@ #include #include "ui_mainwindow.h" #include "qtorrenthandle.h" +#include "torrentpersistentdata.h" #include "statsdialog.h" class QBtSession; @@ -216,6 +217,7 @@ private: bool has_python; #endif QMenu* toolbarMenu; + TorrentPersistentData persistentData; private slots: void on_actionSearch_engine_triggered(); diff --git a/src/qtlibtorrent/qbtsession.cpp b/src/qtlibtorrent/qbtsession.cpp index febfea9e2..c93110e42 100755 --- a/src/qtlibtorrent/qbtsession.cpp +++ b/src/qtlibtorrent/qbtsession.cpp @@ -227,7 +227,7 @@ void QBtSession::processBigRatios() { if (h.is_seed()) { const QString hash = h.hash(); const qreal ratio = getRealRatio(h.status(torrent_handle::query_accurate_download_counters)); - qreal ratio_limit = TorrentPersistentData::getRatioLimit(hash); + qreal ratio_limit = TorrentPersistentData::instance().getRatioLimit(hash); if (ratio_limit == TorrentPersistentData::USE_GLOBAL_RATIO) ratio_limit = global_ratio_limit; if (ratio_limit == TorrentPersistentData::NO_RATIO_LIMIT) @@ -830,7 +830,7 @@ void QBtSession::deleteTorrent(const QString &hash, bool delete_local_files) { foreach (const QString &file, files) { fsutils::forceRemove(torrentBackup.absoluteFilePath(file)); } - TorrentPersistentData::deletePersistentData(hash); + TorrentPersistentData::instance().deletePersistentData(hash); TorrentTempData::deleteTempData(hash); HiddenData::deleteData(hash); // Remove tracker errors @@ -981,7 +981,7 @@ QTorrentHandle QBtSession::addMagnetUri(QString magnet_uri, bool resumed, bool f } if (savePath.isEmpty()) savePath = getSavePath(hash, false); - if (!defaultTempPath.isEmpty() && !TorrentPersistentData::isSeed(hash)) { + if (!defaultTempPath.isEmpty() && !TorrentPersistentData::instance().isSeed(hash)) { qDebug("addMagnetURI: Temp folder is enabled."); QString torrent_tmp_path = defaultTempPath; p.save_path = fsutils::toNativePath(torrent_tmp_path).toUtf8().constData(); @@ -1175,7 +1175,7 @@ QTorrentHandle QBtSession::addTorrent(QString path, bool fromScanDir, QString fr } else { savePath = getSavePath(hash, fromScanDir, path, imported); } - if (!imported && !defaultTempPath.isEmpty() && !TorrentPersistentData::isSeed(hash)) { + if (!imported && !defaultTempPath.isEmpty() && !TorrentPersistentData::instance().isSeed(hash)) { qDebug("addTorrent::Temp folder is enabled."); QString torrent_tmp_path = defaultTempPath; p.save_path = fsutils::toNativePath(torrent_tmp_path).toUtf8().constData(); @@ -1340,9 +1340,9 @@ void QBtSession::loadTorrentTempData(QTorrentHandle &h, QString savePath, bool m // Save persistent data for new torrent qDebug("Saving torrent persistant data"); if (defaultTempPath.isEmpty()) - TorrentPersistentData::saveTorrentPersistentData(h, QString::null, magnet); + TorrentPersistentData::instance().saveTorrentPersistentData(h, QString::null, magnet); else - TorrentPersistentData::saveTorrentPersistentData(h, fsutils::fromNativePath(savePath), magnet); + TorrentPersistentData::instance().saveTorrentPersistentData(h, fsutils::fromNativePath(savePath), magnet); } void QBtSession::mergeTorrents(QTorrentHandle& h_ex, const QString& magnet_uri) @@ -1661,7 +1661,7 @@ void QBtSession::saveFastResumeData() { continue; try { if (isQueueingEnabled()) - TorrentPersistentData::savePriority(h); + TorrentPersistentData::instance().savePriority(h); if (!h.has_metadata()) continue; // Actually with should save fast resume data for paused files too @@ -1850,7 +1850,7 @@ void QBtSession::appendqBextensionToTorrent(const QTorrentHandle &h, bool append void QBtSession::changeLabelInTorrentSavePath(const QTorrentHandle &h, QString old_label, QString new_label) { if (!h.is_valid()) return; if (!appendLabelToSavePath) return; - QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::getSavePath(h.hash())); + QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(h.hash())); if (!old_save_path.startsWith(defaultSavePath)) return; QString new_save_path = fsutils::updateLabelInSavePath(defaultSavePath, old_save_path, old_label, new_label); if (new_save_path != old_save_path) { @@ -1863,10 +1863,10 @@ void QBtSession::changeLabelInTorrentSavePath(const QTorrentHandle &h, QString o void QBtSession::appendLabelToTorrentSavePath(const QTorrentHandle& h) { if (!h.is_valid()) return; - const QString label = TorrentPersistentData::getLabel(h.hash()); + const QString label = TorrentPersistentData::instance().getLabel(h.hash()); if (label.isEmpty()) return; // Current save path - QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::getSavePath(h.hash())); + QString old_save_path = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(h.hash())); QString new_save_path = fsutils::updateLabelInSavePath(defaultSavePath, old_save_path, "", label); if (old_save_path != new_save_path) { // Move storage @@ -1992,20 +1992,20 @@ void QBtSession::setMaxRatioPerTorrent(const QString &hash, qreal ratio) ratio = MAX_RATIO; qDebug("* Set individual max ratio for torrent %s to %.1f.", qPrintable(hash), ratio); - TorrentPersistentData::setRatioLimit(hash, ratio); + TorrentPersistentData::instance().setRatioLimit(hash, ratio); updateRatioTimer(); } void QBtSession::removeRatioPerTorrent(const QString &hash) { qDebug("* Remove individual max ratio for torrent %s.", qPrintable(hash)); - TorrentPersistentData::setRatioLimit(hash, TorrentPersistentData::USE_GLOBAL_RATIO); + TorrentPersistentData::instance().setRatioLimit(hash, TorrentPersistentData::USE_GLOBAL_RATIO); updateRatioTimer(); } qreal QBtSession::getMaxRatioPerTorrent(const QString &hash, bool *usesGlobalRatio) const { - qreal ratio_limit = TorrentPersistentData::getRatioLimit(hash); + qreal ratio_limit = TorrentPersistentData::instance().getRatioLimit(hash); if (ratio_limit == TorrentPersistentData::USE_GLOBAL_RATIO) { ratio_limit = global_ratio_limit; if (usesGlobalRatio) @@ -2019,7 +2019,7 @@ qreal QBtSession::getMaxRatioPerTorrent(const QString &hash, bool *usesGlobalRat void QBtSession::updateRatioTimer() { - if (global_ratio_limit == -1 && !TorrentPersistentData::hasPerTorrentRatioLimit()) { + if (global_ratio_limit == -1 && !TorrentPersistentData::instance().hasPerTorrentRatioLimit()) { if (BigRatioTimer->isActive()) BigRatioTimer->stop(); } else if (!BigRatioTimer->isActive()) { @@ -2099,7 +2099,7 @@ void QBtSession::sendNotificationEmail(const QTorrentHandle &h) { // Prepare mail content QString content = tr("Torrent name: %1").arg(h.name()) + "\n"; content += tr("Torrent size: %1").arg(misc::friendlyUnit(status.total_wanted)) + "\n"; - content += tr("Save path: %1").arg(TorrentPersistentData::getSavePath(h.hash())) + "\n\n"; + content += tr("Save path: %1").arg(TorrentPersistentData::instance().getSavePath(h.hash())) + "\n\n"; content += tr("The torrent was downloaded in %1.", "The torrent was downloaded in 1 hour and 20 seconds").arg(misc::userFriendlyDuration(status.active_time)) + "\n\n\n"; content += tr("Thank you for using qBittorrent.") + "\n"; // Send the notification email @@ -2213,7 +2213,7 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* if (appendqBExtension) appendqBextensionToTorrent(h, false); - const bool was_already_seeded = TorrentPersistentData::isSeed(hash); + const bool was_already_seeded = TorrentPersistentData::instance().isSeed(hash); qDebug("Was already seeded: %d", was_already_seeded); if (!was_already_seeded) { h.save_resume_data(); @@ -2256,7 +2256,7 @@ void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* } // Remember finished state qDebug("Saving seed status"); - TorrentPersistentData::saveSeedStatus(h); + TorrentPersistentData::instance().saveSeedStatus(h); // Recheck if the user asked to Preferences* const pref = Preferences::instance(); if (pref->recheckTorrentsOnCompletion()) { @@ -2418,7 +2418,7 @@ void QBtSession::handleStorageMovedAlert(libtorrent::storage_moved_alert* p) { } if (defaultTempPath.isEmpty() || !new_save_path.startsWith(defaultTempPath)) { qDebug("Storage has been moved, updating save path to %s", qPrintable(new_save_path)); - TorrentPersistentData::saveSavePath(h.hash(), new_save_path); + TorrentPersistentData::instance().saveSavePath(h.hash(), new_save_path); } emit savePathChanged(h); //h.force_recheck(); @@ -2657,11 +2657,11 @@ void QBtSession::handleFastResumeRejectedAlert(libtorrent::fastresume_rejected_a QTorrentHandle h(p->handle); if (h.is_valid()) { qDebug("/!\\ Fast resume failed for %s, reason: %s", qPrintable(h.name()), p->message().c_str()); - if (p->error.value() == 134 && TorrentPersistentData::isSeed(h.hash()) && h.has_missing_files()) { + if (p->error.value() == 134 && TorrentPersistentData::instance().isSeed(h.hash()) && h.has_missing_files()) { const QString hash = h.hash(); // Mismatching file size (files were probably moved logger->addMessage(tr("File sizes mismatch for torrent %1, pausing it.").arg(h.name())); - TorrentPersistentData::setErrorState(hash, true); + TorrentPersistentData::instance().setErrorState(hash, true); pauseTorrent(hash); } else { logger->addMessage(tr("Fast resume data was rejected for torrent %1, checking again...").arg(h.name()), Log::CRITICAL); @@ -2723,7 +2723,7 @@ void QBtSession::handleTorrentCheckedAlert(libtorrent::torrent_checked_alert* p) const QString hash = h.hash(); qDebug("%s have just finished checking", qPrintable(hash)); // Save seed status - TorrentPersistentData::saveSeedStatus(h); + TorrentPersistentData::instance().saveSeedStatus(h); // Move to temp directory if necessary if (!h.is_seed() && !defaultTempPath.isEmpty()) { // Check if directory is different @@ -2846,7 +2846,7 @@ QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString f } qDebug("getSavePath, got save_path from temp data: %s", qPrintable(savePath)); } else { - savePath = fsutils::fromNativePath(TorrentPersistentData::getSavePath(hash)); + savePath = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(hash)); qDebug("SavePath got from persistant data is %s", qPrintable(savePath)); if (savePath.isEmpty()) { if (fromScanDir && m_scanFolders->downloadInTorrentFolder(filePath)) { @@ -2856,7 +2856,7 @@ QString QBtSession::getSavePath(const QString &hash, bool fromScanDir, QString f } } if (!fromScanDir && appendLabelToSavePath) { - const QString label = TorrentPersistentData::getLabel(hash); + const QString label = TorrentPersistentData::instance().getLabel(hash); if (!label.isEmpty()) { qDebug("Torrent label is %s", qPrintable(label)); savePath = fsutils::updateLabelInSavePath(defaultSavePath, savePath, "", label); @@ -3011,7 +3011,7 @@ qreal QBtSession::getPayloadUploadRate() const { void QBtSession::startUpTorrents() { qDebug("Resuming unfinished torrents"); const QDir torrentBackup(fsutils::BTBackupLocation()); - const QStringList known_torrents = TorrentPersistentData::knownTorrents(); + const QStringList known_torrents = TorrentPersistentData::instance().knownTorrents(); // Safety measure because some people reported torrent loss since // we switch the v1.5 way of resuming torrents on startup @@ -3032,7 +3032,7 @@ void QBtSession::startUpTorrents() { if (isQueueingEnabled()) { priority_queue, vector >, std::greater > > torrent_queue; foreach (const QString &hash, known_torrents) { - const int prio = TorrentPersistentData::getPriority(hash); + const int prio = TorrentPersistentData::instance().getPriority(hash); torrent_queue.push(qMakePair(prio, hash)); } qDebug("Priority_queue size: %ld", (long)torrent_queue.size()); @@ -3041,8 +3041,8 @@ void QBtSession::startUpTorrents() { const QString hash = torrent_queue.top().second; torrent_queue.pop(); qDebug("Starting up torrent %s", qPrintable(hash)); - if (TorrentPersistentData::isMagnet(hash)) { - addMagnetUri(TorrentPersistentData::getMagnetUri(hash), true); + if (TorrentPersistentData::instance().isMagnet(hash)) { + addMagnetUri(TorrentPersistentData::instance().getMagnetUri(hash), true); } else { addTorrent(torrentBackup.path()+"/"+hash+".torrent", false, QString(), true); } @@ -3051,8 +3051,8 @@ void QBtSession::startUpTorrents() { // Resume downloads foreach (const QString &hash, known_torrents) { qDebug("Starting up torrent %s", qPrintable(hash)); - if (TorrentPersistentData::isMagnet(hash)) - addMagnetUri(TorrentPersistentData::getMagnetUri(hash), true); + if (TorrentPersistentData::instance().isMagnet(hash)) + addMagnetUri(TorrentPersistentData::instance().getMagnetUri(hash), true); else addTorrent(torrentBackup.path()+"/"+hash+".torrent", false, QString(), true); } @@ -3106,7 +3106,7 @@ void QBtSession::handleIPFilterError() } void QBtSession::recoverPersistentData(const QString &hash, const std::vector &buf) { - if (TorrentPersistentData::isKnownTorrent(hash) || TorrentTempData::hasTempData(hash) || buf.empty()) + if (TorrentPersistentData::instance().isKnownTorrent(hash) || TorrentTempData::hasTempData(hash) || buf.empty()) return; libtorrent::lazy_entry fast; @@ -3123,20 +3123,20 @@ void QBtSession::recoverPersistentData(const QString &hash, const std::vector data) { - (*data)["qBt-savePath"] = fsutils::fromNativePath(TorrentPersistentData::getSavePath(hash)).toUtf8().constData(); - (*data)["qBt-ratioLimit"] = QString::number(TorrentPersistentData::getRatioLimit(hash)).toUtf8().constData(); - (*data)["qBt-label"] = TorrentPersistentData::getLabel(hash).toUtf8().constData(); - (*data)["qBt-queuePosition"] = TorrentPersistentData::getPriority(hash); - (*data)["qBt-seedStatus"] = (int)TorrentPersistentData::isSeed(hash); + (*data)["qBt-savePath"] = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(hash)).toUtf8().constData(); + (*data)["qBt-ratioLimit"] = QString::number(TorrentPersistentData::instance().getRatioLimit(hash)).toUtf8().constData(); + (*data)["qBt-label"] = TorrentPersistentData::instance().getLabel(hash).toUtf8().constData(); + (*data)["qBt-queuePosition"] = TorrentPersistentData::instance().getPriority(hash); + (*data)["qBt-seedStatus"] = (int)TorrentPersistentData::instance().isSeed(hash); } void QBtSession::unhideMagnet(const QString &hash) { @@ -3191,7 +3191,7 @@ void QBtSession::unhideMagnet(const QString &hash) { } h.queue_position_bottom(); - loadTorrentTempData(h, h.save_path(), !h.has_metadata()); //TempData are deleted by a call to TorrentPersistentData::saveTorrentPersistentData() + loadTorrentTempData(h, h.save_path(), !h.has_metadata()); //TempData are deleted by a call to TorrentPersistentData::instance().saveTorrentPersistentData() if (!add_paused) h.resume(); h.move_storage(save_path); diff --git a/src/qtlibtorrent/qtorrenthandle.cpp b/src/qtlibtorrent/qtorrenthandle.cpp index 4c06b68f7..ee221cb3b 100644 --- a/src/qtlibtorrent/qtorrenthandle.cpp +++ b/src/qtlibtorrent/qtorrenthandle.cpp @@ -86,7 +86,7 @@ QString QTorrentHandle::hash() const QString QTorrentHandle::name() const { - QString name = TorrentPersistentData::getName(hash()); + QString name = TorrentPersistentData::instance().getName(hash()); if (name.isEmpty()) { #if LIBTORRENT_VERSION_NUM < 10000 name = misc::toQStringU(torrent_handle::name()); @@ -201,7 +201,7 @@ QString QTorrentHandle::save_path_parsed() const p = firstFileSavePath(); } else { - p = fsutils::fromNativePath(TorrentPersistentData::getSavePath(hash())); + p = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(hash())); if (p.isEmpty()) p = save_path(); } @@ -386,7 +386,7 @@ bool QTorrentHandle::priv() const QString QTorrentHandle::firstFileSavePath() const { Q_ASSERT(has_metadata()); - QString fsave_path = fsutils::fromNativePath(TorrentPersistentData::getSavePath(hash())); + QString fsave_path = fsutils::fromNativePath(TorrentPersistentData::instance().getSavePath(hash())); if (fsave_path.isEmpty()) fsave_path = save_path(); if (!fsave_path.endsWith("/")) @@ -520,13 +520,13 @@ void QTorrentHandle::resume() const torrent_handle::clear_error(); const QString torrent_hash = hash(); - bool has_persistant_error = TorrentPersistentData::hasError(torrent_hash); - TorrentPersistentData::setErrorState(torrent_hash, false); + bool has_persistant_error = TorrentPersistentData::instance().hasError(torrent_hash); + TorrentPersistentData::instance().setErrorState(torrent_hash, false); bool temp_path_enabled = Preferences::instance()->isTempPathEnabled(); if (has_persistant_error && temp_path_enabled) { // Torrent was supposed to be seeding, checking again in final destination qDebug("Resuming a torrent with error..."); - const QString final_save_path = TorrentPersistentData::getSavePath(torrent_hash); + const QString final_save_path = TorrentPersistentData::instance().getSavePath(torrent_hash); qDebug("Torrent final path is: %s", qPrintable(final_save_path)); if (!final_save_path.isEmpty()) move_storage(final_save_path); @@ -691,7 +691,7 @@ void QTorrentHandle::prioritize_files(const vector &files) const if (was_seed && !is_seed()) { qDebug() << "Torrent is no longer SEEDING"; // Save seed status - TorrentPersistentData::saveSeedStatus(*this); + TorrentPersistentData::instance().saveSeedStatus(*this); // Move to temp folder if necessary const Preferences* const pref = Preferences::instance(); if (pref->isTempPathEnabled()) { diff --git a/src/qtlibtorrent/torrentmodel.cpp b/src/qtlibtorrent/torrentmodel.cpp index 4356cddcd..2d7093fc3 100644 --- a/src/qtlibtorrent/torrentmodel.cpp +++ b/src/qtlibtorrent/torrentmodel.cpp @@ -84,9 +84,9 @@ QIcon get_error_icon() { TorrentModelItem::TorrentModelItem(const QTorrentHandle &h) : m_torrent(h) , m_lastStatus(h.status(torrent_handle::query_accurate_download_counters)) - , m_addedTime(TorrentPersistentData::getAddedDate(h.hash())) - , m_label(TorrentPersistentData::getLabel(h.hash())) - , m_name(TorrentPersistentData::getName(h.hash())) + , m_addedTime(TorrentPersistentData::instance().getAddedDate(h.hash())) + , m_label(TorrentPersistentData::instance().getLabel(h.hash())) + , m_name(TorrentPersistentData::instance().getName(h.hash())) , m_hash(h.hash()) { if (m_name.isEmpty()) @@ -202,14 +202,14 @@ bool TorrentModelItem::setData(int column, const QVariant &value, int role) switch(column) { case TR_NAME: m_name = value.toString(); - TorrentPersistentData::saveName(m_torrent.hash(), m_name); + TorrentPersistentData::instance().saveName(m_torrent.hash(), m_name); return true; case TR_LABEL: { QString new_label = value.toString(); if (m_label != new_label) { QString old_label = m_label; m_label = new_label; - TorrentPersistentData::saveLabel(m_torrent.hash(), new_label); + TorrentPersistentData::instance().saveLabel(m_torrent.hash(), new_label); emit labelChanged(old_label, new_label); } return true; diff --git a/src/torrentpersistentdata.cpp b/src/torrentpersistentdata.cpp index 12cd30c9d..0d0065376 100644 --- a/src/torrentpersistentdata.cpp +++ b/src/torrentpersistentdata.cpp @@ -44,6 +44,7 @@ QHash TorrentTempData::data = QHash TorrentTempData::torrentMoveStates = QHash(); QHash HiddenData::data = QHash(); unsigned int HiddenData::metadata_counter = 0; +TorrentPersistentData* TorrentPersistentData::m_instance = 0; bool TorrentTempData::hasTempData(const QString &hash) { return data.contains(hash); @@ -197,37 +198,51 @@ void HiddenData::gotMetadata(const QString &hash) { metadata_counter++; } +TorrentPersistentData::TorrentPersistentData() + : all_data(QIniSettings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")).value("torrents").toHash()) + , dirty(false) +{ + connect(&timer, SIGNAL(timeout()), SLOT(saveImpl())); + m_instance = this; +} + +TorrentPersistentData::~TorrentPersistentData() { + save(); + m_instance = 0; +} + +void TorrentPersistentData::save() { + if (dirty) + saveImpl(); +} + +TorrentPersistentData& TorrentPersistentData::instance() +{ + Q_ASSERT(m_instance); + return *m_instance; +} + bool TorrentPersistentData::isKnownTorrent(QString hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); return all_data.contains(hash); } QStringList TorrentPersistentData::knownTorrents() { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); return all_data.keys(); } void TorrentPersistentData::setRatioLimit(const QString &hash, const qreal &ratio) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["max_ratio"] = ratio; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } qreal TorrentPersistentData::getRatioLimit(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("max_ratio", USE_GLOBAL_RATIO).toReal(); } bool TorrentPersistentData::hasPerTorrentRatioLimit() { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); QHash::ConstIterator it = all_data.constBegin(); QHash::ConstIterator itend = all_data.constEnd(); for ( ; it != itend; ++it) { @@ -239,19 +254,15 @@ bool TorrentPersistentData::hasPerTorrentRatioLimit() { } void TorrentPersistentData::setAddedDate(const QString &hash, const QDateTime &time) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); if (!data.contains("add_date")) { data["add_date"] = time; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } } QDateTime TorrentPersistentData::getAddedDate(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); QDateTime dt = data.value("add_date").toDateTime(); if (!dt.isValid()) { @@ -262,34 +273,26 @@ QDateTime TorrentPersistentData::getAddedDate(const QString &hash) { } void TorrentPersistentData::setErrorState(const QString &hash, const bool has_error) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["has_error"] = has_error; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } bool TorrentPersistentData::hasError(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("has_error", false).toBool(); } QDateTime TorrentPersistentData::getSeedDate(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("seed_date").toDateTime(); } void TorrentPersistentData::deletePersistentData(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); if (all_data.contains(hash)) { all_data.remove(hash); - settings.setValue("torrents", all_data); + markDirty(); } } @@ -297,8 +300,6 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c Q_ASSERT(h.is_valid()); qDebug("Saving persistent data for %s", qPrintable(h.hash())); // Save persistent data - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(h.hash()).toHash(); data["is_magnet"] = is_magnet; if (is_magnet) { @@ -317,7 +318,7 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c data["label"] = TorrentTempData::getLabel(h.hash()); // Save data all_data[h.hash()] = data; - settings.setValue("torrents", all_data); + markDirty(); qDebug("TorrentPersistentData: Saving save_path %s, hash: %s", qPrintable(h.save_path()), qPrintable(h.hash())); // Set Added date setAddedDate(h.hash(), QDateTime::currentDateTime()); @@ -328,121 +329,109 @@ void TorrentPersistentData::saveTorrentPersistentData(const QTorrentHandle &h, c void TorrentPersistentData::saveSavePath(const QString &hash, const QString &save_path) { Q_ASSERT(!hash.isEmpty()); qDebug("TorrentPersistentData::saveSavePath(%s)", qPrintable(save_path)); - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["save_path"] = save_path; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); qDebug("TorrentPersistentData: Saving save_path: %s, hash: %s", qPrintable(save_path), qPrintable(hash)); } void TorrentPersistentData::saveLabel(const QString &hash, const QString &label) { Q_ASSERT(!hash.isEmpty()); - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["label"] = label; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } void TorrentPersistentData::saveName(const QString &hash, const QString &name) { Q_ASSERT(!hash.isEmpty()); - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data.value(hash).toHash(); data["name"] = name; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } void TorrentPersistentData::savePriority(const QTorrentHandle &h) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[h.hash()].toHash(); data["priority"] = h.queue_position(); all_data[h.hash()] = data; - settings.setValue("torrents", all_data); + markDirty(); } void TorrentPersistentData::savePriority(const QString &hash, const int &queue_pos) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[hash].toHash(); data["priority"] = queue_pos; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } void TorrentPersistentData::saveSeedStatus(const QTorrentHandle &h) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[h.hash()].toHash(); bool was_seed = data.value("seed", false).toBool(); if (was_seed != h.is_seed()) { data["seed"] = !was_seed; all_data[h.hash()] = data; - settings.setValue("torrents", all_data); + markDirty(); } } void TorrentPersistentData::saveSeedStatus(const QString &hash, const bool seedStatus) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - QHash all_data = settings.value("torrents").toHash(); QHash data = all_data[hash].toHash(); data["seed"] = seedStatus; all_data[hash] = data; - settings.setValue("torrents", all_data); + markDirty(); } QString TorrentPersistentData::getSavePath(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); //qDebug("TorrentPersistentData: getSavePath %s", data["save_path"].toString().toLocal8Bit().data()); return data.value("save_path").toString(); } QString TorrentPersistentData::getLabel(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("label", "").toString(); } QString TorrentPersistentData::getName(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("name", "").toString(); } int TorrentPersistentData::getPriority(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("priority", -1).toInt(); } bool TorrentPersistentData::isSeed(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("seed", false).toBool(); } bool TorrentPersistentData::isMagnet(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); return data.value("is_magnet", false).toBool(); } QString TorrentPersistentData::getMagnetUri(const QString &hash) { - QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); - const QHash all_data = settings.value("torrents").toHash(); const QHash data = all_data.value(hash).toHash(); Q_ASSERT(data.value("is_magnet", false).toBool()); return data.value("magnet_uri").toString(); } + +void TorrentPersistentData::markDirty() { + if (!dirty) { + dirty = true; + timer.start(1000); + } +} + +void TorrentPersistentData::saveImpl() { + Q_ASSERT(dirty); + + QIniSettings settings(QString::fromUtf8("qBittorrent"), QString::fromUtf8("qBittorrent-resume")); + settings.setValue("torrents", all_data); + dirty = false; + timer.stop(); +} diff --git a/src/torrentpersistentdata.h b/src/torrentpersistentdata.h index c46eb11bf..a7add82ec 100644 --- a/src/torrentpersistentdata.h +++ b/src/torrentpersistentdata.h @@ -32,7 +32,9 @@ #define TORRENTPERSISTENTDATA_H #include +#include #include +#include #include #include "preferences.h" @@ -112,7 +114,9 @@ private: static unsigned int metadata_counter; }; -class TorrentPersistentData { +class TorrentPersistentData : QObject { + Q_OBJECT + Q_DISABLE_COPY(TorrentPersistentData) // This class stores strings w/o modifying separators public: enum RatioLimit { @@ -121,36 +125,55 @@ public: }; public: - static bool isKnownTorrent(QString hash); - static QStringList knownTorrents(); - static void setRatioLimit(const QString &hash, const qreal &ratio); - static qreal getRatioLimit(const QString &hash); - static bool hasPerTorrentRatioLimit() ; - static void setAddedDate(const QString &hash, const QDateTime &time); - static QDateTime getAddedDate(const QString &hash); - static void setErrorState(const QString &hash, const bool has_error); - static bool hasError(const QString &hash); - static QDateTime getSeedDate(const QString &hash); - static void deletePersistentData(const QString &hash); - static void saveTorrentPersistentData(const QTorrentHandle &h, const QString &save_path = QString::null, const bool is_magnet = false); + TorrentPersistentData(); + ~TorrentPersistentData(); + void save(); + +public: + static TorrentPersistentData& instance(); + + bool isKnownTorrent(QString hash); + QStringList knownTorrents(); + void setRatioLimit(const QString &hash, const qreal &ratio); + qreal getRatioLimit(const QString &hash); + bool hasPerTorrentRatioLimit() ; + void setAddedDate(const QString &hash, const QDateTime &time); + QDateTime getAddedDate(const QString &hash); + void setErrorState(const QString &hash, const bool has_error); + bool hasError(const QString &hash); + QDateTime getSeedDate(const QString &hash); + void deletePersistentData(const QString &hash); + void saveTorrentPersistentData(const QTorrentHandle &h, const QString &save_path = QString::null, const bool is_magnet = false); // Setters - static void saveSavePath(const QString &hash, const QString &save_path); - static void saveLabel(const QString &hash, const QString &label); - static void saveName(const QString &hash, const QString &name); - static void savePriority(const QTorrentHandle &h); - static void savePriority(const QString &hash, const int &queue_pos); - static void saveSeedStatus(const QTorrentHandle &h); - static void saveSeedStatus(const QString &hash, const bool seedStatus); + void saveSavePath(const QString &hash, const QString &save_path); + void saveLabel(const QString &hash, const QString &label); + void saveName(const QString &hash, const QString &name); + void savePriority(const QTorrentHandle &h); + void savePriority(const QString &hash, const int &queue_pos); + void saveSeedStatus(const QTorrentHandle &h); + void saveSeedStatus(const QString &hash, const bool seedStatus); // Getters - static QString getSavePath(const QString &hash); - static QString getLabel(const QString &hash); - static QString getName(const QString &hash); - static int getPriority(const QString &hash); - static bool isSeed(const QString &hash); - static bool isMagnet(const QString &hash); - static QString getMagnetUri(const QString &hash); + QString getSavePath(const QString &hash); + QString getLabel(const QString &hash); + QString getName(const QString &hash); + int getPriority(const QString &hash); + bool isSeed(const QString &hash); + bool isMagnet(const QString &hash); + QString getMagnetUri(const QString &hash); + +private: + void markDirty(); + +private slots: + void saveImpl(); + +private: + QHash all_data; + bool dirty; + QTimer timer; + static TorrentPersistentData* m_instance; }; #endif // TORRENTPERSISTENTDATA_H diff --git a/src/transferlistwidget.cpp b/src/transferlistwidget.cpp index 071109158..1491d343d 100644 --- a/src/transferlistwidget.cpp +++ b/src/transferlistwidget.cpp @@ -251,7 +251,7 @@ void TransferListWidget::setSelectedTorrentsLocation() const QStringList hashes = getSelectedTorrentsHashes(); if (hashes.isEmpty()) return; QString dir; - const QDir saveDir(TorrentPersistentData::getSavePath(hashes.first())); + const QDir saveDir(TorrentPersistentData::instance().getSavePath(hashes.first())); qDebug("Old save path is %s", qPrintable(saveDir.absolutePath())); dir = QFileDialog::getExistingDirectory(this, tr("Choose save path"), saveDir.absolutePath(), QFileDialog::DontConfirmOverwrite | QFileDialog::ShowDirsOnly | QFileDialog::HideNameFilterDetails); @@ -268,7 +268,7 @@ void TransferListWidget::setSelectedTorrentsLocation() h.move_storage(savePath.absolutePath()); } else { - TorrentPersistentData::saveSavePath(h.hash(), savePath.absolutePath()); + TorrentPersistentData::instance().saveSavePath(h.hash(), savePath.absolutePath()); main_window->getProperties()->updateSavePath(h); } } diff --git a/src/webui/btjson.cpp b/src/webui/btjson.cpp index 2e21eca3f..e37f7eabf 100644 --- a/src/webui/btjson.cpp +++ b/src/webui/btjson.cpp @@ -415,7 +415,7 @@ QByteArray btjson::getPropertiesForTorrent(const QString& hash) return QByteArray(); // Save path - QString save_path = fsutils::toNativePath(TorrentPersistentData::getSavePath(hash)); + QString save_path = fsutils::toNativePath(TorrentPersistentData::instance().getSavePath(hash)); if (save_path.isEmpty()) save_path = fsutils::toNativePath(h.save_path()); data[KEY_PROP_SAVE_PATH] = save_path; @@ -554,7 +554,7 @@ QVariantMap toMap(const QTorrentHandle& h) ret[KEY_TORRENT_SEQUENTIAL_DOWNLOAD] = status.sequential_download; if (h.has_metadata()) ret[KEY_TORRENT_FIRST_LAST_PIECE_PRIO] = h.first_last_piece_first(); - ret[KEY_TORRENT_LABEL] = TorrentPersistentData::getLabel(h.hash()); + ret[KEY_TORRENT_LABEL] = TorrentPersistentData::instance().getLabel(h.hash()); return ret; } diff --git a/src/webui/qtorrentfilter.cpp b/src/webui/qtorrentfilter.cpp index 9797f5ef9..328d131ad 100644 --- a/src/webui/qtorrentfilter.cpp +++ b/src/webui/qtorrentfilter.cpp @@ -116,5 +116,5 @@ bool QTorrentFilter::torrentHasLabel(const QTorrentHandle &h) const if (label_.isNull()) return true; else - return TorrentPersistentData::getLabel(h.hash()) == label_; + return TorrentPersistentData::instance().getLabel(h.hash()) == label_; }