qBittorrent/src/rss/rssfolder.cpp

218 lines
6.1 KiB
C++
Raw Normal View History

/*
* Bittorrent Client using Qt4 and libtorrent.
* 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
* 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
*/
2010-12-19 13:18:33 +03:00
#include <QDebug>
#include "rssfolder.h"
#include "rssarticle.h"
#include "qbtsession.h"
#include "rssmanager.h"
#include "rssfeed.h"
2011-01-27 21:03:28 +03:00
RssFolder::RssFolder(RssFolder *parent, const QString &name): m_parent(parent), m_name(name) {
}
RssFolder::~RssFolder() {
qDebug("Deleting a RSS folder, removing elements");
2011-01-27 21:03:28 +03:00
qDeleteAll(m_children.values());
}
2011-01-27 20:18:56 +03:00
unsigned int RssFolder::unreadCount() const {
unsigned int nb_unread = 0;
2011-01-27 21:03:28 +03:00
foreach(const RssFile *file, m_children.values()) {
2011-01-27 20:18:56 +03:00
nb_unread += file->unreadCount();
}
return nb_unread;
}
2011-01-27 20:18:56 +03:00
RssFile::FileType RssFolder::type() const {
return RssFile::FOLDER;
}
2011-01-27 21:03:28 +03:00
void RssFolder::removeChild(const QString &childId) {
if(m_children.contains(childId)) {
RssFile* child = m_children.take(childId);
child->removeAllSettings();
delete child;
}
}
2011-01-27 21:03:28 +03:00
RssFolder* RssFolder::addFolder(const QString &name) {
RssFolder *subfolder;
2011-01-27 21:03:28 +03:00
if(!m_children.contains(name)) {
2010-11-14 00:15:52 +03:00
subfolder = new RssFolder(this, name);
2011-01-27 21:03:28 +03:00
m_children[name] = subfolder;
} else {
2011-01-27 21:03:28 +03:00
subfolder = dynamic_cast<RssFolder*>(m_children.value(name));
}
return subfolder;
}
2011-01-27 21:03:28 +03:00
RssFeed* RssFolder::addStream(const QString &url) {
2010-11-14 00:15:52 +03:00
RssFeed* stream = new RssFeed(this, url);
2011-01-27 21:03:28 +03:00
Q_ASSERT(!m_children.contains(stream->url()));
m_children[stream->url()] = stream;
2011-01-27 20:18:56 +03:00
stream->refresh();
return stream;
}
// Refresh All Children
void RssFolder::refresh() {
2011-01-27 21:03:28 +03:00
foreach(RssFile *child, m_children.values()) {
child->refresh();
}
}
2011-01-27 20:18:56 +03:00
QList<RssArticle> RssFolder::articleList() const {
2011-01-25 21:46:38 +03:00
QList<RssArticle> news;
2011-01-27 21:03:28 +03:00
foreach(const RssFile *child, m_children.values()) {
2011-01-27 20:18:56 +03:00
news << child->articleList();
}
return news;
}
2011-01-27 20:18:56 +03:00
QList<RssArticle> RssFolder::unreadArticleList() const {
2011-01-25 21:46:38 +03:00
QList<RssArticle> unread_news;
2011-01-27 21:03:28 +03:00
foreach(const RssFile *child, m_children.values()) {
2011-01-27 20:18:56 +03:00
unread_news << child->unreadArticleList();
}
return unread_news;
}
QList<RssFile*> RssFolder::getContent() const {
2011-01-27 21:03:28 +03:00
return m_children.values();
}
unsigned int RssFolder::getNbFeeds() const {
unsigned int nbFeeds = 0;
2011-01-27 21:03:28 +03:00
foreach(RssFile* item, m_children.values()) {
2011-01-27 20:18:56 +03:00
if(item->type() == RssFile::FOLDER)
nbFeeds += ((RssFolder*)item)->getNbFeeds();
else
nbFeeds += 1;
}
return nbFeeds;
}
2011-01-27 20:18:56 +03:00
QString RssFolder::displayName() const {
return m_name;
}
2011-01-27 21:03:28 +03:00
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
2011-01-27 21:03:28 +03:00
m_parent->renameChildFolder(m_name, new_name);
// Actually rename
2011-01-27 20:18:56 +03:00
m_name = new_name;
}
}
2011-01-27 20:18:56 +03:00
void RssFolder::markAsRead() {
2011-01-27 21:03:28 +03:00
foreach(RssFile *item, m_children.values()) {
2011-01-27 20:18:56 +03:00
item->markAsRead();
}
}
QList<RssFeed*> RssFolder::getAllFeeds() const {
QList<RssFeed*> streams;
2011-01-27 21:03:28 +03:00
foreach(RssFile *item, m_children.values()) {
2011-01-27 20:18:56 +03:00
if(item->type() == RssFile::FEED) {
streams << static_cast<RssFeed*>(item);
} else {
streams << static_cast<RssFolder*>(item)->getAllFeeds();
}
}
return streams;
}
QHash<QString, RssFeed*> RssFolder::getAllFeedsAsHash() const {
QHash<QString, RssFeed*> ret;
2011-01-27 21:03:28 +03:00
foreach(RssFile *item, m_children.values()) {
2011-01-27 20:18:56 +03:00
if(item->type() == RssFile::FEED) {
RssFeed* feed = dynamic_cast<RssFeed*>(item);
Q_ASSERT(feed);
2011-01-27 20:18:56 +03:00
qDebug() << Q_FUNC_INFO << feed->url();
ret[feed->url()] = feed;
} else {
ret.unite(static_cast<RssFolder*>(item)->getAllFeedsAsHash());
}
}
return ret;
}
void RssFolder::addFile(RssFile * item) {
2011-01-27 20:18:56 +03:00
if(item->type() == RssFile::FEED) {
2011-01-27 21:03:28 +03:00
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 {
2011-01-27 21:03:28 +03:00
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() {
2011-01-27 21:03:28 +03:00
qDeleteAll(m_children.values());
m_children.clear();
}
void RssFolder::removeAllSettings() {
2011-01-27 21:03:28 +03:00
foreach(RssFile* child, m_children.values()) {
child->removeAllSettings();
}
}
2011-01-27 20:18:56 +03:00
QString RssFolder::id() const {
return m_name;
}
2011-01-27 21:03:28 +03:00
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);
}