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
listStreams->itemAboutToBeRemoved(item);
// Actually delete the item
rss_item->parent()->removeFile(rss_item->id());
rss_item->parent()->removeChild(rss_item->id());
delete item;
}
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);
// Check if name is already taken
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."));
ok = false;
}
@ -349,7 +349,7 @@ void RSSImp::renameFiles() {
}
}while(!ok);
// Rename item
rss_item->setAlias(newName);
rss_item->rename(newName);
// Update TreeWidget
updateItemInfos(item);
}

View file

@ -119,7 +119,7 @@ QString RssFeed::title() const{
return m_title;
}
void RssFeed::setAlias(const QString &new_name){
void RssFeed::rename(const QString &new_name){
qDebug() << "Renaming stream to" << new_name;
m_alias = new_name;
}
@ -245,7 +245,7 @@ bool RssFeed::parseRSS(QIODevice* device) {
if(xml.name() == "title") {
m_title = xml.readElementText();
if(m_alias == url())
setAlias(m_title);
rename(m_title);
}
else if(xml.name() == "image") {
QString icon_path = xml.attributes().value("url").toString();

View file

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

View file

@ -50,7 +50,7 @@ public:
virtual FileType type() const = 0;
virtual QString displayName() 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 RssFolder* parent() const = 0;
virtual void setParent(RssFolder* parent) = 0;

View file

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

View file

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