PageRenderTime 144ms CodeModel.GetById 60ms app.highlight 48ms RepoModel.GetById 33ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCollection.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 288 lines | 177 code | 67 blank | 44 comment | 8 complexity | d81565e98b58210f062c9b897cd7febf 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 *   Copyright 2010-2011, Leo Franchi            <lfranchi@kde.org>
  5 *   Copyright 2013,      Teo Mrnjavac <teo@kde.org>
  6 *
  7 *   Tomahawk is free software: you can redistribute it and/or modify
  8 *   it under the terms of the GNU General Public License as published by
  9 *   the Free Software Foundation, either version 3 of the License, or
 10 *   (at your option) any later version.
 11 *
 12 *   Tomahawk is distributed in the hope that it will be useful,
 13 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 14 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 15 *   GNU General Public License for more details.
 16 *
 17 *   You should have received a copy of the GNU General Public License
 18 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 19 */
 20
 21#include "DatabaseCollection.h"
 22
 23#include "database/Database.h"
 24#include "database/DatabaseCommand_AllArtists.h"
 25#include "database/DatabaseCommand_AllAlbums.h"
 26#include "database/DatabaseCommand_AllTracks.h"
 27#include "database/DatabaseCommand_AddFiles.h"
 28#include "database/DatabaseCommand_DeleteFiles.h"
 29#include "database/DatabaseCommand_LoadAllPlaylists.h"
 30#include "database/DatabaseCommand_LoadAllAutoPlaylists.h"
 31#include "database/DatabaseCommand_LoadAllStations.h"
 32#include "utils/Logger.h"
 33
 34#include "PlaylistEntry.h"
 35
 36using namespace Tomahawk;
 37
 38
 39DatabaseCollection::DatabaseCollection( const source_ptr& src, QObject* parent )
 40    : Collection( src, QString( "dbcollection:%1" ).arg( src->nodeId() ), parent )
 41{
 42    m_browseCapabilities
 43        << CapabilityBrowseArtists
 44        << CapabilityBrowseAlbums
 45        << CapabilityBrowseTracks;
 46
 47
 48    connect( source().data(), SIGNAL( online() ), SIGNAL( online() ) );
 49    connect( source().data(), SIGNAL( offline() ), SIGNAL( offline() ) );
 50}
 51
 52
 53bool
 54DatabaseCollection::isOnline() const
 55{
 56    return source()->isOnline();
 57}
 58
 59
 60void
 61DatabaseCollection::loadPlaylists()
 62{
 63    DatabaseCommand_LoadAllPlaylists* cmd = new DatabaseCommand_LoadAllPlaylists( source() );
 64
 65    connect( cmd,  SIGNAL( done( const QList<Tomahawk::playlist_ptr>& ) ),
 66                     SLOT( setPlaylists( const QList<Tomahawk::playlist_ptr>& ) ) );
 67
 68    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 69}
 70
 71
 72void
 73DatabaseCollection::loadAutoPlaylists()
 74{
 75    DatabaseCommand_LoadAllAutoPlaylists* cmd = new DatabaseCommand_LoadAllAutoPlaylists( source() );
 76
 77    connect( cmd, SIGNAL( autoPlaylistLoaded( Tomahawk::source_ptr, QVariantList ) ),
 78                    SLOT( autoPlaylistCreated( const Tomahawk::source_ptr&, const QVariantList& ) ) );
 79
 80    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 81}
 82
 83
 84void
 85DatabaseCollection::loadStations()
 86{
 87    DatabaseCommand_LoadAllStations* cmd = new DatabaseCommand_LoadAllStations( source() );
 88
 89    connect( cmd, SIGNAL( stationLoaded( Tomahawk::source_ptr, QVariantList ) ),
 90             SLOT( stationCreated( const Tomahawk::source_ptr&, const QVariantList& ) ) );
 91
 92    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
 93}
 94
 95
 96void
 97DatabaseCollection::addTracks( const QList<QVariant>& newitems )
 98{
 99    qDebug() << Q_FUNC_INFO << newitems.length();
100    DatabaseCommand_AddFiles* cmd = new DatabaseCommand_AddFiles( newitems, source() );
101
102    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
103}
104
105
106void
107DatabaseCollection::removeTracks( const QDir& dir )
108{
109    qDebug() << Q_FUNC_INFO << dir;
110    DatabaseCommand_DeleteFiles* cmd = new DatabaseCommand_DeleteFiles( dir, source() );
111
112    Database::instance()->enqueue( Tomahawk::dbcmd_ptr( cmd ) );
113}
114
115
116QList< Tomahawk::playlist_ptr >
117DatabaseCollection::playlists()
118{
119    if ( Collection::playlists().isEmpty() )
120    {
121        loadPlaylists();
122    }
123
124    return Collection::playlists();
125}
126
127
128QList< dynplaylist_ptr >
129DatabaseCollection::autoPlaylists()
130{
131    // echonest is dead, disable all echonest code
132    /*
133    if ( Collection::autoPlaylists().isEmpty() )
134    {
135        loadAutoPlaylists();
136    }
137    */
138
139    return Collection::autoPlaylists();
140}
141
142
143QList< dynplaylist_ptr >
144DatabaseCollection::stations()
145{
146    // echonest is dead, disable all echonest code
147    /*
148    if ( Collection::stations().isEmpty() )
149    {
150        loadStations();
151    }
152    */
153
154    return Collection::stations();
155}
156
157
158Tomahawk::ArtistsRequest*
159DatabaseCollection::requestArtists()
160{
161    //FIXME: assuming there's only one dbcollection per source, and that this is the one
162    Tomahawk::collection_ptr thisCollection = source()->dbCollection();
163    if ( thisCollection->name() != this->name() )
164        return 0;
165
166    Tomahawk::ArtistsRequest* cmd = new DatabaseCommand_AllArtists( thisCollection );
167
168    return cmd;
169}
170
171
172Tomahawk::AlbumsRequest*
173DatabaseCollection::requestAlbums( const Tomahawk::artist_ptr& artist )
174{
175    //FIXME: assuming there's only one dbcollection per source, and that this is the one
176    Tomahawk::collection_ptr thisCollection = source()->dbCollection();
177    if ( thisCollection->name() != this->name() )
178        return 0;
179
180    Tomahawk::AlbumsRequest* cmd = new DatabaseCommand_AllAlbums( thisCollection, artist );
181
182    return cmd;
183}
184
185
186Tomahawk::TracksRequest*
187DatabaseCollection::requestTracks( const Tomahawk::album_ptr& album )
188{
189    //FIXME: assuming there's only one dbcollection per source, and that this is the one
190    Tomahawk::collection_ptr thisCollection = source()->dbCollection();
191    if ( thisCollection->name() != this->name() )
192        return 0;
193
194    DatabaseCommand_AllTracks* cmd = new DatabaseCommand_AllTracks( thisCollection );
195
196    if ( album )
197    {
198        cmd->setAlbum( album->weakRef() );
199        cmd->setSortOrder( DatabaseCommand_AllTracks::AlbumPosition );
200    }
201
202    return cmd;
203}
204
205
206int
207DatabaseCollection::trackCount() const
208{
209    return source()->trackCount();
210}
211
212
213QPixmap
214DatabaseCollection::icon( const QSize& size ) const
215{
216    return source()->avatar( TomahawkUtils::RoundedCorners, size, true );
217}
218
219
220void
221DatabaseCollection::autoPlaylistCreated( const source_ptr& source, const QVariantList& data )
222{
223    dynplaylist_ptr p( new DynamicPlaylist( source,                  //src
224                                            data[0].toString(),  //current rev
225                                            data[1].toString(),  //title
226                                            data[2].toString(),  //info
227                                            data[3].toString(),  //creator
228                                            data[4].toUInt(),  // createdOn
229                                            data[5].toString(),  // dynamic type
230                                            static_cast<GeneratorMode>(data[6].toInt()),  // dynamic mode
231                                            data[7].toBool(),    //shared
232                                            data[8].toInt(),     //lastmod
233                                            data[9].toString() ), &QObject::deleteLater );  //GUID
234    p->setWeakSelf( p.toWeakRef() );
235
236    addAutoPlaylist( p );
237}
238
239
240void
241DatabaseCollection::stationCreated( const source_ptr& source, const QVariantList& data )
242{
243    dynplaylist_ptr p( new DynamicPlaylist( source,                  //src
244                                            data[0].toString(),  //current rev
245                                            data[1].toString(),  //title
246                                            data[2].toString(),  //info
247                                            data[3].toString(),  //creator
248                                            data[4].toUInt(),  // createdOn
249                                            data[5].toString(),  // dynamic type
250                                            static_cast<GeneratorMode>(data[6].toInt()),  // dynamic mode
251                                            data[7].toBool(),    //shared
252                                            data[8].toInt(),     //lastmod
253                                            data[9].toString() ), &QObject::deleteLater );  //GUID
254    p->setWeakSelf( p.toWeakRef() );
255
256    addStation( p );
257}
258
259
260
261/*
262 * Resolver interface
263 *
264 * We implement searching the database in the DatabaseResolver which avoids a n+1 query here.
265 * We can't simply let ScriptCollection inherit Collection and Resolver because both are QObjects,
266 * although Resolver doesn't need to be a QObject atm, blocking adding signals/slots to Resolver
267 * in future seems to me worse than violating Liskov's law here. ~ domme
268 */
269unsigned int
270DatabaseCollection::timeout() const
271{
272    return 0;
273}
274
275
276unsigned int
277DatabaseCollection::weight() const
278{
279    return 0;
280}
281
282
283void
284DatabaseCollection::resolve( const Tomahawk::query_ptr& query )
285{
286    Q_UNUSED( query );
287    Q_ASSERT(false);
288}