Browse Source

Use data sharing in RSS to save memory and increase performance

adaptive-webui-19844
Christophe Dumez 14 years ago
parent
commit
8f1276350e
  1. 1
      src/rss/rss.pri
  2. 102
      src/rss/rssarticle.cpp
  3. 18
      src/rss/rssarticle.h
  4. 61
      src/rss/rssarticle_p.h

1
src/rss/rss.pri

@ -13,6 +13,7 @@ HEADERS += $$PWD/rss_imp.h \
$$PWD/rssdownloadrule.h \ $$PWD/rssdownloadrule.h \
$$PWD/rssdownloadrulelist.h \ $$PWD/rssdownloadrulelist.h \
$$PWD/cookiesdlg.h \ $$PWD/cookiesdlg.h \
$$PWD/rssarticle_p.h
SOURCES += $$PWD/rss_imp.cpp \ SOURCES += $$PWD/rss_imp.cpp \
$$PWD/rsssettingsdlg.cpp \ $$PWD/rsssettingsdlg.cpp \

102
src/rss/rssarticle.cpp

@ -35,6 +35,7 @@
#include <iostream> #include <iostream>
#include "rssarticle.h" #include "rssarticle.h"
#include "rssarticle_p.h"
static const char shortDay[][4] = { static const char shortDay[][4] = {
"Mon", "Tue", "Wed", "Mon", "Tue", "Wed",
@ -190,7 +191,10 @@ QDateTime RssArticle::parseDate(const QString &string) {
} }
// public constructor // public constructor
RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml): m_parent(parent), m_read(false) { RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml)
{
d = new RssArticleData;
d->parent = parent;
while(!xml.atEnd()) { while(!xml.atEnd()) {
xml.readNext(); xml.readNext();
@ -199,55 +203,65 @@ RssArticle::RssArticle(RssFeed* parent, QXmlStreamReader& xml): m_parent(parent)
if(xml.isStartElement()) { if(xml.isStartElement()) {
if(xml.name() == "title") { if(xml.name() == "title") {
m_title = xml.readElementText(); d->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") {
m_torrentUrl = xml.attributes().value("url").toString(); d->torrentUrl = xml.attributes().value("url").toString();
} }
} }
else if(xml.name() == "link") { else if(xml.name() == "link") {
m_link = xml.readElementText(); d->link = xml.readElementText();
if(m_guid.isEmpty()) if(d->guid.isEmpty())
m_guid = m_link; d->guid = d->link;
} }
else if(xml.name() == "description") { else if(xml.name() == "description") {
m_description = xml.readElementText(); d->description = xml.readElementText();
} }
else if(xml.name() == "pubDate") { else if(xml.name() == "pubDate") {
m_date = parseDate(xml.readElementText()); d->date = parseDate(xml.readElementText());
} }
else if(xml.name() == "author") { else if(xml.name() == "author") {
m_author = xml.readElementText(); d->author = xml.readElementText();
} }
else if(xml.name() == "guid") { else if(xml.name() == "guid") {
m_guid = xml.readElementText(); d->guid = xml.readElementText();
} }
} }
} }
} }
RssArticle::RssArticle(RssFeed* parent, const QString &guid): RssArticle::RssArticle(RssFeed* parent, const QString &guid) {
m_parent(parent), m_guid(guid), m_read(false) { d = new RssArticleData;
d->parent = parent;
d->guid = guid;
} }
RssArticle::~RssArticle(){ RssArticle::~RssArticle() {}
RssArticle::RssArticle(const RssArticle& other): d(other.d) {
}
RssArticle & RssArticle::operator =(const RssArticle &other)
{
d = other.d;
return *this;
} }
bool RssArticle::hasAttachment() const { bool RssArticle::hasAttachment() const {
return !m_torrentUrl.isEmpty(); return !d->torrentUrl.isEmpty();
} }
QVariantHash RssArticle::toHash() const { QVariantHash RssArticle::toHash() const {
QVariantHash item; QVariantHash item;
item["title"] = m_title; item["title"] = d->title;
item["id"] = m_guid; item["id"] = d->guid;
item["torrent_url"] = m_torrentUrl; item["torrent_url"] = d->torrentUrl;
item["news_link"] = m_link; item["news_link"] = d->link;
item["description"] = m_description; item["description"] = d->description;
item["date"] = m_date; item["date"] = d->date;
item["author"] = m_author; item["author"] = d->author;
item["read"] = m_read; item["read"] = d->read;
return item; return item;
} }
@ -255,52 +269,62 @@ RssArticle hashToRssArticle(RssFeed* parent, const QVariantHash &h) {
const QString guid = h.value("id").toString(); const QString guid = h.value("id").toString();
if(guid.isEmpty()) return RssArticle(); if(guid.isEmpty()) return RssArticle();
RssArticle art(parent, guid); RssArticle art(parent, guid);
art.m_title = h.value("title", "").toString(); art.d->title = h.value("title", "").toString();
art.m_torrentUrl = h.value("torrent_url", "").toString(); art.d->torrentUrl = h.value("torrent_url", "").toString();
art.m_link = h.value("news_link", "").toString(); art.d->link = h.value("news_link", "").toString();
art.m_description = h.value("description").toString(); art.d->description = h.value("description").toString();
art.m_date = h.value("date").toDateTime(); art.d->date = h.value("date").toDateTime();
art.m_author = h.value("author").toString(); art.d->author = h.value("author").toString();
art.m_read = h.value("read").toBool(); art.d->read = h.value("read").toBool();
Q_ASSERT(art.isValid()); Q_ASSERT(art.isValid());
return art; return art;
} }
RssFeed* RssArticle::parent() const { RssFeed* RssArticle::parent() const {
return m_parent; return d->parent;
} }
bool RssArticle::isValid() const { bool RssArticle::isValid() const {
return !m_guid.isEmpty(); return !d->guid.isEmpty();
} }
QString RssArticle::author() const { QString RssArticle::author() const {
return m_author; return d->author;
} }
QString RssArticle::torrentUrl() const{ QString RssArticle::torrentUrl() const{
return m_torrentUrl; return d->torrentUrl;
} }
QString RssArticle::link() const { QString RssArticle::link() const {
return m_link; return d->link;
} }
QString RssArticle::description() const{ QString RssArticle::description() const{
if(m_description.isNull()) if(d->description.isNull())
return ""; return "";
return m_description; return d->description;
} }
QDateTime RssArticle::date() const { QDateTime RssArticle::date() const {
return m_date; return d->date;
} }
bool RssArticle::isRead() const{ bool RssArticle::isRead() const{
return m_read; return d->read;
} }
void RssArticle::markAsRead(){ void RssArticle::markAsRead(){
m_read = true; d->read = true;
}
QString RssArticle::guid() const
{
return d->guid;
}
QString RssArticle::title() const
{
return d->title;
} }

18
src/rss/rssarticle.h

@ -34,8 +34,10 @@
#include <QXmlStreamReader> #include <QXmlStreamReader>
#include <QDateTime> #include <QDateTime>
#include <QVariantHash> #include <QVariantHash>
#include <QExplicitlySharedDataPointer>
class RssFeed; class RssFeed;
class RssArticleData;
// Item of a rss stream, single information // Item of a rss stream, single information
class RssArticle { class RssArticle {
@ -43,13 +45,15 @@ class RssArticle {
public: public:
RssArticle(RssFeed* parent, QXmlStreamReader& xml); RssArticle(RssFeed* parent, QXmlStreamReader& xml);
RssArticle(RssFeed* parent = 0, const QString &guid = QString()); RssArticle(RssFeed* parent = 0, const QString &guid = QString());
RssArticle(const RssArticle& other); // Copy constructor
RssArticle& operator=(const RssArticle& other);
~RssArticle(); ~RssArticle();
// Accessors // Accessors
bool isValid() const; bool isValid() const;
bool hasAttachment() const; bool hasAttachment() const;
inline QString guid() const { return m_guid; } QString guid() const;
RssFeed* parent() const; RssFeed* parent() const;
inline QString title() const { return m_title; } QString title() const;
QString author() const; QString author() const;
QString torrentUrl() const; QString torrentUrl() const;
QString link() const; QString link() const;
@ -66,15 +70,7 @@ private:
static QDateTime parseDate(const QString &string); static QDateTime parseDate(const QString &string);
private: private:
RssFeed* m_parent; QExplicitlySharedDataPointer<RssArticleData> d;
QString m_guid;
QString m_title;
QString m_torrentUrl;
QString m_link;
QString m_description;
QDateTime m_date;
QString m_author;
bool m_read;
}; };
RssArticle hashToRssArticle(RssFeed* parent, const QVariantHash &hash); RssArticle hashToRssArticle(RssFeed* parent, const QVariantHash &hash);

61
src/rss/rssarticle_p.h

@ -0,0 +1,61 @@
/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2010 Christophe Dumez, Arnaud Demaiziere
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact: chris@qbittorrent.org, arnaud@qbittorrent.org
*/
#ifndef RSSARTICLE_P_H
#define RSSARTICLE_P_H
#include <QString>
#include <QSharedData>
#include <QDateTime>
class RssFeed;
class RssArticleData: public QSharedData {
public:
RssArticleData(): QSharedData(), read(false) {}
~RssArticleData() {}
RssArticleData(const RssArticleData& other):
QSharedData(other), parent(other.parent),
guid(other.guid), title(other.title), torrentUrl(other.torrentUrl),
link(other.link), description(other.description), date(other.date),
author(other.author), read(other.read) {}
RssFeed* parent;
QString guid;
QString title;
QString torrentUrl;
QString link;
QString description;
QDateTime date;
QString author;
bool read;
};
#endif // RSSARTICLE_P_H
Loading…
Cancel
Save