Browse Source

RSS optimization

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
954e010493
  1. 42
      src/rss/rss_imp.cpp
  2. 26
      src/rss/rssarticle.cpp
  3. 7
      src/rss/rssarticle.h
  4. 6
      src/rss/rssdownloadrule.cpp
  5. 74
      src/rss/rssfeed.cpp
  6. 48
      src/rss/rssfeed.h
  7. 5
      src/rss/rssfile.h
  8. 16
      src/rss/rssfolder.cpp
  9. 4
      src/rss/rssfolder.h
  10. 10
      src/rss/rssmanager.cpp
  11. 4
      src/rss/rssmanager.h

42
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))->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() { @@ -308,11 +308,11 @@ void RSSImp::on_updateAllButton_clicked() {
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->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() { @@ -321,8 +321,8 @@ void RSSImp::downloadTorrent() {
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->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) { @@ -442,7 +442,7 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* item) {
if(!rss_item) return;
qDebug("Getting the list of news");
QList<RssArticle*> news;
QList<RssArticle> news;
if(rss_item == rssmanager)
news = RssManager::sortNewsList(rss_item->getUnreadNewsList());
else if(rss_item)
@ -452,12 +452,12 @@ void RSSImp::refreshNewsList(QTreeWidgetItem* 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() { @@ -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 += "<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->title() + "</div>";
if(article->date().isValid()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Date: ")+"</b>"+article->date().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->author().isEmpty()) {
html += "<div style='background-color: #efefef;'><b>"+tr("Author: ")+"</b>"+article->author()+"</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->description()+"</div>";
html += "<divstyle='margin-left: 5px; margin-right: 5px;'>"+article.description()+"</div>";
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

26
src/rss/rssarticle.cpp

@ -251,19 +251,19 @@ QVariantHash RssArticle::toHash() const { @@ -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 { @@ -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;
}

7
src/rss/rssarticle.h

@ -38,12 +38,11 @@ @@ -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: @@ -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);

6
src/rss/rssdownloadrule.cpp

@ -129,9 +129,9 @@ void RssDownloadRule::setSavePath(const QString &save_path) @@ -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;
}

74
src/rss/rssfeed.cpp

@ -46,9 +46,9 @@ RssFeed::RssFeed(RssFolder* parent, QString _url): parent(parent), alias(""), ic @@ -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<QString, QVariant> 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(){ @@ -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<QString, QVariant> all_old_items = qBTRSS.value("old_items", QHash<QString, QVariant>()).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() { @@ -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<QString, QVariant> feeds_w_downloader = qBTRSS.value("downloader_on", QHash<QString, QVariant>()).toHash();
@ -172,8 +164,8 @@ void RssFeed::setIconPath(QString path) { @@ -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{ @@ -181,29 +173,30 @@ unsigned int RssFeed::getNbNews() const{
}
void RssFeed::markAllAsRead() {
foreach(RssArticle *item, this->values()){
item->markAsRead();
QHash<QString, RssArticle>::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<RssArticle*> RssFeed::getNewsList() const{
QList<RssArticle> RssFeed::getNewsList() const{
return this->values();
}
QList<RssArticle*> RssFeed::getUnreadNewsList() const {
QList<RssArticle*> unread_news;
foreach(RssArticle *item, this->values()) {
if(!item->isRead())
QList<RssArticle> RssFeed::getUnreadNewsList() const {
QList<RssArticle> 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) { @@ -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) { @@ -289,21 +280,23 @@ short RssFeed::readDoc(QIODevice* device) {
// RSS Feed Downloader
if(RssSettings().isRssDownloadingEnabled()) {
foreach(RssArticle* item, values()) {
if(item->isRead()) continue;
QHash<QString, RssArticle>::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() { @@ -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<RssArticle*> listItem = RssManager::sortNewsList(this->values());
const QList<RssArticle> listItem = RssManager::sortNewsList(this->values());
const int excess = nb_articles - max_articles;
for(int i=0; i<excess; ++i){
RssArticle *lastItem = listItem.takeLast();
delete this->take(lastItem->guid());
for(uint i=nb_articles-excess; i<nb_articles; ++i){
this->remove(listItem.at(i).guid());
}
}
}

48
src/rss/rssfeed.h

@ -37,28 +37,9 @@ @@ -37,28 +37,9 @@
class RssManager;
class RssFeed: public RssFile, public QHash<QString, RssArticle*> {
class RssFeed: public RssFile, public QHash<QString, RssArticle> {
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: @@ -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: @@ -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<RssArticle*> getNewsList() const;
QList<RssArticle*> getUnreadNewsList() const;
QList<RssArticle> getNewsList() const;
QList<RssArticle> 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;
};

5
src/rss/rssfile.h

@ -50,15 +50,14 @@ public: @@ -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<RssArticle*> getNewsList() const = 0;
virtual QList<RssArticle*> getUnreadNewsList() const = 0;
virtual QList<RssArticle> getNewsList() const = 0;
virtual QList<RssArticle> getUnreadNewsList() const = 0;
QStringList getPath() const {
QStringList path;
if(getParent()) {

16
src/rss/rssfolder.cpp

@ -89,7 +89,6 @@ void RssFolder::removeFile(QString ID) { @@ -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() { @@ -124,17 +123,17 @@ void RssFolder::refresh() {
}
}
QList<RssArticle*> RssFolder::getNewsList() const {
QList<RssArticle*> news;
foreach(RssFile *child, this->values()) {
QList<RssArticle> RssFolder::getNewsList() const {
QList<RssArticle> news;
foreach(const RssFile *child, this->values()) {
news << child->getNewsList();
}
return news;
}
QList<RssArticle*> RssFolder::getUnreadNewsList() const {
QList<RssArticle*> unread_news;
foreach(RssFile *child, this->values()) {
QList<RssArticle> RssFolder::getUnreadNewsList() const {
QList<RssArticle> unread_news;
foreach(const RssFile *child, this->values()) {
unread_news << child->getUnreadNewsList();
}
return unread_news;
@ -295,9 +294,6 @@ void RssFolder::addFile(RssFile * item) { @@ -295,9 +294,6 @@ void RssFolder::addFile(RssFile * item) {
}
void RssFolder::removeAllItems() {
foreach(RssFile* child, values()) {
child->removeAllItems();
}
qDeleteAll(values());
clear();
}

4
src/rss/rssfolder.h

@ -59,8 +59,8 @@ public: @@ -59,8 +59,8 @@ public:
QString getName() const;
QString getID() const;
bool hasChild(QString ID);
QList<RssArticle*> getNewsList() const;
QList<RssArticle*> getUnreadNewsList() const;
QList<RssArticle> getNewsList() const;
QList<RssArticle> getUnreadNewsList() const;
void removeAllSettings();
void removeAllItems();

10
src/rss/rssmanager.cpp

@ -126,17 +126,17 @@ void RssManager::saveStreamList(){ @@ -126,17 +126,17 @@ void RssManager::saveStreamList(){
settings.setRssFeedsAliases(aliases);
}
void RssManager::insertSortElem(QList<RssArticle*> &list, RssArticle *item) {
void RssManager::insertSortElem(QList<RssArticle> &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<RssArticle*> RssManager::sortNewsList(const QList<RssArticle*>& news_list) {
QList<RssArticle*> new_list;
foreach(RssArticle *item, news_list) {
QList<RssArticle> RssManager::sortNewsList(const QList<RssArticle>& news_list) {
QList<RssArticle> new_list;
foreach(const RssArticle &item, news_list) {
insertSortElem(new_list, item);
}
return new_list;

4
src/rss/rssmanager.h

@ -45,8 +45,8 @@ public: @@ -45,8 +45,8 @@ public:
static RssManager* instance();
static void drop();
~RssManager();
static void insertSortElem(QList<RssArticle*> &list, RssArticle *item);
static QList<RssArticle*> sortNewsList(const QList<RssArticle*>& news_list);
static void insertSortElem(QList<RssArticle> &list, const RssArticle &item);
static QList<RssArticle> sortNewsList(const QList<RssArticle>& news_list);
public slots:
void loadStreamList();

Loading…
Cancel
Save