From fd2ba6f635f16a7e8776109ec87039957736c822 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 19 Aug 2012 15:48:28 +0300 Subject: [PATCH] RSS code clean up --- src/rss/rss_imp.cpp | 75 +++++++++++++++++++++++-------------------- src/rss/rss_imp.h | 5 +-- src/rss/rssfeed.cpp | 7 ++-- src/rss/rssfeed.h | 2 +- src/rss/rssfile.h | 2 ++ src/rss/rssfolder.cpp | 9 +++++- src/rss/rssfolder.h | 1 + 7 files changed, 59 insertions(+), 42 deletions(-) diff --git a/src/rss/rss_imp.cpp b/src/rss/rss_imp.cpp index caf6ae128..c5525528f 100644 --- a/src/rss/rss_imp.cpp +++ b/src/rss/rss_imp.cpp @@ -150,17 +150,14 @@ void RSSImp::askNewFolder() { bool ok; QString new_name = QInputDialog::getText(this, tr("Please choose a folder name"), tr("Folder name:"), QLineEdit::Normal, tr("New folder"), &ok); if (ok) { - RssFolderPtr new_folder = rss_parent->addFolder(new_name); - QTreeWidgetItem* folder_item; + RssFolderPtr newFolder = rss_parent->addFolder(new_name); + QTreeWidgetItem* folderItem = createFolderListItem(newFolder); if (parent_item) - folder_item = new QTreeWidgetItem(parent_item); + parent_item->addChild(folderItem); else - folder_item = new QTreeWidgetItem(m_feedList); + m_feedList->addTopLevelItem(folderItem); // Notify TreeWidget - m_feedList->itemAdded(folder_item, new_folder); - // Set Text - folder_item->setText(0, new_folder->displayName() + QString::fromUtf8(" (0)")); - folder_item->setData(0,Qt::DecorationRole, QVariant(IconProvider::instance()->getIcon("inode-directory"))); + m_feedList->itemAdded(folderItem, newFolder); // Expand parent folder to display new folder if (parent_item) parent_item->setExpanded(true); @@ -357,8 +354,8 @@ void RSSImp::downloadSelectedTorrents() { } } -// open the url of the news in a browser -void RSSImp::openNewsUrl() { +// open the url of the selected RSS articles in the Web browser +void RSSImp::openSelectedArticlesUrls() { QList selected_items = listArticles->selectedItems(); foreach (const QListWidgetItem* item, selected_items) { RssArticlePtr news = m_feedList->getRSSItemFromUrl(item->data(Article::FeedUrlRole).toString()) @@ -370,10 +367,10 @@ void RSSImp::openNewsUrl() { } //right-click on stream : give it an alias -void RSSImp::renameFiles() { +void RSSImp::renameSelectedRssFile() { QList selectedItems = m_feedList->selectedItems(); Q_ASSERT(selectedItems.size() == 1); - QTreeWidgetItem *item = selectedItems.at(0); + QTreeWidgetItem* item = selectedItems.first(); RssFilePtr rss_item = m_feedList->getRSSItem(item); bool ok; QString newName; @@ -388,14 +385,14 @@ void RSSImp::renameFiles() { } else { return; } - }while(!ok); + } while (!ok); // Rename item rss_item->rename(newName); // Update TreeWidget updateItemInfos(item); } -//right-click on stream : refresh it +// right-click on stream : refresh it void RSSImp::refreshSelectedItems() { QList selectedItems = m_feedList->selectedItems(); foreach (QTreeWidgetItem* item, selectedItems) { @@ -432,40 +429,48 @@ void RSSImp::copySelectedFeedsURL() { void RSSImp::on_markReadButton_clicked() { QList selectedItems = m_feedList->selectedItems(); - QTreeWidgetItem* item; - foreach (item, selectedItems) { + foreach (QTreeWidgetItem* item, selectedItems) { RssFilePtr rss_item = m_feedList->getRSSItem(item); + Q_ASSERT(rss_item); rss_item->markAsRead(); updateItemInfos(item); } - if (selectedItems.size()) + // Update article list + if (!selectedItems.isEmpty()) populateArticleList(m_feedList->currentItem()); } -void RSSImp::fillFeedsList(QTreeWidgetItem *parent, const RssFolderPtr& rss_parent) { +QTreeWidgetItem* RSSImp::createFolderListItem(const RssFilePtr& rssFile) +{ + Q_ASSERT(rssFile); + QTreeWidgetItem* item = new QTreeWidgetItem; + item->setData(0, Qt::DisplayRole, QVariant(rssFile->displayName()+ QString::fromUtf8(" (")+QString::number(rssFile->unreadCount(), 10)+QString(")"))); + item->setData(0, Qt::DecorationRole, rssFile->icon()); + + return item; +} + +void RSSImp::fillFeedsList(QTreeWidgetItem* parent, const RssFolderPtr& rss_parent) { QList children; if (parent) { children = rss_parent->getContent(); } else { children = m_rssManager->getContent(); } - foreach (const RssFilePtr& rss_child, children) { - QTreeWidgetItem* item; - if (!parent) - item = new QTreeWidgetItem(m_feedList); + foreach (const RssFilePtr& rssFile, children) { + QTreeWidgetItem* item = createFolderListItem(rssFile); + Q_ASSERT(item); + if (parent) + parent->addChild(item); else - item = new QTreeWidgetItem(parent); - item->setData(0, Qt::DisplayRole, QVariant(rss_child->displayName()+ QString::fromUtf8(" (")+QString::number(rss_child->unreadCount(), 10)+QString(")"))); + m_feedList->addTopLevelItem(item); + // Notify TreeWidget of item addition - m_feedList->itemAdded(item, rss_child); - // Set Icon - if (qSharedPointerDynamicCast(rss_child)) { - item->setData(0,Qt::DecorationRole, QVariant(QIcon(QString::fromUtf8(":/Icons/loading.png")))); - } else if (RssFolderPtr folder = qSharedPointerDynamicCast(rss_child)) { - item->setData(0,Qt::DecorationRole, QVariant(IconProvider::instance()->getIcon("inode-directory"))); - // Recurvive call to load sub folders/files + m_feedList->itemAdded(item, rssFile); + + // Recursive call if this is a folder. + if (RssFolderPtr folder = qSharedPointerDynamicCast(rssFile)) fillFeedsList(item, folder); - } } } @@ -615,7 +620,7 @@ void RSSImp::updateFeedInfos(const QString& url, const QString& display_name, ui RssFeedPtr stream = qSharedPointerCast(m_feedList->getRSSItem(item)); item->setText(0, display_name + QString::fromUtf8(" (") + QString::number(nbUnread)+ QString(")")); if (!stream->isLoading()) - item->setData(0, Qt::DecorationRole, QVariant(QIcon(stream->icon()))); + item->setData(0, Qt::DecorationRole, QVariant(stream->icon())); // Update parent if (item->parent()) updateItemInfos(item->parent()); @@ -681,7 +686,7 @@ RSSImp::RSSImp(QWidget *parent) : QWidget(parent), m_rssManager(new RssManager) // Feeds list actions connect(actionDelete, SIGNAL(triggered()), this, SLOT(deleteSelectedItems())); - connect(actionRename, SIGNAL(triggered()), this, SLOT(renameFiles())); + connect(actionRename, SIGNAL(triggered()), this, SLOT(renameSelectedRssFile())); connect(actionUpdate, SIGNAL(triggered()), this, SLOT(refreshSelectedItems())); connect(actionNew_folder, SIGNAL(triggered()), this, SLOT(askNewFolder())); connect(actionNew_subscription, SIGNAL(triggered()), this, SLOT(on_newFeedButton_clicked())); @@ -690,7 +695,7 @@ RSSImp::RSSImp(QWidget *parent) : QWidget(parent), m_rssManager(new RssManager) connect(actionCopy_feed_URL, SIGNAL(triggered()), this, SLOT(copySelectedFeedsURL())); connect(actionMark_items_read, SIGNAL(triggered()), this, SLOT(on_markReadButton_clicked())); // News list actions - connect(actionOpen_news_URL, SIGNAL(triggered()), this, SLOT(openNewsUrl())); + connect(actionOpen_news_URL, SIGNAL(triggered()), this, SLOT(openSelectedArticlesUrls())); connect(actionDownload_torrent, SIGNAL(triggered()), this, SLOT(downloadSelectedTorrents())); connect(m_feedList, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(populateArticleList(QTreeWidgetItem*))); diff --git a/src/rss/rss_imp.h b/src/rss/rss_imp.h index bbd3440c9..b88c73695 100644 --- a/src/rss/rss_imp.h +++ b/src/rss/rss_imp.h @@ -61,7 +61,7 @@ private slots: void on_markReadButton_clicked(); void displayRSSListMenu(const QPoint&); void displayItemsListMenu(const QPoint&); - void renameFiles(); + void renameSelectedRssFile(); void refreshSelectedItems(); void copySelectedFeedsURL(); void populateArticleList(QTreeWidgetItem* item); @@ -71,7 +71,7 @@ private slots: void onFeedContentChanged(const QString& url); void updateItemsInfos(const QList &items); void updateItemInfos(QTreeWidgetItem *item); - void openNewsUrl(); + void openSelectedArticlesUrls(); void downloadSelectedTorrents(); void fillFeedsList(QTreeWidgetItem *parent=0, const RssFolderPtr& rss_parent = RssFolderPtr()); void saveSlidersPosition(); @@ -86,6 +86,7 @@ private slots: private: static QListWidgetItem* createArticleListItem(const RssArticlePtr& article); + static QTreeWidgetItem* createFolderListItem(const RssFilePtr& rssFile); private: RssManagerPtr m_rssManager; diff --git a/src/rss/rssfeed.cpp b/src/rss/rssfeed.cpp index fe5c355bc..321f9ccc5 100644 --- a/src/rss/rssfeed.cpp +++ b/src/rss/rssfeed.cpp @@ -188,10 +188,11 @@ QString RssFeed::url() const { return m_url; } -QString RssFeed::icon() const { +QIcon RssFeed::icon() const +{ if (m_inErrorState) - return ":/Icons/oxygen/unavailable.png"; - return m_icon; + return QIcon(":/Icons/oxygen/unavailable.png"); + return QIcon(m_icon); } bool RssFeed::hasCustomIcon() const { diff --git a/src/rss/rssfeed.h b/src/rss/rssfeed.h index 70acbe0c2..6b6f0b12e 100644 --- a/src/rss/rssfeed.h +++ b/src/rss/rssfeed.h @@ -65,7 +65,7 @@ public: virtual void rename(const QString &alias); virtual QString displayName() const; QString url() const; - QString icon() const; + virtual QIcon icon() const; bool hasCustomIcon() const; void setIconPath(const QString &pathHierarchy); RssArticlePtr getItem(const QString &guid) const; diff --git a/src/rss/rssfile.h b/src/rss/rssfile.h index bfa8a343e..8467d9e8a 100644 --- a/src/rss/rssfile.h +++ b/src/rss/rssfile.h @@ -31,6 +31,7 @@ #ifndef RSSFILE_H #define RSSFILE_H +#include #include #include #include @@ -54,6 +55,7 @@ public: virtual uint unreadCount() const = 0; virtual QString displayName() const = 0; virtual QString id() const = 0; + virtual QIcon icon() const = 0; virtual void rename(const QString &new_name) = 0; virtual void markAsRead() = 0; virtual RssFolder* parent() const = 0; diff --git a/src/rss/rssfolder.cpp b/src/rss/rssfolder.cpp index e2cd3babe..37a14b0a4 100644 --- a/src/rss/rssfolder.cpp +++ b/src/rss/rssfolder.cpp @@ -30,6 +30,7 @@ #include +#include "iconprovider.h" #include "rssfolder.h" #include "rssarticle.h" #include "qbtsession.h" @@ -225,10 +226,16 @@ void RssFolder::saveItemsToDisk() } } -QString RssFolder::id() const { +QString RssFolder::id() const +{ return m_name; } +QIcon RssFolder::icon() const +{ + return IconProvider::instance()->getIcon("inode-directory"); +} + bool RssFolder::hasChild(const QString &childId) { return m_children.contains(childId); } diff --git a/src/rss/rssfolder.h b/src/rss/rssfolder.h index 702b0e416..38b804ee2 100644 --- a/src/rss/rssfolder.h +++ b/src/rss/rssfolder.h @@ -61,6 +61,7 @@ public: QHash getAllFeedsAsHash() const; virtual QString displayName() const; virtual QString id() const; + virtual QIcon icon() const; bool hasChild(const QString &childId); virtual RssArticleList articleListByDateDesc() const; virtual RssArticleList unreadArticleListByDateDesc() const;