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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 171 - (hide annotations) (download)
Mon Aug 4 05:06:37 2014 UTC (6 years, 2 months ago) by william
File size: 6955 byte(s)

1 william 169 using EmuXPortal.Logging;
2     using System;
3 william 168 using System.Collections.Generic;
4 william 170 using System.Diagnostics;
5 william 169 using System.IO;
6 william 168 using System.Linq;
7     using System.Text;
8 william 170 using System.Windows.Forms;
9 william 169 using System.Xml;
10 william 168
11     namespace EmuXPortal.Api
12     {
13     public class RomFavorite
14     {
15 william 169 #region constants
16 william 170 const string FAVORITES = "favorites";
17     const string FAVORITE = "favorite";
18     const string EMU_CONFIG = "config";
19     const string ROM_FILE = "rom";
20 william 169 #endregion
21     private static List<IRomConfig> lst_favorites = new List<IRomConfig>();
22 william 168 #region static members
23 william 169 public static bool IsFavorite(logger log,IRomConfig rom)
24 william 168 {
25 william 169 return lst_favorites.Contains(rom);
26 william 168 }
27 william 169 public static bool AddFavorite(logger log, IRomConfig rom)
28 william 168 {
29 william 169 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 william 168 }
38 william 169 public static bool RemoveFavorite(logger log, IRomConfig rom)
39 william 168 {
40 william 169 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 william 168 }
49 william 169 public static bool UpdateFavorites(logger log)
50 william 168 {
51 william 169 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 william 168 }
70 william 169
71     internal static bool LoadFavorites(logger log, string ini)
72     {
73     try
74     {
75 william 171 lst_favorites.Clear();
76 william 169 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 william 170 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 william 171 lst_favorites.Add(emu_rom);
101 william 170 }
102     }
103 william 169 }
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 william 170 using (XmlWriter writer = XmlWriter.Create(fs, settings))
137 william 169 {
138 william 170 writer.WriteRaw(System.Environment.NewLine);
139     writer.WriteStartElement(FAVORITES);
140 william 169
141     foreach (var rom in lst_favorites)
142     {
143 william 170 // 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 william 169 }
161 william 170 writer.WriteRaw(System.Environment.NewLine);
162     writer.WriteFullEndElement();
163 william 169 }
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 william 168 #endregion
179     }
180 william 170 }

  ViewVC Help
Powered by ViewVC 1.1.22