OSDN Git Service

LinGui: make Help->Guide work on windows/mingw
[handbrake-jp/handbrake-jp-git.git] / win / C# / Services / Encode.cs
index e4b9e65..a6da850 100644 (file)
@@ -3,8 +3,6 @@
     Homepage: <http://handbrake.fr/>.\r
     It may be used under the terms of the GNU General Public License. */\r
 \r
-using System.Diagnostics.CodeAnalysis;\r
-\r
 namespace Handbrake.Services\r
 {\r
     using System;\r
@@ -23,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
@@ -44,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
@@ -53,29 +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
-        /// </summary>\r
-        public IntPtr ProcessHandle { get; set; }\r
-\r
-        /// <summary>\r
-        /// Gets or sets a value indicating whether IsEncoding.\r
+        /// Gets a value indicating whether IsEncoding.\r
         /// </summary>\r
-        public bool IsEncoding\r
-        {\r
-            get;\r
-            set;\r
-        }\r
+        public bool IsEncoding { get; private 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
@@ -84,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
@@ -99,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
@@ -125,11 +130,11 @@ 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
@@ -143,82 +148,77 @@ 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
                 ResetLogReader();\r
-\r
-                if (this.EncodeStarted != null)\r
-                    this.EncodeStarted(this, new EventArgs());\r
-\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 strCmdLine = String.Format(@" /C """"{0}"" {1} 2>""{2}"" """, handbrakeCLIPath, encJob.Query,\r
-                                                  logPath);\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
-                Process startProcess = Process.Start(cliStart);\r
-\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 (startProcess != null)\r
-                    this.ProcessHandle = startProcess.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
-                if (this.ProcessID != -1)\r
+                Process hbCliProcess = null;\r
+                if (this.processID != -1)\r
                 {\r
-                    HbProcess = Process.GetProcessById(this.ProcessID);\r
-                    HbProcess.EnableRaisingEvents = true;\r
-                    HbProcess.Exited += new EventHandler(HbProcess_Exited);\r
+                    hbCliProcess = Process.GetProcessById(this.processID);\r
+                    hbCliProcess.EnableRaisingEvents = true;\r
+                    hbCliProcess.Exited += new EventHandler(HbProcess_Exited);\r
                 }\r
 \r
-                if (HbProcess != null)\r
+                if (hbCliProcess != null)\r
                     switch (Settings.Default.processPriority)\r
                     {\r
                         case "Realtime":\r
-                            HbProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
+                            hbCliProcess.PriorityClass = ProcessPriorityClass.RealTime;\r
                             break;\r
                         case "High":\r
-                            HbProcess.PriorityClass = ProcessPriorityClass.High;\r
+                            hbCliProcess.PriorityClass = ProcessPriorityClass.High;\r
                             break;\r
                         case "Above Normal":\r
-                            HbProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
+                            hbCliProcess.PriorityClass = ProcessPriorityClass.AboveNormal;\r
                             break;\r
                         case "Normal":\r
-                            HbProcess.PriorityClass = ProcessPriorityClass.Normal;\r
+                            hbCliProcess.PriorityClass = ProcessPriorityClass.Normal;\r
                             break;\r
                         case "Low":\r
-                            HbProcess.PriorityClass = ProcessPriorityClass.Idle;\r
+                            hbCliProcess.PriorityClass = ProcessPriorityClass.Idle;\r
                             break;\r
                         default:\r
-                            HbProcess.PriorityClass = ProcessPriorityClass.BelowNormal;\r
+                            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
@@ -316,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
@@ -330,15 +330,15 @@ namespace Handbrake.Services
         /// </summary>\r
         protected void Finish()\r
         {\r
-            if (this.EncodeEnded != null)\r
-                this.EncodeEnded(this, new EventArgs());\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
@@ -481,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