PageRenderTime 83ms CodeModel.GetById 40ms app.highlight 8ms RepoModel.GetById 32ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_LoadAllAutoPlaylists.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 86 lines | 53 code | 16 blank | 17 comment | 3 complexity | 2d5ec1436305041fccbae38e12a2ed5d 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_LoadAllAutoPlaylists.h"
20
21#include "playlist/dynamic/DynamicPlaylist.h"
22#include "utils/Logger.h"
23
24#include "DatabaseImpl.h"
25#include "PlaylistEntry.h"
26#include "Source.h"
27
28#include <QSqlQuery>
29
30
31using namespace Tomahawk;
32
33
34void
35DatabaseCommand_LoadAllAutoPlaylists::exec( DatabaseImpl* dbi )
36{
37    TomahawkSqlQuery query = dbi->newquery();
38    QString orderToken, sourceToken;
39
40    switch ( m_sortOrder )
41    {
42        case 0:
43            break;
44
45        case DatabaseCommand_LoadAllPlaylists::ModificationTime:
46            orderToken = "playlist.createdOn";
47    }
48
49    if ( !source().isNull() )
50        sourceToken = QString( "AND source %1 " ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) );
51
52
53    query.exec( QString( "SELECT playlist.guid as guid, title, info, creator, createdOn, lastmodified, shared, currentrevision, dynamic_playlist.pltype, dynamic_playlist.plmode "
54                         "FROM playlist, dynamic_playlist WHERE "
55                         "(dynplaylist = 'true' OR dynplaylist = 1) "
56                         "AND playlist.guid = dynamic_playlist.guid "
57                         "AND dynamic_playlist.plmode = %1 "
58                         "AND (dynamic_playlist.autoload = 'true' OR dynamic_playlist.autoload = 1) "
59                         "%2"
60                         "%3 %4 %5"
61                       )
62                       .arg( Static )
63                       .arg( sourceToken )
64                       .arg( m_sortOrder > 0 ? QString( "ORDER BY %1" ).arg( orderToken ) : QString() )
65                       .arg( m_sortDescending ? "DESC" : QString() )
66                       .arg( m_limitAmount > 0 ? QString( "LIMIT 0, %1" ).arg( m_limitAmount ) : QString() ) );
67
68    QList<dynplaylist_ptr> plists;
69    while ( query.next() )
70    {
71            QVariantList data = QVariantList()  <<      query.value(7).toString()  //current rev
72                                                <<      query.value(1).toString()  //title
73                                                <<      query.value(2).toString()  //info
74                                                <<      query.value(3).toString()  //creator
75                                                <<      query.value(4).toString()  //createdOn
76                                                <<      query.value(8).toString()  // dynamic type
77                                                <<      static_cast<GeneratorMode>(query.value(9).toInt())  // dynamic mode
78                                                <<      query.value(6).toBool()    //shared
79                                                <<      query.value(5).toInt()     //lastmod
80                                                <<      query.value(0).toString();  //GUID
81            emit autoPlaylistLoaded( source(), data );
82    }
83
84    emit done();
85}
86