[banshee] LastFM: display progress for multiple-track scrobbles (bgo#691532)



commit 3232ada1abc91d95ce9975e10ac4d78498a22f5b
Author: Phil Trimble <PhilTrimble gmail com>
Date:   Sat Mar 16 23:02:11 2013 +0000

    LastFM: display progress for multiple-track scrobbles (bgo#691532)
    
    Remove progress bar displayed for tracks read from a device
    and instead display progress updates for all Last.FM scrobble
    submissions which have more than one track.
    
    Fixes BGO#691532.
    
    Signed-off-by: Andres G. Aragoneses <knocte gmail com>

 .../AudioscrobblerService.cs                       |  114 ++++++++++++++------
 .../Lastfm/Lastfm/AudioscrobblerConnection.cs      |   51 +++++++++
 2 files changed, 132 insertions(+), 33 deletions(-)
---
diff --git a/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs 
b/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
index c33477f..ce97b09 100644
--- a/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
+++ b/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
@@ -9,6 +9,7 @@
 //   Phil Trimble <philtrimble gmail com>
 //
 // Copyright (C) 2005-2008 Novell, Inc.
+// Copyright (C) 2013 Phil Trimble
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
@@ -77,6 +78,11 @@ namespace Banshee.Lastfm.Audioscrobbler
         private readonly TimeSpan MINIMUM_TRACK_DURATION = TimeSpan.FromSeconds (30);
         private readonly TimeSpan MINIMUM_TRACK_PLAYTIME = TimeSpan.FromSeconds (240);
 
+        private UserJob scrobble_job;
+        private int job_tracks_count;
+        private int job_tracks_total;
+        private string scrobbling_progress_message = Catalog.GetString ("Processed {0} of {1} tracks");
+
         public AudioscrobblerService ()
         {
         }
@@ -108,6 +114,10 @@ namespace Banshee.Lastfm.Audioscrobbler
             connection.UpdateNetworkState (network.Connected);
             network.StateChanged += HandleNetworkStateChanged;
 
+            connection.SubmissionStart += OnSubmissionStart;
+            connection.SubmissionUpdate += OnSubmissionUpdate;
+            connection.SubmissionEnd += OnSubmissionEnd;
+
             // Update the Visit action menu item if we update our account info
             LastfmCore.Account.Updated += delegate (object o, EventArgs args) {
                 actions["AudioscrobblerVisitAction"].Sensitive = String.IsNullOrEmpty 
(LastfmCore.Account.UserName);
@@ -183,6 +193,10 @@ namespace Banshee.Lastfm.Audioscrobbler
             action_service.UIManager.RemoveUi (ui_manager_id);
             action_service.UIManager.RemoveActionGroup (actions);
             actions = null;
+
+            connection.SubmissionStart -= OnSubmissionStart;
+            connection.SubmissionUpdate -= OnSubmissionUpdate;
+            connection.SubmissionEnd -= OnSubmissionEnd;
         }
 
         List<IBatchScrobblerSource> sources_watched;
@@ -399,48 +413,82 @@ namespace Banshee.Lastfm.Audioscrobbler
 
         private void OnReadyToScrobble (object source, ScrobblingBatchEventArgs args)
         {
-            var scrobble_job = new UserJob (Catalog.GetString ("Scrobbling from device"),
-                                            Catalog.GetString ("Scrobbling from device..."));
+            if (!connection.Started) {
+                connection.Start ();
+            }
 
-            scrobble_job.PriorityHints = PriorityHints.DataLossIfStopped;
-            scrobble_job.Register ();
+            int added_track_count = 0;
 
-            try {
-                if (!connection.Started) {
-                    connection.Start ();
-                }
-    
-                int added_track_count = 0, processed_track_count = 0;
-                string message = Catalog.GetString ("Processing track {0} of {1} ...");
-                var batchCount = args.ScrobblingBatch.Count;
-    
-                foreach (var track_entry in args.ScrobblingBatch) {
-                    TrackInfo track = track_entry.Key;
-    
-                    if (IsValidForSubmission (track)) {
-                        IList<DateTime> playtimes = track_entry.Value;
-    
-                        foreach (DateTime playtime in playtimes) {
-                            queue.Add (track, playtime);
-                            added_track_count++;
-                        }
-                        Log.DebugFormat ("Added to Last.fm queue: {0} - Number of plays: {1}", track, 
playtimes.Count);
-                    } else {
-                        Log.DebugFormat ("Track {0} failed validation check for Last.fm submission, 
skipping...",
-                                         track);
+            foreach (var track_entry in args.ScrobblingBatch) {
+                TrackInfo track = track_entry.Key;
+
+                if (IsValidForSubmission (track)) {
+                    IList<DateTime> playtimes = track_entry.Value;
+
+                    foreach (DateTime playtime in playtimes) {
+                        queue.Add (track, playtime);
+                        added_track_count++;
                     }
-    
-                    scrobble_job.Status = String.Format (message, ++processed_track_count, batchCount);
-                    scrobble_job.Progress = processed_track_count / (double) batchCount;
+                    Log.DebugFormat ("Added to Last.fm queue: {0} - Number of plays: {1}", track, 
playtimes.Count);
+                } else {
+                    Log.DebugFormat ("Track {0} failed validation check for Last.fm submission, skipping...",
+                                     track);
                 }
-    
-                Log.InformationFormat ("Number of played tracks from device added to Last.fm queue: {0}", 
added_track_count);
+            }
+
+            Log.InformationFormat ("Number of played tracks from device added to Last.fm queue: {0}", 
added_track_count);
+        }
+
+        private void OnSubmissionStart (object source, SubmissionStartEventArgs args)
+        {
+            // We only want to display something if more than one track is being submitted
+            if (args.TotalCount <= 1) {
+                return;
+            }
 
-            } finally {
+            if (scrobble_job == null) {
+                scrobble_job = new UserJob (Catalog.GetString ("Scrobbling to Last.FM"),
+                                            Catalog.GetString ("Scrobbling to Last.FM..."));
+
+                scrobble_job.PriorityHints = PriorityHints.None;
+                scrobble_job.CanCancel = true;
+                scrobble_job.CancelRequested += OnScrobbleJobCancelRequest;
+                scrobble_job.Register ();
+
+                job_tracks_count = 0;
+                job_tracks_total = args.TotalCount;
+            }
+            UpdateJob ();
+        }
+
+        private void OnSubmissionUpdate (object source, SubmissionUpdateEventArgs args)
+        {
+            if (scrobble_job != null) {
+                job_tracks_count += args.UpdateCount;
+                UpdateJob ();
+            }
+        }
+
+        private void UpdateJob ()
+        {
+            scrobble_job.Status = String.Format (scrobbling_progress_message, job_tracks_count, 
job_tracks_total);
+            scrobble_job.Progress = job_tracks_count / (double) job_tracks_total;
+        }
+
+        private void OnSubmissionEnd (object source, EventArgs args)
+        {
+            if (scrobble_job != null) {
                 scrobble_job.Finish ();
+                scrobble_job = null;
             }
         }
 
+        private void OnScrobbleJobCancelRequest (object source, EventArgs args)
+        {
+            scrobble_job.Finish ();
+            scrobble_job = null;
+        }
+
 #endregion
 
         public static readonly SchemaEntry<string> LastUserSchema = new SchemaEntry<string> (
diff --git a/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs 
b/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
index 230199c..9b63d23 100644
--- a/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
+++ b/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
@@ -48,6 +48,26 @@ using System.Collections.Specialized;
 
 namespace Lastfm
 {
+    public class SubmissionStartEventArgs : EventArgs
+    {
+        public SubmissionStartEventArgs (int total_count)
+        {
+            TotalCount = total_count;
+        }
+
+        public int TotalCount { get; private set; }
+    }
+
+    public class SubmissionUpdateEventArgs : EventArgs
+    {
+        public SubmissionUpdateEventArgs (int update_count)
+        {
+            UpdateCount = update_count;
+        }
+
+        public int UpdateCount { get; private set; }
+    }
+
     public class AudioscrobblerConnection
     {
         private enum State {
@@ -71,6 +91,10 @@ namespace Lastfm
             get { return started; }
         }
 
+        public event EventHandler<SubmissionStartEventArgs> SubmissionStart;
+        public event EventHandler<SubmissionUpdateEventArgs> SubmissionUpdate;
+        public event EventHandler SubmissionEnd;
+
         private System.Timers.Timer timer;
         private DateTime next_interval;
 
@@ -171,11 +195,13 @@ namespace Lastfm
             case State.Idle:
                 if (queue.Any ()) {
                     state = State.NeedTransmit;
+                    RaiseSubmissionStart (queue.Count);
                 } else if (current_now_playing_request != null) {
                     // Now playing info needs to be sent
                     NowPlaying (current_now_playing_request);
                 } else {
                     StopTransitionHandler ();
+                    RaiseSubmissionEnd ();
                 }
 
                 break;
@@ -314,6 +340,7 @@ namespace Lastfm
                 // we succeeded, pop the elements off our queue
                 queue.RemoveRange (0, nb_tracks_scrobbled);
                 queue.Save ();
+                RaiseSubmissionUpdate (nb_tracks_scrobbled);
             } else {
                 // TODO: If error == StationError.InvalidSessionKey,
                 // suggest to the user to (re)do the Last.fm authentication.
@@ -417,5 +444,29 @@ namespace Lastfm
             }
             current_now_playing_request = null;
         }
+
+        private void RaiseSubmissionStart (int total_count)
+        {
+            var handler = SubmissionStart;
+            if (handler != null) {
+                handler (this, new SubmissionStartEventArgs (total_count));
+            }
+        }
+
+        private void RaiseSubmissionUpdate (int update_count)
+        {
+            var handler = SubmissionUpdate;
+            if (handler != null) {
+                handler (this, new SubmissionUpdateEventArgs (update_count));
+            }
+        }
+
+        private void RaiseSubmissionEnd ()
+        {
+            var handler = SubmissionEnd;
+            if (handler != null) {
+                handler (this, null);
+            }
+        }
     }
 }


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