OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmReadDVD.cs
index 4c62658..21dfa65 100644 (file)
@@ -23,18 +23,18 @@ namespace Handbrake
     {\r
         private string inputFile;\r
         private frmMain mainWindow;\r
-        private frmDvdInfo dvdInfo;\r
         private Parsing.DVD thisDvd;\r
         private delegate void UpdateUIHandler();\r
+        Process hbproc;\r
+        Functions.Common hb_common_func = new Functions.Common();\r
+        Functions.Encode process = new Functions.Encode();\r
 \r
-        public frmReadDVD(string inputFile, frmMain parent, frmDvdInfo dvdInfoWindow)\r
+        public frmReadDVD(string inputFile, frmMain parent)\r
         {\r
             InitializeComponent();\r
             this.inputFile = inputFile;\r
             this.mainWindow = parent;\r
-            this.dvdInfo = dvdInfoWindow;\r
             startScan();\r
-\r
         }\r
 \r
         private void startScan()\r
@@ -46,10 +46,51 @@ namespace Handbrake
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString());\r
+                MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
         }\r
+        private void startProc(object state)\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 hb_encode_log.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
+\r
+                using (hbproc = Process.Start(hbParseDvd))\r
+                {\r
+                    hbproc.WaitForExit();\r
+                }\r
+\r
+                if (!File.Exists(dvdInfoPath))\r
+                {\r
+                    throw new Exception("Unable to retrieve the DVD Info. dvdinfo.dat is missing. \nExpected location of dvdinfo.dat: \n" + dvdInfoPath);\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(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                closeWindowAfterError();\r
+            }\r
 \r
+        }\r
         private void updateUIElements()\r
         {\r
             try\r
@@ -63,99 +104,66 @@ namespace Handbrake
                 mainWindow.setStreamReader(thisDvd);\r
 \r
                 mainWindow.drp_dvdtitle.Items.Clear();\r
-                mainWindow.drp_dvdtitle.Items.AddRange(thisDvd.Titles.ToArray());\r
+                if (thisDvd.Titles.Count != 0)\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
+                if (thisDvd.Titles.Count != 0)\r
+                    hb_common_func.selectLongestTitle(mainWindow);\r
 \r
-                foreach (Handbrake.Parsing.Title x in thisDvd.Titles)\r
+                this.Close();\r
+            }\r
+            catch (Exception exc)\r
+            {\r
+                MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                this.Close();\r
+            }\r
+        }\r
+        private void closeWindowAfterError()\r
+        {\r
+            try\r
+            {\r
+                if (this.InvokeRequired)\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
+                    this.BeginInvoke(new UpdateUIHandler(closeWindowAfterError));\r
+                    return;\r
                 }\r
-\r
-\r
-                    mainWindow.drp_dvdtitle.SelectedItem = title2Select;\r
-\r
                 this.Close();\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString());\r
-                this.Close();\r
+                MessageBox.Show("frmReadDVD.cs - closeWindowAfterError - Unable to recover from a serious error. \n\n Error Information: \n " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
         }\r
 \r
-        Functions.CLI process = new Functions.CLI();\r
-\r
-        private void startProc(object state)\r
+        private void btn_cancel_Click(object sender, EventArgs e)\r
         {\r
+            // This may seem like a long way of killing HandBrakeCLI, but for whatever reason,\r
+            // hbproc.kill/close just won't do the trick.\r
             try\r
             {\r
-                string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
-                string dvdInfoPath = Path.Combine(Path.GetTempPath(), "dvdinfo.dat");\r
+                string AppName = "HandBrakeCLI";\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
+                AppName = AppName.ToUpper();\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
+                System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();\r
+                foreach (System.Diagnostics.Process proces in prs)\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
+                    if (proces.ProcessName.ToUpper() == AppName)\r
+                    {\r
+                        proces.Refresh();\r
+                        if (!proces.HasExited)\r
+                            proces.Kill();\r
+                    }\r
                 }\r
-\r
-                updateUIElements();\r
             }\r
-            catch (Exception exc)\r
+            catch (Exception ex)\r
             {\r
-                MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString());\r
-                this.Close();\r
+                MessageBox.Show("Unable to kill HandBrakeCLI.exe \n\nError Information: \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
             }\r
-\r
         }\r
     }\r
 }
\ No newline at end of file