PageRenderTime 103ms CodeModel.GetById 40ms app.highlight 20ms RepoModel.GetById 40ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_AllAlbums.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 194 lines | 141 code | 36 blank | 17 comment | 7 complexity | 2c563f2da4ded9637f9c2637c8834ce1 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 "DatabaseCommand_AllAlbums.h"
 20
 21#include "utils/TomahawkUtils.h"
 22#include "utils/Logger.h"
 23
 24#include "Artist.h"
 25#include "DatabaseImpl.h"
 26#include "PlaylistEntry.h"
 27#include "Source.h"
 28
 29#include <QDateTime>
 30#include <QSqlQuery>
 31
 32
 33namespace Tomahawk
 34{
 35
 36DatabaseCommand_AllAlbums::DatabaseCommand_AllAlbums( const Tomahawk::collection_ptr& collection, const Tomahawk::artist_ptr& artist, QObject* parent )
 37  : DatabaseCommand( parent )
 38  , m_collection( collection.objectCast< DatabaseCollection >() )
 39  , m_artist( artist )
 40  , m_amount( 0 )
 41  , m_sortOrder( DatabaseCommand_AllAlbums::None )
 42  , m_sortDescending( false )
 43{
 44}
 45
 46
 47DatabaseCommand_AllAlbums::~DatabaseCommand_AllAlbums()
 48{
 49}
 50
 51
 52void
 53DatabaseCommand_AllAlbums::setArtist( const Tomahawk::artist_ptr& artist )
 54{
 55    m_artist = artist;
 56}
 57
 58
 59void
 60DatabaseCommand_AllAlbums::execForArtist( DatabaseImpl* dbi )
 61{
 62    TomahawkSqlQuery query = dbi->newquery();
 63    QList<Tomahawk::album_ptr> al;
 64    QString orderToken, sourceToken, filterToken, timeToken, tables;
 65
 66    switch ( m_sortOrder )
 67    {
 68        case 0:
 69            break;
 70
 71        case ModificationTime:
 72            orderToken = "file.mtime";
 73            timeToken = QString( "AND file.mtime <= %1" ).arg( QDateTime::currentDateTimeUtc().toTime_t() );
 74    }
 75
 76    if ( !m_collection.isNull() )
 77        sourceToken = QString( "AND file.source %1" ).arg( m_collection->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
 78
 79    if ( !m_filter.isEmpty() )
 80    {
 81        QString filtersql;
 82        QStringList sl = m_filter.split( " ", QString::SkipEmptyParts );
 83        foreach( QString s, sl )
 84        {
 85            filtersql += QString( " AND ( artist.name LIKE '%%1%' OR album.name LIKE '%%1%' OR track.name LIKE '%%1%' )" ).arg( TomahawkSqlQuery::escape( s ) );
 86        }
 87
 88        filterToken = QString( "AND artist.id = file_join.artist AND file_join.track = track.id %1" ).arg( filtersql );
 89        tables = "file, file_join, artist, track";
 90    }
 91    else
 92        tables = "file, file_join";
 93
 94    QString sql = QString(
 95        "SELECT DISTINCT album.id, album.name "
 96        "FROM %1 "
 97        "LEFT OUTER JOIN album ON file_join.album = album.id "
 98        "WHERE file.id = file_join.file "
 99        "AND file_join.artist = %2 "
100        "%3 %4 %5 %6 %7 %8"
101        ).arg( tables )
102         .arg( m_artist->id() )
103         .arg( sourceToken )
104         .arg( timeToken )
105         .arg( filterToken )
106         .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
107         .arg( m_sortDescending ? "DESC" : QString() )
108         .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
109
110    query.prepare( sql );
111    query.exec();
112
113    while( query.next() )
114    {
115        unsigned int albumId = query.value( 0 ).toUInt();
116        QString albumName = query.value( 1 ).toString();
117        if ( query.value( 0 ).isNull() )
118        {
119            albumName = tr( "Unknown" );
120        }
121
122        Tomahawk::album_ptr album = Tomahawk::Album::get( albumId, albumName, m_artist );
123        al << album;
124    }
125
126    emit albums( al, data() );
127    emit albums( al );
128    emit done();
129}
130
131
132void
133DatabaseCommand_AllAlbums::execForCollection( DatabaseImpl* dbi )
134{
135    TomahawkSqlQuery query = dbi->newquery();
136    QList<Tomahawk::album_ptr> al;
137    QString orderToken, sourceToken;
138
139    switch ( m_sortOrder )
140    {
141        case 0:
142            break;
143
144        case ModificationTime:
145            orderToken = "file.mtime";
146    }
147
148    if ( !m_collection.isNull() )
149        sourceToken = QString( "AND file.source %1 " ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
150
151    QString sql = QString(
152        "SELECT DISTINCT album.id, album.name, album.artist, artist.name "
153        "FROM file_join, file, album "
154        "LEFT OUTER JOIN artist ON album.artist = artist.id "
155        "WHERE file.id = file_join.file "
156        "AND file_join.album = album.id "
157        "%1 "
158        "%2 %3 %4"
159        ).arg( sourceToken )
160         .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
161         .arg( m_sortDescending ? "DESC" : QString() )
162         .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
163
164    query.prepare( sql );
165    query.exec();
166
167    while( query.next() )
168    {
169        Tomahawk::artist_ptr artist = Tomahawk::Artist::get( query.value( 2 ).toUInt(), query.value( 3 ).toString() );
170        Tomahawk::album_ptr album = Tomahawk::Album::get( query.value( 0 ).toUInt(), query.value( 1 ).toString(), artist );
171
172        al << album;
173    }
174
175    emit albums( al, data() );
176    emit albums( al );
177    emit done();
178}
179
180
181void
182DatabaseCommand_AllAlbums::exec( DatabaseImpl* dbi )
183{
184    if ( !m_artist.isNull() )
185    {
186        execForArtist( dbi );
187    }
188    else
189    {
190        execForCollection( dbi );
191    }
192}
193
194}