1
/* === This file is part of Tomahawk Player - <http://tomahawk-player.org> ===
3
* Copyright 2010-2011, Leo Franchi <lfranchi@kde.org>
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.
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.
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/>.
19
#include "DatabaseCommand_SetDynamicPlaylistRevision.h"
22
#include "DatabaseImpl.h"
23
#include "TomahawkSqlQuery.h"
24
#include "playlist/dynamic/DynamicPlaylist.h"
25
#include "playlist/dynamic/DynamicControl.h"
26
#include "network/Servent.h"
27
#include "utils/Logger.h"
31
DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s,
32
const QString& playlistguid,
33
const QString& newrev,
34
const QString& oldrev,
35
const QStringList& orderedguids,
36
const QList< plentry_ptr >& addedentries,
37
const QList<plentry_ptr>& entries,
40
const QList< dyncontrol_ptr >& controls )
41
: DatabaseCommand_SetPlaylistRevision( s, playlistguid, newrev, oldrev, orderedguids, addedentries, entries )
44
, m_controls( controls )
51
DatabaseCommand_SetDynamicPlaylistRevision::DatabaseCommand_SetDynamicPlaylistRevision( const Tomahawk::source_ptr& s,
52
const QString& playlistguid,
53
const QString& newrev,
54
const QString& oldrev,
57
const QList< dyncontrol_ptr >& controls )
58
: DatabaseCommand_SetPlaylistRevision( s, playlistguid, newrev, oldrev, QStringList(), QList< plentry_ptr >(), QList< plentry_ptr >() )
61
, m_controls( controls )
69
DatabaseCommand_SetDynamicPlaylistRevision::controlsV()
71
if ( m_controls.isEmpty() )
74
if ( !m_controls.isEmpty() && m_controlsV.isEmpty() )
76
foreach ( const dyncontrol_ptr& control, m_controls )
78
m_controlsV << QJson::QObjectHelper::qobject2qvariant( control.data() );
87
DatabaseCommand_SetDynamicPlaylistRevision::postCommitHook()
89
if ( source().isNull() || source()->collection().isNull() )
91
tDebug() << "Source has gone offline, not emitting to GUI.";
95
QStringList orderedentriesguids;
96
foreach( const QVariant& v, orderedguids() )
97
orderedentriesguids << v.toString();
99
Q_ASSERT( !source().isNull() );
100
Q_ASSERT( !source()->collection().isNull() );
101
tLog() << "Postcommitting this playlist:" << playlistguid() << source().isNull();
103
// private, but we are a friend. will recall itself in its own thread:
104
dynplaylist_ptr playlist = source()->collection()->autoPlaylist( playlistguid() );
105
if ( playlist.isNull() )
106
playlist = source()->collection()->station( playlistguid() );
107
// UGH we don't have a sharedptr from DynamicPlaylist+
109
DynamicPlaylist* rawPl = playlist.data();
110
if( playlist.isNull() ) // if it's neither an auto or station, it must not be auto-loaded, so we MUST have been told about it directly
115
tLog() <<"Got null playlist with guid:" << playlistguid() << "from source and collection:" << source()->friendlyName() << source()->collection()->name() << "and mode is static?:" << (m_mode == Static);
120
if ( !m_controlsV.isEmpty() && m_controls.isEmpty() )
122
QList<QVariantMap> controlMap;
123
foreach( const QVariant& v, m_controlsV )
124
controlMap << v.toMap();
126
if ( m_mode == OnDemand )
127
rawPl->setRevision( newrev(),
128
true, // this *is* the newest revision so far
133
rawPl->setRevision( newrev(),
135
m_previous_rev_orderedguids,
138
true, // this *is* the newest revision so far
144
if ( m_mode == OnDemand )
145
rawPl->setRevision( newrev(),
146
true, // this *is* the newest revision so far
151
rawPl->setRevision( newrev(),
153
m_previous_rev_orderedguids,
156
true, // this *is* the newest revision so far
161
if ( source()->isLocal() )
162
Servent::instance()->triggerDBSync();
167
DatabaseCommand_SetDynamicPlaylistRevision::exec( DatabaseImpl* lib )
169
DatabaseCommand_SetPlaylistRevision::exec( lib );
171
QVariantList newcontrols;
172
if ( m_controlsV.isEmpty() && !m_controls.isEmpty() )
174
foreach( const dyncontrol_ptr& control, m_controls )
176
newcontrols << control->id();
179
else if( !m_controlsV.isEmpty() )
181
foreach( const QVariant& v, m_controlsV )
183
newcontrols << v.toMap().value( "id" );
187
QJson::Serializer ser;
188
const QByteArray newcontrols_data = ser.serialize( newcontrols );
190
TomahawkSqlQuery query = lib->newquery();
191
QString sql = "INSERT INTO dynamic_playlist_revision (guid, controls, plmode, pltype) "
192
"VALUES(?, ?, ?, ?)";
194
query.prepare( sql );
195
query.addBindValue( m_newrev );
196
query.addBindValue( newcontrols_data );
197
query.addBindValue( QString::number( (int) m_mode ) );
198
query.addBindValue( m_type );
201
// delete all the old controls, replace with new onws
202
qDebug() << "Deleting controls with playlist id" << m_playlistguid;
203
TomahawkSqlQuery delQuery = lib->newquery();
204
delQuery.prepare( "DELETE FROM dynamic_playlist_controls WHERE playlist = ?" );
205
delQuery.addBindValue( m_playlistguid );
206
if ( !delQuery.exec() )
207
tLog() << "Failed to delete controls from dynamic playlist controls table";
209
TomahawkSqlQuery controlsQuery = lib->newquery();
210
controlsQuery.prepare( "INSERT INTO dynamic_playlist_controls( id, playlist, selectedType, match, input ) "
211
"VALUES( ?, ?, ?, ?, ? )" );
212
if ( m_controlsV.isEmpty() && !m_controls.isEmpty() )
214
foreach ( const dyncontrol_ptr& control, m_controls )
216
qDebug() << "inserting dynamic control:" << control->id() << m_playlistguid << control->selectedType() << control->match() << control->input();
217
controlsQuery.addBindValue( control->id() );
218
controlsQuery.addBindValue( m_playlistguid );
219
controlsQuery.addBindValue( control->selectedType() );
220
controlsQuery.addBindValue( control->match() );
221
controlsQuery.addBindValue( control->input() );
223
controlsQuery.exec();
228
foreach ( const QVariant& v, m_controlsV )
230
QVariantMap control = v.toMap();
231
qDebug() << "inserting dynamic control from JSON:" << control.value( "id" ) << m_playlistguid << control.value( "selectedType" ) << control.value( "match" ) << control.value( "input" );
232
controlsQuery.addBindValue( control.value( "id" ) );
233
controlsQuery.addBindValue( m_playlistguid );
234
controlsQuery.addBindValue( control.value( "selectedType" ) );
235
controlsQuery.addBindValue( control.value( "match" ) );
236
controlsQuery.addBindValue( control.value( "input" ) );
238
controlsQuery.exec();
244
tLog() << "updating dynamic playlist, optimistic locking okay";
246
TomahawkSqlQuery query2 = lib->newquery();
247
query2.prepare( "UPDATE dynamic_playlist SET pltype = ?, plmode = ? WHERE guid = ?" );
248
query2.bindValue( 0, m_type );
249
query2.bindValue( 1, m_mode );
250
query2.bindValue( 2, m_playlistguid );
256
DatabaseCommand_SetDynamicPlaylistRevision::setPlaylist( DynamicPlaylist* pl )