PageRenderTime 102ms CodeModel.GetById 50ms app.highlight 11ms RepoModel.GetById 39ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_LoadDynamicPlaylistEntries.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 112 lines | 69 code | 16 blank | 27 comment | 6 complexity | 554d44770309841527aa3850506b2e0d 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_LoadDynamicPlaylistEntries.h"
 20
 21#include "playlist/dynamic/DynamicControl.h"
 22#include "playlist/dynamic/GeneratorInterface.h"
 23#include "playlist/dynamic/GeneratorFactory.h"
 24#include "utils/Json.h"
 25#include "utils/Logger.h"
 26
 27#include "DatabaseImpl.h"
 28#include "PlaylistEntry.h"
 29#include "Source.h"
 30#include "TomahawkSqlQuery.h"
 31
 32#include <QSqlQuery>
 33#include <QString>
 34
 35using namespace Tomahawk;
 36
 37
 38void
 39DatabaseCommand_LoadDynamicPlaylistEntries::exec( DatabaseImpl* dbi )
 40{
 41//    qDebug() << "Loading dynamic playlist guid" << guid();
 42    // load the entries first
 43    generateEntries( dbi );
 44
 45    // now load the controls etc
 46
 47    TomahawkSqlQuery controlsQuery = dbi->newquery();
 48    controlsQuery.prepare( "SELECT playlist_revision.playlist, controls, plmode, pltype "
 49                           "FROM dynamic_playlist_revision, playlist_revision "
 50                           "WHERE dynamic_playlist_revision.guid = ? AND playlist_revision.guid = dynamic_playlist_revision.guid" );
 51    controlsQuery.addBindValue( revisionGuid() );
 52    controlsQuery.exec();
 53
 54    QString type;
 55    GeneratorMode mode;
 56
 57    QList< QVariantMap > controls;
 58    QString playlist_guid;
 59//    qDebug() << "Loading controls..." << revisionGuid();
 60//    qDebug() << "SELECT playlist_revision.playlist, controls, plmode, pltype "
 61//    "FROM dynamic_playlist_revision, playlist_revision "
 62//    "WHERE dynamic_playlist_revision.guid = "<< revisionGuid() << " AND playlist_revision.guid = dynamic_playlist_revision.guid";
 63
 64    if ( controlsQuery.first() )
 65    {
 66        playlist_guid = controlsQuery.value( 0 ).toString();
 67        bool ok;
 68        QVariant v = TomahawkUtils::parseJson( controlsQuery.value(1).toByteArray(), &ok );
 69        Q_ASSERT( ok && v.type() == QVariant::List ); //TODO
 70
 71        type = controlsQuery.value( 3 ).toString();
 72        mode = static_cast<GeneratorMode>( controlsQuery.value( 2 ).toInt() );
 73
 74        QStringList controlIds = v.toStringList();
 75//        qDebug() << "Got controls in dynamic playlist, loading:" << controlIds << controlsQuery.value(1);
 76        foreach ( const QString& controlId, controlIds )
 77        {
 78            TomahawkSqlQuery controlQuery = dbi->newquery();
 79            controlQuery.prepare( "SELECT selectedType, match, input "
 80                                  "FROM dynamic_playlist_controls "
 81                                  "WHERE id = :id" );
 82            controlQuery.bindValue( ":id", controlId );
 83            controlQuery.exec();
 84            if ( controlQuery.next() )
 85            {
 86                QVariantMap c;
 87                c[ "type" ] = type;
 88                c[ "id" ] = controlId;
 89                c[ "selectedType" ] = controlQuery.value( 0 ).toString();
 90                c[ "match" ] = controlQuery.value( 1 ).toString();
 91                c[ "input" ] = controlQuery.value( 2 ).toString();
 92                controls << c;
 93            }
 94        }
 95    }
 96    else
 97    {
 98        // No controls or plguid is null, but that's okay. We'll get a setdynrevision command with a proper revision some point later
 99        return;
100    }
101
102    if ( mode == OnDemand )
103    {
104//        Q_ASSERT( m_entrymap.isEmpty() ); // ondemand should have no entry
105
106        emit done( revisionGuid(), m_islatest, type, controls, true );
107    }
108    else
109    {
110        emit done( revisionGuid(), m_guids, m_oldentries, type, controls, m_islatest, m_entrymap, true );
111    }
112}