| 1 |
using System; |
| 2 |
using System.Collections.Generic; |
| 3 |
using System.Linq; |
| 4 |
using System.Text; |
| 5 |
using System.IO; |
| 6 |
using System.Xml; |
| 7 |
using System.Reflection; |
| 8 |
using System.Diagnostics; |
| 9 |
using SchoolIdolFestivalSimulator.Components; |
| 10 |
namespace SchoolIdolFestivalSimulator.Main { |
| 11 |
public class RssItemCollection:IEnumerable<RssItem>,IDisposable { |
| 12 |
public delegate void CompletedHandler(object sender, System.ComponentModel.AsyncCompletedEventArgs e); |
| 13 |
public event CompletedHandler Completed = delegate { |
| 14 |
}; |
| 15 |
|
| 16 |
private List<RssItem> _list = new List<RssItem>(); |
| 17 |
private readonly string FileName = "rss.xml"; |
| 18 |
|
| 19 |
private System.Net.WebClient _downloadClient = null; |
| 20 |
private string GetMainAppPath() { |
| 21 |
return System.Reflection.Assembly.GetExecutingAssembly().Location; |
| 22 |
} |
| 23 |
public string GetCurrentMainVersion() { |
| 24 |
FileVersionInfo vi = FileVersionInfo.GetVersionInfo(@GetMainAppPath()); |
| 25 |
return vi.FileVersion; |
| 26 |
} |
| 27 |
public RssItem GetLatestVersion() { |
| 28 |
return _list[0]; |
| 29 |
} |
| 30 |
public bool IsLatestVersion() { |
| 31 |
RssItem item = GetLatestVersion(); |
| 32 |
string[] v = GetCurrentMainVersion().Split('.'); |
| 33 |
try { |
| 34 |
int vMajor = Convert.ToInt32(v[0]); |
| 35 |
int vMinor = Convert.ToInt32(v[1]); |
| 36 |
int vRelease = Convert.ToInt32(v[2]); |
| 37 |
int vBuild = Convert.ToInt32(v[3]); |
| 38 |
if (item.VerMajor > vMajor) { |
| 39 |
return false; |
| 40 |
} else if (item.VerMajor < vMajor) { |
| 41 |
return true; |
| 42 |
} |
| 43 |
if (item.VerMinor > vMinor) { |
| 44 |
return false; |
| 45 |
} else if (item.VerMinor < vMinor) { |
| 46 |
return true; |
| 47 |
} |
| 48 |
if (item.VerRelease > vRelease) { |
| 49 |
return false; |
| 50 |
} else if (item.VerRelease < vRelease) { |
| 51 |
return true; |
| 52 |
} |
| 53 |
if (item.VerBuild > vBuild) { |
| 54 |
return false; |
| 55 |
} else if (item.VerBuild < vBuild) { |
| 56 |
return true; |
| 57 |
} |
| 58 |
return true; |
| 59 |
} catch { |
| 60 |
return true; |
| 61 |
} |
| 62 |
} |
| 63 |
|
| 64 |
private void LoadXml() { |
| 65 |
if(!File.Exists(this.XmlPath)){ |
| 66 |
return; |
| 67 |
} |
| 68 |
|
| 69 |
using (XmlTextReader xml = new XmlTextReader(this.XmlPath)) { |
| 70 |
RssItem item = new RssItem(); |
| 71 |
string s = string.Empty; |
| 72 |
_list.Clear(); |
| 73 |
while (xml.Read()) { |
| 74 |
if (xml.NodeType == XmlNodeType.Element) { |
| 75 |
if (xml.Name == "item") { |
| 76 |
item = new RssItem(); |
| 77 |
s = string.Empty; |
| 78 |
} |
| 79 |
} else if (xml.NodeType == XmlNodeType.Text) { |
| 80 |
s = xml.Value; |
| 81 |
} else if (xml.NodeType == XmlNodeType.EndElement) { |
| 82 |
if (xml.Name == "pubDate") { |
| 83 |
if (item != null) { |
| 84 |
item._pubDate = s; |
| 85 |
} |
| 86 |
} else if (xml.Name == "title") { |
| 87 |
if (item != null) { |
| 88 |
item.Title = s; |
| 89 |
} |
| 90 |
} else if (xml.Name == "link") { |
| 91 |
if (item != null) { |
| 92 |
item._link = s; |
| 93 |
} |
| 94 |
} else if (xml.Name == "guid") { |
| 95 |
if (item != null) { |
| 96 |
item._guid = s; |
| 97 |
} |
| 98 |
} else if (xml.Name == "dc:creator") { |
| 99 |
if (item != null) { |
| 100 |
item._creator = s; |
| 101 |
} |
| 102 |
} else if (xml.Name == "description") { |
| 103 |
if (item != null) { |
| 104 |
item._description = s; |
| 105 |
} |
| 106 |
} else if (xml.Name == "item") { |
| 107 |
_list.Add(item); |
| 108 |
} |
| 109 |
} |
| 110 |
} |
| 111 |
xml.Close(); |
| 112 |
this.SortByVersionDesc(); |
| 113 |
} |
| 114 |
} |
| 115 |
public int Count { |
| 116 |
get { |
| 117 |
return _list.Count; |
| 118 |
} |
| 119 |
} |
| 120 |
public RssItem this[int index] { |
| 121 |
get { |
| 122 |
return _list[index]; |
| 123 |
} |
| 124 |
} |
| 125 |
public void CancelDownload() { |
| 126 |
if (_downloadClient != null) { |
| 127 |
_downloadClient.CancelAsync(); |
| 128 |
} |
| 129 |
} |
| 130 |
public void Download() { |
| 131 |
Uri u = new Uri(Consts.RssUri); |
| 132 |
if (_downloadClient == null) { |
| 133 |
_downloadClient = new System.Net.WebClient(); |
| 134 |
_downloadClient.DownloadProgressChanged += |
| 135 |
new System.Net.DownloadProgressChangedEventHandler( |
| 136 |
downloadClient_DownloadProgressChanged); |
| 137 |
_downloadClient.DownloadFileCompleted += |
| 138 |
new System.ComponentModel.AsyncCompletedEventHandler( |
| 139 |
downloadClient_DownloadFileCompleted); |
| 140 |
} |
| 141 |
//非同期ダウンロードを開始する |
| 142 |
_downloadClient.DownloadFileAsync(u, this.XmlPath); |
| 143 |
} |
| 144 |
|
| 145 |
private void downloadClient_DownloadProgressChanged(object sender, |
| 146 |
System.Net.DownloadProgressChangedEventArgs e) { |
| 147 |
} |
| 148 |
private void downloadClient_DownloadFileCompleted(object sender, |
| 149 |
System.ComponentModel.AsyncCompletedEventArgs e) { |
| 150 |
this.LoadXml(); |
| 151 |
Completed(this, e); |
| 152 |
} |
| 153 |
public void SortByVersionDesc() { |
| 154 |
_list.Sort(CompareByVersion); |
| 155 |
_list.Reverse(); |
| 156 |
} |
| 157 |
public static int CompareByVersion(RssItem x, RssItem y) { |
| 158 |
if (x == null && y == null) { |
| 159 |
return 0; |
| 160 |
} |
| 161 |
if (x == null) { |
| 162 |
return -1; |
| 163 |
} |
| 164 |
if (y == null) { |
| 165 |
return 1; |
| 166 |
} |
| 167 |
if (x.VerMajor > y.VerMajor) { |
| 168 |
return 1; |
| 169 |
}if (x.VerMajor < y.VerMajor) { |
| 170 |
return -1; |
| 171 |
} |
| 172 |
if (x.VerMinor > y.VerMinor) { |
| 173 |
return 1; |
| 174 |
} |
| 175 |
if (x.VerMinor < y.VerMinor) { |
| 176 |
return -1; |
| 177 |
} |
| 178 |
if (x.VerRelease > y.VerRelease) { |
| 179 |
return 1; |
| 180 |
} |
| 181 |
if (x.VerRelease < y.VerRelease) { |
| 182 |
return -1; |
| 183 |
} |
| 184 |
if (x.VerBuild > y.VerBuild) { |
| 185 |
return 1; |
| 186 |
} |
| 187 |
if (x.VerBuild < y.VerBuild) { |
| 188 |
return -1; |
| 189 |
} |
| 190 |
return 0; |
| 191 |
} |
| 192 |
private string GetDirectory() { |
| 193 |
return System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().Location); |
| 194 |
} |
| 195 |
private string XmlPath { |
| 196 |
get { |
| 197 |
return System.IO.Path.Combine(GetDirectory(), FileName); |
| 198 |
} |
| 199 |
} |
| 200 |
#region IEnumerable<RssItem> メンバー |
| 201 |
|
| 202 |
public IEnumerator<RssItem> GetEnumerator() { |
| 203 |
return _list.GetEnumerator(); |
| 204 |
} |
| 205 |
#endregion |
| 206 |
#region IEnumerable メンバー |
| 207 |
System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator() { |
| 208 |
return _list.GetEnumerator(); |
| 209 |
} |
| 210 |
#endregion |
| 211 |
|
| 212 |
#region IDisposable メンバー |
| 213 |
|
| 214 |
public void Dispose() { |
| 215 |
_list.Clear(); |
| 216 |
} |
| 217 |
|
| 218 |
#endregion |
| 219 |
} |
| 220 |
} |