OSDN Git Service

merge 0.9.4 to jp
[handbrake-jp/handbrake-jp.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 delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);\r
25         private delegate void DownloadCompleteCallback();\r
26         private delegate void DownloadFailedCallback();\r
27 \r
28         private string file;\r
29 \r
30         public frmDownload(string filename)\r
31         {\r
32             InitializeComponent();\r
33 \r
34             file = filename;\r
35             downloadThread = new Thread(Download);\r
36             downloadThread.Start();\r
37         }\r
38 \r
39         private void Download()\r
40         {\r
41             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
42             string hbUpdate = file;\r
43             WebClient wcDownload = new WebClient();\r
44 \r
45             try\r
46             {\r
47                 if (File.Exists(tempPath))\r
48                     File.Delete(tempPath);\r
49 \r
50                 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
51                 webRequest.Credentials = CredentialCache.DefaultCredentials;\r
52                 webResponse = (HttpWebResponse)webRequest.GetResponse();\r
53                 Int64 fileSize = webResponse.ContentLength;\r
54 \r
55                 responceStream = wcDownload.OpenRead(hbUpdate);\r
56                 loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
57 \r
58                 int bytesSize;\r
59                 byte[] downBuffer = new byte[2048];\r
60 \r
61                 long flength = 0;\r
62                 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
63                 {\r
64                     loacalStream.Write(downBuffer, 0, bytesSize);\r
65                     flength = loacalStream.Length;\r
66                     Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });\r
67                 }\r
68 \r
69                 responceStream.Close();\r
70                 loacalStream.Close();\r
71 \r
72                 if (flength != fileSize)\r
73                     Invoke(new DownloadFailedCallback(this.downloadFailed));\r
74                 else\r
75                     Invoke(new DownloadCompleteCallback(this.downloadComplete));\r
76             }\r
77             catch (Exception)\r
78             {\r
79                 // Do Nothing \r
80             }\r
81         }\r
82 \r
83         private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)\r
84         {\r
85             try\r
86             {\r
87                 long p = (BytesRead * 100) / TotalBytes;\r
88                 progress = int.Parse(p.ToString());\r
89                 progress_download.Value = progress;\r
90                 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";\r
91             }\r
92             catch (Exception exc)\r
93             {\r
94                 MessageBox.Show("Integer Conversion Error On Download \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
95             }\r
96         }\r
97 \r
98         private void downloadComplete()\r
99         {\r
100             lblProgress.Text = "\83_\83E\83\93\83\8d\81[\83h\8a®\97¹";\r
101             btn_cancel.Text = "\95Â\82\82é";\r
102 \r
103             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
104             Process.Start(tempPath);\r
105             this.Close();\r
106             Application.Exit();\r
107         }\r
108 \r
109         private void downloadFailed()\r
110         {\r
111             lblProgress.Text = "\83_\83E\83\93\83\8d\81[\83h\8e¸\94s";\r
112             btn_cancel.Text = "\95Â\82\82é";\r
113         }\r
114 \r
115         private void btn_cancel_Click(object sender, EventArgs e)\r
116         {\r
117             try\r
118             {\r
119                 webResponse.Close();\r
120                 responceStream.Close();\r
121                 loacalStream.Close();\r
122                 downloadThread.Abort();\r
123                 progress_download.Value = 0;\r
124                 lblProgress.Text = "\83_\83E\83\93\83\8d\81[\83h\92â\8e~";\r
125                 this.Close();\r
126             }\r
127             catch (Exception)\r
128             {\r
129                 // Do nothing\r
130             }\r
131         }\r
132     }\r
133 }