[banshee] [Dap.Ipod] Fix consistency of URIs the right way



commit 1e315106c9280073ec5cd3a8ad08d9fba294c6ab
Author: Andrés G. Aragoneses <knocte gmail com>
Date:   Sun Sep 19 13:27:07 2010 +0200

    [Dap.Ipod] Fix consistency of URIs the right way
    
    The previous patch for BGO#620826 had some issues:
    a) It still left a small window of time in which the URI would be
    not the one in the device so it could still trigger wrong metadata
    updates with the patch from BGO#589196.
    b) It decreased performance a bit because the Save() method for
    each track would trigger UI redraw.
    c) Using the CreateTrack(URI) API instead of CreateTrack() and
    the setter for track.Uri made ipod-sharp copy the track to the
    iPod sooner (instead of at TrackDatabase.Save() time) than
    before so the way to display the progress via sync_user_job
    should have changed.
    
    Now with this new approach:
    * Save() isn't called until we have the correct URI, to eliminate
    any period in which the track can have incorrect data in the DB.
    * Save(false) is called instead of Save(true), and when the process
    sync finishes, we call OnTracksAdded () and OnUserNotifyUpdated ()
    to reflect the new tracks in the UI.
    * A new method to display the progress during the CommitToIpod()
    calls has been implemented, reusing the previous one.
    
    Besides, this approach has already been committed for AppleDevice:
    http://git.gnome.org/browse/banshee/commit/?id=c6a6aea30e56616a96872fc593ac17862ccbba79
    
    Signed-off by: Alan McGovern <alan mcgovern gmail com>

 .../Banshee.Dap.Ipod/IpodSource.cs                 |   32 ++++++++++++++-----
 .../Banshee.Dap.Ipod/IpodTrackInfo.cs              |   22 ++++++++++----
 2 files changed, 39 insertions(+), 15 deletions(-)
---
diff --git a/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodSource.cs b/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodSource.cs
index bfff817..3052a1b 100644
--- a/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodSource.cs
+++ b/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodSource.cs
@@ -473,7 +473,6 @@ namespace Banshee.Dap.Ipod
                     PrimarySource = this,
                 };
 
-                ipod_track.Save (false);
                 tracks_to_add.Enqueue (ipod_track);
             }
         }
@@ -562,19 +561,30 @@ namespace Banshee.Dap.Ipod
         {
             Hyena.Log.Debug ("Starting iPod sync thread cycle");
 
+            CreateNewSyncUserJob ();
+            var i = 0;
+            var total = tracks_to_add.Count;
             while (tracks_to_add.Count > 0) {
                 IpodTrackInfo track = null;
                 lock (sync_mutex) {
+                    total = tracks_to_add.Count + i;
                     track = tracks_to_add.Dequeue ();
                 }
 
+                ChangeSyncProgress (track.ArtistName, track.TrackTitle, ++i / total);
+
                 try {
                     track.CommitToIpod (ipod_device);
                     tracks_map[track.TrackId] = track;
+                    track.Save (false);
                 } catch (Exception e) {
                     Log.Exception ("Cannot save track to iPod", e);
                 }
             }
+            if (total > 0) {
+                OnTracksAdded ();
+                OnUserNotifyUpdated ();
+            }
 
             // TODO sync updated metadata to changed tracks
 
@@ -629,7 +639,6 @@ namespace Banshee.Dap.Ipod
             }
 
             try {
-                ipod_device.TrackDatabase.SaveStarted += OnIpodDatabaseSaveStarted;
                 ipod_device.TrackDatabase.SaveEnded += OnIpodDatabaseSaveEnded;
                 ipod_device.TrackDatabase.SaveProgressChanged += OnIpodDatabaseSaveProgressChanged;
                 ipod_device.Save ();
@@ -638,7 +647,6 @@ namespace Banshee.Dap.Ipod
             } catch (Exception e) {
                 Log.Exception ("Failed to save iPod database", e);
             } finally {
-                ipod_device.TrackDatabase.SaveStarted -= OnIpodDatabaseSaveStarted;
                 ipod_device.TrackDatabase.SaveEnded -= OnIpodDatabaseSaveEnded;
                 ipod_device.TrackDatabase.SaveProgressChanged -= OnIpodDatabaseSaveProgressChanged;
                 Hyena.Log.Debug ("Ending iPod sync thread cycle");
@@ -647,10 +655,8 @@ namespace Banshee.Dap.Ipod
 
         private UserJob sync_user_job;
 
-        private void OnIpodDatabaseSaveStarted (object o, EventArgs args)
+        private void CreateNewSyncUserJob ()
         {
-            DisposeSyncUserJob ();
-
             sync_user_job = new UserJob (Catalog.GetString ("Syncing iPod"),
                 Catalog.GetString ("Preparing to synchronize..."), GetIconNames ());
             sync_user_job.Register ();
@@ -671,10 +677,18 @@ namespace Banshee.Dap.Ipod
 
         private void OnIpodDatabaseSaveProgressChanged (object o, IPod.TrackSaveProgressArgs args)
         {
-            double progress = args.CurrentTrack == null ? 0.0 : args.TotalProgress;
-            string message = args.CurrentTrack == null
+            if (args.CurrentTrack == null) {
+                ChangeSyncProgress (null, null, 0.0);
+            } else {
+                ChangeSyncProgress (args.CurrentTrack.Artist, args.CurrentTrack.Title, args.TotalProgress);
+            }
+        }
+
+        private void ChangeSyncProgress (string artist, string title, double progress)
+        {
+            string message = (artist == null && title == null)
                     ? Catalog.GetString ("Updating...")
-                    : String.Format ("{0} - {1}", args.CurrentTrack.Artist, args.CurrentTrack.Title);
+                    : String.Format ("{0} - {1}", artist, title);
 
              if (progress >= 0.99) {
                  sync_user_job.Status = Catalog.GetString ("Flushing to disk...");
diff --git a/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodTrackInfo.cs b/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodTrackInfo.cs
index e35521a..a2be748 100644
--- a/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodTrackInfo.cs
+++ b/src/Dap/Banshee.Dap.Ipod/Banshee.Dap.Ipod/IpodTrackInfo.cs
@@ -189,14 +189,24 @@ namespace Banshee.Dap.Ipod
 
         public void CommitToIpod (IPod.Device device)
         {
-            track = track ?? device.TrackDatabase.CreateTrack ();
+            bool update = (track != null);
+            if (!update) {
+                try {
+                    track = device.TrackDatabase.CreateTrack (Uri.AbsolutePath);
+                } catch (Exception e) {
+                    Log.Exception ("Failed to create iPod track with Uri " + Uri.AbsoluteUri, e);
+                    device.TrackDatabase.RemoveTrack (track);
+                    return;
+                }
+            }
+
             ExternalId = track.Id;
 
-            try {
-                track.Uri = new Uri (Uri.AbsoluteUri);
-            } catch (Exception e) {
-                Log.Exception ("Failed to create System.Uri for iPod track", e);
-                device.TrackDatabase.RemoveTrack (track);
+            //if the track was not in the ipod already, the CreateTrack(uri)
+            //method updates the Uri property with the path of the new file
+            //so we need to save it on Banshee db to be properly synced
+            if (!update) {
+                Uri = new SafeUri (track.Uri);
             }
 
             track.AlbumArtist = AlbumArtist;



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