diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index feea8347a..e94e245fe 100755 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -263,6 +263,30 @@ void FeedsModel::reassignNodeToNewParent(RootItem *original_node, RootItem *new_ } } +QList FeedsModel::serviceRoots() { + QList roots; + + foreach (RootItem *root, m_rootItem->childItems()) { + if (root->kind() == RootItemKind::ServiceRoot) { + roots.append(root->toServiceRoot()); + } + } + + return roots; +} + +StandardServiceRoot *FeedsModel::standardServiceRoot() { + foreach (RootItem *root, m_rootItem->childItems()) { + StandardServiceRoot *std_service_root; + + if ((std_service_root = dynamic_cast(root)) != NULL) { + return std_service_root; + } + } + + return NULL; +} + QList FeedsModel::feedsForScheduledUpdate(bool auto_update_now) { QList feeds_for_update; @@ -430,28 +454,6 @@ Feed *FeedsModel::feedForIndex(const QModelIndex &index) { } } -/* -QList FeedsModel::feedsForIndexes(const QModelIndexList &indexes) { - QList feeds; - - // Get selected feeds for each index. - foreach (const QModelIndex &index, indexes) { - feeds.append(feedsForIndex(index)); - } - - // Now we obtained all feeds from corresponding indexes. - if (indexes.size() != feeds.size()) { - // Selection contains duplicate feeds (for - // example situation where feed and its parent category are both - // selected). So, remove duplicates from the list. - qSort(feeds.begin(), feeds.end(), RootItem::lessThan); - feeds.erase(std::unique(feeds.begin(), feeds.end(), RootItem::isEqual), feeds.end()); - } - - return feeds; -} -*/ - bool FeedsModel::markFeedsRead(const QList &feeds, int read) { QSqlDatabase db_handle = qApp->database()->connection(objectName(), DatabaseFactory::FromSettings); diff --git a/src/core/feedsmodel.h b/src/core/feedsmodel.h index c7540410b..55173ee79 100755 --- a/src/core/feedsmodel.h +++ b/src/core/feedsmodel.h @@ -26,6 +26,8 @@ class Category; class Feed; +class ServiceRoot; +class StandardServiceRoot; class QTimer; class FeedsModel : public QAbstractItemModel { @@ -68,6 +70,15 @@ class FeedsModel : public QAbstractItemModel { // If it is, then it reassigns original_node to new parent. void reassignNodeToNewParent(RootItem *original_node, RootItem *new_parent); + // Returns all activated service roots. + // NOTE: Service root nodes are lying directly UNDER + // the model root item. + QList serviceRoots(); + + // Direct and the only global accessor to standard service root. + // NOTE: Standard service root is always activated. + StandardServiceRoot *standardServiceRoot(); + // Returns the list of feeds which should be updated // according to auto-update schedule. // Variable "auto_update_now" is true, when global timeout @@ -86,9 +97,6 @@ class FeedsModel : public QAbstractItemModel { // Returns list of all feeds contained in the model. QList allFeeds(); - // Returns list of ALL CHILD feeds which belong to given parent indexes. - //QList feedsForIndexes(const QModelIndexList &indexes); - // Returns ALL RECURSIVE CHILD feeds contained within single index. QList feedsForIndex(const QModelIndex &index); @@ -105,6 +113,9 @@ class FeedsModel : public QAbstractItemModel { RootItem *itemForIndex(const QModelIndex &index) const; // Returns source QModelIndex on which lies given item. + // NOTE: This goes through all available indexes and + // checks their bound items manually, there is no + // other way to to this. QModelIndex indexForItem(RootItem *item) const; // Determines if any feed has any new messages. diff --git a/src/core/rootitem.cpp b/src/core/rootitem.cpp index a6b13d141..a46f0a906 100755 --- a/src/core/rootitem.cpp +++ b/src/core/rootitem.cpp @@ -17,8 +17,9 @@ #include "core/rootitem.h" -#include "services/standard/standardcategory.h" -#include "services/standard/standardfeed.h" +#include "services/abstract/serviceroot.h" +#include "services/abstract/feed.h" +#include "services/abstract/category.h" #include "miscellaneous/application.h" #include @@ -91,6 +92,26 @@ QList RootItem::getSubTree() { return children; } +QList RootItem::getSubTree(RootItemKind::Kind kind_of_item) { + QList children; + QList 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; +} + QList RootItem::getSubTreeCategories() { QList children; QList traversable_items; @@ -143,6 +164,10 @@ Feed *RootItem::toFeed() { return static_cast(this); } +ServiceRoot *RootItem::toServiceRoot() { + return static_cast(this); +} + int RootItem::countOfUnreadMessages() const { int total_count = 0; diff --git a/src/core/rootitem.h b/src/core/rootitem.h index 9491033e0..163e80ce9 100755 --- a/src/core/rootitem.h +++ b/src/core/rootitem.h @@ -22,18 +22,24 @@ #include #include + class Category; class Feed; +class ServiceRoot; namespace RootItemKind { // Describes the kind of the item. enum Kind { - Root = 1001, - Bin = 1002, - Feed = 1003, - Category = 1004, - ServiceRoot = 1005 + Root = 1, + Bin = 2, + Feed = 4, + Category = 8, + ServiceRoot = 16 }; + + inline Kind operator|(Kind a, Kind b) { + return static_cast(static_cast(a) | static_cast(b)); + } } // Represents ROOT item of FeedsModel. @@ -147,6 +153,7 @@ class RootItem { // Returns flat list of all items from subtree where this item is a root. // Returned list includes this item too. QList getSubTree(); + QList getSubTree(RootItemKind::Kind kind_of_item); QList getSubTreeCategories(); QList getSubTreeFeeds(); @@ -205,6 +212,7 @@ class RootItem { // Converters Category *toCategory(); Feed *toFeed(); + ServiceRoot *toServiceRoot(); // Compares two model items. static bool isEqual(RootItem *lhs, RootItem *rhs); diff --git a/src/gui/feedsview.cpp b/src/gui/feedsview.cpp index ac1359377..d69e1ea53 100755 --- a/src/gui/feedsview.cpp +++ b/src/gui/feedsview.cpp @@ -91,9 +91,10 @@ RootItem *FeedsView::selectedItem() const { if (selected_rows.isEmpty()) { return NULL; } - - RootItem *selected_item = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(selected_rows.at(0))); - return selected_item == m_sourceModel->rootItem() ? NULL : selected_item; + else { + RootItem *selected_item = m_sourceModel->itemForIndex(m_proxyModel->mapToSource(selected_rows.at(0))); + return selected_item == m_sourceModel->rootItem() ? NULL : selected_item; + } } Category *FeedsView::selectedCategory() const { @@ -108,28 +109,31 @@ Feed *FeedsView::selectedFeed() const { void FeedsView::saveExpandedStates() { Settings *settings = qApp->settings(); + QList expandable_items; + + expandable_items.append(sourceModel()->rootItem()->getSubTree(RootItemKind::Category)); // Iterate all categories and save their expand statuses. - foreach (Category *category, sourceModel()->allCategories()) { - QString setting_name = QString::number(qHash(category->title())) + QL1S("-") + QString::number(category->id()); + foreach (RootItem *item, expandable_items) { + QString setting_name = QString::number(qHash(item->title())) + QL1S("-") + QString::number(item->id()); settings->setValue(GROUP(Categories), setting_name, - isExpanded(model()->mapFromSource(sourceModel()->indexForItem(category)))); + isExpanded(model()->mapFromSource(sourceModel()->indexForItem(item)))); } } void FeedsView::loadExpandedStates() { Settings *settings = qApp->settings(); + QList expandable_items; - // TODO: nastavit všechny service rooty automaticky na expanded - // toto obnáší vytvoření metody sourceModel()->serviceRoots() + expandable_items.append(sourceModel()->rootItem()->getSubTree(RootItemKind::Category | RootItemKind::ServiceRoot)); // Iterate all categories and save their expand statuses. - foreach (Category *category, sourceModel()->allCategories()) { - QString setting_name = QString::number(qHash(category->title())) + QL1S("-") + QString::number(category->id()); + foreach (RootItem *item, expandable_items) { + QString setting_name = QString::number(qHash(item->title())) + QL1S("-") + QString::number(item->id()); - setExpanded(model()->mapFromSource(sourceModel()->indexForItem(category)), + setExpanded(model()->mapFromSource(sourceModel()->indexForItem(item)), settings->value(GROUP(Categories), setting_name, true).toBool()); } }