summaryrefslogtreecommitdiffstats
path: root/win/C#/frmDownload.cs
diff options
context:
space:
mode:
authorsr55 <[email protected]>2007-11-19 19:21:53 +0000
committersr55 <[email protected]>2007-11-19 19:21:53 +0000
commitb8778cc395681bf40e9f9a5fdcada956f174072d (patch)
treeda95afb46233e805c7af78729156f0de96ef3168 /win/C#/frmDownload.cs
parentca6b1dbdf7a7249f8d9ea00cdfe4681a05ade2fa (diff)
WinGui:
- About window text cut down. Had some useless information. - New "sparkle"-like update manager. - Misc Other small updates and fixes. git-svn-id: svn://svn.handbrake.fr/HandBrake/trunk@1070 b64f7644-9d1e-0410-96f1-a4d463321fa5
Diffstat (limited to 'win/C#/frmDownload.cs')
-rw-r--r--win/C#/frmDownload.cs134
1 files changed, 134 insertions, 0 deletions
diff --git a/win/C#/frmDownload.cs b/win/C#/frmDownload.cs
new file mode 100644
index 000000000..cac6f195d
--- /dev/null
+++ b/win/C#/frmDownload.cs
@@ -0,0 +1,134 @@
+using System;
+using System.Collections.Generic;
+using System.ComponentModel;
+using System.Data;
+using System.Drawing;
+using System.Text;
+using System.Windows.Forms;
+using System.Net;
+using System.IO;
+using System.Threading;
+using System.Diagnostics;
+
+namespace Handbrake
+{
+ public partial class frmDownload : Form
+ {
+ private Thread downloadThread;
+ private Stream responceStream;
+ private Stream loacalStream;
+ private HttpWebRequest webRequest;
+ private HttpWebResponse webResponse;
+ private static int progress;
+ private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);
+ private delegate void DownloadCompleteCallback();
+ private delegate void DownloadFailedCallback();
+
+
+ public frmDownload()
+ {
+ InitializeComponent();
+
+ try
+ {
+ downloadThread = new Thread(Download);
+ downloadThread.Start();
+ }
+ catch (Exception exc)
+ {
+ MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+
+ }
+
+ private void Download()
+ {
+ Functions.RssReader rssRead = new Functions.RssReader();
+
+ string appPath = Application.StartupPath.ToString() + "\\";
+ string hbUpdate = rssRead.downloadFile();
+ string downloadPath = appPath + "Handbrake-win.exe";
+ WebClient wcDownload = new WebClient();
+ try
+ {
+ webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);
+ webRequest.Credentials = CredentialCache.DefaultCredentials;
+ webResponse = (HttpWebResponse)webRequest.GetResponse();
+ Int64 fileSize = webResponse.ContentLength;
+
+ responceStream = wcDownload.OpenRead(hbUpdate);
+ loacalStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None);
+
+ int bytesSize = 0;
+ byte[] downBuffer = new byte[2048];
+
+ long flength = 0;
+ while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)
+ {
+ loacalStream.Write(downBuffer, 0, bytesSize);
+ flength = loacalStream.Length;
+ this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });
+ }
+
+ responceStream.Close();
+ loacalStream.Close();
+
+ if (flength != fileSize)
+ this.Invoke(new DownloadFailedCallback(this.downloadFailed));
+ else
+ this.Invoke(new DownloadCompleteCallback(this.downloadComplete));
+ }
+ catch (Exception exc)
+ {
+ // Do Nothing
+ }
+ }
+
+ private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)
+ {
+ try
+ {
+ long p = (BytesRead * 100) / TotalBytes;
+ progress = int.Parse(p.ToString());
+ progress_download.Value = progress;
+ lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";
+ }
+ catch (Exception exc)
+ {
+ MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
+ }
+ }
+
+ private void downloadComplete()
+ {
+ lblProgress.Text = "Download Complete";
+ btn_cancel.Text = "Close";
+
+ string appPath = Application.StartupPath.ToString() + "\\";
+
+ Process hbproc = Process.Start(appPath + "Handbrake-win.exe");
+ hbproc.WaitForExit();
+ hbproc.Dispose();
+ hbproc.Close();
+
+ this.Close();
+ }
+
+ private void downloadFailed()
+ {
+ lblProgress.Text = "Download Failed";
+ btn_cancel.Text = "Close";
+ }
+
+ private void btn_cancel_Click(object sender, EventArgs e)
+ {
+ webResponse.Close();
+ responceStream.Close();
+ loacalStream.Close();
+ downloadThread.Abort();
+ progress_download.Value = 0;
+ lblProgress.Text = "Download Stopped";
+ this.Close();
+ }
+ }
+} \ No newline at end of file