[banshee/moblin] Get play-queue bits syncronized properly ...



commit 79c61a8f96512874c56dc403756793d25e7d4419
Author: Michael Meeks <michael meeks novell com>
Date:   Tue Aug 18 16:32:09 2009 +0100

    Get play-queue bits syncronized properly ...

 .../Banshee.MoblinBackend/MoblinBognor.cs          |  119 ++++++++++++++++++--
 .../Banshee.MoblinBackend/MoblinService.cs         |    2 +-
 2 files changed, 112 insertions(+), 9 deletions(-)
---
diff --git a/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinBognor.cs b/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinBognor.cs
index 08c4158..4e09b72 100644
--- a/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinBognor.cs
+++ b/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinBognor.cs
@@ -38,6 +38,57 @@ using Banshee.MediaEngine;
 using Banshee.ServiceStack;
 using org.moblin.BognorRegis;
 
+// Emulate the track model inside BognorRegis
+internal class TrackModel
+{
+	List<string> uris;
+	public TrackModel()
+	{
+		uris = new List<string>();
+	}
+	public int Count {
+		get { return uris.Count; }
+	}
+	// Sloppy item logic
+	public string this[ int pos ] {
+		get {
+			if ((uint) pos >= (uint) uris.Count)
+				return "";
+			return uris[pos];
+		}
+	}
+	public int FindIndexFrom (int start, string uri)
+	{
+		return uris.FindIndex (start, uris.Count - start,
+				       val => val == uri);
+	}
+	public void Add (string uri, int pos)
+	{
+		if ((uint) pos > (uint) uris.Count + 1) {
+			Console.WriteLine ("Error: out of range item position");
+			return;
+		}
+		uris.Insert (pos, uri);
+	}
+	public void Remove (string uri, int pos)
+	{
+		if ((uint) pos > (uint) uris.Count) {
+			Console.WriteLine ("Error: out of range item position");
+			return;
+		} else if (uris[pos] != uri) {
+			Console.WriteLine ("Error: uri mismatch {0} {1}", uris[pos], uri);
+			return;
+		} 
+		uris.RemoveAt (pos);
+	}
+	// we assume the client has refreshed it's view,
+	// and we don't emit changed requests here.
+	public void ResetTo (List<string> strings)
+	{
+		uris = new List<string>(strings);
+	}
+}
+
 namespace Banshee.MoblinBackend.BognorRegis
 {
 	[DBusExportable (ServiceName = "BognorQueue")]
@@ -59,7 +110,7 @@ namespace Banshee.MoblinBackend.BognorRegis
 		public QueueManager()
 		{
 			ServiceManager.DBusServiceManager.RegisterObject (this);
-			local_queue = new LocalQueue(this, "local_queue");
+			local_queue = new LocalQueue(this);
 		}
 		public void Dispose ()
 		{
@@ -80,7 +131,7 @@ namespace Banshee.MoblinBackend.BognorRegis
 	[DBusExportable (ServiceName = "BognorQueue")]
 	public class LocalQueue : IDBusExportable, IQueue, IDisposable
 	{
-		string serviceName;
+		TrackModel model;
 		QueueManager mgr;
 
 		private PlayQueueSource Queue {
@@ -91,10 +142,15 @@ namespace Banshee.MoblinBackend.BognorRegis
 			}
 		}
 
-		public LocalQueue (QueueManager _mgr, string name)
+		public LocalQueue (QueueManager _mgr)
 		{
-			serviceName = name;
 			mgr = _mgr;
+
+			// model to track what Bognor sees
+			model = new TrackModel();
+			UriAdded += delegate (string uri, int pos) { model.Add (uri, pos); };
+			UriRemoved += delegate (string uri, int pos) { model.Remove (uri, pos); };
+
 			ServiceManager.DBusServiceManager.RegisterObject (this);
 
 			ServiceManager.PlayerEngine.ConnectEvent
@@ -106,7 +162,7 @@ namespace Banshee.MoblinBackend.BognorRegis
 		}
         
 		string IService.ServiceName {
-			get { return serviceName; }
+			get { return "local_queue"; }
 		}
 
 		public void Dispose ()
@@ -169,12 +225,61 @@ namespace Banshee.MoblinBackend.BognorRegis
 		{
 			Queue.Clear();
 		}
-		public string[] ListUris ()
+
+		private List<string> GetTracks()
 		{
 			var uris = new List<string>();
 			var model = Queue.TrackModel;
 			for (int i = 0; i < model.Count; i++)
 			    uris.Add (model[i].Uri.AbsoluteUri);
+			return uris;
+		}
+
+		// walk our idea of the queue, and compare it
+		// with what we think the bognor client has, to
+		// calculate indexes and so on.
+		protected void ResyncUris ()
+		{
+			int i;
+			List<string> tracks = GetTracks();
+
+			Console.WriteLine ("reysnc uris!");
+
+			// first: prune dead wood
+			for (i = 0; i < model.Count; i++) {
+				if (!tracks.Contains (model[i])) {
+					Console.WriteLine ("removed {0} at {1}", model[i], i);
+					UriRemoved (model[i], i);
+					i--;
+				}
+			}
+
+			// second: shuffle tracks until we match
+			for (i = 0; i < tracks.Count; i++) {
+				if (model[i] == tracks[i])
+					continue;
+
+				// do we have this track later to shuffle down ?
+				int pos = model.FindIndexFrom (i, tracks[i]);
+				if (pos != -1)
+					UriRemoved (tracks[i], pos);
+				UriAdded (tracks[i], i);
+				Console.WriteLine ("removed {0} at {1} and added at {2}",
+						   tracks[i], pos, i);
+			}
+		}
+
+//		static bool doneIt = false; // urgh !
+		public string[] ListUris ()
+		{
+		  Console.WriteLine ("Connect to reloaded ...");
+
+			var uris = GetTracks();
+//			if (!doneIt) {
+//				doneIt = true;
+				Queue.TrackModel.Reloaded += delegate { ResyncUris(); };
+//			}
+			model.ResetTo (uris);
 			return uris.ToArray();
 		}
 //		public string,string GetNowPlaying (); - mangling ?
@@ -208,7 +313,5 @@ namespace Banshee.MoblinBackend.BognorRegis
 		public event PlayingChangedHandler    PlayingChanged;
 		public event NowPlayingChangedHandler UriNowPlayingChanged;
 		public event PositionChangedHandler   UriPositionChanged;
-
 	}
-
 }
diff --git a/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinService.cs b/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinService.cs
index 62b523f..624ef10 100644
--- a/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinService.cs
+++ b/src/Backends/Banshee.Moblin/Banshee.MoblinBackend/MoblinService.cs
@@ -47,7 +47,7 @@ namespace Banshee.MoblinBackend
         
         public void Initialize ()
         {
-	    Console.WriteLine ("Init MoblinBackend");
+	    Console.WriteLine ("Init MY ! MoblinBackend");
 	    queue = BognorRegis.QueueManager.Init ();
         }
         



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