rssguard/src/core/rootitem.cpp

167 lines
4.3 KiB
C++
Raw Normal View History

2014-02-26 07:41:40 +01:00
// This file is part of RSS Guard.
//
2015-01-04 14:12:14 +01:00
// Copyright (C) 2011-2015 by Martin Rotter <rotter.martinos@gmail.com>
2014-02-26 07:41:40 +01:00
//
// RSS Guard 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 3 of the License, or
// (at your option) any later version.
//
// RSS Guard 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 RSS Guard. If not, see <http://www.gnu.org/licenses/>.
2015-07-24 10:05:42 +02:00
#include "core/rootitem.h"
2013-12-11 21:37:59 +01:00
#include "services/standard/standardcategory.h"
#include "services/standard/standardfeed.h"
2014-10-28 18:14:17 +01:00
#include "miscellaneous/application.h"
2013-12-21 21:08:52 +01:00
#include <QVariant>
2013-12-11 14:07:18 +01:00
2015-07-24 10:05:42 +02:00
RootItem::RootItem(RootItem *parent_item)
: m_kind(RootItem::Root),
2014-09-02 21:04:20 +02:00
m_id(NO_PARENT_CATEGORY),
m_title(QString()),
m_description(QString()),
m_icon(QIcon()),
m_creationDate(QDateTime()),
2015-07-24 10:05:42 +02:00
m_childItems(QList<RootItem*>()),
2013-12-22 11:29:10 +01:00
m_parentItem(parent_item) {
2014-03-22 18:29:01 +01:00
setupFonts();
2013-12-11 14:07:18 +01:00
}
2015-07-24 10:05:42 +02:00
RootItem::~RootItem() {
2014-01-13 21:43:35 +01:00
qDeleteAll(m_childItems);
2013-12-12 15:07:17 +01:00
}
2015-07-24 10:05:42 +02:00
void RootItem::setupFonts() {
2014-04-17 13:49:38 +02:00
m_normalFont = Application::font("FeedsView");
2014-03-22 18:29:01 +01:00
m_boldFont = m_normalFont;
m_boldFont.setBold(true);
}
2013-12-11 14:07:18 +01:00
2015-07-24 10:05:42 +02:00
int RootItem::row() const {
2013-12-12 10:10:17 +01:00
if (m_parentItem) {
2015-07-24 10:05:42 +02:00
return m_parentItem->m_childItems.indexOf(const_cast<RootItem*>(this));
2013-12-12 10:10:17 +01:00
}
else {
2013-12-16 10:22:23 +01:00
// This item has no parent. Therefore, its row index is 0.
2013-12-12 10:10:17 +01:00
return 0;
}
2013-12-11 18:54:09 +01:00
}
2015-07-24 10:05:42 +02:00
QVariant RootItem::data(int column, int role) const {
2013-12-12 10:10:17 +01:00
Q_UNUSED(column)
Q_UNUSED(role)
2013-12-11 21:37:59 +01:00
2013-12-16 10:22:23 +01:00
// Do not return anything for the root item.
2013-12-11 21:37:59 +01:00
return QVariant();
}
2013-12-12 22:28:51 +01:00
2015-07-24 10:05:42 +02:00
int RootItem::countOfAllMessages() const {
2014-01-10 09:18:29 +01:00
int total_count = 0;
2015-07-24 10:05:42 +02:00
foreach (RootItem *child_item, m_childItems) {
total_count += child_item->countOfAllMessages();
2014-01-10 09:18:29 +01:00
}
return total_count;
2013-12-12 22:28:51 +01:00
}
2015-07-24 10:05:42 +02:00
QList<RootItem*> RootItem::getRecursiveChildren() {
QList<RootItem*> children;
2015-07-24 10:05:42 +02:00
if (kind() == RootItem::Feeed) {
// Root itself is a FEED.
children.append(this);
}
else {
// Root itself is a CATEGORY or ROOT item.
2015-07-24 10:05:42 +02:00
QList<RootItem*> traversable_items;
traversable_items.append(this);
// Iterate all nested categories.
while (!traversable_items.isEmpty()) {
2015-07-24 10:05:42 +02:00
RootItem *active_category = traversable_items.takeFirst();
2015-07-24 10:05:42 +02:00
foreach (RootItem *child, active_category->childItems()) {
if (child->kind() == RootItem::Feeed) {
// This child is feed.
children.append(child);
}
2015-07-24 10:05:42 +02:00
else if (child->kind() == RootItem::Cattegory) {
// This child is category, add its child feeds too.
traversable_items.append(child);
}
}
}
}
return children;
}
2015-07-24 10:05:42 +02:00
bool RootItem::removeChild(RootItem *child) {
return m_childItems.removeOne(child);
}
StandardCategory *RootItem::toCategory() {
return static_cast<StandardCategory*>(this);
2014-10-28 18:14:17 +01:00
}
StandardFeed *RootItem::toFeed() {
return static_cast<StandardFeed*>(this);
2014-10-28 18:14:17 +01:00
}
2015-07-24 10:05:42 +02:00
RootItem *RootItem::child(RootItem::Kind kind_of_child, const QString &identifier) {
foreach (RootItem *child, childItems()) {
if (child->kind() == kind_of_child) {
2015-07-24 10:05:42 +02:00
if ((kind_of_child == Cattegory && child->title() == identifier) ||
(kind_of_child == Feeed && child->toFeed()->url() == identifier)) {
return child;
}
}
}
return NULL;
}
2015-07-24 10:05:42 +02:00
int RootItem::countOfUnreadMessages() const {
2014-01-10 09:18:29 +01:00
int total_count = 0;
2015-07-24 10:05:42 +02:00
foreach (RootItem *child_item, m_childItems) {
total_count += child_item->countOfUnreadMessages();
2014-01-10 09:18:29 +01:00
}
return total_count;
2013-12-12 22:28:51 +01:00
}
2013-12-13 16:35:52 +01:00
2015-07-24 10:05:42 +02:00
bool RootItem::removeChild(int index) {
2014-01-10 08:36:08 +01:00
if (index >= 0 && index < m_childItems.size()) {
m_childItems.removeAt(index);
return true;
}
else {
return false;
}
}
2015-07-24 10:05:42 +02:00
bool RootItem::isEqual(RootItem *lhs, RootItem *rhs) {
return (lhs->kind() == rhs->kind()) && (lhs->id() == rhs->id());
}
2015-07-24 10:05:42 +02:00
bool RootItem::lessThan(RootItem *lhs, RootItem *rhs) {
if (lhs->kind() == rhs->kind()) {
return lhs->id() < rhs->id();
}
else {
return false;
}
}