/****************************************************************************
**
** 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/>.
****************************************************************************/

#ifndef PAGEITEMMODEL_P_H
#define PAGEITEMMODEL_P_H
#include "pageitem.h"
#include <QAbstractItemModel>
#include <QSet>
#include <QString>

namespace WebFeeds
{
class PageItemModelPrivate : public QAbstractItemModel
{
    Q_OBJECT
public:
    enum Role
    {
        ItemAddressRole = Qt::UserRole,
        LastCustomRole
    };
    explicit PageItemModelPrivate(QObject *parent = 0);
    explicit PageItemModelPrivate(PageItemPtr rootItem, QObject *parent = 0);
    virtual ~PageItemModelPrivate();

    QVariant data(const QModelIndex &index, int role) const;
    Qt::ItemFlags flags(const QModelIndex &index) const;
    QModelIndex index(int row, int column,
                      const QModelIndex &parent = QModelIndex()) const;
    QModelIndex parent(const QModelIndex &index) const;
    int rowCount(const QModelIndex &parent = QModelIndex()) const;
    int columnCount(const QModelIndex &parent = QModelIndex()) const;
    bool canFetchMore(const QModelIndex &parent) const;
    void fetchMore(const QModelIndex &parent);
    void setupModelData(const QSet<QString> &customPropertyNames);
    QModelIndex getIndex(PageItem* item) const;    
    PageItemPtr itemForIndex(const QModelIndex& index) const;

public slots:
    Q_INVOKABLE QVariant index(PageItem* item) const;
    Q_INVOKABLE int rowCount(QVariant parent);
    Q_INVOKABLE PageItem* item(const QVariant& index);

protected slots:
    void onAboutToInsertChildren(PageItemPtr parent, int start, int end);
    void onAboutToRemoveChildren(PageItemPtr parent, int start, int end);
    void onChildrenInserted();
    void onChildrenRemoved();
    void onItemDataChanged(PageItemPtr item);
    void onItemChildrenDataChanged(PageItemPtr parent);

private:
    PageItemPtr m_rootItem;

    void registerItem(PageItemPtr item) const;
};
} // namespace WebFeeds
#endif // PAGEITEMMODEL_P_H
