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

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

Parent Directory Parent Directory | Revision Log Revision Log


Revision 169 - (show annotations) (download)
Mon Aug 4 04:11:41 2014 UTC (6 years, 1 month ago) by william
File size: 4979 byte(s)

1 using EmuXPortal.Logging;
2 using System;
3 using System.Collections.Generic;
4 using System.IO;
5 using System.Linq;
6 using System.Text;
7 using System.Xml;
8
9 namespace EmuXPortal.Api
10 {
11 public class RomFavorite
12 {
13 #region constants
14 const string FAVORITE = "favories";
15 #endregion
16 private static List<IRomConfig> lst_favorites = new List<IRomConfig>();
17 #region static members
18 public static bool IsFavorite(logger log,IRomConfig rom)
19 {
20 return lst_favorites.Contains(rom);
21 }
22 public static bool AddFavorite(logger log, IRomConfig rom)
23 {
24 if (IsFavorite(log,rom))
25 {
26 log.WriteLine("Rom is already favorited: [{0}]",rom.RomTitle);
27 return false;
28 }
29 lst_favorites.Add(rom);
30 log.WriteLine("Added rom to favorites: [{0}]", rom.RomTitle);
31 return true;
32 }
33 public static bool RemoveFavorite(logger log, IRomConfig rom)
34 {
35 if (!IsFavorite(log, rom))
36 {
37 log.WriteLine("Rom is not favorited: [{0}]", rom.RomTitle);
38 return false;
39 }
40 lst_favorites.Remove(rom);
41 log.WriteLine("Removed rom from favorites: [{0}]", rom.RomTitle);
42 return true;
43 }
44 public static bool UpdateFavorites(logger log)
45 {
46 bool ret = SaveFavorites(log);
47 if (!ret)
48 {
49 log.WriteLine("Failed to update favorites (failed to save)");
50 }
51 else
52 {
53 ret = Config.ConfigLoader.LoadFavorites(log);
54 if (!ret)
55 {
56 log.WriteLine("Failed to update favorites (failed to load)");
57 }
58 else
59 {
60 log.WriteLine("Updated favorites");
61 }
62 }
63 return ret;
64 }
65
66 internal static bool LoadFavorites(logger log, string ini)
67 {
68 try
69 {
70 log.WriteLine("Loading Favorites: {0}", ini);
71 FileInfo fi = new FileInfo(ini);
72 if (!fi.Exists)
73 {
74 log.WriteLine("\tFailed to Load Favorites (file not found): {0}", ini);
75 return false;
76 }
77 using (FileStream fs = new FileStream(ini, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
78 {
79 using (XmlReader reader = XmlReader.Create(fs))
80 {
81
82
83 }
84 }
85 log.WriteLine("Loaded Favorites: {0}", ini);
86 return true;
87
88 }
89 catch (Exception ex)
90 {
91 log.WriteLine("\tFailed to Load Favorites: {0}", ini);
92 Console.WriteLine(ex.ToString());
93 log.WriteLine("Error: {0}", ex.ToString());
94 return false;
95 }
96 }
97 internal static bool SaveFavorites(logger log)
98 {
99 string ini = Config.ConfigLoader.FAVORITES_FILE_INI;
100 try
101 {
102 log.WriteLine("Saving Favorites: {0}", ini);
103 FileInfo fi = new FileInfo(ini);
104 if (fi.Exists)
105 {
106 log.WriteLine("Favorites file already exists - overwriting: {0}", ini);
107 fi.Delete();
108 }
109 else
110 {
111 log.WriteLine("Favorites file does not exist - creating: {0}", ini);
112 }
113 using (FileStream fs = new FileStream(ini, FileMode.CreateNew, FileAccess.Write, FileShare.Read))
114 {
115 XmlWriterSettings settings = new XmlWriterSettings();
116 using (XmlWriter reader = XmlWriter.Create(fs, settings))
117 {
118 reader.WriteRaw(System.Environment.NewLine);
119 reader.WriteStartElement(FAVORITE);
120
121 foreach (var rom in lst_favorites)
122 {
123 }
124 reader.WriteRaw(System.Environment.NewLine);
125 reader.WriteFullEndElement();
126 }
127 }
128
129 log.WriteLine("Saved Favorites: {0}", ini);
130 return true;
131
132 }
133 catch (Exception ex)
134 {
135 log.WriteLine("\tFailed to Save Favorites: {0}", ini);
136 Console.WriteLine(ex.ToString());
137 log.WriteLine("Error: {0}", ex.ToString());
138 return false;
139 }
140 }
141 #endregion
142 }
143 }

  ViewVC Help
Powered by ViewVC 1.1.22