1
0
mirror of https://github.com/tomahawk-player/tomahawk.git synced 2025-08-04 21:27:58 +02:00

Keep track of spotify IDs of tracks in a synced playlist and update to id api

This commit is contained in:
Leo Franchi
2012-03-27 23:07:58 -04:00
parent 02ff39f595
commit f414e92b74
8 changed files with 77 additions and 31 deletions

View File

@@ -168,7 +168,7 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg
SpotifyPlaylistUpdater* updater = m_updaters[ plid ]; SpotifyPlaylistUpdater* updater = m_updaters[ plid ];
Q_ASSERT( updater->sync() ); Q_ASSERT( updater->sync() );
const int startPos = msg.value( "startPosition" ).toInt(); const QString startPos = msg.value( "startPosition" ).toString();
const QVariantList tracksList = msg.value( "tracks" ).toList(); const QVariantList tracksList = msg.value( "tracks" ).toList();
const QString newRev = msg.value( "revid" ).toString(); const QString newRev = msg.value( "revid" ).toString();
const QString oldRev = msg.value( "oldRev" ).toString(); const QString oldRev = msg.value( "oldRev" ).toString();
@@ -210,7 +210,6 @@ SpotifyAccount::resolverMessage( const QString &msgType, const QVariantMap &msg
const QString newRev = msg.value( "revid" ).toString(); const QString newRev = msg.value( "revid" ).toString();
const QString oldRev = msg.value( "oldRev" ).toString(); const QString oldRev = msg.value( "oldRev" ).toString();
updater->spotifyTracksMoved( tracksList, newRev, oldRev ); updater->spotifyTracksMoved( tracksList, newRev, oldRev );
} }
} }

View File

@@ -47,7 +47,7 @@ SpotifyUpdaterFactory::create( const Tomahawk::playlist_ptr& pl )
} }
// Register the updater with the account // Register the updater with the account
const QString spotifyId = QString( "playlistupdaters/%1" ).arg( pl->guid() ); const QString spotifyId = TomahawkSettings::instance()->value( QString( "playlistupdaters/%1/spotifyId" ).arg( pl->guid() ) ).toString();
Q_ASSERT( !spotifyId.isEmpty() ); Q_ASSERT( !spotifyId.isEmpty() );
SpotifyPlaylistUpdater* updater = new SpotifyPlaylistUpdater( m_account, pl ); SpotifyPlaylistUpdater* updater = new SpotifyPlaylistUpdater( m_account, pl );
m_account->registerUpdaterForPlaylist( spotifyId, updater ); m_account->registerUpdaterForPlaylist( spotifyId, updater );
@@ -140,28 +140,40 @@ SpotifyPlaylistUpdater::sync() const
void void
SpotifyPlaylistUpdater::spotifyTracksAdded( const QVariantList& tracks, int startPos, const QString& newRev, const QString& oldRev ) SpotifyPlaylistUpdater::spotifyTracksAdded( const QVariantList& tracks, const QString& startPosId, const QString& newRev, const QString& oldRev )
{ {
const QList< query_ptr > queries = variantToQueries( tracks ); const QList< query_ptr > queries = variantToQueries( tracks );
qDebug() << Q_FUNC_INFO << "inserting tracks in middle of tomahawk playlist, from spotify command!" << tracks << startPos << newRev << oldRev; qDebug() << Q_FUNC_INFO << "inserting tracks in middle of tomahawk playlist, from spotify command!" << tracks << startPosId << newRev << oldRev;
// Uh oh, dont' want to get out of sync!! // Uh oh, dont' want to get out of sync!!
// Q_ASSERT( m_latestRev == oldRev ); // Q_ASSERT( m_latestRev == oldRev );
// m_latestRev = newRev; // m_latestRev = newRev;
if ( startPos > playlist()->entries().size() ) // Find the position of the track to insert from
startPos = playlist()->entries().size(); int pos = -1;
QList< plentry_ptr > entries = playlist()->entries();
for ( int i = 0; i < entries.size(); i++ )
{
if ( entries[ i ]->annotation() == startPosId )
{
pos = i;
break;
}
}
if ( pos == -1 )
pos = entries.size();
qDebug() << Q_FUNC_INFO << "inserting tracks at position:" << pos;
m_plRevisionBeforeUpdate = playlist()->currentrevision(); m_plRevisionBeforeUpdate = playlist()->currentrevision();
playlist()->insertEntries( queries, startPos, playlist()->currentrevision() ); playlist()->insertEntries( queries, pos, playlist()->currentrevision() );
} }
void void
SpotifyPlaylistUpdater::spotifyTracksRemoved( const QVariantList& trackPositions, const QString& newRev, const QString& oldRev ) SpotifyPlaylistUpdater::spotifyTracksRemoved( const QVariantList& trackIds, const QString& newRev, const QString& oldRev )
{ {
qDebug() << Q_FUNC_INFO << "remove tracks in middle of tomahawk playlist, from spotify command!" << trackPositions << newRev << oldRev; qDebug() << Q_FUNC_INFO << "remove tracks in middle of tomahawk playlist, from spotify command!" << trackIds << newRev << oldRev;
// Uh oh, dont' want to get out of sync!! // Uh oh, dont' want to get out of sync!!
// Q_ASSERT( m_latestRev == oldRev ); // Q_ASSERT( m_latestRev == oldRev );
// m_latestRev = newRev; // m_latestRev = newRev;
@@ -170,17 +182,23 @@ SpotifyPlaylistUpdater::spotifyTracksRemoved( const QVariantList& trackPositions
// Collect list of tracks to remove (can't remove in-place as that might modify the indices) // Collect list of tracks to remove (can't remove in-place as that might modify the indices)
QList<plentry_ptr> toRemove; QList<plentry_ptr> toRemove;
foreach( const QVariant posV, trackPositions ) foreach( const QVariant trackIdV, trackIds )
{ {
bool ok; const QString id = trackIdV.toString();
const int pos = posV.toInt( &ok ); if ( id.isEmpty() )
if ( !ok || pos < 0 || pos >= entries.size() )
{ {
qWarning() << Q_FUNC_INFO << "Tried to get track position to remove, but either couldn't convert to int, or out of bounds:" << ok << pos << entries.size() << entries << trackPositions; qWarning() << Q_FUNC_INFO << "Tried to get track id to remove, but either couldn't convert to qstring:" << trackIdV;
continue; continue;
} }
toRemove << entries.at(pos); foreach ( const plentry_ptr& entry, entries )
{
if ( entry->annotation() == id )
{
toRemove << entry;
break;
}
}
} }
@@ -191,12 +209,13 @@ SpotifyPlaylistUpdater::spotifyTracksRemoved( const QVariantList& trackPositions
m_plRevisionBeforeUpdate = playlist()->currentrevision(); m_plRevisionBeforeUpdate = playlist()->currentrevision();
qDebug() << "We were asked to delete:" << trackPositions.size() << "tracks from the playlist, and we deleted:" << ( playlist()->entries().size() - entries.size() ); const int sizeDiff = playlist()->entries().size() - entries.size();
if ( trackPositions.size() != ( playlist()->entries().size() - entries.size() ) ) qDebug() << "We were asked to delete:" << trackIds.size() << "tracks from the playlist, and we deleted:" << sizeDiff;
if ( trackIds.size() != ( playlist()->entries().size() - entries.size() ) )
qWarning() << "========================= Failed to delete all the tracks we were asked for!! Didn't find some indicesss... ==================="; qWarning() << "========================= Failed to delete all the tracks we were asked for!! Didn't find some indicesss... ===================";
if ( sizeDiff > 0 )
playlist()->createNewRevision( uuid(), playlist()->currentrevision(), entries ); playlist()->createNewRevision( uuid(), playlist()->currentrevision(), entries );
} }
@@ -221,7 +240,20 @@ SpotifyPlaylistUpdater::tomahawkTracksInserted( const QList< plentry_ptr >& trac
QVariantMap msg; QVariantMap msg;
msg[ "_msgtype" ] = "addTracksToPlaylist"; msg[ "_msgtype" ] = "addTracksToPlaylist";
msg[ "oldrev" ] = m_latestRev; msg[ "oldrev" ] = m_latestRev;
msg[ "startPosition" ] = pos;
// Find the trackid of the nearest spotify track
QList< plentry_ptr > plTracks = playlist()->entries();
Q_ASSERT( pos-1 < plTracks.size() );
for ( int i = pos-1; i >= 0; i-- )
{
if ( !plTracks[ i ]->annotation().isEmpty() && plTracks[ i ]->annotation().contains( "spotify:track") )
{
msg[ "startPosition" ] = plTracks[ i ]->annotation();
break;
}
}
msg[ "playlistid" ] = m_spotifyId; msg[ "playlistid" ] = m_spotifyId;
QVariantList tracksJson; QVariantList tracksJson;
@@ -300,8 +332,8 @@ SpotifyPlaylistUpdater::queryToVariant( const query_ptr& query )
m[ "artist" ] = query->artist(); m[ "artist" ] = query->artist();
m[ "album" ] = query->album(); m[ "album" ] = query->album();
if ( !query->property( "spotifytrackid" ).isNull() ) if ( !query->property( "annotation" ).isNull() )
m[ "id" ] = query->property( "spotifytrackid" ); m[ "id" ] = query->property( "annotation" );
return m; return m;
} }
@@ -316,7 +348,7 @@ SpotifyPlaylistUpdater::variantToQueries( const QVariantList& list )
QVariantMap trackMap = blob.toMap(); QVariantMap trackMap = blob.toMap();
const query_ptr q = Query::get( trackMap.value( "artist" ).toString(), trackMap.value( "track" ).toString(), trackMap.value( "album" ).toString(), uuid(), false ); const query_ptr q = Query::get( trackMap.value( "artist" ).toString(), trackMap.value( "track" ).toString(), trackMap.value( "album" ).toString(), uuid(), false );
if ( trackMap.contains( "id" ) ) if ( trackMap.contains( "id" ) )
q->setProperty( "spotifytrackid", trackMap.value( "id" ) ); q->setProperty( "annotation", trackMap.value( "id" ) );
queries << q; queries << q;
} }

View File

@@ -49,7 +49,7 @@ public:
void setSync( bool sync ); void setSync( bool sync );
/// Spotify callbacks when we are directly instructed from the resolver /// Spotify callbacks when we are directly instructed from the resolver
void spotifyTracksAdded( const QVariantList& tracks, int startPos, const QString& newRev, const QString& oldRev ); void spotifyTracksAdded( const QVariantList& tracks, const QString& startPosId, const QString& newRev, const QString& oldRev );
void spotifyTracksRemoved( const QVariantList& tracks, const QString& newRev, const QString& oldRev ); void spotifyTracksRemoved( const QVariantList& tracks, const QString& newRev, const QString& oldRev );
void spotifyTracksMoved( const QVariantList& tracks, const QString& newRev, const QString& oldRev ); void spotifyTracksMoved( const QVariantList& tracks, const QString& newRev, const QString& oldRev );
@@ -68,6 +68,7 @@ private slots:
private: private:
void init(); void init();
static QVariantList queriesToVariant( const QList< Tomahawk::query_ptr >& queries ); static QVariantList queriesToVariant( const QList< Tomahawk::query_ptr >& queries );
static QVariant queryToVariant( const Tomahawk::query_ptr& query ); static QVariant queryToVariant( const Tomahawk::query_ptr& query );
static QList< Tomahawk::query_ptr > variantToQueries( const QVariantList& list ); static QList< Tomahawk::query_ptr > variantToQueries( const QVariantList& list );

View File

@@ -77,6 +77,7 @@ DatabaseCommand_LoadPlaylistEntries::generateEntries( DatabaseImpl* dbi )
Tomahawk::query_ptr q = Tomahawk::Query::get( query.value( 2 ).toString(), query.value( 1 ).toString(), query.value( 3 ).toString() ); Tomahawk::query_ptr q = Tomahawk::Query::get( query.value( 2 ).toString(), query.value( 1 ).toString(), query.value( 3 ).toString() );
q->setResultHint( query.value( 8 ).toString() ); q->setResultHint( query.value( 8 ).toString() );
q->setProperty( "annotation", e->annotation() );
e->setQuery( q ); e->setQuery( q );
m_entrymap.insert( e->guid(), e ); m_entrymap.insert( e->guid(), e );

View File

@@ -1,7 +1,7 @@
/* /*
Copyright (C) 2011 Leo Franchi <lfranchi@kde.org> Copyright (C) 2011 Leo Franchi <lfranchi@kde.org>
Copyright (C) 2011, Jeff Mitchell <jeff@tomahawk-player.org> Copyright (C) 2011, Jeff Mitchell <jeff@tomahawk-player.org>
This program is free software; you can redistribute it and/or modify This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or the Free Software Foundation; either version 2 of the License, or
@@ -974,7 +974,10 @@ GlobalActionManager::doBookmark( const playlist_ptr& pl, const query_ptr& q )
e->setDuration( 0 ); e->setDuration( 0 );
e->setLastmodified( 0 ); e->setLastmodified( 0 );
e->setAnnotation( "" ); // FIXME QString annotation = "";
if ( !q->property( "annotation" ).toString().isEmpty() )
annotation = q->property( "annotation" ).toString();
e->setAnnotation( annotation );
e->setQuery( q ); e->setQuery( q );
pl->createNewRevision( uuid(), pl->currentrevision(), QList< plentry_ptr >( pl->entries() ) << e ); pl->createNewRevision( uuid(), pl->currentrevision(), QList< plentry_ptr >( pl->entries() ) << e );

View File

@@ -181,7 +181,10 @@ Playlist::create( const source_ptr& author,
p->setGuid( uuid() ); p->setGuid( uuid() );
p->setDuration( query->duration() ); p->setDuration( query->duration() );
p->setLastmodified( 0 ); p->setLastmodified( 0 );
p->setAnnotation( "" ); QString annotation = "";
if ( !query->property( "annotation" ).toString().isEmpty() )
annotation = query->property( "annotation" ).toString();
p->setAnnotation( annotation );
p->setQuery( query ); p->setQuery( query );
entries << p; entries << p;
@@ -607,7 +610,10 @@ Playlist::entriesFromQueries( const QList<Tomahawk::query_ptr>& queries, bool cl
e->setDuration( 0 ); e->setDuration( 0 );
e->setLastmodified( 0 ); e->setLastmodified( 0 );
e->setAnnotation( "" ); // FIXME QString annotation = "";
if ( !query->property( "annotation" ).toString().isEmpty() )
annotation = query->property( "annotation" ).toString();
e->setAnnotation( annotation ); // FIXME
e->setQuery( query ); e->setQuery( query );
el << e; el << e;

View File

@@ -60,7 +60,7 @@ class DLLEXPORT XspfUpdaterFactory : public PlaylistUpdaterFactory
public: public:
XspfUpdaterFactory() {} XspfUpdaterFactory() {}
virtual QString type() const { return "type"; } virtual QString type() const { return "xspf"; }
virtual PlaylistUpdaterInterface* create( const playlist_ptr& pl ) { return new XspfUpdater( pl ); } virtual PlaylistUpdaterInterface* create( const playlist_ptr& pl ) { return new XspfUpdater( pl ); }
}; };

View File

@@ -193,7 +193,11 @@ PlaylistModel::insert( const QList< Tomahawk::query_ptr >& queries, int row )
entry->setDuration( 0 ); entry->setDuration( 0 );
entry->setLastmodified( 0 ); entry->setLastmodified( 0 );
entry->setAnnotation( "" ); // FIXME QString annotation = "";
if ( !query->property( "annotation" ).toString().isEmpty() )
annotation = query->property( "annotation" ).toString();
entry->setAnnotation( annotation );
entry->setQuery( query ); entry->setQuery( query );
entry->setGuid( uuid() ); entry->setGuid( uuid() );