PageRenderTime 309ms CodeModel.GetById 120ms app.highlight 108ms RepoModel.GetById 74ms app.codeStats 1ms

/src/libtomahawk/database/DatabaseCommand_LoadAllSortedPlaylists.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 77 lines | 47 code | 13 blank | 17 comment | 4 complexity | 7b833856a22edc3dc68287929d77218d MD5 | raw file
 1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
 2 *
 3 *   Copyright 2010-2011, Leo Franchi <lfranchi@kde.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_LoadAllSortedPlaylists.h"
20#include "DatabaseImpl.h"
21
22#include "Playlist.h"
23#include "SourceList.h"
24
25using namespace Tomahawk;
26
27void
28DatabaseCommand_LoadAllSortedPlaylists::exec( DatabaseImpl* dbi )
29{
30    TomahawkSqlQuery query = dbi->newquery();
31    QString orderToken, sourceToken, ascDescToken;
32
33    switch ( m_sortOrder )
34    {
35        case 0:
36            break;
37
38        case DatabaseCommand_LoadAllPlaylists::ModificationTime:
39            orderToken = "playlist.createdOn";
40    }
41
42    switch ( m_sortAscDesc )
43    {
44        case DatabaseCommand_LoadAllPlaylists::NoOrder:
45            break;
46        case DatabaseCommand_LoadAllPlaylists::Ascending:
47            ascDescToken = "ASC";
48            break;
49        case DatabaseCommand_LoadAllPlaylists::Descending:
50            ascDescToken = "DESC";
51            break;
52    }
53
54    if ( !source().isNull() )
55        sourceToken = QString( "AND source %1 " ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) );
56
57
58    query.exec( QString( "SELECT playlist.guid as guid, title, info, creator, lastmodified, shared, currentrevision, createdOn, dynplaylist, source, dynamic_playlist.pltype, dynamic_playlist.plmode "
59                         "FROM playlist "
60                         "LEFT JOIN dynamic_playlist ON playlist.guid = dynamic_playlist.guid  "
61                         "%1 "
62                         "%2 %3 %4"
63    )
64    .arg( sourceToken )
65    .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
66    .arg( ascDescToken )
67    .arg( m_limitAmount > 0 ? QString( "LIMIT 0, %1" ).arg( m_limitAmount ) : QString() ) );
68
69    QList<SourcePlaylistPair> plists;
70    while ( query.next() )
71    {
72        plists << QPair< int, QString >( query.value(9).toInt(), query.value(0).toString() );
73
74    }
75
76    emit done( plists );
77}