From b7b5e60f982581538ae71738355557f3f8dce20a Mon Sep 17 00:00:00 2001
From: Leo Franchi <lfranchi@kde.org>
Date: Fri, 13 Apr 2012 23:06:39 -0400
Subject: [PATCH] notifications for track moved

---
 .../spotify/SpotifyPlaylistUpdater.cpp        | 12 ++++++++
 src/accounts/spotify/SpotifyPlaylistUpdater.h |  1 +
 src/libtomahawk/playlist.h                    |  4 +++
 src/libtomahawk/playlist/playlistmodel.cpp    | 29 ++++++++++++++++++-
 4 files changed, 45 insertions(+), 1 deletion(-)

diff --git a/src/accounts/spotify/SpotifyPlaylistUpdater.cpp b/src/accounts/spotify/SpotifyPlaylistUpdater.cpp
index d01743f52..6254156ec 100644
--- a/src/accounts/spotify/SpotifyPlaylistUpdater.cpp
+++ b/src/accounts/spotify/SpotifyPlaylistUpdater.cpp
@@ -85,6 +85,7 @@ SpotifyPlaylistUpdater::init()
 
     connect( playlist().data(), SIGNAL( tracksInserted( QList<Tomahawk::plentry_ptr>, int ) ), this, SLOT( tomahawkTracksInserted( QList<Tomahawk::plentry_ptr>, int ) ) );
     connect( playlist().data(), SIGNAL( tracksRemoved( QList<Tomahawk::query_ptr> ) ), this, SLOT( tomahawkTracksRemoved( QList<Tomahawk::query_ptr> ) ) );
+    connect( playlist().data(), SIGNAL( tracksMoved( QList<Tomahawk::plentry_ptr>, int ) ), this, SLOT( tomahawkTracksMoved( QList<Tomahawk::plentry_ptr>, int ) ) );
     connect( playlist().data(), SIGNAL( renamed( QString, QString ) ), this, SLOT( tomahawkPlaylistRenamed( QString, QString ) ) );
     connect( playlist().data(), SIGNAL( revisionLoaded( Tomahawk::PlaylistRevision ) ), this, SLOT( playlistRevisionLoaded() ), Qt::QueuedConnection ); // Queued so that in playlist.cpp:443 we let the playlist clear its own queue first
     // TODO reorders in a playlist
@@ -554,6 +555,17 @@ SpotifyPlaylistUpdater::onTracksRemovedReturn( const QString& msgType, const QVa
 }
 
 
+void
+SpotifyPlaylistUpdater::tomahawkTracksMoved( const QList< plentry_ptr >& tracks, int position )
+{
+    qDebug() << Q_FUNC_INFO << "Got tracks moved at position:" << position;
+    foreach ( const plentry_ptr ple, tracks )
+    {
+        qDebug() << ple->query()->track() << ple->query()->artist();
+    }
+}
+
+
 QVariantList
 SpotifyPlaylistUpdater::queriesToVariant( const QList< query_ptr >& queries )
 {
diff --git a/src/accounts/spotify/SpotifyPlaylistUpdater.h b/src/accounts/spotify/SpotifyPlaylistUpdater.h
index 75a8cc429..e2c4e6fd7 100644
--- a/src/accounts/spotify/SpotifyPlaylistUpdater.h
+++ b/src/accounts/spotify/SpotifyPlaylistUpdater.h
@@ -69,6 +69,7 @@ protected:
 private slots:
     void tomahawkTracksInserted( const QList<Tomahawk::plentry_ptr>& ,int );
     void tomahawkTracksRemoved( const QList<Tomahawk::query_ptr>& );
+    void tomahawkTracksMoved( const QList<Tomahawk::plentry_ptr>& ,int );
     void tomahawkPlaylistRenamed( const QString&, const QString& );
     // SpotifyResolver message handlers, all take msgtype, msg as argument
     void onTracksInsertedReturn( const QString& msgType, const QVariantMap& msg );
diff --git a/src/libtomahawk/playlist.h b/src/libtomahawk/playlist.h
index 948d35bb1..304382295 100644
--- a/src/libtomahawk/playlist.h
+++ b/src/libtomahawk/playlist.h
@@ -220,6 +220,10 @@ signals:
     /// Notification for tracks being removed from playlist
     void tracksRemoved( const QList< Tomahawk::query_ptr >& tracks );
 
+    /// Notification for tracks being moved in a playlist. List is of new tracks, and new position of first track
+    /// Contiguous range from startPosition
+    void tracksMoved( const QList< Tomahawk::plentry_ptr >& tracks, int startPosition );
+
 public slots:
     // want to update the playlist from the model?
     // generate a newrev using uuid() and call this:
diff --git a/src/libtomahawk/playlist/playlistmodel.cpp b/src/libtomahawk/playlist/playlistmodel.cpp
index 26c696ccf..13ef1a9d9 100644
--- a/src/libtomahawk/playlist/playlistmodel.cpp
+++ b/src/libtomahawk/playlist/playlistmodel.cpp
@@ -428,7 +428,34 @@ PlaylistModel::endPlaylistChanges()
         m_playlist->createNewRevision( newrev, m_playlist->currentrevision(), l );
     }
 
-    if ( m_savedInsertPos >= 0 )
+    if ( m_savedInsertPos >= 0 && !m_savedInsertTracks.isEmpty() &&
+         !m_savedRemoveTracks.isEmpty() )
+    {
+        // If we have *both* an insert and remove, then it's a move action
+        // However, since we got the insert before the remove (Qt...), the index we have as the saved
+        // insert position is no longer valid. Find the proper one by finding the location of the first inserted
+        // track
+        for ( int i = 0; i < rowCount( QModelIndex() ); i++ )
+        {
+            const QModelIndex idx = index( i, 0, QModelIndex() );
+            if ( !idx.isValid() )
+                continue;
+            const TrackModelItem* item = itemFromIndex( idx );
+            if ( !item || item->entry().isNull() )
+                continue;
+
+            if ( item->entry() == m_savedInsertTracks.first() )
+            {
+                // Found our index
+                emit m_playlist->tracksMoved( m_savedInsertTracks, i );
+                break;
+            }
+        }
+        m_savedInsertPos = -1;
+        m_savedInsertTracks.clear();
+        m_savedRemoveTracks.clear();
+    }
+    else if ( m_savedInsertPos >= 0 )
     {
         emit m_playlist->tracksInserted( m_savedInsertTracks, m_savedInsertPos );
         m_savedInsertPos = -1;