OSDN Git Service

WinGui: Bug Fixes
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmReadDVD.cs
index f969f97..4c62658 100644 (file)
@@ -1,3 +1,9 @@
+/*  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
@@ -8,6 +14,7 @@ using System.Windows.Forms;
 using System.IO;\r
 using System.Threading;\r
 using System.Diagnostics;\r
+using System.Collections;\r
 \r
 \r
 namespace Handbrake\r
@@ -18,7 +25,6 @@ namespace Handbrake
         private frmMain mainWindow;\r
         private frmDvdInfo dvdInfo;\r
         private Parsing.DVD thisDvd;\r
-        private Process hbProc;\r
         private delegate void UpdateUIHandler();\r
 \r
         public frmReadDVD(string inputFile, frmMain parent, frmDvdInfo dvdInfoWindow)\r
@@ -27,66 +33,129 @@ namespace Handbrake
             this.inputFile = inputFile;\r
             this.mainWindow = parent;\r
             this.dvdInfo = dvdInfoWindow;\r
-            Parsing.Parser.OnScanProgress += Parser_OnScanProgress;\r
+            startScan();\r
+\r
         }\r
 \r
-        private void btn_ok_Click(object sender, EventArgs e)\r
+        private void startScan()\r
         {\r
-            btn_ok.Enabled = false;\r
-            lbl_pressOk.Visible = false;\r
-            lbl_progress.Text = "0%";\r
-            lbl_progress.Visible = true;\r
-            lbl_status.Visible = true;\r
-            // throw cli call and parsing on it's own thread\r
-            ThreadPool.QueueUserWorkItem(startProc);\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
+\r
         private void updateUIElements()\r
         {\r
-            if (this.InvokeRequired)\r
+            try\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
+                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.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
-            this.Close();\r
-        }\r
+                // Now select the longest title\r
+                int current_largest = 0;\r
+                Handbrake.Parsing.Title title2Select = thisDvd.Titles[0];\r
 \r
-        private void startProc(object state)\r
-        {\r
-            string query = "-i " + '"' + inputFile + '"' + " -t0";\r
-          \r
-            Functions.CLI process = new Functions.CLI();\r
-            hbProc = process.runCli(this, query, true, true, false, true);\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
 \r
-            Parsing.Parser readData = new Parsing.Parser(hbProc.StandardError.BaseStream);\r
-            hbProc.Close();\r
 \r
-            // Setup the parser\r
-            thisDvd = Parsing.DVD.Parse(readData);\r
+                    mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
 \r
-            updateUIElements();\r
+                this.Close();\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString());\r
+                this.Close();\r
+            }\r
         }\r
 \r
-        private void Parser_OnScanProgress(object Sender, int CurrentTitle, int TitleCount)\r
+        Functions.CLI process = new Functions.CLI();\r
+\r
+        private void startProc(object state)\r
         {\r
-            if (this.InvokeRequired)\r
+            try\r
             {\r
-                this.BeginInvoke(new Parsing.ScanProgressEventHandler(Parser_OnScanProgress), new object[] { Sender, CurrentTitle, TitleCount });\r
-                return;\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
+                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
+                }\r
+\r
+                updateUIElements();\r
             }\r
-            int progress = Convert.ToInt32(Convert.ToDouble(CurrentTitle) / Convert.ToDouble(TitleCount) * 100) + 1;\r
-            if (progress > 100)\r
+            catch (Exception exc)\r
             {\r
-                progress = 100;\r
+                MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString());\r
+                this.Close();\r
             }\r
-            this.lbl_progress.Text = progress.ToString() + "%";\r
-        }\r
 \r
+        }\r
     }\r
 }
\ No newline at end of file