PageRenderTime 123ms CodeModel.GetById 60ms app.highlight 15ms RepoModel.GetById 46ms app.codeStats 0ms

/src/libtomahawk/database/DatabaseCommand_CreatePlaylist.cpp

http://github.com/tomahawk-player/tomahawk
C++ | 143 lines | 102 code | 24 blank | 17 comment | 8 complexity | 39aa49ca1a774f687b26aef9585d4d10 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 *
  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_CreatePlaylist.h"
 20
 21#include "network/Servent.h"
 22#include "utils/Json.h"
 23#include "utils/Logger.h"
 24
 25#include "DatabaseImpl.h"
 26#include "Playlist.h"
 27#include "SourceList.h"
 28#include "TomahawkSqlQuery.h"
 29
 30#include <QDateTime>
 31#include <QSqlQuery>
 32
 33
 34using namespace Tomahawk;
 35
 36
 37DatabaseCommand_CreatePlaylist::DatabaseCommand_CreatePlaylist( QObject* parent )
 38    : DatabaseCommandLoggable( parent )
 39    , m_report( true )
 40{
 41}
 42
 43
 44DatabaseCommand_CreatePlaylist::DatabaseCommand_CreatePlaylist( const source_ptr& author,
 45                                                                const playlist_ptr& playlist )
 46    : DatabaseCommandLoggable( author )
 47    , m_playlist( playlist )
 48    , m_report( false ) //this ctor used when creating locally, reporting done elsewhere
 49{
 50}
 51
 52DatabaseCommand_CreatePlaylist::~DatabaseCommand_CreatePlaylist()
 53{}
 54
 55void
 56DatabaseCommand_CreatePlaylist::exec( DatabaseImpl* lib )
 57{
 58    createPlaylist( lib, false );
 59}
 60
 61QVariant
 62DatabaseCommand_CreatePlaylist::playlistV() const
 63{
 64    if( m_v.isNull() )
 65        return TomahawkUtils::qobject2qvariant( (QObject*)m_playlist.data() );
 66    else
 67        return m_v;
 68 }
 69
 70
 71void
 72DatabaseCommand_CreatePlaylist::postCommitHook()
 73{
 74    qDebug() << Q_FUNC_INFO;
 75
 76    if ( source()->isLocal() )
 77        Servent::instance()->triggerDBSync();
 78
 79    if ( m_report == false )
 80        return;
 81
 82    tDebug() << Q_FUNC_INFO << "reporting...";
 83    if ( m_playlist.isNull() )
 84    {
 85        QMetaObject::invokeMethod( SourceList::instance(),
 86                                   "createPlaylist",
 87                                   Qt::BlockingQueuedConnection,
 88                                   QGenericArgument( "Tomahawk::source_ptr", (const void*)&source() ),
 89                                   Q_ARG( QVariant, m_v ) );
 90    }
 91    else
 92    {
 93        m_playlist->reportCreated( m_playlist );
 94    }
 95}
 96
 97
 98void
 99DatabaseCommand_CreatePlaylist::createPlaylist( DatabaseImpl* lib, bool dynamic)
100{
101    Q_ASSERT( !( m_playlist.isNull() && m_v.isNull() ) );
102    Q_ASSERT( !source().isNull() );
103
104    uint now = 0;
105    if ( m_playlist.isNull() )
106    {
107        now = m_v.toMap()[ "createdon" ].toUInt();
108    }
109    else
110    {
111        now = QDateTime::currentDateTime().toTime_t();
112        m_playlist->setCreatedOn( now );
113    }
114
115    TomahawkSqlQuery cre = lib->newquery();
116    cre.prepare( "INSERT INTO playlist( guid, source, shared, title, info, creator, lastmodified, dynplaylist, createdOn ) "
117                 "VALUES( :guid, :source, :shared, :title, :info, :creator, :lastmodified, :dynplaylist, :createdOn )" );
118
119    cre.bindValue( ":source", source()->isLocal() ? QVariant(QVariant::Int) : source()->id() );
120    cre.bindValue( ":dynplaylist", dynamic ? "true" : "false" );
121    cre.bindValue( ":createdOn", now );
122    if ( !m_playlist.isNull() )
123    {
124        cre.bindValue( ":guid", m_playlist->guid() );
125        cre.bindValue( ":shared", m_playlist->shared() ? "true" : "false" );
126        cre.bindValue( ":title", m_playlist->title() );
127        cre.bindValue( ":info", m_playlist->info() );
128        cre.bindValue( ":creator", m_playlist->creator() );
129        cre.bindValue( ":lastmodified", m_playlist->lastmodified() );
130    }
131    else
132    {
133        QVariantMap m = m_v.toMap();
134        cre.bindValue( ":guid", m.value( "guid" ) );
135        cre.bindValue( ":shared", m.value( "shared" ) );
136        cre.bindValue( ":title", m.value( "title" ) );
137        cre.bindValue( ":info", m.value( "info" ) );
138        cre.bindValue( ":creator", m.value( "creator" ) );
139        cre.bindValue( ":lastmodified", m.value( "lastmodified", 0 ) );
140    }
141
142    cre.exec();
143}