2009-08-23 12:10:56 +04:00
|
|
|
#ifndef FEEDLIST_H
|
|
|
|
#define FEEDLIST_H
|
|
|
|
|
|
|
|
#include <QTreeWidget>
|
|
|
|
#include <QTreeWidgetItem>
|
|
|
|
#include <QDropEvent>
|
|
|
|
#include <QDragMoveEvent>
|
|
|
|
#include <QStringList>
|
2009-08-23 16:22:19 +04:00
|
|
|
#include <QHash>
|
|
|
|
#include <QUrl>
|
2009-08-23 12:10:56 +04:00
|
|
|
#include "rss.h"
|
|
|
|
|
2009-08-23 16:49:37 +04:00
|
|
|
class FeedList: public QTreeWidget {
|
|
|
|
Q_OBJECT
|
2009-08-23 12:10:56 +04:00
|
|
|
|
|
|
|
private:
|
|
|
|
RssManager *rssmanager;
|
2009-08-23 16:22:19 +04:00
|
|
|
QHash<QTreeWidgetItem*, RssFile*> mapping;
|
|
|
|
QHash<QString, QTreeWidgetItem*> feeds_items;
|
|
|
|
QTreeWidgetItem* current_feed;
|
2009-08-24 10:28:31 +04:00
|
|
|
QTreeWidgetItem *unread_item;
|
2009-08-23 12:10:56 +04:00
|
|
|
|
|
|
|
public:
|
|
|
|
FeedList(QWidget *parent, RssManager *rssmanager): QTreeWidget(parent), rssmanager(rssmanager) {
|
|
|
|
setContextMenuPolicy(Qt::CustomContextMenu);
|
|
|
|
setDragDropMode(QAbstractItemView::InternalMove);
|
|
|
|
setSelectionMode(QAbstractItemView::ExtendedSelection);
|
2009-08-23 16:22:19 +04:00
|
|
|
setColumnCount(1);
|
2009-08-23 12:10:56 +04:00
|
|
|
QTreeWidgetItem *___qtreewidgetitem = headerItem();
|
|
|
|
___qtreewidgetitem->setText(0, QApplication::translate("RSS", "RSS feeds", 0, QApplication::UnicodeUTF8));
|
2009-08-23 16:22:19 +04:00
|
|
|
connect(this, SIGNAL(currentItemChanged(QTreeWidgetItem*,QTreeWidgetItem*)), this, SLOT(updateCurrentFeed(QTreeWidgetItem*)));
|
2009-08-24 10:28:31 +04:00
|
|
|
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);
|
|
|
|
setCurrentItem(unread_item);
|
|
|
|
}
|
2009-08-23 12:10:56 +04:00
|
|
|
|
2009-08-23 16:22:19 +04:00
|
|
|
void itemAdded(QTreeWidgetItem *item, RssFile* file) {
|
|
|
|
mapping[item] = file;
|
2009-08-23 17:35:45 +04:00
|
|
|
if(file->getType() == RssFile::STREAM) {
|
2009-08-23 16:22:19 +04:00
|
|
|
feeds_items[file->getID()] = item;
|
2009-08-23 17:35:45 +04:00
|
|
|
}
|
2009-08-23 16:22:19 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void itemRemoved(QTreeWidgetItem *item) {
|
|
|
|
RssFile* file = mapping.take(item);
|
|
|
|
if(file->getType() == RssFile::STREAM)
|
|
|
|
feeds_items.remove(file->getID());
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
bool hasFeed(QString url) const {
|
2009-08-23 16:22:19 +04:00
|
|
|
return feeds_items.contains(QUrl(url).toString());
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
QList<QTreeWidgetItem*> getAllFeedItems() const {
|
2009-08-23 17:05:47 +04:00
|
|
|
return feeds_items.values();
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
QTreeWidgetItem* getUnreadItem() const {
|
|
|
|
return unread_item;
|
|
|
|
}
|
|
|
|
|
|
|
|
QStringList getItemPath(QTreeWidgetItem* item) const {
|
2009-08-23 18:56:44 +04:00
|
|
|
QStringList path;
|
|
|
|
if(item) {
|
|
|
|
if(item->parent())
|
|
|
|
path.append(getItemPath(item->parent()));
|
|
|
|
path.append(getRSSItem(item)->getID());
|
|
|
|
}
|
|
|
|
return path;
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
QList<QTreeWidgetItem*> getAllOpenFolders(QTreeWidgetItem *parent=0) const {
|
2009-08-23 18:56:44 +04:00
|
|
|
QList<QTreeWidgetItem*> open_folders;
|
|
|
|
int nbChildren;
|
|
|
|
if(parent)
|
|
|
|
nbChildren = parent->childCount();
|
|
|
|
else
|
|
|
|
nbChildren = topLevelItemCount();
|
|
|
|
for(int i=0; i<nbChildren; ++i) {
|
|
|
|
QTreeWidgetItem *item;
|
|
|
|
if(parent)
|
|
|
|
item = parent->child(i);
|
|
|
|
else
|
|
|
|
item = topLevelItem(i);
|
|
|
|
if(getItemType(item) == RssFile::FOLDER && item->isExpanded()) {
|
|
|
|
QList<QTreeWidgetItem*> open_subfolders = getAllOpenFolders(item);
|
|
|
|
if(!open_subfolders.empty()) {
|
|
|
|
open_folders.append(open_subfolders);
|
|
|
|
} else {
|
|
|
|
open_folders << item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return open_folders;
|
|
|
|
}
|
|
|
|
|
2009-08-23 17:20:26 +04:00
|
|
|
QList<QTreeWidgetItem*> getAllFeedItems(QTreeWidgetItem* folder) {
|
|
|
|
QList<QTreeWidgetItem*> feeds;
|
|
|
|
int nbChildren = folder->childCount();
|
|
|
|
for(int i=0; i<nbChildren; ++i) {
|
2009-08-23 18:56:44 +04:00
|
|
|
QTreeWidgetItem *item = folder->child(i);
|
2009-08-23 17:20:26 +04:00
|
|
|
if(getItemType(item) == RssFile::STREAM) {
|
|
|
|
feeds << item;
|
|
|
|
} else {
|
|
|
|
feeds.append(getAllFeedItems(item));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return feeds;
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
RssFile* getRSSItem(QTreeWidgetItem *item) const {
|
2009-08-23 16:22:19 +04:00
|
|
|
return mapping[item];
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
RssFile::FileType getItemType(QTreeWidgetItem *item) const {
|
2009-08-23 16:22:19 +04:00
|
|
|
return mapping[item]->getType();
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
QString getItemID(QTreeWidgetItem *item) const {
|
2009-08-23 16:22:19 +04:00
|
|
|
return mapping[item]->getID();
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeWidgetItem* getTreeItemFromUrl(QString url) const{
|
|
|
|
return feeds_items[url];
|
|
|
|
}
|
|
|
|
|
2009-08-24 10:28:31 +04:00
|
|
|
RssStream* getRSSItemFromUrl(QString url) const {
|
|
|
|
return (RssStream*)getRSSItem(getTreeItemFromUrl(url));
|
|
|
|
}
|
|
|
|
|
2009-08-23 16:22:19 +04:00
|
|
|
QTreeWidgetItem* currentItem() const {
|
|
|
|
return current_feed;
|
|
|
|
}
|
|
|
|
|
|
|
|
QTreeWidgetItem* currentFeed() const {
|
|
|
|
return current_feed;
|
|
|
|
}
|
|
|
|
|
2009-08-23 16:49:37 +04:00
|
|
|
signals:
|
|
|
|
void foldersAltered(QList<QTreeWidgetItem*> folders);
|
2009-08-24 08:01:26 +04:00
|
|
|
void overwriteAttempt(QString filename);
|
2009-08-23 16:49:37 +04:00
|
|
|
|
2009-08-23 16:22:19 +04:00
|
|
|
protected slots:
|
|
|
|
void updateCurrentFeed(QTreeWidgetItem* new_item) {
|
2009-08-24 10:28:31 +04:00
|
|
|
if((new_item && getItemType(new_item) == RssFile::STREAM) || new_item == unread_item)
|
2009-08-23 16:22:19 +04:00
|
|
|
current_feed = new_item;
|
2009-08-23 12:10:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
void dragMoveEvent(QDragMoveEvent * event) {
|
|
|
|
QTreeWidgetItem *item = itemAt(event->pos());
|
2009-08-23 16:22:19 +04:00
|
|
|
if(item && getItemType(item) != RssFile::FOLDER)
|
2009-08-23 12:10:56 +04:00
|
|
|
event->ignore();
|
|
|
|
else {
|
2009-08-24 10:28:31 +04:00
|
|
|
if(selectedItems().contains(unread_item)) {
|
|
|
|
event->ignore();
|
|
|
|
} else {
|
|
|
|
QTreeWidget::dragMoveEvent(event);
|
|
|
|
}
|
2009-08-23 12:10:56 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void dropEvent(QDropEvent *event) {
|
|
|
|
qDebug("dropEvent");
|
2009-08-23 16:49:37 +04:00
|
|
|
QList<QTreeWidgetItem*> folders_altered;
|
2009-08-23 16:22:19 +04:00
|
|
|
QTreeWidgetItem *dest_folder_item = itemAt(event->pos());
|
|
|
|
RssFolder *dest_folder;
|
2009-08-23 16:49:37 +04:00
|
|
|
if(dest_folder_item) {
|
2009-08-23 16:22:19 +04:00
|
|
|
dest_folder = (RssFolder*)getRSSItem(dest_folder_item);
|
2009-08-23 16:49:37 +04:00
|
|
|
folders_altered << dest_folder_item;
|
|
|
|
} else {
|
2009-08-23 16:22:19 +04:00
|
|
|
dest_folder = rssmanager;
|
2009-08-23 16:49:37 +04:00
|
|
|
}
|
2009-08-23 12:10:56 +04:00
|
|
|
QList<QTreeWidgetItem *> src_items = selectedItems();
|
2009-08-24 08:01:26 +04:00
|
|
|
// 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
|
2009-08-23 12:10:56 +04:00
|
|
|
foreach(QTreeWidgetItem *src_item, src_items) {
|
2009-08-23 16:49:37 +04:00
|
|
|
QTreeWidgetItem *parent_folder = src_item->parent();
|
|
|
|
if(parent_folder && !folders_altered.contains(parent_folder))
|
|
|
|
folders_altered << parent_folder;
|
|
|
|
// Actually move the file
|
2009-08-23 16:22:19 +04:00
|
|
|
RssFile *file = getRSSItem(src_item);
|
|
|
|
rssmanager->moveFile(file, dest_folder);
|
2009-08-23 12:10:56 +04:00
|
|
|
}
|
2009-08-23 16:22:19 +04:00
|
|
|
QTreeWidget::dropEvent(event);
|
|
|
|
if(dest_folder_item)
|
|
|
|
dest_folder_item->setExpanded(true);
|
2009-08-23 16:49:37 +04:00
|
|
|
// Emit signal for update
|
|
|
|
if(!folders_altered.empty())
|
|
|
|
emit foldersAltered(folders_altered);
|
2009-08-23 12:10:56 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
#endif // FEEDLIST_H
|