PageRenderTime 255ms CodeModel.GetById 121ms app.highlight 57ms RepoModel.GetById 74ms app.codeStats 0ms

/src/libtomahawk/playlist/AlbumModel.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 267 lines | 189 code | 60 blank | 18 comment | 23 complexity | a2199e05b148d08876da546e480a8c55 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2015, Christian Muehlhaeuser <muesli@tomahawk-player.org>
  4 *   Copyright 2010-2011, Jeff Mitchell <jeff@tomahawk-player.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "AlbumModel.h"
 21
 22#include <QListView>
 23#include <QMimeData>
 24#include <QNetworkReply>
 25
 26#include "Artist.h"
 27#include "PlayableItem.h"
 28#include "Source.h"
 29#include "SourceList.h"
 30#include "database/Database.h"
 31#include "utils/TomahawkUtils.h"
 32#include "utils/Logger.h"
 33
 34using namespace Tomahawk;
 35
 36
 37AlbumModel::AlbumModel( QObject* parent )
 38    : PlayableModel( parent )
 39    , m_overwriteOnAdd( false )
 40{
 41}
 42
 43
 44AlbumModel::~AlbumModel()
 45{
 46}
 47
 48
 49void
 50AlbumModel::addCollection( const collection_ptr& collection, bool overwrite )
 51{
 52    DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection );
 53    m_overwriteOnAdd = overwrite;
 54    m_collection = collection;
 55
 56    connect( cmd, SIGNAL( albums( QList<Tomahawk::album_ptr>, QVariant ) ),
 57                    SLOT( addAlbums( QList<Tomahawk::album_ptr> ) ) );
 58
 59    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 60
 61    setTitle( tr( "All albums from %1" ).arg( collection->prettyName() ) );
 62
 63    if ( collection.isNull() )
 64    {
 65        connect( SourceList::instance(), SIGNAL( sourceAdded( Tomahawk::source_ptr ) ), SLOT( onSourceAdded( Tomahawk::source_ptr ) ), Qt::UniqueConnection );
 66
 67        QList<Tomahawk::source_ptr> sources = SourceList::instance()->sources();
 68        foreach ( const source_ptr& source, sources )
 69        {
 70            connect( source->dbCollection().data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );
 71        }
 72    }
 73    else
 74    {
 75        connect( collection.data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );
 76    }
 77
 78    emit loadingStarted();
 79}
 80
 81
 82void
 83AlbumModel::addFilteredCollection( const collection_ptr& collection, unsigned int amount, DatabaseCommand_AllAlbums::SortOrder order, bool overwrite )
 84{
 85    DatabaseCommand_AllAlbums* cmd = new DatabaseCommand_AllAlbums( collection );
 86    cmd->setLimit( amount );
 87    cmd->setSortOrder( order );
 88    cmd->setSortDescending( true );
 89    m_overwriteOnAdd = overwrite;
 90    m_collection = collection;
 91
 92    connect( cmd, SIGNAL( albums( QList<Tomahawk::album_ptr>, QVariant ) ),
 93                    SLOT( addAlbums( QList<Tomahawk::album_ptr> ) ) );
 94
 95    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 96
 97    if ( !collection.isNull() )
 98        setTitle( tr( "All albums from %1" ).arg( collection->prettyName() ) );
 99    else
100        setTitle( tr( "All albums" ) );
101
102    emit loadingStarted();
103}
104
105
106void
107AlbumModel::addAlbums( const QList<Tomahawk::album_ptr>& albums )
108{
109    emit loadingFinished();
110
111    if ( m_overwriteOnAdd )
112        clear();
113
114    QList<Tomahawk::album_ptr> trimmedAlbums;
115    foreach ( const album_ptr& album, albums )
116    {
117        if ( !album.isNull() && !album->name().isEmpty() )
118        {
119            if ( findItem( album ) || trimmedAlbums.contains( album ) )
120                continue;
121            trimmedAlbums << album;
122        }
123    }
124
125    if ( trimmedAlbums.isEmpty() )
126    {
127        emit itemCountChanged( rowCount( QModelIndex() ) );
128        return;
129    }
130
131    int c = rowCount( QModelIndex() );
132    QPair< int, int > crows;
133    crows.first = c;
134    crows.second = c + trimmedAlbums.count() - 1;
135
136    emit beginInsertRows( QModelIndex(), crows.first, crows.second );
137
138    foreach( const album_ptr& album, trimmedAlbums )
139    {
140        PlayableItem* albumitem = new PlayableItem( album, rootItem() );
141        albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
142
143        connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
144    }
145
146    emit endInsertRows();
147    emit itemCountChanged( rowCount( QModelIndex() ) );
148}
149
150
151void
152AlbumModel::addArtists( const QList<Tomahawk::artist_ptr>& artists )
153{
154    emit loadingFinished();
155
156    if ( m_overwriteOnAdd )
157        clear();
158
159    QList<Tomahawk::artist_ptr> trimmedArtists;
160    foreach ( const artist_ptr& artist, artists )
161    {
162        if ( !artist.isNull() && !artist->name().isEmpty() )
163        {
164            if ( findItem( artist ) || trimmedArtists.contains( artist ) )
165                continue;
166            trimmedArtists << artist;
167        }
168    }
169
170    if ( trimmedArtists.isEmpty() )
171    {
172        emit itemCountChanged( rowCount( QModelIndex() ) );
173        return;
174    }
175
176    int c = rowCount( QModelIndex() );
177    QPair< int, int > crows;
178    crows.first = c;
179    crows.second = c + trimmedArtists.count() - 1;
180
181    emit beginInsertRows( QModelIndex(), crows.first, crows.second );
182
183    foreach ( const artist_ptr& artist, trimmedArtists )
184    {
185        PlayableItem* albumitem = new PlayableItem( artist, rootItem() );
186        albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
187
188        connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
189    }
190
191    emit endInsertRows();
192    emit itemCountChanged( rowCount( QModelIndex() ) );
193}
194
195
196void
197AlbumModel::addQueries( const QList<Tomahawk::query_ptr>& queries )
198{
199    emit loadingFinished();
200
201    if ( m_overwriteOnAdd )
202        clear();
203
204    int c = rowCount( QModelIndex() );
205    QPair< int, int > crows;
206    crows.first = c;
207    crows.second = c + queries.count() - 1;
208
209    emit beginInsertRows( QModelIndex(), crows.first, crows.second );
210
211    foreach ( const query_ptr& query, queries )
212    {
213        PlayableItem* albumitem = new PlayableItem( query, rootItem() );
214        albumitem->index = createIndex( rootItem()->children.count() - 1, 0, albumitem );
215
216        connect( albumitem, SIGNAL( dataChanged() ), SLOT( onDataChanged() ) );
217    }
218
219    emit endInsertRows();
220    emit itemCountChanged( rowCount( QModelIndex() ) );
221}
222
223
224void
225AlbumModel::onSourceAdded( const Tomahawk::source_ptr& source )
226{
227    connect( source->dbCollection().data(), SIGNAL( changed() ), SLOT( onCollectionChanged() ), Qt::UniqueConnection );
228}
229
230
231void
232AlbumModel::onCollectionChanged()
233{
234    addCollection( m_collection, true );
235}
236
237
238PlayableItem*
239AlbumModel::findItem( const artist_ptr& artist ) const
240{
241    for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
242    {
243        PlayableItem* item = itemFromIndex( index( i, 0, QModelIndex() ) );
244        if ( !item->artist().isNull() && item->artist() == artist )
245        {
246            return item;
247        }
248    }
249
250    return 0;
251}
252
253
254PlayableItem*
255AlbumModel::findItem( const album_ptr& album ) const
256{
257    for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
258    {
259        PlayableItem* item = itemFromIndex( index( i, 0, QModelIndex() ) );
260        if ( !item->album().isNull() && item->album() == album )
261        {
262            return item;
263        }
264    }
265
266    return 0;
267}