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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 171 - (show annotations) (download)
Mon Aug 4 05:06:37 2014 UTC (6 years, 1 month ago) by william
File size: 6955 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 lst_favorites.Clear();
76 log.WriteLine("Loading Favorites: {0}", ini);
77 FileInfo fi = new FileInfo(ini);
78 if (!fi.Exists)
79 {
80 log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
81 return false;
82 }
83 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
84 {
85 using (XmlReader reader = XmlReader.Create(fs))
86 {
87 reader.ReadToDescendant(FAVORITES);
88 while (reader.Read())
89 {
90 if (reader.HasAttributes && reader.Name == FAVORITE)
91 {
92 int count = reader.AttributeCount;
93 string config = reader.GetAttribute(EMU_CONFIG);
94 string rom = reader.GetAttribute(ROM_FILE);
95 log.WriteLine("\tCONFIG: {0}", config);
96 log.WriteLine("\tROM: {0}", rom);
97 // load romfile from config and file
98 IEmuConfig emu_config = EmuConfigLoader.Load(log, config, "");
99 var emu_rom = RomLoader.Load(log, rom, emu_config);
100 lst_favorites.Add(emu_rom);
101 }
102 }
103 }
104 }
105 log.WriteLine("Loaded Favorites: {0}", ini);
106 return true;
107
108 }
109 catch (Exception ex)
110 {
111 log.WriteLine("\tFailed to Load Favorites: {0}", ini);
112 Console.WriteLine(ex.ToString());
113 log.WriteLine("Error: {0}", ex.ToString());
114 return false;
115 }
116 }
117 internal static bool SaveFavorites(logger log)
118 {
119 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
120 try
121 {
122 log.WriteLine("Saving Favorites: {0}", ini);
123 FileInfo fi = new FileInfo(ini);
124 if (fi.Exists)
125 {
126 log.WriteLine("Favorites file already exists - overwriting: {0}", ini);
127 fi.Delete();
128 }
129 else
130 {
131 log.WriteLine("Favorites file does not exist - creating: {0}", ini);
132 }
133 using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
134 {
135 XmlWriterSettings settings = new XmlWriterSettings();
136 using (XmlWriter writer = XmlWriter.Create(fs, settings))
137 {
138 writer.WriteRaw(System.Environment.NewLine);
139 writer.WriteStartElement(FAVORITES);
140
141 foreach (var rom in lst_favorites)
142 {
143 // config file
144 string rom_config_path = rom.Config.ConfigPath;
145 string rom_file = rom.RomFile;
146 writer.WriteRaw(System.Environment.NewLine);
147
148 writer.WriteStartElement(FAVORITE);
149
150 writer.WriteStartAttribute(EMU_CONFIG);
151 writer.WriteString(rom_config_path);
152 writer.WriteEndAttribute();
153
154 writer.WriteStartAttribute(ROM_FILE);
155 writer.WriteString(rom_file);
156 writer.WriteEndAttribute();
157
158 writer.WriteEndElement();
159
160 }
161 writer.WriteRaw(System.Environment.NewLine);
162 writer.WriteFullEndElement();
163 }
164 }
165
166 log.WriteLine("Saved Favorites: {0}", ini);
167 return true;
168
169 }
170 catch (Exception ex)
171 {
172 log.WriteLine("\tFailed to Save Favorites: {0}", ini);
173 Console.WriteLine(ex.ToString());
174 log.WriteLine("Error: {0}", ex.ToString());
175 return false;
176 }
177 }
178 #endregion
179 }
180 }

  ViewVC Help
Powered by ViewVC 1.1.22