OSDN Git Service

Set an minimum subtitle display time of three seconds *or* until the next subtitle...
[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.m0k.org/>.\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 \r
50         private void Download()\r
51         {\r
52             Functions.RssReader rssRead = new Functions.RssReader();\r
53 \r
54             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
55 \r
56             if (File.Exists(tempPath))\r
57                 File.Delete(tempPath);\r
58 \r
59             string hbUpdate = rssRead.downloadFile();\r
60 \r
61             WebClient wcDownload = new WebClient();\r
62                 try\r
63                 {\r
64                     webRequest = (HttpWebRequest)WebRequest.Create(hbUpdate);\r
65                     webRequest.Credentials = CredentialCache.DefaultCredentials;\r
66                     webResponse = (HttpWebResponse)webRequest.GetResponse();\r
67                     Int64 fileSize = webResponse.ContentLength;\r
68 \r
69                     responceStream = wcDownload.OpenRead(hbUpdate);\r
70                     loacalStream = new FileStream(tempPath, FileMode.Create, FileAccess.Write, FileShare.None);\r
71 \r
72                     int bytesSize = 0;\r
73                     byte[] downBuffer = new byte[2048];\r
74 \r
75                     long flength = 0;\r
76                     while ((bytesSize = responceStream.Read(downBuffer, 0, downBuffer.Length)) > 0)\r
77                     {\r
78                         loacalStream.Write(downBuffer, 0, bytesSize);\r
79                         flength = loacalStream.Length;\r
80                         this.Invoke(new UpdateProgessCallback(this.UpdateProgress), new object[] { loacalStream.Length, fileSize });\r
81                     }\r
82 \r
83                     responceStream.Close();\r
84                     loacalStream.Close();\r
85 \r
86                     if (flength != fileSize)\r
87                         this.Invoke(new DownloadFailedCallback(this.downloadFailed));\r
88                     else\r
89                         this.Invoke(new DownloadCompleteCallback(this.downloadComplete));\r
90                 }\r
91                 catch (Exception)\r
92                 {\r
93                     // Do Nothing \r
94                 }\r
95         }\r
96 \r
97         private void UpdateProgress(Int64 BytesRead, Int64 TotalBytes)\r
98         {\r
99             try\r
100             {\r
101                 long p = (BytesRead * 100) / TotalBytes;\r
102                 progress = int.Parse(p.ToString());\r
103                 progress_download.Value = progress;\r
104                 lblProgress.Text = (BytesRead / 1024) + "k of " + (TotalBytes / 1024) + "k ";\r
105             }\r
106             catch (Exception exc)\r
107             {\r
108                 MessageBox.Show("Integer Convertion Error On Download \n" + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
109             }\r
110         }\r
111 \r
112         private void downloadComplete()\r
113         {\r
114             lblProgress.Text = "Download Complete";\r
115             btn_cancel.Text = "Close";\r
116 \r
117             string tempPath = Path.Combine(Path.GetTempPath(), "handbrake-setup.exe");\r
118 \r
119             Process startInstall = Process.Start(tempPath);\r
120             this.Close();\r
121             Application.Exit();\r
122         }\r
123 \r
124         private void downloadFailed()\r
125         {\r
126             lblProgress.Text = "Download Failed";\r
127             btn_cancel.Text = "Close";\r
128         }\r
129 \r
130         private void btn_cancel_Click(object sender, EventArgs e)\r
131         {\r
132             try\r
133             {\r
134                 webResponse.Close();\r
135                 responceStream.Close();\r
136                 loacalStream.Close();\r
137                 downloadThread.Abort();\r
138                 progress_download.Value = 0;\r
139                 lblProgress.Text = "Download Stopped";\r
140                 this.Close();\r
141             }\r
142             catch (Exception)\r
143             {\r
144                 // Do nothing\r
145             }\r
146         }\r
147     }\r
148 }