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 Boolean _killThread;
\r
25 private delegate void UpdateProgessCallback(Int64 bytesRead, Int64 totalBytes);
\r
26 private delegate void DownloadCompleteCallback();
\r
27 private delegate void DownloadFailedCallback();
\r
30 public frmDownload(string filename)
\r
32 InitializeComponent();
\r
34 _downloadThread = new Thread(Download);
\r
35 _downloadThread.Start(filename);
\r
38 private void Download(object file)
\r
40 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
41 string hbUpdate = (string)file;
\r
42 WebClient wcDownload = new WebClient();
\r
46 if (File.Exists(tempPath))
\r
47 File.Delete(tempPath);
\r
49 _webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
\r
50 _webRequest.Credentials = CredentialCache.DefaultCredentials;
\r
51 _webResponse = (HttpWebResponse)_webRequest.GetResponse();
\r
52 Int64 fileSize = _webResponse.ContentLength;
\r
54 _responceStream = wcDownload.OpenRead(hbUpdate);
\r
55 _loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
\r
58 byte[] downBuffer = new byte[2048];
\r
61 while ((bytesSize = _responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
\r
65 _loacalStream.Write(downBuffer, 0, bytesSize);
\r
66 flength = _loacalStream.Length;
\r
67 Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { _loacalStream.Length, fileSize });
\r
70 _responceStream.Close();
\r
71 _loacalStream.Close();
\r
73 if (flength != fileSize)
\r
74 Invoke(new DownloadFailedCallback(this.DownloadFailed));
\r
76 Invoke(new DownloadCompleteCallback(this.DownloadComplete));
\r
84 private void UpdateProgress(Int64 bytesRead, Int64 totalBytes)
\r
86 long p = (bytesRead * 100) / totalBytes;
\r
87 int.TryParse(p.ToString(), out _progress);
\r
88 progress_download.Value = _progress;
\r
89 lblProgress.Text = (bytesRead / 1024) + "k of " + (totalBytes / 1024) + "k ";
\r
92 private void DownloadComplete()
\r
94 lblProgress.Text = "Download Complete";
\r
95 btn_cancel.Text = "Close";
\r
97 Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"));
\r
102 private void DownloadFailed()
\r
104 lblProgress.Text = "Download Failed";
\r
105 btn_cancel.Text = "Close";
\r
108 private void btn_cancel_Click(object sender, EventArgs e)
\r
110 _killThread = true;
\r
111 lblProgress.Text = "Cancelling ...";
\r
112 if (_webResponse != null) _webResponse.Close();
\r
113 if (_responceStream != null) _responceStream.Close();
\r
114 if (_loacalStream != null) _loacalStream.Close();
\r