More RSS code clean up

This commit is contained in:
Christophe Dumez 2011-01-27 18:03:28 +00:00
parent 138cd90736
commit 7616c22897
7 changed files with 74 additions and 58 deletions

View file

@ -243,7 +243,7 @@ void RSSImp::deleteSelectedItems() {
// Notify TreeWidget // Notify TreeWidget
listStreams->itemAboutToBeRemoved(item); listStreams->itemAboutToBeRemoved(item);
// Actually delete the item // Actually delete the item
rss_item->parent()->removeFile(rss_item->id()); rss_item->parent()->removeChild(rss_item->id());
delete item; delete item;
} }
rssmanager->saveStreamList(); rssmanager->saveStreamList();
@ -340,7 +340,7 @@ void RSSImp::renameFiles() {
newName = QInputDialog::getText(this, tr("Please choose a new name for this RSS feed"), tr("New feed name:"), QLineEdit::Normal, listStreams->getRSSItem(item)->displayName(), &ok); newName = QInputDialog::getText(this, tr("Please choose a new name for this RSS feed"), tr("New feed name:"), QLineEdit::Normal, listStreams->getRSSItem(item)->displayName(), &ok);
// Check if name is already taken // Check if name is already taken
if(ok) { if(ok) {
if(rss_item->parent()->contains(newName)) { if(rss_item->parent()->hasChild(newName)) {
QMessageBox::warning(0, tr("Name already in use"), tr("This name is already used by another item, please choose another one.")); QMessageBox::warning(0, tr("Name already in use"), tr("This name is already used by another item, please choose another one."));
ok = false; ok = false;
} }
@ -349,7 +349,7 @@ void RSSImp::renameFiles() {
} }
}while(!ok); }while(!ok);
// Rename item // Rename item
rss_item->setAlias(newName); rss_item->rename(newName);
// Update TreeWidget // Update TreeWidget
updateItemInfos(item); updateItemInfos(item);
} }

View file

@ -119,7 +119,7 @@ QString RssFeed::title() const{
return m_title; return m_title;
} }
void RssFeed::setAlias(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;
} }
@ -245,7 +245,7 @@ bool RssFeed::parseRSS(QIODevice* device) {
if(xml.name() == "title") { if(xml.name() == "title") {
m_title = xml.readElementText(); m_title = xml.readElementText();
if(m_alias == url()) if(m_alias == url())
setAlias(m_title); rename(m_title);
} }
else if(xml.name() == "image") { else if(xml.name() == "image") {
QString icon_path = xml.attributes().value("url").toString(); QString icon_path = xml.attributes().value("url").toString();

View file

@ -53,7 +53,7 @@ public:
void setLoading(bool val); void setLoading(bool val);
bool isLoading() const; bool isLoading() const;
QString title() const; QString title() const;
void setAlias(const QString &alias); void rename(const QString &alias);
QString displayName() const; QString displayName() const;
QString url() const; QString url() const;
QString icon() const; QString icon() const;

View file

@ -50,7 +50,7 @@ public:
virtual FileType type() const = 0; virtual FileType type() const = 0;
virtual QString displayName() const = 0; virtual QString displayName() const = 0;
virtual QString id() const = 0; virtual QString id() const = 0;
virtual void setAlias(const QString &new_name) = 0; virtual void rename(const QString &new_name) = 0;
virtual void markAsRead() = 0; virtual void markAsRead() = 0;
virtual RssFolder* parent() const = 0; virtual RssFolder* parent() const = 0;
virtual void setParent(RssFolder* parent) = 0; virtual void setParent(RssFolder* parent) = 0;

View file

@ -33,21 +33,20 @@
#include "rssfolder.h" #include "rssfolder.h"
#include "rssarticle.h" #include "rssarticle.h"
#include "qbtsession.h" #include "qbtsession.h"
#include "downloadthread.h"
#include "rssmanager.h" #include "rssmanager.h"
#include "rssfeed.h" #include "rssfeed.h"
RssFolder::RssFolder(RssFolder *parent, QString name): m_parent(parent), m_name(name) { RssFolder::RssFolder(RssFolder *parent, const QString &name): m_parent(parent), m_name(name) {
} }
RssFolder::~RssFolder() { RssFolder::~RssFolder() {
qDebug("Deleting a RSS folder, removing elements"); qDebug("Deleting a RSS folder, removing elements");
qDeleteAll(this->values()); qDeleteAll(m_children.values());
} }
unsigned int RssFolder::unreadCount() const { unsigned int RssFolder::unreadCount() const {
unsigned int nb_unread = 0; unsigned int nb_unread = 0;
foreach(RssFile *file, this->values()) { foreach(const RssFile *file, m_children.values()) {
nb_unread += file->unreadCount(); nb_unread += file->unreadCount();
} }
return nb_unread; return nb_unread;
@ -57,43 +56,43 @@ RssFile::FileType RssFolder::type() const {
return RssFile::FOLDER; return RssFile::FOLDER;
} }
void RssFolder::removeFile(QString ID) { void RssFolder::removeChild(const QString &childId) {
if(this->contains(ID)) { if(m_children.contains(childId)) {
RssFile* child = this->take(ID); RssFile* child = m_children.take(childId);
child->removeAllSettings(); child->removeAllSettings();
delete child; delete child;
} }
} }
RssFolder* RssFolder::addFolder(QString name) { RssFolder* RssFolder::addFolder(const QString &name) {
RssFolder *subfolder; RssFolder *subfolder;
if(!this->contains(name)) { if(!m_children.contains(name)) {
subfolder = new RssFolder(this, name); subfolder = new RssFolder(this, name);
(*this)[name] = subfolder; m_children[name] = subfolder;
} else { } else {
subfolder = (RssFolder*)this->value(name); subfolder = dynamic_cast<RssFolder*>(m_children.value(name));
} }
return subfolder; return subfolder;
} }
RssFeed* RssFolder::addStream(QString url) { RssFeed* RssFolder::addStream(const QString &url) {
RssFeed* stream = new RssFeed(this, url); RssFeed* stream = new RssFeed(this, url);
Q_ASSERT(!this->contains(stream->url())); Q_ASSERT(!m_children.contains(stream->url()));
(*this)[stream->url()] = stream; m_children[stream->url()] = stream;
stream->refresh(); stream->refresh();
return stream; return stream;
} }
// Refresh All Children // Refresh All Children
void RssFolder::refresh() { void RssFolder::refresh() {
foreach(RssFile *child, this->values()) { foreach(RssFile *child, m_children.values()) {
child->refresh(); child->refresh();
} }
} }
QList<RssArticle> RssFolder::articleList() const { QList<RssArticle> RssFolder::articleList() const {
QList<RssArticle> news; QList<RssArticle> news;
foreach(const RssFile *child, this->values()) { foreach(const RssFile *child, m_children.values()) {
news << child->articleList(); news << child->articleList();
} }
return news; return news;
@ -101,19 +100,19 @@ QList<RssArticle> RssFolder::articleList() const {
QList<RssArticle> RssFolder::unreadArticleList() const { QList<RssArticle> RssFolder::unreadArticleList() const {
QList<RssArticle> unread_news; QList<RssArticle> unread_news;
foreach(const RssFile *child, this->values()) { foreach(const RssFile *child, m_children.values()) {
unread_news << child->unreadArticleList(); unread_news << child->unreadArticleList();
} }
return unread_news; return unread_news;
} }
QList<RssFile*> RssFolder::getContent() const { QList<RssFile*> RssFolder::getContent() const {
return this->values(); return m_children.values();
} }
unsigned int RssFolder::getNbFeeds() const { unsigned int RssFolder::getNbFeeds() const {
unsigned int nbFeeds = 0; unsigned int nbFeeds = 0;
foreach(RssFile* item, this->values()) { foreach(RssFile* item, m_children.values()) {
if(item->type() == RssFile::FOLDER) if(item->type() == RssFile::FOLDER)
nbFeeds += ((RssFolder*)item)->getNbFeeds(); nbFeeds += ((RssFolder*)item)->getNbFeeds();
else else
@ -126,25 +125,26 @@ QString RssFolder::displayName() const {
return m_name; return m_name;
} }
void RssFolder::setAlias(const QString &new_name) { void RssFolder::rename(const QString &new_name) {
Q_ASSERT(!m_parent->contains(new_name)); if(m_name == new_name) return;
if(!m_parent->contains(new_name)) { Q_ASSERT(!m_parent->hasChild(new_name));
if(!m_parent->hasChild(new_name)) {
// Update parent // Update parent
(*m_parent)[new_name] = m_parent->take(m_name); m_parent->renameChildFolder(m_name, new_name);
// Actually rename // Actually rename
m_name = new_name; m_name = new_name;
} }
} }
void RssFolder::markAsRead() { void RssFolder::markAsRead() {
foreach(RssFile *item, this->values()) { foreach(RssFile *item, m_children.values()) {
item->markAsRead(); item->markAsRead();
} }
} }
QList<RssFeed*> RssFolder::getAllFeeds() const { QList<RssFeed*> RssFolder::getAllFeeds() const {
QList<RssFeed*> streams; QList<RssFeed*> streams;
foreach(RssFile *item, this->values()) { foreach(RssFile *item, m_children.values()) {
if(item->type() == RssFile::FEED) { if(item->type() == RssFile::FEED) {
streams << static_cast<RssFeed*>(item); streams << static_cast<RssFeed*>(item);
} else { } else {
@ -156,7 +156,7 @@ QList<RssFeed*> RssFolder::getAllFeeds() const {
QHash<QString, RssFeed*> RssFolder::getAllFeedsAsHash() const { QHash<QString, RssFeed*> RssFolder::getAllFeedsAsHash() const {
QHash<QString, RssFeed*> ret; QHash<QString, RssFeed*> ret;
foreach(RssFile *item, this->values()) { foreach(RssFile *item, m_children.values()) {
if(item->type() == RssFile::FEED) { if(item->type() == RssFile::FEED) {
RssFeed* feed = dynamic_cast<RssFeed*>(item); RssFeed* feed = dynamic_cast<RssFeed*>(item);
Q_ASSERT(feed); Q_ASSERT(feed);
@ -171,25 +171,27 @@ QHash<QString, RssFeed*> RssFolder::getAllFeedsAsHash() const {
void RssFolder::addFile(RssFile * item) { void RssFolder::addFile(RssFile * item) {
if(item->type() == RssFile::FEED) { if(item->type() == RssFile::FEED) {
Q_ASSERT(!this->contains(((RssFeed*)item)->url())); RssFeed* feedItem = dynamic_cast<RssFeed*>(item);
(*this)[((RssFeed*)item)->url()] = item; Q_ASSERT(!m_children.contains(feedItem->url()));
qDebug("Added feed %s to folder ./%s", ((RssFeed*)item)->url().toLocal8Bit().data(), m_name.toLocal8Bit().data()); m_children[feedItem->url()] = item;
qDebug("Added feed %s to folder ./%s", qPrintable(feedItem->url()), qPrintable(m_name));
} else { } else {
Q_ASSERT(!this->contains(((RssFolder*)item)->displayName())); RssFolder* folderItem = dynamic_cast<RssFolder*>(item);
(*this)[((RssFolder*)item)->displayName()] = item; Q_ASSERT(!m_children.contains(folderItem->displayName()));
qDebug("Added folder %s to folder ./%s", ((RssFolder*)item)->displayName().toLocal8Bit().data(), m_name.toLocal8Bit().data()); m_children[folderItem->displayName()] = item;
qDebug("Added folder %s to folder ./%s", qPrintable(folderItem->displayName()), qPrintable(m_name));
} }
// Update parent // Update parent
item->setParent(this); item->setParent(this);
} }
void RssFolder::removeAllItems() { void RssFolder::removeAllItems() {
qDeleteAll(values()); qDeleteAll(m_children.values());
clear(); m_children.clear();
} }
void RssFolder::removeAllSettings() { void RssFolder::removeAllSettings() {
foreach(RssFile* child, values()) { foreach(RssFile* child, m_children.values()) {
child->removeAllSettings(); child->removeAllSettings();
} }
} }
@ -198,6 +200,18 @@ QString RssFolder::id() const {
return m_name; return m_name;
} }
bool RssFolder::hasChild(QString ID) { bool RssFolder::hasChild(const QString &childId) {
return this->contains(ID); return m_children.contains(childId);
}
void RssFolder::renameChildFolder(const QString &old_name, const QString &new_name)
{
Q_ASSERT(m_children.contains(old_name));
RssFile *folder = m_children.take(old_name);
m_children[new_name] = folder;
}
RssFile * RssFolder::takeChild(const QString &childId)
{
return m_children.take(childId);
} }

View file

@ -36,43 +36,45 @@
#include "rssfile.h" #include "rssfile.h"
class RssArticle; class RssArticle;
class DownloadThread;
class RssFeed; class RssFeed;
class RssFolder: public RssFile, public QHash<QString, RssFile*> { class RssFolder: public RssFile {
Q_OBJECT Q_OBJECT
public: public:
RssFolder(RssFolder *parent = 0, QString name = QString()); RssFolder(RssFolder *parent = 0, const QString &name = QString());
~RssFolder(); virtual ~RssFolder();
RssFolder* parent() const { return m_parent; } inline RssFolder* parent() const { return m_parent; }
void setParent(RssFolder* parent) { m_parent = parent; } void setParent(RssFolder* parent) { m_parent = parent; }
unsigned int unreadCount() const; unsigned int unreadCount() const;
FileType type() const; FileType type() const;
RssFeed* addStream(QString url); RssFeed* addStream(const QString &url);
RssFolder* addFolder(QString name); RssFolder* addFolder(const QString &name);
unsigned int getNbFeeds() const; unsigned int getNbFeeds() const;
QList<RssFile*> getContent() const; QList<RssFile*> getContent() const;
QList<RssFeed*> getAllFeeds() const; QList<RssFeed*> getAllFeeds() const;
QHash<QString, RssFeed*> getAllFeedsAsHash() const; QHash<QString, RssFeed*> getAllFeedsAsHash() const;
QString displayName() const; QString displayName() const;
QString id() const; QString id() const;
bool hasChild(QString ID); bool hasChild(const QString &childId);
QList<RssArticle> articleList() const; QList<RssArticle> articleList() const;
QList<RssArticle> unreadArticleList() const; QList<RssArticle> unreadArticleList() const;
void removeAllSettings(); void removeAllSettings();
void removeAllItems(); void removeAllItems();
void renameChildFolder(const QString &old_name, const QString &new_name);
RssFile *takeChild(const QString &childId);
public slots: public slots:
void refresh(); void refresh();
void addFile(RssFile * item); void addFile(RssFile * item);
void removeFile(QString ID); void removeChild(const QString &childId);
void setAlias(const QString &new_name); void rename(const QString &new_name);
void markAsRead(); void markAsRead();
private: private:
RssFolder *m_parent; RssFolder *m_parent;
QString m_name; QString m_name;
QHash<QString, RssFile*> m_children;
}; };
#endif // RSSFOLDER_H #endif // RSSFOLDER_H

View file

@ -87,7 +87,7 @@ void RssManager::loadStreamList() {
RssFeed *stream = feed_parent->addStream(feed_url); RssFeed *stream = feed_parent->addStream(feed_url);
const QString alias = aliases.at(i); const QString alias = aliases.at(i);
if(!alias.isEmpty()) { if(!alias.isEmpty()) {
stream->setAlias(alias); stream->rename(alias);
} }
++i; ++i;
} }
@ -105,10 +105,10 @@ void RssManager::forwardFeedIconChanged(QString url, QString icon_path) {
void RssManager::moveFile(RssFile* file, RssFolder* dest_folder) { void RssManager::moveFile(RssFile* file, RssFolder* dest_folder) {
RssFolder* src_folder = file->parent(); RssFolder* src_folder = file->parent();
if(dest_folder != src_folder) { if(dest_folder != src_folder) {
// Copy to new Folder
dest_folder->addFile(file);
// Remove reference in old folder // Remove reference in old folder
src_folder->remove(file->id()); src_folder->takeChild(file->id());
// add to new Folder
dest_folder->addFile(file);
} else { } else {
qDebug("Nothing to move, same destination folder"); qDebug("Nothing to move, same destination folder");
} }