From 289ad37f06f4cd1c68920b3de60aff98bfd63c86 Mon Sep 17 00:00:00 2001 From: Christophe Dumez Date: Sun, 10 Oct 2010 11:03:09 +0000 Subject: [PATCH] Further RSS code cleanup --- src/rss/feedList.h | 221 ------------------------------------- src/rss/feedlistwidget.cpp | 220 ++++++++++++++++++++++++++++++++++++ src/rss/feedlistwidget.h | 85 ++++++++++++++ src/rss/rss.h | 2 +- src/rss/rss.pri | 7 +- src/rss/rss_imp.cpp | 4 +- src/rss/rss_imp.h | 4 +- 7 files changed, 314 insertions(+), 229 deletions(-) delete mode 100644 src/rss/feedList.h create mode 100644 src/rss/feedlistwidget.cpp create mode 100644 src/rss/feedlistwidget.h diff --git a/src/rss/feedList.h b/src/rss/feedList.h deleted file mode 100644 index 168c3b7db..000000000 --- a/src/rss/feedList.h +++ /dev/null @@ -1,221 +0,0 @@ -#ifndef FEEDLIST_H -#define FEEDLIST_H - -#include -#include -#include -#include -#include -#include -#include -#include "rss.h" - -class FeedList: public QTreeWidget { - Q_OBJECT - -private: - RssManager *rssmanager; - QHash mapping; - QHash feeds_items; - QTreeWidgetItem* current_feed; - QTreeWidgetItem *unread_item; - -public: - FeedList(QWidget *parent, RssManager *rssmanager): QTreeWidget(parent), rssmanager(rssmanager) { - setContextMenuPolicy(Qt::CustomContextMenu); - setDragDropMode(QAbstractItemView::InternalMove); - setSelectionMode(QAbstractItemView::ExtendedSelection); - setColumnCount(1); - QTreeWidgetItem *___qtreewidgetitem = headerItem(); - ___qtreewidgetitem->setText(0, QApplication::translate("RSS", "RSS feeds", 0, QApplication::UnicodeUTF8)); - unread_item = new QTreeWidgetItem(this); - unread_item->setText(0, tr("Unread") + QString::fromUtf8(" (") + QString::number(rssmanager->getNbUnRead(), 10)+ QString(")")); - unread_item->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/oxygen/mail-folder-inbox.png"))); - itemAdded(unread_item, rssmanager); - connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateCurrentFeed(QTreeWidgetItem*))); - setCurrentItem(unread_item); - } - - ~FeedList() { - disconnect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateCurrentFeed(QTreeWidgetItem*))); - delete unread_item; - } - - void itemAdded(QTreeWidgetItem *item, RssFile* file) { - mapping[item] = file; - if(file->getType() == RssFile::STREAM) { - feeds_items[file->getID()] = item; - } - } - - void itemAboutToBeRemoved(QTreeWidgetItem *item) { - RssFile* file = mapping.take(item); - if(file->getType() == RssFile::STREAM) { - feeds_items.remove(file->getID()); - } else { - QList feeds = ((RssFolder*)file)->getAllFeeds(); - foreach(RssStream* feed, feeds) { - feeds_items.remove(feed->getID()); - } - } - } - - bool hasFeed(QString url) const { - return feeds_items.contains(QUrl(url).toString()); - } - - QList getAllFeedItems() const { - return feeds_items.values(); - } - - QTreeWidgetItem* getUnreadItem() const { - return unread_item; - } - - QStringList getItemPath(QTreeWidgetItem* item) const { - QStringList path; - if(item) { - if(item->parent()) - path << getItemPath(item->parent()); - path.append(getRSSItem(item)->getID()); - } - return path; - } - - QList getAllOpenFolders(QTreeWidgetItem *parent=0) const { - QList open_folders; - int nbChildren; - if(parent) - nbChildren = parent->childCount(); - else - nbChildren = topLevelItemCount(); - for(int i=0; ichild(i); - else - item = topLevelItem(i); - if(getItemType(item) == RssFile::FOLDER && item->isExpanded()) { - QList open_subfolders = getAllOpenFolders(item); - if(!open_subfolders.empty()) { - open_folders << open_subfolders; - } else { - open_folders << item; - } - } - } - return open_folders; - } - - QList getAllFeedItems(QTreeWidgetItem* folder) { - QList feeds; - int nbChildren = folder->childCount(); - for(int i=0; ichild(i); - if(getItemType(item) == RssFile::STREAM) { - feeds << item; - } else { - feeds << getAllFeedItems(item); - } - } - return feeds; - } - - RssFile* getRSSItem(QTreeWidgetItem *item) const { - return mapping.value(item, 0); - } - - RssFile::FileType getItemType(QTreeWidgetItem *item) const { - return mapping.value(item)->getType(); - } - - QString getItemID(QTreeWidgetItem *item) const { - return mapping.value(item)->getID(); - } - - QTreeWidgetItem* getTreeItemFromUrl(QString url) const{ - return feeds_items.value(url, 0); - } - - RssStream* getRSSItemFromUrl(QString url) const { - return (RssStream*)getRSSItem(getTreeItemFromUrl(url)); - } - - QTreeWidgetItem* currentItem() const { - return current_feed; - } - - QTreeWidgetItem* currentFeed() const { - return current_feed; - } - -signals: - void foldersAltered(QList folders); - void overwriteAttempt(QString filename); - -protected slots: - void updateCurrentFeed(QTreeWidgetItem* new_item) { - if(!new_item) return; - if(!mapping.contains(new_item)) return; - if((getItemType(new_item) == RssFile::STREAM) || new_item == unread_item) - current_feed = new_item; - } - -protected: - void dragMoveEvent(QDragMoveEvent * event) { - QTreeWidgetItem *item = itemAt(event->pos()); - if(item == unread_item) { - event->ignore(); - } else { - if(item && getItemType(item) != RssFile::FOLDER) - event->ignore(); - else { - if(selectedItems().contains(unread_item)) { - event->ignore(); - } else { - QTreeWidget::dragMoveEvent(event); - } - } - } - } - - void dropEvent(QDropEvent *event) { - qDebug("dropEvent"); - QList folders_altered; - QTreeWidgetItem *dest_folder_item = itemAt(event->pos()); - RssFolder *dest_folder; - if(dest_folder_item) { - dest_folder = (RssFolder*)getRSSItem(dest_folder_item); - folders_altered << dest_folder_item; - } else { - dest_folder = rssmanager; - } - QList src_items = selectedItems(); - // Check if there is not going to overwrite another file - foreach(QTreeWidgetItem *src_item, src_items) { - RssFile *file = getRSSItem(src_item); - if(dest_folder->hasChild(file->getID())) { - emit overwriteAttempt(file->getID()); - return; - } - } - // Proceed with the move - foreach(QTreeWidgetItem *src_item, src_items) { - QTreeWidgetItem *parent_folder = src_item->parent(); - if(parent_folder && !folders_altered.contains(parent_folder)) - folders_altered << parent_folder; - // Actually move the file - RssFile *file = getRSSItem(src_item); - rssmanager->moveFile(file, dest_folder); - } - QTreeWidget::dropEvent(event); - if(dest_folder_item) - dest_folder_item->setExpanded(true); - // Emit signal for update - if(!folders_altered.empty()) - emit foldersAltered(folders_altered); - } - -}; - -#endif // FEEDLIST_H diff --git a/src/rss/feedlistwidget.cpp b/src/rss/feedlistwidget.cpp new file mode 100644 index 000000000..ff44f78ab --- /dev/null +++ b/src/rss/feedlistwidget.cpp @@ -0,0 +1,220 @@ +/* + * Bittorrent Client using Qt4 and libtorrent. + * Copyright (C) 2010 Christophe Dumez + * + * 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 + */ + +#include "feedlistwidget.h" + +FeedListWidget::FeedListWidget(QWidget *parent, RssManager *rssmanager): QTreeWidget(parent), rssmanager(rssmanager) { + setContextMenuPolicy(Qt::CustomContextMenu); + setDragDropMode(QAbstractItemView::InternalMove); + setSelectionMode(QAbstractItemView::ExtendedSelection); + setColumnCount(1); + QTreeWidgetItem *___qtreewidgetitem = headerItem(); + ___qtreewidgetitem->setText(0, QApplication::translate("RSS", "RSS feeds", 0, QApplication::UnicodeUTF8)); + unread_item = new QTreeWidgetItem(this); + unread_item->setText(0, tr("Unread") + QString::fromUtf8(" (") + QString::number(rssmanager->getNbUnRead(), 10)+ QString(")")); + unread_item->setData(0,Qt::DecorationRole, QVariant(QIcon(":/Icons/oxygen/mail-folder-inbox.png"))); + itemAdded(unread_item, rssmanager); + connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateCurrentFeed(QTreeWidgetItem*))); + setCurrentItem(unread_item); +} + +FeedListWidget::~FeedListWidget() { + disconnect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateCurrentFeed(QTreeWidgetItem*))); + delete unread_item; +} + +void FeedListWidget::itemAdded(QTreeWidgetItem *item, RssFile* file) { + mapping[item] = file; + if(file->getType() == RssFile::STREAM) { + feeds_items[file->getID()] = item; + } +} + +void FeedListWidget::itemAboutToBeRemoved(QTreeWidgetItem *item) { + RssFile* file = mapping.take(item); + if(file->getType() == RssFile::STREAM) { + feeds_items.remove(file->getID()); + } else { + QList feeds = ((RssFolder*)file)->getAllFeeds(); + foreach(RssStream* feed, feeds) { + feeds_items.remove(feed->getID()); + } + } +} + +bool FeedListWidget::hasFeed(QString url) const { + return feeds_items.contains(QUrl(url).toString()); +} + +QList FeedListWidget::getAllFeedItems() const { + return feeds_items.values(); +} + +QTreeWidgetItem* FeedListWidget::getUnreadItem() const { + return unread_item; +} + +QStringList FeedListWidget::getItemPath(QTreeWidgetItem* item) const { + QStringList path; + if(item) { + if(item->parent()) + path << getItemPath(item->parent()); + path.append(getRSSItem(item)->getID()); + } + return path; +} + +QList FeedListWidget::getAllOpenFolders(QTreeWidgetItem *parent) const { + QList open_folders; + int nbChildren; + if(parent) + nbChildren = parent->childCount(); + else + nbChildren = topLevelItemCount(); + for(int i=0; ichild(i); + else + item = topLevelItem(i); + if(getItemType(item) == RssFile::FOLDER && item->isExpanded()) { + QList open_subfolders = getAllOpenFolders(item); + if(!open_subfolders.empty()) { + open_folders << open_subfolders; + } else { + open_folders << item; + } + } + } + return open_folders; +} + +QList FeedListWidget::getAllFeedItems(QTreeWidgetItem* folder) { + QList feeds; + int nbChildren = folder->childCount(); + for(int i=0; ichild(i); + if(getItemType(item) == RssFile::STREAM) { + feeds << item; + } else { + feeds << getAllFeedItems(item); + } + } + return feeds; +} + +RssFile* FeedListWidget::getRSSItem(QTreeWidgetItem *item) const { + return mapping.value(item, 0); +} + +RssFile::FileType FeedListWidget::getItemType(QTreeWidgetItem *item) const { + return mapping.value(item)->getType(); +} + +QString FeedListWidget::getItemID(QTreeWidgetItem *item) const { + return mapping.value(item)->getID(); +} + +QTreeWidgetItem* FeedListWidget::getTreeItemFromUrl(QString url) const{ + return feeds_items.value(url, 0); +} + +RssStream* FeedListWidget::getRSSItemFromUrl(QString url) const { + return (RssStream*)getRSSItem(getTreeItemFromUrl(url)); +} + +QTreeWidgetItem* FeedListWidget::currentItem() const { + return current_feed; +} + +QTreeWidgetItem* FeedListWidget::currentFeed() const { + return current_feed; +} + +void FeedListWidget::updateCurrentFeed(QTreeWidgetItem* new_item) { + if(!new_item) return; + if(!mapping.contains(new_item)) return; + if((getItemType(new_item) == RssFile::STREAM) || new_item == unread_item) + current_feed = new_item; +} + +void FeedListWidget::dragMoveEvent(QDragMoveEvent * event) { + QTreeWidgetItem *item = itemAt(event->pos()); + if(item == unread_item) { + event->ignore(); + } else { + if(item && getItemType(item) != RssFile::FOLDER) + event->ignore(); + else { + if(selectedItems().contains(unread_item)) { + event->ignore(); + } else { + QTreeWidget::dragMoveEvent(event); + } + } + } +} + +void FeedListWidget::dropEvent(QDropEvent *event) { + qDebug("dropEvent"); + QList folders_altered; + QTreeWidgetItem *dest_folder_item = itemAt(event->pos()); + RssFolder *dest_folder; + if(dest_folder_item) { + dest_folder = (RssFolder*)getRSSItem(dest_folder_item); + folders_altered << dest_folder_item; + } else { + dest_folder = rssmanager; + } + QList src_items = selectedItems(); + // Check if there is not going to overwrite another file + foreach(QTreeWidgetItem *src_item, src_items) { + RssFile *file = getRSSItem(src_item); + if(dest_folder->hasChild(file->getID())) { + emit overwriteAttempt(file->getID()); + return; + } + } + // Proceed with the move + foreach(QTreeWidgetItem *src_item, src_items) { + QTreeWidgetItem *parent_folder = src_item->parent(); + if(parent_folder && !folders_altered.contains(parent_folder)) + folders_altered << parent_folder; + // Actually move the file + RssFile *file = getRSSItem(src_item); + rssmanager->moveFile(file, dest_folder); + } + QTreeWidget::dropEvent(event); + if(dest_folder_item) + dest_folder_item->setExpanded(true); + // Emit signal for update + if(!folders_altered.empty()) + emit foldersAltered(folders_altered); +} diff --git a/src/rss/feedlistwidget.h b/src/rss/feedlistwidget.h new file mode 100644 index 000000000..5ea3ddcb3 --- /dev/null +++ b/src/rss/feedlistwidget.h @@ -0,0 +1,85 @@ +/* + * Bittorrent Client using Qt4 and libtorrent. + * Copyright (C) 2010 Christophe Dumez + * + * 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 FEEDLIST_H +#define FEEDLIST_H + +#include +#include +#include +#include +#include +#include +#include +#include "rss.h" + +class FeedListWidget: public QTreeWidget { + Q_OBJECT + +public: + FeedListWidget(QWidget *parent, RssManager *rssmanager); + ~FeedListWidget(); + void itemAdded(QTreeWidgetItem *item, RssFile* file); + void itemAboutToBeRemoved(QTreeWidgetItem *item); + + bool hasFeed(QString url) const; + QList getAllFeedItems() const; + QTreeWidgetItem* getUnreadItem() const; + QStringList getItemPath(QTreeWidgetItem* item) const; + QList getAllOpenFolders(QTreeWidgetItem *parent=0) const; + QList getAllFeedItems(QTreeWidgetItem* folder); + RssFile* getRSSItem(QTreeWidgetItem *item) const; + RssFile::FileType getItemType(QTreeWidgetItem *item) const; + QString getItemID(QTreeWidgetItem *item) const; + QTreeWidgetItem* getTreeItemFromUrl(QString url) const; + RssStream* getRSSItemFromUrl(QString url) const; + QTreeWidgetItem* currentItem() const; + QTreeWidgetItem* currentFeed() const; + +signals: + void foldersAltered(QList folders); + void overwriteAttempt(QString filename); + +protected slots: + void updateCurrentFeed(QTreeWidgetItem* new_item); + +protected: + void dragMoveEvent(QDragMoveEvent * event); + void dropEvent(QDropEvent *event); + +private: + RssManager *rssmanager; + QHash mapping; + QHash feeds_items; + QTreeWidgetItem* current_feed; + QTreeWidgetItem *unread_item; +}; + +#endif // FEEDLIST_H diff --git a/src/rss/rss.h b/src/rss/rss.h index 472f309c2..61045dbe0 100644 --- a/src/rss/rss.h +++ b/src/rss/rss.h @@ -1,6 +1,6 @@ /* * Bittorrent Client using Qt4 and libtorrent. - * Copyright (C) 2006 Christophe Dumez, Arnaud Demaiziere + * 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 diff --git a/src/rss/rss.pri b/src/rss/rss.pri index aeed586f9..5e7afa4b8 100644 --- a/src/rss/rss.pri +++ b/src/rss/rss.pri @@ -6,14 +6,15 @@ INCLUDEPATH += $$PWD $$PWD/rss_imp.h \ $$PWD/rsssettings.h \ $$PWD/feeddownloader.h \ - $$PWD/feedList.h \ + $$PWD/feedlistwidget.h \ SOURCES += $$PWD/rss.cpp \ $$PWD/rss_imp.cpp \ - $$PWD/rsssettings.cpp + $$PWD/rsssettings.cpp \ + $$PWD/feedlistwidget.cpp FORMS += $$PWD/ui/rss.ui \ $$PWD/ui/feeddownloader.ui \ $$PWD/ui/rsssettings.ui -} \ No newline at end of file +} diff --git a/src/rss/rss_imp.cpp b/src/rss/rss_imp.cpp index c39d10ee9..694ab8072 100644 --- a/src/rss/rss_imp.cpp +++ b/src/rss/rss_imp.cpp @@ -39,7 +39,7 @@ #include "rss_imp.h" #include "feeddownloader.h" -#include "feedList.h" +#include "feedlistwidget.h" #include "qbtsession.h" #include "cookiesdlg.h" #include "preferences.h" @@ -586,7 +586,7 @@ RSSImp::RSSImp(Bittorrent *BTSession) : QWidget(), BTSession(BTSession){ rssmanager = new RssManager(BTSession); - listStreams = new FeedList(splitter_h, rssmanager); + listStreams = new FeedListWidget(splitter_h, rssmanager); splitter_h->insertWidget(0, listStreams); listNews->hideColumn(NEWS_URL_COL); listNews->setColumnWidth(0, 16); diff --git a/src/rss/rss_imp.h b/src/rss/rss_imp.h index 14d370f08..7b54090e0 100644 --- a/src/rss/rss_imp.h +++ b/src/rss/rss_imp.h @@ -38,7 +38,7 @@ #include "rss.h" class Bittorrent; -class FeedList; +class FeedListWidget; class QTreeWidgetItem; class RSSImp : public QWidget, public Ui::RSS{ @@ -83,7 +83,7 @@ protected slots: private: RssManager *rssmanager; Bittorrent *BTSession; - FeedList *listStreams; + FeedListWidget *listStreams; QTreeWidgetItem* previous_news; };