1 /* frmActivityWindow.cs $
\r
3 This file is part of the HandBrake source code.
\r
4 Homepage: <http://handbrake.fr>.
\r
5 It may be used under the terms of the GNU General Public License. */
\r
8 using System.ComponentModel;
\r
10 using System.Windows.Forms;
\r
12 using System.Threading;
\r
13 using Handbrake.Functions;
\r
14 using Microsoft.Win32;
\r
19 public partial class frmActivityWindow : Form
\r
21 private delegate void setTextCallback(StringBuilder text);
\r
22 private delegate void setTextClearCallback();
\r
23 private static int _position;
\r
24 private static string _lastMode;
\r
25 private static string _currentMode;
\r
26 private Thread monitor;
\r
27 private Boolean kilLThread;
\r
29 public frmActivityWindow(string mode)
\r
38 InitializeComponent();
\r
40 private void NewActivityWindow_Load(object sender, EventArgs e)
\r
42 monitor = new Thread(LogMonitor);
\r
48 catch (Exception exc)
\r
50 MessageBox.Show(exc.ToString());
\r
54 private void LogMonitor()
\r
58 if (!IsHandleCreated || kilLThread) // break out the thread if the window has been disposed.
\r
61 // Perform a reset if require.
\r
62 // If we have switched to a different log file, we want to start from the beginning.
\r
63 if (SetLogFile != _lastMode)
\r
66 // Perform the window update
\r
69 case "last_scan_log.txt":
\r
70 AppendWindowText(ReadFile("last_scan_log.txt"));
\r
71 _lastMode = "last_scan_log.txt";
\r
73 case "last_encode_log.txt":
\r
74 AppendWindowText(ReadFile("last_encode_log.txt"));
\r
75 _lastMode = "last_encode_log.txt";
\r
83 catch (ThreadInterruptedException)
\r
90 private StringBuilder ReadFile(string file)
\r
92 StringBuilder appendText = new StringBuilder();
\r
94 // 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
95 // we'll need to make a copy of it.
\r
96 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
97 string logFile = Path.Combine(logDir, file);
\r
98 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
102 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
103 if (File.Exists(logFile2))
\r
104 File.Delete(logFile2);
\r
106 // Copy the log file.
\r
107 if (File.Exists(logFile))
\r
108 File.Copy(logFile, logFile2, true);
\r
111 appendText.AppendFormat("Waiting for the log file to be generated ...\n");
\r
118 // Start the Reader
\r
119 // Only use text which continues on from the last read line
\r
120 StreamReader sr = new StreamReader(logFile2);
\r
123 while ((line = sr.ReadLine()) != null)
\r
127 appendText.AppendLine(line);
\r
136 catch (Exception exc)
\r
138 appendText.AppendFormat("\nERROR: The Log file could not be read. You may need to restart HandBrake! " + exc);
\r
144 private void AppendWindowText(StringBuilder text)
\r
148 if (IsHandleCreated)
\r
150 if (rtf_actLog.InvokeRequired)
\r
152 IAsyncResult invoked = BeginInvoke(new setTextCallback(AppendWindowText), new object[] { text });
\r
153 EndInvoke(invoked);
\r
156 rtf_actLog.AppendText(text.ToString());
\r
158 } catch(ThreadInterruptedException)
\r
162 catch (Exception exc)
\r
164 MessageBox.Show("SetWindowText(): Exception: \n" + exc);
\r
167 private void ClearWindowText()
\r
171 if (IsHandleCreated)
\r
173 if (rtf_actLog.InvokeRequired)
\r
175 IAsyncResult invoked = BeginInvoke(new setTextClearCallback(ClearWindowText));
\r
176 EndInvoke(invoked);
\r
179 rtf_actLog.Clear();
\r
182 catch (Exception exc)
\r
184 MessageBox.Show("ClearWindowText(): Exception: \n" + exc);
\r
187 private void PrintLogHeader()
\r
191 if (IsHandleCreated)
\r
193 if (rtf_actLog.InvokeRequired)
\r
195 IAsyncResult invoked = BeginInvoke(new setTextClearCallback(PrintLogHeader));
\r
196 EndInvoke(invoked);
\r
200 // Print the log header. This function will be re-implimented later. Do not delete.
\r
201 rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
202 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
203 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));
\r
204 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));
\r
205 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));
\r
206 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
207 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
208 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
209 rtf_actLog.AppendText("#########################################\n\n");
\r
213 catch (Exception exc)
\r
215 MessageBox.Show("PrintLogHeader(): Exception: \n" + exc);
\r
219 private void Reset()
\r
228 public string SetLogFile
\r
230 get { return string.IsNullOrEmpty(_currentMode) ? "" : _currentMode; }
\r
231 set { _currentMode = value; }
\r
233 public void SetScanMode()
\r
236 SetLogFile = "last_scan_log.txt";
\r
237 this.Text = "Activity Window (Scan Log)";
\r
239 public void SetEncodeMode()
\r
242 SetLogFile = "last_encode_log.txt";
\r
243 this.Text = "Activity Window (Enocde Log)";
\r
248 #region User Interface
\r
249 private void mnu_copy_log_Click(object sender, EventArgs e)
\r
251 if (rtf_actLog.SelectedText != "")
\r
252 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
254 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
256 private void mnu_openLogFolder_Click(object sender, EventArgs e)
\r
258 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
259 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
260 System.Diagnostics.Process prc = new System.Diagnostics.Process();
\r
261 prc.StartInfo.FileName = windir + @"\explorer.exe";
\r
262 prc.StartInfo.Arguments = logDir;
\r
265 private void btn_copy_Click(object sender, EventArgs e)
\r
267 if (rtf_actLog.SelectedText != "")
\r
268 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
270 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
272 private void btn_scan_log_Click(object sender, EventArgs e)
\r
276 private void btn_encode_log_Click(object sender, EventArgs e)
\r
282 #region System Information
\r
284 /// Returns the total physical ram in a system
\r
286 /// <returns></returns>
\r
287 public uint TotalPhysicalMemory()
\r
289 Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();
\r
290 Win32.GlobalMemoryStatus(ref memStatus);
\r
292 uint MemoryInfo = memStatus.dwTotalPhys;
\r
293 MemoryInfo = MemoryInfo / 1024 / 1024;
\r
299 /// Get the number of CPU Cores
\r
301 /// <returns>Object</returns>
\r
302 public Object getCpuCount()
\r
304 RegistryKey RegKey = Registry.LocalMachine;
\r
305 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
\r
306 return RegKey.GetValue("ProcessorNameString");
\r
310 /// Get the System screen size information.
\r
312 /// <returns>System.Windows.Forms.Scree</returns>
\r
313 public Screen screenBounds()
\r
315 return Screen.PrimaryScreen;
\r
319 protected override void OnClosing(CancelEventArgs e)
\r
322 monitor.Interrupt();
\r