forked from RossMetacraft/WorldOfAiDownloader
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MainForm.cs
442 lines (408 loc) · 16.7 KB
/
MainForm.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
using System;
using System.Collections.Generic;
using System.Collections.Specialized;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;
using HtmlAgilityPack;
namespace Metacraft.FlightSimulation.WoaiDownloader
{
public partial class MainForm : Form
{
private const string LOCAL_PACKAGE_LIST_FILE = "packages.html";
private const string WORLD_OF_AI_PACKAGE_LIST_URL = "http://www.world-of-ai.com/allpackages.php";
private const string AVSIM_LOGIN_URL = "http://library.avsim.net/dologin.php";
private const string AVSIM_DOWNLOAD_URL_FORMAT = "http://library.avsim.net/sendfile.php?Location=AVSIM&Proto=ftp&DLID={0}";
private List<PackageGroup> mPackageGroups = new List<PackageGroup>() {
new PackageGroup("airlines", "Passenger Airlines"),
new PackageGroup("cargo", "Cargo Airlines"),
new PackageGroup("ga", "General Aviation"),
new PackageGroup("military", "Military"),
new PackageGroup("ceased", "Airlines No Longer Operating")
};
private Dictionary<string, Dictionary<string, List<PackageInfo>>> mPackages;
private WebClient mPackageListClient;
private CookieAwareWebClient mPackageDownloadClient;
private bool mDownloadInProgress;
private List<PackageInfo> mSelectedPackages;
private int mCurrentPackageIndex;
public MainForm()
{
InitializeComponent();
mPackageListClient = new WebClient();
mPackageListClient.DownloadProgressChanged += mPackageListClient_DownloadProgressChanged;
mPackageListClient.DownloadStringCompleted += mPackageListClient_DownloadStringCompleted;
mPackageDownloadClient = new CookieAwareWebClient();
mPackageDownloadClient.UploadValuesCompleted += mPackageDownloadClient_UploadValuesCompleted;
mPackageDownloadClient.DownloadStringCompleted += mPackageDownloadClient_DownloadStringCompleted;
mPackageDownloadClient.DownloadFileCompleted += mPackageDownloadClient_DownloadFileCompleted;
ddlSim.Items.Add("FS9");
ddlSim.Items.Add("FSX");
Config cfg = Config.Load();
txtAvsimUsername.Text = cfg.AvsimUsername;
txtAvsimPassword.Text = cfg.AvsimPassword;
chkSavePassword.Checked = cfg.SavePassword;
ddlSim.SelectedItem = cfg.Simulator;
txtDownloadFolder.Text = cfg.DownloadFolder;
SetControlStates();
}
private void SetControlStates()
{
if (mDownloadInProgress) {
progCurrentFile.Style = ProgressBarStyle.Marquee;
btnDownloadPackages.Text = "Cancel Download";
btnDownloadPackages.Enabled = true;
grpConfiguration.Enabled = false;
treePackages.Enabled = false;
btnRefreshPackageList.Enabled = false;
} else {
progCurrentFile.Style = ProgressBarStyle.Continuous;
btnDownloadPackages.Text = "Download Selected Packages";
btnDownloadPackages.Enabled =
!string.IsNullOrEmpty(txtAvsimUsername.Text)
&& !string.IsNullOrEmpty(txtAvsimPassword.Text)
&& (GetCheckedNodes(treePackages.Nodes).Count > 0);
grpConfiguration.Enabled = true;
treePackages.Enabled = true;
btnRefreshPackageList.Enabled = true;
}
}
private void MainForm_Shown(object sender, EventArgs e)
{
Application.DoEvents();
FetchPackageList();
}
private void btnRefreshPackageList_Click(object sender, EventArgs e)
{
FetchPackageList();
}
private void FetchPackageList()
{
AddMessage("Fetching package list ...");
if (File.Exists(LOCAL_PACKAGE_LIST_FILE)) {
try {
string html = File.ReadAllText(LOCAL_PACKAGE_LIST_FILE);
AddMessage(" done." + Environment.NewLine);
ParsePackageList(html);
if (mPackages != null) PopulateTree();
}
catch (Exception ex) {
MessageBox.Show(this, string.Format("Error loading local package file: {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
} else {
try {
btnRefreshPackageList.Hide();
progFetchPackageList.Value = 0;
progFetchPackageList.Show();
mPackageListClient.DownloadStringAsync(new Uri(WORLD_OF_AI_PACKAGE_LIST_URL));
}
catch (Exception ex) {
MessageBox.Show(this, string.Format("Error loading package list from WoAI web site: {0}", ex.Message), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
}
}
void mPackageListClient_DownloadProgressChanged(object sender, DownloadProgressChangedEventArgs e)
{
progFetchPackageList.Value = e.ProgressPercentage;
}
void mPackageListClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Cancelled) return;
if (e.Error != null) {
MessageBox.Show(this, string.Format("Error loading package list from WoAI web site: {0}{1}{2}{3}", e.Error.Message, Environment.NewLine, Environment.NewLine, e.Error.InnerException != null ? e.Error.InnerException.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
AddErrorMessage(" error." + Environment.NewLine);
return;
}
AddMessage(" done." + Environment.NewLine);
progFetchPackageList.Hide();
btnRefreshPackageList.Show();
ParsePackageList(e.Result);
if (mPackages != null) PopulateTree();
}
private void ParsePackageList(string html)
{
AddMessage("Parsing package links ...");
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
mPackages = new Dictionary<string, Dictionary<string, List<PackageInfo>>>();
doc.LoadHtml(html);
foreach (PackageGroup packageGroup in mPackageGroups) {
mPackages.Add(packageGroup.Name, new Dictionary<string, List<PackageInfo>>());
string query = string.Format("//a[@name='{0}']/following::table[1]", packageGroup.Anchor);
HtmlNode table = doc.DocumentNode.SelectSingleNode(query);
if (table == null) continue;
foreach (HtmlNode row in table.SelectNodes("tr")) {
HtmlNodeCollection cells = row.SelectNodes("td");
if (cells.Count != 6) continue;
PackageInfo pi = new PackageInfo() {
Name = cells[1].InnerText.Trim(),
Country = cells[2].InnerText.Trim()
};
if (string.IsNullOrEmpty(pi.Country)) pi.Country = "N/A";
HtmlNodeCollection links = cells[5].SelectNodes("a");
List<HtmlAgilityPack.HtmlNode> avsimLinks = new List<HtmlAgilityPack.HtmlNode>();
foreach (HtmlAgilityPack.HtmlNode link in links) {
if (link.InnerText.ToLower().Trim() == "avsim") {
avsimLinks.Add(link);
}
}
if (avsimLinks.Count == 1) {
pi.AvsimUrlFs9 = avsimLinks[0].Attributes["href"].Value;
pi.AvsimUrlFsx = pi.AvsimUrlFs9;
} else if (avsimLinks.Count == 2) {
pi.AvsimUrlFs9 = avsimLinks[0].Attributes["href"].Value;
pi.AvsimUrlFsx = avsimLinks[1].Attributes["href"].Value;
}
else {
MessageBox.Show(this, "Error finding AVSIM links for package: \"" + pi.Name + "\"", "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
if (!mPackages[packageGroup.Name].ContainsKey(pi.Country)) mPackages[packageGroup.Name].Add(pi.Country, new List<PackageInfo>());
mPackages[packageGroup.Name][pi.Country].Add(pi);
}
}
AddMessage(" done." + Environment.NewLine);
}
private void PopulateTree()
{
treePackages.Nodes.Clear();
foreach (PackageGroup packageGroup in mPackageGroups) {
TreeNode groupNode = treePackages.Nodes.Add(packageGroup.Name);
foreach (string country in mPackages[packageGroup.Name].Keys.OrderBy(x => x)) {
TreeNode countryNode = groupNode.Nodes.Add(country);
foreach (PackageInfo pi in mPackages[packageGroup.Name][country].OrderBy(x => x.Name)) {
TreeNode packageNode = countryNode.Nodes.Add(pi.Name);
packageNode.Tag = pi;
}
}
}
}
private void treePackages_AfterExpand(object sender, TreeViewEventArgs e)
{
if (e.Node.Parent != null) e.Node.Parent.Expand();
}
private void treePackages_AfterCheck(object sender, TreeViewEventArgs e)
{
treePackages.AfterCheck -= new TreeViewEventHandler(treePackages_AfterCheck);
CheckAllNodes(e.Node, e.Node.Checked);
CheckParentsWhereAllChildrenChecked(treePackages.Nodes);
treePackages.AfterCheck += new TreeViewEventHandler(treePackages_AfterCheck);
SetControlStates();
}
private List<TreeNode> GetCheckedNodes(TreeNodeCollection nodes)
{
List<TreeNode> checkedNodes = new List<TreeNode>();
foreach (TreeNode node in nodes) {
if (node.Checked) checkedNodes.Add(node);
checkedNodes.AddRange(GetCheckedNodes(node.Nodes));
}
return checkedNodes;
}
private void CheckAllNodes(TreeNode node, bool isChecked)
{
foreach (TreeNode childNode in node.Nodes) {
childNode.Checked = isChecked;
CheckAllNodes(childNode, isChecked);
}
}
private void CheckParentsWhereAllChildrenChecked(TreeNodeCollection nodes)
{
bool allChecked = true;
foreach (TreeNode node in nodes) {
CheckParentsWhereAllChildrenChecked(node.Nodes);
if (!node.Checked) allChecked = false;
}
if ((nodes.Count > 0) && (nodes[0].Parent != null)) nodes[0].Parent.Checked = allChecked;
}
private void btnBrowseDownloadFolder_Click(object sender, EventArgs e)
{
FolderBrowserDialog dlg = new FolderBrowserDialog();
dlg.Description = "Specify the folder where the package files will be saved:";
dlg.ShowNewFolderButton = true;
dlg.SelectedPath = txtDownloadFolder.Text;
DialogResult result = dlg.ShowDialog(this);
if (result == DialogResult.OK) txtDownloadFolder.Text = dlg.SelectedPath;
}
private void MainForm_FormClosing(object sender, FormClosingEventArgs e)
{
SaveConfig();
if (mPackageListClient != null) mPackageListClient.Dispose();
if (mPackageDownloadClient != null) mPackageDownloadClient.Dispose();
}
private void Credentials_TextChanged(object sender, EventArgs e)
{
SetControlStates();
}
private void btnDownloadPackages_Click(object sender, EventArgs e)
{
if (mDownloadInProgress) {
StopDownload();
} else {
StartDownload();
}
}
private bool hasWriteAccessToFolder(string folderPath)
{
try {
// Attempt to get a list of security permissions from the folder.
// This will raise an exception if the path is read only or do not have access to view the permissions.
System.Security.AccessControl.DirectorySecurity ds = Directory.GetAccessControl(folderPath);
return true;
}
catch (UnauthorizedAccessException) {
return false;
}
}
private void StartDownload()
{
if (!hasWriteAccessToFolder(txtDownloadFolder.Text)) {
MessageBox.Show(this, "You do not appear to have write access to the specified download folder.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
mDownloadInProgress = true;
SetControlStates();
mSelectedPackages = GetCheckedNodes(treePackages.Nodes).Where(x => x.Tag != null).Select(x => x.Tag as PackageInfo).ToList();
mCurrentPackageIndex = -1;
progOverall.Value = 0;
progOverall.Maximum = mSelectedPackages.Count;
DoLogin();
}
private void StopDownload()
{
if (mPackageDownloadClient.IsBusy) mPackageDownloadClient.CancelAsync();
mDownloadInProgress = false;
SetControlStates();
progOverall.Value = 0;
AddErrorMessage("Download aborted." + Environment.NewLine);
}
private void DoLogin()
{
AddMessage("Logging into AVSIM ...");
NameValueCollection form = new NameValueCollection();
form.Add("UserLogin", txtAvsimUsername.Text);
form.Add("Password", txtAvsimPassword.Text);
mPackageDownloadClient.UploadValuesAsync(new Uri(AVSIM_LOGIN_URL), "POST", form);
}
void mPackageDownloadClient_UploadValuesCompleted(object sender, UploadValuesCompletedEventArgs e)
{
if (e.Cancelled) return;
if (e.Error != null) {
AddErrorMessage(" error." + Environment.NewLine);
StopDownload();
MessageBox.Show(this, string.Format("Error logging into AVSIM: {0}{1}{2}{3}", e.Error.Message, Environment.NewLine, Environment.NewLine, e.Error.InnerException != null ? e.Error.InnerException.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
CookieCollection cookies = mPackageDownloadClient.CookieContainer.GetCookies(new Uri(AVSIM_LOGIN_URL));
bool gotAuthCookie = false;
foreach (Cookie cookie in cookies) {
cookie.Path = "/";
if (cookie.Name == "LibraryLogin") gotAuthCookie = true;
}
mPackageDownloadClient.CookieContainer.Add(cookies);
if (!gotAuthCookie) {
AddErrorMessage(" error." + Environment.NewLine);
StopDownload();
MessageBox.Show(this, "AVSIM login failed. Please check your username and password.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
AddMessage(" done." + Environment.NewLine);
DownloadNextPackage();
}
private void DownloadNextPackage()
{
mCurrentPackageIndex++;
if (mCurrentPackageIndex >= mSelectedPackages.Count) {
mDownloadInProgress = false;
SetControlStates();
MessageBox.Show(this, "Package download complete!", "Complete", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
AddMessage(string.Format("Fetching download link for {0} ...", mSelectedPackages[mCurrentPackageIndex].Name));
string downloadUri = ((string)ddlSim.SelectedItem == "FSX") ? mSelectedPackages[mCurrentPackageIndex].AvsimUrlFsx : mSelectedPackages[mCurrentPackageIndex].AvsimUrlFs9;
mPackageDownloadClient.DownloadStringAsync(new Uri(downloadUri));
}
void mPackageDownloadClient_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
{
if (e.Cancelled) return;
if (e.Error != null) {
AddErrorMessage(" error." + Environment.NewLine);
StopDownload();
MessageBox.Show(this, string.Format("Error fetching download link or FTP URL: {0}{1}{2}{3}", e.Error.Message, Environment.NewLine, Environment.NewLine, e.Error.InnerException != null ? e.Error.InnerException.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
string redirectUrl = mPackageDownloadClient.ResponseHeaders[HttpResponseHeader.Location];
AddMessage(" done." + Environment.NewLine);
if (string.IsNullOrEmpty(redirectUrl)) {
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
doc.LoadHtml(e.Result);
HtmlNode link = doc.DocumentNode.SelectSingleNode("//a[contains(@href,'download.php')]");
if (link == null) {
AddErrorMessage("Could not find download link in HTML returned from AVSIM." + Environment.NewLine);
DownloadNextPackage();
return;
}
Match match = Regex.Match(link.Attributes["href"].Value, @"DLID=(\d+)", RegexOptions.IgnoreCase);
if (!match.Success) {
AddErrorMessage("Could not find download link in HTML returned from AVSIM." + Environment.NewLine);
DownloadNextPackage();
return;
}
string downloadUrl = string.Format(AVSIM_DOWNLOAD_URL_FORMAT, match.Groups[1].Value);
AddMessage("Fetching FTP URL ...");
mPackageDownloadClient.DownloadStringAsync(new Uri(downloadUrl));
} else {
AddMessage("Downloading file ...");
Uri ftpUri = new Uri(redirectUrl);
string filename = ftpUri.Segments.Last();
mPackageDownloadClient.DownloadFileAsync(ftpUri, Path.Combine(txtDownloadFolder.Text, filename));
}
}
void mPackageDownloadClient_DownloadFileCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
{
if (e.Cancelled) return;
if (e.Error != null) {
AddErrorMessage(" error." + Environment.NewLine);
StopDownload();
MessageBox.Show(this, string.Format("Error downloading package: {0}{1}{2}{3}", e.Error.Message, Environment.NewLine, Environment.NewLine, e.Error.InnerException != null ? e.Error.InnerException.Message : ""), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
return;
}
AddMessage(" done." + Environment.NewLine);
progOverall.Value = mCurrentPackageIndex + 1;
DownloadNextPackage();
}
private void AddMessage(string message)
{
AddMessage(message, Color.Black);
}
private void AddErrorMessage(string error)
{
AddMessage(error, Color.Red);
}
private void AddMessage(string message, Color color)
{
if (rtfMessages.IsDisposed || rtfMessages.Disposing) return;
if (string.IsNullOrEmpty(message)) return;
bool scrolledToBottom = rtfMessages.IsAtMaxScroll();
rtfMessages.SelectionStart = rtfMessages.TextLength;
rtfMessages.SelectionColor = color;
rtfMessages.SelectedText = message;
rtfMessages.SelectionStart = rtfMessages.TextLength;
if (scrolledToBottom) rtfMessages.ScrollToCaret();
}
private void SaveConfig()
{
Config cfg = new Config() {
AvsimUsername = txtAvsimUsername.Text,
AvsimPassword = chkSavePassword.Checked ? txtAvsimPassword.Text : string.Empty,
SavePassword = chkSavePassword.Checked,
Simulator = ddlSim.SelectedItem.ToString(),
DownloadFolder = txtDownloadFolder.Text
};
cfg.Save();
}
}
}