From 3bc75bb06858ca94ad20aa8917d663f4a4894884 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 19 Feb 2012 15:13:39 +0200 Subject: [PATCH] RSS optimizations --- src/rss/rss_imp.cpp | 6 ++++-- src/rss/rssarticle.cpp | 12 ++++-------- src/rss/rssarticle_p.h | 3 ++- src/rss/rssfeed.cpp | 13 +++++++------ src/rss/rssfeed.h | 6 +++--- src/rss/rssmanager.cpp | 17 +++++------------ src/rss/rssmanager.h | 4 ++-- 7 files changed, 27 insertions(+), 34 deletions(-) diff --git a/src/rss/rss_imp.cpp b/src/rss/rss_imp.cpp index 8b552bf60..90bb64015 100644 --- a/src/rss/rss_imp.cpp +++ b/src/rss/rss_imp.cpp @@ -461,9 +461,11 @@ void RSSImp::refreshArticleList(QTreeWidgetItem* item) { qDebug("Getting the list of news"); QList news; if(rss_item == m_rssManager) - news = RssManager::sortNewsList(rss_item->unreadArticleList()); + news = rss_item->unreadArticleList(); else if(rss_item) - news = RssManager::sortNewsList(rss_item->articleList()); + news = rss_item->articleList(); + // Sort + RssManager::sortNewsList(news); // Clear the list first textBrowser->clear(); m_currentArticle = 0; diff --git a/src/rss/rssarticle.cpp b/src/rss/rssarticle.cpp index 8a557e93e..265c57e52 100644 --- a/src/rss/rssarticle.cpp +++ b/src/rss/rssarticle.cpp @@ -191,10 +191,9 @@ QDateTime RssArticle::parseDate(const QString &string) { } // public constructor -RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml) +RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml): + d(new RssArticleData(parent)) { - d = new RssArticleData; - d->parent = parent; while(!xml.atEnd()) { xml.readNext(); @@ -231,11 +230,8 @@ RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml) } } -RssArticle::RssArticle(RssFeed* parent, const QString &guid) { - d = new RssArticleData; - d->parent = parent; - d->guid = guid; -} +RssArticle::RssArticle(RssFeed* parent, const QString &guid): + d(new RssArticleData(parent, guid)) {} RssArticle::~RssArticle() {} diff --git a/src/rss/rssarticle_p.h b/src/rss/rssarticle_p.h index 20cfdc8e1..d15e684fa 100644 --- a/src/rss/rssarticle_p.h +++ b/src/rss/rssarticle_p.h @@ -39,7 +39,8 @@ class RssFeed; class RssArticleData: public QSharedData { public: - RssArticleData(): QSharedData(), read(false) {} + RssArticleData(RssFeed* _parent, const QString& _guid = QString()): + parent(_parent), guid(_guid), read(false) {} ~RssArticleData() {} RssArticleData(const RssArticleData& other): QSharedData(other), parent(other.parent), diff --git a/src/rss/rssfeed.cpp b/src/rss/rssfeed.cpp index aef0f94c0..ddb719eb0 100644 --- a/src/rss/rssfeed.cpp +++ b/src/rss/rssfeed.cpp @@ -123,8 +123,8 @@ void RssFeed::removeAllSettings() { } } -bool RssFeed::itemAlreadyExists(const QString &hash) const { - return m_articles.contains(hash); +bool RssFeed::itemAlreadyExists(const QString &guid) const { + return m_articles.contains(guid); } void RssFeed::setLoading(bool val) { @@ -177,8 +177,8 @@ void RssFeed::setIconPath(const QString &path) { m_icon = path; } -RssArticle& RssFeed::getItem(const QString &id) { - return m_articles[id]; +RssArticle& RssFeed::getItem(const QString &guid) { + return m_articles[guid]; } uint RssFeed::count() const{ @@ -323,10 +323,11 @@ void RssFeed::resizeList() { const uint max_articles = RssSettings().getRSSMaxArticlesPerFeed(); const uint nb_articles = m_articles.size(); if(nb_articles > max_articles) { - const QList listItem = RssManager::sortNewsList(m_articles.values()); + QList listItems = m_articles.values(); + RssManager::sortNewsList(listItems); const int excess = nb_articles - max_articles; for(uint i=nb_articles-excess; i &list, const RssArticle &item) { - int i = 0; - while(i < list.size() && item.date() < list.at(i).date()) { - ++i; - } - list.insert(i, item); +static bool earlierItemDate(const RssArticle& a, const RssArticle& b) +{ + return (a.date() < b.date()); } -QList RssManager::sortNewsList(const QList& news_list) { - QList new_list; - foreach(const RssArticle &item, news_list) { - insertSortElem(new_list, item); - } - return new_list; +void RssManager::sortNewsList(QList& news_list) { + qSort(news_list.begin(), news_list.end(), earlierItemDate); } RssManager * RssManager::instance() diff --git a/src/rss/rssmanager.h b/src/rss/rssmanager.h index e4fd770ce..17090f231 100644 --- a/src/rss/rssmanager.h +++ b/src/rss/rssmanager.h @@ -46,10 +46,10 @@ private: public: static RssManager* instance(); static void drop(); - ~RssManager(); + virtual ~RssManager(); inline DownloadThread* rssDownloader() const { return m_rssDownloader; } static void insertSortElem(QList &list, const RssArticle &item); - static QList sortNewsList(const QList& news_list); + static void sortNewsList(QList& news_list); public slots: void loadStreamList();