OSDN Git Service

WinGui:
[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 frmDvdInfo dvdInfo;\r
27         private Parsing.DVD thisDvd;\r
28         private delegate void UpdateUIHandler();\r
29 \r
30         public frmReadDVD(string inputFile, frmMain parent, frmDvdInfo dvdInfoWindow)\r
31         {\r
32             InitializeComponent();\r
33             this.inputFile = inputFile;\r
34             this.mainWindow = parent;\r
35             this.dvdInfo = dvdInfoWindow;\r
36             startScan();\r
37 \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());\r
50             }\r
51         }\r
52 \r
53         private void updateUIElements()\r
54         {\r
55             try\r
56             {\r
57                 if (this.InvokeRequired)\r
58                 {\r
59                     this.BeginInvoke(new UpdateUIHandler(updateUIElements));\r
60                     return;\r
61                 }\r
62                 // Now pass this streamreader to frmMain so that it can be used there.\r
63                 mainWindow.setStreamReader(thisDvd);\r
64 \r
65                 mainWindow.drp_dvdtitle.Items.Clear();\r
66                 mainWindow.drp_dvdtitle.Items.AddRange(thisDvd.Titles.ToArray());\r
67                 mainWindow.drp_dvdtitle.Text = "Automatic";\r
68                 mainWindow.drop_chapterFinish.Text = "Auto";\r
69                 mainWindow.drop_chapterStart.Text = "Auto";\r
70 \r
71                 // Now select the longest title\r
72                 int current_largest = 0;\r
73                 Handbrake.Parsing.Title title2Select = thisDvd.Titles[0];\r
74 \r
75                 foreach (Handbrake.Parsing.Title x in thisDvd.Titles)\r
76                 {\r
77                     string title = x.ToString();\r
78                     if (title != "Automatic")\r
79                     {\r
80                         string[] y = title.Split(' ');\r
81                         string time = y[1].Replace("(", "").Replace(")", "");\r
82                         string[] z = time.Split(':');\r
83 \r
84                         int hours = int.Parse(z[0]) * 60 * 60;\r
85                         int minutes = int.Parse(z[1]) * 60;\r
86                         int seconds = int.Parse(z[2]);\r
87                         int total_sec = hours + minutes + seconds;\r
88 \r
89                         if (current_largest == 0)\r
90                         {\r
91                             current_largest = hours + minutes + seconds;\r
92                             title2Select = x;\r
93                         }\r
94                         else\r
95                         {\r
96                             if (total_sec > current_largest)\r
97                             {\r
98                                 current_largest = total_sec;\r
99                                 title2Select = x;\r
100                             }\r
101                         }\r
102                     }\r
103             \r
104                 }\r
105 \r
106 \r
107                     mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
108 \r
109                 this.Close();\r
110             }\r
111             catch (Exception exc)\r
112             {\r
113                 MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString());\r
114             }\r
115         }\r
116 \r
117         Functions.CLI process = new Functions.CLI();\r
118 \r
119         private void startProc(object state)\r
120         {\r
121             try\r
122             {\r
123                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
124                 string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat");\r
125 \r
126                 // Make we don't pick up a stale dvdinfo.dat (and that we have rights to the file)\r
127                 if (File.Exists(dvdInfoPath))\r
128                     File.Delete(dvdInfoPath);\r
129 \r
130                 string strCmdLine = String.Format(@"cmd /c """"{0}"" -i ""{1}"" -t0 -v >""{2}"" 2>&1""", handbrakeCLIPath, inputFile, dvdInfoPath);\r
131 \r
132                 ProcessStartInfo hbParseDvd = new ProcessStartInfo("CMD.exe", strCmdLine);\r
133                 hbParseDvd.WindowStyle = ProcessWindowStyle.Hidden;\r
134                 using (Process hbproc = Process.Start(hbParseDvd))\r
135                 {\r
136                     hbproc.WaitForExit();\r
137                     // TODO: Verify exit code if the CLI supports it properly\r
138                 } \r
139 \r
140                 if (!File.Exists(dvdInfoPath))\r
141                 {\r
142                     throw new Exception("Unable to retrieve the DVD Info. dvdinfo.dat is missing.");\r
143                 }\r
144 \r
145                 using (StreamReader sr = new StreamReader(dvdInfoPath))\r
146                 {\r
147                     thisDvd = Parsing.DVD.Parse(sr);\r
148                 }\r
149 \r
150                 updateUIElements();\r
151             }\r
152             catch (Exception exc)\r
153             {\r
154                 MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString());\r
155             }\r
156 \r
157         }\r
158     }\r
159 }