/* frmActivityWindow.cs $\r
- \r
- This file is part of the HandBrake source code.\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.ComponentModel;\r
-using System.Windows.Forms;\r
-using System.IO;\r
-using System.Threading;\r
-using Handbrake.EncodeQueue;\r
-using Handbrake.Functions;\r
-using Microsoft.Win32;\r
-\r
+ This file is part of the HandBrake source code.\r
+ Homepage: <http://handbrake.fr>.\r
+ It may be used under the terms of the GNU General Public License. */\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 Model;\r
+ using Services;\r
+ using Timer = System.Threading.Timer;\r
+\r
+ /// <summary>\r
+ /// The Activity Log Window\r
+ /// </summary>\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
-\r
- public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)\r
+ /// <summary>\r
+ /// The current position in the log file\r
+ /// </summary>\r
+ private int position;\r
+\r
+ /// <summary>\r
+ /// A Timer for this window\r
+ /// </summary>\r
+ private Timer windowTimer;\r
+\r
+ /// <summary>\r
+ /// The Encode Object\r
+ /// </summary>\r
+ private Encode encode;\r
+\r
+ /// <summary>\r
+ /// The Scan Object\r
+ /// </summary>\r
+ private Scan scan;\r
+\r
+ /// <summary>\r
+ /// The Type of log that the window is currently dealing with\r
+ /// </summary>\r
+ private ActivityLogMode mode;\r
+\r
+ /// <summary>\r
+ /// Initializes a new instance of the <see cref="frmActivityWindow"/> class.\r
+ /// </summary>\r
+ /// <param name="mode">\r
+ /// The mode.\r
+ /// </param>\r
+ /// <param name="encode">\r
+ /// The encode.\r
+ /// </param>\r
+ /// <param name="scan">\r
+ /// The scan.\r
+ /// </param>\r
+ public frmActivityWindow(ActivityLogMode mode, Encode encode, Scan scan)\r
{\r
InitializeComponent();\r
\r
- _encodeQueue = eh;\r
- _mainWin = mw;\r
+ this.encode = encode;\r
+ this.scan = scan;\r
+ this.mode = mode;\r
+ this.position = 0;\r
+ }\r
\r
- fileNotFoundQuickFix = false;\r
+ /// <summary>\r
+ /// A callback function for updating the ui\r
+ /// </summary>\r
+ /// <param name="text">\r
+ /// The text.\r
+ /// </param>\r
+ private delegate void SetTextCallback(StringBuilder text);\r
\r
- if (file == "last_scan_log.txt")\r
- SetLogView(true);\r
- else\r
- SetLogView(false);\r
+ /// <summary>\r
+ /// Clear text callback\r
+ /// </summary>\r
+ private delegate void SetTextClearCallback();\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
- }\r
+ // Public\r
+\r
+ /// <summary>\r
+ /// Set the window to scan mode\r
+ /// </summary>\r
+ /// <param name="setMode">\r
+ /// The set Mode.\r
+ /// </param>\r
+ public void SetMode(ActivityLogMode setMode)\r
+ {\r
+ Reset();\r
+ this.mode = setMode;\r
+ this.Text = mode == ActivityLogMode.Scan ? "Activity Window (Scan Log)" : "Activity Window (Enocde Log)";\r
+ }\r
+\r
+ // Logging\r
+\r
+ /// <summary>\r
+ /// On Window load, start a new timer\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void NewActivityWindow_Load(object sender, EventArgs e)\r
+ {\r
+ windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Append new text to the window\r
+ /// </summary>\r
+ /// <param name="n">\r
+ /// The n.\r
+ /// </param>\r
+ private void LogMonitor(object n)\r
+ {\r
+ AppendWindowText(GetLog());\r
}\r
\r
/// <summary>\r
- /// Set the view which the Log window displays.\r
- /// Scan = true;\r
- /// Encode = false;\r
+ /// New Code for getting the Activity log from the Services rather than reading a file.\r
/// </summary>\r
- /// <param name="scan">Boolean. Scan = true</param>\r
- public void SetLogView(Boolean scan)\r
+ /// <returns>\r
+ /// The StringBuilder containing a log\r
+ /// </returns>\r
+ private StringBuilder GetLog()\r
{\r
- // Reset\r
- _position = 0;\r
- rtf_actLog.Text = String.Empty;\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
-\r
- // Seutp the log file\r
- if (scan)\r
+ StringBuilder appendText = new StringBuilder();\r
+ \r
+ if (this.mode == ActivityLogMode.Scan)\r
{\r
- txt_log.Text = "Scan Log";\r
- _readFile = "last_scan_log.txt";\r
+ if (scan == null || scan.ActivityLog == string.Empty)\r
+ {\r
+ appendText.AppendFormat("Waiting for the log to be generated ...\n");\r
+ position = 0;\r
+ ClearWindowText();\r
+ PrintLogHeader();\r
+ return appendText;\r
+ }\r
+\r
+ using (StringReader reader = new StringReader(scan.ActivityLog))\r
+ {\r
+ LogReader(reader, appendText);\r
+ }\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
+ if (encode == null || encode.ActivityLog == string.Empty)\r
+ {\r
+ appendText.AppendFormat("Waiting for the log to be generated ...\n");\r
+ position = 0;\r
+ ClearWindowText();\r
+ PrintLogHeader();\r
+ return appendText;\r
+ }\r
+\r
+ using (StringReader reader = new StringReader(encode.ActivityLog))\r
+ {\r
+ LogReader(reader, appendText);\r
+ }\r
}\r
- _lastUpdate = false;\r
+ return appendText;\r
}\r
\r
- private void AutoUpdate(object state)\r
+ /// <summary>\r
+ /// Reads the log data from a Scan or Encode object\r
+ /// </summary>\r
+ /// <param name="reader">\r
+ /// The reader.\r
+ /// </param>\r
+ /// <param name="appendText">\r
+ /// The append text.\r
+ /// </param>\r
+ private void LogReader(StringReader reader, StringBuilder appendText)\r
{\r
- try\r
+ string line;\r
+ int i = 1;\r
+ while ((line = reader.ReadLine()) != null)\r
{\r
- _lastUpdate = false;\r
- UpdateTextFromThread();\r
- while (true)\r
+ if (i > position)\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
-\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(1000);\r
+ appendText.AppendLine(line);\r
+ position++;\r
}\r
- }\r
- catch (Exception exc)\r
- {\r
- MessageBox.Show("AutoUpdate(): Exception: \n" + exc);\r
+ i++;\r
}\r
}\r
- private void UpdateTextFromThread()\r
+\r
+ /// <summary>\r
+ /// Append text to the RTF box\r
+ /// </summary>\r
+ /// <param name="text">\r
+ /// The text.\r
+ /// </param>\r
+ private void AppendWindowText(StringBuilder text)\r
{\r
try\r
{\r
- String info = ReadFile();\r
- if (info.Contains("has exited"))\r
- info += "\n ############ End of Log ############## \n";\r
-\r
- SetText(info);\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 exc)\r
+ catch (Exception)\r
{\r
- MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc);\r
+ return;\r
}\r
}\r
- private void SetText(string text)\r
+\r
+ /// <summary>\r
+ /// Clear the contents of the log window\r
+ /// </summary>\r
+ private void ClearWindowText()\r
{\r
try\r
{\r
{\r
if (rtf_actLog.InvokeRequired)\r
{\r
- IAsyncResult invoked = BeginInvoke(new setTextCallback(SetText), new object[] { text });\r
+ IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(ClearWindowText));\r
EndInvoke(invoked);\r
}\r
else\r
- rtf_actLog.AppendText(text);\r
+ lock (rtf_actLog)\r
+ rtf_actLog.Clear();\r
}\r
}\r
- catch (Exception exc)\r
+ catch (Exception)\r
{\r
- MessageBox.Show("SetText(): Exception: \n" + exc);\r
+ return;\r
}\r
}\r
- private String ReadFile()\r
+\r
+ /// <summary>\r
+ /// Display the log header\r
+ /// </summary>\r
+ private void PrintLogHeader()\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(PrintLogHeader));\r
+ EndInvoke(invoked);\r
}\r
- i++;\r
- }\r
- sr.Close();\r
- sr.Dispose();\r
+ else\r
+ {\r
+ lock (rtf_actLog)\r
+ {\r
+ // Print the log header. This function will be re-implimented later. Do not delete.\r
+ StringBuilder header = new StringBuilder();\r
\r
- return appendText;\r
+ header.Append(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));\r
+ header.Append(String.Format("### Running: {0} \n###\n", Environment.OSVersion));\r
+ header.Append(String.Format("### CPU: {0} \n", SystemInfo.GetCpuCount));\r
+ header.Append(String.Format("### Ram: {0} MB \n", SystemInfo.TotalPhysicalMemory));\r
+ header.Append(String.Format("### Screen: {0}x{1} \n", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height));\r
+ header.Append(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));\r
+ header.Append(String.Format("### Install Dir: {0} \n", Application.StartupPath));\r
+ header.Append(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));\r
+ header.Append("#########################################\n\n");\r
+\r
+ rtf_actLog.AppendText(header.ToString());\r
+ }\r
+ }\r
+ }\r
}\r
- catch (Exception exc)\r
+ catch (Exception)\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
+ return;\r
}\r
}\r
\r
- protected override void OnClosing(CancelEventArgs e)\r
+ /// <summary>\r
+ /// Reset Everything\r
+ /// </summary>\r
+ private void Reset()\r
{\r
- e.Cancel = true;\r
- this.Hide();\r
- base.OnClosing(e);\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 User Interface\r
- private void mnu_copy_log_Click(object sender, EventArgs e)\r
+ // Menus and Buttons\r
+\r
+ /// <summary>\r
+ /// Copy log to clipboard\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void MnuCopyLogClick(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
+ Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
}\r
- private void mnu_openLogFolder_Click(object sender, EventArgs e)\r
+\r
+ /// <summary>\r
+ /// Open the log folder\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void MnuOpenLogFolderClick(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
+ Process prc = new Process\r
+ {\r
+ StartInfo =\r
+ {\r
+ FileName = windir + @"\explorer.exe",\r
+ Arguments = logDir\r
+ }\r
+ };\r
prc.Start();\r
}\r
- private void btn_copy_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 btn_scan_log_Click(object sender, EventArgs e)\r
- {\r
- SetLogView(true);\r
- }\r
- private void btn_encode_log_Click(object sender, EventArgs e)\r
+\r
+ /// <summary>\r
+ /// Copy the log\r
+ /// </summary>\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void BtnCopyClick(object sender, EventArgs e)\r
{\r
- SetLogView(false);\r
+ Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
}\r
- #endregion\r
\r
- #region System Information\r
/// <summary>\r
- /// Returns the total physical ram in a system\r
+ /// Set scan mode\r
/// </summary>\r
- /// <returns></returns>\r
- public uint TotalPhysicalMemory()\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void BtnScanLogClick(object sender, EventArgs e)\r
{\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
+ SetMode(ActivityLogMode.Scan);\r
}\r
\r
/// <summary>\r
- /// Get the number of CPU Cores\r
+ /// Set the encode mode\r
/// </summary>\r
- /// <returns>Object</returns>\r
- public Object getCpuCount()\r
+ /// <param name="sender">\r
+ /// The sender.\r
+ /// </param>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ private void BtnEncodeLogClick(object sender, EventArgs e)\r
{\r
- RegistryKey RegKey = Registry.LocalMachine;\r
- RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");\r
- return RegKey.GetValue("ProcessorNameString");\r
+ SetMode(ActivityLogMode.Encode);\r
}\r
\r
+ // Overrides\r
+\r
/// <summary>\r
- /// Get the System screen size information.\r
+ /// override onclosing\r
/// </summary>\r
- /// <returns>System.Windows.Forms.Scree</returns>\r
- public Screen screenBounds()\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
+ protected override void OnClosing(CancelEventArgs e)\r
{\r
- return Screen.PrimaryScreen;\r
+ windowTimer.Dispose();\r
+ e.Cancel = true;\r
+ this.Dispose();\r
+ base.OnClosing(e);\r
}\r
- #endregion\r
-\r
}\r
}
\ No newline at end of file