/[EmuXPortal]/trunk/EmuXPortal/Api/RomFavorite.cs
ViewVC logotype

Diff of /trunk/EmuXPortal/Api/RomFavorite.cs

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

--- trunk/EmuXPortal/Api/RomFavorite.cs	2014/08/04 05:36:08	172
+++ trunk/EmuXPortal/Api/RomFavorite.cs	2014/08/04 05:57:32	173
@@ -10,26 +10,51 @@
 
 namespace EmuXPortal.Api
 {
-    public class RomFavorite
+    public interface IRomFavorite : IComparable<IRomFavorite>
     {
+        IRomConfig RomConfig { get; }
+        DateTime DateAdded { get; }
+    }
+    public class RomFavorite : IRomFavorite
+    {
+        public RomFavorite(IRomConfig config)
+        {
+            this.RomConfig = config;
+            this.DateAdded = DateTime.Now;
+        }
+        public RomFavorite(IRomConfig config, DateTime added) : this(config)
+        {
+            this.DateAdded = added;
+        }
+        #region IFavoriteRom members
+        public IRomConfig RomConfig { get; private set; }
+        public DateTime DateAdded { get; private set; }
+        #endregion
+        #region  IComparable<IRomFavorite> members
+        public int CompareTo(IRomFavorite other)
+        {
+            return this.DateAdded.CompareTo(other.DateAdded);
+        }
+        #endregion
         #region constants
         const string FAVORITES = "favorites";
         const string FAVORITE = "favorite";
         const string EMU_CONFIG = "config";
         const string ROM_FILE = "rom";
+        const string DATE_ADDED = "date_added";
         #endregion
-        private static List<IRomConfig> lst_favorites = new List<IRomConfig>();
+        private static List<IRomFavorite> lst_favorites = new List<IRomFavorite>();
 
-        private class FavoriesComparor : IEqualityComparer<IRomConfig>
+        private class FavoriesComparor : IEqualityComparer<IRomFavorite>
         {
-            public bool Equals(IRomConfig x, IRomConfig y) { return x.RomFile.Equals(y.RomFile); }
-            public int GetHashCode(IRomConfig obj) { return obj.RomFile.GetHashCode(); }
+            public bool Equals(IRomFavorite x, IRomFavorite y) { return x.RomConfig.RomFile.Equals(y.RomConfig.RomFile); }
+            public int GetHashCode(IRomFavorite obj) { return obj.RomConfig.RomFile.GetHashCode(); }
         }
         #region static members
 
-        public static List<IRomConfig> GetFavorites()
+        public static List<IRomFavorite> GetFavorites()
         {
-            List<IRomConfig> favorites = new List<IRomConfig>();
+            List<IRomFavorite> favorites = new List<IRomFavorite>();
             foreach (var rom in lst_favorites)
             {
                 favorites.Add(rom);
@@ -41,7 +66,7 @@
         {
             foreach (var c_rom in lst_favorites)
             {
-                if (c_rom.RomFile == rom.RomFile)
+                if (c_rom.RomConfig.RomFile == rom.RomFile)
                 {
                     return true;
                 }
@@ -55,7 +80,7 @@
                 log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
                 return false;
             }
-            lst_favorites.Add(rom);
+            lst_favorites.Add(new RomFavorite(rom));
             log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
             return true;
         }
@@ -66,7 +91,23 @@
                 log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
                 return false;
             }
-            lst_favorites.Remove(rom);
+            //lst_favorites.Remove(new RomFavorite(rom));
+
+            IRomFavorite f_rom = new RomFavorite(rom);
+            List<int> indices = new List<int>();
+            for (int i = 0; i < lst_favorites.Count; i++)
+            {
+                var k = lst_favorites[i];
+                if (k.RomConfig.RomFile == rom.RomFile)
+                {
+                    indices.Add(lst_favorites.IndexOf(k));
+                }
+            }
+            foreach (var index in indices)
+            {
+                lst_favorites.RemoveAt(index);
+            }
+
             log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
             return true;
         }
@@ -115,13 +156,12 @@
                             {
                                 int count = reader.AttributeCount;
                                 string config = reader.GetAttribute(EMU_CONFIG);
-                                string rom = reader.GetAttribute(ROM_FILE);
-                                log.WriteLine("\tCONFIG: {0}", config);
-                                log.WriteLine("\tROM: {0}", rom);
-                                // load romfile from config and file
+                                string rom = reader.GetAttribute(ROM_FILE);                                
+                                string added = reader.GetAttribute(DATE_ADDED);
+                                if (added == null) { added = DateTime.Now.ToString(); }
                                 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
                                 var emu_rom = RomLoader.Load(log, rom, emu_config);
-                                lst_favorites.Add(emu_rom);
+                                lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
                             }                            
                         }
                     }
@@ -143,6 +183,7 @@
         {
             // clean-up any duplicates
             lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
+            lst_favorites.Sort();
             string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
             try
             {
@@ -168,8 +209,10 @@
                         foreach (var rom in lst_favorites)
                         {
                             // config file
-                            string rom_config_path = rom.Config.ConfigPath;
-                            string rom_file = rom.RomFile;
+                            string rom_config_path = rom.RomConfig.Config.ConfigPath;
+                            string rom_file = rom.RomConfig.RomFile;
+                            string date = rom.DateAdded.ToString();
+
                             writer.WriteRaw(System.Environment.NewLine);
 
                             writer.WriteStartElement(FAVORITE);
@@ -182,6 +225,11 @@
                             writer.WriteString(rom_file);
                             writer.WriteEndAttribute();
 
+                            
+                            writer.WriteStartAttribute(DATE_ADDED);
+                            writer.WriteString(date);
+                            writer.WriteEndAttribute();
+
                             writer.WriteEndElement();
 
                         }

 

  ViewVC Help
Powered by ViewVC 1.1.22