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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 173 - (show annotations) (download)
Mon Aug 4 05:57:32 2014 UTC (6 years, 1 month ago) by william
File size: 9776 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 public static List<IRomFavorite> GetFavorites()
56 {
57 List<IRomFavorite> favorites = new List<IRomFavorite>();
58 foreach (var rom in lst_favorites)
59 {
60 favorites.Add(rom);
61 }
62 return favorites;
63 }
64
65 public static bool IsFavorite(logger log,IRomConfig rom)
66 {
67 foreach (var c_rom in lst_favorites)
68 {
69 if (c_rom.RomConfig.RomFile == rom.RomFile)
70 {
71 return true;
72 }
73 }
74 return false;
75 }
76 public static bool AddFavorite(logger log, IRomConfig rom)
77 {
78 if (IsFavorite(log,rom))
79 {
80 log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
81 return false;
82 }
83 lst_favorites.Add(new RomFavorite(rom));
84 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
85 return true;
86 }
87 public static bool RemoveFavorite(logger log, IRomConfig rom)
88 {
89 if (!IsFavorite(log, rom))
90 {
91 log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
92 return false;
93 }
94 //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 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
112 return true;
113 }
114 public static bool UpdateFavorites(logger log)
115 {
116 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 }
135
136 internal static bool LoadFavorites(logger log, string ini)
137 {
138 try
139 {
140 lst_favorites.Clear();
141 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 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 string rom = reader.GetAttribute(ROM_FILE);
160 string added = reader.GetAttribute(DATE_ADDED);
161 if (added == null) { added = DateTime.Now.ToString(); }
162 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
163 var emu_rom = RomLoader.Load(log, rom, emu_config);
164 lst_favorites.Add(new RomFavorite(emu_rom, DateTime.Parse(added)));
165 }
166 }
167 }
168 }
169 log.WriteLine("Loaded Favorites: {0}", ini);
170 SaveFavorites(log);
171 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 // clean-up any duplicates
185 lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
186 lst_favorites.Sort();
187 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 using (XmlWriter writer = XmlWriter.Create(fs, settings))
205 {
206 writer.WriteRaw(System.Environment.NewLine);
207 writer.WriteStartElement(FAVORITES);
208
209 foreach (var rom in lst_favorites)
210 {
211 // config file
212 string rom_config_path = rom.RomConfig.Config.ConfigPath;
213 string rom_file = rom.RomConfig.RomFile;
214 string date = rom.DateAdded.ToString();
215
216 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
229 writer.WriteStartAttribute(DATE_ADDED);
230 writer.WriteString(date);
231 writer.WriteEndAttribute();
232
233 writer.WriteEndElement();
234
235 }
236 writer.WriteRaw(System.Environment.NewLine);
237 writer.WriteFullEndElement();
238 }
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 #endregion
254 }
255 }

  ViewVC Help
Powered by ViewVC 1.1.22