OSDN Git Service

import jp-0.9.3
[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.Collections.Generic;\r
9 using System.Windows.Forms;\r
10 using System.Net;\r
11 using System.IO;\r
12 using System.Threading;\r
13 using System.Diagnostics;\r
14 \r
15 namespace Handbrake\r
16 {\r
17     public partial class frmDownload : Form\r
18     {\r
19         private Thread downloadThread;\r
20         private Stream responceStream;\r
21         private Stream loacalStream;\r
22         private HttpWebRequest webRequest;\r
23         private HttpWebResponse webResponse;\r
24         private static int progress;\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         private string file;\r
30 \r
31         public frmDownload(string filename)\r
32         {\r
33             InitializeComponent();\r
34 \r
35             file = filename;\r
36             downloadThread = new Thread(Download);\r
37             downloadThread.Start();\r
38         }\r
39 \r
40         private void Download()\r
41         {\r
42             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
43             string hbUpdate = file;\r
44             WebClient wcDownload = new WebClient();\r
45 \r
46             try\r
47             {\r
48                 if (File.Exists(tempPath))\r
49                     File.Delete(tempPath);\r
50 \r
51                 webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
52                 webRequest.Credentials = CredentialCache.DefaultCredentials;\r
53                 webResponse = (HttpWebResponse)webRequest.GetResponse();\r
54                 Int64 fileSize = webResponse.ContentLength;\r
55 \r
56                 responceStream = wcDownload.OpenRead(hbUpdate);\r
57                 loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
58 \r
59                 int bytesSize = 0;\r
60                 byte[] downBuffer = new byte[2048];\r
61 \r
62                 long flength = 0;\r
63                 while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
64                 {\r
65                     loacalStream.Write(downBuffer, 0, bytesSize);\r
66                     flength = loacalStream.Length;\r
67                     this.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                     this.Invoke(new DownloadFailedCallback(this.downloadFailed));\r
75                 else\r
76                     this.Invoke(new DownloadCompleteCallback(this.downloadComplete));\r
77             }\r
78             catch (Exception)\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             try\r
87             {\r
88                 long p = (BytesRead * 100) / TotalBytes;\r
89                 progress = int.Parse(p.ToString());\r
90                 progress_download.Value = progress;\r
91                 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";\r
92             }\r
93             catch (Exception exc)\r
94             {\r
95                 MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
96             }\r
97         }\r
98 \r
99         private void downloadComplete()\r
100         {\r
101             lblProgress.Text = "\83_\83E\83\93\83\8d\81[\83h\8a®\97¹";\r
102             btn_cancel.Text = "\95Â\82\82é";\r
103 \r
104             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
105 \r
106             Process startInstall = Process.Start(tempPath);\r
107             this.Close();\r
108             Application.Exit();\r
109         }\r
110 \r
111         private void downloadFailed()\r
112         {\r
113             lblProgress.Text = "\83_\83E\83\93\83\8d\81[\83h\8e¸\94s";\r
114             btn_cancel.Text = "\95Â\82\82é";\r
115         }\r
116 \r
117         private void btn_cancel_Click(object sender, EventArgs e)\r
118         {\r
119             try\r
120             {\r
121                 webResponse.Close();\r
122                 responceStream.Close();\r
123                 loacalStream.Close();\r
124                 downloadThread.Abort();\r
125                 progress_download.Value = 0;\r
126                 lblProgress.Text = "\83_\83E\83\93\83\8d\81[\83h\92â\8e~";\r
127                 this.Close();\r
128             }\r
129             catch (Exception)\r
130             {\r
131                 // Do nothing\r
132             }\r
133         }\r
134     }\r
135 }