[blam] Make channel update/refresh saner



commit ee24951d2670e824176e84bfa68705c3d1ee5881
Author: Carlos Martín Nieto <carlos cmartin tk>
Date:   Thu Mar 3 02:54:12 2011 +0100

    Make channel update/refresh saner
    
    The previous code worked through some magic which broke as soon as I
    tried to keep reference counts in items properly. The Channel class is
    now in charge of the update, so FeedUpdater doesn't need to do
    anything.
    
    Signed-off-by: Carlos Martín Nieto <carlos cmartin tk>

 src/Channel.cs     |   78 +++++++++++++++++++++++++++++++++------------------
 src/FeedUpdater.cs |   17 +----------
 src/ItemStore.cs   |    2 +-
 3 files changed, 53 insertions(+), 44 deletions(-)
---
diff --git a/src/Channel.cs b/src/Channel.cs
index af852a0..355b94b 100644
--- a/src/Channel.cs
+++ b/src/Channel.cs
@@ -203,48 +203,70 @@ namespace Imendio.Blam {
             }
         }
 
-		private ArrayList mUnupdatedItems;
+        /**
+         * Update this channel with the given feed
+         */
+        public bool Update(SyndicationFeed feed)
+        {
+            /* If our name is sub-optimal */
+            if((Name == "" || Name == Url) &&
+                feed.Title.Text != null) {
+                Name = HtmlUtils.StripHtml(feed.Title.Text.Trim());
+            }
 
-		// Sets the channel in update mode. 
-		public void StartRefresh ()
-		{
-			this.LastRefreshed = DateTime.Now;
-            ArrayList tmp = new ArrayList();
-            lock(obj){
-                foreach(string id in item_list){
-                    tmp.Add(store.Items[id]);
-                }
+            LastRefreshed = DateTime.Now;
+
+            foreach(SyndicationItem sitem in feed.Items){
+                Add(new Item(sitem));
             }
-			mUnupdatedItems = tmp;
-		}
 
-		// Removes any items not being part of the RSS feed any more
-		public void FinishRefresh ()
-		{
-			// Remove old items
-			foreach (Item item in mUnupdatedItems) {
-                if(item == null){ /* No idea how it happens, but it does */
-                    continue;
+            /*
+             * The new item list is just the new list, so create it
+             * on the spot
+             */
+            ArrayList nitems = new ArrayList();
+            foreach(SyndicationItem sitem in feed.Items){
+                nitems.Add(sitem.Id);
+                if(item_list.Contains(sitem.Id)){
+                    /* We already had it, so just update it */
+                    (store.Items[sitem.Id] as Item).Update(sitem);
+                } else {
+                    store.Add(new Item(sitem));
                 }
-				if (item.Permanent) {
-					// Don't remove permanent items
-					continue;
-				}
+            }
 
-                lock(obj){
-                    item_list.Remove(item.Id);
-                    store.Remove(item);
+            /*
+             * Delete the items that are no longer in the feed
+             */
+            lock(obj){
+                foreach(string id in item_list){
+                    if(!nitems.Contains(id)){
+                        store.Remove(id);
+                    }
                 }
-			}
-		}
+
+                item_list = nitems;
+            }
+
+            return true;
+        }
 
         public void ItemUpdated(Item item)
         {
             EmitUpdated();
         }
 
+        /**
+         * Adds or updates the entry
+         */
         public void Add(Item item)
         {
+            if(item_list.Contains(item.Id)){
+                /* In this case we only need to update */
+                (store.Items[item.Id] as Item).Update(item);
+                return;
+            }
+
             store.Add(item);
             lock(obj){
                 item_list.Add(item.Id);
diff --git a/src/FeedUpdater.cs b/src/FeedUpdater.cs
index f300810..92053de 100644
--- a/src/FeedUpdater.cs
+++ b/src/FeedUpdater.cs
@@ -58,21 +58,8 @@ namespace Imendio.Blam {
                 return false;
             }
 
-            channel.StartRefresh();
-
-            if ((channel.Name == "" || channel.Name == channel.Url) && 
-                feed.Title.Text != null) {
-                channel.Name = HtmlUtils.StripHtml(feed.Title.Text.Trim());
-            }
-
-            updated = true;
-            foreach(SyndicationItem item in feed.Items){
-                channel.Add(new Item(item));
-            }
-
-            channel.FinishRefresh();
-        return updated;
-    }
+            return channel.Update(feed);
+        }
 
     private static void set_credentials_from_uri(WebRequest req)
     {
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index ced5d76..2b4060a 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -80,7 +80,7 @@ namespace Imendio.Blam
             Console.WriteLine("about to format");
             SyndicationFeed feed = SyndicationFeed.Load(reader);
             foreach(SyndicationItem item in feed.Items){
-                instance.Add(new Item(item));
+                instance.Items.Add(item.Id, new Item(item));
             }
         }
 



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