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.Collections.Generic;
\r
9 using System.Windows.Forms;
\r
11 using System.Threading;
\r
12 using System.Runtime.InteropServices;
\r
13 using Microsoft.Win32;
\r
18 public partial class frmActivityWindow : Form
\r
20 delegate void SetTextCallback(string text);
\r
23 Functions.Encode encodeHandler;
\r
24 int position; // Position in the arraylist reached by the current log output in the rtf box.
\r
27 /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.
\r
29 public frmActivityWindow(string file, Functions.Encode eh)
\r
31 InitializeComponent();
\r
32 this.rtf_actLog.Text = string.Empty;
\r
34 // When the window closes, we want to abort the monitor thread.
\r
35 this.Disposed += new EventHandler(forceQuit);
\r
41 // Print the Log header in the Rich text box.
\r
44 if (file == "dvdinfo.dat")
\r
45 txt_log.Text = "Scan Log";
\r
46 else if (file == "hb_encode_log.dat")
\r
47 txt_log.Text = "Encode Log";
\r
49 // Start a new thread which will montior and keep the log window up to date if required/
\r
50 startLogThread(read_file);
\r
54 /// Displays the Log header
\r
56 private void displayLogHeader()
\r
58 // Add a header to the log file indicating that it's from the Windows GUI and display the windows version
\r
59 rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
60 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
61 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));
\r
62 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));
\r
63 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));
\r
64 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
65 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
66 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
67 rtf_actLog.AppendText("#########################################\n\n");
\r
71 /// Starts a new thread which runs the autoUpdate function.
\r
73 /// <param name="file"> File which will be used to populate the Rich text box.</param>
\r
74 private void startLogThread(string file)
\r
78 string logFile = Path.Combine(Path.GetTempPath(), file);
\r
79 if (File.Exists(logFile))
\r
81 // Start a new thread to run the autoUpdate process
\r
82 monitor = new Thread(autoUpdate);
\r
83 monitor.IsBackground = true;
\r
87 rtf_actLog.AppendText("\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
90 catch (Exception exc)
\r
92 MessageBox.Show("startLogThread(): Exception: \n" + exc);
\r
97 /// Change the log file to be displayed to hb_encode_log.dat
\r
99 /// <param name="sender"></param>
\r
100 /// <param name="e"></param>
\r
101 private void btn_scan_log_Click(object sender, EventArgs e)
\r
103 if (monitor != null)
\r
106 rtf_actLog.Clear();
\r
107 read_file = "dvdinfo.dat";
\r
108 displayLogHeader();
\r
109 startLogThread(read_file);
\r
110 txt_log.Text = "Scan Log";
\r
114 /// Change the log file to be displayed to dvdinfo.dat
\r
116 /// <param name="sender"></param>
\r
117 /// <param name="e"></param>
\r
118 private void btn_encode_log_Click(object sender, EventArgs e)
\r
120 if (monitor != null)
\r
123 rtf_actLog.Clear();
\r
124 read_file = "hb_encode_log.dat";
\r
126 displayLogHeader();
\r
127 startLogThread(read_file);
\r
128 txt_log.Text = "Encode Log";
\r
132 /// Copy to Clipboard
\r
134 /// <param name="sender"></param>
\r
135 /// <param name="e"></param>
\r
136 private void btn_copy_Click(object sender, EventArgs e)
\r
138 if (rtf_actLog.SelectedText != "")
\r
139 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
141 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
145 /// Updates the log window with any new data which is in the log file.
\r
146 /// This is done every 5 seconds.
\r
148 /// <param name="state"></param>
\r
149 private void autoUpdate(object state)
\r
153 Boolean lastUpdate = false;
\r
154 updateTextFromThread();
\r
157 if (encodeHandler.isEncoding)
\r
158 updateTextFromThread();
\r
161 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
162 if (lastUpdate == false)
\r
163 updateTextFromThread();
\r
165 lastUpdate = true; // Prevents the log window from being updated when there is no encode going.
\r
166 position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused
\r
168 Thread.Sleep(5000);
\r
171 catch (ThreadAbortException)
\r
173 // Do Nothing. This is needed since we run thread.abort().
\r
174 // Should probably find a better way of making this work at some point.
\r
176 catch (Exception exc)
\r
178 MessageBox.Show("autoUpdate(): Exception: \n" + exc);
\r
183 /// Finds any new text in the log file and calls a funciton to display this new text.
\r
185 private void updateTextFromThread()
\r
190 List<string> data = readFile();
\r
191 int count = data.Count;
\r
193 while (position < count)
\r
195 text = data[position];
\r
196 if (data[position].Contains("has exited"))
\r
197 text = "\n ############ End of Log ############## \n";
\r
203 catch (Exception exc)
\r
205 MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);
\r
210 /// Updates the rich text box with anything in the string text.
\r
212 /// <param name="text"></param>
\r
213 private void SetText(string text)
\r
217 // InvokeRequired required compares the thread ID of the
\r
218 // calling thread to the thread ID of the creating thread.
\r
219 // If these threads are different, it returns true.
\r
220 if (IsHandleCreated) // Make sure the windows has a handle before doing anything
\r
222 if (rtf_actLog.InvokeRequired)
\r
224 SetTextCallback d = new SetTextCallback(SetText);
\r
225 Invoke(d, new object[] { text });
\r
228 rtf_actLog.AppendText(text);
\r
231 catch (Exception exc)
\r
233 MessageBox.Show("SetText(): Exception: \n" + exc);
\r
238 /// Read the log file, and store the data in a List.
\r
240 /// <returns></returns>
\r
241 private List<string> readFile()
\r
243 // Ok, the task here is to, Get an arraylist of log data.
\r
244 // And update some global varibles which are pointers to the last displayed log line.
\r
245 List<string> logData = new List<string>();
\r
249 // hb_encode_log.dat 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
250 // we'll need to make a copy of it.
\r
251 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
252 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");
\r
254 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
255 if (File.Exists(logFile2))
\r
256 File.Delete(logFile2);
\r
258 // Copy the log file.
\r
259 File.Copy(logFile, logFile2);
\r
261 // Open the copied log file for reading
\r
262 StreamReader sr = new StreamReader(logFile2);
\r
263 string line = sr.ReadLine();
\r
264 while (line != null)
\r
266 if (line.Trim() != "")
\r
267 logData.Add(line + Environment.NewLine);
\r
269 line = sr.ReadLine();
\r
276 catch (Exception exc)
\r
278 MessageBox.Show("Error in readFile() \n Unable to read the log file.\n You may have to restart HandBrake.\n Error Information: \n\n" + exc, "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r
284 /// Kills the montior thead when the window is disposed of.
\r
286 /// <param name="sender"></param>
\r
287 /// <param name="e"></param>
\r
288 private void forceQuit(object sender, EventArgs e)
\r
290 if (monitor != null)
\r
292 while (monitor.IsAlive)
\r
300 /// Copy Log Menu Item on the right click menu for the log rtf box
\r
302 /// <param name="sender"></param>
\r
303 /// <param name="e"></param>
\r
304 private void mnu_copy_log_Click(object sender, EventArgs e)
\r
306 if (rtf_actLog.SelectedText != "")
\r
307 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
309 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
312 #region System Information
\r
313 private struct MEMORYSTATUS // Unused var's are requred here.
\r
315 public UInt32 dwLength;
\r
316 public UInt32 dwMemoryLoad;
\r
317 public UInt32 dwTotalPhys; // Used
\r
318 public UInt32 dwAvailPhys;
\r
319 public UInt32 dwTotalPageFile;
\r
320 public UInt32 dwAvailPageFile;
\r
321 public UInt32 dwTotalVirtual;
\r
322 public UInt32 dwAvailVirtual;
\r
325 [DllImport("kernel32.dll")]
\r
326 private static extern void GlobalMemoryStatus
\r
328 ref MEMORYSTATUS lpBuffer
\r
332 /// Returns the total physical ram in a system
\r
334 /// <returns></returns>
\r
335 public uint TotalPhysicalMemory()
\r
337 MEMORYSTATUS memStatus = new MEMORYSTATUS();
\r
338 GlobalMemoryStatus(ref memStatus);
\r
340 uint MemoryInfo = memStatus.dwTotalPhys;
\r
341 MemoryInfo = MemoryInfo / 1024 / 1024;
\r
347 /// Get the number of CPU Cores
\r
349 /// <returns>Object</returns>
\r
350 public Object getCpuCount()
\r
352 RegistryKey RegKey = Registry.LocalMachine;
\r
353 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
\r
354 return RegKey.GetValue("ProcessorNameString");
\r
358 /// Get the System screen size information.
\r
360 /// <returns>System.Windows.Forms.Scree</returns>
\r
361 public Screen screenBounds()
\r
363 return Screen.PrimaryScreen;
\r