From 5a02c568653517e7905e9f339807166fbbb56471 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sat, 13 Nov 2010 15:14:50 +0000 Subject: [PATCH] Fix rss download rule import from qBittorrent < v2.5.0 --- src/rss/automatedrssdownloader.cpp | 9 ++++++- src/rss/rssdownloadrule.cpp | 9 ++++--- src/rss/rssdownloadrulelist.cpp | 40 +++++++++++++++++++----------- src/rss/rssdownloadrulelist.h | 3 ++- 4 files changed, 42 insertions(+), 19 deletions(-) diff --git a/src/rss/automatedrssdownloader.cpp b/src/rss/automatedrssdownloader.cpp index 4cc19f589..c22c8cc63 100644 --- a/src/rss/automatedrssdownloader.cpp +++ b/src/rss/automatedrssdownloader.cpp @@ -88,6 +88,11 @@ void AutomatedRssDownloader::saveSettings() void AutomatedRssDownloader::loadRulesList() { + // Make sure we save the current item before clearing + if(ui->listRules->currentItem()) { + saveCurrentRule(ui->listRules->currentItem()); + } + ui->listRules->clear(); foreach (const QString &rule_name, m_ruleList->ruleNames()) { QListWidgetItem *item = new QListWidgetItem(rule_name, ui->listRules); item->setFlags(item->flags()|Qt::ItemIsUserCheckable); @@ -286,8 +291,10 @@ void AutomatedRssDownloader::on_importBtn_clicked() QString load_path = QFileDialog::getOpenFileName(this, tr("Please point to the RSS download rules file"), QDir::homePath(), tr("Rules list (*.rssrules *.filters)")); if(load_path.isEmpty() || !QFile::exists(load_path)) return; // Load it - if(m_ruleList->unserialize(load_path)) { + if(!m_ruleList->unserialize(load_path)) { QMessageBox::warning(this, tr("Import Error"), tr("Failed to import the selected rules file")); return; } + // Reload the rule list + loadRulesList(); } diff --git a/src/rss/rssdownloadrule.cpp b/src/rss/rssdownloadrule.cpp index 588bd859e..85973df81 100644 --- a/src/rss/rssdownloadrule.cpp +++ b/src/rss/rssdownloadrule.cpp @@ -29,6 +29,7 @@ */ #include +#include #include "rssdownloadrule.h" #include "preferences.h" @@ -69,16 +70,18 @@ void RssDownloadRule::setMustNotContain(const QString &tokens) RssDownloadRule RssDownloadRule::fromOldFormat(const QVariantHash &rule_hash, const QString &feed_url, const QString &rule_name) { + qDebug() << Q_FUNC_INFO << feed_url << rule_name; RssDownloadRule rule; rule.setName(rule_name); rule.setMustContain(rule_hash.value("matches", "").toString()); rule.setMustNotContain(rule_hash.value("not", "").toString()); - rule.setRssFeeds(QStringList() << feed_url); + if(!feed_url.isEmpty()) + rule.setRssFeeds(QStringList() << feed_url); rule.setSavePath(rule_hash.value("save_path", "").toString()); // Is enabled? QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); - const QHash feeds_w_downloader = qBTRSS.value("downloader_on", true).toHash(); - rule.setEnabled(feeds_w_downloader.value(feed_url, false).toBool()); + const QHash feeds_w_downloader = qBTRSS.value("downloader_on").toHash(); + rule.setEnabled(feeds_w_downloader.value(feed_url, true).toBool()); // label was unsupported < 2.5.0 return rule; } diff --git a/src/rss/rssdownloadrulelist.cpp b/src/rss/rssdownloadrulelist.cpp index dbd3991b1..aad7c1556 100644 --- a/src/rss/rssdownloadrulelist.cpp +++ b/src/rss/rssdownloadrulelist.cpp @@ -29,6 +29,7 @@ */ #include +#include #include #include "rssdownloadrulelist.h" @@ -73,7 +74,7 @@ void RssDownloadRuleList::loadRulesFromStorage() { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); if(qBTRSS.contains("feed_filters")) { - importRulesInOldFormat(qBTRSS.value("feed_filters").toHash()); + importFeedsInOldFormat(qBTRSS.value("feed_filters").toHash()); // Remove outdated rules qBTRSS.remove("feed_filters"); // Save to new format @@ -84,20 +85,24 @@ void RssDownloadRuleList::loadRulesFromStorage() loadRulesFromVariantHash(qBTRSS.value("download_rules").toHash()); } -void RssDownloadRuleList::importRulesInOldFormat(const QHash &rules) +void RssDownloadRuleList::importFeedsInOldFormat(const QHash &rules) { foreach(const QString &feed_url, rules.keys()) { - const QHash feed_rules = rules.value(feed_url).toHash(); - foreach(const QString &rule_name, feed_rules.keys()) { - RssDownloadRule rule = RssDownloadRule::fromOldFormat(feed_rules.value(rule_name).toHash(), feed_url, rule_name); - if(!rule.isValid()) continue; - // Check for rule name clash - while(m_rules.contains(rule.name())) { - rule.setName(rule.name()+"_"); - } - // Add the rule to the list - saveRule(rule); + importFeedRulesInOldFormat(feed_url, rules.value(feed_url).toHash()); + } +} + +void RssDownloadRuleList::importFeedRulesInOldFormat(const QString &feed_url, const QHash &rules) +{ + foreach(const QString &rule_name, rules.keys()) { + RssDownloadRule rule = RssDownloadRule::fromOldFormat(rules.value(rule_name).toHash(), feed_url, rule_name); + if(!rule.isValid()) continue; + // Check for rule name clash + while(m_rules.contains(rule.name())) { + rule.setName(rule.name()+"_"); } + // Add the rule to the list + saveRule(rule); } } @@ -192,23 +197,30 @@ bool RssDownloadRuleList::unserialize(const QString &path) QFile f(path); if(f.open(QIODevice::ReadOnly)) { QDataStream in(&f); - if(path.endsWith(".filters")) { + if(path.endsWith(".filters", Qt::CaseInsensitive)) { // Old format (< 2.5.0) + qDebug("Old serialization format detected, processing..."); in.setVersion(QDataStream::Qt_4_3); QVariantHash tmp; in >> tmp; f.close(); if(tmp.isEmpty()) return false; - importRulesInOldFormat(tmp); + qDebug("Processing was successful!"); + // Unfortunately the feed_url is lost + importFeedRulesInOldFormat("", tmp); } else { + qDebug("New serialization format detected, processing..."); in.setVersion(QDataStream::Qt_4_5); QVariantHash tmp; in >> tmp; f.close(); if(tmp.isEmpty()) return false; + qDebug("Processing was successful!"); loadRulesFromVariantHash(tmp); } return true; } + qDebug("Error: could not open file at %s", qPrintable(path)); return false; } + diff --git a/src/rss/rssdownloadrulelist.h b/src/rss/rssdownloadrulelist.h index e28b18a98..e1d2dbc48 100644 --- a/src/rss/rssdownloadrulelist.h +++ b/src/rss/rssdownloadrulelist.h @@ -63,7 +63,8 @@ public: private: void loadRulesFromStorage(); - void importRulesInOldFormat(const QHash &rules); // Before v2.5.0 + void importFeedsInOldFormat(const QHash &feedrules); // Before v2.5.0 + void importFeedRulesInOldFormat(const QString &feed_url, const QHash &rules); // Before v2.5.0 void loadRulesFromVariantHash(const QVariantHash& l); QVariantHash toVariantHash() const; void saveRulesToStorage();