OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmActivityWindow.cs
index 9f9f411..9e26170 100644 (file)
@@ -6,6 +6,7 @@
 \r
 using System;\r
 using System.ComponentModel;\r
+using System.Text;\r
 using System.Windows.Forms;\r
 using System.IO;\r
 using System.Threading;\r
@@ -18,130 +19,113 @@ namespace Handbrake
 {\r
     public partial class frmActivityWindow : Form\r
     {\r
-        private delegate void setTextCallback(string text);\r
-        private String _readFile;\r
-        private readonly EncodeAndQueueHandler _encodeQueue;\r
-        private int _position;  // Position in the arraylist reached by the current log output in the rtf box.\r
-        private readonly frmMain _mainWin;\r
-        private Boolean _lastUpdate;\r
-        private Boolean fileNotFoundQuickFix;\r
+        private delegate void setTextCallback(StringBuilder text);\r
+        private delegate void setTextClearCallback();\r
+        private static int _position;\r
+        private static string _lastMode;\r
+        private static string _currentMode;\r
 \r
-        public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)\r
+        public frmActivityWindow()\r
         {\r
             InitializeComponent();\r
+        }\r
+        private void NewActivityWindow_Load(object sender, EventArgs e)\r
+        {\r
+            SetScanMode();\r
+            Thread monitor = new Thread(LogMonitor);\r
 \r
-            _encodeQueue = eh;\r
-            _mainWin = mw;\r
-\r
-            fileNotFoundQuickFix = false;\r
-\r
-            if (file == "last_scan_log.txt")\r
-                SetLogView(true);\r
-            else\r
-                SetLogView(false);\r
-\r
-            // Start a new thread which will montior and keep the log window up to date if required/\r
             try\r
             {\r
-                Thread monitor = new Thread(AutoUpdate) { IsBackground = true };\r
                 monitor.Start();\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("startLogThread(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+                MessageBox.Show(exc.ToString());\r
             }\r
         }\r
 \r
-        /// <summary>\r
-        /// Set the view which the Log window displays.\r
-        /// Scan = true;\r
-        /// Encode = false;\r
-        /// </summary>\r
-        /// <param name="scan">Boolean. Scan = true</param>\r
-        public void SetLogView(Boolean scan)\r
+        private void LogMonitor()\r
         {\r
-            // Reset\r
-            _position = 0;\r
-            rtf_actLog.Text = String.Empty;\r
+            while (true)\r
+            {\r
+                // Perform a reset if require.\r
+                // If we have switched to a different log file, we want to start from the beginning.\r
+                if (SetLogFile != _lastMode)\r
+                {\r
+                    _position = 0;\r
+                    ClearWindowText();\r
+                }\r
 \r
-            // Print the log header\r
-            rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
-            rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
-            rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
-            rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
-            rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
-            rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
-            rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
-            rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
-            rtf_actLog.AppendText("#########################################\n\n");\r
+                // Perform the window update\r
+                switch (SetLogFile)\r
+                {\r
+                    case "last_scan_log.txt":\r
+                        AppendWindowText(ReadFile("last_scan_log.txt"));\r
+                        _lastMode = "last_scan_log.txt";\r
+                        break;\r
+                    case "last_encode_log.txt":\r
+                        AppendWindowText(ReadFile("last_encode_log.txt"));\r
+                        _lastMode = "last_encode_log.txt";\r
+                        break;\r
+                }\r
 \r
-            // Seutp the log file\r
-            if (scan)\r
-            {\r
-                txt_log.Text = "Scan Log";\r
-                _readFile = "last_scan_log.txt";\r
+                Thread.Sleep(1000);\r
             }\r
-            else\r
-            {\r
-                _readFile = "last_encode_log.txt";\r
-                txt_log.Text = "Encode Log";\r
-                if (_encodeQueue.isEncoding)\r
-                    if ((!_encodeQueue.LastEncode.IsEmpty) && _encodeQueue.LastEncode.Query != String.Empty)\r
-                    {\r
-                        rtf_actLog.AppendText("### CLI Query: " + _encodeQueue.LastEncode.Query + "\n");\r
-                        rtf_actLog.AppendText("### Custom Query: " + _encodeQueue.LastEncode.CustomQuery + "\n\n");\r
-                        rtf_actLog.AppendText("#########################################\n\n");\r
-                    }\r
-            }\r
-            _lastUpdate = false;\r
         }\r
-\r
-        private void AutoUpdate(object state)\r
+        private StringBuilder ReadFile(string file)\r
         {\r
+            StringBuilder appendText = new StringBuilder();\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 logFile = Path.Combine(logDir, file);\r
+            string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
+\r
             try\r
             {\r
-                _lastUpdate = false;\r
-                UpdateTextFromThread();\r
-                while (true)\r
+                // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
+                if (File.Exists(logFile2))\r
+                    File.Delete(logFile2);\r
+\r
+                // Copy the log file.\r
+                if (File.Exists(logFile))\r
+                    File.Copy(logFile, logFile2, true);\r
+                else\r
                 {\r
-                    if (IsHandleCreated)\r
-                    {\r
-                        if (_encodeQueue.isEncoding || _mainWin.isScanning)\r
-                            UpdateTextFromThread();\r
-                        else\r
-                        {\r
-                            // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
-                            if (_lastUpdate == false)\r
-                                UpdateTextFromThread();\r
+                    appendText.AppendFormat("Waiting for the log file to be generated ...\n");\r
+                    _position = 0;\r
+                    ClearWindowText();\r
+                    return appendText;\r
+                }\r
 \r
-                            _lastUpdate = true; // Prevents the log window from being updated when there is no encode going.\r
-                            _position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused\r
-                        }\r
+                // Start the Reader\r
+                // Only use text which continues on from the last read line\r
+                StreamReader sr = new StreamReader(logFile2);\r
+                string line;\r
+                int i = 1;\r
+                while ((line = sr.ReadLine()) != null)\r
+                {\r
+                    if (i > _position)\r
+                    {\r
+                        appendText.AppendLine(line);\r
+                        _position++;\r
                     }\r
-                    Thread.Sleep(1000);\r
+                    i++;\r
                 }\r
-            }\r
-            catch (Exception exc)\r
-            {\r
-                MessageBox.Show("AutoUpdate(): Exception: \n" + exc);\r
-            }\r
-        }\r
-        private void UpdateTextFromThread()\r
-        {\r
-            try\r
-            {\r
-                String info = ReadFile();\r
-                if (info.Contains("has exited"))\r
-                    info += "\n ############ End of Log ############## \n";\r
+                sr.Close();\r
+                sr.Dispose();\r
 \r
-                SetText(info);\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc);\r
+                appendText.AppendFormat("\nERROR: The Log file could not be read. You may need to restart HandBrake! " + exc);\r
+                _position = 0;\r
+                ClearWindowText();\r
             }\r
+            return appendText;\r
         }\r
-        private void SetText(string text)\r
+        private void AppendWindowText(StringBuilder text)\r
         {\r
             try\r
             {\r
@@ -149,74 +133,73 @@ namespace Handbrake
                 {\r
                     if (rtf_actLog.InvokeRequired)\r
                     {\r
-                        IAsyncResult invoked = BeginInvoke(new setTextCallback(SetText), new object[] { text });\r
+                        IAsyncResult invoked = BeginInvoke(new setTextCallback(AppendWindowText), new object[] { text });\r
                         EndInvoke(invoked);\r
                     }\r
                     else\r
-                        rtf_actLog.AppendText(text);\r
+                        rtf_actLog.AppendText(text.ToString());\r
                 }\r
             }\r
             catch (Exception exc)\r
             {\r
-                MessageBox.Show("SetText(): Exception: \n" + exc);\r
+                MessageBox.Show("SetWindowText(): Exception: \n" + exc);\r
             }\r
         }\r
-        private String ReadFile()\r
+        private void ClearWindowText()\r
         {\r
-            String appendText = String.Empty;\r
             try\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 logFile = Path.Combine(logDir, _readFile);\r
-                string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
-\r
-                // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.\r
-                if (File.Exists(logFile2))\r
-                    File.Delete(logFile2);\r
-\r
-                // Copy the log file.\r
-                if (File.Exists(logFile))\r
-                    File.Copy(logFile, logFile2, true);\r
-                else\r
-                {\r
-                    if (fileNotFoundQuickFix)\r
-                        return "";\r
-                    fileNotFoundQuickFix = true;\r
-                    return "\n\n\nERROR: The log file could not be found. \nMaybe you cleared your system's tempory folder or maybe you just havn't run an encode yet. \nTried to find the log file in: " + logFile;\r
-                }\r
-\r
-                StreamReader sr = new StreamReader(logFile2);\r
-                string line;\r
-                int i = 1;\r
-                while ((line = sr.ReadLine()) != null)\r
+                if (IsHandleCreated)\r
                 {\r
-                    if (i > _position)\r
+                    if (rtf_actLog.InvokeRequired)\r
                     {\r
-                        appendText += line + Environment.NewLine;\r
-                        _position++;\r
+                        IAsyncResult invoked = BeginInvoke(new setTextClearCallback(ClearWindowText));\r
+                        EndInvoke(invoked);\r
                     }\r
-                    i++;\r
+                    else\r
+                        rtf_actLog.Clear();\r
                 }\r
-                sr.Close();\r
-                sr.Dispose();\r
-\r
-                return appendText;\r
             }\r
             catch (Exception exc)\r
             {\r
-                return "Error in ReadFile() \n Unable to read the log file.\n You may have to restart HandBrake. Will try reading the file again in a few seconds... \n  Error Information: \n\n" + exc;\r
+                MessageBox.Show("ClearWindowText(): Exception: \n" + exc);\r
             }\r
         }\r
 \r
-        protected override void OnClosing(CancelEventArgs e)\r
+        public void PrintLogHeader()\r
         {\r
-            e.Cancel = true;\r
-            this.Hide();\r
-            base.OnClosing(e);\r
+            // Print the log header. This function will be re-implimented later. Do not delete.\r
+            rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
+            rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
+            rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));\r
+            rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));\r
+            rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));\r
+            rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
+            rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
+            rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
+            rtf_actLog.AppendText("#########################################\n\n");\r
         }\r
 \r
+        #region Public\r
+\r
+        public string SetLogFile\r
+        {\r
+            get { return string.IsNullOrEmpty(_currentMode) ? "" : _currentMode; }\r
+            set { _currentMode = value; }\r
+        }\r
+        public void SetScanMode()\r
+        {\r
+            SetLogFile = "last_scan_log.txt";\r
+            this.Text = "Activity Window (Scan Log)";\r
+        }\r
+        public void SetEncodeMode()\r
+        {\r
+            SetLogFile = "last_encode_log.txt";\r
+            this.Text = "Activity Window (Enocde Log)";\r
+        }\r
+\r
+        #endregion\r
+\r
         #region User Interface\r
         private void mnu_copy_log_Click(object sender, EventArgs e)\r
         {\r
@@ -243,11 +226,11 @@ namespace Handbrake
         }\r
         private void btn_scan_log_Click(object sender, EventArgs e)\r
         {\r
-            SetLogView(true);\r
+            SetScanMode();\r
         }\r
         private void btn_encode_log_Click(object sender, EventArgs e)\r
         {\r
-            SetLogView(false);\r
+            SetEncodeMode();\r
         }\r
         #endregion\r
 \r
@@ -288,5 +271,11 @@ namespace Handbrake
         }\r
         #endregion\r
 \r
+        protected override void OnClosing(CancelEventArgs e)\r
+        {\r
+            e.Cancel = true;\r
+            this.Hide();\r
+            base.OnClosing(e);\r
+        }\r
     }\r
 }
\ No newline at end of file