2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
9 using System.Diagnostics;
\r
12 using System.Threading;
\r
13 using System.Windows.Forms;
\r
16 /// The Update Download Screen
\r
18 public partial class frmDownload : Form
\r
20 private readonly Thread downloadThread;
\r
21 private Stream responceStream;
\r
22 private Stream loacalStream;
\r
23 private HttpWebRequest webRequest;
\r
24 private HttpWebResponse webResponse;
\r
25 private static int progress;
\r
26 private bool killThread;
\r
28 private delegate void UpdateProgessCallback(long bytesRead, long totalBytes);
\r
30 private delegate void DownloadCompleteCallback();
\r
32 private delegate void DownloadFailedCallback();
\r
34 public frmDownload(string filename)
\r
36 InitializeComponent();
\r
38 downloadThread = new Thread(Download);
\r
39 downloadThread.Start(filename);
\r
42 private void Download(object file)
\r
44 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
45 string hbUpdate = (string)file;
\r
46 WebClient wcDownload = new WebClient();
\r
50 if (File.Exists(tempPath))
\r
51 File.Delete(tempPath);
\r
53 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
\r
54 webRequest.Credentials = CredentialCache.DefaultCredentials;
\r
55 webResponse = (HttpWebResponse)webRequest.GetResponse();
\r
56 long fileSize = webResponse.ContentLength;
\r
58 responceStream = wcDownload.OpenRead(hbUpdate);
\r
59 loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
\r
62 byte[] downBuffer = new byte[2048];
\r
65 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
\r
69 loacalStream.Write(downBuffer, 0, bytesSize);
\r
70 flength = loacalStream.Length;
\r
71 Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] {loacalStream.Length, fileSize});
\r
74 responceStream.Close();
\r
75 loacalStream.Close();
\r
77 if (flength != fileSize)
\r
78 Invoke(new DownloadFailedCallback(this.DownloadFailed));
\r
80 Invoke(new DownloadCompleteCallback(this.DownloadComplete));
\r
88 private void UpdateProgress(long bytesRead, long totalBytes)
\r
90 long p = (bytesRead * 100) / totalBytes;
\r
91 int.TryParse(p.ToString(), out progress);
\r
92 progress_download.Value = progress;
\r
93 lblProgress.Text = (bytesRead / 1024) + "k of " + (totalBytes / 1024) + "k ";
\r
96 private void DownloadComplete()
\r
98 lblProgress.Text = "Download Complete";
\r
99 btn_cancel.Text = "Close";
\r
101 Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"));
\r
103 Application.Exit();
\r
106 private void DownloadFailed()
\r
108 lblProgress.Text = "Download Failed";
\r
109 btn_cancel.Text = "Close";
\r
112 private void btn_cancel_Click(object sender, EventArgs e)
\r
115 lblProgress.Text = "Cancelling ...";
\r
116 if (webResponse != null) webResponse.Close();
\r
117 if (responceStream != null) responceStream.Close();
\r
118 if (loacalStream != null) loacalStream.Close();
\r