OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmReadDVD.cs
index 23be024..21dfa65 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
@@ -5,203 +11,159 @@ using System.Data;
 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
-\r
-        string inputFile;\r
-\r
-        public frmReadDVD(string inputFile)\r
+        private string inputFile;\r
+        private frmMain mainWindow;\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)\r
         {\r
             InitializeComponent();\r
             this.inputFile = inputFile;\r
+            this.mainWindow = parent;\r
+            startScan();\r
         }\r
 \r
-        public void scan(string filename)\r
+        private void startScan()\r
         {\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.Arguments = query;\r
-            hbProc.StartInfo.UseShellExecute = false;\r
-            hbProc.Start();\r
-            System.IO.StreamReader errorReader = new System.IO.StreamReader(new System.IO.BufferedStream(hbProc.StandardError.BaseStream));\r
-            //rtf_dvdInfo.AppendText(errorReader.ReadToEnd());\r
-            hbProc.WaitForExit();\r
-            hbProc.Close();\r
-\r
-            String DvdData = errorReader.ReadToEnd();\r
-            DvdData = DvdData + "-- end --";\r
-\r
-            String[] DvdDataArr = DvdData.Split('\n');\r
-            int DvdDataSize = DvdDataArr.Length -1;\r
-            String line = "";\r
-            int counter = 0;\r
-\r
-            //\r
-            // Some varbiles used for parseing HandBrakes output\r
-            //\r
-\r
-            // DVD info stroage varibles\r
-            string titleData  = "";\r
-            string duationData  = "";\r
-            string sizeData  = "";\r
-            string cropdata  = "";\r
-            string chatperData  = "";\r
-            string audioData  = "";\r
-            string subtitleData  = "";\r
-\r
-            string fullTitleData  = "";\r
-\r
-            // Position Pointers\r
-            bool chapterPointer  = false;\r
-            bool audioPointer = false;\r
-            bool subtitlePointer = false;\r
-\r
-            // Error handling varibles\r
-            bool titleError = false;\r
-            bool readError = false;\r
-            \r
-            while (counter <= DvdDataSize)\r
+            try\r
             {\r
-                line = DvdDataArr[counter];\r
-                counter++;\r
-                 \r
-                // Get all the 1 liner data and set chaper potiner to true when done\r
-                if (line.Contains("exited.")){\r
-                    subtitlePointer = false;\r
-                    fullTitleData = titleData.Trim() + " ~ " + duationData.Trim() + " ~ " + sizeData.Trim() + " ~ " + cropdata.Trim() + " ~ " + chatperData.Trim() + " ~ " + audioData.Trim() + " ~ " + subtitleData.Trim();\r
-                    add(fullTitleData, titleData, duationData);\r
-                    counter++;\r
-                }else if (line.Contains("+ title")){\r
-                    if (titleData != "") {\r
-                        subtitlePointer = true;\r
-                        fullTitleData = titleData.Trim() + " ~ " + duationData.Trim() + " ~ " + sizeData.Trim() + " ~ " + cropdata.Trim() + " ~ " + chatperData.Trim() + " ~ " + audioData.Trim() + " ~ " + subtitleData.Trim();\r
-                        add(fullTitleData, titleData, duationData);\r
-                        counter = counter + 1;\r
-                    }\r
-                    titleData = line;\r
-                }else if (line.Contains("+ duration")) {\r
-                    duationData = line;\r
-                }else if (line.Contains("+ size")) {\r
-                    sizeData = line;\r
-                }else if (line.Contains("+ autocrop")) {\r
-                    cropdata = line;\r
-                }else if (line.Contains("+ chapters")) {\r
-                    chatperData = line;\r
-                    chapterPointer = true;\r
+                lbl_status.Visible = true;\r
+                ThreadPool.QueueUserWorkItem(startProc);\r
+            }\r
+            catch (Exception exc)\r
+            {\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
-                // Get all the chapter information in 1 varible\r
-                if (chapterPointer == true)\r
+                if (!File.Exists(dvdInfoPath))\r
                 {\r
-                    if (!line.Contains("+ audio"))\r
-                    {\r
-                        chapterPointer = false;\r
-                        audioPointer = true;\r
-                        audioData = line;\r
-                   }\r
-                    else \r
-                    {\r
-                        if (!chatperData.Equals(line))\r
-                        {\r
-                            chatperData = chatperData + " & " + line.Trim();\r
-                        }\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
-                 // Get all the audio channel information in 1 varible\r
-                if (audioPointer == true)\r
-                { \r
-                    if (line.Contains("+ subtitle"))\r
-                    {\r
-                        audioPointer = false;\r
-                        subtitlePointer = true;\r
-                        subtitleData = line;\r
-                    } \r
-                    else \r
-                    {\r
-                        if (!audioData.Equals(line))\r
-                        {\r
-                            audioData = audioData + " & " + line.Trim();\r
-                        }\r
-                    }\r
-                 }\r
-\r
-                 //Get all the subtitle data into 1 varible\r
-                 if (subtitlePointer == true)\r
-                 {\r
-                            if (line.Contains("+ subtitle")) \r
-                            {\r
-                                subtitleData = line;\r
-                            } else \r
-                            {\r
-                                if (!subtitleData.Equals(line))\r
-                                {\r
-                                    subtitleData = subtitleData + " & " + line.Trim();\r
-                                }\r
-                            }\r
-                  }\r
-\r
-                  // Handle some of Handbrakes Error outputs if they occur.\r
-                  if (line.Contains("No title"))\r
-                  {\r
-                    titleError = true;\r
-                  }\r
-\r
-                  if (line.Contains("***")) \r
-                  {\r
-                    readError = true;\r
-                  }\r
-\r
-                 // Display error messages for errors detected above.\r
-                 if (readError == true)\r
-                 {\r
-                     MessageBox.Show("Some DVD Title information may be missing however you may still be able to select your required title for encoding!", "Alert", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);\r
-                 }\r
-\r
-                 if (titleError == true)\r
-                 {\r
-                     MessageBox.Show("No Title(s) found. Please make sure you have selected a valid, non-copy protected source.", "Error", MessageBoxButtons.OK, MessageBoxIcon.Hand);\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
-        public void add(string fullTitleData, string titleData, string durationData)\r
+        }\r
+        private void updateUIElements()\r
         {\r
-\r
             try\r
             {\r
-                string t = titleData.Trim().Substring(8).Replace(":", "");\r
-                string d = durationData.Trim().Substring(12);\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
-                // Lets store the captured full title data as a string in the programs settings file.\r
-                // This can then be used by the DVD title dropdown menu to populate other fields.\r
+                mainWindow.drp_dvdtitle.Items.Clear();\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
-                Properties.Settings.Default.FullDVDInfo = Properties.Settings.Default.FullDVDInfo + " \n " + fullTitleData;\r
+                // Now select the longest title\r
+                if (thisDvd.Titles.Count != 0)\r
+                    hb_common_func.selectLongestTitle(mainWindow);\r
 \r
-                //Now lets add the info to the main form dropdowns\r
-                frmMain form = (frmMain)frmMain.ActiveForm;\r
-                string title = t + " " + " " + d + " ";\r
-                form.drp_dvdtitle.Items.Add(title);\r
+                this.Close();\r
             }\r
-            catch (Exception)\r
+            catch (Exception exc)\r
             {\r
-                // Don't really need to do anything about it.\r
+                MessageBox.Show("frmReadDVD.cs - updateUIElements " + exc.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                this.Close();\r
             }\r
         }\r
-\r
-\r
-        private void btn_ok_Click(object sender, EventArgs e)\r
+        private void closeWindowAfterError()\r
         {\r
-            scan(inputFile);\r
+            try\r
+            {\r
+                if (this.InvokeRequired)\r
+                {\r
+                    this.BeginInvoke(new UpdateUIHandler(closeWindowAfterError));\r
+                    return;\r
+                }\r
+                this.Close();\r
+            }\r
+            catch (Exception exc)\r
+            {\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
-       \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 AppName = "HandBrakeCLI";\r
+\r
+                AppName = AppName.ToUpper();\r
+\r
+                System.Diagnostics.Process[] prs = System.Diagnostics.Process.GetProcesses();\r
+                foreach (System.Diagnostics.Process proces in prs)\r
+                {\r
+                    if (proces.ProcessName.ToUpper() == AppName)\r
+                    {\r
+                        proces.Refresh();\r
+                        if (!proces.HasExited)\r
+                            proces.Kill();\r
+                    }\r
+                }\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show("Unable to kill HandBrakeCLI.exe \n\nError Information: \n" + ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
+        }\r
     }\r
 }
\ No newline at end of file