banshee r3312 - in trunk/banshee: . src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Radio src/Libraries/Lastfm/Lastfm



Author: ahixon
Date: Sat Feb 23 21:53:43 2008
New Revision: 3312
URL: http://svn.gnome.org/viewvc/banshee?rev=3312&view=rev

Log:
2008-02-23  Alexander Hixon  <hixon alexander mediati org>
    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Radio/LastfmSource.cs: Use the static
      instance of Account shared by others, rather than creating a seperate instance.
    
    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs:
    * src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs: Added support for keeping
      track of network connection state and automatically connecting. Also included better
      debugging messages.
    
    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs:
      Fix queuing tracks that have actually been played long enough but were not submitted.
      Also added a few simple checks to see if the current track attempting to be queued
      is null or has been listened to for less than one second.
    
    * src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs: Send tracks
      with the correct original play-start time (instead of double-universal time).
    
    * src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs: Make sure that errors when
      POSTing the current track being listened to aren't displayed to the user via the HIG
      message dialog - log them silently to console instead.


Modified:
   trunk/banshee/ChangeLog
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
   trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Radio/LastfmSource.cs
   trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/AudioscrobblerService.cs	Sat Feb 23 21:53:43 2008
@@ -47,6 +47,7 @@
 using Banshee.Configuration;
 using Banshee.ServiceStack;
 using Banshee.Gui;
+using Banshee.Networking;
 
 using Banshee.Collection;
 
@@ -73,22 +74,31 @@
         
         void IExtensionService.Initialize ()
         {
-            account = new Account ();
-            account.UserName = LastUserSchema.Get ();
-            account.CryptedPassword = LastPassSchema.Get ();
+            account = Account.Instance;
+            
+            if (account.UserName == null) {
+                account.UserName = LastUserSchema.Get ();
+                account.CryptedPassword = LastPassSchema.Get ();
+            }
             
             queue = new Queue ();
             connection = new AudioscrobblerConnection (account, queue);
             
+            // This auto-connects for us if we start off connected to the network.
+            connection.UpdateNetworkState (NetworkDetect.Instance.Connected);
+            NetworkDetect.Instance.StateChanged += delegate (object o, NetworkStateChangedArgs args) {
+                connection.UpdateNetworkState (args.Connected);
+            };
+            
             ServiceManager.PlayerEngine.EventChanged += OnPlayerEngineEventChanged;
             ServiceManager.PlayerEngine.StateChanged += OnPlayerEngineStateChanged;
             
             action_service = ServiceManager.Get<InterfaceActionService> ("InterfaceActionService");
             InterfaceInitialize ();
         
-            if (!connection.Started) {
+            /*if (!connection.Started && account.UserName != null && account.CryptedPassword != null) {
                 connection.Connect ();
-            }
+            }*/
         }
         
         public void InterfaceInitialize ()
@@ -159,9 +169,16 @@
         SongTimer st = new SongTimer ();
         
         private void Queue (TrackInfo track) {
+            if (track == null || st.PlayTime == 0) {
+                return;
+            }
+            
+            Log.DebugFormat ("Track {4} had playtime of {0} sec, duration {1} sec, started: {2}, queued: {3}",
+                st.PlayTime, track.Duration.TotalSeconds, song_started, queued, track);
+            
             if (song_started && !queued && track.Duration.TotalSeconds > 30 && 
                 track.ArtistName != "" && track.TrackTitle != "" &&
-               (st.PlayTime >  track.Duration.TotalSeconds / 2 || st.PlayTime > 240)) {
+                (st.PlayTime >  track.Duration.TotalSeconds / 2 || st.PlayTime > 240)) {
                   queue.Add (track, song_start_time);
                   queued = true;
             }
@@ -173,6 +190,7 @@
                 case PlayerEngineEvent.StartOfStream:
                     // Queue the previous track in case of a skip
                     st.Stop ();
+                    //Log.DebugFormat ("Attempting to queue track (from start-o-stream): {0}", last_track);
                     Queue (last_track);
                 
                     st.Reset (); st.Start ();
@@ -182,14 +200,18 @@
                     song_started = true;
 
                     // Queue as now playing
-                    connection.NowPlaying (last_track.ArtistName, last_track.TrackTitle,
-                        last_track.AlbumTitle, last_track.Duration.TotalSeconds, last_track.TrackNumber);
+                    if (last_track != null) {
+                        connection.NowPlaying (last_track.ArtistName, last_track.TrackTitle,
+                            last_track.AlbumTitle, last_track.Duration.TotalSeconds, last_track.TrackNumber);
+                    }
+                    
                     break;
                 
                 case PlayerEngineEvent.EndOfStream:
                     st.Stop ();
                     Queue (ServiceManager.PlayerEngine.CurrentTrack);
-                    queued = true;
+                    //Log.DebugFormat ("Attempting to queue track (from end-o-stream): {0}", ServiceManager.PlayerEngine.CurrentTrack);
+                    //queued = true;
                     break;
             }
         }

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Audioscrobbler/Queue.cs	Sat Feb 23 21:53:43 2008
@@ -56,7 +56,7 @@
                 this.title = track.TrackTitle;
                 this.track_number = (int) track.TrackNumber;
                 this.duration = (int) track.Duration.TotalSeconds;
-                this.start_time = DateTimeUtil.ToTimeT(start_time.ToUniversalTime ());
+                this.start_time = DateTimeUtil.ToTimeT(start_time.ToLocalTime ());
             }
 
             public QueuedTrack (string artist, string album,

Modified: trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Radio/LastfmSource.cs
==============================================================================
--- trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Radio/LastfmSource.cs	(original)
+++ trunk/banshee/src/Extensions/Banshee.Lastfm/Banshee.Lastfm.Radio/LastfmSource.cs	Sat Feb 23 21:53:43 2008
@@ -69,12 +69,14 @@
 
         public LastfmSource () : base (lastfm, lastfm, 150)
         {
-            account = new Account ();
+            account = Account.Instance;
 
             // We don't automatically connect to Last.fm, but load the last Last.fm
             // username we used so we can load the user's stations.
-            account.UserName = LastUserSchema.Get ();
-            account.CryptedPassword = LastPassSchema.Get ();
+            if (account.UserName != null) {
+                account.UserName = LastUserSchema.Get ();
+                account.CryptedPassword = LastPassSchema.Get ();
+            }
 
             connection = new RadioConnection (account, Banshee.Web.Browser.UserAgent);
             connection.UpdateNetworkState (NetworkDetect.Instance.Connected);

Modified: trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs
==============================================================================
--- trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs	(original)
+++ trunk/banshee/src/Libraries/Lastfm/Lastfm/AudioscrobblerConnection.cs	Sat Feb 23 21:53:43 2008
@@ -66,6 +66,10 @@
         string session_id = null;
         string now_playing_url;
         bool now_playing_submitted = false;
+        bool connected = false;
+        public bool Connected {
+            get { return connected; }
+        }
         
         bool started = false;
         public bool Started {
@@ -98,23 +102,38 @@
         
         private void AccountUpdated (object o, EventArgs args)
         {
+            Stop ();
+            session_id = null;
             Connect ();
         }
         
         public void Connect ()
         {
-            if (session_id == null) {
-                if (!started) {
-                    Start ();
+            if (!started) {
+                Start ();
+            }
+        
+            if (session_id == null && started) {
+                if (connected) {
+                    state = State.NEED_HANDSHAKE;
+                    Handshake ();
+                } else {
+                    Hyena.Log.Debug ("Not connecting to Audioscrobbler", "Not connected to network.");
                 }
-                
-                Handshake ();
+            }
+        }
+        
+        public void UpdateNetworkState (bool connected)
+        {
+            Log.DebugFormat ("Changing Audioscrobbler connected state: {0}", connected ? "connected" : "disconnected");
+            this.connected = connected;
+            if (connected) {
+                Connect ();
             }
         }
 
         private void Start ()
         {
-            state = State.NEED_HANDSHAKE;
             started = true;
             queue.TrackAdded += delegate(object o, EventArgs args) {
                 StartTransitionHandler ();
@@ -125,6 +144,11 @@
 
         private void StartTransitionHandler ()
         {
+            if (!started) {
+                // Don't run if we're not actually connected.
+                return;
+            }
+            
             if (timer == null) {
                 timer = new System.Timers.Timer ();
                 timer.Interval = TICK_INTERVAL;
@@ -132,10 +156,10 @@
                 timer.Elapsed += new ElapsedEventHandler (StateTransitionHandler);
                 
                 timer.Start ();
-                Console.WriteLine ("Timer started.");
+                //Console.WriteLine ("Timer started.");
             } else if (!timer.Enabled) {
                 timer.Start ();
-                Console.WriteLine ("Restarting timer from stopped state.");
+                //Console.WriteLine ("Restarting timer from stopped state.");
             }
         }
 
@@ -161,13 +185,13 @@
 
         private void StateTransitionHandler (object o, ElapsedEventArgs e)
         {
-            Console.WriteLine ("State transition handler running.");
+            Hyena.Log.DebugFormat ("State transition handler running; state: {0}", state);
+            
             /* if we're not connected, don't bother doing anything
              * involving the network. */
-             // TODO!
-            /*if (!NetworkDetect.Instance.Connected) {
-                return true;
-            }*/
+            if (!connected) {
+                return;
+            }
                         
             if ((state == State.IDLE || state == State.NEED_TRANSMIT) && hard_failures > 2) {
                 state = State.NEED_HANDSHAKE;
@@ -233,7 +257,7 @@
 
             next_interval = DateTime.MinValue;
 
-            if (post_url == null) {
+            if (post_url == null || !connected) {
                 return;
             }
 
@@ -528,6 +552,7 @@
                 }
                 else if (line.StartsWith ("OK")) {
                     // NowPlaying submitted  
+                    Hyena.Log.DebugFormat ("Submitted NowPlaying track to Audioscrobbler");
                 }
                 else {
                     Hyena.Log.Warning ("Audioscrobbler NowPlaying failed", "Unexpected or no response", false);       
@@ -535,7 +560,7 @@
             }
             catch (Exception e) {
                 Hyena.Log.Error ("Audioscrobbler NowPlaying failed", 
-                              String.Format("Failed to post NowPlaying: {0}", e));
+                              String.Format("Failed to post NowPlaying: {0}", e), false);
             }
         }
     }



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