+/* frmReadDVD.cs $\r
+ \r
+ This file is part of the HandBrake source code.\r
+ Homepage: <http://handbrake.fr>.\r
+ It may be used under the terms of the GNU General Public License. */\r
+\r
using System;\r
using System.Collections.Generic;\r
using System.ComponentModel;\r
using System.Drawing;\r
using System.Text;\r
using System.Windows.Forms;\r
+using System.IO;\r
+using System.Threading;\r
+using System.Diagnostics;\r
+using System.Collections;\r
+\r
\r
namespace Handbrake\r
{\r
public partial class frmReadDVD : Form\r
{\r
+ private string inputFile;\r
+ private frmMain mainWindow;\r
+ private Parsing.DVD thisDvd;\r
+ private delegate void UpdateUIHandler();\r
\r
- string inputFile;\r
-\r
- public frmReadDVD(string inputFile)\r
+ public frmReadDVD(string inputFile, frmMain parent)\r
{\r
InitializeComponent();\r
this.inputFile = inputFile;\r
+ this.mainWindow = parent;\r
+ startScan();\r
+\r
}\r
\r
- private void frmReadDVD_Load(object sender, EventArgs e)\r
+ private void startScan()\r
{\r
- //start(inputFile);\r
+ try\r
+ {\r
+ lbl_status.Visible = true;\r
+ ThreadPool.QueueUserWorkItem(startProc);\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString());\r
+ }\r
}\r
\r
- public void start(string filename)\r
+ private void updateUIElements()\r
{\r
- MessageBox.Show(filename);\r
- string query = "-i " + '"' + filename + '"' + " -t0";\r
- System.Diagnostics.Process hbProc = new System.Diagnostics.Process();\r
- hbProc.StartInfo.FileName = "hbcli.exe";\r
- hbProc.StartInfo.RedirectStandardOutput = true;\r
- hbProc.StartInfo.RedirectStandardError = true;\r
- hbProc.StartInfo.StandardOutputEncoding = System.Text.Encoding.UTF8;\r
- hbProc.StartInfo.StandardErrorEncoding = System.Text.Encoding.UTF8;\r
- hbProc.StartInfo.Arguments = query;\r
- hbProc.StartInfo.UseShellExecute = false;\r
- hbProc.Start();\r
-\r
- while (hbProc.StandardOutput.BaseStream.CanRead && !hbProc.HasExited)\r
+ try\r
+ {\r
+ if (this.InvokeRequired)\r
+ {\r
+ this.BeginInvoke(new UpdateUIHandler(updateUIElements));\r
+ return;\r
+ }\r
+ // Now pass this streamreader to frmMain so that it can be used there.\r
+ mainWindow.setStreamReader(thisDvd);\r
+\r
+ mainWindow.drp_dvdtitle.Items.Clear();\r
+ mainWindow.drp_dvdtitle.Items.AddRange(thisDvd.Titles.ToArray());\r
+ mainWindow.drp_dvdtitle.Text = "Automatic";\r
+ mainWindow.drop_chapterFinish.Text = "Auto";\r
+ mainWindow.drop_chapterStart.Text = "Auto";\r
+\r
+ // Now select the longest title\r
+ int current_largest = 0;\r
+ Handbrake.Parsing.Title title2Select = thisDvd.Titles[0];\r
+\r
+ foreach (Handbrake.Parsing.Title x in thisDvd.Titles)\r
+ {\r
+ string title = x.ToString();\r
+ if (title != "Automatic")\r
+ {\r
+ string[] y = title.Split(' ');\r
+ string time = y[1].Replace("(", "").Replace(")", "");\r
+ string[] z = time.Split(':');\r
+\r
+ int hours = int.Parse(z[0]) * 60 * 60;\r
+ int minutes = int.Parse(z[1]) * 60;\r
+ int seconds = int.Parse(z[2]);\r
+ int total_sec = hours + minutes + seconds;\r
+\r
+ if (current_largest == 0)\r
+ {\r
+ current_largest = hours + minutes + seconds;\r
+ title2Select = x;\r
+ }\r
+ else\r
+ {\r
+ if (total_sec > current_largest)\r
+ {\r
+ current_largest = total_sec;\r
+ title2Select = x;\r
+ }\r
+ }\r
+ }\r
+ }\r
+\r
+ mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
+\r
+ this.Close();\r
+ }\r
+ catch (Exception exc)\r
{\r
- MessageBox.Show(hbProc.StandardOutput.ReadLine());\r
- MessageBox.Show(hbProc.StandardError.ReadLine());\r
- Console.Read();\r
+ MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString());\r
+ this.Close();\r
}\r
}\r
\r
- private void btn_ok_Click(object sender, EventArgs e)\r
+ Functions.CLI process = new Functions.CLI();\r
+\r
+ private void startProc(object state)\r
{\r
- start(inputFile);\r
- }\r
+ try\r
+ {\r
+ string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
+ string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat");\r
+\r
+ // Make we don't pick up a stale dvdinfo.dat (and that we have rights to the file)\r
+ if (File.Exists(dvdInfoPath))\r
+ File.Delete(dvdInfoPath);\r
+\r
+ string strCmdLine = String.Format(@"cmd /c """"{0}"" -i ""{1}"" -t0 -v >""{2}"" 2>&1""", handbrakeCLIPath, inputFile, dvdInfoPath);\r
+\r
+ ProcessStartInfo hbParseDvd = new ProcessStartInfo("CMD.exe", strCmdLine);\r
+ hbParseDvd.WindowStyle = ProcessWindowStyle.Hidden;\r
+ using (Process hbproc = Process.Start(hbParseDvd))\r
+ {\r
+ hbproc.WaitForExit();\r
+ // TODO: Verify exit code if the CLI supports it properly\r
+ }\r
\r
- \r
+ if (!File.Exists(dvdInfoPath))\r
+ {\r
+ throw new Exception("Unable to retrieve the DVD Info. dvdinfo.dat is missing.");\r
+ }\r
+\r
+ using (StreamReader sr = new StreamReader(dvdInfoPath))\r
+ {\r
+ thisDvd = Parsing.DVD.Parse(sr);\r
+ sr.Close();\r
+ sr.Dispose();\r
+ }\r
+\r
+ updateUIElements();\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString());\r
+ this.Close();\r
+ }\r
+\r
+ }\r
}\r
}
\ No newline at end of file