OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmDownload.cs
index cac6f19..8fe53ba 100644 (file)
@@ -1,9 +1,11 @@
+/*  frmDownload.cs $\r
+       \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
 using System;\r
 using System.Collections.Generic;\r
-using System.ComponentModel;\r
-using System.Data;\r
-using System.Drawing;\r
-using System.Text;\r
 using System.Windows.Forms;\r
 using System.Net;\r
 using System.IO;\r
@@ -24,64 +26,59 @@ namespace Handbrake
         private delegate void DownloadCompleteCallback();\r
         private delegate void DownloadFailedCallback();\r
 \r
+        private string file;\r
 \r
-        public frmDownload()\r
+        public frmDownload(string filename)\r
         {\r
             InitializeComponent();\r
 \r
-            try\r
-            {\r
-                downloadThread = new Thread(Download);\r
-                downloadThread.Start();\r
-            }\r
-            catch (Exception exc)\r
-            {\r
-                MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);\r
-            }\r
-          \r
+            file = filename;\r
+            downloadThread = new Thread(Download);\r
+            downloadThread.Start();\r
         }\r
 \r
         private void Download()\r
         {\r
-            Functions.RssReader rssRead = new Functions.RssReader();\r
-\r
-            string appPath = Application.StartupPath.ToString() + "\\";\r
-            string hbUpdate = rssRead.downloadFile(); \r
-            string downloadPath = appPath + "Handbrake-win.exe";\r
+            string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
+            string hbUpdate = file;\r
             WebClient wcDownload = new WebClient();\r
-                try\r
-                {\r
-                    webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
-                    webRequest.Credentials = CredentialCache.DefaultCredentials;\r
-                    webResponse = (HttpWebResponse)webRequest.GetResponse();\r
-                    Int64 fileSize = webResponse.ContentLength;\r
-\r
-                    responceStream = wcDownload.OpenRead(hbUpdate);\r
-                    loacalStream = new FileStream(downloadPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
-\r
-                    int bytesSize = 0;\r
-                    byte[] downBuffer = new byte[2048];\r
-\r
-                    long flength = 0;\r
-                    while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
-                    {\r
-                        loacalStream.Write(downBuffer, 0, bytesSize);\r
-                        flength = loacalStream.Length;\r
-                        this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });\r
-                    }\r
-\r
-                    responceStream.Close();\r
-                    loacalStream.Close();\r
-\r
-                    if (flength != fileSize)\r
-                        this.Invoke(new DownloadFailedCallback(this.downloadFailed));\r
-                    else\r
-                        this.Invoke(new DownloadCompleteCallback(this.downloadComplete));\r
-                }\r
-                catch (Exception exc)\r
+\r
+            try\r
+            {\r
+                if (File.Exists(tempPath))\r
+                    File.Delete(tempPath);\r
+\r
+                webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
+                webRequest.Credentials = CredentialCache.DefaultCredentials;\r
+                webResponse = (HttpWebResponse)webRequest.GetResponse();\r
+                Int64 fileSize = webResponse.ContentLength;\r
+\r
+                responceStream = wcDownload.OpenRead(hbUpdate);\r
+                loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
+\r
+                int bytesSize = 0;\r
+                byte[] downBuffer = new byte[2048];\r
+\r
+                long flength = 0;\r
+                while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
                 {\r
-                    // Do Nothing \r
+                    loacalStream.Write(downBuffer, 0, bytesSize);\r
+                    flength = loacalStream.Length;\r
+                    this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });\r
                 }\r
+\r
+                responceStream.Close();\r
+                loacalStream.Close();\r
+\r
+                if (flength != fileSize)\r
+                    this.Invoke(new DownloadFailedCallback(this.downloadFailed));\r
+                else\r
+                    this.Invoke(new DownloadCompleteCallback(this.downloadComplete));\r
+            }\r
+            catch (Exception)\r
+            {\r
+                // Do Nothing \r
+            }\r
         }\r
 \r
         private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)\r
@@ -104,14 +101,11 @@ namespace Handbrake
             lblProgress.Text = "Download Complete";\r
             btn_cancel.Text = "Close";\r
 \r
-            string appPath = Application.StartupPath.ToString() + "\\";\r
-            \r
-            Process hbproc = Process.Start(appPath + "Handbrake-win.exe");\r
-            hbproc.WaitForExit();\r
-            hbproc.Dispose();\r
-            hbproc.Close();\r
+            string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
 \r
+            Process startInstall = Process.Start(tempPath);\r
             this.Close();\r
+            Application.Exit();\r
         }\r
 \r
         private void downloadFailed()\r
@@ -122,13 +116,20 @@ namespace Handbrake
 \r
         private void btn_cancel_Click(object sender, EventArgs e)\r
         {\r
-            webResponse.Close();\r
-            responceStream.Close();\r
-            loacalStream.Close();\r
-            downloadThread.Abort();\r
-            progress_download.Value = 0;\r
-            lblProgress.Text = "Download Stopped";\r
-            this.Close();\r
+            try\r
+            {\r
+                webResponse.Close();\r
+                responceStream.Close();\r
+                loacalStream.Close();\r
+                downloadThread.Abort();\r
+                progress_download.Value = 0;\r
+                lblProgress.Text = "Download Stopped";\r
+                this.Close();\r
+            }\r
+            catch (Exception)\r
+            {\r
+                // Do nothing\r
+            }\r
         }\r
     }\r
 }
\ No newline at end of file