OSDN Git Service

LinGui: make Help->Guide work on windows/mingw
[handbrake-jp/handbrake-jp-git.git] / win / C# / Services / Encode.cs
index ba19f18..a6da850 100644 (file)
@@ -21,6 +21,8 @@ namespace Handbrake.Services
     /// </summary>\r
     public class Encode\r
     {\r
+        /* Private Variables */\r
+\r
         /// <summary>\r
         /// An Encode Job\r
         /// </summary>\r
@@ -42,6 +44,18 @@ namespace Handbrake.Services
         private Timer windowTimer;\r
 \r
         /// <summary>\r
+        /// Gets The Process Handle\r
+        /// </summary>\r
+        private IntPtr processHandle;\r
+\r
+        /// <summary>\r
+        /// Gets the Process ID\r
+        /// </summary>\r
+        private int processID;\r
+\r
+        /* Event Handlers */\r
+\r
+        /// <summary>\r
         /// Fires when a new CLI Job starts\r
         /// </summary>\r
         public event EventHandler EncodeStarted;\r
@@ -51,25 +65,19 @@ namespace Handbrake.Services
         /// </summary>\r
         public event EventHandler EncodeEnded;\r
 \r
+        /* Properties */\r
+\r
         /// <summary>\r
         /// Gets or sets The HB Process\r
         /// </summary>\r
         public Process HbProcess { get; set; }\r
 \r
         /// <summary>\r
-        /// Gets or sets The Process Handle\r
+        /// Gets a value indicating whether IsEncoding.\r
         /// </summary>\r
-        public IntPtr ProcessHandle { get; set; }\r
+        public bool IsEncoding { get; private set; }\r
 \r
-        /// <summary>\r
-        /// Gets or sets a value indicating whether HandBrakeCLI.exe is running\r
-        /// </summary>\r
-        public bool IsEncoding { get; set; }\r
-\r
-        /// <summary>\r
-        /// Gets or sets the Process ID\r
-        /// </summary>\r
-        public int ProcessID { get; set; }\r
+        /* Public Methods */\r
 \r
         /// <summary>\r
         /// Gets ActivityLog.\r
@@ -78,10 +86,13 @@ namespace Handbrake.Services
         {\r
             get\r
             {\r
-                if (logBuffer != null)\r
-                    return logBuffer.ToString();\r
+                if (logBuffer == null)\r
+                {\r
+                    ResetLogReader();\r
+                    ReadFile(null);\r
+                }\r
 \r
-                return string.Empty;\r
+                return logBuffer != null ? logBuffer.ToString() : string.Empty;\r
             }\r
         }\r
 \r
@@ -93,7 +104,7 @@ namespace Handbrake.Services
         /// </param>\r
         public void CreatePreviewSample(string query)\r
         {\r
-            this.Run(new Job {Query = query});\r
+            this.Run(new Job { Query = query }, true);\r
         }\r
 \r
         /// <summary>\r
@@ -110,8 +121,6 @@ namespace Handbrake.Services
 \r
             if (this.EncodeEnded != null)\r
                 this.EncodeEnded(this, new EventArgs());\r
-\r
-            IsEncoding = false;\r
         }\r
 \r
         /// <summary>\r
@@ -121,18 +130,16 @@ namespace Handbrake.Services
         /// </summary>\r
         public void SafelyClose()\r
         {\r
-            if ((int)this.ProcessHandle == 0)\r
+            if ((int)this.processHandle == 0)\r
                 return;\r
 \r
             // Allow the CLI to exit cleanly\r
-            Win32.SetForegroundWindow((int)this.ProcessHandle);\r
+            Win32.SetForegroundWindow((int)this.processHandle);\r
             SendKeys.Send("^C");\r
             SendKeys.Flush();\r
 \r
             // HbProcess.StandardInput.AutoFlush = true;\r
             // HbProcess.StandardInput.WriteLine("^C");\r
-\r
-            IsEncoding = false;\r
         }\r
 \r
         /// <summary>\r
@@ -141,51 +148,50 @@ namespace Handbrake.Services
         /// <param name="encJob">\r
         /// The enc Job.\r
         /// </param>\r
-        protected void Run(Job encJob)\r
+        /// <param name="RequireStandardOuput">\r
+        /// Set to True to show no window and force standard output redirect\r
+        /// </param>\r
+        protected void Run(Job encJob, bool RequireStandardOuput)\r
         {\r
             this.job = encJob;\r
             try\r
             {\r
-                IsEncoding = true;\r
-\r
                 ResetLogReader();\r
+                IsEncoding = true;\r
 \r
                 string handbrakeCLIPath = Path.Combine(Application.StartupPath, "HandBrakeCLI.exe");\r
-                string logPath =\r
-                    Path.Combine(\r
-                        Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs",\r
-                        "last_encode_log.txt");\r
+                string logPath = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs", "last_encode_log.txt");\r
                 string strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query, logPath);\r
                 var cliStart = new ProcessStartInfo("CMD.exe", strCmdLine);\r
 \r
-                if (Settings.Default.enocdeStatusInGui)\r
+                if (Settings.Default.enocdeStatusInGui || RequireStandardOuput)\r
                 {\r
                     cliStart.RedirectStandardOutput = true;\r
                     cliStart.UseShellExecute = false;\r
-                    if (!Settings.Default.showCliForInGuiEncodeStatus)\r
+                    if (!Settings.Default.showCliForInGuiEncodeStatus || RequireStandardOuput)\r
                         cliStart.CreateNoWindow = true;\r
                 }\r
                 if (Settings.Default.cli_minimized)\r
                     cliStart.WindowStyle = ProcessWindowStyle.Minimized;\r
 \r
                 Process[] before = Process.GetProcesses(); // Get a list of running processes before starting.\r
-                this.HbProcess = Process.Start(cliStart);\r
-                this.ProcessID = Main.GetCliProcess(before);\r
-\r
-                // Fire the Encode Started Event\r
-                if (this.EncodeStarted != null)\r
-                    this.EncodeStarted(this, new EventArgs());\r
-\r
-                if (this.HbProcess != null)\r
-                    this.ProcessHandle = this.HbProcess.MainWindowHandle; // Set the process Handle\r
+                HbProcess = Process.Start(cliStart);\r
+                this.processID = Main.GetCliProcess(before);\r
 \r
+                if (HbProcess != null)\r
+                    this.processHandle = HbProcess.MainWindowHandle; // Set the process Handle\r
+      \r
                 // Start the Log Monitor\r
                 windowTimer = new Timer(new TimerCallback(ReadFile), null, 1000, 1000);\r
 \r
                 // Set the process Priority\r
                 Process hbCliProcess = null;\r
-                if (this.ProcessID != -1)\r
-                    hbCliProcess = Process.GetProcessById(this.ProcessID);\r
+                if (this.processID != -1)\r
+                {\r
+                    hbCliProcess = Process.GetProcessById(this.processID);\r
+                    hbCliProcess.EnableRaisingEvents = true;\r
+                    hbCliProcess.Exited += new EventHandler(HbProcess_Exited);\r
+                }\r
 \r
                 if (hbCliProcess != null)\r
                     switch (Settings.Default.processPriority)\r
@@ -209,6 +215,10 @@ namespace Handbrake.Services
                             hbCliProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
                             break;\r
                     }\r
+\r
+                // Fire the Encode Started Event\r
+                if (this.EncodeStarted != null)\r
+                    this.EncodeStarted(this, new EventArgs());\r
             }\r
             catch (Exception exc)\r
             {\r
@@ -222,6 +232,20 @@ namespace Handbrake.Services
         }\r
 \r
         /// <summary>\r
+        /// The HandBrakeCLI process has exited.\r
+        /// </summary>\r
+        /// <param name="sender">\r
+        /// The sender.\r
+        /// </param>\r
+        /// <param name="e">\r
+        /// The EventArgs.\r
+        /// </param>\r
+        private void HbProcess_Exited(object sender, EventArgs e)\r
+        {\r
+            IsEncoding = false;\r
+        }\r
+\r
+        /// <summary>\r
         /// Function to run the CLI directly rather than via CMD\r
         /// TODO: Code to handle the Log data has yet to be written.\r
         /// TODO: Code to handle the % / ETA info has to be written.\r
@@ -237,24 +261,25 @@ namespace Handbrake.Services
                     this.EncodeStarted(this, new EventArgs());\r
 \r
                 IsEncoding = true;\r
+\r
                 ResetLogReader();\r
 \r
                 // Setup the job\r
                 string handbrakeCLIPath = Path.Combine(Environment.CurrentDirectory, "HandBrakeCLI.exe");\r
                 HbProcess = new Process\r
-                                 {\r
-                                     StartInfo =\r
-                                         {\r
-                                             FileName = handbrakeCLIPath,\r
-                                             Arguments = query,\r
-                                             UseShellExecute = false,\r
-                                             RedirectStandardOutput = true,\r
-                                             RedirectStandardError = true,\r
-                                             RedirectStandardInput = true,\r
-                                             CreateNoWindow = false,\r
-                                             WindowStyle = ProcessWindowStyle.Minimized\r
-                                         }\r
-                                 };\r
+                                {\r
+                                    StartInfo =\r
+                                        {\r
+                                            FileName = handbrakeCLIPath,\r
+                                            Arguments = query,\r
+                                            UseShellExecute = false,\r
+                                            RedirectStandardOutput = true,\r
+                                            RedirectStandardError = true,\r
+                                            RedirectStandardInput = true,\r
+                                            CreateNoWindow = false,\r
+                                            WindowStyle = ProcessWindowStyle.Minimized\r
+                                        }\r
+                                };\r
 \r
                 // Setup event handlers for rediected data\r
                 HbProcess.ErrorDataReceived += new DataReceivedEventHandler(HbProcErrorDataReceived);\r
@@ -291,8 +316,8 @@ namespace Handbrake.Services
                 }\r
 \r
                 // Set the class items\r
-                this.ProcessID = HbProcess.Id;\r
-                this.ProcessHandle = HbProcess.Handle;\r
+                this.processID = HbProcess.Id;\r
+                this.processHandle = HbProcess.Handle;\r
             }\r
             catch (Exception exc)\r
             {\r
@@ -305,17 +330,15 @@ namespace Handbrake.Services
         /// </summary>\r
         protected void Finish()\r
         {\r
-            if (this.EncodeEnded != null)\r
-                this.EncodeEnded(this, new EventArgs());\r
-\r
-            IsEncoding = false;\r
-\r
             if (!IsEncoding)\r
             {\r
                 windowTimer.Dispose();\r
                 ReadFile(null);\r
             }\r
 \r
+            if (this.EncodeEnded != null)\r
+                this.EncodeEnded(this, new EventArgs());\r
+\r
             // Growl\r
             if (Settings.Default.growlQueue)\r
                 GrowlCommunicator.Notify("Queue Completed", "Put down that cocktail...\nyour Handbrake queue is done.");\r
@@ -356,7 +379,8 @@ namespace Handbrake.Services
         {\r
             try\r
             {\r
-                string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+                string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
+                                "\\HandBrake\\logs";\r
                 string logPath = Path.Combine(logDir, "last_encode_log.txt");\r
 \r
                 var reader = new StreamReader(File.Open(logPath, FileMode.Open, FileAccess.Read, FileShare.Read));\r
@@ -436,7 +460,8 @@ namespace Handbrake.Services
             {\r
                 // last_encode_log.txt is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it (Not even in read only mode),\r
                 // we'll need to make a copy of it.\r
-                string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+                string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +\r
+                                "\\HandBrake\\logs";\r
                 string logFile = Path.Combine(logDir, "last_encode_log.txt");\r
                 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
 \r
@@ -456,7 +481,7 @@ namespace Handbrake.Services
                     }\r
 \r
                     // Put the Query and User Generated Query Flag on the log.\r
-                    if (logFilePosition == 0)\r
+                    if (logFilePosition == 0 && job.Query != null)\r
                     {\r
                         logBuffer.AppendLine("### CLI Query: " + job.Query);\r
                         logBuffer.AppendLine("### User Query: " + job.CustomQuery);\r