OSDN Git Service

LinGui: make Help->Guide work on windows/mingw
[handbrake-jp/handbrake-jp-git.git] / win / C# / Services / Scan.cs
index abecfec..3c62ec9 100644 (file)
@@ -16,12 +16,14 @@ namespace Handbrake.Services
     /// <summary>\r
     /// Scan a Source\r
     /// </summary>\r
-    public class Scan\r
+    public class ScanService\r
     {\r
+        /* Private Variables */\r
+\r
         /// <summary>\r
-        /// The information for this source\r
+        /// A Lock object\r
         /// </summary>\r
-        private DVD thisDvd;\r
+        private static readonly object locker = new object();\r
 \r
         /// <summary>\r
         /// The CLI data parser\r
@@ -34,11 +36,6 @@ namespace Handbrake.Services
         private StringBuilder logBuffer;\r
 \r
         /// <summary>\r
-        /// A Lock object\r
-        /// </summary>\r
-        private static object locker = new object();\r
-\r
-        /// <summary>\r
         /// The line number thats been read to in the log file\r
         /// </summary>\r
         private int logFilePosition;\r
@@ -48,10 +45,7 @@ namespace Handbrake.Services
         /// </summary>\r
         private Process hbProc;\r
 \r
-        /// <summary>\r
-        /// The Progress of the scan\r
-        /// </summary>\r
-        private string scanProgress;\r
+        /* Event Handlers */\r
 \r
         /// <summary>\r
         /// Scan has Started\r
@@ -68,10 +62,22 @@ namespace Handbrake.Services
         /// </summary>\r
         public event EventHandler ScanStatusChanged;\r
 \r
+        /* Properties */\r
+\r
+        /// <summary>\r
+        /// Gets a value indicating whether IsScanning.\r
+        /// </summary>\r
+        public bool IsScanning { get; private set; }\r
+\r
+        /// <summary>\r
+        /// Gets the Scan Status.\r
+        /// </summary>\r
+        public string ScanStatus { get; private set; }\r
+\r
         /// <summary>\r
-        /// Gets or sets a value indicating whether IsScanning.\r
+        /// Gets the Souce Data.\r
         /// </summary>\r
-        public bool IsScanning { get; set; }\r
+        public DVD SouceData { get; private set; }\r
 \r
         /// <summary>\r
         /// Gets ActivityLog.\r
@@ -81,58 +87,58 @@ namespace Handbrake.Services
             get\r
             {\r
                 if (IsScanning)\r
-                    return readData.Buffer;\r
+                    return readData.Buffer.ToString();\r
 \r
-                ReadFile();\r
-                return logBuffer.ToString();\r
+                if (logBuffer == null)\r
+                {\r
+                    ResetLogReader();\r
+                    ReadLastScanFile();  \r
+                }\r
+\r
+                return logBuffer != null ? logBuffer.ToString() : string.Empty;\r
             }\r
         }\r
 \r
+        /* Public Methods */\r
+\r
         /// <summary>\r
         /// Scan a Source Path.\r
         /// Title 0: scan all\r
         /// </summary>\r
         /// <param name="sourcePath">Path to the file to scan</param>\r
         /// <param name="title">int title number. 0 for scan all</param>\r
-        public void ScanSource(string sourcePath, int title)\r
+        public void Scan(string sourcePath, int title)\r
         {\r
-            Thread t = new Thread(unused => this.RunScan(sourcePath, title));\r
+            Thread t = new Thread(unused => this.ScanSource(sourcePath, title));\r
             t.Start();\r
         }\r
 \r
         /// <summary>\r
-        /// Object containing the information parsed in the scan.\r
+        /// Kill the scan\r
         /// </summary>\r
-        /// <returns>The DVD object containing the scan information</returns>\r
-        public DVD SouceData()\r
+        public void Stop()\r
         {\r
-            return this.thisDvd;\r
+            try\r
+            {\r
+                if (hbProc != null)\r
+                    hbProc.Kill();\r
+            }\r
+            catch (Exception ex)\r
+            {\r
+                MessageBox.Show(\r
+                    "Unable to kill HandBrakeCLI.exe \nYou may need to manually kill HandBrakeCLI.exe using the Windows Task Manager if it does not close automatically within the next few minutes. \n\nError Information: \n" +\r
+                    ex.Message, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+            }\r
         }\r
 \r
-        /// <summary>\r
-        /// Progress of the scan.\r
-        /// </summary>\r
-        /// <returns>The progress of the scan</returns>\r
-        public string ScanStatus()\r
-        {\r
-            return this.scanProgress;\r
-        }\r
-\r
-        /// <summary>\r
-        /// The Scan Process\r
-        /// </summary>\r
-        /// <returns>The CLI process</returns>\r
-        public Process ScanProcess()\r
-        {\r
-            return this.hbProc;\r
-        }\r
+        /* Private Methods */\r
 \r
         /// <summary>\r
         /// Start a scan for a given source path and title\r
         /// </summary>\r
         /// <param name="sourcePath">Path to the source file</param>\r
         /// <param name="title">the title number to look at</param>\r
-        private void RunScan(object sourcePath, int title)\r
+        private void ScanSource(object sourcePath, int title)\r
         {\r
             try\r
             {\r
@@ -159,52 +165,58 @@ namespace Handbrake.Services
                     extraArguments += " --scan ";\r
 \r
                 this.hbProc = new Process\r
-                                      {\r
-                                          StartInfo =\r
-                                              {\r
-                                                  FileName = handbrakeCLIPath,\r
-                                                  Arguments =\r
-                                                      String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title, extraArguments),\r
-                                                  RedirectStandardOutput = true,\r
-                                                  RedirectStandardError = true,\r
-                                                  UseShellExecute = false,\r
-                                                  CreateNoWindow = true\r
-                                              }\r
-                                      };\r
+                                  {\r
+                                      StartInfo =\r
+                                          {\r
+                                              FileName = handbrakeCLIPath,\r
+                                              Arguments =\r
+                                                  String.Format(@" -i ""{0}"" -t{1} {2} -v ", sourcePath, title,\r
+                                                                extraArguments),\r
+                                              RedirectStandardOutput = true,\r
+                                              RedirectStandardError = true,\r
+                                              UseShellExecute = false,\r
+                                              CreateNoWindow = true\r
+                                          }\r
+                                  };\r
 \r
                 // Start the Scan\r
                 this.hbProc.Start();\r
 \r
                 this.readData = new Parser(this.hbProc.StandardError.BaseStream);\r
                 this.readData.OnScanProgress += new ScanProgressEventHandler(this.OnScanProgress);\r
-                this.thisDvd = DVD.Parse(this.readData);\r
+                this.SouceData = DVD.Parse(this.readData);\r
 \r
                 // Write the Buffer out to file.\r
                 StreamWriter scanLog = new StreamWriter(dvdInfoPath);\r
                 scanLog.Write(this.readData.Buffer);\r
                 scanLog.Flush();\r
                 scanLog.Close();\r
+                logBuffer = readData.Buffer;\r
+\r
+                IsScanning = false;\r
 \r
                 if (this.ScanCompleted != null)\r
                     this.ScanCompleted(this, new EventArgs());\r
-                IsScanning = false;\r
             }\r
             catch (Exception exc)\r
             {\r
-                Console.WriteLine("frmMain.cs - scanProcess() " + exc);\r
+                frmExceptionWindow exceptionWindow = new frmExceptionWindow();\r
+                exceptionWindow.Setup("frmMain.cs - scanProcess() Error", exc.ToString());\r
+                exceptionWindow.ShowDialog();\r
             }\r
         }\r
 \r
         /// <summary>\r
         /// Read the log file\r
         /// </summary>\r
-        private void ReadFile()\r
+        private void ReadLastScanFile()\r
         {\r
             lock (locker)\r
             {\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_scan_log.txt");\r
                 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
 \r
@@ -240,8 +252,9 @@ namespace Handbrake.Services
                     sr.Close();\r
                     sr.Dispose();\r
                 }\r
-                catch (Exception)\r
+                catch (Exception exc)\r
                 {\r
+                    Console.WriteLine(exc.ToString());\r
                     ResetLogReader();\r
                 }\r
             }\r
@@ -264,7 +277,7 @@ namespace Handbrake.Services
         /// <param name="titleCount">the total number of titles</param>\r
         private void OnScanProgress(object sender, int currentTitle, int titleCount)\r
         {\r
-            this.scanProgress = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);\r
+            this.ScanStatus = string.Format("Processing Title: {0} of {1}", currentTitle, titleCount);\r
             if (this.ScanStatusChanged != null)\r
                 this.ScanStatusChanged(this, new EventArgs());\r
         }\r