/****************************************************************************
**
** Copyright (C) 2012 Róbert Márki
**
** This file is part of Web Feeds.
**
** Web Feeds 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.
**
** Web Feeds 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 Web Feeds.  If not, see <http://www.gnu.org/licenses/>.
****************************************************************************/

#include "everythinggroup.h"
#include "storyitem.h"

namespace NewsBlur
{
/*!
  \class EverythingGroup
  \brief The EverythingGroup class represents the NewsBlur Everything feed, which can contain
  only unread stories from multiple feeds.
*/

/*!
  Constructs an EverythingGroup object with the given \a parent and \a service object.
*/
EverythingGroup::EverythingGroup(PageItemPtr parent, AbstractFeedAggreagatorService* service) :
    FeedItem(parent, service)
{
}

/*!
  Reimplemented from FeedItem::itemType().
*/
NewsBlurItem::ItemType EverythingGroup::itemType() const
{
    return NewsBlurItem::EverythingGroup;
}

/*!
  Returns the number of child items which are not marked as read yet.
*/
int EverythingGroup::unreadChildCount() const
{
    int ret = 0;
    for(int i = 0; i < childCount(); ++i)
    {
        StoryItemPtr storyItem = childItem(i).dynamicCast<class StoryItem>();
        if(storyItem && !storyItem->isRead())
            ++ret;
    }
    return ret;
}

/*!
  Removes the child items which are already marked as read.
*/
void EverythingGroup::removeReadChildren()
{
    QList<StoryItemPtr> readStories;
    for(int i = 0; i < childCount(); ++i)
    {
        StoryItemPtr storyItem = childItem(i).dynamicCast<class StoryItem>();
        if(storyItem && storyItem->isRead())
            readStories << storyItem;
    }
    foreach(StoryItemPtr story, readStories)
        removeChild(story);
}
} // namespace NewsBlur
