OSDN Git Service

Don't discard titles during scan just because of a read failure on one or more of...
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmReadDVD.cs
index 2c95731..8adfb1d 100644 (file)
@@ -5,204 +5,165 @@ 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
+\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 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
         {\r
             InitializeComponent();\r
             this.inputFile = inputFile;\r
+            this.mainWindow = parent;\r
+            this.dvdInfo = dvdInfoWindow;\r
+            startScan();\r
+            \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
-            hbProc.WaitForExit();\r
-            hbProc.Close();\r
-\r
-            //Parsing.DVD thisDvd = Parsing.DVD.Parse(errorReader);\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
+                //btn_skip.Visible = true;\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
+            }\r
+            catch (Exception exc)\r
+            {\r
+                if (Properties.Settings.Default.GuiDebug == "Checked")\r
+                {\r
+                    MessageBox.Show("frmReadDVD.cs - startScan " + exc.ToString());\r
                 }\r
-\r
-                // Get all the chapter information in 1 varible\r
-                if (chapterPointer == true)\r
+                else\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
+                    MessageBox.Show(Properties.Settings.Default.defaultError.ToString());\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
-\r
             }\r
         }\r
+   \r
+        private void updateUIElements()\r
+        {\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
+                this.Close();\r
+            }\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
+            }\r
+        }\r
+\r
+        Functions.CLI process = new Functions.CLI();\r
 \r
-        public void add(string fullTitleData, string titleData, string durationData)\r
+        private void startProc(object state)\r
         {\r
+            //string query = "-i " + '"' + inputFile + '"' + " -t0";\r
+            // hbProc = process.runCli(this, query, true, true, false, true);\r
 \r
             try\r
             {\r
-                string t = titleData.Trim().Substring(8).Replace(":", "");\r
-                string d = durationData.Trim().Substring(12);\r
+                string appPath = Application.StartupPath.ToString()+ "\\";\r
+                string strCmdLine = "cmd /c " + '"' + '"' + appPath + "hbcli.exe" + '"' +  " -i " + '"' + inputFile + '"' + " -t0 >" + '"'+ appPath + "dvdinfo.dat" + '"' + " 2>&1" + '"';\r
+                Process hbproc = Process.Start("CMD.exe", strCmdLine);\r
+                hbproc.WaitForExit();\r
+                hbproc.Dispose();\r
+                hbproc.Close();\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
+          \r
+                StreamReader sr = new StreamReader(appPath + "dvdinfo.dat");\r
+                thisDvd = Parsing.DVD.Parse(sr);\r
+                sr.Close();\r
 \r
-                Properties.Settings.Default.FullDVDInfo = Properties.Settings.Default.FullDVDInfo + " \n " + fullTitleData;\r
+                Console.ReadLine();\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
+                updateUIElements();\r
             }\r
-            catch (Exception)\r
+            catch (Exception exc)\r
             {\r
-                // Don't really need to do anything about it.\r
+                if (Properties.Settings.Default.GuiDebug == "Checked")\r
+                {\r
+                    MessageBox.Show("frmReadDVD.cs - startProc " + exc.ToString());\r
+                }\r
+                else\r
+                {\r
+                    MessageBox.Show(Properties.Settings.Default.defaultError.ToString());\r
+                }\r
             }\r
+\r
         }\r
 \r
+            //*********************************************************************************************************************************************\r
+            /*\r
+             * This has been temporily disabled due to the stderr issue\r
+             * \r
+             * \r
+            Parsing.Parser readData = new Parsing.Parser(hbProc.StandardError.BaseStream);\r
+            readData.OnScanProgress += Parser_OnScanProgress;\r
+            readData.OnReadLine += dvdInfo.HandleParsedData;\r
+            readData.OnReadToEnd += dvdInfo.HandleParsedData;\r
+\r
+            // Setup the parser\r
+            \r
+\r
+            if (cancel != 1)\r
+            {\r
+                updateUIElements();\r
+                process.killCLI();\r
+                process.closeCLI();\r
+            }\r
+            */\r
+            //*********************************************************************************************************************************************\r
+\r
 \r
-        private void btn_ok_Click(object sender, EventArgs e)\r
+        /*private void Parser_OnScanProgress(object Sender, int CurrentTitle, int TitleCount)\r
         {\r
-            scan(inputFile);\r
-        }\r
+            if (this.InvokeRequired)\r
+            {\r
+                this.BeginInvoke(new Parsing.ScanProgressEventHandler(Parser_OnScanProgress), new object[] { Sender, CurrentTitle, TitleCount });\r
+                return;\r
+            }\r
+            int progress = Convert.ToInt32(Convert.ToDouble(CurrentTitle) / Convert.ToDouble(TitleCount) * 100) + 1;\r
+            if (progress > 100)\r
+            {\r
+                progress = 100;\r
+            }\r
+            this.lbl_progress.Text = progress.ToString() + "%";\r
+        }*/\r
 \r
-       \r
     }\r
 }
\ No newline at end of file