OSDN Git Service

WinGui:
[handbrake-jp/handbrake-jp-git.git] / win / C# / frmActivityWindow.cs
index 981ed2f..c099201 100644 (file)
@@ -5,11 +5,12 @@
           It may be used under the terms of the GNU General Public License. */\r
 \r
 using System;\r
+using System.ComponentModel;\r
 using System.Windows.Forms;\r
 using System.IO;\r
 using System.Threading;\r
-using System.Runtime.InteropServices;\r
 using Handbrake.EncodeQueue;\r
+using Handbrake.Functions;\r
 using Microsoft.Win32;\r
 \r
 \r
@@ -17,45 +18,55 @@ namespace Handbrake
 {\r
     public partial class frmActivityWindow : Form\r
     {\r
-        delegate void SetTextCallback(string text);\r
-        String read_file;\r
-        Thread monitor;\r
-        QueueHandler encodeQueue;\r
-        int position;  // Position in the arraylist reached by the current log output in the rtf box.\r
-        string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
+        private delegate void SetTextCallback(string text);\r
+        private String read_file;\r
+        private Thread monitor;\r
+        private EncodeAndQueueHandler encodeQueue;\r
+        private int position;  // Position in the arraylist reached by the current log output in the rtf box.\r
+        private string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";\r
         private frmMain mainWin;\r
+        private Boolean lastUpdate;\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
-        public frmActivityWindow(string file, QueueHandler eh, frmMain mw)\r
+        public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)\r
         {\r
             InitializeComponent();\r
 \r
-            rtf_actLog.Text = string.Empty;\r
             encodeQueue = eh;\r
-            read_file = file;\r
-            position = 0;\r
             mainWin = mw;\r
-            \r
-            // When the window closes, we want to abort the monitor thread.\r
-            this.Disposed += new EventHandler(forceQuit);\r
-\r
-            // Print the Log header in the Rich text box.\r
-            displayLogHeader();\r
 \r
             if (file == "last_scan_log.txt")\r
-                txt_log.Text = "Scan Log";\r
-            else if (file == "last_encode_log.txt")\r
-                txt_log.Text = "Encode Log";\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
             startLogThread(read_file);\r
         }\r
 \r
         /// <summary>\r
-        /// Displays the Log header\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
+        {\r
+            position = 0;\r
+            rtf_actLog.Text = String.Empty;\r
+            displayLogHeader();\r
+\r
+            if (scan)\r
+            {\r
+                txt_log.Text = "Scan Log";\r
+                read_file = "last_scan_log.txt";\r
+            }\r
+            else\r
+            {\r
+                read_file = "last_encode_log.txt";\r
+                txt_log.Text = "Encode Log";\r
+            }\r
+            lastUpdate = false;\r
+        }\r
         private void displayLogHeader()\r
         {\r
             // Add a header to the log file indicating that it's from the Windows GUI and display the windows version\r
@@ -68,17 +79,12 @@ namespace Handbrake
             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
-            if (encodeQueue.isEncoding && encodeQueue.lastQueueItem.Query != String.Empty)\r
+            if (encodeQueue.isEncoding && encodeQueue.LastEncode.Query != String.Empty)\r
             {\r
-                rtf_actLog.AppendText("### CLI Query: " + encodeQueue.lastQueueItem.Query + "\n\n");\r
+                rtf_actLog.AppendText("### CLI Query: " + encodeQueue.LastEncode.Query + "\n\n");\r
                 rtf_actLog.AppendText("#########################################\n\n");\r
             }\r
         }\r
-\r
-        /// <summary>\r
-        /// Starts a new thread which runs the autoUpdate function.\r
-        /// </summary>\r
-        /// <param name="file"> File which will be used to populate the Rich text box.</param>\r
         private void startLogThread(string file)\r
         {\r
             try\r
@@ -86,9 +92,7 @@ namespace Handbrake
                 string logFile = Path.Combine(logDir, file);\r
                 if (File.Exists(logFile))\r
                 {\r
-                    // Start a new thread to run the autoUpdate process\r
-                    monitor = new Thread(autoUpdate);\r
-                    monitor.IsBackground = true;\r
+                    monitor = new Thread(autoUpdate) { IsBackground = true };\r
                     monitor.Start();\r
                 }\r
                 else\r
@@ -100,48 +104,36 @@ namespace Handbrake
                 MessageBox.Show("startLogThread(): Exception: \n" + exc);\r
             }\r
         }\r
-\r
-        /// <summary>\r
-        /// Updates the log window with any new data which is in the log file.\r
-        /// This is done every 5 seconds.\r
-        /// </summary>\r
-        /// <param name="state"></param>\r
         private void autoUpdate(object state)\r
         {\r
             try\r
             {\r
-                Boolean lastUpdate = false;\r
+                lastUpdate = false;\r
                 updateTextFromThread();\r
                 while (true)\r
                 {\r
-                    if (encodeQueue.isEncoding || mainWin.isScanning)\r
-                        updateTextFromThread();\r
-                    else\r
+                    if (IsHandleCreated)\r
                     {\r
-                        // The encode may just have stoped, so, refresh the log one more time before restarting it.\r
-                        if (lastUpdate == false)\r
+                        if (encodeQueue.isEncoding || mainWin.isScanning)\r
                             updateTextFromThread();\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
+                        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
+\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
                     }\r
-                    Thread.Sleep(5000);\r
+                    Thread.Sleep(1000);\r
                 }\r
             }\r
-            catch (ThreadAbortException)\r
-            {\r
-                // Do Nothing. This is needed since we run thread.abort(). \r
-                // Should probably find a better way of making this work at some point.\r
-            }\r
             catch (Exception exc)\r
             {\r
                 MessageBox.Show("autoUpdate(): Exception: \n" + exc);\r
             }\r
         }\r
-\r
-        /// <summary>\r
-        /// Finds any new text in the log file and calls a funciton to display this new text.\r
-        /// </summary>\r
         private void updateTextFromThread()\r
         {\r
             try\r
@@ -157,26 +149,17 @@ namespace Handbrake
                 MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);\r
             }\r
         }\r
-\r
-        /// <summary>\r
-        /// Updates the rich text box with anything in the string text.\r
-        /// </summary>\r
-        /// <param name="text"></param>\r
         private void SetText(string text)\r
         {\r
             try\r
             {\r
-                // InvokeRequired required compares the thread ID of the\r
-                // calling thread to the thread ID of the creating thread.\r
-                // If these threads are different, it returns true.\r
-                if (IsHandleCreated) // Make sure the windows has a handle before doing anything\r
+                if (IsHandleCreated)\r
                 {\r
                     if (rtf_actLog.InvokeRequired)\r
                     {\r
-                        SetTextCallback d = new SetTextCallback(SetText);\r
-                        Invoke(d, new object[] { text });\r
-                    }\r
-                    else\r
+                        IAsyncResult invoked = BeginInvoke(new SetTextCallback(SetText), new object[] {text});\r
+                        EndInvoke(invoked);\r
+                    } else \r
                         rtf_actLog.AppendText(text);\r
                 }\r
             }\r
@@ -185,11 +168,6 @@ namespace Handbrake
                 MessageBox.Show("SetText(): Exception: \n" + exc);\r
             }\r
         }\r
-\r
-        /// <summary>\r
-        /// Read the log file, and store the data in a List.\r
-        /// </summary>\r
-        /// <returns></returns>\r
         private String readFile()\r
         {\r
             String appendText = String.Empty;\r
@@ -232,24 +210,14 @@ namespace Handbrake
             return null;\r
         }\r
 \r
-        /// <summary>\r
-        /// Kills the montior thead when the window is disposed of.\r
-        /// </summary>\r
-        /// <param name="sender"></param>\r
-        /// <param name="e"></param>\r
-        private void forceQuit(object sender, EventArgs e)\r
+        protected override void OnClosing(CancelEventArgs e)\r
         {\r
-            if (monitor != null)\r
-            {\r
-                while (monitor.IsAlive)\r
-                    monitor.Abort();\r
-            }\r
-\r
-            this.Close();\r
+            e.Cancel = true;\r
+            this.Hide();\r
+            base.OnClosing(e);\r
         }\r
 \r
         #region User Interface\r
-\r
         private void mnu_copy_log_Click(object sender, EventArgs e)\r
         {\r
             if (rtf_actLog.SelectedText != "")\r
@@ -275,61 +243,23 @@ namespace Handbrake
         }\r
         private void btn_scan_log_Click(object sender, EventArgs e)\r
         {\r
-            // Switch to the scan log.\r
-\r
-            if (monitor != null)\r
-                monitor.Abort();\r
-\r
-            rtf_actLog.Clear();\r
-            read_file = "last_scan_log.txt";\r
-            displayLogHeader();\r
-            startLogThread(read_file);\r
-            txt_log.Text = "Scan Log";\r
+            setLogView(true);\r
         }\r
         private void btn_encode_log_Click(object sender, EventArgs e)\r
         {\r
-            // Switch to the encode log\r
-\r
-            if (monitor != null)\r
-                monitor.Abort();\r
-\r
-            rtf_actLog.Clear();\r
-            read_file = "last_encode_log.txt";\r
-            position = 0;\r
-            displayLogHeader();\r
-            startLogThread(read_file);\r
-            txt_log.Text = "Encode Log";\r
+            setLogView(false);\r
         }\r
-\r
         #endregion\r
 \r
         #region System Information\r
-        private struct MEMORYSTATUS // Unused var's are requred here.\r
-        {\r
-            public UInt32 dwLength;\r
-            public UInt32 dwMemoryLoad;\r
-            public UInt32 dwTotalPhys; // Used\r
-            public UInt32 dwAvailPhys;\r
-            public UInt32 dwTotalPageFile;\r
-            public UInt32 dwAvailPageFile;\r
-            public UInt32 dwTotalVirtual;\r
-            public UInt32 dwAvailVirtual;\r
-        }\r
-\r
-        [DllImport("kernel32.dll")]\r
-        private static extern void GlobalMemoryStatus\r
-        (\r
-            ref MEMORYSTATUS lpBuffer\r
-        );\r
-\r
         /// <summary>\r
         /// Returns the total physical ram in a system\r
         /// </summary>\r
         /// <returns></returns>\r
         public uint TotalPhysicalMemory()\r
         {\r
-            MEMORYSTATUS memStatus = new MEMORYSTATUS();\r
-            GlobalMemoryStatus(ref memStatus);\r
+            Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
+            Win32.GlobalMemoryStatus(ref memStatus);\r
 \r
             uint MemoryInfo = memStatus.dwTotalPhys;\r
             MemoryInfo = MemoryInfo / 1024 / 1024;\r
@@ -356,7 +286,6 @@ namespace Handbrake
         {\r
             return Screen.PrimaryScreen;\r
         }\r
-\r
         #endregion\r
 \r
     }\r