~ubuntu-branches/ubuntu/trusty/tomahawk/trusty-proposed

« back to all changes in this revision

Viewing changes to src/libtomahawk/database/DatabaseCommand_AllArtists.cpp

  • Committer: Package Import Robot
  • Author(s): Harald Sitter
  • Date: 2013-03-07 21:50:13 UTC
  • Revision ID: package-import@ubuntu.com-20130307215013-6gdjkdds7i9uenvs
Tags: upstream-0.6.0+dfsg
ImportĀ upstreamĀ versionĀ 0.6.0+dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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_AllArtists.h"
 
20
 
 
21
#include <QSqlQuery>
 
22
 
 
23
#include "Artist.h"
 
24
#include "DatabaseImpl.h"
 
25
#include "Source.h"
 
26
#include "utils/TomahawkUtils.h"
 
27
#include "utils/Logger.h"
 
28
 
 
29
 
 
30
DatabaseCommand_AllArtists::DatabaseCommand_AllArtists( const Tomahawk::collection_ptr& collection, QObject* parent )
 
31
    : DatabaseCommand( parent )
 
32
    , m_collection( collection )
 
33
    , m_amount( 0 )
 
34
    , m_sortOrder( DatabaseCommand_AllArtists::None )
 
35
    , m_sortDescending( false )
 
36
{
 
37
}
 
38
 
 
39
 
 
40
DatabaseCommand_AllArtists::~DatabaseCommand_AllArtists()
 
41
{
 
42
}
 
43
 
 
44
 
 
45
void
 
46
DatabaseCommand_AllArtists::exec( DatabaseImpl* dbi )
 
47
{
 
48
    TomahawkSqlQuery query = dbi->newquery();
 
49
    QString orderToken, sourceToken, filterToken, tables, joins;
 
50
 
 
51
    switch ( m_sortOrder )
 
52
    {
 
53
        case 0:
 
54
            break;
 
55
 
 
56
        case ModificationTime:
 
57
            orderToken = "file.mtime";
 
58
    }
 
59
 
 
60
    if ( !m_collection.isNull() )
 
61
        sourceToken = QString( "AND file.source %1" ).arg( m_collection->source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( m_collection->source()->id() ) );
 
62
 
 
63
    if ( !m_filter.isEmpty() )
 
64
    {
 
65
        QString filtersql;
 
66
        QStringList sl = m_filter.split( " ", QString::SkipEmptyParts );
 
67
        foreach( QString s, sl )
 
68
        {
 
69
            filtersql += QString( " AND ( artist.name LIKE '%%1%' OR album.name LIKE '%%1%' OR track.name LIKE '%%1%' )" ).arg( TomahawkSqlQuery::escape( s ) );
 
70
        }
 
71
 
 
72
        filterToken = QString( "AND file_join.track = track.id %1" ).arg( filtersql );
 
73
        joins = "LEFT JOIN album ON album.id = file_join.album";
 
74
        tables = "artist, track, file, file_join";
 
75
    }
 
76
    else
 
77
        tables = "artist, file, file_join";
 
78
 
 
79
    QString sql = QString(
 
80
            "SELECT DISTINCT artist.id, artist.name "
 
81
            "FROM %1 "
 
82
            "%2 "
 
83
            "WHERE file.id = file_join.file "
 
84
            "AND file_join.artist = artist.id "
 
85
            "%3 %4 %5 %6 %7"
 
86
            ).arg( tables )
 
87
             .arg( joins )
 
88
             .arg( sourceToken )
 
89
             .arg( filterToken )
 
90
             .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
 
91
             .arg( m_sortDescending ? "DESC" : QString() )
 
92
             .arg( m_amount > 0 ? QString( "LIMIT 0, %1" ).arg( m_amount ) : QString() );
 
93
 
 
94
    query.prepare( sql );
 
95
    query.exec();
 
96
 
 
97
    QList<Tomahawk::artist_ptr> al;
 
98
    while ( query.next() )
 
99
    {
 
100
        Tomahawk::artist_ptr artist = Tomahawk::Artist::get( query.value( 0 ).toUInt(), query.value( 1 ).toString() );
 
101
        al << artist;
 
102
    }
 
103
 
 
104
    emit artists( al );
 
105
    emit done();
 
106
}