OSDN Git Service

ec9b318d12f9b2bc7a56022bce25ecad814c6bd9
[handbrake-jp/handbrake-jp-git.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.Common hb_common_func = new Functions.Common();\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         private void startProc(object state)\r
53         {\r
54             try\r
55             {\r
56                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
57                 string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat");\r
58 \r
59                 // Make we don't pick up a stale hb_encode_log.dat (and that we have rights to the file)\r
60                 if (File.Exists(dvdInfoPath))\r
61                     File.Delete(dvdInfoPath);\r
62 \r
63                 string strCmdLine = String.Format(@"cmd /c """"{0}"" -i ""{1}"" -t0 -v >""{2}"" 2>&1""", handbrakeCLIPath, inputFile, dvdInfoPath);\r
64 \r
65                 ProcessStartInfo hbParseDvd = new ProcessStartInfo("CMD.exe", strCmdLine);\r
66                 hbParseDvd.WindowStyle = ProcessWindowStyle.Hidden;\r
67 \r
68                 using (hbproc = Process.Start(hbParseDvd))\r
69                 {\r
70                     hbproc.WaitForExit();\r
71                 }\r
72 \r
73                 if (!File.Exists(dvdInfoPath))\r
74                 {\r
75                     throw new Exception("Unable to retrieve the DVD Info. dvdinfo.dat is missing. \nExpected location of dvdinfo.dat: \n" + dvdInfoPath);\r
76                 }\r
77 \r
78                 using (StreamReader sr = new StreamReader(dvdInfoPath))\r
79                 {\r
80                     thisDvd = Parsing.DVD.Parse(sr);\r
81                     sr.Close();\r
82                     sr.Dispose();\r
83                 }\r
84 \r
85                 updateUIElements();\r
86             }\r
87             catch (Exception exc)\r
88             {\r
89                 MessageBox.Show("frmReadDVD.cs - startProc() " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
90                 closeWindowAfterError();\r
91             }\r
92         }\r
93 \r
94         private void updateUIElements()\r
95         {\r
96             try\r
97             {\r
98                 if (this.InvokeRequired)\r
99                 {\r
100                     this.BeginInvoke(new UpdateUIHandler(updateUIElements));\r
101                     return;\r
102                 }\r
103                 // Now pass this streamreader to frmMain so that it can be used there.\r
104                 mainWindow.setStreamReader(thisDvd);\r
105 \r
106                 mainWindow.drp_dvdtitle.Items.Clear();\r
107                 if (thisDvd.Titles.Count != 0)\r
108                     mainWindow.drp_dvdtitle.Items.AddRange(thisDvd.Titles.ToArray());\r
109                 mainWindow.drp_dvdtitle.Text = "Automatic";\r
110                 mainWindow.drop_chapterFinish.Text = "Auto";\r
111                 mainWindow.drop_chapterStart.Text = "Auto";\r
112 \r
113                 // Now select the longest title\r
114                 if (thisDvd.Titles.Count != 0)\r
115                     hb_common_func.selectLongestTitle(mainWindow);\r
116 \r
117                 this.Close();\r
118             }\r
119             catch (Exception exc)\r
120             {\r
121                 MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
122                 this.Close();\r
123             }\r
124         }\r
125         private void closeWindowAfterError()\r
126         {\r
127             try\r
128             {\r
129                 if (this.InvokeRequired)\r
130                 {\r
131                     this.BeginInvoke(new UpdateUIHandler(closeWindowAfterError));\r
132                     return;\r
133                 }\r
134                 this.Close();\r
135             }\r
136             catch (Exception exc)\r
137             {\r
138                 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
139             }\r
140         }\r
141 \r
142         private void btn_cancel_Click(object sender, EventArgs e)\r
143         {\r
144             // This may seem like a long way of killing HandBrakeCLI, but for whatever reason,\r
145             // hbproc.kill/close just won't do the trick.\r
146             try\r
147             {\r
148                 string AppName = "HandBrakeCLI";\r
149 \r
150                 AppName = AppName.ToUpper();\r
151 \r
152                 System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();\r
153                 foreach (System.Diagnostics.Process proces in prs)\r
154                 {\r
155                     if (proces.ProcessName.ToUpper() == AppName)\r
156                     {\r
157                         proces.Refresh();\r
158                         if (!proces.HasExited)\r
159                             proces.Kill();\r
160                     }\r
161                 }\r
162             }\r
163             catch (Exception ex)\r
164             {\r
165                 MessageBox.Show("Unable to kill HandBrakeCLI.exe \nYou may need to manually kill HandBrakeCLI.exe using the Windows Task Manager if it does not close automatically within the next few minutes. \n\nError Information: \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
166             }\r
167         }\r
168     }\r
169 }