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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 172 - (show annotations) (download)
Mon Aug 4 05:36:08 2014 UTC (6 years, 2 months ago) by william
File size: 7906 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 class RomFavorite
14 {
15 #region constants
16 const string FAVORITES = "favorites";
17 const string FAVORITE = "favorite";
18 const string EMU_CONFIG = "config";
19 const string ROM_FILE = "rom";
20 #endregion
21 private static List<IRomConfig> lst_favorites = new List<IRomConfig>();
22
23 private class FavoriesComparor : IEqualityComparer<IRomConfig>
24 {
25 public bool Equals(IRomConfig x, IRomConfig y) { return x.RomFile.Equals(y.RomFile); }
26 public int GetHashCode(IRomConfig obj) { return obj.RomFile.GetHashCode(); }
27 }
28 #region static members
29
30 public static List<IRomConfig> GetFavorites()
31 {
32 List<IRomConfig> favorites = new List<IRomConfig>();
33 foreach (var rom in lst_favorites)
34 {
35 favorites.Add(rom);
36 }
37 return favorites;
38 }
39
40 public static bool IsFavorite(logger log,IRomConfig rom)
41 {
42 foreach (var c_rom in lst_favorites)
43 {
44 if (c_rom.RomFile == rom.RomFile)
45 {
46 return true;
47 }
48 }
49 return false;
50 }
51 public static bool AddFavorite(logger log, IRomConfig rom)
52 {
53 if (IsFavorite(log,rom))
54 {
55 log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
56 return false;
57 }
58 lst_favorites.Add(rom);
59 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
60 return true;
61 }
62 public static bool RemoveFavorite(logger log, IRomConfig rom)
63 {
64 if (!IsFavorite(log, rom))
65 {
66 log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
67 return false;
68 }
69 lst_favorites.Remove(rom);
70 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
71 return true;
72 }
73 public static bool UpdateFavorites(logger log)
74 {
75 bool ret = SaveFavorites(log);
76 if (!ret)
77 {
78 log.WriteLine("Failed to update favorites (failed to save)");
79 }
80 else
81 {
82 ret = Config.ConfigLoader.LoadFavorites(log);
83 if (!ret)
84 {
85 log.WriteLine("Failed to update favorites (failed to load)");
86 }
87 else
88 {
89 log.WriteLine("Updated favorites");
90 }
91 }
92 return ret;
93 }
94
95 internal static bool LoadFavorites(logger log, string ini)
96 {
97 try
98 {
99 lst_favorites.Clear();
100 log.WriteLine("Loading Favorites: {0}", ini);
101 FileInfo fi = new FileInfo(ini);
102 if (!fi.Exists)
103 {
104 log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
105 return false;
106 }
107 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
108 {
109 using (XmlReader reader = XmlReader.Create(fs))
110 {
111 reader.ReadToDescendant(FAVORITES);
112 while (reader.Read())
113 {
114 if (reader.HasAttributes && reader.Name == FAVORITE)
115 {
116 int count = reader.AttributeCount;
117 string config = reader.GetAttribute(EMU_CONFIG);
118 string rom = reader.GetAttribute(ROM_FILE);
119 log.WriteLine("\tCONFIG: {0}", config);
120 log.WriteLine("\tROM: {0}", rom);
121 // load romfile from config and file
122 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
123 var emu_rom = RomLoader.Load(log, rom, emu_config);
124 lst_favorites.Add(emu_rom);
125 }
126 }
127 }
128 }
129 log.WriteLine("Loaded Favorites: {0}", ini);
130 SaveFavorites(log);
131 return true;
132
133 }
134 catch (Exception ex)
135 {
136 log.WriteLine("\tFailed to Load Favorites: {0}", ini);
137 Console.WriteLine(ex.ToString());
138 log.WriteLine("Error: {0}", ex.ToString());
139 return false;
140 }
141 }
142 internal static bool SaveFavorites(logger log)
143 {
144 // clean-up any duplicates
145 lst_favorites = lst_favorites.Distinct(new FavoriesComparor()).ToList();
146 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
147 try
148 {
149 log.WriteLine("Saving Favorites: {0}", ini);
150 FileInfo fi = new FileInfo(ini);
151 if (fi.Exists)
152 {
153 log.WriteLine("Favorites file already exists - overwriting: {0}", ini);
154 fi.Delete();
155 }
156 else
157 {
158 log.WriteLine("Favorites file does not exist - creating: {0}", ini);
159 }
160 using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
161 {
162 XmlWriterSettings settings = new XmlWriterSettings();
163 using (XmlWriter writer = XmlWriter.Create(fs, settings))
164 {
165 writer.WriteRaw(System.Environment.NewLine);
166 writer.WriteStartElement(FAVORITES);
167
168 foreach (var rom in lst_favorites)
169 {
170 // config file
171 string rom_config_path = rom.Config.ConfigPath;
172 string rom_file = rom.RomFile;
173 writer.WriteRaw(System.Environment.NewLine);
174
175 writer.WriteStartElement(FAVORITE);
176
177 writer.WriteStartAttribute(EMU_CONFIG);
178 writer.WriteString(rom_config_path);
179 writer.WriteEndAttribute();
180
181 writer.WriteStartAttribute(ROM_FILE);
182 writer.WriteString(rom_file);
183 writer.WriteEndAttribute();
184
185 writer.WriteEndElement();
186
187 }
188 writer.WriteRaw(System.Environment.NewLine);
189 writer.WriteFullEndElement();
190 }
191 }
192
193 log.WriteLine("Saved Favorites: {0}", ini);
194 return true;
195
196 }
197 catch (Exception ex)
198 {
199 log.WriteLine("\tFailed to Save Favorites: {0}", ini);
200 Console.WriteLine(ex.ToString());
201 log.WriteLine("Error: {0}", ex.ToString());
202 return false;
203 }
204 }
205 #endregion
206 }
207 }

  ViewVC Help
Powered by ViewVC 1.1.22