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.EncodeQueue;
\r
14 using Handbrake.Functions;
\r
15 using Microsoft.Win32;
\r
20 public partial class frmActivityWindow : Form
\r
22 private delegate void setTextCallback(StringBuilder text);
\r
23 private delegate void setTextClearCallback();
\r
24 private static int _position;
\r
25 private static string _lastMode;
\r
26 private static string _currentMode;
\r
27 private Thread monitor;
\r
28 private Boolean kilLThread;
\r
30 public frmActivityWindow(string mode)
\r
37 InitializeComponent();
\r
39 private void NewActivityWindow_Load(object sender, EventArgs e)
\r
41 monitor = new Thread(LogMonitor);
\r
49 catch (Exception exc)
\r
51 MessageBox.Show(exc.ToString());
\r
55 private void LogMonitor()
\r
59 if (!IsHandleCreated || kilLThread) // break out the thread if the window has been disposed.
\r
62 // Perform a reset if require.
\r
63 // If we have switched to a different log file, we want to start from the beginning.
\r
64 if (SetLogFile != _lastMode)
\r
71 // Perform the window update
\r
74 case "last_scan_log.txt":
\r
75 AppendWindowText(ReadFile("last_scan_log.txt"));
\r
76 _lastMode = "last_scan_log.txt";
\r
78 case "last_encode_log.txt":
\r
79 AppendWindowText(ReadFile("last_encode_log.txt"));
\r
80 _lastMode = "last_encode_log.txt";
\r
88 catch (ThreadInterruptedException)
\r
95 private StringBuilder ReadFile(string file)
\r
97 StringBuilder appendText = new StringBuilder();
\r
99 // 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
100 // we'll need to make a copy of it.
\r
101 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
102 string logFile = Path.Combine(logDir, file);
\r
103 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
107 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
108 if (File.Exists(logFile2))
\r
109 File.Delete(logFile2);
\r
111 // Copy the log file.
\r
112 if (File.Exists(logFile))
\r
113 File.Copy(logFile, logFile2, true);
\r
116 appendText.AppendFormat("Waiting for the log file to be generated ...\n");
\r
123 // Start the Reader
\r
124 // Only use text which continues on from the last read line
\r
125 StreamReader sr = new StreamReader(logFile2);
\r
128 while ((line = sr.ReadLine()) != null)
\r
132 appendText.AppendLine(line);
\r
141 catch (Exception exc)
\r
143 appendText.AppendFormat("\nERROR: The Log file could not be read. You may need to restart HandBrake! " + exc);
\r
149 private void AppendWindowText(StringBuilder text)
\r
153 if (IsHandleCreated)
\r
155 if (rtf_actLog.InvokeRequired)
\r
157 IAsyncResult invoked = BeginInvoke(new setTextCallback(AppendWindowText), new object[] { text });
\r
158 EndInvoke(invoked);
\r
161 rtf_actLog.AppendText(text.ToString());
\r
163 } catch(ThreadInterruptedException)
\r
167 catch (Exception exc)
\r
169 MessageBox.Show("SetWindowText(): Exception: \n" + exc);
\r
172 private void ClearWindowText()
\r
176 if (IsHandleCreated)
\r
178 if (rtf_actLog.InvokeRequired)
\r
180 IAsyncResult invoked = BeginInvoke(new setTextClearCallback(ClearWindowText));
\r
181 EndInvoke(invoked);
\r
184 rtf_actLog.Clear();
\r
187 catch (Exception exc)
\r
189 MessageBox.Show("ClearWindowText(): Exception: \n" + exc);
\r
192 public void PrintLogHeader()
\r
196 if (IsHandleCreated)
\r
198 if (rtf_actLog.InvokeRequired)
\r
200 IAsyncResult invoked = BeginInvoke(new setTextClearCallback(PrintLogHeader));
\r
201 EndInvoke(invoked);
\r
205 // Print the log header. This function will be re-implimented later. Do not delete.
\r
206 rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
207 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
208 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));
\r
209 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));
\r
210 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));
\r
211 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
212 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
213 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
214 rtf_actLog.AppendText("#########################################\n\n");
\r
218 catch (Exception exc)
\r
220 MessageBox.Show("PrintLogHeader(): Exception: \n" + exc);
\r
227 public string SetLogFile
\r
229 get { return string.IsNullOrEmpty(_currentMode) ? "" : _currentMode; }
\r
230 set { _currentMode = value; }
\r
232 public void SetScanMode()
\r
234 SetLogFile = "last_scan_log.txt";
\r
235 this.Text = "Activity Window (Scan Log)";
\r
237 public void SetEncodeMode()
\r
239 SetLogFile = "last_encode_log.txt";
\r
240 this.Text = "Activity Window (Enocde Log)";
\r
245 #region User Interface
\r
246 private void mnu_copy_log_Click(object sender, EventArgs e)
\r
248 if (rtf_actLog.SelectedText != "")
\r
249 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
251 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
253 private void mnu_openLogFolder_Click(object sender, EventArgs e)
\r
255 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
256 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
257 System.Diagnostics.Process prc = new System.Diagnostics.Process();
\r
258 prc.StartInfo.FileName = windir + @"\explorer.exe";
\r
259 prc.StartInfo.Arguments = logDir;
\r
262 private void btn_copy_Click(object sender, EventArgs e)
\r
264 if (rtf_actLog.SelectedText != "")
\r
265 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
267 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
269 private void btn_scan_log_Click(object sender, EventArgs e)
\r
273 private void btn_encode_log_Click(object sender, EventArgs e)
\r
279 #region System Information
\r
281 /// Returns the total physical ram in a system
\r
283 /// <returns></returns>
\r
284 public uint TotalPhysicalMemory()
\r
286 Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();
\r
287 Win32.GlobalMemoryStatus(ref memStatus);
\r
289 uint MemoryInfo = memStatus.dwTotalPhys;
\r
290 MemoryInfo = MemoryInfo / 1024 / 1024;
\r
296 /// Get the number of CPU Cores
\r
298 /// <returns>Object</returns>
\r
299 public Object getCpuCount()
\r
301 RegistryKey RegKey = Registry.LocalMachine;
\r
302 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
\r
303 return RegKey.GetValue("ProcessorNameString");
\r
307 /// Get the System screen size information.
\r
309 /// <returns>System.Windows.Forms.Scree</returns>
\r
310 public Screen screenBounds()
\r
312 return Screen.PrimaryScreen;
\r
316 protected override void OnClosing(CancelEventArgs e)
\r
319 monitor.Interrupt();
\r