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
|
|
|
|
2015-11-04 09:35:49 +01:00
|
|
|
#include "services/abstract/serviceroot.h"
|
|
|
|
#include "services/abstract/feed.h"
|
|
|
|
#include "services/abstract/category.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)
|
2015-11-09 09:40:26 +01:00
|
|
|
: QObject(NULL),
|
|
|
|
m_kind(RootItemKind::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-11-09 10:17:48 +01:00
|
|
|
QList<QAction*> RootItem::contextMenuActions() {
|
2015-11-06 09:41:36 +01:00
|
|
|
return QList<QAction*>();
|
|
|
|
}
|
|
|
|
|
2015-11-12 10:53:17 +01:00
|
|
|
bool RootItem::canBeEdited() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootItem::editViaGui() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootItem::canBeDeleted() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootItem::deleteViaGui() {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootItem::canBeMarkedAsReadUnread(ReadStatus status) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootItem::markAsReadUnread(ReadStatus status) {
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
foreach (RootItem *child, m_childItems) {
|
|
|
|
if (child->canBeMarkedAsReadUnread(status)) {
|
|
|
|
result &= child->markAsReadUnread(status);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RootItem::cleanMessages(bool clear_only_read) {
|
|
|
|
bool result = true;
|
|
|
|
|
|
|
|
foreach (RootItem *child, m_childItems) {
|
|
|
|
result &= child->cleanMessages(clear_only_read);
|
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-11-13 08:44:37 +01:00
|
|
|
void RootItem::updateCounts(bool including_total_count) {
|
|
|
|
foreach (RootItem *child, m_childItems) {
|
|
|
|
child->updateCounts(including_total_count);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
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);
|
|
|
|
}
|
2015-11-09 09:42:48 +01:00
|
|
|
|
|
|
|
QFont RootItem::boldFont() const {
|
2015-11-09 09:40:26 +01:00
|
|
|
return m_boldFont;
|
|
|
|
}
|
|
|
|
|
2015-11-09 09:42:48 +01:00
|
|
|
void RootItem::setBoldFont(const QFont &bold_font) {
|
|
|
|
m_boldFont = bold_font;
|
2015-11-09 09:40:26 +01:00
|
|
|
}
|
|
|
|
|
2015-11-09 09:42:48 +01:00
|
|
|
QFont RootItem::normalFont() const {
|
2015-11-09 09:40:26 +01:00
|
|
|
return m_normalFont;
|
|
|
|
}
|
|
|
|
|
2015-11-09 09:42:48 +01:00
|
|
|
void RootItem::setNormalFont(const QFont &normal_font) {
|
|
|
|
m_normalFont = normal_font;
|
2015-11-09 09:40:26 +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
|
|
|
|
2015-11-09 09:40:26 +01:00
|
|
|
switch (role) {
|
|
|
|
case Qt::EditRole:
|
|
|
|
if (column == FDS_MODEL_TITLE_INDEX) {
|
|
|
|
return title();
|
|
|
|
}
|
|
|
|
else if (column == FDS_MODEL_COUNTS_INDEX) {
|
|
|
|
return countOfUnreadMessages();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::FontRole:
|
|
|
|
return countOfUnreadMessages() > 0 ? boldFont() : normalFont();
|
|
|
|
|
|
|
|
case Qt::DisplayRole:
|
|
|
|
if (column == FDS_MODEL_TITLE_INDEX) {
|
|
|
|
return title();
|
|
|
|
}
|
|
|
|
else if (column == FDS_MODEL_COUNTS_INDEX) {
|
|
|
|
int count_all = countOfAllMessages();
|
|
|
|
int count_unread = countOfUnreadMessages();
|
|
|
|
|
|
|
|
return qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::CountFormat)).toString()
|
2015-11-12 19:48:29 +01:00
|
|
|
.replace(PLACEHOLDER_UNREAD_COUNTS, count_unread < 0 ? QSL("-") : QString::number(count_unread))
|
|
|
|
.replace(PLACEHOLDER_ALL_COUNTS, count_all < 0 ? QSL("-") : QString::number(count_all));
|
2015-11-09 09:40:26 +01:00
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::DecorationRole:
|
|
|
|
if (column == FDS_MODEL_TITLE_INDEX) {
|
|
|
|
return icon();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
case Qt::TextAlignmentRole:
|
|
|
|
if (column == FDS_MODEL_COUNTS_INDEX) {
|
|
|
|
return Qt::AlignCenter;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
return QVariant();
|
|
|
|
}
|
|
|
|
|
|
|
|
default:
|
|
|
|
return QVariant();
|
|
|
|
}
|
2013-12-11 21:37:59 +01:00
|
|
|
}
|
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) {
|
2015-10-30 12:15:27 +01:00
|
|
|
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-11-04 08:42:52 +01:00
|
|
|
QList<RootItem*> RootItem::getSubTree() {
|
2015-07-24 10:05:42 +02:00
|
|
|
QList<RootItem*> children;
|
2015-11-04 08:42:52 +01:00
|
|
|
QList<RootItem*> traversable_items;
|
|
|
|
|
|
|
|
traversable_items.append(this);
|
|
|
|
|
2015-11-04 09:12:40 +01:00
|
|
|
// Iterate all nested items.
|
2015-11-04 08:42:52 +01:00
|
|
|
while (!traversable_items.isEmpty()) {
|
|
|
|
RootItem *active_item = traversable_items.takeFirst();
|
|
|
|
|
|
|
|
children.append(active_item);
|
|
|
|
traversable_items.append(active_item->childItems());
|
2015-11-04 09:12:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2015-11-04 09:35:49 +01:00
|
|
|
QList<RootItem*> RootItem::getSubTree(RootItemKind::Kind kind_of_item) {
|
|
|
|
QList<RootItem*> children;
|
|
|
|
QList<RootItem*> traversable_items;
|
|
|
|
|
|
|
|
traversable_items.append(this);
|
|
|
|
|
|
|
|
// Iterate all nested items.
|
|
|
|
while (!traversable_items.isEmpty()) {
|
|
|
|
RootItem *active_item = traversable_items.takeFirst();
|
|
|
|
|
|
|
|
if ((active_item->kind() & kind_of_item) > 0) {
|
|
|
|
children.append(active_item);
|
|
|
|
}
|
|
|
|
|
|
|
|
traversable_items.append(active_item->childItems());
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2015-11-04 09:12:40 +01:00
|
|
|
QList<Category*> RootItem::getSubTreeCategories() {
|
|
|
|
QList<Category*> children;
|
|
|
|
QList<RootItem*> traversable_items;
|
|
|
|
|
|
|
|
traversable_items.append(this);
|
|
|
|
|
|
|
|
// Iterate all nested items.
|
|
|
|
while (!traversable_items.isEmpty()) {
|
|
|
|
RootItem *active_item = traversable_items.takeFirst();
|
|
|
|
|
|
|
|
if (active_item->kind() == RootItemKind::Category) {
|
|
|
|
children.append(active_item->toCategory());
|
|
|
|
}
|
|
|
|
|
|
|
|
traversable_items.append(active_item->childItems());
|
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
|
|
|
QList<Feed*> RootItem::getSubTreeFeeds() {
|
|
|
|
QList<Feed*> children;
|
|
|
|
QList<RootItem*> traversable_items;
|
|
|
|
|
|
|
|
traversable_items.append(this);
|
|
|
|
|
|
|
|
// Iterate all nested items.
|
|
|
|
while (!traversable_items.isEmpty()) {
|
|
|
|
RootItem *active_item = traversable_items.takeFirst();
|
|
|
|
|
|
|
|
if (active_item->kind() == RootItemKind::Feed) {
|
|
|
|
children.append(active_item->toFeed());
|
|
|
|
}
|
|
|
|
|
|
|
|
traversable_items.append(active_item->childItems());
|
2015-05-02 19:32:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return children;
|
|
|
|
}
|
|
|
|
|
2015-11-04 14:05:33 +01:00
|
|
|
ServiceRoot *RootItem::getParentServiceRoot() {
|
|
|
|
RootItem *working_parent = this;
|
|
|
|
|
|
|
|
while (working_parent->kind() != RootItemKind::Root) {
|
|
|
|
if (working_parent->kind() == RootItemKind::ServiceRoot) {
|
|
|
|
return working_parent->toServiceRoot();
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
working_parent = working_parent->parent();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
2015-07-24 10:05:42 +02:00
|
|
|
bool RootItem::removeChild(RootItem *child) {
|
2014-01-17 19:10:10 +01:00
|
|
|
return m_childItems.removeOne(child);
|
|
|
|
}
|
|
|
|
|
2015-11-03 10:06:34 +01:00
|
|
|
Category *RootItem::toCategory() {
|
|
|
|
return static_cast<Category*>(this);
|
2014-10-28 18:14:17 +01:00
|
|
|
}
|
|
|
|
|
2015-11-03 10:06:34 +01:00
|
|
|
Feed *RootItem::toFeed() {
|
|
|
|
return static_cast<Feed*>(this);
|
2014-09-04 15:52:42 +02:00
|
|
|
}
|
|
|
|
|
2015-11-04 09:35:49 +01:00
|
|
|
ServiceRoot *RootItem::toServiceRoot() {
|
|
|
|
return static_cast<ServiceRoot*>(this);
|
|
|
|
}
|
|
|
|
|
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) {
|
2015-10-30 12:15:27 +01:00
|
|
|
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;
|
|
|
|
}
|
2014-01-07 19:09:19 +01:00
|
|
|
}
|