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.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             downloadThread = new Thread(Download);\r
39             downloadThread.Start();\r
40         }\r
41 \r
42         private void Download()\r
43         {\r
44             Functions.AppcastReader rssRead = new Functions.AppcastReader();\r
45             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
46             string hbUpdate = rssRead.downloadFile();\r
47             WebClient wcDownload = new WebClient();\r
48 \r
49             try\r
50             {\r
51                 if (File.Exists(tempPath))\r
52                     File.Delete(tempPath);\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(tempPath, 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 tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
108 \r
109             Process startInstall = Process.Start(tempPath);\r
110             this.Close();\r
111             Application.Exit();\r
112         }\r
113 \r
114         private void downloadFailed()\r
115         {\r
116             lblProgress.Text = "Download Failed";\r
117             btn_cancel.Text = "Close";\r
118         }\r
119 \r
120         private void btn_cancel_Click(object sender, EventArgs e)\r
121         {\r
122             try\r
123             {\r
124                 webResponse.Close();\r
125                 responceStream.Close();\r
126                 loacalStream.Close();\r
127                 downloadThread.Abort();\r
128                 progress_download.Value = 0;\r
129                 lblProgress.Text = "Download Stopped";\r
130                 this.Close();\r
131             }\r
132             catch (Exception)\r
133             {\r
134                 // Do nothing\r
135             }\r
136         }\r
137     }\r
138 }