From 8e529fc179a10943f23fc87191316458cebf3a0f Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Mon, 20 Feb 2012 20:49:31 +0200 Subject: [PATCH] RSS code clean up - Also drops support for Qt 4.5 --- INSTALL | 2 +- configure | 8 ++-- qcm/qt4.qcm | 8 ++-- src/rss/rss_imp.cpp | 2 +- src/rss/rssarticle.cpp | 53 ++++++++++-------------- src/rss/rssdownloadrule.cpp | 38 +++++------------- src/rss/rssdownloadrule.h | 3 +- src/rss/rssdownloadrulelist.cpp | 71 +++++++-------------------------- src/rss/rssdownloadrulelist.h | 5 +-- src/rss/rssfeed.cpp | 2 +- src/rss/rssfeed.h | 2 +- src/rss/rssmanager.cpp | 10 +++-- src/rss/rssmanager.h | 4 +- 13 files changed, 70 insertions(+), 138 deletions(-) diff --git a/INSTALL b/INSTALL index 13a40129a..ffafb55d9 100644 --- a/INSTALL +++ b/INSTALL @@ -10,7 +10,7 @@ qBittorrent - A BitTorrent client in C++ / Qt4 will install and execute qBittorrent hopefully without any problems. Dependencies: - - Qt >= 4.5.0 (libqtgui, libqtcore, libqtnetwork, libqtxml, libqtdbus/optional) + - Qt >= 4.6.0 (libqtgui, libqtcore, libqtnetwork, libqtxml, libqtdbus/optional) - pkg-config executable diff --git a/configure b/configure index 64739574c..cb5fe8300 100755 --- a/configure +++ b/configure @@ -325,7 +325,7 @@ cat >$1/modules.cpp <= 4.5 +name: Qt >= 4.6 arg: enable-debug, Enable debug mode arg: disable-gui, Disable qBittorrent Graphical user interface for headless running -----END QCMOD----- @@ -334,8 +334,8 @@ class qc_qt4 : public ConfObj { public: qc_qt4(Conf *c) : ConfObj(c) {} - QString name() const { return "Qt >= 4.5"; } - QString shortname() const { return "Qt 4.5"; } + QString name() const { return "Qt >= 4.6"; } + QString shortname() const { return "Qt 4.6"; } bool exec() { // NOX mode @@ -356,7 +356,7 @@ public: #else conf->addExtra("MANPREFIX = \$\$PREFIX/share"); #endif - return(QT_VERSION >= 0x040500); + return(QT_VERSION >= 0x040600); } }; #line 1 "qt-dbus.qcm" diff --git a/qcm/qt4.qcm b/qcm/qt4.qcm index c718dc882..216aaf0cd 100644 --- a/qcm/qt4.qcm +++ b/qcm/qt4.qcm @@ -1,6 +1,6 @@ /* -----BEGIN QCMOD----- -name: Qt >= 4.5 +name: Qt >= 4.6 arg: enable-debug, Enable debug mode arg: disable-gui, Disable qBittorrent Graphical user interface for headless running -----END QCMOD----- @@ -9,8 +9,8 @@ class qc_qt4 : public ConfObj { public: qc_qt4(Conf *c) : ConfObj(c) {} - QString name() const { return "Qt >= 4.5"; } - QString shortname() const { return "Qt 4.5"; } + QString name() const { return "Qt >= 4.6"; } + QString shortname() const { return "Qt 4.6"; } bool exec() { // NOX mode @@ -31,6 +31,6 @@ public: #else conf->addExtra("MANPREFIX = $$PREFIX/share"); #endif - return(QT_VERSION >= 0x040500); + return(QT_VERSION >= 0x040600); } }; diff --git a/src/rss/rss_imp.cpp b/src/rss/rss_imp.cpp index 5a5d2bebe..b669c52ce 100644 --- a/src/rss/rss_imp.cpp +++ b/src/rss/rss_imp.cpp @@ -465,7 +465,7 @@ void RSSImp::refreshArticleList(QTreeWidgetItem* item) { else if (rss_item) news = rss_item->articleList(); // Sort - RssManager::sortNewsList(news); + RssManager::sortArticleListByDateDesc(news); // Clear the list first textBrowser->clear(); m_currentArticle = 0; diff --git a/src/rss/rssarticle.cpp b/src/rss/rssarticle.cpp index 9091a6483..badef9f95 100644 --- a/src/rss/rssarticle.cpp +++ b/src/rss/rssarticle.cpp @@ -220,39 +220,30 @@ RssArticlePtr xmlToRssArticle(RssFeed* parent, QXmlStreamReader& xml) QDateTime date; QString author; - while(!xml.atEnd()) { - xml.readNext(); + Q_ASSERT(xml.isStartElement() && xml.name() == "item"); - if (xml.isEndElement() && xml.name() == "item") - break; - - if (xml.isStartElement()) { - if (xml.name() == "title") { - title = xml.readElementText(); - } - else if (xml.name() == "enclosure") { - if (xml.attributes().value("type") == "application/x-bittorrent") { - torrentUrl = xml.attributes().value("url").toString(); - } - } - else if (xml.name() == "link") { - link = xml.readElementText(); - if (guid.isEmpty()) - guid = link; - } - else if (xml.name() == "description") { - description = xml.readElementText(); - } - else if (xml.name() == "pubDate") { - date = RssArticle::parseDate(xml.readElementText()); - } - else if (xml.name() == "author") { - author = xml.readElementText(); - } - else if (xml.name() == "guid") { - guid = xml.readElementText(); - } + while (xml.readNextStartElement()) { + if (xml.name() == "title") + title = xml.readElementText(); + else if (xml.name() == "enclosure") { + if (xml.attributes().value("type") == "application/x-bittorrent") + torrentUrl = xml.attributes().value("url").toString(); + } + else if (xml.name() == "link") { + link = xml.readElementText(); + if (guid.isEmpty()) + guid = link; } + else if (xml.name() == "description") + description = xml.readElementText(); + else if (xml.name() == "pubDate") + date = RssArticle::parseDate(xml.readElementText()); + else if (xml.name() == "author") + author = xml.readElementText(); + else if (xml.name() == "guid") + guid = xml.readElementText(); + else + xml.skipCurrentElement(); } if (guid.isEmpty()) diff --git a/src/rss/rssdownloadrule.cpp b/src/rss/rssdownloadrule.cpp index 6ae6e8c89..7b1b621cb 100644 --- a/src/rss/rssdownloadrule.cpp +++ b/src/rss/rssdownloadrule.cpp @@ -44,18 +44,20 @@ RssDownloadRule::RssDownloadRule(): m_enabled(false), m_useRegex(false) bool RssDownloadRule::matches(const QString &article_title) const { foreach (const QString& token, m_mustContain) { - if (token.isEmpty() || token == "") - continue; - QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard); - //reg.setMinimal(false); - if (reg.indexIn(article_title) < 0) return false; + if (!token.isEmpty()) { + QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard); + if (reg.indexIn(article_title) < 0) + return false; + } } qDebug("Checking not matching tokens"); // Checking not matching foreach (const QString& token, m_mustNotContain) { - if (token.isEmpty()) continue; - QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard); - if (reg.indexIn(article_title) > -1) return false; + if (!token.isEmpty()) { + QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard); + if (reg.indexIn(article_title) > -1) + return false; + } } return true; } @@ -76,25 +78,7 @@ void RssDownloadRule::setMustNotContain(const QString &tokens) m_mustNotContain = tokens.split(QRegExp("[\\s|]")); } -RssDownloadRulePtr RssDownloadRule::fromOldFormat(const QVariantHash &rule_hash, const QString &feed_url, const QString &rule_name) -{ - qDebug() << Q_FUNC_INFO << feed_url << rule_name; - RssDownloadRulePtr rule(new RssDownloadRule); - rule->setName(rule_name); - rule->setMustContain(rule_hash.value("matches", "").toString()); - rule->setMustNotContain(rule_hash.value("not", "").toString()); - 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").toHash(); - rule->setEnabled(feeds_w_downloader.value(feed_url, true).toBool()); - // label was unsupported < 2.5.0 - return rule; -} - -RssDownloadRulePtr RssDownloadRule::fromNewFormat(const QVariantHash &rule_hash) +RssDownloadRulePtr RssDownloadRule::fromVariantHash(const QVariantHash &rule_hash) { RssDownloadRulePtr rule(new RssDownloadRule); rule->setName(rule_hash.value("name").toString()); diff --git a/src/rss/rssdownloadrule.h b/src/rss/rssdownloadrule.h index 47c736c93..5df462a22 100644 --- a/src/rss/rssdownloadrule.h +++ b/src/rss/rssdownloadrule.h @@ -46,8 +46,7 @@ class RssDownloadRule public: explicit RssDownloadRule(); - static RssDownloadRulePtr fromOldFormat(const QVariantHash& rule_hash, const QString &feed_url, const QString &rule_name); // Before v2.5.0 - static RssDownloadRulePtr fromNewFormat(const QVariantHash &rule_hash); + static RssDownloadRulePtr fromVariantHash(const QVariantHash &rule_hash); QVariantHash toVariantHash() const; bool matches(const QString &article_title) const; void setMustContain(const QString &tokens); diff --git a/src/rss/rssdownloadrulelist.cpp b/src/rss/rssdownloadrulelist.cpp index 4b0c24d5e..ed5bf9311 100644 --- a/src/rss/rssdownloadrulelist.cpp +++ b/src/rss/rssdownloadrulelist.cpp @@ -61,39 +61,9 @@ void RssDownloadRuleList::saveRulesToStorage() void RssDownloadRuleList::loadRulesFromStorage() { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); - if (qBTRSS.contains("feed_filters")) { - importFeedsInOldFormat(qBTRSS.value("feed_filters").toHash()); - // Remove outdated rules - qBTRSS.remove("feed_filters"); - // Save to new format - saveRulesToStorage(); - return; - } - // Load from new format loadRulesFromVariantHash(qBTRSS.value("download_rules").toHash()); } -void RssDownloadRuleList::importFeedsInOldFormat(const QHash &rules) -{ - foreach (const QString &feed_url, rules.keys()) { - 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()) { - RssDownloadRulePtr rule = RssDownloadRule::fromOldFormat(rules.value(rule_name).toHash(), feed_url, rule_name); - if (!rule) continue; - // Check for rule name clash - while(m_rules.contains(rule->name())) { - rule->setName(rule->name()+"_"); - } - // Add the rule to the list - saveRule(rule); - } -} - QVariantHash RssDownloadRuleList::toVariantHash() const { QVariantHash ret; @@ -105,11 +75,10 @@ QVariantHash RssDownloadRuleList::toVariantHash() const void RssDownloadRuleList::loadRulesFromVariantHash(const QVariantHash &h) { - foreach (const QVariant& v, h.values()) { - RssDownloadRulePtr rule = RssDownloadRule::fromNewFormat(v.toHash()); - if (rule && !rule->name().isEmpty()) { + for (QVariantHash::ConstIterator it = h.begin(); it != h.end(); it++) { + RssDownloadRulePtr rule = RssDownloadRule::fromVariantHash(it.value().toHash()); + if (rule && !rule->name().isEmpty()) saveRule(rule); - } } } @@ -182,30 +151,18 @@ bool RssDownloadRuleList::unserialize(const QString &path) QFile f(path); if (f.open(QIODevice::ReadOnly)) { QDataStream in(&f); - 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; - 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); - } + 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; + } else { + qDebug("Error: could not open file at %s", qPrintable(path)); + return false; } - 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 3a133e889..c3c151df0 100644 --- a/src/rss/rssdownloadrulelist.h +++ b/src/rss/rssdownloadrulelist.h @@ -35,8 +35,7 @@ #include #include #include "rssdownloadrule.h" -s -// This class is not thread-safe (not required) + class RssDownloadRuleList { Q_DISABLE_COPY(RssDownloadRuleList) @@ -56,8 +55,6 @@ public: private: void loadRulesFromStorage(); - 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(); diff --git a/src/rss/rssfeed.cpp b/src/rss/rssfeed.cpp index 707bd5e90..f11d8d055 100644 --- a/src/rss/rssfeed.cpp +++ b/src/rss/rssfeed.cpp @@ -319,7 +319,7 @@ void RssFeed::resizeList() { const uint nb_articles = m_articles.size(); if (nb_articles > max_articles) { RssArticleList listItems = m_articles.values(); - RssManager::sortNewsList(listItems); + RssManager::sortArticleListByDateDesc(listItems); const int excess = nb_articles - max_articles; for (uint i=nb_articles-excess; iguid()); diff --git a/src/rss/rssfeed.h b/src/rss/rssfeed.h index f3badb54a..ea43ba30e 100644 --- a/src/rss/rssfeed.h +++ b/src/rss/rssfeed.h @@ -41,7 +41,7 @@ class RssManager; typedef QHash RssArticleHash; typedef QSharedPointer RssFeedPtr; -typedef RssFeedList RssFeedList; +typedef QList RssFeedList; class RssFeed: public QObject, public RssFile { Q_OBJECT diff --git a/src/rss/rssmanager.cpp b/src/rss/rssmanager.cpp index e454c2142..dbff5b857 100644 --- a/src/rss/rssmanager.cpp +++ b/src/rss/rssmanager.cpp @@ -47,13 +47,17 @@ RssManager::RssManager(): RssManager::~RssManager() { qDebug("Deleting RSSManager..."); - delete m_rssDownloader; delete m_downloadRules; saveItemsToDisk(); saveStreamList(); qDebug("RSSManager deleted"); } +DownloadThread *RssManager::rssDownloader() const +{ + return m_rssDownloader; +} + void RssManager::updateRefreshInterval(uint val) { if (m_refreshInterval != val) { m_refreshInterval = val; @@ -86,7 +90,7 @@ void RssManager::loadStreamList() { // Create feed qDebug() << "Adding feed to parent folder"; RssFeedPtr stream = feed_parent->addStream(this, feed_url); - const QString alias = aliases.at(i); + const QString& alias = aliases[i]; if (!alias.isEmpty()) { stream->rename(alias); } @@ -138,7 +142,7 @@ static bool laterItemDate(const RssArticlePtr& a, const RssArticlePtr& b) return (a->date() > b->date()); } -void RssManager::sortNewsList(RssArticleList& news_list) { +void RssManager::sortArticleListByDateDesc(RssArticleList& news_list) { qSort(news_list.begin(), news_list.end(), laterItemDate); } diff --git a/src/rss/rssmanager.h b/src/rss/rssmanager.h index c296dd7ab..64e2b33ce 100644 --- a/src/rss/rssmanager.h +++ b/src/rss/rssmanager.h @@ -49,8 +49,8 @@ public: RssManager(); virtual ~RssManager(); - inline DownloadThread* rssDownloader() const { return m_rssDownloader; } - static void sortNewsList(RssArticleList& news_list); + DownloadThread* rssDownloader() const; + static void sortArticleListByDateDesc(RssArticleList& news_list); RssDownloadRuleList* downloadRules() const;