OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmDownload.cs
1 /*  frmDownload.cs $\r
2     This file is part of the HandBrake source code.\r
3     Homepage: <http://handbrake.fr>.\r
4     It may be used under the terms of the GNU General Public License. */\r
5 \r
6 namespace Handbrake\r
7 {\r
8     using System;\r
9     using System.Diagnostics;\r
10     using System.IO;\r
11     using System.Net;\r
12     using System.Threading;\r
13     using System.Windows.Forms;\r
14 \r
15     /// <summary>\r
16     /// The Download Window\r
17     /// </summary>\r
18     public partial class frmDownload : Form\r
19     {\r
20         private readonly Thread downloadThread;\r
21         private Stream responceStream;\r
22         private Stream localStream;\r
23         private HttpWebRequest webRequest;\r
24         private HttpWebResponse webResponse;\r
25         private static int progress;\r
26         private bool killThread;\r
27 \r
28         private delegate void UpdateProgessCallback(long bytesRead, long totalBytes);\r
29 \r
30         private delegate void DownloadCompleteCallback();\r
31 \r
32         private delegate void DownloadFailedCallback();\r
33 \r
34         public frmDownload(string filename)\r
35         {\r
36             InitializeComponent();\r
37 \r
38             this.downloadThread = new Thread(Download);\r
39             this.downloadThread.Start(filename);\r
40         }\r
41 \r
42         private void Download(object file)\r
43         {\r
44             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
45             string hbUpdate = (string)file;\r
46             WebClient wcDownload = new WebClient();\r
47 \r
48             try\r
49             {\r
50                 if (File.Exists(tempPath))\r
51                     File.Delete(tempPath);\r
52 \r
53                 this.webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
54                 this.webRequest.Credentials = CredentialCache.DefaultCredentials;\r
55                 this.webResponse = (HttpWebResponse)this.webRequest.GetResponse();\r
56                 long fileSize = this.webResponse.ContentLength;\r
57 \r
58                 this.responceStream = wcDownload.OpenRead(hbUpdate);\r
59                 this.localStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
60 \r
61                 int bytesSize;\r
62                 byte[] downBuffer = new byte[2048];\r
63 \r
64                 long flength = 0;\r
65                 while ((bytesSize = this.responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
66                 {\r
67                     if (this.killThread)\r
68                         return;\r
69                     this.localStream.Write(downBuffer, 0, bytesSize);\r
70                     flength = this.localStream.Length;\r
71                     Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] {this.localStream.Length, fileSize});\r
72                 }\r
73 \r
74                 this.responceStream.Close();\r
75                 this.localStream.Close();\r
76 \r
77                 if (flength != fileSize)\r
78                     Invoke(new DownloadFailedCallback(this.DownloadFailed));\r
79                 else\r
80                     Invoke(new DownloadCompleteCallback(this.DownloadComplete));\r
81             }\r
82             catch\r
83             {\r
84                 // Do Nothing \r
85             }\r
86         }\r
87 \r
88         private void UpdateProgress(long bytesRead, long totalBytes)\r
89         {\r
90             long p = (bytesRead * 100) / totalBytes;\r
91             int.TryParse(p.ToString(), out progress);\r
92             progress_download.Value = progress;\r
93             lblProgress.Text = (bytesRead / 1024) + "k of " + (totalBytes / 1024) + "k ";\r
94         }\r
95 \r
96         private void DownloadComplete()\r
97         {\r
98             lblProgress.Text = "Download Complete";\r
99             btn_cancel.Text = "Close";\r
100 \r
101             Process.Start(Path.Combine(Path.GetTempPath(), "handbrake-setup.exe"));\r
102             this.Close();\r
103             Application.Exit();\r
104         }\r
105 \r
106         private void DownloadFailed()\r
107         {\r
108             lblProgress.Text = "Download Failed";\r
109             btn_cancel.Text = "Close";\r
110         }\r
111 \r
112         private void btn_cancel_Click(object sender, EventArgs e)\r
113         {\r
114             this.killThread = true;\r
115             lblProgress.Text = "Cancelling ...";\r
116             if (this.webResponse != null) this.webResponse.Close();\r
117             if (this.responceStream != null) this.responceStream.Close();\r
118             if (this.localStream != null) this.localStream.Close();\r
119             this.Close();\r
120         }\r
121     }\r
122 }