3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.Collections.Generic;
\r
9 using System.ComponentModel;
\r
11 using System.Drawing;
\r
13 using System.Windows.Forms;
\r
16 using System.Threading;
\r
17 using System.Diagnostics;
\r
21 public partial class frmDownload : Form
\r
23 private Thread downloadThread;
\r
24 private Stream responceStream;
\r
25 private Stream loacalStream;
\r
26 private HttpWebRequest webRequest;
\r
27 private HttpWebResponse webResponse;
\r
28 private static int progress;
\r
29 private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
\r
30 private delegate void DownloadCompleteCallback();
\r
31 private delegate void DownloadFailedCallback();
\r
34 public frmDownload()
\r
36 InitializeComponent();
\r
40 downloadThread = new Thread(Download);
\r
41 downloadThread.Start();
\r
43 catch (Exception exc)
\r
45 MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
49 private void Download()
\r
51 Functions.AppcastReader rssRead = new Functions.AppcastReader();
\r
53 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
55 if (File.Exists(tempPath))
\r
56 File.Delete(tempPath);
\r
58 string hbUpdate = rssRead.downloadFile();
\r
60 WebClient wcDownload = new WebClient();
\r
63 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
\r
64 webRequest.Credentials = CredentialCache.DefaultCredentials;
\r
65 webResponse = (HttpWebResponse)webRequest.GetResponse();
\r
66 Int64 fileSize = webResponse.ContentLength;
\r
68 responceStream = wcDownload.OpenRead(hbUpdate);
\r
69 loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
\r
72 byte[] downBuffer = new byte[2048];
\r
75 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
\r
77 loacalStream.Write(downBuffer, 0, bytesSize);
\r
78 flength = loacalStream.Length;
\r
79 this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });
\r
82 responceStream.Close();
\r
83 loacalStream.Close();
\r
85 if (flength != fileSize)
\r
86 this.Invoke(new DownloadFailedCallback(this.downloadFailed));
\r
88 this.Invoke(new DownloadCompleteCallback(this.downloadComplete));
\r
96 private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
\r
100 long p = (BytesRead * 100) / TotalBytes;
\r
101 progress = int.Parse(p.ToString());
\r
102 progress_download.Value = progress;
\r
103 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";
\r
105 catch (Exception exc)
\r
107 MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
111 private void downloadComplete()
\r
113 lblProgress.Text = "Download Complete";
\r
114 btn_cancel.Text = "Close";
\r
116 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
118 Process startInstall = Process.Start(tempPath);
\r
120 Application.Exit();
\r
123 private void downloadFailed()
\r
125 lblProgress.Text = "Download Failed";
\r
126 btn_cancel.Text = "Close";
\r
129 private void btn_cancel_Click(object sender, EventArgs e)
\r
133 webResponse.Close();
\r
134 responceStream.Close();
\r
135 loacalStream.Close();
\r
136 downloadThread.Abort();
\r
137 progress_download.Value = 0;
\r
138 lblProgress.Text = "Download Stopped";
\r