diff --git a/src/core/feedsmodel.cpp b/src/core/feedsmodel.cpp index af7ce7ee8..051de41c9 100644 --- a/src/core/feedsmodel.cpp +++ b/src/core/feedsmodel.cpp @@ -426,20 +426,26 @@ QHash FeedsModel::getAllCategories() { return getCategories(m_rootItem); } -// TODO: Rewrite this iterativelly (instead of -// current recursive implementation). QHash FeedsModel::getCategories(FeedsModelRootItem *root) { QHash categories; + QList parents; - foreach (FeedsModelRootItem *child, root->childItems()) { - if (child->kind() == FeedsModelRootItem::Category) { - FeedsModelCategory *converted = static_cast(child); + parents.append(root->childItems()); - // This child is some kind of category. - categories.insert(converted->id(), converted); + while (!parents.isEmpty()) { + FeedsModelRootItem *item = parents.takeFirst(); - // Moreover, add all child categories of this category. - categories.unite(getCategories(converted)); + if (item->kind() == FeedsModelRootItem::Category) { + // This item is category, add it to the output list and + // scan its children. + int category_id = item->id(); + FeedsModelCategory *category = static_cast(item); + + if (!categories.contains(category_id)) { + categories.insert(category_id, category); + } + + parents.append(category->childItems()); } }