bool
SingleCollectionTreeItemModel::canFetchMore( const QModelIndex &parent ) const
{
    if ( !parent.isValid() )
       return m_rootItem->requiresUpdate();

    CollectionTreeItem *item = static_cast<CollectionTreeItem*>( parent.internalPointer() );
    return item->level() < m_levelType.count() && item->requiresUpdate();
}
QVariant CollectionTreeModel::data(const QModelIndex &index, int role) const
{
    if(!index.isValid()) {
        return QVariant();
    }

    CollectionTreeItem *item = static_cast<CollectionTreeItem *>(index.internalPointer());

    return item->data(role);
}
int CollectionTreeModel::rowCount(const QModelIndex &parent) const
{
    CollectionTreeItem *parentItem;
    if(parent.column() > 0) {
        return 0;
    }

    if(!parent.isValid()) {
        parentItem = tree()->root();
    } else {
        parentItem = static_cast<CollectionTreeItem *>(parent.internalPointer());
    }

    return parentItem->childCount();
}
QModelIndex CollectionTreeModel::parent(const QModelIndex &index) const
{
    if(!index.isValid()) {
        return QModelIndex();
    }

    CollectionTreeItem *childItem = static_cast<CollectionTreeItem *>(index.internalPointer());
    CollectionTreeItem *parentItem = childItem->parent();

    if(parentItem == tree()->root()) {
        return QModelIndex();
    }

    return createIndex(parentItem->row(), 0, parentItem);
}
QMimeData *
ScriptableServiceCollectionTreeModel::mimeData( const QList<CollectionTreeItem *> &items ) const
{
    // this is basically a copy of superclass method with a couple of changes:
    // 1. we don't reuse tracks already in the model
    // 2. we tell the querymaker to masquerade special tracks

    using namespace Collections;
    Meta::TrackList tracks;
    QList<QueryMaker *> queries;
    foreach( CollectionTreeItem *item, items )
    {
        if( item->isTrackItem() )
        {
            using namespace Meta;
            const ScriptableServiceTrack *serviceTrack =
                    dynamic_cast<const ScriptableServiceTrack *>( item->data().data() );
            if( !serviceTrack )
            {
                error() << "failed to convert generic track" << item->data() << "to ScriptableServiceTrack";
                continue;
            }
            tracks << serviceTrack->playableTrack();
            continue;
        }

        ScriptableServiceQueryMaker *qm = qobject_cast<ScriptableServiceQueryMaker *>( item->queryMaker() );
        if( !qm )
        {
            error() << "failed to convert generic QueryMaker to ScriptableService one";
            continue;
        }
        qm->setConvertToMultiTracks( true );
        for( CollectionTreeItem *tmp = item; tmp; tmp = tmp->parent() )
            tmp->addMatch( qm, levelCategory( tmp->level() - 1 ) );
        Collections::addTextualFilter( qm, m_currentFilter );
        queries.append( qm );
    }

    if( queries.isEmpty() && tracks.isEmpty() )
        return 0;

    AmarokMimeData *mimeData = new AmarokMimeData();
    mimeData->setTracks( tracks );
    mimeData->setQueryMakers( queries );
    mimeData->startQueries();
    return mimeData;
}
QModelIndex CollectionTreeModel::index(int row, int column, const QModelIndex &parent) const
{
    if(!hasIndex(row, column, parent)) {
            return QModelIndex();
    }

    CollectionTreeItem *parentItem;

    if(!parent.isValid()) {
        parentItem = tree()->root();
    } else {
        parentItem = static_cast<CollectionTreeItem *>(parent.internalPointer());
    }

    CollectionTreeItem *childItem = parentItem->child(row);

    if (childItem) {
        return createIndex(row, column, childItem);
    } else {
        return QModelIndex();
    }
}