OSDN Git Service

import original 0.9.5 release
[handbrake-jp/handbrake-jp.git] / win / C# / HandBrake.Framework / Views / DownloadUpdate.cs
diff --git a/win/C#/HandBrake.Framework/Views/DownloadUpdate.cs b/win/C#/HandBrake.Framework/Views/DownloadUpdate.cs
new file mode 100644 (file)
index 0000000..3b12a64
--- /dev/null
@@ -0,0 +1,122 @@
+/*  DownloadUpdate.cs $\r
+    This file is part of the HandBrake source code.\r
+    Homepage: <http://handbrake.fr>.\r
+    It may be used under the terms of the GNU General Public License. */\r
+\r
+namespace HandBrake.Framework.Views\r
+{\r
+    using System;\r
+    using System.Diagnostics;\r
+    using System.IO;\r
+    using System.Net;\r
+    using System.Threading;\r
+    using System.Windows.Forms;\r
+\r
+    /// <summary>\r
+    /// The Download Window\r
+    /// </summary>\r
+    public partial class DownloadUpdate : Form\r
+    {\r
+        private readonly Thread downloadThread;\r
+        private Stream responceStream;\r
+        private Stream localStream;\r
+        private HttpWebRequest webRequest;\r
+        private HttpWebResponse webResponse;\r
+        private static int progress;\r
+        private bool killThread;\r
+\r
+        private delegate void UpdateProgessCallback(long bytesRead, long totalBytes);\r
+\r
+        private delegate void DownloadCompleteCallback();\r
+\r
+        private delegate void DownloadFailedCallback();\r
+\r
+        public DownloadUpdate(string filename)\r
+        {\r
+            InitializeComponent();\r
+\r
+            this.downloadThread = new Thread(Download);\r
+            this.downloadThread.Start(filename);\r
+        }\r
+\r
+        private void Download(object file)\r
+        {\r
+            string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
+            string hbUpdate = (string)file;\r
+            WebClient wcDownload = new WebClient();\r
+\r
+            try\r
+            {\r
+                if (File.Exists(tempPath))\r
+                    File.Delete(tempPath);\r
+\r
+                this.webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
+                this.webRequest.Credentials = CredentialCache.DefaultCredentials;\r
+                this.webResponse = (HttpWebResponse)this.webRequest.GetResponse();\r
+                long fileSize = this.webResponse.ContentLength;\r
+\r
+                this.responceStream = wcDownload.OpenRead(hbUpdate);\r
+                this.localStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
+\r
+                int bytesSize;\r
+                byte[] downBuffer = new byte[2048];\r
+\r
+                long flength = 0;\r
+                while ((bytesSize = this.responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
+                {\r
+                    if (this.killThread)\r
+                        return;\r
+                    this.localStream.Write(downBuffer, 0, bytesSize);\r
+                    flength = this.localStream.Length;\r
+                    Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] {this.localStream.Length, fileSize});\r
+                }\r
+\r
+                this.responceStream.Close();\r
+                this.localStream.Close();\r
+\r
+                if (flength != fileSize)\r
+                    Invoke(new DownloadFailedCallback(this.DownloadFailed));\r
+                else\r
+                    Invoke(new DownloadCompleteCallback(this.DownloadComplete));\r
+            }\r
+            catch\r
+            {\r
+                lblProgress.Text = "An Error Occured. Please try again later.";\r
+            }\r
+        }\r
+\r
+        private void UpdateProgress(long bytesRead, long totalBytes)\r
+        {\r
+            long p = (bytesRead * 100) / totalBytes;\r
+            int.TryParse(p.ToString(), out progress);\r
+            progress_download.Value = progress;\r
+            lblProgress.Text = (bytesRead / 1024) + "k of " + (totalBytes / 1024) + "k ";\r
+        }\r
+\r
+        private void DownloadComplete()\r
+        {\r
+            lblProgress.Text = "Download Complete";\r
+            btn_cancel.Text = "Close";\r
+\r
+            Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"));\r
+            this.Close();\r
+            Application.Exit();\r
+        }\r
+\r
+        private void DownloadFailed()\r
+        {\r
+            lblProgress.Text = "Download Failed";\r
+            btn_cancel.Text = "Close";\r
+        }\r
+\r
+        private void BtnCancelClick(object sender, EventArgs e)\r
+        {\r
+            this.killThread = true;\r
+            lblProgress.Text = "Cancelling ...";\r
+            if (this.webResponse != null) this.webResponse.Close();\r
+            if (this.responceStream != null) this.responceStream.Close();\r
+            if (this.localStream != null) this.localStream.Close();\r
+            this.Close();\r
+        }\r
+    }\r
+}
\ No newline at end of file