[banshee] Allow primary sources to marked as temporary (bgo#548366)



commit 70303ded98c662af2a356d65de5db23e29c69363
Author: Neil Loknath <neil loknath gmail com>
Date:   Mon Jan 25 23:33:44 2010 -0700

    Allow primary sources to marked as temporary (bgo#548366)
    
    Move the IsTemporary property to the DatabaseSource class, so that it
    can also be used for primary sources. This takes advantage of the
    IsTemporary field in the CorePrimarySources table.
    
    This fixes bgo#548366 by also making sure that a temporary source is
    first purged when it is initialized.

 .../Banshee.Playlist/AbstractPlaylistSource.cs     |    6 ----
 .../Banshee.Playlist/PlaylistSource.cs             |   12 +++++++
 .../Banshee.Sources/DatabaseSource.cs              |    2 +
 .../Banshee.Sources/PrimarySource.cs               |   32 ++++++++++++++++++-
 .../Banshee.Daap/DaapPlaylistSource.cs             |    1 +
 .../Banshee.Daap/Banshee.Daap/DaapSource.cs        |    2 +-
 6 files changed, 46 insertions(+), 9 deletions(-)
---
diff --git a/src/Core/Banshee.Services/Banshee.Playlist/AbstractPlaylistSource.cs b/src/Core/Banshee.Services/Banshee.Playlist/AbstractPlaylistSource.cs
index ca14771..f9659ce 100644
--- a/src/Core/Banshee.Services/Banshee.Playlist/AbstractPlaylistSource.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlist/AbstractPlaylistSource.cs
@@ -77,12 +77,6 @@ namespace Banshee.Playlist
             get { return true; }
         }
 
-        private bool is_temporary = false;
-        public bool IsTemporary {
-            get { return is_temporary; }
-            set { is_temporary = value; }
-        }
-
         public int? DbId {
             get { return dbid; }
             protected set {
diff --git a/src/Core/Banshee.Services/Banshee.Playlist/PlaylistSource.cs b/src/Core/Banshee.Services/Banshee.Playlist/PlaylistSource.cs
index b343a3f..360a1c6 100644
--- a/src/Core/Banshee.Services/Banshee.Playlist/PlaylistSource.cs
+++ b/src/Core/Banshee.Services/Banshee.Playlist/PlaylistSource.cs
@@ -368,6 +368,18 @@ namespace Banshee.Playlist
             ServiceManager.DbConnection.CommitTransaction ();
         }
 
+        public static void ClearTemporary (PrimarySource parent)
+        {
+            if (parent != null) {
+                ServiceManager.DbConnection.BeginTransaction ();
+                ServiceManager.DbConnection.Execute (@"
+                    DELETE FROM CorePlaylistEntries WHERE PlaylistID IN (SELECT PlaylistID FROM CorePlaylists WHERE PrimarySourceID = ? AND IsTemporary = 1);
+                    DELETE FROM CorePlaylists WHERE PrimarySourceID = ? AND IsTemporary = 1;", parent.DbId, parent.DbId
+                );
+                ServiceManager.DbConnection.CommitTransaction ();
+            }
+        }
+
         private static int GetPlaylistId (string name)
         {
             return ServiceManager.DbConnection.Query<int> (
diff --git a/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs b/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
index b9b0a1f..64622b6 100644
--- a/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
+++ b/src/Core/Banshee.Services/Banshee.Sources/DatabaseSource.cs
@@ -304,6 +304,8 @@ namespace Banshee.Sources
             return CanAddTracks && source != this;
         }
 
+        public bool IsTemporary { get; set; }
+
         public override bool AcceptsUserInputFromSource (Source source)
         {
             return base.AcceptsUserInputFromSource (source) && CanAddTracks;
diff --git a/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs b/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs
index fd055e0..2a6dc39 100644
--- a/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs
+++ b/src/Core/Banshee.Services/Banshee.Sources/PrimarySource.cs
@@ -126,9 +126,10 @@ namespace Banshee.Sources
 
                 dbid = ServiceManager.DbConnection.Query<int> ("SELECT PrimarySourceID FROM CorePrimarySources WHERE StringID = ?", UniqueId);
                 if (dbid == 0) {
-                    dbid = ServiceManager.DbConnection.Execute ("INSERT INTO CorePrimarySources (StringID) VALUES (?)", UniqueId);
+                    dbid = ServiceManager.DbConnection.Execute ("INSERT INTO CorePrimarySources (StringID, IsTemporary) VALUES (?, ?)", UniqueId, IsTemporary);
                 } else {
                     SavedCount = ServiceManager.DbConnection.Query<int> ("SELECT CachedCount FROM CorePrimarySources WHERE PrimarySourceID = ?", dbid);
+                    IsTemporary = ServiceManager.DbConnection.Query<bool> ("SELECT IsTemporary FROM CorePrimarySources WHERE PrimarySourceID = ?", dbid);
                 }
 
                 if (dbid == 0) {
@@ -203,9 +204,14 @@ namespace Banshee.Sources
             get { return base_dir_with_sep ?? (base_dir_with_sep = BaseDirectory + System.IO.Path.DirectorySeparatorChar); }
         }
 
-        protected PrimarySource (string generic_name, string name, string id, int order) : base (generic_name, name, id, order)
+        protected PrimarySource (string generic_name, string name, string id, int order) : this (generic_name, name, id, order, false)
+        {
+        }
+
+        protected PrimarySource (string generic_name, string name, string id, int order, bool is_temp) : base (generic_name, name, id, order)
         {
             Properties.SetString ("SortChildrenActionLabel", Catalog.GetString ("Sort Playlists By"));
+            IsTemporary = is_temp;
             PrimarySourceInitialize ();
         }
 
@@ -245,6 +251,8 @@ namespace Banshee.Sources
 
         public virtual void Dispose ()
         {
+            PurgeSelfIfTemporary ();
+
             if (Application.ShuttingDown)
                 return;
 
@@ -270,6 +278,12 @@ namespace Banshee.Sources
 
             primary_sources[DbId] = this;
 
+            // If there was a crash, tracks can be left behind, for example in DaapSource.
+            // Temporary playlists are cleaned up by the PlaylistSource.LoadAll call below
+            if (IsTemporary && SavedCount > 0) {
+                PurgeTracks ();
+            }
+
             // Load our playlists and smart playlists
             foreach (PlaylistSource pl in PlaylistSource.LoadAll (this)) {
                 AddChildSource (pl);
@@ -434,6 +448,20 @@ namespace Banshee.Sources
             OnTracksDeleted ();
         }
 
+        protected virtual void PurgeSelfIfTemporary ()
+        {
+            if (!IsTemporary) {
+                return;
+            }
+
+            PlaylistSource.ClearTemporary (this);
+            PurgeTracks ();
+
+            ServiceManager.DbConnection.Execute (new HyenaSqliteCommand (@"
+                DELETE FROM CorePrimarySources WHERE PrimarySourceId = ?"),
+                DbId);
+        }
+
         protected virtual void PurgeTracks ()
         {
             ServiceManager.DbConnection.Execute (purge_tracks_command, DbId);
diff --git a/src/Extensions/Banshee.Daap/Banshee.Daap/DaapPlaylistSource.cs b/src/Extensions/Banshee.Daap/Banshee.Daap/DaapPlaylistSource.cs
index 4b08165..f6c2b6c 100644
--- a/src/Extensions/Banshee.Daap/Banshee.Daap/DaapPlaylistSource.cs
+++ b/src/Extensions/Banshee.Daap/Banshee.Daap/DaapPlaylistSource.cs
@@ -58,6 +58,7 @@ namespace Banshee.Daap
 
         public DaapPlaylistSource (DAAP.Playlist playlist, DaapSource parent) : base (playlist.Name, parent)
         {
+            IsTemporary = true;
             this.parent = parent;
             Save ();
 
diff --git a/src/Extensions/Banshee.Daap/Banshee.Daap/DaapSource.cs b/src/Extensions/Banshee.Daap/Banshee.Daap/DaapSource.cs
index 1b800da..d3784df 100644
--- a/src/Extensions/Banshee.Daap/Banshee.Daap/DaapSource.cs
+++ b/src/Extensions/Banshee.Daap/Banshee.Daap/DaapSource.cs
@@ -58,7 +58,7 @@ namespace Banshee.Daap
         private bool is_activating;
 
         public DaapSource (DAAP.Service service) : base (Catalog.GetString ("Music Share"), service.Name,
-                                                    (service.Address.ToString () + service.Port).Replace (":", "").Replace (".", ""), 300)
+                                                    (service.Address.ToString () + service.Port).Replace (":", "").Replace (".", ""), 300, true)
         {
             this.service = service;
             Properties.SetString ("UnmapSourceActionLabel", Catalog.GetString ("Disconnect"));



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