1 /* frmActivityWindow.cs $
\r
2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
9 using System.ComponentModel;
\r
10 using System.Diagnostics;
\r
13 using System.Threading;
\r
14 using System.Windows.Forms;
\r
16 using Timer = System.Threading.Timer;
\r
19 /// The Activity Log Window
\r
21 public partial class frmActivityWindow : Form
\r
24 /// The current position in the log file
\r
26 private int position;
\r
29 /// The previous mode
\r
31 private string lastMode;
\r
34 /// The current mode
\r
36 private string currentMode;
\r
39 /// A Timer for this window
\r
41 private Timer windowTimer;
\r
44 /// Initializes a new instance of the <see cref="frmActivityWindow"/> class.
\r
46 /// <param name="mode">
\r
49 public frmActivityWindow(string mode)
\r
51 InitializeComponent();
\r
61 /// A callback function for updating the ui
\r
63 /// <param name="text">
\r
66 private delegate void SetTextCallback(StringBuilder text);
\r
69 /// Clear text callback
\r
71 private delegate void SetTextClearCallback();
\r
76 /// Gets or sets SetLogFile.
\r
78 public string SetLogFile
\r
80 get { return string.IsNullOrEmpty(currentMode) ? string.Empty : currentMode; }
\r
81 set { currentMode = value; }
\r
85 /// Set the window to scan mode
\r
87 public void SetScanMode()
\r
90 SetLogFile = "last_scan_log.txt";
\r
91 this.Text = "Activity Window (Scan Log)";
\r
95 /// Set the window to encode mode
\r
97 public void SetEncodeMode()
\r
100 SetLogFile = "last_encode_log.txt";
\r
101 this.Text = "Activity Window (Enocde Log)";
\r
107 /// On Window load, start a new timer
\r
109 /// <param name="sender">
\r
112 /// <param name="e">
\r
115 private void NewActivityWindow_Load(object sender, EventArgs e)
\r
117 windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);
\r
121 /// Append new text to the window
\r
123 /// <param name="n">
\r
126 private void LogMonitor(object n)
\r
128 if (SetLogFile != lastMode) Reset();
\r
130 // Perform the window update
\r
131 switch (SetLogFile)
\r
133 case "last_scan_log.txt":
\r
134 AppendWindowText(ReadFile("last_scan_log.txt"));
\r
135 lastMode = "last_scan_log.txt";
\r
137 case "last_encode_log.txt":
\r
138 AppendWindowText(ReadFile("last_encode_log.txt"));
\r
139 lastMode = "last_encode_log.txt";
\r
145 /// Read the log file
\r
147 /// <param name="file">
\r
151 /// A string builder containing the log data
\r
153 private StringBuilder ReadFile(string file)
\r
155 StringBuilder appendText = new StringBuilder();
\r
158 // 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
159 // we'll need to make a copy of it.
\r
160 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) +
\r
161 "\\HandBrake\\logs";
\r
162 string logFile = Path.Combine(logDir, file);
\r
163 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
167 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
168 if (File.Exists(logFile2))
\r
169 File.Delete(logFile2);
\r
171 // Copy the log file.
\r
172 if (File.Exists(logFile))
\r
173 File.Copy(logFile, logFile2, true);
\r
176 appendText.AppendFormat("Waiting for the log file to be generated ...\n");
\r
184 // TODO This is just Experimental Code. Just ignore it.
\r
185 ////if (encode.ActivityLog == null)
\r
187 //// appendText.AppendFormat("Waiting for the log file to be generated ...\n");
\r
189 //// ClearWindowText();
\r
190 //// PrintLogHeader();
\r
191 //// return appendText;
\r
194 ////using (StringReader reader = new StringReader(encode.ActivityLog))
\r
198 //// while ((line = reader.ReadLine()) != null)
\r
200 //// if (i > position)
\r
202 //// appendText.AppendLine(line);
\r
210 // Start the Reader
\r
211 // Only use text which continues on from the last read line
\r
212 StreamReader sr = new StreamReader(logFile2);
\r
215 while ((line = sr.ReadLine()) != null)
\r
219 appendText.AppendLine(line);
\r
230 appendText = new StringBuilder();
\r
231 appendText.AppendLine("\nThe Log file is currently in use. Waiting for the log file to become accessible ...\n");
\r
238 /// Append text to the RTF box
\r
240 /// <param name="text">
\r
243 private void AppendWindowText(StringBuilder text)
\r
247 if (IsHandleCreated)
\r
249 if (rtf_actLog.InvokeRequired)
\r
251 IAsyncResult invoked = BeginInvoke(new SetTextCallback(AppendWindowText), new object[] { text });
\r
252 EndInvoke(invoked);
\r
256 rtf_actLog.AppendText(text.ToString());
\r
266 /// Clear the contents of the log window
\r
268 private void ClearWindowText()
\r
272 if (IsHandleCreated)
\r
274 if (rtf_actLog.InvokeRequired)
\r
276 IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(ClearWindowText));
\r
277 EndInvoke(invoked);
\r
281 rtf_actLog.Clear();
\r
291 /// Display the log header
\r
293 private void PrintLogHeader()
\r
297 if (IsHandleCreated)
\r
299 if (rtf_actLog.InvokeRequired)
\r
301 IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(PrintLogHeader));
\r
302 EndInvoke(invoked);
\r
308 // Print the log header. This function will be re-implimented later. Do not delete.
\r
309 StringBuilder header = new StringBuilder();
\r
311 header.AppendLine(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
312 header.AppendLine(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
313 header.AppendLine(String.Format("### CPU: {0} \n", SystemInfo.GetCpuCount));
\r
314 header.AppendLine(String.Format("### Ram: {0} MB \n", SystemInfo.TotalPhysicalMemory));
\r
315 header.AppendLine(String.Format("### Screen: {0}x{1} \n", SystemInfo.ScreenBounds.Bounds.Width, SystemInfo.ScreenBounds.Bounds.Height));
\r
316 header.AppendLine(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
317 header.AppendLine(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
318 header.AppendLine(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
319 header.AppendLine("#########################################\n\n");
\r
321 rtf_actLog.AppendText(header.ToString());
\r
333 /// Reset Everything
\r
335 private void Reset()
\r
337 if (windowTimer != null)
\r
338 windowTimer.Dispose();
\r
342 windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);
\r
345 // Menus and Buttons
\r
348 /// Copy log to clipboard
\r
350 /// <param name="sender">
\r
353 /// <param name="e">
\r
356 private void MnuCopyLogClick(object sender, EventArgs e)
\r
358 Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);
\r
362 /// Open the log folder
\r
364 /// <param name="sender">
\r
367 /// <param name="e">
\r
370 private void MnuOpenLogFolderClick(object sender, EventArgs e)
\r
372 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
373 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
374 Process prc = new Process
\r
378 FileName = windir + @"\explorer.exe",
\r
388 /// <param name="sender">
\r
391 /// <param name="e">
\r
394 private void BtnCopyClick(object sender, EventArgs e)
\r
396 Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);
\r
402 /// <param name="sender">
\r
405 /// <param name="e">
\r
408 private void BtnScanLogClick(object sender, EventArgs e)
\r
414 /// Set the encode mode
\r
416 /// <param name="sender">
\r
419 /// <param name="e">
\r
422 private void BtnEncodeLogClick(object sender, EventArgs e)
\r
430 /// override onclosing
\r
432 /// <param name="e">
\r
435 protected override void OnClosing(CancelEventArgs e)
\r
437 windowTimer.Dispose();
\r