From d7c36c9c3c312659166b59d690d9fa3178efbbc8 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 19 Aug 2012 17:22:30 +0300 Subject: [PATCH] RSS code clean up --- src/rss/rssarticle.cpp | 19 ++++--- src/rss/rssarticle.h | 18 +++--- src/rss/rssfeed.cpp | 126 +++++++++++++++++++++++++---------------- src/rss/rssfeed.h | 3 +- src/rss/rssmanager.cpp | 6 +- src/rss/rssmanager.h | 1 - 6 files changed, 100 insertions(+), 73 deletions(-) diff --git a/src/rss/rssarticle.cpp b/src/rss/rssarticle.cpp index bc6e6399f..7868b5f78 100644 --- a/src/rss/rssarticle.cpp +++ b/src/rss/rssarticle.cpp @@ -36,7 +36,7 @@ #include "rssfeed.h" // public constructor -RssArticle::RssArticle(RssFeed* parent, const QString &guid): +RssArticle::RssArticle(RssFeed* parent, const QString& guid): m_parent(parent), m_guid(guid), m_read(false) {} bool RssArticle::hasAttachment() const { @@ -56,9 +56,10 @@ QVariantHash RssArticle::toHash() const { return item; } -RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash &h) { +RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash& h) { const QString guid = h.value("id").toString(); - if (guid.isEmpty()) return RssArticlePtr(); + if (guid.isEmpty()) + return RssArticlePtr(); RssArticlePtr art(new RssArticle(parent, guid)); art->m_title = h.value("title", "").toString(); @@ -76,25 +77,25 @@ RssFeed* RssArticle::parent() const { return m_parent; } -QString RssArticle::author() const { +const QString& RssArticle::author() const { return m_author; } -QString RssArticle::torrentUrl() const { +const QString& RssArticle::torrentUrl() const { return m_torrentUrl.isEmpty() ? m_link : m_torrentUrl; } -QString RssArticle::link() const { +const QString& RssArticle::link() const { return m_link; } -QString RssArticle::description() const { +const QString& RssArticle::description() const { if (m_description.isNull()) return ""; return m_description; } -QDateTime RssArticle::date() const { +const QDateTime& RssArticle::date() const { return m_date; } @@ -115,7 +116,7 @@ const QString& RssArticle::guid() const return m_guid; } -QString RssArticle::title() const +const QString& RssArticle::title() const { return m_title; } diff --git a/src/rss/rssarticle.h b/src/rss/rssarticle.h index 51a7e3bc6..7b7086816 100644 --- a/src/rss/rssarticle.h +++ b/src/rss/rssarticle.h @@ -45,24 +45,24 @@ typedef QSharedPointer RssArticlePtr; class RssArticle { public: - RssArticle(RssFeed* parent, const QString &guid); + RssArticle(RssFeed* parent, const QString& guid); // Accessors bool hasAttachment() const; const QString& guid() const; RssFeed* parent() const; - QString title() const; - QString author() const; - QString torrentUrl() const; - QString link() const; - QString description() const; - QDateTime date() const; + const QString& title() const; + const QString& author() const; + const QString& torrentUrl() const; + const QString& link() const; + const QString& description() const; + const QDateTime& date() const; bool isRead() const; // Setters void markAsRead(); // Serialization QVariantHash toHash() const; - friend RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash &hash); + friend RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash& hash); private: RssFeed* m_parent; @@ -76,6 +76,6 @@ private: bool m_read; }; -RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash &hash); +RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash& hash); #endif // RSSARTICLE_H diff --git a/src/rss/rssfeed.cpp b/src/rss/rssfeed.cpp index 321f9ccc5..c399b723a 100644 --- a/src/rss/rssfeed.cpp +++ b/src/rss/rssfeed.cpp @@ -46,11 +46,17 @@ bool rssArticleDateRecentThan(const RssArticlePtr& left, const RssArticlePtr& ri return left->date() > right->date(); } -RssFeed::RssFeed(RssManager* manager, RssFolder* parent, const QString &url): - m_manager(manager), m_parent(parent), m_icon(":/Icons/oxygen/application-rss+xml.png"), - m_unreadCount(0), m_dirty(false), m_inErrorState(false), m_loading(false) { - qDebug() << Q_FUNC_INFO << url; - m_url = QUrl::fromEncoded(url.toUtf8()).toString(); +RssFeed::RssFeed(RssManager* manager, RssFolder* parent, const QString& url): + m_manager(manager), + m_parent(parent), + m_url (QUrl::fromEncoded(url.toUtf8()).toString()), + m_icon(":/Icons/oxygen/application-rss+xml.png"), + m_unreadCount(0), + m_dirty(false), + m_inErrorState(false), + m_loading(false) +{ + qDebug() << Q_FUNC_INFO << m_url; // Listen for new RSS downloads connect(manager->rssDownloader(), SIGNAL(downloadFinished(QString,QString)), SLOT(handleFinishedDownload(QString,QString))); connect(manager->rssDownloader(), SIGNAL(downloadFailure(QString,QString)), SLOT(handleDownloadFailure(QString,QString))); @@ -66,12 +72,14 @@ RssFeed::RssFeed(RssManager* manager, RssFolder* parent, const QString &url): loadItemsFromDisk(); } -RssFeed::~RssFeed() { +RssFeed::~RssFeed() +{ if (!m_icon.startsWith(":/") && QFile::exists(m_icon)) fsutils::forceRemove(m_icon); } -void RssFeed::saveItemsToDisk() { +void RssFeed::saveItemsToDisk() +{ qDebug() << Q_FUNC_INFO << m_url; if (!m_dirty) return; @@ -80,25 +88,26 @@ void RssFeed::saveItemsToDisk() { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QVariantList old_items; - RssArticleHash::ConstIterator it=m_articles.begin(); - RssArticleHash::ConstIterator itend=m_articles.end(); + RssArticleHash::ConstIterator it = m_articles.begin(); + RssArticleHash::ConstIterator itend = m_articles.end(); for ( ; it != itend; ++it) { old_items << it.value()->toHash(); } - qDebug("Saving %d old items for feed %s", old_items.size(), displayName().toLocal8Bit().data()); + qDebug("Saving %d old items for feed %s", old_items.size(), qPrintable(displayName())); QHash all_old_items = qBTRSS.value("old_items", QHash()).toHash(); all_old_items[m_url] = old_items; qBTRSS.setValue("old_items", all_old_items); } -void RssFeed::loadItemsFromDisk() { +void RssFeed::loadItemsFromDisk() +{ QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QHash all_old_items = qBTRSS.value("old_items", QHash()).toHash(); const QVariantList old_items = all_old_items.value(m_url, QVariantList()).toList(); - qDebug("Loading %d old items for feed %s", old_items.size(), displayName().toLocal8Bit().data()); + qDebug("Loading %d old items for feed %s", old_items.size(), qPrintable(displayName())); - foreach (const QVariant &var_it, old_items) { - QHash item = var_it.toHash(); + foreach (const QVariant& var_it, old_items) { + QVariantHash item = var_it.toHash(); RssArticlePtr rss_item = hashToRssArticle(this, item); if (rss_item) addArticle(rss_item); @@ -127,9 +136,10 @@ void RssFeed::addArticle(const RssArticlePtr& article) } } -bool RssFeed::refresh() { +bool RssFeed::refresh() +{ if (m_loading) { - qWarning() << Q_FUNC_INFO << "Feed" << this->displayName() << "is already being refreshed, ignoring request"; + qWarning() << Q_FUNC_INFO << "Feed" << displayName() << "is already being refreshed, ignoring request"; return false; } m_loading = true; @@ -138,45 +148,46 @@ bool RssFeed::refresh() { return true; } -void RssFeed::removeAllSettings() { +void RssFeed::removeAllSettings() +{ qDebug() << "Removing all settings / history for feed: " << m_url; QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); - QHash feeds_w_downloader = qBTRSS.value("downloader_on", QHash()).toHash(); + QVariantHash feeds_w_downloader = qBTRSS.value("downloader_on", QVariantHash()).toHash(); if (feeds_w_downloader.contains(m_url)) { feeds_w_downloader.remove(m_url); qBTRSS.setValue("downloader_on", feeds_w_downloader); } - QHash all_feeds_filters = qBTRSS.value("feed_filters", QHash()).toHash(); + QVariantHash all_feeds_filters = qBTRSS.value("feed_filters", QVariantHash()).toHash(); if (all_feeds_filters.contains(m_url)) { all_feeds_filters.remove(m_url); qBTRSS.setValue("feed_filters", all_feeds_filters); } - QHash all_old_items = qBTRSS.value("old_items", QHash()).toHash(); + QVariantHash all_old_items = qBTRSS.value("old_items", QVariantHash()).toHash(); if (all_old_items.contains(m_url)) { all_old_items.remove(m_url); qBTRSS.setValue("old_items", all_old_items); } } -void RssFeed::setLoading(bool val) { - m_loading = val; -} - -bool RssFeed::isLoading() const { +bool RssFeed::isLoading() const +{ return m_loading; } -QString RssFeed::title() const { +QString RssFeed::title() const +{ return m_title; } -void RssFeed::rename(const QString &new_name) { +void RssFeed::rename(const QString &new_name) +{ qDebug() << "Renaming stream to" << new_name; m_alias = new_name; } // Return the alias if the stream has one, the url if it has no alias -QString RssFeed::displayName() const { +QString RssFeed::displayName() const +{ if (!m_alias.isEmpty()) return m_alias; if (!m_title.isEmpty()) @@ -184,7 +195,8 @@ QString RssFeed::displayName() const { return m_url; } -QString RssFeed::url() const { +QString RssFeed::url() const +{ return m_url; } @@ -192,29 +204,37 @@ QIcon RssFeed::icon() const { if (m_inErrorState) return QIcon(":/Icons/oxygen/unavailable.png"); + return QIcon(m_icon); } -bool RssFeed::hasCustomIcon() const { +bool RssFeed::hasCustomIcon() const +{ return !m_icon.startsWith(":/"); } -void RssFeed::setIconPath(const QString &path) { - if (path.isEmpty() || !QFile::exists(path)) return; +void RssFeed::setIconPath(const QString& path) +{ + if (path.isEmpty() || !QFile::exists(path)) + return; + m_icon = path; } -RssArticlePtr RssFeed::getItem(const QString &guid) const { +RssArticlePtr RssFeed::getItem(const QString& guid) const +{ return m_articles.value(guid); } -uint RssFeed::count() const { +uint RssFeed::count() const +{ return m_articles.size(); } -void RssFeed::markAsRead() { - RssArticleHash::ConstIterator it=m_articles.begin(); - RssArticleHash::ConstIterator itend=m_articles.end(); +void RssFeed::markAsRead() +{ + RssArticleHash::ConstIterator it = m_articles.begin(); + RssArticleHash::ConstIterator itend = m_articles.end(); for ( ; it != itend; ++it) { it.value()->markAsRead(); } @@ -227,11 +247,13 @@ uint RssFeed::unreadCount() const return m_unreadCount; } -RssArticleList RssFeed::articleListByDateDesc() const { +RssArticleList RssFeed::articleListByDateDesc() const +{ return m_articlesByDate; } -RssArticleList RssFeed::unreadArticleListByDateDesc() const { +RssArticleList RssFeed::unreadArticleListByDateDesc() const +{ RssArticleList unread_news; RssArticleList::ConstIterator it = m_articlesByDate.begin(); @@ -244,30 +266,34 @@ RssArticleList RssFeed::unreadArticleListByDateDesc() const { } // download the icon from the adress -QString RssFeed::iconUrl() const { +QString RssFeed::iconUrl() const +{ // XXX: This works for most sites but it is not perfect - return QString("http://")+QUrl(m_url).host()+QString("/favicon.ico"); + return QString("http://") + QUrl(m_url).host() + QString("/favicon.ico"); } // read and store the downloaded rss' informations -void RssFeed::handleFinishedDownload(const QString& url, const QString &file_path) { +void RssFeed::handleFinishedDownload(const QString& url, const QString& filePath) +{ if (url == m_url) { qDebug() << Q_FUNC_INFO << "Successfully downloaded RSS feed at" << url; // Parse the download RSS - m_manager->rssParser()->parseRssFile(m_url, file_path); - } - else if (url == m_iconUrl) { - m_icon = file_path; + m_manager->rssParser()->parseRssFile(m_url, filePath); + } else if (url == m_iconUrl) { + m_icon = filePath; qDebug() << Q_FUNC_INFO << "icon path:" << m_icon; - m_manager->forwardFeedIconChanged(m_url, m_icon); // XXX: Ugly + m_manager->forwardFeedIconChanged(m_url, m_icon); } } -void RssFeed::handleDownloadFailure(const QString &url, const QString& error) { - if (url != m_url) return; +void RssFeed::handleDownloadFailure(const QString& url, const QString& error) +{ + if (url != m_url) + return; + m_inErrorState = true; m_loading = false; - m_manager->forwardFeedInfosChanged(m_url, displayName(), m_unreadCount); // XXX: Ugly + m_manager->forwardFeedInfosChanged(m_url, displayName(), m_unreadCount); qWarning() << "Failed to download RSS feed at" << url; qWarning() << "Reason:" << error; } diff --git a/src/rss/rssfeed.h b/src/rss/rssfeed.h index 6b6f0b12e..4c2165cf1 100644 --- a/src/rss/rssfeed.h +++ b/src/rss/rssfeed.h @@ -51,7 +51,7 @@ class RssFeed: public QObject, public RssFile { Q_OBJECT public: - RssFeed(RssManager* manager, RssFolder* m_parent, const QString &url); + RssFeed(RssManager* manager, RssFolder* m_parent, const QString& url); virtual ~RssFeed(); virtual RssFolder* parent() const { return m_parent; } virtual void setParent(RssFolder* parent) { m_parent = parent; } @@ -59,7 +59,6 @@ public: virtual QString id() const { return m_url; } virtual void removeAllSettings(); virtual void saveItemsToDisk(); - void setLoading(bool val); bool isLoading() const; QString title() const; virtual void rename(const QString &alias); diff --git a/src/rss/rssmanager.cpp b/src/rss/rssmanager.cpp index 332fa223c..d6f580171 100644 --- a/src/rss/rssmanager.cpp +++ b/src/rss/rssmanager.cpp @@ -38,14 +38,16 @@ #include "rssparser.h" #include "downloadthread.h" +static const int MSECS_PER_MIN = 60000; + RssManager::RssManager(): m_rssDownloader(new DownloadThread(this)), m_downloadRules(new RssDownloadRuleList), m_rssParser(new RssParser(this)) { - connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh())); + connect(&m_refreshTimer, SIGNAL(timeout()), SLOT(refresh())); m_refreshInterval = RssSettings().getRSSRefreshInterval(); - m_refreshTimer.start(m_refreshInterval*60000); + m_refreshTimer.start(m_refreshInterval * MSECS_PER_MIN); } RssManager::~RssManager() diff --git a/src/rss/rssmanager.h b/src/rss/rssmanager.h index a8be332d4..f539a1801 100644 --- a/src/rss/rssmanager.h +++ b/src/rss/rssmanager.h @@ -52,7 +52,6 @@ public: DownloadThread* rssDownloader() const; RssParser* rssParser() const; - RssDownloadRuleList* downloadRules() const; public slots: