/* 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.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 System.Diagnostics;\r
-using System.Runtime.InteropServices;\r
-\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
+ /// <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
- Thread monitorFile;\r
- String read_file;\r
- frmMain mainWindow;\r
- frmQueue queueWindow;\r
+ /// <summary>\r
+ /// The Type of log that the window is currently dealing with\r
+ /// </summary>\r
+ private ActivityLogMode mode;\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
+ /// Initializes a new instance of the <see cref="frmActivityWindow"/> class.\r
/// </summary>\r
- /// \r
- public frmActivityWindow(string file, frmMain fm, frmQueue fq)\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
- mainWindow = fm;\r
- queueWindow = fq;\r
+ this.encode = encode;\r
+ this.scan = scan;\r
+ this.mode = mode;\r
+ this.position = 0;\r
+ }\r
+\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
+ /// <summary>\r
+ /// Clear text callback\r
+ /// </summary>\r
+ private delegate void SetTextClearCallback();\r
\r
- this.rtf_actLog.Text = string.Empty;\r
+ // Public\r
\r
- read_file = file;\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
- string logFile = Path.Combine(Path.GetTempPath(), read_file);\r
- if (File.Exists(logFile))\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
+ /// New Code for getting the Activity log from the Services rather than reading a file.\r
+ /// </summary>\r
+ /// <returns>\r
+ /// The StringBuilder containing a log\r
+ /// </returns>\r
+ private StringBuilder GetLog()\r
+ {\r
+ StringBuilder appendText = new StringBuilder();\r
+ \r
+ if (this.mode == ActivityLogMode.Scan)\r
{\r
- if (read_file == "dvdinfo.dat") // No need to refresh the window if we are viwing dvdinfo.dat\r
- updateTextFromThread();\r
- else // however, we should refresh when reading the encode log file.\r
+ if (scan == null || scan.ActivityLog == string.Empty)\r
{\r
- monitorFile = new Thread(autoUpdate);\r
- monitorFile.Start();\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
- MessageBox.Show("The log file could not be found. Maybe you cleared your system's tempory folder or maybe you just havn't run an encode yet.", "Notice", MessageBoxButtons.OK, MessageBoxIcon.Warning);\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
+ return appendText;\r
}\r
\r
- // Update the Activity window every 5 seconds with the latest log data.\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
- updateTextFromThread();\r
- while (true)\r
+ string line;\r
+ int i = 1;\r
+ while ((line = reader.ReadLine()) != null)\r
{\r
- if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))\r
- updateTextFromThread();\r
- Thread.Sleep(5000);\r
+ if (i > position)\r
+ {\r
+ appendText.AppendLine(line);\r
+ position++;\r
+ }\r
+ i++;\r
}\r
}\r
\r
- private delegate void UpdateUIHandler();\r
- private void updateTextFromThread()\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
- if (this.InvokeRequired)\r
+ if (IsHandleCreated)\r
{\r
- this.BeginInvoke(new UpdateUIHandler(updateTextFromThread));\r
- return;\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
- rtf_actLog.Text = readFile();\r
- this.rtf_actLog.SelectionStart = this.rtf_actLog.Text.Length - 1;\r
- this.rtf_actLog.ScrollToCaret();\r
}\r
- catch (Exception exc)\r
+ catch (Exception)\r
{\r
- MessageBox.Show(exc.ToString());\r
+ return;\r
}\r
}\r
\r
- private string readFile()\r
+ /// <summary>\r
+ /// Clear the contents of the log window\r
+ /// </summary>\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(), read_file);\r
- string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");\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
+ lock (rtf_actLog)\r
+ rtf_actLog.Clear();\r
+ }\r
+ }\r
+ catch (Exception)\r
+ {\r
+ return;\r
+ }\r
+ }\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
+ /// <summary>\r
+ /// Display the log header\r
+ /// </summary>\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
+ 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
- // Copy the log file.\r
- File.Copy(logFile, logFile2);\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
- // Begin processing the log file.\r
- StreamReader sr = new StreamReader(logFile2);\r
- string line = sr.ReadLine();\r
- while (line != null)\r
- {\r
- log = log + (line + System.Environment.NewLine);\r
- line = sr.ReadLine();\r
+ rtf_actLog.AppendText(header.ToString());\r
+ }\r
+ }\r
}\r
- sr.Close();\r
-\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
+ /// <summary>\r
+ /// Reset Everything\r
+ /// </summary>\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
+ // Menus and Buttons\r
\r
- return log;\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
+ Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
+ }\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
+ Process prc = new Process\r
+ {\r
+ StartInfo =\r
+ {\r
+ FileName = windir + @"\explorer.exe",\r
+ Arguments = logDir\r
+ }\r
+ };\r
+ prc.Start();\r
}\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
+ Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Set scan mode\r
+ /// </summary>\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
+ SetMode(ActivityLogMode.Scan);\r
+ }\r
+\r
+ /// <summary>\r
+ /// Set the encode mode\r
+ /// </summary>\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
+ SetMode(ActivityLogMode.Encode);\r
+ }\r
+\r
+ // Overrides\r
+\r
+ /// <summary>\r
+ /// override onclosing\r
+ /// </summary>\r
+ /// <param name="e">\r
+ /// The e.\r
+ /// </param>\r
protected override void OnClosing(CancelEventArgs e)\r
{\r
- if (monitorFile != null)\r
- monitorFile.Abort();\r
+ windowTimer.Dispose();\r
e.Cancel = true;\r
- this.Hide();\r
+ this.Dispose();\r
base.OnClosing(e);\r
}\r
-\r
}\r
}
\ No newline at end of file