PageRenderTime 347ms CodeModel.GetById 81ms app.highlight 159ms RepoModel.GetById 41ms app.codeStats 12ms

/src/libtomahawk/playlist/collectionflatmodel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 150 lines | 97 code | 34 blank | 19 comment | 13 complexity | 991f7dde0dfa279f77c0d195a90c4a84 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2011, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *
  5 *   Tomahawk is free software: you can redistribute it and/or modify
  6 *   it under the terms of the GNU General Public License as published by
  7 *   the Free Software Foundation, either version 3 of the License, or
  8 *   (at your option) any later version.
  9 *
 10 *   Tomahawk is distributed in the hope that it will be useful,
 11 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 12 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 13 *   GNU General Public License for more details.
 14 *
 15 *   You should have received a copy of the GNU General Public License
 16 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 17 */
 18
 19#include "collectionflatmodel.h"
 20
 21#include "database/database.h"
 22#include "sourcelist.h"
 23#include "utils/logger.h"
 24
 25using namespace Tomahawk;
 26
 27
 28CollectionFlatModel::CollectionFlatModel( QObject* parent )
 29    : TrackModel( parent )
 30{
 31}
 32
 33
 34CollectionFlatModel::~CollectionFlatModel()
 35{
 36}
 37
 38
 39void
 40CollectionFlatModel::addCollections( const QList< collection_ptr >& collections )
 41{
 42    foreach( const collection_ptr& col, collections )
 43    {
 44        addCollection( col );
 45    }
 46
 47    // we are waiting for some to load
 48    if( !m_loadingCollections.isEmpty() )
 49        emit loadingStarted();
 50}
 51
 52
 53void
 54CollectionFlatModel::addCollection( const collection_ptr& collection, bool sendNotifications )
 55{
 56    qDebug() << Q_FUNC_INFO << collection->name()
 57                            << collection->source()->id()
 58                            << collection->source()->userName();
 59
 60    if ( sendNotifications )
 61        emit loadingStarted();
 62
 63    DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( collection );
 64    connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr>, QVariant ) ),
 65                    SLOT( onTracksAdded( QList<Tomahawk::query_ptr> ) ), Qt::QueuedConnection );
 66
 67    Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
 68
 69    m_loadingCollections << collection.data();
 70
 71    if ( collection->source()->isLocal() )
 72        setTitle( tr( "My Collection" ) );
 73    else
 74        setTitle( tr( "Collection of %1" ).arg( collection->source()->friendlyName() ) );
 75}
 76
 77
 78void
 79CollectionFlatModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllTracks::SortOrder order )
 80{
 81    qDebug() << Q_FUNC_INFO << collection->name()
 82                            << collection->source()->id()
 83                            << collection->source()->userName()
 84                            << amount << order;
 85
 86    DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( collection );
 87    cmd->setLimit( amount );
 88    cmd->setSortOrder( order );
 89    cmd->setSortDescending( true );
 90
 91    connect( cmd, SIGNAL( tracks( QList<Tomahawk::query_ptr>, QVariant ) ),
 92                    SLOT( onTracksAdded( QList<Tomahawk::query_ptr> ) ), Qt::QueuedConnection );
 93
 94    Database::instance()->enqueue( QSharedPointer<DatabaseCommand>( cmd ) );
 95}
 96
 97
 98void
 99CollectionFlatModel::onTracksAdded( const QList<Tomahawk::query_ptr>& tracks )
100{
101    qDebug() << Q_FUNC_INFO << tracks.count() << rowCount( QModelIndex() );
102
103    if ( !m_loadingCollections.isEmpty() && sender() && qobject_cast< Collection* >( sender() ) )
104    {
105        // we are keeping track and are called as a slot
106        m_loadingCollections.removeAll( qobject_cast< Collection* >( sender() ) );
107    }
108
109    append( tracks );
110
111    if ( m_loadingCollections.isEmpty() )
112        emit loadingFinished();
113}
114
115
116void
117CollectionFlatModel::onTracksRemoved( const QList<Tomahawk::query_ptr>& tracks )
118{
119    QList<Tomahawk::query_ptr> t = tracks;
120    for ( int i = rowCount( QModelIndex() ); i >= 0 && t.count(); i-- )
121    {
122        QModelIndex idx = index( i, 0, QModelIndex() );
123        TrackModelItem* item = itemFromIndex( idx );
124        if ( !item )
125            continue;
126
127        int j = 0;
128        foreach ( const query_ptr& query, t )
129        {
130            if ( item->query().data() == query.data() )
131            {
132                remove( idx );
133                t.removeAt( j );
134                break;
135            }
136
137            j++;
138        }
139    }
140}
141
142
143void
144CollectionFlatModel::onDataChanged()
145{
146    TrackModelItem* p = (TrackModelItem*)sender();
147
148    if ( p )
149        emit dataChanged( p->index, p->index.sibling( p->index.row(), columnCount( QModelIndex() ) - 1 ) );
150}