[banshee] Fix skipping removed/deleted tracks (bgo#635951)



commit 958a5e2743341ec87a6d17714815ca28f1ce6a1a
Author: Gabriel Burt <gabriel burt gmail com>
Date:   Tue Mar 22 15:28:58 2011 -0500

    Fix skipping removed/deleted tracks (bgo#635951)
    
    Stop playback if Stop When Finished is activated, otherwise go to the
    next song, which in non-shuffle mode is the first not-removed song.

 .../DatabaseTrackListModel.cs                      |    8 +++
 .../Banshee.Sources/DatabaseSource.cs              |   46 ++++++++++++++++++++
 .../Banshee.Sources/PrimarySource.cs               |    2 +
 .../Banshee.Gui/TrackActions.cs                    |   13 ------
 4 files changed, 56 insertions(+), 13 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackListModel.cs b/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackListModel.cs
index 733aec2..a26ada3 100644
--- a/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackListModel.cs
+++ b/src/Core/Banshee.Services/Banshee.Collection.Database/DatabaseTrackListModel.cs
@@ -358,6 +358,14 @@ namespace Banshee.Collection.Database
             protected set { query_fields = value; }
         }
 
+        public bool Contains (DatabaseTrackInfo track)
+        {
+            return track != null && connection.Query<bool> (
+                String.Format ("SELECT COUNT(*) > 0 {0} AND CoreTracks.TrackID = ? LIMIT 1", UnfilteredQuery),
+                track.TrackId
+            );
+        }
+
         public int IndexOf (QueryNode query, long offset)
         {
             lock (this) {
diff --git a/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs b/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
index fed5a0a..e18a9b1 100644
--- a/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
+++ b/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
@@ -28,6 +28,7 @@
 //
 
 using System;
+using System.Linq;
 using System.Text;
 using System.Collections.Generic;
 
@@ -422,6 +423,7 @@ namespace Banshee.Sources
         public void RemoveTrack (int index)
         {
             if (index != -1) {
+                first_nonremoved_track = TrackModel[index - 1];
                 RemoveTrackRange (track_model, new RangeCollection.Range (index, index));
                 OnTracksRemoved ();
             }
@@ -439,10 +441,27 @@ namespace Banshee.Sources
 
         public void RemoveTracks (DatabaseTrackListModel model, Selection selection)
         {
+            FindFirstNotRemovedTrack (model, selection);
             WithTrackSelection (model, selection, RemoveTrackRange);
             OnTracksRemoved ();
         }
 
+        protected void FindFirstNotRemovedTrack (DatabaseTrackListModel model, Selection selection)
+        {
+            first_nonremoved_track = null;
+
+            var playback_src = ServiceManager.PlaybackController.Source as DatabaseSource;
+            if (playback_src != this && playback_src.Parent != this)
+                return;
+
+            int i = model.IndexOf (ServiceManager.PlayerEngine.CurrentTrack);
+            if (!selection.Contains (i))
+                return;
+
+            var range = selection.Ranges.First (r => r.Start <= i && i <= r.End);
+            first_nonremoved_track = model[range.Start - 1];
+        }
+
         public void DeleteTracks (Selection selection)
         {
             DeleteTracks (track_model, selection);
@@ -453,6 +472,7 @@ namespace Banshee.Sources
             if (model == null)
                 return;
 
+            FindFirstNotRemovedTrack (model, selection);
             WithTrackSelection (model, selection, DeleteTrackRange);
             OnTracksDeleted ();
         }
@@ -551,6 +571,7 @@ namespace Banshee.Sources
             }
         }
 
+        TrackInfo first_nonremoved_track = null;
         protected virtual void OnTracksDeleted ()
         {
             PruneArtistsAlbums ();
@@ -558,12 +579,37 @@ namespace Banshee.Sources
             foreach (PrimarySource psource in PrimarySources) {
                 psource.NotifyTracksDeleted ();
             }
+            SkipTrackIfRemoved ();
         }
 
         protected virtual void OnTracksRemoved ()
         {
             PruneArtistsAlbums ();
             Reload ();
+            SkipTrackIfRemoved ();
+        }
+
+        protected void SkipTrackIfRemoved ()
+        {
+            var playback_src = ServiceManager.PlaybackController.Source as DatabaseSource;
+            if (playback_src != this && playback_src.Parent != this)
+                return;
+
+            var track = ServiceManager.PlayerEngine.CurrentTrack as DatabaseTrackInfo;
+            if (track == null || playback_src.DatabaseTrackModel.Contains (track))
+                return;
+
+            Log.DebugFormat ("Playing track was removed from {0}, so stopping it", this);
+            if (ServiceManager.PlaybackController.StopWhenFinished) {
+                ServiceManager.PlayerEngine.Close ();
+            } else {
+                // Set the PriorTrack to the track before the one we removed, so that, if we're in non-shuffle mode,
+                // we'll pick the first non-removed track after the one that was playing
+                if (first_nonremoved_track != null) {
+                    ServiceManager.PlaybackController.PriorTrack = first_nonremoved_track;
+                }
+                ServiceManager.PlaybackController.Next ();
+            }
         }
 
         // If we are a PrimarySource, return ourself and our children, otherwise if our Parent
diff --git a/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs b/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs
index 7719513..a27e7ca 100644
--- a/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs
+++ b/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs
@@ -421,6 +421,8 @@ namespace Banshee.Sources
                 if (handler != null) {
                     handler (this, new TrackEventArgs ());
                 }
+
+                SkipTrackIfRemoved ();
             });
         }
 
diff --git a/src/Core/Banshee.ThickClient/Banshee.Gui/TrackActions.cs b/src/Core/Banshee.ThickClient/Banshee.Gui/TrackActions.cs
index 2eafd49..8f6db16 100644
--- a/src/Core/Banshee.ThickClient/Banshee.Gui/TrackActions.cs
+++ b/src/Core/Banshee.ThickClient/Banshee.Gui/TrackActions.cs
@@ -502,13 +502,6 @@ namespace Banshee.Gui
                         return;
                     }
 
-                    foreach (var track in source.TrackModel.SelectedItems) {
-                        if (track.IsPlaying) {
-                            ServiceManager.PlayerEngine.Close ();
-                            break;
-                        }
-                    }
-
                     ThreadAssist.SpawnFromMain (delegate {
                         library.RemoveTracks (source.TrackModel as DatabaseTrackListModel, Selection);
                     });
@@ -559,12 +552,6 @@ namespace Banshee.Gui
                 return;
 
             if (source != null && source.CanDeleteTracks) {
-                foreach (var track in source.TrackModel.SelectedItems) {
-                    if (track.IsPlaying) {
-                        ServiceManager.PlayerEngine.Close ();
-                        break;
-                    }
-                }
                 source.DeleteTracks (Selection);
             }
         }



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]