[banshee] SourceManager: Add and delete group sources as necessary (bgo#639805)



commit 8e3ae137df78a57bb2c6a2169f4a9381ecda4a79
Author: Travis Patterson <masslessparticle gmail com>
Date:   Wed May 25 11:44:25 2011 -0600

    SourceManager: Add and delete group sources as necessary (bgo#639805)
    
    Whenever a source is added or removed, SourceManager checks its list of
    GroupSources to determine if a GroupSource needs to be added or removed
    from the source list. To achieve this, the SourceManager keeps track of
    GroupSources in a separate list in addition to the main sources list.
    
    DapService is also modified to reflect this generalized approach. The
    GroupSource is only added once when the DapService is initialized. From
    there, the SourceManager decides whether or not to display the
    GroupSource depending on whats added/removed.
    
    Signed-off-by: Bertrand Lorentz <bertrand lorentz gmail com>

 .../Banshee.Sources/SourceManager.cs               |   54 +++++++++++++++++++-
 src/Dap/Banshee.Dap/Banshee.Dap/DapService.cs      |   21 ++------
 2 files changed, 58 insertions(+), 17 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs b/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs
index d50210c..d0878bd 100644
--- a/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs
+++ b/src/Core/Banshee.Services/Banshee.Sources/SourceManager.cs
@@ -55,6 +55,7 @@ namespace Banshee.Sources
     public class SourceManager : /*ISourceManager,*/ IInitializeService, IRequiredService, IDBusExportable, IDisposable
     {
         private List<Source> sources = new List<Source>();
+        private List<GroupSource> group_sources = new List<GroupSource> ();
         private Dictionary<string, Source> extension_sources = new Dictionary<string, Source> ();
 
         private Source active_source;
@@ -81,8 +82,9 @@ namespace Banshee.Sources
             // things that depend on being loaded before the music library is added.
             //AddSource (music_library = new MusicLibrarySource (), true);
             //AddSource (video_library = new VideoLibrarySource (), false);
-            AddSource (new GroupSource (Catalog.GetString ("Libraries"), 39));
-            AddSource (new GroupSource (Catalog.GetString ("Online Media"), 60));
+
+            group_sources.Add (new GroupSource (Catalog.GetString ("Online Media"), 60));
+            group_sources.Add (new GroupSource (Catalog.GetString ("Libraries"), 39));
         }
 
         internal void LoadExtensionSources ()
@@ -154,6 +156,14 @@ namespace Banshee.Sources
                 return;
             }
 
+            GroupSource group_source = source as GroupSource;
+            if (group_source != null && !group_sources.Contains (group_source)) {
+                group_sources.Add (group_source);
+                return;
+            }
+
+            AddSource (FindAssociatedGroupSource (source.Order));
+
             int position = FindSourceInsertPosition(source);
             sources.Insert(position, source);
 
@@ -212,6 +222,11 @@ namespace Banshee.Sources
 
             sources.Remove(source);
 
+            GroupSource associated_groupsource = FindAssociatedGroupSource (source.Order);
+            if (!GroupSourceHasMembers (associated_groupsource)) {
+                RemoveSource (associated_groupsource, recursivelyDispose);
+            }
+
             foreach(Source child_source in source.Children) {
                 RemoveSource (child_source, recursivelyDispose);
             }
@@ -288,6 +303,41 @@ namespace Banshee.Sources
             RemoveSource (args.Source);
         }
 
+
+        private GroupSource FindAssociatedGroupSource (int order)
+        {
+            int current_order = -1;
+            GroupSource associated_groupsource = null;
+            foreach (GroupSource source in group_sources){
+                if (order == source.Order) {
+                    return null;
+                }
+
+                if (order > source.Order && current_order < source.Order) {
+                    associated_groupsource = source;
+                    current_order = source.Order;
+                }
+            }
+            return associated_groupsource;
+        }
+
+        private bool GroupSourceHasMembers (GroupSource group_source) {
+            Source source = group_source as Source;
+            if (group_source == null || !sources.Contains (source)) {
+                return false;
+            }
+
+            int source_index = FindSourceInsertPosition (source);
+
+            if (source_index < sources.Count - 1) {
+                Source next_source = sources[source_index + 1];
+                GroupSource associated_groupsource = FindAssociatedGroupSource (next_source.Order);
+                return group_source.Equals (associated_groupsource);
+            } else {
+                return false;
+            }
+        }
+
         private int FindSourceInsertPosition(Source source)
         {
             for(int i = sources.Count - 1; i >= 0; i--) {
diff --git a/src/Dap/Banshee.Dap/Banshee.Dap/DapService.cs b/src/Dap/Banshee.Dap/Banshee.Dap/DapService.cs
index b3bb1e0..93e6a63 100644
--- a/src/Dap/Banshee.Dap/Banshee.Dap/DapService.cs
+++ b/src/Dap/Banshee.Dap/Banshee.Dap/DapService.cs
@@ -48,27 +48,24 @@ namespace Banshee.Dap
 {
     public class DapService : IExtensionService, IDelayedInitializeService, IDisposable
     {
-        private static SourceManager.GroupSource dap_group;
         private Dictionary<string, DapSource> sources;
         private List<DeviceCommand> unhandled_device_commands;
         private List<DapPriorityNode> supported_dap_types;
         private bool initialized;
         private object sync = new object ();
 
-        static DapService ()
-        {
-            // This group source gives us a seperator for DAPs in the source view.
-            // We add it when we get our first dap source, and then remove it when
-            //we lose the last one.
-            dap_group = new SourceManager.GroupSource (Catalog.GetString ("Devices"), 400);
-        }
-
         public void Initialize ()
         {
         }
 
         public void DelayedInitialize ()
         {
+            // This group source gives us a seperator for DAPs in the source view.
+            SourceManager.GroupSource dap_group = new SourceManager.GroupSource (Catalog.GetString ("Devices"), 400);
+            ThreadAssist.ProxyToMain (delegate {
+                ServiceManager.SourceManager.AddSource (dap_group);
+            });
+
             lock (sync) {
                 if (initialized || ServiceManager.HardwareManager == null)
                     return;
@@ -228,9 +225,6 @@ namespace Banshee.Dap
 
                 if (source != null) {
                     ThreadAssist.ProxyToMain (delegate {
-                        if (!ServiceManager.SourceManager.ContainsSource (dap_group)) {
-                            ServiceManager.SourceManager.AddSource (dap_group);
-                        }
 
                         ServiceManager.SourceManager.AddSource (source);
                         source.NotifyUser ();
@@ -274,9 +268,6 @@ namespace Banshee.Dap
                     source.Dispose ();
                     ThreadAssist.ProxyToMain (delegate {
                         ServiceManager.SourceManager.RemoveSource (source);
-                        if (ServiceManager.SourceManager.FindSources<DapSource> ().Count () < 1) {
-                            ServiceManager.SourceManager.RemoveSource (dap_group);
-                        }
                     });
                 } catch (Exception e) {
                     Log.Exception (e);



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