OSDN Git Service

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