Browse Source

RSS code clean up

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
86b4db75be
  1. 30
      src/rss/rss_imp.cpp
  2. 113
      src/rss/rssarticle.cpp
  3. 53
      src/rss/rssarticle.h
  4. 4
      src/rss/rssdownloadrule.cpp
  5. 30
      src/rss/rssfeed.cpp
  6. 2
      src/rss/rssmanager.cpp

30
src/rss/rss_imp.cpp

@ -99,7 +99,7 @@ void RSSImp::displayItemsListMenu(const QPoint&){
foreach(QTreeWidgetItem *item, selectedItems) { foreach(QTreeWidgetItem *item, selectedItems) {
qDebug("text(3) URL: %s", qPrintable(item->text(NEWS_URL_COL))); qDebug("text(3) URL: %s", qPrintable(item->text(NEWS_URL_COL)));
qDebug("text(2) TITLE: %s", qPrintable(item->text(NEWS_TITLE_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))->has_attachment()) { if(listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID))->hasAttachment()) {
has_attachment = true; has_attachment = true;
break; break;
} }
@ -309,10 +309,10 @@ void RSSImp::downloadTorrent() {
QList<QTreeWidgetItem *> selected_items = listNews->selectedItems(); QList<QTreeWidgetItem *> selected_items = listNews->selectedItems();
foreach(const QTreeWidgetItem* item, selected_items) { foreach(const QTreeWidgetItem* item, selected_items) {
RssArticle* article = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)); RssArticle* article = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID));
if(article->has_attachment()) { if(article->hasAttachment()) {
QBtSession::instance()->downloadFromUrl(article->getTorrentUrl()); QBtSession::instance()->downloadFromUrl(article->torrentUrl());
} else { } else {
QBtSession::instance()->downloadFromUrl(article->getLink()); QBtSession::instance()->downloadFromUrl(article->link());
} }
} }
} }
@ -322,7 +322,7 @@ void RSSImp::openNewsUrl() {
QList<QTreeWidgetItem *> selected_items = listNews->selectedItems(); QList<QTreeWidgetItem *> selected_items = listNews->selectedItems();
foreach(const QTreeWidgetItem* item, selected_items) { foreach(const QTreeWidgetItem* item, selected_items) {
RssArticle* news = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID)); RssArticle* news = listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID));
QString link = news->getLink(); QString link = news->link();
if(!link.isEmpty()) if(!link.isEmpty())
QDesktopServices::openUrl(QUrl(link)); QDesktopServices::openUrl(QUrl(link));
} }
@ -454,9 +454,9 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* item) {
qDebug("Got the list of news"); qDebug("Got the list of news");
foreach(RssArticle* article, news){ foreach(RssArticle* article, news){
QTreeWidgetItem* it = new QTreeWidgetItem(listNews); QTreeWidgetItem* it = new QTreeWidgetItem(listNews);
it->setText(NEWS_TITLE_COL, article->getTitle()); it->setText(NEWS_TITLE_COL, article->title());
it->setText(NEWS_URL_COL, article->getParent()->getUrl()); it->setText(NEWS_URL_COL, article->parent()->getUrl());
it->setText(NEWS_ID, article->getId()); it->setText(NEWS_ID, article->guid());
if(article->isRead()){ if(article->isRead()){
it->setData(NEWS_TITLE_COL, Qt::ForegroundRole, QVariant(QColor("grey"))); it->setData(NEWS_TITLE_COL, Qt::ForegroundRole, QVariant(QColor("grey")));
it->setData(NEWS_ICON, Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png"))); it->setData(NEWS_ICON, Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png")));
@ -492,17 +492,17 @@ void RSSImp::refreshTextBrowser() {
RssArticle* article = stream->getItem(item->text(NEWS_ID)); RssArticle* article = stream->getItem(item->text(NEWS_ID));
QString html; QString html;
html += "<div style='border: 2px solid red; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;'>"; html += "<div style='border: 2px solid red; margin-left: 5px; margin-right: 5px; margin-bottom: 5px;'>";
html += "<div style='background-color: #678db2; font-weight: bold; color: #fff;'>"+article->getTitle() + "</div>"; html += "<div style='background-color: #678db2; font-weight: bold; color: #fff;'>"+article->title() + "</div>";
if(article->getDate().isValid()) { if(article->date().isValid()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Date: ")+"</b>"+article->getDate().toLocalTime().toString(Qt::SystemLocaleLongDate)+"</div>"; html += "<div style='background-color: #efefef;'><b>"+tr("Date: ")+"</b>"+article->date().toLocalTime().toString(Qt::SystemLocaleLongDate)+"</div>";
} }
if(!article->getAuthor().isEmpty()) { if(!article->author().isEmpty()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Author: ")+"</b>"+article->getAuthor()+"</div>"; html += "<div style='background-color: #efefef;'><b>"+tr("Author: ")+"</b>"+article->author()+"</div>";
} }
html += "</div>"; html += "</div>";
html += "<divstyle='margin-left: 5px; margin-right: 5px;'>"+article->getDescription()+"</div>"; html += "<divstyle='margin-left: 5px; margin-right: 5px;'>"+article->description()+"</div>";
textBrowser->setHtml(html); textBrowser->setHtml(html);
article->setRead(); article->markAsRead();
item->setData(NEWS_TITLE_COL, Qt::ForegroundRole, QVariant(QColor("grey"))); item->setData(NEWS_TITLE_COL, Qt::ForegroundRole, QVariant(QColor("grey")));
item->setData(0, Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png"))); item->setData(0, Qt::DecorationRole, QVariant(QIcon(":/Icons/sphere.png")));
// Decrement feed nb unread news // Decrement feed nb unread news

113
src/rss/rssarticle.cpp

@ -190,11 +190,7 @@ QDateTime RssArticle::parseDate(const QString &string) {
} }
// public constructor // public constructor
RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml): parent(parent), read(false) { RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml): m_parent(parent), m_read(false) {
is_valid = false;
torrent_url = QString::null;
news_link = QString::null;
title = QString::null;
while(!xml.atEnd()) { while(!xml.atEnd()) {
xml.readNext(); xml.readNext();
@ -203,107 +199,108 @@ RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml): parent(parent),
if(xml.isStartElement()) { if(xml.isStartElement()) {
if(xml.name() == "title") { if(xml.name() == "title") {
title = xml.readElementText(); m_title = xml.readElementText();
} }
else if(xml.name() == "enclosure") { else if(xml.name() == "enclosure") {
if(xml.attributes().value("type") == "application/x-bittorrent") { if(xml.attributes().value("type") == "application/x-bittorrent") {
torrent_url = xml.attributes().value("url").toString(); m_torrentUrl = xml.attributes().value("url").toString();
} }
} }
else if(xml.name() == "link") { else if(xml.name() == "link") {
news_link = xml.readElementText(); m_link = xml.readElementText();
if(id.isEmpty()) if(m_guid.isEmpty())
id = news_link; m_guid = m_link;
} }
else if(xml.name() == "description") { else if(xml.name() == "description") {
description = xml.readElementText(); m_description = xml.readElementText();
} }
else if(xml.name() == "pubDate") { else if(xml.name() == "pubDate") {
date = parseDate(xml.readElementText()); m_date = parseDate(xml.readElementText());
} }
else if(xml.name() == "author") { else if(xml.name() == "author") {
author = xml.readElementText(); m_author = xml.readElementText();
} }
else if(xml.name() == "guid") { else if(xml.name() == "guid") {
id = xml.readElementText(); m_guid = xml.readElementText();
} }
} }
} }
if(!id.isEmpty())
is_valid = true;
} }
RssArticle::RssArticle(RssFeed* parent, QString _id, QString _title, QString _torrent_url, QString _news_link, QString _description, QDateTime _date, QString _author, bool _read): RssArticle::RssArticle(RssFeed* parent, const QString &guid):
parent(parent), id(_id), title(_title), torrent_url(_torrent_url), news_link(_news_link), description(_description), date(_date), author(_author), read(_read){ m_parent(parent), m_guid(guid) {
if(id.isEmpty())
id = news_link;
if(!id.isEmpty()) {
is_valid = true;
} else {
std::cerr << "ERROR: an invalid RSS item was saved" << std::endl;
is_valid = false;
}
} }
RssArticle::~RssArticle(){ RssArticle::~RssArticle(){
} }
bool RssArticle::has_attachment() const { bool RssArticle::hasAttachment() const {
return !torrent_url.isEmpty(); return !m_torrentUrl.isEmpty();
} }
QHash<QString, QVariant> RssArticle::toHash() const { QVariantHash RssArticle::toHash() const {
QHash<QString, QVariant> item; QVariantHash item;
item["title"] = title; item["title"] = m_title;
item["id"] = id; item["id"] = m_guid;
item["torrent_url"] = torrent_url; item["torrent_url"] = m_torrentUrl;
item["news_link"] = news_link; item["news_link"] = m_link;
item["description"] = description; item["description"] = m_description;
item["date"] = date; item["date"] = m_date;
item["author"] = author; item["author"] = m_author;
item["read"] = read; item["read"] = m_read;
return item; return item;
} }
RssArticle* RssArticle::fromHash(RssFeed* parent, const QHash<QString, QVariant> &h) { RssArticle* hashToRssArticle(RssFeed* parent, const QVariantHash &h) {
return new RssArticle(parent, h.value("id", "").toString(), h["title"].toString(), h["torrent_url"].toString(), h["news_link"].toString(), const QString guid = h.value("id").toString();
h["description"].toString(), h["date"].toDateTime(), h["author"].toString(), h["read"].toBool()); 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();
Q_ASSERT(art->isValid());
return art;
} }
RssFeed* RssArticle::getParent() const { RssFeed* RssArticle::parent() const {
return parent; return m_parent;
} }
bool RssArticle::isValid() const { bool RssArticle::isValid() const {
return is_valid; return !m_guid.isEmpty();
} }
QString RssArticle::getAuthor() const { QString RssArticle::author() const {
return author; return m_author;
} }
QString RssArticle::getTorrentUrl() const{ QString RssArticle::torrentUrl() const{
return torrent_url; return m_torrentUrl;
} }
QString RssArticle::getLink() const { QString RssArticle::link() const {
return news_link; return m_link;
} }
QString RssArticle::getDescription() const{ QString RssArticle::description() const{
if(description.isEmpty()) if(m_description.isEmpty())
return tr("No description available"); return tr("No description available");
return description; return m_description;
} }
QDateTime RssArticle::getDate() const { QDateTime RssArticle::date() const {
return date; return m_date;
} }
bool RssArticle::isRead() const{ bool RssArticle::isRead() const{
return read; return m_read;
} }
void RssArticle::setRead(){ void RssArticle::markAsRead(){
read = true; m_read = true;
} }

53
src/rss/rssarticle.h

@ -33,7 +33,7 @@
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QDateTime> #include <QDateTime>
#include <QHash> #include <QVariantHash>
class RssFeed; class RssFeed;
@ -43,40 +43,39 @@ class RssArticle: public QObject {
public: public:
RssArticle(RssFeed* parent, QXmlStreamReader& xml); RssArticle(RssFeed* parent, QXmlStreamReader& xml);
RssArticle(RssFeed* parent, QString _id, QString _title, RssArticle(RssFeed* parent, const QString &guid);
QString _torrent_url, QString _news_link, QString _description,
QDateTime _date, QString _author, bool _read);
~RssArticle(); ~RssArticle();
bool has_attachment() const; // Accessors
inline QString getId() const { return id; }
QHash<QString, QVariant> toHash() const;
static RssArticle* fromHash(RssFeed* parent, const QHash<QString, QVariant> &h);
RssFeed* getParent() const;
bool isValid() const; bool isValid() const;
inline QString getTitle() const { return title; } bool hasAttachment() const;
QString getAuthor() const; inline QString guid() const { return m_guid; }
QString getTorrentUrl() const; RssFeed* parent() const;
QString getLink() const; inline QString title() const { return m_title; }
QString getDescription() const; QString author() const;
QDateTime getDate() const; QString torrentUrl() const;
QString link() const;
QString description() const;
QDateTime date() const;
bool isRead() const; bool isRead() const;
void setRead(); // Setters
void markAsRead();
// Serialization
QVariantHash toHash() const;
friend RssArticle* hashToRssArticle(RssFeed* parent, const QVariantHash &hash);
protected: protected:
QDateTime parseDate(const QString &string); QDateTime parseDate(const QString &string);
private: private:
RssFeed* parent; RssFeed* m_parent;
QString id; QString m_guid;
QString title; QString m_title;
QString torrent_url; QString m_torrentUrl;
QString news_link; QString m_link;
QString description; QString m_description;
QDateTime date; QDateTime m_date;
QString author; QString m_author;
bool is_valid; bool m_read;
bool read;
}; };
#endif // RSSARTICLE_H #endif // RSSARTICLE_H

4
src/rss/rssdownloadrule.cpp

@ -130,8 +130,8 @@ QStringList RssDownloadRule::findMatchingArticles(const RssFeed *feed) const
{ {
QStringList ret; QStringList ret;
foreach(const RssArticle *art, feed->values()) { foreach(const RssArticle *art, feed->values()) {
if(matches(art->getTitle())) if(matches(art->title()))
ret << art->getTitle(); ret << art->title();
} }
return ret; return ret;
} }

30
src/rss/rssfeed.cpp

@ -42,15 +42,13 @@ RssFeed::RssFeed(RssFolder* parent, QString _url): parent(parent), alias(""), ic
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
url = QUrl(_url).toString(); url = QUrl(_url).toString();
QHash<QString, QVariant> all_old_items = qBTRSS.value("old_items", QHash<QString, QVariant>()).toHash(); QHash<QString, QVariant> all_old_items = qBTRSS.value("old_items", QHash<QString, QVariant>()).toHash();
QVariantList old_items = all_old_items.value(url, QVariantList()).toList(); const QVariantList old_items = all_old_items.value(url, QVariantList()).toList();
qDebug("Loading %d old items for feed %s", old_items.size(), getName().toLocal8Bit().data()); qDebug("Loading %d old items for feed %s", old_items.size(), getName().toLocal8Bit().data());
foreach(const QVariant &var_it, old_items) { foreach(const QVariant &var_it, old_items) {
QHash<QString, QVariant> item = var_it.toHash(); QHash<QString, QVariant> item = var_it.toHash();
RssArticle *rss_item = RssArticle::fromHash(this, item); RssArticle *rss_item = hashToRssArticle(this, item);
if(rss_item->isValid()) { if(rss_item) {
(*this)[rss_item->getId()] = rss_item; insert(rss_item->guid(), rss_item);
} else {
delete rss_item;
} }
} }
} }
@ -184,7 +182,7 @@ unsigned int RssFeed::getNbNews() const{
void RssFeed::markAllAsRead() { void RssFeed::markAllAsRead() {
foreach(RssArticle *item, this->values()){ foreach(RssArticle *item, this->values()){
item->setRead(); item->markAsRead();
} }
RssManager::instance()->forwardFeedInfosChanged(url, getName(), 0); RssManager::instance()->forwardFeedInfosChanged(url, getName(), 0);
} }
@ -275,8 +273,8 @@ short RssFeed::readDoc(QIODevice* device) {
} }
else if(xml.name() == "item") { else if(xml.name() == "item") {
RssArticle * item = new RssArticle(this, xml); RssArticle * item = new RssArticle(this, xml);
if(item->isValid() && !itemAlreadyExists(item->getId())) { if(item->isValid() && !itemAlreadyExists(item->guid())) {
this->insert(item->getId(), item); this->insert(item->guid(), item);
} else { } else {
delete item; delete item;
} }
@ -294,18 +292,18 @@ short RssFeed::readDoc(QIODevice* device) {
foreach(RssArticle* item, values()) { foreach(RssArticle* item, values()) {
if(item->isRead()) continue; if(item->isRead()) continue;
QString torrent_url; QString torrent_url;
if(item->has_attachment()) if(item->hasAttachment())
torrent_url = item->getTorrentUrl(); torrent_url = item->torrentUrl();
else else
torrent_url = item->getLink(); torrent_url = item->link();
// Check if the item should be automatically downloaded // Check if the item should be automatically downloaded
const RssDownloadRule matching_rule = RssDownloadRuleList::instance()->findMatchingRule(url, item->getTitle()); const RssDownloadRule matching_rule = RssDownloadRuleList::instance()->findMatchingRule(url, item->title());
if(matching_rule.isValid()) { if(matching_rule.isValid()) {
// Download the torrent // Download the torrent
QBtSession::instance()->addConsoleMessage(tr("Automatically downloading %1 torrent from %2 RSS feed...").arg(item->getTitle()).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()); QBtSession::instance()->downloadUrlAndSkipDialog(torrent_url, matching_rule.savePath(), matching_rule.label());
// Item was downloaded, consider it as Read // Item was downloaded, consider it as Read
item->setRead(); item->markAsRead();
} }
} }
} }
@ -320,7 +318,7 @@ void RssFeed::resizeList() {
const int excess = nb_articles - max_articles; const int excess = nb_articles - max_articles;
for(int i=0; i<excess; ++i){ for(int i=0; i<excess; ++i){
RssArticle *lastItem = listItem.takeLast(); RssArticle *lastItem = listItem.takeLast();
delete this->take(lastItem->getId()); delete this->take(lastItem->guid());
} }
} }
} }

2
src/rss/rssmanager.cpp

@ -128,7 +128,7 @@ void RssManager::saveStreamList(){
void RssManager::insertSortElem(QList<RssArticle*> &list, RssArticle *item) { void RssManager::insertSortElem(QList<RssArticle*> &list, RssArticle *item) {
int i = 0; int i = 0;
while(i < list.size() && item->getDate() < list.at(i)->getDate()) { while(i < list.size() && item->date() < list.at(i)->date()) {
++i; ++i;
} }
list.insert(i, item); list.insert(i, item);

Loading…
Cancel
Save