[banshee/stable-1.8] AppleDevice: Remove invalid tracks before syncing (bgo#634652)



commit ecc5a4dd3a808e3681be9f110daefe989a521280
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Wed Dec 22 22:45:50 2010 +0100

    AppleDevice: Remove invalid tracks before syncing (bgo#634652)
    
    It can happen that the device has tracks that are in the database
    but not as physical files. In this case the files have a null IpodPath
    property, which was causing an NRE at the moment of loading the device
    in Banshee. These tracks couldn't be seen anyway in the device's
    interface, so it is safe to remove them (besides, it seems the that
    the existence of them would actually prevent the sync process
    from really happen properly).

 .../Banshee.Dap.AppleDevice/AppleDeviceSource.cs   |   46 ++++++++++++++-----
 1 files changed, 34 insertions(+), 12 deletions(-)
---
diff --git a/src/Dap/Banshee.Dap.AppleDevice/Banshee.Dap.AppleDevice/AppleDeviceSource.cs b/src/Dap/Banshee.Dap.AppleDevice/Banshee.Dap.AppleDevice/AppleDeviceSource.cs
index 8b1ee2d..cdde24f 100644
--- a/src/Dap/Banshee.Dap.AppleDevice/Banshee.Dap.AppleDevice/AppleDeviceSource.cs
+++ b/src/Dap/Banshee.Dap.AppleDevice/Banshee.Dap.AppleDevice/AppleDeviceSource.cs
@@ -196,7 +196,14 @@ namespace Banshee.Dap.AppleDevice
                 });
             }
 
+            var invalid_tracks = new List<GPod.Track> ();
             foreach (var ipod_track in MediaDatabase.Tracks) {
+
+                if (String.IsNullOrEmpty (ipod_track.IpodPath)) {
+                    invalid_tracks.Add (ipod_track);
+                    continue;
+                }
+
                 try {
                     var track = new AppleDeviceTrackInfo (ipod_track);
                     if (!tracks_map.ContainsKey (track.TrackId)) {
@@ -208,6 +215,13 @@ namespace Banshee.Dap.AppleDevice
                     Log.Exception (e);
                 }
             }
+            if (invalid_tracks.Count > 0) {
+                Log.Warning (String.Format ("Found {0} invalid tracks on the device", invalid_tracks.Count));
+                foreach (var track in invalid_tracks) {
+                    DeleteTrack (track, false);
+                }
+            }
+
 
             Hyena.Data.Sqlite.HyenaSqliteCommand insert_cmd = new Hyena.Data.Sqlite.HyenaSqliteCommand (
                 @"INSERT INTO CorePlaylistEntries (PlaylistID, TrackID)
@@ -402,6 +416,25 @@ namespace Banshee.Dap.AppleDevice
             }
         }
 
+
+        private void DeleteTrack (GPod.Track track, bool removeFile)
+        {
+            foreach (var playlist in MediaDatabase.Playlists) {
+                playlist.Tracks.Remove (track);
+            }
+
+            if (SupportsPodcasts && track.MediaType == GPod.MediaType.Podcast) {
+                MediaDatabase.PodcastsPlaylist.Tracks.Remove (track);
+            }
+
+            MediaDatabase.MasterPlaylist.Tracks.Remove (track);
+            MediaDatabase.Tracks.Remove (track);
+
+            if (removeFile) {
+                Banshee.IO.File.Delete (new SafeUri (GPod.ITDB.GetLocalPath (Device, track)));
+            }
+        }
+
         protected override void AddTrackToDevice (DatabaseTrackInfo track, SafeUri fromUri)
         {
             lock (sync_mutex) {
@@ -569,18 +602,7 @@ namespace Banshee.Dap.AppleDevice
                     if (track.IpodTrack != null) {
                         UpdateProgress (progressUpdater, message, total - tracks_to_remove.Count, total);
 
-                        foreach (var playlist in MediaDatabase.Playlists) {
-                            playlist.Tracks.Remove (track.IpodTrack);
-                        }
-
-                        if (SupportsPodcasts && track.IpodTrack.MediaType == GPod.MediaType.Podcast) {
-                            MediaDatabase.PodcastsPlaylist.Tracks.Remove (track.IpodTrack);
-                        }
-
-                        MediaDatabase.MasterPlaylist.Tracks.Remove (track.IpodTrack);
-                        MediaDatabase.Tracks.Remove (track.IpodTrack);
-
-                        Banshee.IO.File.Delete (new SafeUri (GPod.ITDB.GetLocalPath (Device, track.IpodTrack)));
+                        DeleteTrack (track.IpodTrack, true);
                     } else {
                         Log.Error ("The ipod track was null");
                     }



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