From 954e010493a927979fd23709cc89abaf94a56883 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Tue, 25 Jan 2011 18:46:38 +0000 Subject: [PATCH] RSS optimization --- src/rss/rss_imp.cpp | 42 ++++++++++----------- src/rss/rssarticle.cpp | 26 ++++++------- src/rss/rssarticle.h | 7 ++-- src/rss/rssdownloadrule.cpp | 6 +-- src/rss/rssfeed.cpp | 74 +++++++++++++++++-------------------- src/rss/rssfeed.h | 48 ++++++++++++------------ src/rss/rssfile.h | 5 +-- src/rss/rssfolder.cpp | 16 +++----- src/rss/rssfolder.h | 4 +- src/rss/rssmanager.cpp | 10 ++--- src/rss/rssmanager.h | 4 +- 11 files changed, 114 insertions(+), 128 deletions(-) diff --git a/src/rss/rss_imp.cpp b/src/rss/rss_imp.cpp index fb8a45140..7c1bc4fef 100644 --- a/src/rss/rss_imp.cpp +++ b/src/rss/rss_imp.cpp @@ -99,7 +99,7 @@ void RSSImp::displayItemsListMenu(const QPoint&){ foreach(QTreeWidgetItem *item, selectedItems) { qDebug("text(3) URL: %s", qPrintable(item->text(NEWS_URL_COL))); qDebug("text(2) TITLE: %s", qPrintable(item->text(NEWS_TITLE_COL))); - if(listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID))->hasAttachment()) { + if(listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)).hasAttachment()) { has_attachment = true; break; } @@ -308,11 +308,11 @@ void RSSImp::on_updateAllButton_clicked() { void RSSImp::downloadTorrent() { QList selected_items = listNews->selectedItems(); foreach(const QTreeWidgetItem* item, selected_items) { - RssArticle* article = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)); - if(article->hasAttachment()) { - QBtSession::instance()->downloadFromUrl(article->torrentUrl()); + const RssArticle article = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)); + if(article.hasAttachment()) { + QBtSession::instance()->downloadFromUrl(article.torrentUrl()); } else { - QBtSession::instance()->downloadFromUrl(article->link()); + QBtSession::instance()->downloadFromUrl(article.link()); } } } @@ -321,8 +321,8 @@ void RSSImp::downloadTorrent() { void RSSImp::openNewsUrl() { QList selected_items = listNews->selectedItems(); foreach(const QTreeWidgetItem* item, selected_items) { - RssArticle* news = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)); - QString link = news->link(); + const RssArticle news = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)); + const QString link = news.link(); if(!link.isEmpty()) QDesktopServices::openUrl(QUrl(link)); } @@ -442,7 +442,7 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* item) { if(!rss_item) return; qDebug("Getting the list of news"); - QList news; + QList news; if(rss_item == rssmanager) news = RssManager::sortNewsList(rss_item->getUnreadNewsList()); else if(rss_item) @@ -452,12 +452,12 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* item) { previous_news = 0; listNews->clear(); qDebug("Got the list of news"); - foreach(RssArticle* article, news){ + foreach(const RssArticle &article, news){ QTreeWidgetItem* it = new QTreeWidgetItem(listNews); - it->setText(NEWS_TITLE_COL, article->title()); - it->setText(NEWS_URL_COL, article->parent()->getUrl()); - it->setText(NEWS_ID, article->guid()); - if(article->isRead()){ + it->setText(NEWS_TITLE_COL, article.title()); + it->setText(NEWS_URL_COL, article.parent()->getUrl()); + it->setText(NEWS_ID, article.guid()); + if(article.isRead()){ it->setData(NEWS_TITLE_COL, Qt::ForegroundRole, QVariant(QColor("grey"))); it->setData(NEWS_ICON, Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png"))); }else{ @@ -489,20 +489,20 @@ void RSSImp::refreshTextBrowser() { previous_news = item; } RssFeed *stream = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL)); - RssArticle* article = stream->getItem(item->text(NEWS_ID)); + RssArticle article = stream->getItem(item->text(NEWS_ID)); QString html; html += "
"; - html += "
"+article->title() + "
"; - if(article->date().isValid()) { - html += "
"+tr("Date: ")+""+article->date().toLocalTime().toString(Qt::SystemLocaleLongDate)+"
"; + html += "
"+article.title() + "
"; + if(article.date().isValid()) { + html += "
"+tr("Date: ")+""+article.date().toLocalTime().toString(Qt::SystemLocaleLongDate)+"
"; } - if(!article->author().isEmpty()) { - html += "
"+tr("Author: ")+""+article->author()+"
"; + if(!article.author().isEmpty()) { + html += "
"+tr("Author: ")+""+article.author()+"
"; } html += "
"; - html += ""+article->description()+""; + html += ""+article.description()+""; textBrowser->setHtml(html); - article->markAsRead(); + article.markAsRead(); item->setData(NEWS_TITLE_COL, Qt::ForegroundRole, QVariant(QColor("grey"))); item->setData(0, Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png"))); // Decrement feed nb unread news diff --git a/src/rss/rssarticle.cpp b/src/rss/rssarticle.cpp index 1fb236479..f09350e99 100644 --- a/src/rss/rssarticle.cpp +++ b/src/rss/rssarticle.cpp @@ -251,19 +251,19 @@ QVariantHash RssArticle::toHash() const { return item; } -RssArticle* hashToRssArticle(RssFeed* parent, const QVariantHash &h) { +RssArticle hashToRssArticle(RssFeed* parent, const QVariantHash &h) { const QString guid = h.value("id").toString(); - if(guid.isEmpty()) return 0; - RssArticle *art = new RssArticle(parent, guid); - art->m_title = h.value("title", "").toString(); - art->m_torrentUrl = h.value("torrent_url", "").toString(); - art->m_link = h.value("news_link", "").toString(); - art->m_description = h.value("description").toString(); - art->m_date = h.value("date").toDateTime(); - art->m_author = h.value("author").toString(); - art->m_read = h.value("read").toBool(); + if(guid.isEmpty()) return RssArticle(); + RssArticle art(parent, guid); + art.m_title = h.value("title", "").toString(); + art.m_torrentUrl = h.value("torrent_url", "").toString(); + art.m_link = h.value("news_link", "").toString(); + art.m_description = h.value("description").toString(); + art.m_date = h.value("date").toDateTime(); + art.m_author = h.value("author").toString(); + art.m_read = h.value("read").toBool(); - Q_ASSERT(art->isValid()); + Q_ASSERT(art.isValid()); return art; } @@ -288,8 +288,8 @@ QString RssArticle::link() const { } QString RssArticle::description() const{ - if(m_description.isEmpty()) - return tr("No description available"); + if(m_description.isNull()) + return ""; return m_description; } diff --git a/src/rss/rssarticle.h b/src/rss/rssarticle.h index 757bfbfca..373786b97 100644 --- a/src/rss/rssarticle.h +++ b/src/rss/rssarticle.h @@ -38,12 +38,11 @@ class RssFeed; // Item of a rss stream, single information -class RssArticle: public QObject { - Q_OBJECT +class RssArticle { public: RssArticle(RssFeed* parent, QXmlStreamReader& xml); - RssArticle(RssFeed* parent, const QString &guid); + RssArticle(RssFeed* parent = 0, const QString &guid = QString()); ~RssArticle(); // Accessors bool isValid() const; @@ -61,7 +60,7 @@ public: void markAsRead(); // Serialization QVariantHash toHash() const; - friend RssArticle* hashToRssArticle(RssFeed* parent, const QVariantHash &hash); + friend RssArticle hashToRssArticle(RssFeed* parent, const QVariantHash &hash); protected: QDateTime parseDate(const QString &string); diff --git a/src/rss/rssdownloadrule.cpp b/src/rss/rssdownloadrule.cpp index 11005c454..beefac8d1 100644 --- a/src/rss/rssdownloadrule.cpp +++ b/src/rss/rssdownloadrule.cpp @@ -129,9 +129,9 @@ void RssDownloadRule::setSavePath(const QString &save_path) QStringList RssDownloadRule::findMatchingArticles(const RssFeed *feed) const { QStringList ret; - foreach(const RssArticle *art, feed->values()) { - if(matches(art->title())) - ret << art->title(); + foreach(const RssArticle &art, feed->values()) { + if(matches(art.title())) + ret << art.title(); } return ret; } diff --git a/src/rss/rssfeed.cpp b/src/rss/rssfeed.cpp index a5945deff..47d82a1f7 100644 --- a/src/rss/rssfeed.cpp +++ b/src/rss/rssfeed.cpp @@ -46,9 +46,9 @@ RssFeed::RssFeed(RssFolder* parent, QString _url): parent(parent), alias(""), ic qDebug("Loading %d old items for feed %s", old_items.size(), getName().toLocal8Bit().data()); foreach(const QVariant &var_it, old_items) { QHash item = var_it.toHash(); - RssArticle *rss_item = hashToRssArticle(this, item); - if(rss_item) { - insert(rss_item->guid(), rss_item); + const RssArticle rss_item = hashToRssArticle(this, item); + if(rss_item.isValid()) { + insert(rss_item.guid(), rss_item); } } } @@ -58,16 +58,14 @@ RssFeed::~RssFeed(){ if(refreshed) { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QVariantList old_items; - foreach(RssArticle *item, this->values()) { - old_items << item->toHash(); + foreach(const RssArticle &item, this->values()) { + old_items << item.toHash(); } qDebug("Saving %d old items for feed %s", old_items.size(), getName().toLocal8Bit().data()); QHash all_old_items = qBTRSS.value("old_items", QHash()).toHash(); all_old_items[url] = old_items; qBTRSS.setValue("old_items", all_old_items); } - qDebug("Removing all item from feed"); - removeAllItems(); qDebug("All items were removed"); if(QFile::exists(filePath)) misc::safeRemove(filePath); @@ -83,12 +81,6 @@ void RssFeed::refresh() { parent->refreshStream(url); } -// delete all the items saved -void RssFeed::removeAllItems() { - qDeleteAll(this->values()); - this->clear(); -} - void RssFeed::removeAllSettings() { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QHash feeds_w_downloader = qBTRSS.value("downloader_on", QHash()).toHash(); @@ -172,8 +164,8 @@ void RssFeed::setIconPath(QString path) { iconPath = path; } -RssArticle* RssFeed::getItem(QString id) const{ - return this->value(id); +RssArticle& RssFeed::getItem(QString id) { + return (*this)[id]; } unsigned int RssFeed::getNbNews() const{ @@ -181,29 +173,30 @@ unsigned int RssFeed::getNbNews() const{ } void RssFeed::markAllAsRead() { - foreach(RssArticle *item, this->values()){ - item->markAsRead(); + QHash::iterator it; + for(it = this->begin(); it != this->end(); it++) { + it.value().markAsRead(); } RssManager::instance()->forwardFeedInfosChanged(url, getName(), 0); } unsigned int RssFeed::getNbUnRead() const{ unsigned int nbUnread=0; - foreach(RssArticle *item, this->values()) { - if(!item->isRead()) + foreach(const RssArticle &item, this->values()) { + if(!item.isRead()) ++nbUnread; } return nbUnread; } -QList RssFeed::getNewsList() const{ +QList RssFeed::getNewsList() const{ return this->values(); } -QList RssFeed::getUnreadNewsList() const { - QList unread_news; - foreach(RssArticle *item, this->values()) { - if(!item->isRead()) +QList RssFeed::getUnreadNewsList() const { + QList unread_news; + foreach(const RssArticle &item, this->values()) { + if(!item.isRead()) unread_news << item; } return unread_news; @@ -272,11 +265,9 @@ short RssFeed::readDoc(QIODevice* device) { image = xml.attributes().value("url").toString(); } else if(xml.name() == "item") { - RssArticle * item = new RssArticle(this, xml); - if(item->isValid() && !itemAlreadyExists(item->guid())) { - this->insert(item->guid(), item); - } else { - delete item; + RssArticle item(this, xml); + if(item.isValid() && !itemAlreadyExists(item.guid())) { + this->insert(item.guid(), item); } } } @@ -289,21 +280,23 @@ short RssFeed::readDoc(QIODevice* device) { // RSS Feed Downloader if(RssSettings().isRssDownloadingEnabled()) { - foreach(RssArticle* item, values()) { - if(item->isRead()) continue; + QHash::iterator it; + for(it = this->begin(); it != this->end(); it++) { + RssArticle &item = it.value(); + if(item.isRead()) continue; QString torrent_url; - if(item->hasAttachment()) - torrent_url = item->torrentUrl(); + if(item.hasAttachment()) + torrent_url = item.torrentUrl(); else - torrent_url = item->link(); + torrent_url = item.link(); // Check if the item should be automatically downloaded - const RssDownloadRule matching_rule = RssDownloadRuleList::instance()->findMatchingRule(url, item->title()); + const RssDownloadRule matching_rule = RssDownloadRuleList::instance()->findMatchingRule(url, item.title()); if(matching_rule.isValid()) { // Download the torrent - QBtSession::instance()->addConsoleMessage(tr("Automatically downloading %1 torrent from %2 RSS feed...").arg(item->title()).arg(getName())); + QBtSession::instance()->addConsoleMessage(tr("Automatically downloading %1 torrent from %2 RSS feed...").arg(item.title()).arg(getName())); QBtSession::instance()->downloadUrlAndSkipDialog(torrent_url, matching_rule.savePath(), matching_rule.label()); // Item was downloaded, consider it as Read - item->markAsRead(); + item.markAsRead(); } } } @@ -314,11 +307,10 @@ void RssFeed::resizeList() { const unsigned int max_articles = RssSettings().getRSSMaxArticlesPerFeed(); const unsigned int nb_articles = this->size(); if(nb_articles > max_articles) { - QList listItem = RssManager::sortNewsList(this->values()); + const QList listItem = RssManager::sortNewsList(this->values()); const int excess = nb_articles - max_articles; - for(int i=0; itake(lastItem->guid()); + for(uint i=nb_articles-excess; iremove(listItem.at(i).guid()); } } } diff --git a/src/rss/rssfeed.h b/src/rss/rssfeed.h index ce09cc3b8..66d57a8ff 100644 --- a/src/rss/rssfeed.h +++ b/src/rss/rssfeed.h @@ -37,28 +37,9 @@ class RssManager; -class RssFeed: public RssFile, public QHash { +class RssFeed: public RssFile, public QHash { Q_OBJECT -private: - RssFolder *parent; - QString title; - QString link; - QString description; - QString image; - QString url; - QString alias; - QString filePath; - QString iconPath; - bool read; - bool refreshed; - bool downloadFailure; - bool currently_loading; - -public slots: - void processDownloadedFile(QString file_path); - void setDownloadFailed(); - public: RssFeed(RssFolder* parent, QString _url); ~RssFeed(); @@ -67,7 +48,6 @@ public: FileType getType() const; void refresh(); QString getID() const { return url; } - void removeAllItems(); void removeAllSettings(); bool itemAlreadyExists(QString hash); void setLoading(bool val); @@ -83,18 +63,38 @@ public: QString getIconPath() const; bool hasCustomIcon() const; void setIconPath(QString path); - RssArticle* getItem(QString name) const; + RssArticle& getItem(QString name); unsigned int getNbNews() const; void markAllAsRead(); unsigned int getNbUnRead() const; - QList getNewsList() const; - QList getUnreadNewsList() const; + QList getNewsList() const; + QList getUnreadNewsList() const; QString getIconUrl(); +public slots: + void processDownloadedFile(QString file_path); + void setDownloadFailed(); + private: short readDoc(QIODevice* device); void resizeList(); short openRss(); + +private: + RssFolder *parent; + QString title; + QString link; + QString description; + QString image; + QString url; + QString alias; + QString filePath; + QString iconPath; + bool read; + bool refreshed; + bool downloadFailure; + bool currently_loading; + }; diff --git a/src/rss/rssfile.h b/src/rss/rssfile.h index 64d15eac1..15b9b0d5b 100644 --- a/src/rss/rssfile.h +++ b/src/rss/rssfile.h @@ -50,15 +50,14 @@ public: virtual FileType getType() const = 0; virtual QString getName() const = 0; virtual QString getID() const = 0; - virtual void removeAllItems() = 0; virtual void rename(QString new_name) = 0; virtual void markAllAsRead() = 0; virtual RssFolder* getParent() const = 0; virtual void setParent(RssFolder*) = 0; virtual void refresh() = 0; virtual void removeAllSettings() = 0; - virtual QList getNewsList() const = 0; - virtual QList getUnreadNewsList() const = 0; + virtual QList getNewsList() const = 0; + virtual QList getUnreadNewsList() const = 0; QStringList getPath() const { QStringList path; if(getParent()) { diff --git a/src/rss/rssfolder.cpp b/src/rss/rssfolder.cpp index 28eb101f7..76fddbcd8 100644 --- a/src/rss/rssfolder.cpp +++ b/src/rss/rssfolder.cpp @@ -89,7 +89,6 @@ void RssFolder::removeFile(QString ID) { if(this->contains(ID)) { RssFile* child = this->take(ID); child->removeAllSettings(); - child->removeAllItems(); delete child; } } @@ -124,17 +123,17 @@ void RssFolder::refresh() { } } -QList RssFolder::getNewsList() const { - QList news; - foreach(RssFile *child, this->values()) { +QList RssFolder::getNewsList() const { + QList news; + foreach(const RssFile *child, this->values()) { news << child->getNewsList(); } return news; } -QList RssFolder::getUnreadNewsList() const { - QList unread_news; - foreach(RssFile *child, this->values()) { +QList RssFolder::getUnreadNewsList() const { + QList unread_news; + foreach(const RssFile *child, this->values()) { unread_news << child->getUnreadNewsList(); } return unread_news; @@ -295,9 +294,6 @@ void RssFolder::addFile(RssFile * item) { } void RssFolder::removeAllItems() { - foreach(RssFile* child, values()) { - child->removeAllItems(); - } qDeleteAll(values()); clear(); } diff --git a/src/rss/rssfolder.h b/src/rss/rssfolder.h index 1bd6eba24..999f0f0a8 100644 --- a/src/rss/rssfolder.h +++ b/src/rss/rssfolder.h @@ -59,8 +59,8 @@ public: QString getName() const; QString getID() const; bool hasChild(QString ID); - QList getNewsList() const; - QList getUnreadNewsList() const; + QList getNewsList() const; + QList getUnreadNewsList() const; void removeAllSettings(); void removeAllItems(); diff --git a/src/rss/rssmanager.cpp b/src/rss/rssmanager.cpp index d3320051b..2b27b3363 100644 --- a/src/rss/rssmanager.cpp +++ b/src/rss/rssmanager.cpp @@ -126,17 +126,17 @@ void RssManager::saveStreamList(){ settings.setRssFeedsAliases(aliases); } -void RssManager::insertSortElem(QList &list, RssArticle *item) { +void RssManager::insertSortElem(QList &list, const RssArticle &item) { int i = 0; - while(i < list.size() && item->date() < list.at(i)->date()) { + while(i < list.size() && item.date() < list.at(i).date()) { ++i; } list.insert(i, item); } -QList RssManager::sortNewsList(const QList& news_list) { - QList new_list; - foreach(RssArticle *item, news_list) { +QList RssManager::sortNewsList(const QList& news_list) { + QList new_list; + foreach(const RssArticle &item, news_list) { insertSortElem(new_list, item); } return new_list; diff --git a/src/rss/rssmanager.h b/src/rss/rssmanager.h index e4dbb855a..189c11d42 100644 --- a/src/rss/rssmanager.h +++ b/src/rss/rssmanager.h @@ -45,8 +45,8 @@ public: static RssManager* instance(); static void drop(); ~RssManager(); - static void insertSortElem(QList &list, RssArticle *item); - static QList sortNewsList(const QList& news_list); + static void insertSortElem(QList &list, const RssArticle &item); + static QList sortNewsList(const QList& news_list); public slots: void loadStreamList();