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.Windows.Forms;
\r
12 using System.Threading;
\r
13 using System.Diagnostics;
\r
17 public partial class frmDownload : Form
\r
19 private Thread downloadThread;
\r
20 private Stream responceStream;
\r
21 private Stream loacalStream;
\r
22 private HttpWebRequest webRequest;
\r
23 private HttpWebResponse webResponse;
\r
24 private static int progress;
\r
25 private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
\r
26 private delegate void DownloadCompleteCallback();
\r
27 private delegate void DownloadFailedCallback();
\r
29 private string file;
\r
31 public frmDownload(string filename)
\r
33 InitializeComponent();
\r
36 downloadThread = new Thread(Download);
\r
37 downloadThread.Start();
\r
40 private void Download()
\r
42 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
43 string hbUpdate = file;
\r
44 WebClient wcDownload = new WebClient();
\r
48 if (File.Exists(tempPath))
\r
49 File.Delete(tempPath);
\r
51 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
\r
52 webRequest.Credentials = CredentialCache.DefaultCredentials;
\r
53 webResponse = (HttpWebResponse)webRequest.GetResponse();
\r
54 Int64 fileSize = webResponse.ContentLength;
\r
56 responceStream = wcDownload.OpenRead(hbUpdate);
\r
57 loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);
\r
60 byte[] downBuffer = new byte[2048];
\r
63 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
\r
65 loacalStream.Write(downBuffer, 0, bytesSize);
\r
66 flength = loacalStream.Length;
\r
67 this.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 this.Invoke(new DownloadFailedCallback(this.downloadFailed));
\r
76 this.Invoke(new DownloadCompleteCallback(this.downloadComplete));
\r
84 private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
\r
88 long p = (BytesRead * 100) / TotalBytes;
\r
89 progress = int.Parse(p.ToString());
\r
90 progress_download.Value = progress;
\r
91 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";
\r
93 catch (Exception exc)
\r
95 MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
99 private void downloadComplete()
\r
101 lblProgress.Text = "Download Complete";
\r
102 btn_cancel.Text = "Close";
\r
104 string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");
\r
106 Process startInstall = Process.Start(tempPath);
\r
108 Application.Exit();
\r
111 private void downloadFailed()
\r
113 lblProgress.Text = "Download Failed";
\r
114 btn_cancel.Text = "Close";
\r
117 private void btn_cancel_Click(object sender, EventArgs e)
\r
121 webResponse.Close();
\r
122 responceStream.Close();
\r
123 loacalStream.Close();
\r
124 downloadThread.Abort();
\r
125 progress_download.Value = 0;
\r
126 lblProgress.Text = "Download Stopped";
\r