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

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

Parent Directory Parent Directory | Revision Log Revision Log


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

  ViewVC Help
Powered by ViewVC 1.1.22