OSDN Git Service

WinGui: Bug Fixes
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmReadDVD.cs
index 49197ea..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
@@ -27,7 +34,7 @@ namespace Handbrake
             this.mainWindow = parent;\r
             this.dvdInfo = dvdInfoWindow;\r
             startScan();\r
-            \r
+\r
         }\r
 \r
         private void startScan()\r
@@ -39,17 +46,10 @@ namespace Handbrake
             }\r
             catch (Exception exc)\r
             {\r
-                if (Properties.Settings.Default.GuiDebug == "Checked")\r
-                {\r
-                    MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString());\r
-                }\r
-                else\r
-                {\r
-                    MessageBox.Show(Properties.Settings.Default.defaultError.ToString());\r
-                }\r
+                MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString());\r
             }\r
         }\r
-   \r
+\r
         private void updateUIElements()\r
         {\r
             try\r
@@ -67,19 +67,51 @@ namespace Handbrake
                 mainWindow.drp_dvdtitle.Text = "Automatic";\r
                 mainWindow.drop_chapterFinish.Text = "Auto";\r
                 mainWindow.drop_chapterStart.Text = "Auto";\r
-  \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
+\r
+\r
+                    mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
+\r
                 this.Close();\r
             }\r
-            catch(Exception exc)\r
+            catch (Exception exc)\r
             {\r
-                if (Properties.Settings.Default.GuiDebug == "Checked")\r
-                {\r
-                    MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString());\r
-                }\r
-                else\r
-                {\r
-                    MessageBox.Show(Properties.Settings.Default.defaultError.ToString());\r
-                }\r
+                MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString());\r
+                this.Close();\r
             }\r
         }\r
 \r
@@ -89,30 +121,39 @@ namespace Handbrake
         {\r
             try\r
             {\r
-                string appPath = Application.StartupPath.ToString()+ "\\";\r
-                string strCmdLine = "cmd /c " + '"' + '"' + appPath + "HandBrakeCLI.exe" + '"' + " -i " + '"' + inputFile + '"' + " -t0 -v >" + '"' + appPath + "dvdinfo.dat" + '"' + " 2>&1" + '"';\r
-                Process hbproc = Process.Start("CMD.exe", strCmdLine);\r
-                hbproc.WaitForExit();\r
-                hbproc.Dispose();\r
-                hbproc.Close();\r
-\r
-          \r
-                StreamReader sr = new StreamReader(appPath + "dvdinfo.dat");\r
-                thisDvd = Parsing.DVD.Parse(sr);\r
-                sr.Close();\r
+                string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
+                string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat");\r
 \r
-                updateUIElements();\r
-            }\r
-            catch (Exception exc)\r
-            {\r
-                if (Properties.Settings.Default.GuiDebug == "Checked")\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
-                    MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString());\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
-                else\r
+\r
+                using (StreamReader sr = new StreamReader(dvdInfoPath))\r
                 {\r
-                    MessageBox.Show(Properties.Settings.Default.defaultError.ToString());\r
+                    thisDvd = Parsing.DVD.Parse(sr);\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