Browse Source

RSS code clean up

adaptive-webui-19844
Christophe Dumez 12 years ago
parent
commit
d7c36c9c3c
  1. 19
      src/rss/rssarticle.cpp
  2. 18
      src/rss/rssarticle.h
  3. 126
      src/rss/rssfeed.cpp
  4. 3
      src/rss/rssfeed.h
  5. 6
      src/rss/rssmanager.cpp
  6. 1
      src/rss/rssmanager.h

19
src/rss/rssarticle.cpp

@ -36,7 +36,7 @@
#include "rssfeed.h" #include "rssfeed.h"
// public constructor // public constructor
RssArticle::RssArticle(RssFeed* parent, const QString &guid): RssArticle::RssArticle(RssFeed* parent, const QString& guid):
m_parent(parent), m_guid(guid), m_read(false) {} m_parent(parent), m_guid(guid), m_read(false) {}
bool RssArticle::hasAttachment() const { bool RssArticle::hasAttachment() const {
@ -56,9 +56,10 @@ QVariantHash RssArticle::toHash() const {
return item; return item;
} }
RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash &h) { RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash& h) {
const QString guid = h.value("id").toString(); const QString guid = h.value("id").toString();
if (guid.isEmpty()) return RssArticlePtr(); if (guid.isEmpty())
return RssArticlePtr();
RssArticlePtr art(new RssArticle(parent, guid)); RssArticlePtr art(new RssArticle(parent, guid));
art->m_title = h.value("title", "").toString(); art->m_title = h.value("title", "").toString();
@ -76,25 +77,25 @@ RssFeed* RssArticle::parent() const {
return m_parent; return m_parent;
} }
QString RssArticle::author() const { const QString& RssArticle::author() const {
return m_author; return m_author;
} }
QString RssArticle::torrentUrl() const { const QString& RssArticle::torrentUrl() const {
return m_torrentUrl.isEmpty() ? m_link : m_torrentUrl; return m_torrentUrl.isEmpty() ? m_link : m_torrentUrl;
} }
QString RssArticle::link() const { const QString& RssArticle::link() const {
return m_link; return m_link;
} }
QString RssArticle::description() const { const QString& RssArticle::description() const {
if (m_description.isNull()) if (m_description.isNull())
return ""; return "";
return m_description; return m_description;
} }
QDateTime RssArticle::date() const { const QDateTime& RssArticle::date() const {
return m_date; return m_date;
} }
@ -115,7 +116,7 @@ const QString& RssArticle::guid() const
return m_guid; return m_guid;
} }
QString RssArticle::title() const const QString& RssArticle::title() const
{ {
return m_title; return m_title;
} }

18
src/rss/rssarticle.h

@ -45,24 +45,24 @@ typedef QSharedPointer<RssArticle> RssArticlePtr;
class RssArticle { class RssArticle {
public: public:
RssArticle(RssFeed* parent, const QString &guid); RssArticle(RssFeed* parent, const QString& guid);
// Accessors // Accessors
bool hasAttachment() const; bool hasAttachment() const;
const QString& guid() const; const QString& guid() const;
RssFeed* parent() const; RssFeed* parent() const;
QString title() const; const QString& title() const;
QString author() const; const QString& author() const;
QString torrentUrl() const; const QString& torrentUrl() const;
QString link() const; const QString& link() const;
QString description() const; const QString& description() const;
QDateTime date() const; const QDateTime& date() const;
bool isRead() const; bool isRead() const;
// Setters // Setters
void markAsRead(); void markAsRead();
// Serialization // Serialization
QVariantHash toHash() const; QVariantHash toHash() const;
friend RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash &hash); friend RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash& hash);
private: private:
RssFeed* m_parent; RssFeed* m_parent;
@ -76,6 +76,6 @@ private:
bool m_read; bool m_read;
}; };
RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash &hash); RssArticlePtr hashToRssArticle(RssFeed* parent, const QVariantHash& hash);
#endif // RSSARTICLE_H #endif // RSSARTICLE_H

126
src/rss/rssfeed.cpp

@ -46,11 +46,17 @@ bool rssArticleDateRecentThan(const RssArticlePtr& left, const RssArticlePtr& ri
return left->date() > right->date(); return left->date() > right->date();
} }
RssFeed::RssFeed(RssManager* manager, RssFolder* parent, const QString &url): RssFeed::RssFeed(RssManager* manager, RssFolder* parent, const QString& url):
m_manager(manager), m_parent(parent), m_icon(":/Icons/oxygen/application-rss+xml.png"), m_manager(manager),
m_unreadCount(0), m_dirty(false), m_inErrorState(false), m_loading(false) { m_parent(parent),
qDebug() << Q_FUNC_INFO << url; m_url (QUrl::fromEncoded(url.toUtf8()).toString()),
m_url = QUrl::fromEncoded(url.toUtf8()).toString(); m_icon(":/Icons/oxygen/application-rss+xml.png"),
m_unreadCount(0),
m_dirty(false),
m_inErrorState(false),
m_loading(false)
{
qDebug() << Q_FUNC_INFO << m_url;
// Listen for new RSS downloads // Listen for new RSS downloads
connect(manager->rssDownloader(), SIGNAL(downloadFinished(QString,QString)), SLOT(handleFinishedDownload(QString,QString))); connect(manager->rssDownloader(), SIGNAL(downloadFinished(QString,QString)), SLOT(handleFinishedDownload(QString,QString)));
connect(manager->rssDownloader(), SIGNAL(downloadFailure(QString,QString)), SLOT(handleDownloadFailure(QString,QString))); connect(manager->rssDownloader(), SIGNAL(downloadFailure(QString,QString)), SLOT(handleDownloadFailure(QString,QString)));
@ -66,12 +72,14 @@ RssFeed::RssFeed(RssManager* manager, RssFolder* parent, const QString &url):
loadItemsFromDisk(); loadItemsFromDisk();
} }
RssFeed::~RssFeed() { RssFeed::~RssFeed()
{
if (!m_icon.startsWith(":/") && QFile::exists(m_icon)) if (!m_icon.startsWith(":/") && QFile::exists(m_icon))
fsutils::forceRemove(m_icon); fsutils::forceRemove(m_icon);
} }
void RssFeed::saveItemsToDisk() { void RssFeed::saveItemsToDisk()
{
qDebug() << Q_FUNC_INFO << m_url; qDebug() << Q_FUNC_INFO << m_url;
if (!m_dirty) if (!m_dirty)
return; return;
@ -80,25 +88,26 @@ void RssFeed::saveItemsToDisk() {
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
QVariantList old_items; QVariantList old_items;
RssArticleHash::ConstIterator it=m_articles.begin(); RssArticleHash::ConstIterator it = m_articles.begin();
RssArticleHash::ConstIterator itend=m_articles.end(); RssArticleHash::ConstIterator itend = m_articles.end();
for ( ; it != itend; ++it) { for ( ; it != itend; ++it) {
old_items << it.value()->toHash(); old_items << it.value()->toHash();
} }
qDebug("Saving %d old items for feed %s", old_items.size(), displayName().toLocal8Bit().data()); qDebug("Saving %d old items for feed %s", old_items.size(), qPrintable(displayName()));
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();
all_old_items[m_url] = old_items; all_old_items[m_url] = old_items;
qBTRSS.setValue("old_items", all_old_items); qBTRSS.setValue("old_items", all_old_items);
} }
void RssFeed::loadItemsFromDisk() { void RssFeed::loadItemsFromDisk()
{
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
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();
const QVariantList old_items = all_old_items.value(m_url, QVariantList()).toList(); const QVariantList old_items = all_old_items.value(m_url, QVariantList()).toList();
qDebug("Loading %d old items for feed %s", old_items.size(), displayName().toLocal8Bit().data()); qDebug("Loading %d old items for feed %s", old_items.size(), qPrintable(displayName()));
foreach (const QVariant &var_it, old_items) { foreach (const QVariant& var_it, old_items) {
QHash<QString, QVariant> item = var_it.toHash(); QVariantHash item = var_it.toHash();
RssArticlePtr rss_item = hashToRssArticle(this, item); RssArticlePtr rss_item = hashToRssArticle(this, item);
if (rss_item) if (rss_item)
addArticle(rss_item); addArticle(rss_item);
@ -127,9 +136,10 @@ void RssFeed::addArticle(const RssArticlePtr& article)
} }
} }
bool RssFeed::refresh() { bool RssFeed::refresh()
{
if (m_loading) { if (m_loading) {
qWarning() << Q_FUNC_INFO << "Feed" << this->displayName() << "is already being refreshed, ignoring request"; qWarning() << Q_FUNC_INFO << "Feed" << displayName() << "is already being refreshed, ignoring request";
return false; return false;
} }
m_loading = true; m_loading = true;
@ -138,45 +148,46 @@ bool RssFeed::refresh() {
return true; return true;
} }
void RssFeed::removeAllSettings() { void RssFeed::removeAllSettings()
{
qDebug() << "Removing all settings / history for feed: " << m_url; qDebug() << "Removing all settings / history for feed: " << m_url;
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
QHash<QString, QVariant> feeds_w_downloader = qBTRSS.value("downloader_on", QHash<QString, QVariant>()).toHash(); QVariantHash feeds_w_downloader = qBTRSS.value("downloader_on", QVariantHash()).toHash();
if (feeds_w_downloader.contains(m_url)) { if (feeds_w_downloader.contains(m_url)) {
feeds_w_downloader.remove(m_url); feeds_w_downloader.remove(m_url);
qBTRSS.setValue("downloader_on", feeds_w_downloader); qBTRSS.setValue("downloader_on", feeds_w_downloader);
} }
QHash<QString, QVariant> all_feeds_filters = qBTRSS.value("feed_filters", QHash<QString, QVariant>()).toHash(); QVariantHash all_feeds_filters = qBTRSS.value("feed_filters", QVariantHash()).toHash();
if (all_feeds_filters.contains(m_url)) { if (all_feeds_filters.contains(m_url)) {
all_feeds_filters.remove(m_url); all_feeds_filters.remove(m_url);
qBTRSS.setValue("feed_filters", all_feeds_filters); qBTRSS.setValue("feed_filters", all_feeds_filters);
} }
QHash<QString, QVariant> all_old_items = qBTRSS.value("old_items", QHash<QString, QVariant>()).toHash(); QVariantHash all_old_items = qBTRSS.value("old_items", QVariantHash()).toHash();
if (all_old_items.contains(m_url)) { if (all_old_items.contains(m_url)) {
all_old_items.remove(m_url); all_old_items.remove(m_url);
qBTRSS.setValue("old_items", all_old_items); qBTRSS.setValue("old_items", all_old_items);
} }
} }
void RssFeed::setLoading(bool val) { bool RssFeed::isLoading() const
m_loading = val; {
}
bool RssFeed::isLoading() const {
return m_loading; return m_loading;
} }
QString RssFeed::title() const { QString RssFeed::title() const
{
return m_title; return m_title;
} }
void RssFeed::rename(const QString &new_name) { void RssFeed::rename(const QString &new_name)
{
qDebug() << "Renaming stream to" << new_name; qDebug() << "Renaming stream to" << new_name;
m_alias = new_name; m_alias = new_name;
} }
// Return the alias if the stream has one, the url if it has no alias // Return the alias if the stream has one, the url if it has no alias
QString RssFeed::displayName() const { QString RssFeed::displayName() const
{
if (!m_alias.isEmpty()) if (!m_alias.isEmpty())
return m_alias; return m_alias;
if (!m_title.isEmpty()) if (!m_title.isEmpty())
@ -184,7 +195,8 @@ QString RssFeed::displayName() const {
return m_url; return m_url;
} }
QString RssFeed::url() const { QString RssFeed::url() const
{
return m_url; return m_url;
} }
@ -192,29 +204,37 @@ QIcon RssFeed::icon() const
{ {
if (m_inErrorState) if (m_inErrorState)
return QIcon(":/Icons/oxygen/unavailable.png"); return QIcon(":/Icons/oxygen/unavailable.png");
return QIcon(m_icon); return QIcon(m_icon);
} }
bool RssFeed::hasCustomIcon() const { bool RssFeed::hasCustomIcon() const
{
return !m_icon.startsWith(":/"); return !m_icon.startsWith(":/");
} }
void RssFeed::setIconPath(const QString &path) { void RssFeed::setIconPath(const QString& path)
if (path.isEmpty() || !QFile::exists(path)) return; {
if (path.isEmpty() || !QFile::exists(path))
return;
m_icon = path; m_icon = path;
} }
RssArticlePtr RssFeed::getItem(const QString &guid) const { RssArticlePtr RssFeed::getItem(const QString& guid) const
{
return m_articles.value(guid); return m_articles.value(guid);
} }
uint RssFeed::count() const { uint RssFeed::count() const
{
return m_articles.size(); return m_articles.size();
} }
void RssFeed::markAsRead() { void RssFeed::markAsRead()
RssArticleHash::ConstIterator it=m_articles.begin(); {
RssArticleHash::ConstIterator itend=m_articles.end(); RssArticleHash::ConstIterator it = m_articles.begin();
RssArticleHash::ConstIterator itend = m_articles.end();
for ( ; it != itend; ++it) { for ( ; it != itend; ++it) {
it.value()->markAsRead(); it.value()->markAsRead();
} }
@ -227,11 +247,13 @@ uint RssFeed::unreadCount() const
return m_unreadCount; return m_unreadCount;
} }
RssArticleList RssFeed::articleListByDateDesc() const { RssArticleList RssFeed::articleListByDateDesc() const
{
return m_articlesByDate; return m_articlesByDate;
} }
RssArticleList RssFeed::unreadArticleListByDateDesc() const { RssArticleList RssFeed::unreadArticleListByDateDesc() const
{
RssArticleList unread_news; RssArticleList unread_news;
RssArticleList::ConstIterator it = m_articlesByDate.begin(); RssArticleList::ConstIterator it = m_articlesByDate.begin();
@ -244,30 +266,34 @@ RssArticleList RssFeed::unreadArticleListByDateDesc() const {
} }
// download the icon from the adress // download the icon from the adress
QString RssFeed::iconUrl() const { QString RssFeed::iconUrl() const
{
// XXX: This works for most sites but it is not perfect // XXX: This works for most sites but it is not perfect
return QString("http://")+QUrl(m_url).host()+QString("/favicon.ico"); return QString("http://") + QUrl(m_url).host() + QString("/favicon.ico");
} }
// read and store the downloaded rss' informations // read and store the downloaded rss' informations
void RssFeed::handleFinishedDownload(const QString& url, const QString &file_path) { void RssFeed::handleFinishedDownload(const QString& url, const QString& filePath)
{
if (url == m_url) { if (url == m_url) {
qDebug() << Q_FUNC_INFO << "Successfully downloaded RSS feed at" << url; qDebug() << Q_FUNC_INFO << "Successfully downloaded RSS feed at" << url;
// Parse the download RSS // Parse the download RSS
m_manager->rssParser()->parseRssFile(m_url, file_path); m_manager->rssParser()->parseRssFile(m_url, filePath);
} } else if (url == m_iconUrl) {
else if (url == m_iconUrl) { m_icon = filePath;
m_icon = file_path;
qDebug() << Q_FUNC_INFO << "icon path:" << m_icon; qDebug() << Q_FUNC_INFO << "icon path:" << m_icon;
m_manager->forwardFeedIconChanged(m_url, m_icon); // XXX: Ugly m_manager->forwardFeedIconChanged(m_url, m_icon);
} }
} }
void RssFeed::handleDownloadFailure(const QString &url, const QString& error) { void RssFeed::handleDownloadFailure(const QString& url, const QString& error)
if (url != m_url) return; {
if (url != m_url)
return;
m_inErrorState = true; m_inErrorState = true;
m_loading = false; m_loading = false;
m_manager->forwardFeedInfosChanged(m_url, displayName(), m_unreadCount); // XXX: Ugly m_manager->forwardFeedInfosChanged(m_url, displayName(), m_unreadCount);
qWarning() << "Failed to download RSS feed at" << url; qWarning() << "Failed to download RSS feed at" << url;
qWarning() << "Reason:" << error; qWarning() << "Reason:" << error;
} }

3
src/rss/rssfeed.h

@ -51,7 +51,7 @@ class RssFeed: public QObject, public RssFile {
Q_OBJECT Q_OBJECT
public: public:
RssFeed(RssManager* manager, RssFolder* m_parent, const QString &url); RssFeed(RssManager* manager, RssFolder* m_parent, const QString& url);
virtual ~RssFeed(); virtual ~RssFeed();
virtual RssFolder* parent() const { return m_parent; } virtual RssFolder* parent() const { return m_parent; }
virtual void setParent(RssFolder* parent) { m_parent = parent; } virtual void setParent(RssFolder* parent) { m_parent = parent; }
@ -59,7 +59,6 @@ public:
virtual QString id() const { return m_url; } virtual QString id() const { return m_url; }
virtual void removeAllSettings(); virtual void removeAllSettings();
virtual void saveItemsToDisk(); virtual void saveItemsToDisk();
void setLoading(bool val);
bool isLoading() const; bool isLoading() const;
QString title() const; QString title() const;
virtual void rename(const QString &alias); virtual void rename(const QString &alias);

6
src/rss/rssmanager.cpp

@ -38,14 +38,16 @@
#include "rssparser.h" #include "rssparser.h"
#include "downloadthread.h" #include "downloadthread.h"
static const int MSECS_PER_MIN = 60000;
RssManager::RssManager(): RssManager::RssManager():
m_rssDownloader(new DownloadThread(this)), m_rssDownloader(new DownloadThread(this)),
m_downloadRules(new RssDownloadRuleList), m_downloadRules(new RssDownloadRuleList),
m_rssParser(new RssParser(this)) m_rssParser(new RssParser(this))
{ {
connect(&m_refreshTimer, SIGNAL(timeout()), this, SLOT(refresh())); connect(&m_refreshTimer, SIGNAL(timeout()), SLOT(refresh()));
m_refreshInterval = RssSettings().getRSSRefreshInterval(); m_refreshInterval = RssSettings().getRSSRefreshInterval();
m_refreshTimer.start(m_refreshInterval*60000); m_refreshTimer.start(m_refreshInterval * MSECS_PER_MIN);
} }
RssManager::~RssManager() RssManager::~RssManager()

1
src/rss/rssmanager.h

@ -52,7 +52,6 @@ public:
DownloadThread* rssDownloader() const; DownloadThread* rssDownloader() const;
RssParser* rssParser() const; RssParser* rssParser() const;
RssDownloadRuleList* downloadRules() const; RssDownloadRuleList* downloadRules() const;
public slots: public slots:

Loading…
Cancel
Save