diff --git a/src/rss/rssdownloadrule.cpp b/src/rss/rssdownloadrule.cpp index 5e9dbcfda..b9def9f39 100644 --- a/src/rss/rssdownloadrule.cpp +++ b/src/rss/rssdownloadrule.cpp @@ -82,6 +82,30 @@ RssDownloadRule RssDownloadRule::fromOldFormat(const QHash &r return rule; } +RssDownloadRule RssDownloadRule::fromNewFormat(const QHash &rule_hash) +{ + RssDownloadRule rule; + rule.setName(rule_hash.value("name").toString()); + rule.setMustContain(rule_hash.value("must_contain").toString()); + rule.setMustNotContain(rule_hash.value("must_not_contain").toString()); + rule.setRssFeeds(rule_hash.value("affected_feeds").toStringList()); + rule.setEnabled(rule_hash.value("enabled", false).toBool()); + rule.setLabel(rule_hash.value("label_assigned").toString()); + return rule; +} + +QHash RssDownloadRule::toHash() const +{ + QHash hash; + hash["name"] = m_name; + hash["must_contain"] = m_mustContain.join(" "); + hash["must_not_contain"] = m_mustNotContain.join(" "); + hash["affected_feeds"] = m_rssFeeds; + hash["enabled"] = m_enabled; + hash["label_assigned"] = m_label; + return hash; +} + bool RssDownloadRule::operator==(const RssDownloadRule &other) { return m_name == other.name(); } diff --git a/src/rss/rssdownloadrule.h b/src/rss/rssdownloadrule.h index df0bf2161..2bc987272 100644 --- a/src/rss/rssdownloadrule.h +++ b/src/rss/rssdownloadrule.h @@ -40,6 +40,8 @@ class RssDownloadRule public: explicit RssDownloadRule(); static RssDownloadRule fromOldFormat(const QHash& rule_hash, const QString &feed_url, const QString &rule_name); // Before v2.5.0 + static RssDownloadRule fromNewFormat(const QHash &rule_hash); + QHash toHash() const; bool matches(const QString &article_title) const; void setMustContain(const QString &tokens); void setMustNotContain(const QString &tokens); diff --git a/src/rss/rssdownloadrulelist.cpp b/src/rss/rssdownloadrulelist.cpp index e19a2ae14..699770032 100644 --- a/src/rss/rssdownloadrulelist.cpp +++ b/src/rss/rssdownloadrulelist.cpp @@ -59,12 +59,25 @@ const RssDownloadRule * RssDownloadRuleList::findMatchingRule(const QString &fee return 0; } +void RssDownloadRuleList::saveRulesToStorage() +{ + QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); + qBTRSS.setValue("download_rules", toVariantList()); +} + void RssDownloadRuleList::loadRulesFromStorage() { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); if(qBTRSS.contains("feed_filters")) { importRulesInOldFormat(qBTRSS.value("feed_filters").toHash()); + // Remove outdated rules + qBTRSS.remove("feed_filters"); + // Save to new format + saveRulesToStorage(); + return; } + // Load from new format + loadRulesFromVariantList(qBTRSS.value("download_rules").toList()); } void RssDownloadRuleList::importRulesInOldFormat(const QHash &rules) @@ -72,7 +85,12 @@ void RssDownloadRuleList::importRulesInOldFormat(const QHash foreach(const QString &feed_url, rules.keys()) { const QHash feed_rules = rules.value(feed_url).toHash(); foreach(const QString &rule_name, feed_rules.keys()) { - const RssDownloadRule rule = RssDownloadRule::fromOldFormat(feed_rules.value(rule_name).toHash(), feed_url, rule_name); + RssDownloadRule rule = RssDownloadRule::fromOldFormat(feed_rules.value(rule_name).toHash(), feed_url, rule_name); + // Check for rule name clash + while(contains(rule)) { + rule.setName(rule.name()+"_"); + } + // Add the rule to the list append(rule); } } @@ -82,7 +100,28 @@ void RssDownloadRuleList::append(const RssDownloadRule &rule) { Q_ASSERT(!contains(rule)); QList::append(rule); + // Update feedRules hashtable foreach(const QString &feed_url, rule.rssFeeds()) { m_feedRules[feed_url].append(&last()); } } + +QVariantList RssDownloadRuleList::toVariantList() const +{ + QVariantList l; + QList::const_iterator it; + for(it = begin(); it != end(); it++) { + l << it->toHash(); + } + return l; +} + +void RssDownloadRuleList::loadRulesFromVariantList(const QVariantList &l) +{ + QVariantList::const_iterator it; + for(it=l.begin(); it !=l.end(); it++) { + RssDownloadRule rule = RssDownloadRule::fromNewFormat(it->toHash()); + if(!rule.name().isEmpty()) + append(rule); + } +} diff --git a/src/rss/rssdownloadrulelist.h b/src/rss/rssdownloadrulelist.h index e4f7abf71..f75ef40c8 100644 --- a/src/rss/rssdownloadrulelist.h +++ b/src/rss/rssdownloadrulelist.h @@ -33,6 +33,7 @@ #include #include +#include #include "rssdownloadrule.h" @@ -50,11 +51,14 @@ public: static void drop(); const RssDownloadRule* findMatchingRule(const QString &feed_url, const QString &article_title) const; inline QList feedRules(const QString &feed_url) const { return m_feedRules.value(feed_url); } + QVariantList toVariantList() const; void append(const RssDownloadRule& rule); + void saveRulesToStorage(); private: void loadRulesFromStorage(); void importRulesInOldFormat(const QHash &rules); // Before v2.5.0 + void loadRulesFromVariantList(const QVariantList& l); private: QHash > m_feedRules;