PageRenderTime 92ms CodeModel.GetById 30ms app.highlight 12ms RepoModel.GetById 48ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_CreateDynamicPlaylist.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 124 lines | 87 code | 20 blank | 17 comment | 11 complexity | fad174fae64f1c211ae34836b7db001d 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_CreateDynamicPlaylist.h"
 20
 21#include "playlist/dynamic/DynamicPlaylist.h"
 22#include "playlist/dynamic/DynamicControl.h"
 23#include "playlist/dynamic/GeneratorInterface.h"
 24#include "network/Servent.h"
 25#include "utils/Json.h"
 26#include "utils/Logger.h"
 27
 28#include "DatabaseImpl.h"
 29#include "PlaylistEntry.h"
 30#include "SourceList.h"
 31#include "TomahawkSqlQuery.h"
 32
 33#include <QSqlQuery>
 34#include <QSqlDriver>
 35
 36using namespace Tomahawk;
 37
 38
 39DatabaseCommand_CreateDynamicPlaylist::DatabaseCommand_CreateDynamicPlaylist( QObject* parent )
 40    : DatabaseCommand_CreatePlaylist( parent )
 41    , m_autoLoad( true )
 42{
 43    tLog( LOGVERBOSE ) << Q_FUNC_INFO << "creating dynamiccreatecommand 1";
 44}
 45
 46
 47DatabaseCommand_CreateDynamicPlaylist::DatabaseCommand_CreateDynamicPlaylist( const source_ptr& author,
 48                                                                const dynplaylist_ptr& playlist, bool autoLoad )
 49    : DatabaseCommand_CreatePlaylist( author, playlist.staticCast<Playlist>() )
 50    , m_playlist( playlist )
 51    , m_autoLoad( autoLoad )
 52{
 53    tLog( LOGVERBOSE ) << Q_FUNC_INFO << "creating dynamiccreatecommand 2";
 54}
 55
 56DatabaseCommand_CreateDynamicPlaylist::~DatabaseCommand_CreateDynamicPlaylist()
 57{}
 58
 59QVariant
 60DatabaseCommand_CreateDynamicPlaylist::playlistV() const
 61{
 62        if( m_v.isNull() )
 63            return TomahawkUtils::qobject2qvariant( (QObject*)m_playlist.data() );
 64        else
 65            return m_v;
 66}
 67
 68void
 69DatabaseCommand_CreateDynamicPlaylist::exec( DatabaseImpl* lib )
 70{
 71    Q_ASSERT( !( m_playlist.isNull() && m_v.isNull() ) );
 72    Q_ASSERT( !source().isNull() );
 73
 74    DatabaseCommand_CreatePlaylist::createPlaylist( lib, true );
 75    tLog( LOGVERBOSE ) << Q_FUNC_INFO << "Created normal playlist, now creating additional dynamic info!";
 76
 77    tLog( LOGVERBOSE ) << Q_FUNC_INFO <<  "Create dynamic execing!" << m_playlist << m_v;
 78    TomahawkSqlQuery cre = lib->newquery();
 79
 80    cre.prepare( "INSERT INTO dynamic_playlist( guid, pltype, plmode, autoload ) "
 81                 "VALUES( ?, ?, ?, ? )" );
 82
 83    if( m_playlist.isNull() ) {
 84        QVariantMap m = m_v.toMap();
 85        cre.addBindValue( m.value( "guid" ) );
 86        cre.addBindValue( m.value( "type" ) );
 87        cre.addBindValue( m.value( "mode" ) );
 88    } else {
 89        cre.addBindValue( m_playlist->guid() );
 90        cre.addBindValue( m_playlist->type() );
 91        cre.addBindValue( m_playlist->mode() );
 92    }
 93    cre.addBindValue( m_autoLoad ? "true" : "false" );
 94    cre.exec();
 95}
 96
 97
 98void
 99DatabaseCommand_CreateDynamicPlaylist::postCommitHook()
100{
101    if ( source().isNull() || source()->dbCollection().isNull() )
102    {
103        tDebug() << "Source has gone offline, not emitting to GUI.";
104        return;
105    }
106
107    if(  !DatabaseCommand_CreatePlaylist::report() || report() == false )
108        return;
109
110    tDebug( LOGVERBOSE ) << Q_FUNC_INFO << "..reporting..";
111    if( m_playlist.isNull() ) {
112        QMetaObject::invokeMethod( SourceList::instance(),
113                                   "createDynamicPlaylist",
114                                   Qt::BlockingQueuedConnection,
115                                   QGenericArgument( "Tomahawk::source_ptr", (const void*)&source() ),
116                                   Q_ARG( QVariant, m_v ) );
117    }
118    else
119    {
120        m_playlist->reportCreated( m_playlist );
121    }
122    if( source()->isLocal() )
123        Servent::instance()->triggerDBSync();
124}