2010-10-16 21:39:03 +04:00
|
|
|
/*
|
|
|
|
* 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>
|
|
|
|
|
2010-10-16 21:39:03 +04:00
|
|
|
#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) {
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
RssFolder::~RssFolder() {
|
|
|
|
qDebug("Deleting a RSS folder, removing elements");
|
2011-01-27 21:03:28 +03:00
|
|
|
qDeleteAll(m_children.values());
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
2011-01-27 20:18:56 +03:00
|
|
|
unsigned int RssFolder::unreadCount() const {
|
2010-10-16 21:39:03 +04:00
|
|
|
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();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
return nb_unread;
|
|
|
|
}
|
|
|
|
|
2011-01-27 20:18:56 +03:00
|
|
|
RssFile::FileType RssFolder::type() const {
|
2010-10-16 21:39:03 +04:00
|
|
|
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);
|
2010-10-16 21:39:03 +04:00
|
|
|
child->removeAllSettings();
|
|
|
|
delete child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 21:03:28 +03:00
|
|
|
RssFolder* RssFolder::addFolder(const QString &name) {
|
2010-10-16 21:39:03 +04:00
|
|
|
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;
|
2010-10-16 21:39:03 +04:00
|
|
|
} else {
|
2011-01-27 21:03:28 +03:00
|
|
|
subfolder = dynamic_cast<RssFolder*>(m_children.value(name));
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
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();
|
2010-10-16 21:39:03 +04:00
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Refresh All Children
|
|
|
|
void RssFolder::refresh() {
|
2011-01-27 21:03:28 +03:00
|
|
|
foreach(RssFile *child, m_children.values()) {
|
2010-10-16 21:39:03 +04:00
|
|
|
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();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
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();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
return unread_news;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<RssFile*> RssFolder::getContent() const {
|
2011-01-27 21:03:28 +03:00
|
|
|
return m_children.values();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
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)
|
2010-10-16 21:39:03 +04:00
|
|
|
nbFeeds += ((RssFolder*)item)->getNbFeeds();
|
|
|
|
else
|
|
|
|
nbFeeds += 1;
|
|
|
|
}
|
|
|
|
return nbFeeds;
|
|
|
|
}
|
|
|
|
|
2011-01-27 20:18:56 +03:00
|
|
|
QString RssFolder::displayName() const {
|
|
|
|
return m_name;
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
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)) {
|
2010-10-16 21:39:03 +04:00
|
|
|
// Update parent
|
2011-01-27 21:03:28 +03:00
|
|
|
m_parent->renameChildFolder(m_name, new_name);
|
2010-10-16 21:39:03 +04:00
|
|
|
// Actually rename
|
2011-01-27 20:18:56 +03:00
|
|
|
m_name = new_name;
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
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) {
|
2010-11-13 22:36:46 +03:00
|
|
|
streams << static_cast<RssFeed*>(item);
|
2010-10-16 21:39:03 +04:00
|
|
|
} else {
|
2010-11-13 22:36:46 +03:00
|
|
|
streams << static_cast<RssFolder*>(item)->getAllFeeds();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return streams;
|
|
|
|
}
|
|
|
|
|
2010-11-13 22:36:46 +03:00
|
|
|
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) {
|
2010-12-18 21:42:31 +03:00
|
|
|
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;
|
2010-11-13 22:36:46 +03:00
|
|
|
} else {
|
|
|
|
ret.unite(static_cast<RssFolder*>(item)->getAllFeedsAsHash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2010-10-16 21:39:03 +04:00
|
|
|
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));
|
2010-10-16 21:39:03 +04:00
|
|
|
} 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));
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
// Update parent
|
|
|
|
item->setParent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void RssFolder::removeAllItems() {
|
2011-01-27 21:03:28 +03:00
|
|
|
qDeleteAll(m_children.values());
|
|
|
|
m_children.clear();
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
void RssFolder::removeAllSettings() {
|
2011-01-27 21:03:28 +03:00
|
|
|
foreach(RssFile* child, m_children.values()) {
|
2010-10-16 21:39:03 +04:00
|
|
|
child->removeAllSettings();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2011-01-27 20:18:56 +03:00
|
|
|
QString RssFolder::id() const {
|
|
|
|
return m_name;
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|
|
|
|
|
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);
|
2010-10-16 21:39:03 +04:00
|
|
|
}
|