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.Windows.Forms;
\r
11 using System.Threading;
\r
12 using System.Diagnostics;
\r
16 public partial class frmDownload : Form
\r
18 private readonly Thread downloadThread;
\r
19 private Stream responceStream;
\r
20 private Stream loacalStream;
\r
21 private HttpWebRequest webRequest;
\r
22 private HttpWebResponse webResponse;
\r
23 private static int progress;
\r
24 private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
\r
25 private delegate void DownloadCompleteCallback();
\r
26 private delegate void DownloadFailedCallback();
\r
28 private string file;
\r
30 public frmDownload(string filename)
\r
32 InitializeComponent();
\r
35 downloadThread = new Thread(Download);
\r
36 downloadThread.Start();
\r
39 private void Download()
\r
41 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
42 string hbUpdate = file;
\r
43 WebClient wcDownload = new WebClient();
\r
47 if (File.Exists(tempPath))
\r
48 File.Delete(tempPath);
\r
50 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
\r
51 webRequest.Credentials = CredentialCache.DefaultCredentials;
\r
52 webResponse = (HttpWebResponse)webRequest.GetResponse();
\r
53 Int64 fileSize = webResponse.ContentLength;
\r
55 responceStream = wcDownload.OpenRead(hbUpdate);
\r
56 loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
\r
59 byte[] downBuffer = new byte[2048];
\r
62 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
\r
64 loacalStream.Write(downBuffer, 0, bytesSize);
\r
65 flength = loacalStream.Length;
\r
66 Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });
\r
69 responceStream.Close();
\r
70 loacalStream.Close();
\r
72 if (flength != fileSize)
\r
73 Invoke(new DownloadFailedCallback(this.downloadFailed));
\r
75 Invoke(new DownloadCompleteCallback(this.downloadComplete));
\r
83 private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
\r
87 long p = (BytesRead * 100) / TotalBytes;
\r
88 progress = int.Parse(p.ToString());
\r
89 progress_download.Value = progress;
\r
90 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";
\r
92 catch (Exception exc)
\r
94 MessageBox.Show("Integer Conversion Error On Download \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
98 private void downloadComplete()
\r
100 lblProgress.Text = "Download Complete";
\r
101 btn_cancel.Text = "Close";
\r
103 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
104 Process.Start(tempPath);
\r
106 Application.Exit();
\r
109 private void downloadFailed()
\r
111 lblProgress.Text = "Download Failed";
\r
112 btn_cancel.Text = "Close";
\r
115 private void btn_cancel_Click(object sender, EventArgs e)
\r
119 webResponse.Close();
\r
120 responceStream.Close();
\r
121 loacalStream.Close();
\r
122 downloadThread.Abort();
\r
123 progress_download.Value = 0;
\r
124 lblProgress.Text = "Download Stopped";
\r