OSDN Git Service

merge 0.9.4 to jp
[handbrake-jp/handbrake-jp.git] / win / C# / frmReadDVD.cs
1 /*  frmReadDVD.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.IO;\r
15 using System.Threading;\r
16 using System.Diagnostics;\r
17 using System.Collections;\r
18 \r
19 \r
20 namespace Handbrake\r
21 {\r
22     public partial class frmReadDVD : Form\r
23     {\r
24         private string inputFile;\r
25         private frmMain mainWindow;\r
26         private Parsing.DVD thisDvd;\r
27         private delegate void UpdateUIHandler();\r
28         Process hbproc;\r
29         Functions.Main hb_common_func = new Functions.Main();\r
30         Functions.Encode process = new Functions.Encode();\r
31 \r
32         public frmReadDVD(string inputFile, frmMain parent)\r
33         {\r
34             InitializeComponent();\r
35             this.inputFile = inputFile;\r
36             this.mainWindow = parent;\r
37             startScan();\r
38         }\r
39 \r
40         private void startScan()\r
41         {\r
42             try\r
43             {\r
44                 lbl_status.Visible = true;\r
45                 ThreadPool.QueueUserWorkItem(startProc);\r
46             }\r
47             catch (Exception exc)\r
48             {\r
49                 MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
50             }\r
51         }\r
52 \r
53         private void startProc(object state)\r
54         {\r
55             try\r
56             {\r
57                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
58                 string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat");\r
59 \r
60                 // Make we don't pick up a stale hb_encode_log.dat (and that we have rights to the file)\r
61                 if (File.Exists(dvdInfoPath))\r
62                     File.Delete(dvdInfoPath);\r
63 \r
64                 string strCmdLine = String.Format(@"cmd /c """"{0}"" -i ""{1}"" -t0 -v >""{2}"" 2>&1""", handbrakeCLIPath, inputFile, dvdInfoPath);\r
65 \r
66                 ProcessStartInfo hbParseDvd = new ProcessStartInfo("CMD.exe", strCmdLine);\r
67                 hbParseDvd.WindowStyle = ProcessWindowStyle.Hidden;\r
68 \r
69                 using (hbproc = Process.Start(hbParseDvd))\r
70                 {\r
71                     hbproc.WaitForExit();\r
72                 }\r
73 \r
74                 if (!File.Exists(dvdInfoPath))\r
75                 {\r
76                     throw new Exception("DVD\82Ì\8fî\95ñ\82ð\8eæ\93¾\82Å\82«\82Ü\82¹\82ñ\82Å\82µ\82½\81Bdvdinfo.dat\82ª\82 \82è\82Ü\82¹\82ñ\nExpected location of dvdinfo.dat: \n" + dvdInfoPath);\r
77                 }\r
78 \r
79                 using (StreamReader sr = new StreamReader(dvdInfoPath))\r
80                 {\r
81                     thisDvd = Parsing.DVD.Parse(sr);\r
82                     sr.Close();\r
83                     sr.Dispose();\r
84                 }\r
85 \r
86                 updateUIElements();\r
87             }\r
88             catch (Exception exc)\r
89             {\r
90                 MessageBox.Show("frmReadDVD.cs - startProc() " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
91                 closeWindowAfterError();\r
92             }\r
93         }\r
94 \r
95         private void updateUIElements()\r
96         {\r
97             try\r
98             {\r
99                 if (this.InvokeRequired)\r
100                 {\r
101                     this.BeginInvoke(new UpdateUIHandler(updateUIElements));\r
102                     return;\r
103                 }\r
104                 // Now pass this streamreader to frmMain so that it can be used there.\r
105                 mainWindow.setStreamReader(thisDvd);\r
106 \r
107                 mainWindow.drp_dvdtitle.Items.Clear();\r
108                 if (thisDvd.Titles.Count != 0)\r
109                     mainWindow.drp_dvdtitle.Items.AddRange(thisDvd.Titles.ToArray());\r
110                 mainWindow.drp_dvdtitle.Text = "Automatic";\r
111                 mainWindow.drop_chapterFinish.Text = "Auto";\r
112                 mainWindow.drop_chapterStart.Text = "Auto";\r
113 \r
114                 // Now select the longest title\r
115                 if (thisDvd.Titles.Count != 0)\r
116                     mainWindow.drp_dvdtitle.SelectedItem = hb_common_func.selectLongestTitle(mainWindow.drp_dvdtitle);\r
117 \r
118                 this.Close();\r
119             }\r
120             catch (Exception exc)\r
121             {\r
122                 MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
123                 this.Close();\r
124             }\r
125         }\r
126 \r
127         private void closeWindowAfterError()\r
128         {\r
129             try\r
130             {\r
131                 if (this.InvokeRequired)\r
132                 {\r
133                     this.BeginInvoke(new UpdateUIHandler(closeWindowAfterError));\r
134                     return;\r
135                 }\r
136                 this.Close();\r
137             }\r
138             catch (Exception exc)\r
139             {\r
140                 MessageBox.Show("frmReadDVD.cs - closeWindowAfterError - Unable to recover from a serious error. \nYou may need to restart HandBrake. \n\n Error Information: \n " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
141             }\r
142         }\r
143 \r
144         private void btn_cancel_Click(object sender, EventArgs e)\r
145         {\r
146             // This may seem like a long way of killing HandBrakeCLI, but for whatever reason,\r
147             // hbproc.kill/close just won't do the trick.\r
148             try\r
149             {\r
150                 string AppName = "HandBrakeCLI";\r
151 \r
152                 AppName = AppName.ToUpper();\r
153 \r
154                 System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();\r
155                 foreach (System.Diagnostics.Process proces in prs)\r
156                 {\r
157                     if (proces.ProcessName.ToUpper() == AppName)\r
158                     {\r
159                         proces.Refresh();\r
160                         if (!proces.HasExited)\r
161                             proces.Kill();\r
162                     }\r
163                 }\r
164             }\r
165             catch (Exception ex)\r
166             {\r
167                 MessageBox.Show("HandBrakeCLI.exe\82Ì\8fI\97¹\82É\8e¸\94s\82µ\82Ü\82µ\82½\81B\n\nError Information: \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
168             }\r
169         }\r
170     }\r
171 }