33using System . ComponentModel ;
44using System . Data ;
55using System . Diagnostics ;
6+ using System . Drawing ;
67using System . IO ;
78using System . Linq ;
89using System . Net ;
910using System . Text . RegularExpressions ;
11+ using System . Threading . Tasks ;
1012using System . Windows . Forms ;
1113using YoutubeExtractor ;
1214
@@ -24,7 +26,6 @@ public Main()
2426 string name ;
2527 string savePath ;
2628 VideoInfo video ;
27- WebClient webClient ; // Our WebClient that will be doing the downloading for us
2829 Stopwatch sw = new Stopwatch ( ) ; // The stopwatch which we will be using to calculate the download speed
2930
3031 List < DownloadHelper . downloadFile > ldf = new List < DownloadHelper . downloadFile > ( ) ;
@@ -68,7 +69,6 @@ private void btndownload_Click(object sender, EventArgs e)
6869 ext = ".mp3" ;
6970 else
7071 ext = ".Mp4" ;
71- bool found = true ;
7272
7373 ListViewItem item = list_Items . FindItemWithText ( name + " " + ( video . Resolution == 0 ? ext . Replace ( "." , "" ) : video . Resolution . ToString ( ) ) ) ;
7474
@@ -101,7 +101,10 @@ private void btndownload_Click(object sender, EventArgs e)
101101 ldf . Add ( d ) ;
102102
103103 Action < int , int , object > act1 = new Action < int , int , object > ( delegate ( int idx , int sidx , object obj )
104- { list_Items . Invoke ( new Action ( ( ) => list_Items . Items [ idx ] . SubItems [ sidx ] . Text = obj . ToString ( ) ) ) ; } ) ;
104+ {
105+ list_Items . Invoke ( new Action ( ( ) => list_Items . Items [ idx ] . SubItems [ sidx ] . Text = obj . ToString ( ) ) ) ;
106+
107+ } ) ;
105108
106109 d . eSize += ( object s1 , string size ) => act1 . Invoke ( indx , 1 , size ) ;
107110 d . eDownloadedSize += ( object s1 , string size ) => act1 . Invoke ( indx , 2 , size ) ;
@@ -129,6 +132,9 @@ private void btndownload_Click(object sender, EventArgs e)
129132 Action < int , int , object > act1 = new Action < int , int , object > ( delegate ( int idx , int sidx , object obj )
130133 {
131134 list_Items . Invoke ( new Action ( ( ) => list_Items . Items [ idx ] . SubItems [ sidx ] . Text = obj . ToString ( ) ) ) ;
135+
136+
137+
132138 } ) ;
133139
134140 d . eSize += ( object s1 , string size ) => act1 . Invoke ( indx , 1 , size ) ;
@@ -148,6 +154,9 @@ private void btndownload_Click(object sender, EventArgs e)
148154
149155 }
150156
157+
158+
159+
151160 /*
152161 public void DownloadFile(string urlAddress, string location)
153162 {
@@ -217,9 +226,10 @@ private void Completed(object sender, AsyncCompletedEventArgs e)
217226 private void Main_Load ( object sender , EventArgs e )
218227 {
219228#if DEBUG
220- txtlink . Text = "https://www.youtube.com/watch?v=sTAIvHEvd48 " ;
229+ txtlink . Text = "https://www.youtube.com/watch?v=xsXectQvo6o " ;
221230#endif
222- backgroundWorker1 . RunWorkerAsync ( ) ;
231+ Activate ( ) ;
232+ backgroundWorker1 . RunWorkerAsync ( list_Items ) ;
223233 Text = Application . ProductName + " " + Application . ProductVersion ;
224234 btndownload . Enabled = false ;
225235 DirectoryInfo s = new DirectoryInfo ( savePath = lblpath . Text = Environment . GetFolderPath ( Environment . SpecialFolder . MyVideos ) ) ;
@@ -365,7 +375,7 @@ private void btnPause_Click(object sender, EventArgs e)
365375 ldf [ indx ] . ResumeDownload ( ) ;
366376 btnPause . Text = "Pause" ;
367377 }
368- btnPause . Enabled = false ;
378+
369379 btndownload . Enabled = true ;
370380 }
371381
@@ -385,6 +395,7 @@ private void list_Items_SelectedIndexChanged(object sender, EventArgs e)
385395 btnPause . Text = "Pause" ;
386396
387397 }
398+
388399 else if ( list_Items . Items [ indx ] . SubItems [ 4 ] . Text == "Paused" )
389400 {
390401 btnPause . Enabled = true ;
@@ -396,6 +407,13 @@ private void list_Items_SelectedIndexChanged(object sender, EventArgs e)
396407 btnPause . Enabled = false ;
397408 btnPause . Text = "Pause/Resume" ;
398409 }
410+
411+ if ( list_Items . SelectedItems [ 0 ] . SubItems [ 4 ] . Text == "Completed" )
412+ btncle . Enabled = true ;
413+ else if ( list_Items . SelectedItems [ 0 ] . SubItems [ 4 ] . Text == "Paused" )
414+ btncle . Enabled = true ;
415+ else
416+ btncle . Enabled = false ;
399417 }
400418 else
401419 {
@@ -409,13 +427,55 @@ private void button2_Click(object sender, EventArgs e)
409427 txtlink . Clear ( ) ;
410428 }
411429
430+ bool done = false ;
431+ ListView lst ;
432+
412433 private void backgroundWorker1_DoWork ( object sender , DoWorkEventArgs e )
413434 {
414- if ( list_Items . Items . Count > 0 )
415- foreach ( ListViewItem l in list_Items . Items )
435+ try
436+ {
437+ BeginInvoke ( ( MethodInvoker ) delegate
416438 {
417- list_Items . Invoke ( new Action ( ( ) => list_Items . Items [ l . Index ] . SubItems [ 4 ] . BackColor = System . Drawing . Color . Green ) ) ;
418- }
439+
440+ if ( list_Items . Items . Count > 0 )
441+ foreach ( ListViewItem items in list_Items . Items )
442+ {
443+ done = items . SubItems [ 4 ] . Text == "Completed" ;
444+ Invoke ( new Action ( ( ) => items . BackColor = done ? Color . Lime : System . Drawing . SystemColors . Window ) ) ;
445+
446+ }
447+
448+ } ) ;
449+ }
450+ catch ( Exception wx )
451+ {
452+ MessageBox . Show ( wx . Message + "\n " + wx . StackTrace ) ;
453+ }
454+ }
455+
456+ private void button3_Click ( object sender , EventArgs e )
457+ {
458+ var ext = video . Resolution . ToString ( ) ;
459+ if ( ext == "0" )
460+ ext = ".mp3" ;
461+ else
462+ ext = ".Mp4" ;
463+
464+ if ( list_Items . SelectedItems [ 0 ] . SubItems [ 4 ] . Text == "Completed" )
465+ list_Items . SelectedItems [ 0 ] . Remove ( ) ;
466+
467+ else if ( list_Items . SelectedItems [ 0 ] . SubItems [ 4 ] . Text == "Paused" )
468+ {
469+ ldf . RemoveAll ( a => a . DownloadState == "Paused" ) ;
470+ File . Delete ( Path . Combine ( savePath , name + " " + ( video . Resolution == 0 ? ext . Replace ( "." , "" ) : video . Resolution . ToString ( ) ) + ext ) ) ;
471+ list_Items . SelectedItems [ 0 ] . Remove ( ) ;
472+ }
473+
474+ }
475+
476+ private void backgroundWorker1_RunWorkerCompleted ( object sender , RunWorkerCompletedEventArgs e )
477+ {
478+ backgroundWorker1 . RunWorkerAsync ( ) ;
419479 }
420480 }
421481}
0 commit comments