/* frmActivityWindow.cs $\r
\r
This file is part of the HandBrake source code.\r
- Homepage: <http://handbrake.m0k.org/>.\r
+ 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
+using Handbrake.Functions;\r
+using Microsoft.Win32;\r
\r
\r
namespace Handbrake\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
- public frmActivityWindow()\r
- {\r
- InitializeComponent();\r
- this.rtf_actLog.Text = string.Empty; \r
- }\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
+ private Thread _monitor;\r
+ private Boolean _kilLThread;\r
\r
- private void btn_close_Click(object sender, EventArgs e)\r
+ public frmActivityWindow(string mode)\r
{\r
- this.Hide();\r
- }\r
+ _kilLThread = false;\r
+ _position = 0;\r
+ if (mode == "scan")\r
+ SetScanMode();\r
+ else\r
+ SetEncodeMode();\r
\r
- private void frmActivityWindow_Load(object sender, EventArgs e)\r
+ InitializeComponent();\r
+ }\r
+ private void NewActivityWindow_Load(object sender, EventArgs e)\r
{\r
- this.rtf_actLog.Text = string.Empty;\r
- readFile();\r
+ _monitor = new Thread(LogMonitor);\r
+ \r
+ try\r
+ {\r
+ _monitor.Start();\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ MessageBox.Show("Unable to monitor HandBrakes log files. Please report this error. If the problem presists, try rebooting your computer.\n\n Debug Informaton:\n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+ }\r
}\r
\r
- private void readFile()\r
+ private void LogMonitor()\r
{\r
- try\r
+ while (true)\r
{\r
- string dvdInfoPath = Path.Combine(Path.GetTempPath(), "hb_encode_log.dat");\r
- FileStream f = System.IO.File.Open(dvdInfoPath, FileMode.Open, FileAccess.Read, FileShare.Read);\r
+ if (!IsHandleCreated || _kilLThread) // break out the thread if the window has been disposed.\r
+ break;\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
+ Reset();\r
\r
- StreamReader sr = new StreamReader(f);\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
+ try\r
+ {\r
+ Thread.Sleep(1000);\r
+ }\r
+ catch (ThreadInterruptedException)\r
+ {\r
+ // Do Nothnig.\r
+ }\r
\r
+ }\r
+ }\r
+ private StringBuilder ReadFile(string file)\r
+ {\r
+ StringBuilder appendText = new StringBuilder();\r
\r
- string line = sr.ReadLine();\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
- while (line != null)\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
- this.rtf_actLog.AppendText(line + System.Environment.NewLine);\r
- line = sr.ReadLine();\r
+ if (i > _position)\r
+ {\r
+ appendText.AppendLine(line);\r
+ _position++;\r
+ }\r
+ i++;\r
}\r
sr.Close();\r
+ sr.Dispose();\r
+\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ appendText.AppendFormat("\n The Log file could not be read. You may need to restart HandBrake! " + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);\r
+ _position = 0;\r
+ ClearWindowText();\r
+ }\r
+ return appendText;\r
+ }\r
+ private void AppendWindowText(StringBuilder text)\r
+ {\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
+ rtf_actLog.AppendText(text.ToString());\r
+ }\r
+ } catch(ThreadInterruptedException)\r
+ {\r
+ // Do Nothing\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ MessageBox.Show("Unless you are having problems, you can probably ignore this error. It would not hurt to report this error!\n\nSetWindowText(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
+ }\r
+ }\r
+ private void ClearWindowText()\r
+ {\r
+ try\r
+ {\r
+ if (IsHandleCreated)\r
+ {\r
+ if (rtf_actLog.InvokeRequired)\r
+ {\r
+ IAsyncResult invoked = BeginInvoke(new setTextClearCallback(ClearWindowText));\r
+ EndInvoke(invoked);\r
+ }\r
+ else\r
+ rtf_actLog.Clear();\r
+ }\r
+ }\r
+ catch (Exception exc)\r
+ {\r
+ MessageBox.Show("Unless you are having problems, you can probably ignore this error. It would not hurt to report this error!\n\nClearWindowText(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
+ }\r
+ }\r
+ private void PrintLogHeader()\r
+ {\r
+ try\r
+ {\r
+ if (IsHandleCreated)\r
+ {\r
+ if (rtf_actLog.InvokeRequired)\r
+ {\r
+ IAsyncResult invoked = BeginInvoke(new setTextClearCallback(PrintLogHeader));\r
+ EndInvoke(invoked);\r
+ }\r
+ else\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", 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
}\r
- catch (Exception)\r
+ catch (Exception exc)\r
{\r
- rtf_actLog.Clear();\r
- rtf_actLog.Text = "Please wait until the encode has finished to view the log.";\r
+ MessageBox.Show("Unless you are having problems, you can probably ignore this error. It would not hurt to report this error!\n\nPrintLogHeader(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Information);\r
}\r
+\r
+ }\r
+ private void Reset()\r
+ {\r
+ _position = 0;\r
+ ClearWindowText();\r
+ PrintLogHeader();\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
+ Reset();\r
+ SetLogFile = "last_scan_log.txt";\r
+ this.Text = "Activity Window (Scan Log)";\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
+ private void mnu_copy_log_Click(object sender, EventArgs e)\r
+ {\r
+ if (rtf_actLog.SelectedText != "")\r
+ Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
+ else\r
+ Clipboard.SetDataObject(rtf_actLog.Text, true);\r
+ }\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
+ System.Diagnostics.Process prc = new System.Diagnostics.Process();\r
+ prc.StartInfo.FileName = windir + @"\explorer.exe";\r
+ prc.StartInfo.Arguments = logDir;\r
+ prc.Start();\r
+ }\r
private void btn_copy_Click(object sender, EventArgs e)\r
{\r
- if (rtf_actLog.Text != "")\r
- Clipboard.SetText(rtf_actLog.Text, TextDataFormat.Text);\r
+ if (rtf_actLog.SelectedText != "")\r
+ Clipboard.SetDataObject(rtf_actLog.SelectedText, true);\r
+ else\r
+ Clipboard.SetDataObject(rtf_actLog.Text, true);\r
+ }\r
+ private void btn_scan_log_Click(object sender, EventArgs e)\r
+ {\r
+ SetScanMode();\r
}\r
+ private void btn_encode_log_Click(object sender, EventArgs e)\r
+ {\r
+ SetEncodeMode();\r
+ }\r
+ #endregion\r
\r
- private void btn_refresh_Click(object sender, EventArgs e)\r
+ #region System Information\r
+ /// <summary>\r
+ /// Returns the total physical ram in a system\r
+ /// </summary>\r
+ /// <returns></returns>\r
+ public uint TotalPhysicalMemory()\r
{\r
- rtf_actLog.Clear();\r
- readFile();\r
+ Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();\r
+ Win32.GlobalMemoryStatus(ref memStatus);\r
+\r
+ uint MemoryInfo = memStatus.dwTotalPhys;\r
+ MemoryInfo = MemoryInfo / 1024 / 1024;\r
+\r
+ return MemoryInfo;\r
}\r
\r
+ /// <summary>\r
+ /// Get the number of CPU Cores\r
+ /// </summary>\r
+ /// <returns>Object</returns>\r
+ public Object getCpuCount()\r
+ {\r
+ RegistryKey RegKey = Registry.LocalMachine;\r
+ RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
+ return RegKey.GetValue("ProcessorNameString");\r
+ }\r
\r
+ /// <summary>\r
+ /// Get the System screen size information.\r
+ /// </summary>\r
+ /// <returns>System.Windows.Forms.Scree</returns>\r
+ public Screen screenBounds()\r
+ {\r
+ return Screen.PrimaryScreen;\r
+ }\r
+ #endregion\r
+\r
+ protected override void OnClosing(CancelEventArgs e)\r
+ {\r
+ _kilLThread = true;\r
+ _monitor.Interrupt();\r
+ _monitor.Join();\r
+ e.Cancel = true;\r
+ this.Dispose();\r
+ base.OnClosing(e);\r
+ }\r
}\r
}
\ No newline at end of file