ViewVC Help
View File | Revision Log | Show Annotations | Download File | View Changeset | Root Listing
root/EmuXPortal/trunk/EmuXPortal/Api/RomFavorite.cs
Revision: 238
Committed: Tue Aug 5 04:50:39 2014 UTC (9 years, 6 months ago) by william
File size: 12058 byte(s)
Log Message:
+ use Enterprise.Logging instead of EmuXPortal.Logging

File Contents

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