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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 173 - (hide annotations) (download)
Mon Aug 4 05:57:32 2014 UTC (6 years, 2 months ago) by william
File size: 9776 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 173 public static List<IRomFavorite> GetFavorites()
56 william 172 {
57 william 173 List<IRomFavorite> favorites = new List<IRomFavorite>();
58 william 172 foreach (var rom in lst_favorites)
59     {
60     favorites.Add(rom);
61     }
62     return favorites;
63     }
64    
65 william 169 public static bool IsFavorite(logger log,IRomConfig rom)
66 william 168 {
67 william 172 foreach (var c_rom in lst_favorites)
68     {
69 william 173 if (c_rom.RomConfig.RomFile == rom.RomFile)
70 william 172 {
71     return true;
72     }
73     }
74     return false;
75 william 168 }
76 william 169 public static bool AddFavorite(logger log, IRomConfig rom)
77 william 168 {
78 william 169 if (IsFavorite(log,rom))
79     {
80     log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
81     return false;
82     }
83 william 173 lst_favorites.Add(new RomFavorite(rom));
84 william 169 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
85     return true;
86 william 168 }
87 william 169 public static bool RemoveFavorite(logger log, IRomConfig rom)
88 william 168 {
89 william 169 if (!IsFavorite(log, rom))
90     {
91     log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
92     return false;
93     }
94 william 173 //lst_favorites.Remove(new RomFavorite(rom));
95    
96     IRomFavorite f_rom = new RomFavorite(rom);
97     List<int> indices = new List<int>();
98     for (int i = 0; i < lst_favorites.Count; i++)
99     {
100     var k = lst_favorites[i];
101     if (k.RomConfig.RomFile == rom.RomFile)
102     {
103     indices.Add(lst_favorites.IndexOf(k));
104     }
105     }
106     foreach (var index in indices)
107     {
108     lst_favorites.RemoveAt(index);
109     }
110    
111 william 169 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
112     return true;
113 william 168 }
114 william 169 public static bool UpdateFavorites(logger log)
115 william 172 {
116 william 169 bool ret = SaveFavorites(log);
117     if (!ret)
118     {
119     log.WriteLine("Failed to update favorites (failed to save)");
120     }
121     else
122     {
123     ret = Config.ConfigLoader.LoadFavorites(log);
124     if (!ret)
125     {
126     log.WriteLine("Failed to update favorites (failed to load)");
127     }
128     else
129     {
130     log.WriteLine("Updated favorites");
131     }
132     }
133     return ret;
134 william 168 }
135 william 169
136     internal static bool LoadFavorites(logger log, string ini)
137     {
138     try
139     {
140 william 171 lst_favorites.Clear();
141 william 169 log.WriteLine("Loading Favorites: {0}", ini);
142     FileInfo fi = new FileInfo(ini);
143     if (!fi.Exists)
144     {
145     log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
146     return false;
147     }
148     using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
149     {
150     using (XmlReader reader = XmlReader.Create(fs))
151     {
152 william 170 reader.ReadToDescendant(FAVORITES);
153     while (reader.Read())
154     {
155     if (reader.HasAttributes && reader.Name == FAVORITE)
156     {
157     int count = reader.AttributeCount;
158     string config = reader.GetAttribute(EMU_CONFIG);
159 william 173 string rom = reader.GetAttribute(ROM_FILE);
160     string added = reader.GetAttribute(DATE_ADDED);
161     if (added == null) { added = DateTime.Now.ToString(); }
162 william 170 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
163     var emu_rom = RomLoader.Load(log, rom, emu_config);
164 william 173 lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
165 william 170 }
166     }
167 william 169 }
168 william 172 }
169 william 169 log.WriteLine("Loaded Favorites: {0}", ini);
170 william 172 SaveFavorites(log);
171 william 169 return true;
172    
173     }
174     catch (Exception ex)
175     {
176     log.WriteLine("\tFailed to Load Favorites: {0}", ini);
177     Console.WriteLine(ex.ToString());
178     log.WriteLine("Error: {0}", ex.ToString());
179     return false;
180     }
181     }
182     internal static bool SaveFavorites(logger log)
183     {
184 william 172 // clean-up any duplicates
185     lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
186 william 173 lst_favorites.Sort();
187 william 169 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
188     try
189     {
190     log.WriteLine("Saving Favorites: {0}", ini);
191     FileInfo fi = new FileInfo(ini);
192     if (fi.Exists)
193     {
194     log.WriteLine("Favorites file already exists - overwriting: {0}", ini);
195     fi.Delete();
196     }
197     else
198     {
199     log.WriteLine("Favorites file does not exist - creating: {0}", ini);
200     }
201     using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
202     {
203     XmlWriterSettings settings = new XmlWriterSettings();
204 william 170 using (XmlWriter writer = XmlWriter.Create(fs, settings))
205 william 169 {
206 william 170 writer.WriteRaw(System.Environment.NewLine);
207     writer.WriteStartElement(FAVORITES);
208 william 169
209     foreach (var rom in lst_favorites)
210     {
211 william 170 // config file
212 william 173 string rom_config_path = rom.RomConfig.Config.ConfigPath;
213     string rom_file = rom.RomConfig.RomFile;
214     string date = rom.DateAdded.ToString();
215    
216 william 170 writer.WriteRaw(System.Environment.NewLine);
217    
218     writer.WriteStartElement(FAVORITE);
219    
220     writer.WriteStartAttribute(EMU_CONFIG);
221     writer.WriteString(rom_config_path);
222     writer.WriteEndAttribute();
223    
224     writer.WriteStartAttribute(ROM_FILE);
225     writer.WriteString(rom_file);
226     writer.WriteEndAttribute();
227    
228 william 173
229     writer.WriteStartAttribute(DATE_ADDED);
230     writer.WriteString(date);
231     writer.WriteEndAttribute();
232    
233 william 170 writer.WriteEndElement();
234    
235 william 169 }
236 william 170 writer.WriteRaw(System.Environment.NewLine);
237     writer.WriteFullEndElement();
238 william 169 }
239     }
240    
241     log.WriteLine("Saved Favorites: {0}", ini);
242     return true;
243    
244     }
245     catch (Exception ex)
246     {
247     log.WriteLine("\tFailed to Save Favorites: {0}", ini);
248     Console.WriteLine(ex.ToString());
249     log.WriteLine("Error: {0}", ex.ToString());
250     return false;
251     }
252     }
253 william 168 #endregion
254     }
255 william 170 }

  ViewVC Help
Powered by ViewVC 1.1.22