-
Notifications
You must be signed in to change notification settings - Fork 3
/
Form1.cs
814 lines (709 loc) · 31.3 KB
/
Form1.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
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Net;
using System.Runtime.InteropServices;
using System.Runtime.InteropServices.ComTypes;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
using AngleSharp.Dom;
using AngleSharp.Html.Dom;
using AngleSharp.Html.Parser;
using Newtonsoft.Json;
//using TagLib;
//using TagLib.Mpeg;
//using TagLib.Id3v2;
using File = System.IO.File;
using Version = System.Version;
using AngleSharp.Io;
using System.Web;
using System.Xml.Linq;
using static System.Windows.Forms.LinkLabel;
using System.Security.Policy;
using System.Text.RegularExpressions;
// ReSharper disable ConvertToUsingDeclaration
namespace KhinsiderDownloader
{
public partial class Form1 : Form
{
public static string programName = "KhinsiderDownloader";
class UpdateTagResult
{
// ReSharper disable once InconsistentNaming
public string tag_name { get; set; }
}
private void checkUpdates()
{
try
{
WebClient cl = new WebClient();
cl.Headers.Add("User-Agent", "KhinsiderDownloader");
cl.Headers.Add("Content-Type", "application/json");
string lastVersion =
cl.DownloadString(new Uri("https://api.github.com/repos/weespin/KhinsiderDownloader/releases/latest"));
var result = JsonConvert.DeserializeObject<UpdateTagResult>(lastVersion);
Version newVersion = new Version(result.tag_name);
Version currentVersion = new Version(Application.ProductVersion);
if (newVersion > currentVersion)
{
var promptResult = MessageBox.Show(
"Download the latest version of KhinsiderDownloader!\nClick OK to open the download page",
"A new update has been released!", MessageBoxButtons.OKCancel);
if (promptResult == DialogResult.OK)
{
Process.Start("https://github.com/weespin/KhinsiderDownloader/releases");
}
}
else if (newVersion < currentVersion)
{
if (label1.InvokeRequired)
{
label1.Invoke(new Action(() => { label1.Text = "DEV " + label1.Text; }));
}
}
}
catch (Exception e)
{
Log($"Failed to check for updates!");
}
}
public Form1()
{
InitializeComponent();
Program.MainForm = this;
if (!LoadConfig())
{
Downloader.g_songsParralelOptions.MaxDegreeOfParallelism = (int)num_threads.Value;
Downloader.g_albumsParralelOptions.MaxDegreeOfParallelism = 1;
}
lbl_path.Text = Downloader.m_szDownloadPath;
Task.Run(() => { checkUpdates(); });
Log($"KhinsiderDownloader - {new Version(Application.ProductVersion)}\r\nIf you encounter any problems, crashes, or have suggestions, please share your feedback on GitHub: https://github.com/weespin/KhinsiderDownloader/issues");
}
public void Log(string textIn)
{
var LogAction = new Action(() =>
{
txt_log.Text += $"{textIn}\r\n";
txt_log.SelectionStart = txt_log.Text.Length;
txt_log.ScrollToCaret();
});
if (txt_log.InvokeRequired)
{
//Called from a different thread
txt_log.Invoke(LogAction);
}
else
{
LogAction();
}
}
public bool LoadConfig()
{
if (!File.Exists("khinsiderdl.config"))
{
//Probably a first launch
return false;
}
try
{
var configLines = File.ReadAllLines("khinsiderdl.config");
lbl_path.Text = Downloader.m_szDownloadPath = configLines[0];
Downloader.eQuality = bool.Parse(configLines[1])
? Downloader.EDownloadQuality.QUALITY_MP3_ONLY
: Downloader.EDownloadQuality.QUALITY_BEST_ONLY;
num_threads.Value = Downloader.g_songsParralelOptions.MaxDegreeOfParallelism = Int32.Parse(configLines[2]);
num_album_threads.Value = Downloader.g_albumsParralelOptions.MaxDegreeOfParallelism = Int32.Parse(configLines[3]);
chk_suppress_downloading_logs.Checked = Downloader.m_bSuppessLogs = bool.Parse(configLines[4]);
chk_download_art.Checked = Downloader.m_bDownloadArt = bool.Parse(configLines[5]);
chk_skipdownloaded.Checked = Downloader.m_bSkipDownloaded = bool.Parse(configLines[6]);
//Downloader.m_bArtFix = bool.Parse(configLines[7]);
if (Downloader.eQuality == Downloader.EDownloadQuality.QUALITY_MP3_ONLY)
{
radio_mp3only.Checked = true;
}
else
{
radio_betterquality.Checked = true;
}
return true;
}
catch (Exception)
{
Program.MainForm.Log("Failed to load a config!");
}
return false;
}
void SaveConfig()
{
string[] configLines = new string[7];
configLines[0] = Downloader.m_szDownloadPath;
configLines[1] = (Downloader.eQuality == Downloader.EDownloadQuality.QUALITY_MP3_ONLY).ToString();
configLines[2] = Downloader.g_songsParralelOptions.MaxDegreeOfParallelism.ToString();
configLines[3] = Downloader.g_albumsParralelOptions.MaxDegreeOfParallelism.ToString();
configLines[4] = Downloader.m_bSuppessLogs.ToString();
configLines[5] = Downloader.m_bDownloadArt.ToString();
configLines[6] = Downloader.m_bSkipDownloaded.ToString();
//configLines[7] = Downloader.m_bArtFix.ToString();
File.WriteAllLines("khinsiderdl.config", configLines);
}
void ToggleControls(bool value)
{
btn_download.Text = value ? "Download" : "Cancel";
radio_betterquality.Enabled = value;
radio_mp3only.Enabled = value;
btn_selectpath.Enabled = value;
num_album_threads.Enabled = value;
num_threads.Enabled = value;
chk_download_art.Enabled = value;
chk_skipdownloaded.Enabled = value;
Downloader.IsDownloading = !value;
}
private void btn_download_Click(object sender, EventArgs e)
{
if (Downloader.IsDownloading)
{
ToggleControls(true);
Downloader.cancelTokenSource.Cancel();
return;
}
List<string> urls = txt_urllist.Text
.Split(new string[] { Environment.NewLine }, StringSplitOptions.RemoveEmptyEntries).Distinct().ToList();
if(urls.Count == 0)
{
Log("No URLs provided!");
return;
}
ToggleControls(false);
Task.Run(() => { Downloader.DownloadAlbums(urls); }).ContinueWith((task =>
{
if (btn_download.InvokeRequired)
{
btn_download.Invoke(new Action(() =>
{
ToggleControls(true);
TaskbarProgress.SetState(this.Handle, TaskbarProgress.TaskbarStates.NoProgress);
Downloader.cancelTokenSource = new CancellationTokenSource();
}));
}
}));
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
Downloader.eQuality = radio_mp3only.Checked
? Downloader.EDownloadQuality.QUALITY_MP3_ONLY
: Downloader.EDownloadQuality.QUALITY_BEST_ONLY;
SaveConfig();
}
private void btn_DownloadPath_Click(object sender, EventArgs e)
{
using (var browserDialog = new FolderBrowserDialog())
{
DialogResult result = browserDialog.ShowDialog();
if (result == DialogResult.OK && !string.IsNullOrWhiteSpace(browserDialog.SelectedPath))
{
lbl_path.Text = Downloader.m_szDownloadPath = browserDialog.SelectedPath;
SaveConfig();
}
}
}
private void label1_DoubleClick(object sender, EventArgs e)
{
Process.Start("https://github.com/weespin");
}
private void btn_opensearch_Click(object sender, EventArgs e)
{
SearchForm searchForm = new SearchForm() {linkbox = txt_urllist};
searchForm.Show();
}
private void num_threads_ValueChanged(object sender, EventArgs e)
{
Downloader.g_songsParralelOptions.MaxDegreeOfParallelism = (int)num_threads.Value;
SaveConfig();
}
private void num_album_threads_ValueChanged(object sender, EventArgs e)
{
Downloader.g_albumsParralelOptions.MaxDegreeOfParallelism = (int)num_album_threads.Value;
SaveConfig();
}
private void chk_suppress_downloading_logs_CheckedChanged(object sender, EventArgs e)
{
Downloader.m_bSuppessLogs = chk_suppress_downloading_logs.Checked;
SaveConfig();
}
private void chk_set_art_as_icon_CheckedChanged(object sender, EventArgs e)
{
//Downloader.m_bArtFix = chk_fix_picture.Checked;
}
private void chk_download_art_CheckedChanged(object sender, EventArgs e)
{
Downloader.m_bDownloadArt = chk_download_art.Checked;
SaveConfig();
}
private void chk_skipdownloaded_CheckedChanged(object sender, EventArgs e)
{
Downloader.m_bSkipDownloaded = chk_skipdownloaded.Checked;
SaveConfig();
}
private void lbl_path_Click(object sender, EventArgs e)
{
Downloader.CheckAndCreateDownloadFolder();
Process.Start(Downloader.m_szDownloadPath);
}
}
static class Downloader
{
public static CancellationTokenSource cancelTokenSource = new CancellationTokenSource();
public static bool IsDownloading = false;
//ParallelOptions for songs in album
public static ParallelOptions g_songsParralelOptions = new ParallelOptions()
{ MaxDegreeOfParallelism = Environment.ProcessorCount };
//ParallelOptions for albums
public static ParallelOptions g_albumsParralelOptions = new ParallelOptions()
{ MaxDegreeOfParallelism = Environment.ProcessorCount };
public static int nTotalAlbums = 0;
public static int nAlbumsDownloaded = 0;
public static void CheckAndCreateDownloadFolder()
{
if (!Directory.Exists(Downloader.m_szDownloadPath))
{
Directory.CreateDirectory(Downloader.m_szDownloadPath);
}
}
static void UpdateTitle(int value, int max)
{
Program.MainForm.Invoke(new Action(() =>
{
Program.MainForm.Text = $"{Form1.programName} ({value}/{max})";
TaskbarProgress.SetValue(Program.MainForm.Handle, value, max);
}));
}
static void ResetTitle()
{
Program.MainForm.Invoke(new Action(() => { Program.MainForm.Text = $"{Form1.programName}"; }));
}
public enum EDownloadQuality : byte
{
QUALITY_MP3_ONLY,
QUALITY_BEST_ONLY
}
public static EDownloadQuality eQuality = EDownloadQuality.QUALITY_MP3_ONLY;
public static string m_szDownloadPath = Directory.GetCurrentDirectory() + "\\Downloads\\";
public static bool m_bSuppessLogs;
public static bool m_bDownloadArt;
public static bool m_bSkipDownloaded;
//public static bool m_bArtFix;
public static string m_szHostName = "https://downloads.khinsider.com";
public static void DownloadAlbums(List<string> url)
{
g_albumsParralelOptions.CancellationToken = Downloader.cancelTokenSource.Token;
g_songsParralelOptions.CancellationToken = Downloader.cancelTokenSource.Token;
CheckAndCreateDownloadFolder();
var maxthreads = g_songsParralelOptions.MaxDegreeOfParallelism *
g_albumsParralelOptions.MaxDegreeOfParallelism;
ThreadPool.SetMinThreads(maxthreads, maxthreads);
System.Net.ServicePointManager.DefaultConnectionLimit = Int32.MaxValue;
nAlbumsDownloaded = 0;
nTotalAlbums = url.Count;
if (nTotalAlbums != 1)
{
UpdateTitle(nAlbumsDownloaded, nTotalAlbums);
}
try
{
Parallel.ForEach(url, g_albumsParralelOptions, DownloadAlbum);
}
catch (OperationCanceledException)
{
}
if (cancelTokenSource.IsCancellationRequested)
{
Program.MainForm.Log("Download was cancelled");
}
ResetTitle();
}
public struct HTMLResult
{
public string ResponseURI;
public string HTML;
}
public static HTMLResult GetHTMLFromURL(string sUrl)
{
HTMLResult result = new HTMLResult();
HttpWebRequest httpWebRequest = (HttpWebRequest)WebRequest.Create(sUrl);
httpWebRequest.Proxy = null;
httpWebRequest.KeepAlive = false;
httpWebRequest.Timeout = 30 * 1000; //TCP timeout
try
{
using (HttpWebResponse httpWebResponse = (HttpWebResponse)httpWebRequest.GetResponse())
{
if (httpWebResponse.StatusCode == HttpStatusCode.OK)
{
result.ResponseURI = httpWebResponse.ResponseUri.ToString();
using (Stream responseStream = httpWebResponse.GetResponseStream())
{
if (responseStream != null)
{
using (StreamReader reader = new StreamReader(responseStream))
{
result.HTML = reader.ReadToEnd();
}
}
}
}
}
}
catch (Exception e)
{
}
return result;
}
public static void DownloadAlbum(string sUrl)
{
bool bIsPlaylist = false;
string pattern = @"\/playlist\/\w+";
Regex regex = new Regex(pattern);
bIsPlaylist = regex.Match(sUrl).Success;
SynchronizedCollection<Task> currentTasks = new SynchronizedCollection<Task>();
string albumHTML = String.Empty;
try
{
albumHTML = GetHTMLFromURL(sUrl).HTML;
}
catch (Exception e)
{
string errorMessage = $"Failed to open {sUrl} ({e.Message})";
Program.MainForm.Log(errorMessage);
#if DEBUG
Debug.WriteLine(errorMessage);
#endif
return;
}
var parser = new HtmlParser();
var albumHtmlDocument = parser.ParseDocument(albumHTML);
Program.MainForm.Log($"\r\nParsing {sUrl}");
var songNodes = albumHtmlDocument.All.Where(element =>
element.LocalName == "td" &&
element.ClassName ==
"playlistDownloadSong"); //.SelectNodes("//td[contains(@class, 'playlistDownloadSong')]");
var qualityNode = albumHtmlDocument.All.FirstOrDefault(element =>
element.LocalName == "tr" &&
element.Id ==
"songlist_header"); //DocumentNode.SelectNodes("//tr[contains(@id, 'songlist_header')]/th");
var albumNameNode = albumHtmlDocument.All
.FirstOrDefault(element => element.Id == "pageContent")
?.Children[1]; //DocumentNode.SelectSingleNode("//*[@id=\"EchoTopic\"]/h2[1]");
var albumImageNodes = albumHtmlDocument.All.Where(element =>
element.LocalName == "div" &&
element.ClassName ==
"albumImage"); //.SelectNodes("//td[contains(@class, 'playlistDownloadSong')]");
string szAlbumName;
if (albumNameNode != null && songNodes.Any() && qualityNode != null)
{
//Trim spaces and dots!
szAlbumName = string
.Join("_", WebUtility.HtmlDecode(albumNameNode.Text()).Split(Path.GetInvalidFileNameChars()))
.Trim(new[] { ' ', '.' });
//'info' hack. Remove the last words from inner child
if (albumNameNode.Children != null && albumNameNode.Children.Length > 0 &&
szAlbumName.Length >= albumNameNode.Children[0].Text().Length)
{
szAlbumName = szAlbumName.Substring(0, szAlbumName.Length - albumNameNode.Children[0].Text().Length);
}
}
else
{
Program.MainForm.Log($"Failed to parse {sUrl}");
return;
}
List<string> albumImageLinks = albumImageNodes
.Where(albumImageNode => albumImageNode.ChildElementCount > 0)
.Select(albumImageNode => albumImageNode.Children[0].GetAttribute("href"))
.ToList();
//byte[] ArtFix = null;
//string ArtFixPath = "";
//if (m_bArtFix)
//{
// //find and download best image
// string bestLink = albumImageLinks
// .FirstOrDefault(link =>
// {
// string fileName = link.Split('/').Last();
// string fileExtension = fileName.Split('.').Last();
// return fileExtension.ToLower().Contains("cover");
// }) ?? albumImageLinks.FirstOrDefault();
// if (bestLink != null)
// {
// WebClient downloadClient = new WebClient() { Proxy = null };
// ArtFix = downloadClient.DownloadData(bestLink);
// ArtFixPath = bestLink;
// }
//}
if (m_bDownloadArt)
{
if (albumImageLinks.Count != 0)
{
Directory.CreateDirectory(m_szDownloadPath + "\\" + szAlbumName + "\\art\\");
foreach (var albumImageLink in albumImageLinks)
{
WebClient downloadClient = new WebClient() { Proxy = null };
string filename = albumImageLink.Split('/').Last();
var shh = string
.Join("_", Uri.UnescapeDataString(filename).Split(Path.GetInvalidFileNameChars()))
.Trim(new[] { ' ', '.' });
Task currentTask = downloadClient.DownloadFileTaskAsync(new Uri(albumImageLink), m_szDownloadPath + "\\" + szAlbumName + "\\art\\" + shh);
currentTask.ContinueWith(
task => { downloadClient.Dispose(); });
currentTasks.Add(currentTask);
}
}
}
Directory.CreateDirectory(Downloader.m_szDownloadPath + "\\" + szAlbumName);
var selectedFormat = ".mp3";
if (eQuality != EDownloadQuality.QUALITY_MP3_ONLY)
{
//List<IElement> qualitySubNodes = qualityNodes.Skip(3).ToList();
//find non mp3 file
bool bBreak = false;
foreach (var cell in qualityNode.Children.ToList())
{
foreach (var cellchilds in cell.Children.ToList())
{
if (cellchilds.InnerHtml == "FLAC")
{
selectedFormat = ".flac";
bBreak = true;
break;
}
if (cellchilds.InnerHtml == "OGG")
{
selectedFormat = ".ogg";
bBreak = true;
break;
}
if (cellchilds.InnerHtml == "M4A")
{
selectedFormat = ".m4a";
bBreak = true;
break;
}
}
if (bBreak)
{
break;
}
}
}
int nTotalSongs = songNodes.Count();
int nCurrentSong = 0;
Action IncrementTitle = () =>
{
++nCurrentSong;
if (nTotalAlbums == 1)
{
UpdateTitle(nCurrentSong, nTotalSongs);
}
};
try
{
Parallel.ForEach(songNodes, g_songsParralelOptions, song =>
{
var songPageURL =
m_szHostName + song.Children[0].GetAttribute("href"); //["href"].Value;
IHtmlDocument songPageDocument;
try
{
if (g_songsParralelOptions.MaxDegreeOfParallelism == 1)
{
songPageDocument = new HtmlParser().ParseDocument(GetHTMLFromURL(songPageURL).HTML);
}
else
{
songPageDocument = parser.ParseDocument(GetHTMLFromURL(songPageURL).HTML);
}
}
catch (Exception e)
{
string message = $"Failed to parse {songPageURL} ({e.Message})";
Program.MainForm.Log(message);
#if DEBUG
Debug.WriteLine(message);
#endif
return;
}
var downloadLinkNodes =
songPageDocument.All.Where(element => element.ClassName == "songDownloadLink")
.ToList(); //"//span[@class='songDownloadLink']"); //[1].ParentElement.GetAttribute("href");
//Do not use Parallel.ForEach as we usually have ~2 nodes
int nDownloadNodes = downloadLinkNodes.Count();
for (var index = 0; index < nDownloadNodes; index++)
{
var dlsongentry = downloadLinkNodes[index];
var songFileURL = dlsongentry.ParentElement.GetAttribute("href"); //.Value;
if (bIsPlaylist)
{
if (songFileURL.EndsWith(".mp3") && eQuality != EDownloadQuality.QUALITY_MP3_ONLY && nDownloadNodes > 1)
{
continue;
}
}
if (songFileURL.EndsWith(selectedFormat) || bIsPlaylist)
{
var name = WebUtility.UrlDecode(
songFileURL.Substring(songFileURL.LastIndexOf("/", StringComparison.Ordinal) + 1));
if (!IsDownloading)
{
return;
}
string filename = m_szDownloadPath + "\\" + szAlbumName + "\\" +
string.Join("_", name.Split(Path.GetInvalidFileNameChars()));
if (Downloader.m_bSkipDownloaded && File.Exists(filename))
{
IncrementTitle();
if (!m_bSuppessLogs)
Program.MainForm.Log($"{name} has been skipped as it already exists.");
return;
}
if (!m_bSuppessLogs)
{
Program.MainForm.Log($"Downloading {name}...");
}
try
{
WebClient downloadClient = new WebClient() {Proxy = null};
cancelTokenSource.Token.Register(downloadClient.CancelAsync);
Task currentTask = downloadClient.DownloadFileTaskAsync(new Uri(songFileURL), filename);
currentTask.ContinueWith(
task =>
{
downloadClient.Dispose();
if (!IsDownloading)
{
File.Delete(filename);
return;
}
//if (m_bArtFix)
//{
// var file = AudioFile.Create(filename);
// if (file.Tag.Pictures.Length > 0)
// {
// if (file.Tag.Pictures.All(n => n.Type != PictureType.FrontCover))
// {
// file.Tag.Pictures[0].Type = PictureType.FrontCover;
// }
// }
// else
// {
// file.Tag.Pictures = new IPicture[]
// {
// new Picture(new ByteVector(ArtFix))
// {
// Type = PictureType.FrontCover,
// Description = "Cover",
// MimeType = MimeMapping.GetMimeMapping(ArtFixPath)
// }
// };
// }
// file.Save();
//}
if (!m_bSuppessLogs && !cancelTokenSource.Token.IsCancellationRequested)
{
Program.MainForm.Log($"{name} has been downloaded!");
}
IncrementTitle();
}, TaskContinuationOptions.ExecuteSynchronously);
currentTasks.Add(currentTask);
}
catch (Exception e)
{
string errorMessage = $"Failed to download {songFileURL} to {filename}\r\n({e.Message})";
Program.MainForm.Log(errorMessage);
#if DEBUG
Debug.WriteLine(errorMessage);
#endif
try
{
if (File.Exists(filename))
{
File.Delete(filename);
}
}
catch (Exception removeexc)
{
Program.MainForm.Log($"Failed to remove semi-downloaded {filename}\r\n({removeexc.Message})");
}
}
}
}
});
Task.WaitAll(currentTasks.ToArray(), cancelTokenSource.Token);
}
catch (OperationCanceledException)
{
}
Program.MainForm.Log($"Finished downloading {szAlbumName}!");
if (nTotalAlbums != 1)
{
UpdateTitle(++nAlbumsDownloaded, nTotalAlbums);
}
}
}
//https://stackoverflow.com/a/24187171
public static class TaskbarProgress
{
public enum TaskbarStates
{
NoProgress = 0,
Indeterminate = 0x1,
Normal = 0x2,
Error = 0x4,
Paused = 0x8
}
[ComImport()]
[Guid("ea1afb91-9e28-4b86-90e9-9e9f8a5eefaf")]
[InterfaceType(ComInterfaceType.InterfaceIsIUnknown)]
private interface ITaskbarList3
{
// ITaskbarList
[PreserveSig]
void HrInit();
[PreserveSig]
void AddTab(IntPtr hwnd);
[PreserveSig]
void DeleteTab(IntPtr hwnd);
[PreserveSig]
void ActivateTab(IntPtr hwnd);
[PreserveSig]
void SetActiveAlt(IntPtr hwnd);
// ITaskbarList2
[PreserveSig]
void MarkFullscreenWindow(IntPtr hwnd, [MarshalAs(UnmanagedType.Bool)] bool fFullscreen);
// ITaskbarList3
[PreserveSig]
void SetProgressValue(IntPtr hwnd, UInt64 ullCompleted, UInt64 ullTotal);
[PreserveSig]
void SetProgressState(IntPtr hwnd, TaskbarStates state);
}
[ComImport()]
[Guid("56fdf344-fd6d-11d0-958a-006097c9a090")]
[ClassInterface(ClassInterfaceType.None)]
private class TaskbarInstance
{
}
private static readonly ITaskbarList3 taskbarInstance = (ITaskbarList3)new TaskbarInstance();
private static readonly bool taskbarSupported = Environment.OSVersion.Version >= new Version(6, 1);
public static void SetState(IntPtr windowHandle, TaskbarStates taskbarState)
{
if (taskbarSupported) taskbarInstance.SetProgressState(windowHandle, taskbarState);
}
public static void SetValue(IntPtr windowHandle, double progressValue, double progressMax)
{
if (taskbarSupported)
taskbarInstance.SetProgressValue(windowHandle, (ulong)progressValue, (ulong)progressMax);
}
}
}