Browse Source

Keep RSS articles sorted by date in memory do avoid sorting them all the time

Optimization to address issue #34.
adaptive-webui-19844
Christophe Dumez 12 years ago
parent
commit
d1e30cacf8
  1. 6
      src/rss/rss_imp.cpp
  2. 72
      src/rss/rssfeed.cpp
  3. 11
      src/rss/rssfeed.h
  4. 4
      src/rss/rssfile.h
  5. 12
      src/rss/rssfolder.cpp
  6. 4
      src/rss/rssfolder.h
  7. 9
      src/rss/rssmanager.cpp
  8. 1
      src/rss/rssmanager.h

6
src/rss/rss_imp.cpp

@ -490,11 +490,9 @@ void RSSImp::refreshArticleList(QTreeWidgetItem* item) { @@ -490,11 +490,9 @@ void RSSImp::refreshArticleList(QTreeWidgetItem* item) {
qDebug("Getting the list of news");
RssArticleList news;
if (rss_item == m_rssManager)
news = rss_item->unreadArticleList();
news = rss_item->unreadArticleListByDateDesc();
else if (rss_item)
news = rss_item->articleList();
// Sort
RssManager::sortArticleListByDateDesc(news);
news = rss_item->articleListByDateDesc();
// Clear the list first
textBrowser->clear();
m_currentArticle = 0;

72
src/rss/rssfeed.cpp

@ -41,6 +41,11 @@ @@ -41,6 +41,11 @@
#include "downloadthread.h"
#include "fs_utils.h"
bool rssArticleDateRecentThan(const RssArticlePtr& left, const RssArticlePtr& right)
{
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) {
@ -95,11 +100,30 @@ void RssFeed::loadItemsFromDisk() { @@ -95,11 +100,30 @@ void RssFeed::loadItemsFromDisk() {
foreach (const QVariant &var_it, old_items) {
QHash<QString, QVariant> item = var_it.toHash();
RssArticlePtr rss_item = hashToRssArticle(this, item);
if (rss_item) {
m_articles.insert(rss_item->guid(), rss_item);
if (!rss_item->isRead())
++m_unreadCount;
}
if (rss_item)
addArticle(rss_item);
}
}
void RssFeed::addArticle(const RssArticlePtr& article)
{
Q_ASSERT(!m_articles.contains(article->guid()));
// Update unreadCount
if (!article->isRead())
++m_unreadCount;
// Insert in hash table
m_articles[article->guid()] = article;
// Insertion sort
RssArticleList::Iterator lowerBound = qLowerBound(m_articlesByDate.begin(), m_articlesByDate.end(), article, rssArticleDateRecentThan);
m_articlesByDate.insert(lowerBound, article);
// Restrict size
const int max_articles = RssSettings().getRSSMaxArticlesPerFeed();
if (m_articlesByDate.size() > max_articles) {
RssArticlePtr oldestArticle = m_articlesByDate.takeLast();
m_articles.remove(oldestArticle->guid());
// Update unreadCount
if (!oldestArticle->isRead())
--m_unreadCount;
}
}
@ -201,18 +225,18 @@ uint RssFeed::unreadCount() const @@ -201,18 +225,18 @@ uint RssFeed::unreadCount() const
return m_unreadCount;
}
RssArticleList RssFeed::articleList() const {
return m_articles.values();
RssArticleList RssFeed::articleListByDateDesc() const {
return m_articlesByDate;
}
RssArticleList RssFeed::unreadArticleList() const {
RssArticleList RssFeed::unreadArticleListByDateDesc() const {
RssArticleList unread_news;
RssArticleHash::ConstIterator it = m_articles.begin();
RssArticleHash::ConstIterator itend = m_articles.end();
RssArticleList::ConstIterator it = m_articlesByDate.begin();
RssArticleList::ConstIterator itend = m_articlesByDate.end();
for ( ; it != itend; ++it) {
if (!it.value()->isRead())
unread_news << it.value();
if (!(*it)->isRead())
unread_news << *it;
}
return unread_news;
}
@ -223,22 +247,6 @@ QString RssFeed::iconUrl() const { @@ -223,22 +247,6 @@ QString RssFeed::iconUrl() const {
return QString("http://")+QUrl(m_url).host()+QString("/favicon.ico");
}
void RssFeed::removeOldArticles() {
const uint max_articles = RssSettings().getRSSMaxArticlesPerFeed();
const uint nb_articles = m_articles.size();
if (nb_articles > max_articles) {
RssArticleList listItems = m_articles.values();
RssManager::sortArticleListByDateDesc(listItems);
const int excess = nb_articles - max_articles;
for (uint i=nb_articles-excess; i<nb_articles; ++i) {
RssArticlePtr article = m_articles.take(listItems.at(i)->guid());
// Update unreadCount
if (!article->isRead())
--m_unreadCount;
}
}
}
// read and store the downloaded rss' informations
void RssFeed::handleFinishedDownload(const QString& url, const QString &file_path) {
if (url == m_url) {
@ -290,7 +298,7 @@ void RssFeed::handleNewArticle(const QString& feedUrl, const QVariantHash& artic @@ -290,7 +298,7 @@ void RssFeed::handleNewArticle(const QString& feedUrl, const QVariantHash& artic
RssArticlePtr article = hashToRssArticle(this, articleData);
Q_ASSERT(article);
m_articles[guid] = article;
addArticle(article);
// Download torrent if necessary.
if (RssSettings().isRssDownloadingEnabled()) {
@ -307,9 +315,6 @@ void RssFeed::handleNewArticle(const QString& feedUrl, const QVariantHash& artic @@ -307,9 +315,6 @@ void RssFeed::handleNewArticle(const QString& feedUrl, const QVariantHash& artic
QBtSession::instance()->downloadUrlAndSkipDialog(torrent_url, matching_rule->savePath(), matching_rule->label());
}
}
// Update unreadCount if necessary
if (!article->isRead())
++m_unreadCount;
m_manager->forwardFeedInfosChanged(m_url, displayName(), m_unreadCount);
// FIXME: We should forward the information here but this would seriously decrease
@ -327,9 +332,6 @@ void RssFeed::handleFeedParsingFinished(const QString& feedUrl, const QString& e @@ -327,9 +332,6 @@ void RssFeed::handleFeedParsingFinished(const QString& feedUrl, const QString& e
qWarning() << "Reason:" << error;
}
// Make sure we limit the number of articles
removeOldArticles();
m_loading = false;
m_inErrorState = !error.isEmpty();

11
src/rss/rssfeed.h

@ -45,6 +45,8 @@ typedef QHash<QString, RssArticlePtr> RssArticleHash; @@ -45,6 +45,8 @@ typedef QHash<QString, RssArticlePtr> RssArticleHash;
typedef QSharedPointer<RssFeed> RssFeedPtr;
typedef QList<RssFeedPtr> RssFeedList;
bool rssArticleDateRecentThan(const RssArticlePtr& left, const RssArticlePtr& right);
class RssFeed: public QObject, public RssFile {
Q_OBJECT
@ -70,9 +72,9 @@ public: @@ -70,9 +72,9 @@ public:
uint count() const;
virtual void markAsRead();
virtual uint unreadCount() const;
virtual RssArticleList articleList() const;
virtual RssArticleList articleListByDateDesc() const;
const RssArticleHash& articleHash() const { return m_articles; }
virtual RssArticleList unreadArticleList() const;
virtual RssArticleList unreadArticleListByDateDesc() const;
void decrementUnreadCount();
private slots:
@ -83,14 +85,15 @@ private slots: @@ -83,14 +85,15 @@ private slots:
void handleFeedParsingFinished(const QString& feedUrl, const QString& error);
private:
void removeOldArticles();
QString iconUrl() const;
void loadItemsFromDisk();
void addArticle(const RssArticlePtr& article);
private:
RssManager* m_manager;
RssArticleHash m_articles;
RssFolder *m_parent;
RssArticleList m_articlesByDate; // Articles sorted by date (more recent first)
RssFolder* m_parent;
QString m_title;
QString m_url;
QString m_alias;

4
src/rss/rssfile.h

@ -59,8 +59,8 @@ public: @@ -59,8 +59,8 @@ public:
virtual RssFolder* parent() const = 0;
virtual void setParent(RssFolder* parent) = 0;
virtual void refresh() = 0;
virtual RssArticleList articleList() const = 0;
virtual RssArticleList unreadArticleList() const = 0;
virtual RssArticleList articleListByDateDesc() const = 0;
virtual RssArticleList unreadArticleListByDateDesc() const = 0;
virtual void removeAllSettings() = 0;
virtual void saveItemsToDisk() = 0;
QStringList pathHierarchy() const;

12
src/rss/rssfolder.cpp

@ -91,24 +91,28 @@ void RssFolder::refresh() { @@ -91,24 +91,28 @@ void RssFolder::refresh() {
}
}
RssArticleList RssFolder::articleList() const {
RssArticleList RssFolder::articleListByDateDesc() const {
RssArticleList news;
RssFileHash::ConstIterator it = m_children.begin();
RssFileHash::ConstIterator itend = m_children.end();
for ( ; it != itend; ++it) {
news << it.value()->articleList();
int n = news.size();
news << it.value()->articleListByDateDesc();
std::inplace_merge(news.begin(), news.begin() + n, news.end(), rssArticleDateRecentThan);
}
return news;
}
RssArticleList RssFolder::unreadArticleList() const {
RssArticleList RssFolder::unreadArticleListByDateDesc() const {
RssArticleList unread_news;
RssFileHash::ConstIterator it = m_children.begin();
RssFileHash::ConstIterator itend = m_children.end();
for ( ; it != itend; ++it) {
unread_news << it.value()->unreadArticleList();
int n = unread_news.size();
unread_news << it.value()->unreadArticleListByDateDesc();
std::inplace_merge(unread_news.begin(), unread_news.begin() + n, unread_news.end(), rssArticleDateRecentThan);
}
return unread_news;
}

4
src/rss/rssfolder.h

@ -62,8 +62,8 @@ public: @@ -62,8 +62,8 @@ public:
virtual QString displayName() const;
virtual QString id() const;
bool hasChild(const QString &childId);
virtual RssArticleList articleList() const;
virtual RssArticleList unreadArticleList() const;
virtual RssArticleList articleListByDateDesc() const;
virtual RssArticleList unreadArticleListByDateDesc() const;
virtual void removeAllSettings();
virtual void saveItemsToDisk();
void removeAllItems();

9
src/rss/rssmanager.cpp

@ -151,15 +151,6 @@ void RssManager::saveStreamList() const { @@ -151,15 +151,6 @@ void RssManager::saveStreamList() const {
settings.setRssFeedsAliases(aliases);
}
static bool laterItemDate(const RssArticlePtr& a, const RssArticlePtr& b)
{
return (a->date() > b->date());
}
void RssManager::sortArticleListByDateDesc(RssArticleList& news_list) {
qSort(news_list.begin(), news_list.end(), laterItemDate);
}
RssDownloadRuleList *RssManager::downloadRules() const
{
Q_ASSERT(m_downloadRules);

1
src/rss/rssmanager.h

@ -52,7 +52,6 @@ public: @@ -52,7 +52,6 @@ public:
DownloadThread* rssDownloader() const;
RssParser* rssParser() const;
static void sortArticleListByDateDesc(RssArticleList& news_list);
RssDownloadRuleList* downloadRules() const;

Loading…
Cancel
Save