PageRenderTime 241ms CodeModel.GetById 80ms app.highlight 118ms RepoModel.GetById 33ms app.codeStats 1ms

/src/libtomahawk/database/DatabaseCommand_LoadAllPlaylists.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 147 lines | 101 code | 28 blank | 18 comment | 6 complexity | fd5c6bc2ee8cf651d0224bc27e41aa31 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 2011, Leo Franchi <lfranchi@kde.org>
  5 *
  6 *   Tomahawk is free software: you can redistribute it and/or modify
  7 *   it under the terms of the GNU General Public License as published by
  8 *   the Free Software Foundation, either version 3 of the License, or
  9 *   (at your option) any later version.
 10 *
 11 *   Tomahawk is distributed in the hope that it will be useful,
 12 *   but WITHOUT ANY WARRANTY; without even the implied warranty of
 13 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 14 *   GNU General Public License for more details.
 15 *
 16 *   You should have received a copy of the GNU General Public License
 17 *   along with Tomahawk. If not, see <http://www.gnu.org/licenses/>.
 18 */
 19
 20#include "DatabaseCommand_LoadAllPlaylists_p.h"
 21
 22#include "utils/Json.h"
 23
 24#include "DatabaseImpl.h"
 25#include "Playlist.h"
 26#include "PlaylistEntry.h"
 27#include "Source.h"
 28
 29#include <QSqlQuery>
 30
 31using namespace Tomahawk;
 32
 33
 34DatabaseCommand_LoadAllPlaylists::DatabaseCommand_LoadAllPlaylists( const source_ptr& s, QObject* parent )
 35    : DatabaseCommand( parent, new DatabaseCommand_LoadAllPlaylistsPrivate( this, s ) )
 36{
 37    qRegisterMetaType< QHash< Tomahawk::playlist_ptr, QStringList > >("QHash< Tomahawk::playlist_ptr, QStringList >");
 38}
 39
 40
 41void
 42DatabaseCommand_LoadAllPlaylists::exec( DatabaseImpl* dbi )
 43{
 44    Q_D( DatabaseCommand_LoadAllPlaylists );
 45
 46    TomahawkSqlQuery query = dbi->newquery();
 47    QString orderToken, sourceToken;
 48
 49    switch ( d->sortOrder )
 50    {
 51    case 0:
 52        break;
 53
 54    case ModificationTime:
 55            orderToken = "ORDER BY playlist.createdOn";
 56    }
 57
 58    if ( !source().isNull() )
 59        sourceToken = QString( "AND source %1 " ).arg( source()->isLocal() ? "IS NULL" : QString( "= %1" ).arg( source()->id() ) );
 60
 61    QString trackIdJoin;
 62    QString trackIdFields;
 63    if ( d->returnPlEntryIds )
 64    {
 65        trackIdFields = ", pr.entries";
 66        trackIdJoin = "JOIN playlist_revision pr ON pr.playlist = p.guid AND pr.guid = p.currentrevision";
 67    }
 68
 69    query.exec( QString( " SELECT p.guid, p.title, p.info, p.creator, p.lastmodified, p.shared, p.currentrevision, p.createdOn %6 "
 70                         " FROM playlist p "
 71                         " %5 "
 72                         " WHERE ( ( dynplaylist = 'false' ) OR ( dynplaylist = 0 ) ) "
 73                         " %1 "
 74                         " %2 %3 %4 "
 75                         )
 76                .arg( sourceToken )
 77                .arg( orderToken )
 78                .arg( d->sortDescending ? "DESC" : QString() )
 79                .arg( d->limitAmount > 0 ? QString( "LIMIT 0, %1" ).arg( d->limitAmount ) : QString() )
 80                .arg( trackIdJoin )
 81                .arg( trackIdFields )
 82                );
 83
 84    QList<playlist_ptr> plists;
 85    QHash<playlist_ptr, QStringList> phash;
 86    while ( query.next() )
 87    {
 88        playlist_ptr p( new Playlist( source(),                  //src
 89                                      query.value(6).toString(), //current rev
 90                                      query.value(1).toString(), //title
 91                                      query.value(2).toString(), //info
 92                                      query.value(3).toString(), //creator
 93                                      query.value(7).toInt(),    //lastmod / createdOn
 94                                      query.value(5).toBool(),   //shared
 95                                      query.value(4).toInt(),    //lastmod
 96                                      query.value(0).toString()  //GUID
 97                                    ), &QObject::deleteLater );
 98        p->setWeakSelf( p.toWeakRef() );
 99        plists.append( p );
100
101        if ( d->returnPlEntryIds )
102        {
103            QStringList trackIds = TomahawkUtils::parseJson( query.value( 8 ).toByteArray() ).toStringList();
104            phash.insert( p, trackIds );
105        }
106    }
107
108    emit done( plists );
109
110    if ( d->returnPlEntryIds )
111    {
112        emit done( phash );
113    }
114}
115
116
117void
118DatabaseCommand_LoadAllPlaylists::setLimit( unsigned int limit )
119{
120    Q_D( DatabaseCommand_LoadAllPlaylists );
121    d->limitAmount = limit;
122}
123
124
125void
126DatabaseCommand_LoadAllPlaylists::setSortOrder( DatabaseCommand_LoadAllPlaylists::SortOrder order )
127{
128    Q_D( DatabaseCommand_LoadAllPlaylists );
129    d->sortOrder = order;
130}
131
132
133void
134DatabaseCommand_LoadAllPlaylists::setSortDescending( bool descending )
135{
136    Q_D( DatabaseCommand_LoadAllPlaylists );
137    d->sortDescending = descending;
138}
139
140
141void
142DatabaseCommand_LoadAllPlaylists::setReturnPlEntryIds( bool returnPlEntryIds )
143{
144    Q_D( DatabaseCommand_LoadAllPlaylists );
145    d->returnPlEntryIds = returnPlEntryIds;
146}
147