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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

1 using EmuXPortal.Logging;
2 using System;
3 using System.Collections.Generic;
4 using System.Diagnostics;
5 using System.IO;
6 using System.Linq;
7 using System.Text;
8 using System.Windows.Forms;
9 using System.Xml;
10
11 namespace EmuXPortal.Api
12 {
13 public interface IRomFavorite : IComparable<IRomFavorite>
14 {
15 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 #region constants
40 const string FAVORITES = "favorites";
41 const string FAVORITE = "favorite";
42 const string EMU_CONFIG = "config";
43 const string ROM_FILE = "rom";
44 const string DATE_ADDED = "date_added";
45 #endregion
46 private static List<IRomFavorite> lst_favorites = new List<IRomFavorite>();
47
48 private class FavoriesComparor : IEqualityComparer<IRomFavorite>
49 {
50 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 }
53 #region static members
54
55 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 public static List<IRomFavorite> GetFavorites()
63 {
64 List<IRomFavorite> favorites = new List<IRomFavorite>();
65 foreach (var rom in lst_favorites)
66 {
67 favorites.Add(rom);
68 }
69 return favorites;
70 }
71
72 public static bool IsFavorite(logger log,IRomConfig rom)
73 {
74 foreach (var c_rom in lst_favorites)
75 {
76 if (c_rom.RomConfig.RomFile == rom.RomFile)
77 {
78 return true;
79 }
80 }
81 return false;
82 }
83 public static bool AddFavorite(logger log, IRomConfig rom)
84 {
85 if (IsFavorite(log,rom))
86 {
87 log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
88 return false;
89 }
90 lst_favorites.Add(new RomFavorite(rom));
91 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
92 return true;
93 }
94 public static bool RemoveFavorite(logger log, IRomConfig rom)
95 {
96 if (!IsFavorite(log, rom))
97 {
98 log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
99 return false;
100 }
101 //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 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
119 return true;
120 }
121 public static bool UpdateFavorites(logger log)
122 {
123 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 }
142
143 internal static bool LoadFavorites(logger log, string ini)
144 {
145 try
146 {
147 lst_favorites.Clear();
148 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 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 string rom = reader.GetAttribute(ROM_FILE);
167 string added = reader.GetAttribute(DATE_ADDED);
168 if (added == null) { added = DateTime.Now.ToString(); }
169 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
170 var emu_rom = RomLoader.Load(log, rom, emu_config);
171 lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
172 }
173 }
174 }
175 }
176 log.WriteLine("Loaded Favorites: {0}", ini);
177 SaveFavorites(log);
178 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 // clean-up any duplicates
192 lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
193 lst_favorites = lst_favorites.OrderBy(s => s.RomConfig.Config.PlatformNameShort).ThenBy(s => s.DateAdded).Reverse().ToList();
194 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 using (XmlWriter writer = XmlWriter.Create(fs, settings))
212 {
213 writer.WriteRaw(System.Environment.NewLine);
214 writer.WriteStartElement(FAVORITES);
215
216 foreach (var rom in lst_favorites)
217 {
218 // config file
219 string rom_config_path = rom.RomConfig.Config.ConfigPath;
220 string rom_file = rom.RomConfig.RomFile;
221 string date = rom.DateAdded.ToString();
222
223 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
236 writer.WriteStartAttribute(DATE_ADDED);
237 writer.WriteString(date);
238 writer.WriteEndAttribute();
239
240 writer.WriteEndElement();
241
242 }
243 writer.WriteRaw(System.Environment.NewLine);
244 writer.WriteFullEndElement();
245 }
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 #endregion
261 }
262 }

  ViewVC Help
Powered by ViewVC 1.1.22