OSDN Git Service

b8b74d2757090eeea81992223d554aecb4d37be5
[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.Collections.Generic;\r
9 using System.ComponentModel;\r
10 using System.Data;\r
11 using System.Drawing;\r
12 using System.Text;\r
13 using System.Windows.Forms;\r
14 using System.Net;\r
15 using System.IO;\r
16 using System.Threading;\r
17 using System.Diagnostics;\r
18 \r
19 namespace Handbrake\r
20 {\r
21     public partial class frmDownload : Form\r
22     {\r
23         private Thread downloadThread;\r
24         private Stream responceStream;\r
25         private Stream loacalStream;\r
26         private HttpWebRequest webRequest;\r
27         private HttpWebResponse webResponse;\r
28         private static int progress;\r
29         private delegate void UpdateProgessCallback(Int64 BytesRead, Int64 TotalBytes);\r
30         private delegate void DownloadCompleteCallback();\r
31         private delegate void DownloadFailedCallback();\r
32 \r
33 \r
34         public frmDownload()\r
35         {\r
36             InitializeComponent();\r
37 \r
38             try\r
39             {\r
40                 downloadThread = new Thread(Download);\r
41                 downloadThread.Start();\r
42             }\r
43             catch (Exception exc)\r
44             {\r
45                 MessageBox.Show("An error occured on the Download Thread \n" + exc.ToString(),"Error",MessageBoxButtons.OK, MessageBoxIcon.Error);\r
46             }\r
47         }\r
48 \r
49         private void Download()\r
50         {\r
51             Functions.AppcastReader rssRead = new Functions.AppcastReader();\r
52 \r
53             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
54 \r
55             if (File.Exists(tempPath))\r
56                 File.Delete(tempPath);\r
57 \r
58             string hbUpdate = rssRead.downloadFile();\r
59 \r
60             WebClient wcDownload = new WebClient();\r
61                 try\r
62                 {\r
63                     webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
64                     webRequest.Credentials = CredentialCache.DefaultCredentials;\r
65                     webResponse = (HttpWebResponse)webRequest.GetResponse();\r
66                     Int64 fileSize = webResponse.ContentLength;\r
67 \r
68                     responceStream = wcDownload.OpenRead(hbUpdate);\r
69                     loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
70 \r
71                     int bytesSize = 0;\r
72                     byte[] downBuffer = new byte[2048];\r
73 \r
74                     long flength = 0;\r
75                     while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
76                     {\r
77                         loacalStream.Write(downBuffer, 0, bytesSize);\r
78                         flength = loacalStream.Length;\r
79                         this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });\r
80                     }\r
81 \r
82                     responceStream.Close();\r
83                     loacalStream.Close();\r
84 \r
85                     if (flength != fileSize)\r
86                         this.Invoke(new DownloadFailedCallback(this.downloadFailed));\r
87                     else\r
88                         this.Invoke(new DownloadCompleteCallback(this.downloadComplete));\r
89                 }\r
90                 catch (Exception)\r
91                 {\r
92                     // Do Nothing \r
93                 }\r
94         }\r
95 \r
96         private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)\r
97         {\r
98             try\r
99             {\r
100                 long p = (BytesRead * 100) / TotalBytes;\r
101                 progress = int.Parse(p.ToString());\r
102                 progress_download.Value = progress;\r
103                 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";\r
104             }\r
105             catch (Exception exc)\r
106             {\r
107                 MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
108             }\r
109         }\r
110 \r
111         private void downloadComplete()\r
112         {\r
113             lblProgress.Text = "Download Complete";\r
114             btn_cancel.Text = "Close";\r
115 \r
116             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
117 \r
118             Process startInstall = Process.Start(tempPath);\r
119             this.Close();\r
120             Application.Exit();\r
121         }\r
122 \r
123         private void downloadFailed()\r
124         {\r
125             lblProgress.Text = "Download Failed";\r
126             btn_cancel.Text = "Close";\r
127         }\r
128 \r
129         private void btn_cancel_Click(object sender, EventArgs e)\r
130         {\r
131             try\r
132             {\r
133                 webResponse.Close();\r
134                 responceStream.Close();\r
135                 loacalStream.Close();\r
136                 downloadThread.Abort();\r
137                 progress_download.Value = 0;\r
138                 lblProgress.Text = "Download Stopped";\r
139                 this.Close();\r
140             }\r
141             catch (Exception)\r
142             {\r
143                 // Do nothing\r
144             }\r
145         }\r
146     }\r
147 }