OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmActivityWindow.cs
index 4864df6..07e8f3d 100644 (file)
           Homepage: <http://handbrake.fr>.\r
           It may be used under the terms of the GNU General Public License. */\r
 \r
-using System;\r
-using System.Collections.Generic;\r
-using System.ComponentModel;\r
-using System.Data;\r
-using System.Drawing;\r
-using System.Text;\r
-using System.Windows.Forms;\r
-using System.IO;\r
-using System.Threading;\r
-\r
-\r
 namespace Handbrake\r
 {\r
+    using System;\r
+    using System.ComponentModel;\r
+    using System.Diagnostics;\r
+    using System.IO;\r
+    using System.Text;\r
+    using System.Threading;\r
+    using System.Windows.Forms;\r
+    using Functions;\r
+    using Timer = System.Threading.Timer;\r
+\r
     public partial class frmActivityWindow : Form\r
     {\r
-        /// <summary>\r
-        /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.\r
-        /// </summary>\r
-        /// \r
-        Thread monitorFile;\r
-        public frmActivityWindow()\r
+        private delegate void SetTextCallback(StringBuilder text);\r
+\r
+        private delegate void SetTextClearCallback();\r
+\r
+        private int Position;\r
+        private string LastMode;\r
+        private string CurrentMode;\r
+        private Timer WindowTimer;\r
+\r
+        public frmActivityWindow(string mode)\r
         {\r
             InitializeComponent();\r
-            this.rtf_actLog.Text = string.Empty;\r
-            monitorFile = new Thread(autoUpdate);\r
 \r
+            Position = 0;\r
+            if (mode == "scan")\r
+                SetScanMode();\r
+            else\r
+                SetEncodeMode();\r
         }\r
-        \r
-        private void btn_close_Click(object sender, EventArgs e)\r
+\r
+        private void NewActivityWindow_Load(object sender, EventArgs e)\r
         {\r
-            monitorFile.Abort();\r
-            this.Hide();\r
+            WindowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);\r
         }\r
 \r
-        private void frmActivityWindow_Load(object sender, EventArgs e)\r
+        private void LogMonitor(object n)\r
         {\r
-            this.rtf_actLog.Text = string.Empty;\r
-            rtf_actLog.Text = readFile();\r
-   \r
-            monitorFile.Start();\r
+            if (SetLogFile != LastMode) Reset();\r
+\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
 \r
-        private void autoUpdate(object state)\r
+        private StringBuilder ReadFile(string file)\r
         {\r
-            while (true)\r
+            StringBuilder appendText = new StringBuilder();\r
+            lock (this)\r
             {\r
-                updateTextFromThread();\r
-                Thread.Sleep(3000);\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) +\r
+                                "\\HandBrake\\logs";\r
+                string logFile = Path.Combine(logDir, file);\r
+                string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");\r
+\r
+                try\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
+                        appendText.AppendFormat("Waiting for the log file to be generated ...\n");\r
+                        Position = 0;\r
+                        ClearWindowText();\r
+                        PrintLogHeader();\r
+                        return appendText;\r
+                    }\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
+                        i++;\r
+                    }\r
+                    sr.Close();\r
+                    sr.Dispose();\r
+                }\r
+                catch (Exception)\r
+                {\r
+                    Reset();\r
+                    appendText = new StringBuilder();\r
+                    appendText.AppendFormat(\r
+                        "\nThe Log file is currently in use. Waiting for the log file to become accessible ...\n");\r
+                }\r
             }\r
+            return appendText;\r
         }\r
-        \r
-        private delegate void UpdateUIHandler();\r
-        private void updateTextFromThread()\r
+\r
+        private void AppendWindowText(StringBuilder text)\r
         {\r
-            if (this.InvokeRequired)\r
+            try\r
+            {\r
+                if (IsHandleCreated)\r
+                {\r
+                    if (rtf_actLog.InvokeRequired)\r
+                    {\r
+                        IAsyncResult invoked = BeginInvoke(new SetTextCallback(AppendWindowText), new object[] {text});\r
+                        EndInvoke(invoked);\r
+                    }\r
+                    else\r
+                        lock (rtf_actLog)\r
+                            rtf_actLog.AppendText(text.ToString());\r
+                }\r
+            }\r
+            catch (Exception)\r
             {\r
-                this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));\r
                 return;\r
             }\r
-            rtf_actLog.Text = readFile();\r
-            this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length -1;\r
-            this.rtf_actLog.ScrollToCaret();\r
-\r
-            if (rtf_actLog.Text.Contains("HandBrake has exited."))\r
-                monitorFile.Abort();\r
         }\r
 \r
-        private string readFile()\r
+        private void ClearWindowText()\r
         {\r
-            string log = "";\r
             try\r
             {\r
-                // hb_encode_log.dat is the primary log file. Since .NET can't read this file whilst the CLI is outputing to it,\r
-                // we'll need to make a copy of it.\r
-                string logFile = Path.Combine(Path.GetTempPath(), "hb_encode_log.dat");\r
-                string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\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
-                File.Copy(logFile, logFile2);\r
+                if (IsHandleCreated)\r
+                {\r
+                    if (rtf_actLog.InvokeRequired)\r
+                    {\r
+                        IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(ClearWindowText));\r
+                        EndInvoke(invoked);\r
+                    }\r
+                    else\r
+                        lock (rtf_actLog)\r
+                            rtf_actLog.Clear();\r
+                }\r
+            }\r
+            catch (Exception)\r
+            {\r
+                return;\r
+            }\r
+        }\r
 \r
-                // Begin processing the log file.\r
-                StreamReader sr = new StreamReader(logFile2);\r
-                string line = sr.ReadLine();\r
-                while (line != null)\r
+        private void PrintLogHeader()\r
+        {\r
+            try\r
+            {\r
+                if (IsHandleCreated)\r
                 {\r
-                    log = log + (line + System.Environment.NewLine);\r
-                    line = sr.ReadLine();\r
+                    if (rtf_actLog.InvokeRequired)\r
+                    {\r
+                        IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(PrintLogHeader));\r
+                        EndInvoke(invoked);\r
+                    }\r
+                    else\r
+                    {\r
+                        lock (rtf_actLog)\r
+                        {\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", \r
+                                                                Properties.Settings.Default.hb_build, \r
+                                                                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", SystemInfo.GetCpuCount));\r
+                            rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", SystemInfo.TotalPhysicalMemory));\r
+                            rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", \r
+                                                                SystemInfo.ScreenBounds.Bounds.Width, \r
+                                                                SystemInfo.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
                 }\r
-                sr.Close();\r
             }\r
-            catch (Exception exc)\r
+            catch (Exception)\r
             {\r
-                MessageBox.Show("An Error has occured! \n\n" + exc.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);\r
+                return;\r
             }\r
+        }\r
+\r
+        private void Reset()\r
+        {\r
+            if (WindowTimer != null)\r
+                WindowTimer.Dispose();\r
+            Position = 0;\r
+            ClearWindowText();\r
+            PrintLogHeader();\r
+            WindowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);\r
+        }\r
+\r
+        #region Public\r
+\r
+        public string SetLogFile\r
+        {\r
+            get { return string.IsNullOrEmpty(CurrentMode) ? string.Empty : CurrentMode; }\r
+            set { CurrentMode = value; }\r
+        }\r
+\r
+        public void SetScanMode()\r
+        {\r
+            Reset();\r
+            SetLogFile = "last_scan_log.txt";\r
+            this.Text = "Activity Window (Scan Log)";\r
+        }\r
+\r
+        public void SetEncodeMode()\r
+        {\r
+            Reset();\r
+            SetLogFile = "last_encode_log.txt";\r
+            this.Text = "Activity Window (Enocde Log)";\r
+        }\r
+\r
+        #endregion\r
+\r
+        #region User Interface\r
+\r
+        private void mnu_copy_log_Click(object sender, EventArgs e)\r
+        {\r
+            Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
+        }\r
 \r
-            return log;\r
+        private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
+        {\r
+            string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+            string windir = Environment.GetEnvironmentVariable("WINDIR");\r
+            Process prc = new Process\r
+                              {\r
+                                  StartInfo =\r
+                                      {\r
+                                          FileName = windir + @"\explorer.exe", \r
+                                          Arguments = logDir\r
+                                      }\r
+                              };\r
+            prc.Start();\r
         }\r
 \r
         private void btn_copy_Click(object sender, EventArgs e)\r
         {\r
-            if (rtf_actLog.Text != "")\r
-                Clipboard.SetText(rtf_actLog.Text, TextDataFormat.Rtf);\r
+            Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
+        }\r
+\r
+        private void btn_scan_log_Click(object sender, EventArgs e)\r
+        {\r
+            SetScanMode();\r
         }\r
 \r
-        private void btn_refresh_Click(object sender, EventArgs e)\r
+        private void btn_encode_log_Click(object sender, EventArgs e)\r
         {\r
-            rtf_actLog.Clear();\r
-            rtf_actLog.Text = readFile();\r
+            SetEncodeMode();\r
         }\r
 \r
+        #endregion\r
 \r
+        protected override void OnClosing(CancelEventArgs e)\r
+        {\r
+            WindowTimer.Dispose();\r
+            e.Cancel = true;\r
+            this.Dispose();\r
+            base.OnClosing(e);\r
+        }\r
     }\r
 }
\ No newline at end of file