PageRenderTime 254ms CodeModel.GetById 81ms app.highlight 92ms RepoModel.GetById 79ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_LoadPlaylistEntries.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 142 lines | 97 code | 25 blank | 20 comment | 12 complexity | bd4ca5ac4f6ee021f6bf9adc8b05e876 MD5 | raw file
  1/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
  2 *
  3 *   Copyright 2010-2015, 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_LoadPlaylistEntries.h"
 20
 21#include "utils/Json.h"
 22#include "utils/Logger.h"
 23
 24#include "DatabaseImpl.h"
 25#include "PlaylistEntry.h"
 26#include "Query.h"
 27#include "Source.h"
 28
 29#include <QSqlQuery>
 30
 31using namespace Tomahawk;
 32
 33
 34void
 35DatabaseCommand_LoadPlaylistEntries::exec( DatabaseImpl* dbi )
 36{
 37    generateEntries( dbi );
 38
 39    emit done( m_revguid, m_guids, m_oldentries, m_islatest, m_entrymap, true );
 40}
 41
 42
 43void
 44DatabaseCommand_LoadPlaylistEntries::generateEntries( DatabaseImpl* dbi )
 45{
 46    TomahawkSqlQuery query_entries = dbi->newquery();
 47    query_entries.prepare( "SELECT entries, playlist, author, timestamp, previous_revision "
 48                           "FROM playlist_revision "
 49                           "WHERE guid = :guid" );
 50    query_entries.bindValue( ":guid", m_revguid );
 51    query_entries.exec();
 52
 53    tLog( LOGVERBOSE ) << "trying to load playlist entries for guid:" << m_revguid;
 54    QString prevrev;
 55    bool ok;
 56
 57    if ( query_entries.next() )
 58    {
 59        if ( !query_entries.value( 0 ).isNull() )
 60        {
 61            // entries should be a list of strings:
 62            QVariant v = TomahawkUtils::parseJson( query_entries.value( 0 ).toByteArray(), &ok );
 63            Q_ASSERT( ok && v.type() == QVariant::List ); //TODO
 64
 65            m_guids = v.toStringList();
 66            QString inclause = QString( "('%1')" ).arg( m_guids.join( "', '" ) );
 67
 68            TomahawkSqlQuery query = dbi->newquery();
 69            QString sql = QString( "SELECT guid, trackname, artistname, albumname, annotation, "
 70                                "duration, addedon, addedby, result_hint "
 71                                "FROM playlist_item "
 72                                "WHERE guid IN %1" ).arg( inclause );
 73
 74            query.exec( sql );
 75            while ( query.next() )
 76            {
 77                plentry_ptr e( new PlaylistEntry );
 78                e->setGuid( query.value( 0 ).toString() );
 79                e->setAnnotation( query.value( 4 ).toString() );
 80                e->setDuration( query.value( 5 ).toUInt() );
 81                e->setLastmodified( 0 ); // TODO e->lastmodified = query.value( 6 ).toInt();
 82                const QString resultHint = query.value( 8 ).toString();
 83                e->setResultHint( resultHint );
 84
 85                Tomahawk::query_ptr q = Tomahawk::Query::get( query.value( 2 ).toString(), query.value( 1 ).toString(), query.value( 3 ).toString() );
 86                if ( q.isNull() )
 87                    continue;
 88
 89                q->setResultHint( resultHint );
 90                if ( resultHint.startsWith( "http" ) )
 91                    q->setSaveHTTPResultHint( true );
 92
 93                q->setProperty( "annotation", e->annotation() );
 94                e->setQuery( q );
 95
 96                m_entrymap.insert( e->guid(), e );
 97            }
 98        }
 99
100        prevrev = query_entries.value( 4 ).toString();
101    }
102    else
103    {
104//        qDebug() << "Playlist has no current revision data";
105    }
106
107    if ( !prevrev.isEmpty() )
108    {
109        TomahawkSqlQuery query_entries_old = dbi->newquery();
110        query_entries_old.prepare( "SELECT entries, "
111                                   "(SELECT currentrevision = ? FROM playlist WHERE guid = ?) "
112                                   "FROM playlist_revision "
113                                   "WHERE guid = ?" );
114        query_entries_old.addBindValue( m_revguid );
115        query_entries_old.addBindValue( query_entries.value( 1 ).toString() );
116        query_entries_old.addBindValue( prevrev );
117
118        query_entries_old.exec();
119        if ( !query_entries_old.next() )
120        {
121            return;
122            Q_ASSERT( false );
123        }
124
125        if ( !query_entries_old.value( 0 ).isNull() )
126        {
127            QVariant v = TomahawkUtils::parseJson( query_entries_old.value( 0 ).toByteArray(), &ok );
128            Q_ASSERT( ok && v.type() == QVariant::List ); //TODO
129            m_oldentries = v.toStringList();
130        }
131        m_islatest = query_entries_old.value( 1 ).toBool();
132    }
133
134//    qDebug() << Q_FUNC_INFO << "entrymap:" << m_entrymap;
135}
136
137DatabaseCommand_LoadPlaylistEntries::DatabaseCommand_LoadPlaylistEntries( QString revision_guid, QObject *parent )
138    : DatabaseCommand( parent )
139    , m_islatest( true )
140    , m_revguid( revision_guid )
141{
142}