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&){ @@ -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))->has_attachment()) {
if(listStreams->getRSSItemFromUrl(item->text(NEWS_URL_COL))->getItem(item->text(NEWS_ID))->hasAttachment()) {
has_attachment = true;
break;
}
@ -309,10 +309,10 @@ void RSSImp::downloadTorrent() { @@ -309,10 +309,10 @@ void RSSImp::downloadTorrent() {
QList<QTreeWidgetItem *> 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->has_attachment()) {
QBtSession::instance()->downloadFromUrl(article->getTorrentUrl());
if(article->hasAttachment()) {
QBtSession::instance()->downloadFromUrl(article->torrentUrl());
} else {
QBtSession::instance()->downloadFromUrl(article->getLink());
QBtSession::instance()->downloadFromUrl(article->link());
}
}
}
@ -322,7 +322,7 @@ void RSSImp::openNewsUrl() { @@ -322,7 +322,7 @@ void RSSImp::openNewsUrl() {
QList<QTreeWidgetItem *> 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->getLink();
QString link = news->link();
if(!link.isEmpty())
QDesktopServices::openUrl(QUrl(link));
}
@ -454,9 +454,9 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* item) { @@ -454,9 +454,9 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* item) {
qDebug("Got the list of news");
foreach(RssArticle* article, news){
QTreeWidgetItem* it = new QTreeWidgetItem(listNews);
it->setText(NEWS_TITLE_COL, article->getTitle());
it->setText(NEWS_URL_COL, article->getParent()->getUrl());
it->setText(NEWS_ID, article->getId());
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")));
@ -492,17 +492,17 @@ void RSSImp::refreshTextBrowser() { @@ -492,17 +492,17 @@ void RSSImp::refreshTextBrowser() {
RssArticle* article = stream->getItem(item->text(NEWS_ID));
QString html;
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>";
if(article->getDate().isValid()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Date: ")+"</b>"+article->getDate().toLocalTime().toString(Qt::SystemLocaleLongDate)+"</div>";
html += "<div style='background-color: #678db2; font-weight: bold; color: #fff;'>"+article->title() + "</div>";
if(article->date().isValid()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Date: ")+"</b>"+article->date().toLocalTime().toString(Qt::SystemLocaleLongDate)+"</div>";
}
if(!article->getAuthor().isEmpty()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Author: ")+"</b>"+article->getAuthor()+"</div>";
if(!article->author().isEmpty()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Author: ")+"</b>"+article->author()+"</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);
article->setRead();
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

113
src/rss/rssarticle.cpp

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

53
src/rss/rssarticle.h

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

4
src/rss/rssdownloadrule.cpp

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

30
src/rss/rssfeed.cpp

@ -42,15 +42,13 @@ RssFeed::RssFeed(RssFolder* parent, QString _url): parent(parent), alias(""), ic @@ -42,15 +42,13 @@ RssFeed::RssFeed(RssFolder* parent, QString _url): parent(parent), alias(""), ic
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
url = QUrl(_url).toString();
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());
foreach(const QVariant &var_it, old_items) {
QHash<QString, QVariant> item = var_it.toHash();
RssArticle *rss_item = RssArticle::fromHash(this, item);
if(rss_item->isValid()) {
(*this)[rss_item->getId()] = rss_item;
} else {
delete rss_item;
RssArticle *rss_item = hashToRssArticle(this, item);
if(rss_item) {
insert(rss_item->guid(), rss_item);
}
}
}
@ -184,7 +182,7 @@ unsigned int RssFeed::getNbNews() const{ @@ -184,7 +182,7 @@ unsigned int RssFeed::getNbNews() const{
void RssFeed::markAllAsRead() {
foreach(RssArticle *item, this->values()){
item->setRead();
item->markAsRead();
}
RssManager::instance()->forwardFeedInfosChanged(url, getName(), 0);
}
@ -275,8 +273,8 @@ short RssFeed::readDoc(QIODevice* device) { @@ -275,8 +273,8 @@ short RssFeed::readDoc(QIODevice* device) {
}
else if(xml.name() == "item") {
RssArticle * item = new RssArticle(this, xml);
if(item->isValid() && !itemAlreadyExists(item->getId())) {
this->insert(item->getId(), item);
if(item->isValid() && !itemAlreadyExists(item->guid())) {
this->insert(item->guid(), item);
} else {
delete item;
}
@ -294,18 +292,18 @@ short RssFeed::readDoc(QIODevice* device) { @@ -294,18 +292,18 @@ short RssFeed::readDoc(QIODevice* device) {
foreach(RssArticle* item, values()) {
if(item->isRead()) continue;
QString torrent_url;
if(item->has_attachment())
torrent_url = item->getTorrentUrl();
if(item->hasAttachment())
torrent_url = item->torrentUrl();
else
torrent_url = item->getLink();
torrent_url = item->link();
// 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()) {
// 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());
// Item was downloaded, consider it as Read
item->setRead();
item->markAsRead();
}
}
}
@ -320,7 +318,7 @@ void RssFeed::resizeList() { @@ -320,7 +318,7 @@ void RssFeed::resizeList() {
const int excess = nb_articles - max_articles;
for(int i=0; i<excess; ++i){
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(){ @@ -128,7 +128,7 @@ void RssManager::saveStreamList(){
void RssManager::insertSortElem(QList<RssArticle*> &list, RssArticle *item) {
int i = 0;
while(i < list.size() && item->getDate() < list.at(i)->getDate()) {
while(i < list.size() && item->date() < list.at(i)->date()) {
++i;
}
list.insert(i, item);

Loading…
Cancel
Save