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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 175 - (hide annotations) (download)
Mon Aug 4 06:31:48 2014 UTC (6 years, 2 months ago) by william
File size: 10117 byte(s)

1 william 169 using EmuXPortal.Logging;
2     using System;
3 william 168 using System.Collections.Generic;
4 william 170 using System.Diagnostics;
5 william 169 using System.IO;
6 william 168 using System.Linq;
7     using System.Text;
8 william 170 using System.Windows.Forms;
9 william 169 using System.Xml;
10 william 168
11     namespace EmuXPortal.Api
12     {
13 william 173 public interface IRomFavorite : IComparable<IRomFavorite>
14 william 168 {
15 william 173 IRomConfig RomConfig { get; }
16     DateTime DateAdded { get; }
17     }
18     public class RomFavorite : IRomFavorite
19     {
20     public RomFavorite(IRomConfig config)
21     {
22     this.RomConfig = config;
23     this.DateAdded = DateTime.Now;
24     }
25     public RomFavorite(IRomConfig config, DateTime added) : this(config)
26     {
27     this.DateAdded = added;
28     }
29     #region IFavoriteRom members
30     public IRomConfig RomConfig { get; private set; }
31     public DateTime DateAdded { get; private set; }
32     #endregion
33     #region IComparable<IRomFavorite> members
34     public int CompareTo(IRomFavorite other)
35     {
36     return this.DateAdded.CompareTo(other.DateAdded);
37     }
38     #endregion
39 william 169 #region constants
40 william 170 const string FAVORITES = "favorites";
41     const string FAVORITE = "favorite";
42     const string EMU_CONFIG = "config";
43     const string ROM_FILE = "rom";
44 william 173 const string DATE_ADDED = "date_added";
45 william 169 #endregion
46 william 173 private static List<IRomFavorite> lst_favorites = new List<IRomFavorite>();
47 william 172
48 william 173 private class FavoriesComparor : IEqualityComparer<IRomFavorite>
49 william 172 {
50 william 173 public bool Equals(IRomFavorite x, IRomFavorite y) { return x.RomConfig.RomFile.Equals(y.RomConfig.RomFile); }
51     public int GetHashCode(IRomFavorite obj) { return obj.RomConfig.RomFile.GetHashCode(); }
52 william 172 }
53 william 168 #region static members
54 william 172
55 william 175 internal static List<IRomConfig> GetRoms(logger log)
56     {
57     List<IRomConfig> roms = new List<IRomConfig>();
58     lst_favorites.ForEach(s => roms.Add(s.RomConfig));
59     return roms;
60     }
61    
62 william 173 public static List<IRomFavorite> GetFavorites()
63 william 172 {
64 william 173 List<IRomFavorite> favorites = new List<IRomFavorite>();
65 william 172 foreach (var rom in lst_favorites)
66     {
67     favorites.Add(rom);
68     }
69     return favorites;
70     }
71    
72 william 169 public static bool IsFavorite(logger log,IRomConfig rom)
73 william 168 {
74 william 172 foreach (var c_rom in lst_favorites)
75     {
76 william 173 if (c_rom.RomConfig.RomFile == rom.RomFile)
77 william 172 {
78     return true;
79     }
80     }
81     return false;
82 william 168 }
83 william 169 public static bool AddFavorite(logger log, IRomConfig rom)
84 william 168 {
85 william 169 if (IsFavorite(log,rom))
86     {
87     log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
88     return false;
89     }
90 william 173 lst_favorites.Add(new RomFavorite(rom));
91 william 169 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
92     return true;
93 william 168 }
94 william 169 public static bool RemoveFavorite(logger log, IRomConfig rom)
95 william 168 {
96 william 169 if (!IsFavorite(log, rom))
97     {
98     log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
99     return false;
100     }
101 william 173 //lst_favorites.Remove(new RomFavorite(rom));
102    
103     IRomFavorite f_rom = new RomFavorite(rom);
104     List<int> indices = new List<int>();
105     for (int i = 0; i < lst_favorites.Count; i++)
106     {
107     var k = lst_favorites[i];
108     if (k.RomConfig.RomFile == rom.RomFile)
109     {
110     indices.Add(lst_favorites.IndexOf(k));
111     }
112     }
113     foreach (var index in indices)
114     {
115     lst_favorites.RemoveAt(index);
116     }
117    
118 william 169 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
119     return true;
120 william 168 }
121 william 169 public static bool UpdateFavorites(logger log)
122 william 172 {
123 william 169 bool ret = SaveFavorites(log);
124     if (!ret)
125     {
126     log.WriteLine("Failed to update favorites (failed to save)");
127     }
128     else
129     {
130     ret = Config.ConfigLoader.LoadFavorites(log);
131     if (!ret)
132     {
133     log.WriteLine("Failed to update favorites (failed to load)");
134     }
135     else
136     {
137     log.WriteLine("Updated favorites");
138     }
139     }
140     return ret;
141 william 168 }
142 william 169
143     internal static bool LoadFavorites(logger log, string ini)
144     {
145     try
146     {
147 william 171 lst_favorites.Clear();
148 william 169 log.WriteLine("Loading Favorites: {0}", ini);
149     FileInfo fi = new FileInfo(ini);
150     if (!fi.Exists)
151     {
152     log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
153     return false;
154     }
155     using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
156     {
157     using (XmlReader reader = XmlReader.Create(fs))
158     {
159 william 170 reader.ReadToDescendant(FAVORITES);
160     while (reader.Read())
161     {
162     if (reader.HasAttributes && reader.Name == FAVORITE)
163     {
164     int count = reader.AttributeCount;
165     string config = reader.GetAttribute(EMU_CONFIG);
166 william 173 string rom = reader.GetAttribute(ROM_FILE);
167     string added = reader.GetAttribute(DATE_ADDED);
168     if (added == null) { added = DateTime.Now.ToString(); }
169 william 170 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
170     var emu_rom = RomLoader.Load(log, rom, emu_config);
171 william 173 lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
172 william 170 }
173     }
174 william 169 }
175 william 172 }
176 william 169 log.WriteLine("Loaded Favorites: {0}", ini);
177 william 172 SaveFavorites(log);
178 william 169 return true;
179    
180     }
181     catch (Exception ex)
182     {
183     log.WriteLine("\tFailed to Load Favorites: {0}", ini);
184     Console.WriteLine(ex.ToString());
185     log.WriteLine("Error: {0}", ex.ToString());
186     return false;
187     }
188     }
189     internal static bool SaveFavorites(logger log)
190     {
191 william 172 // clean-up any duplicates
192     lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
193 william 174 lst_favorites = lst_favorites.OrderBy(s => s.RomConfig.Config.PlatformNameShort).ThenBy(s => s.DateAdded).Reverse().ToList();
194 william 169 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
195     try
196     {
197     log.WriteLine("Saving Favorites: {0}", ini);
198     FileInfo fi = new FileInfo(ini);
199     if (fi.Exists)
200     {
201     log.WriteLine("Favorites file already exists - overwriting: {0}", ini);
202     fi.Delete();
203     }
204     else
205     {
206     log.WriteLine("Favorites file does not exist - creating: {0}", ini);
207     }
208     using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
209     {
210     XmlWriterSettings settings = new XmlWriterSettings();
211 william 170 using (XmlWriter writer = XmlWriter.Create(fs, settings))
212 william 169 {
213 william 170 writer.WriteRaw(System.Environment.NewLine);
214     writer.WriteStartElement(FAVORITES);
215 william 169
216     foreach (var rom in lst_favorites)
217     {
218 william 170 // config file
219 william 173 string rom_config_path = rom.RomConfig.Config.ConfigPath;
220     string rom_file = rom.RomConfig.RomFile;
221     string date = rom.DateAdded.ToString();
222    
223 william 170 writer.WriteRaw(System.Environment.NewLine);
224    
225     writer.WriteStartElement(FAVORITE);
226    
227     writer.WriteStartAttribute(EMU_CONFIG);
228     writer.WriteString(rom_config_path);
229     writer.WriteEndAttribute();
230    
231     writer.WriteStartAttribute(ROM_FILE);
232     writer.WriteString(rom_file);
233     writer.WriteEndAttribute();
234    
235 william 173
236     writer.WriteStartAttribute(DATE_ADDED);
237     writer.WriteString(date);
238     writer.WriteEndAttribute();
239    
240 william 170 writer.WriteEndElement();
241    
242 william 169 }
243 william 170 writer.WriteRaw(System.Environment.NewLine);
244     writer.WriteFullEndElement();
245 william 169 }
246     }
247    
248     log.WriteLine("Saved Favorites: {0}", ini);
249     return true;
250    
251     }
252     catch (Exception ex)
253     {
254     log.WriteLine("\tFailed to Save Favorites: {0}", ini);
255     Console.WriteLine(ex.ToString());
256     log.WriteLine("Error: {0}", ex.ToString());
257     return false;
258     }
259     }
260 william 168 #endregion
261     }
262 william 170 }

  ViewVC Help
Powered by ViewVC 1.1.22