OSDN Git Service

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