[blam] ItemStore: Save and load the items in a file



commit ab9a7eb19af95278b866a71b07a371d14feff25d
Author: Carlos Martín Nieto <carlos cmartin tk>
Date:   Mon Sep 27 15:43:18 2010 +0200

    ItemStore: Save and load the items in a file

 src/ItemStore.cs |   47 +++++++++++++++++++++++++++++++++--------------
 1 files changed, 33 insertions(+), 14 deletions(-)
---
diff --git a/src/ItemStore.cs b/src/ItemStore.cs
index 3112816..cedfd33 100644
--- a/src/ItemStore.cs
+++ b/src/ItemStore.cs
@@ -11,27 +11,30 @@ namespace Imendio.Blam
     {
         Hashtable items;
         static ItemStore instance = null;
-        static XmlSerializer serializer = new XmlSerializer(typeof(ItemStore));
         static string itemfile = Defines.APP_HOMEDIR + "/" + Defines.APP_ITEMSTORE_FILE;
 
-        public ArrayList _items = new ArrayList();
-
-        [XmlIgnore]
         public Hashtable Items {
             get {
                 return items;
             }
+
+            set {
+                items = value;
+            }
         }
 
         public void Add(Item item)
         {
-            _items.Add(item);
+            if(items.ContainsKey(item.Id)){
+                /* FIXME: See if we can try to update or something */
+                return;
+            }
+
             items.Add(item.Id, item);
         }
 
         public void Remove(Item item)
         {
-            _items.Remove(item);
             items.Remove(item.Id);
         }
 
@@ -46,12 +49,21 @@ namespace Imendio.Blam
 
         private static void Load()
         {
+            instance = new ItemStore();
+
+            XmlReader reader;
             try{
-                XmlTextReader reader = new XmlTextReader(itemfile);
-                instance = serializer.Deserialize(reader) as ItemStore;
-            } catch(FileNotFoundException)
-            {
-                instance = new ItemStore();
+                reader = new XmlTextReader(itemfile);
+            } catch(FileNotFoundException){
+                Console.WriteLine("item file not found");
+                instance.items = new Hashtable();
+                return;
+            }
+
+            Console.WriteLine("about to format");
+            SyndicationFeed feed = SyndicationFeed.Load(reader);
+            foreach(SyndicationItem item in feed.Items){
+                instance.Add(new Item(item));
             }
         }
 
@@ -61,8 +73,16 @@ namespace Imendio.Blam
                 return;
             }
 
-            Stream writer = new FileStream(itemfile, FileMode.Create);
-            serializer.Serialize(writer, instance);
+            foreach(Item item in instance.items.Values){
+                item.WriteExtensions();
+            }
+            XmlWriter writer = XmlWriter.Create(itemfile);
+            SyndicationFeed sf = new SyndicationFeed();
+            Item[] items = new Item[instance.items.Count];
+            instance.items.Values.CopyTo(items, 0);
+            sf.Items = items;
+            SyndicationFeedFormatter fmtr = new Atom10FeedFormatter(sf);
+            fmtr.WriteTo(writer);
         }
 
        private ItemStore ()
@@ -71,4 +91,3 @@ namespace Imendio.Blam
        }
     }
 }
-



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