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.Windows.Forms;
\r
10 using System.Threading;
\r
11 using Handbrake.EncodeQueue;
\r
12 using Handbrake.Functions;
\r
13 using Microsoft.Win32;
\r
18 public partial class frmActivityWindow : Form
\r
20 delegate void SetTextCallback(string text);
\r
23 EncodeAndQueueHandler encodeQueue;
\r
24 int position; // Position in the arraylist reached by the current log output in the rtf box.
\r
25 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
26 private frmMain mainWin;
\r
29 /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.
\r
31 public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)
\r
33 InitializeComponent();
\r
35 rtf_actLog.Text = string.Empty;
\r
40 // When the window closes, we want to abort the monitor thread.
\r
41 this.Disposed += new EventHandler(forceQuit);
\r
43 // Print the Log header in the Rich text box.
\r
46 if (file == "last_scan_log.txt")
\r
51 // Start a new thread which will montior and keep the log window up to date if required/
\r
52 startLogThread(read_file);
\r
56 /// Set the file which the log window is viewing.
\r
58 /// <param name="scan"></param>
\r
59 public void setLogView(Boolean scan)
\r
63 txt_log.Text = "Scan Log";
\r
64 read_file = "last_scan_log.txt";
\r
68 read_file = "last_encode_log.txt";
\r
69 txt_log.Text = "Encode Log";
\r
74 /// Displays the Log header
\r
76 private void displayLogHeader()
\r
78 // Add a header to the log file indicating that it's from the Windows GUI and display the windows version
\r
79 rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
80 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
81 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));
\r
82 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));
\r
83 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));
\r
84 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
85 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
86 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
87 rtf_actLog.AppendText("#########################################\n\n");
\r
88 if (encodeQueue.isEncoding && encodeQueue.LastEncode.Query != String.Empty)
\r
90 rtf_actLog.AppendText("### CLI Query: " + encodeQueue.LastEncode.Query + "\n\n");
\r
91 rtf_actLog.AppendText("#########################################\n\n");
\r
96 /// Starts a new thread which runs the autoUpdate function.
\r
98 /// <param name="file"> File which will be used to populate the Rich text box.</param>
\r
99 private void startLogThread(string file)
\r
103 string logFile = Path.Combine(logDir, file);
\r
104 if (File.Exists(logFile))
\r
106 // Start a new thread to run the autoUpdate process
\r
107 monitor = new Thread(autoUpdate);
\r
108 monitor.IsBackground = true;
\r
112 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
115 catch (Exception exc)
\r
117 MessageBox.Show("startLogThread(): Exception: \n" + exc);
\r
122 /// Updates the log window with any new data which is in the log file.
\r
123 /// This is done every 5 seconds.
\r
125 /// <param name="state"></param>
\r
126 private void autoUpdate(object state)
\r
130 Boolean lastUpdate = false;
\r
131 updateTextFromThread();
\r
134 if (encodeQueue.isEncoding || mainWin.isScanning)
\r
135 updateTextFromThread();
\r
138 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
139 if (lastUpdate == false)
\r
140 updateTextFromThread();
\r
142 lastUpdate = true; // Prevents the log window from being updated when there is no encode going.
\r
143 position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused
\r
145 Thread.Sleep(5000);
\r
148 catch (ThreadAbortException)
\r
150 // Do Nothing. This is needed since we run thread.abort().
\r
151 // Should probably find a better way of making this work at some point.
\r
153 catch (Exception exc)
\r
155 MessageBox.Show("autoUpdate(): Exception: \n" + exc);
\r
160 /// Finds any new text in the log file and calls a funciton to display this new text.
\r
162 private void updateTextFromThread()
\r
166 String info = readFile();
\r
167 if (info.Contains("has exited"))
\r
168 info += "\n ############ End of Log ############## \n";
\r
172 catch (Exception exc)
\r
174 MessageBox.Show("updateTextFromThread(): Exception: \n" + exc);
\r
179 /// Updates the rich text box with anything in the string text.
\r
181 /// <param name="text"></param>
\r
182 private void SetText(string text)
\r
186 // InvokeRequired required compares the thread ID of the
\r
187 // calling thread to the thread ID of the creating thread.
\r
188 // If these threads are different, it returns true.
\r
189 if (IsHandleCreated) // Make sure the windows has a handle before doing anything
\r
191 if (rtf_actLog.InvokeRequired)
\r
193 SetTextCallback d = new SetTextCallback(SetText);
\r
194 Invoke(d, new object[] { text });
\r
197 rtf_actLog.AppendText(text);
\r
200 catch (Exception exc)
\r
202 MessageBox.Show("SetText(): Exception: \n" + exc);
\r
207 /// Read the log file, and store the data in a List.
\r
209 /// <returns></returns>
\r
210 private String readFile()
\r
212 String appendText = String.Empty;
\r
215 // 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
216 // we'll need to make a copy of it.
\r
217 string logFile = Path.Combine(logDir, read_file);
\r
218 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
220 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
221 if (File.Exists(logFile2))
\r
222 File.Delete(logFile2);
\r
224 // Copy the log file.
\r
225 File.Copy(logFile, logFile2);
\r
227 // Open the copied log file for reading
\r
228 StreamReader sr = new StreamReader(logFile2);
\r
231 while ((line = sr.ReadLine()) != null)
\r
235 appendText += line + Environment.NewLine;
\r
245 catch (Exception exc)
\r
247 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
253 /// Kills the montior thead when the window is disposed of.
\r
255 /// <param name="sender"></param>
\r
256 /// <param name="e"></param>
\r
257 private void forceQuit(object sender, EventArgs e)
\r
259 if (monitor != null)
\r
261 while (monitor.IsAlive)
\r
268 #region User Interface
\r
270 private void mnu_copy_log_Click(object sender, EventArgs e)
\r
272 if (rtf_actLog.SelectedText != "")
\r
273 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
275 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
277 private void mnu_openLogFolder_Click(object sender, EventArgs e)
\r
279 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
280 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
281 System.Diagnostics.Process prc = new System.Diagnostics.Process();
\r
282 prc.StartInfo.FileName = windir + @"\explorer.exe";
\r
283 prc.StartInfo.Arguments = logDir;
\r
286 private void btn_copy_Click(object sender, EventArgs e)
\r
288 if (rtf_actLog.SelectedText != "")
\r
289 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
291 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
293 private void btn_scan_log_Click(object sender, EventArgs e)
\r
295 // Switch to the scan log.
\r
297 if (monitor != null)
\r
300 rtf_actLog.Clear();
\r
301 read_file = "last_scan_log.txt";
\r
302 displayLogHeader();
\r
303 startLogThread(read_file);
\r
304 txt_log.Text = "Scan Log";
\r
306 private void btn_encode_log_Click(object sender, EventArgs e)
\r
308 // Switch to the encode log
\r
310 if (monitor != null)
\r
313 rtf_actLog.Clear();
\r
314 read_file = "last_encode_log.txt";
\r
316 displayLogHeader();
\r
317 startLogThread(read_file);
\r
318 txt_log.Text = "Encode Log";
\r
323 #region System Information
\r
327 /// Returns the total physical ram in a system
\r
329 /// <returns></returns>
\r
330 public uint TotalPhysicalMemory()
\r
332 Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();
\r
333 Win32.GlobalMemoryStatus(ref memStatus);
\r
335 uint MemoryInfo = memStatus.dwTotalPhys;
\r
336 MemoryInfo = MemoryInfo / 1024 / 1024;
\r
342 /// Get the number of CPU Cores
\r
344 /// <returns>Object</returns>
\r
345 public Object getCpuCount()
\r
347 RegistryKey RegKey = Registry.LocalMachine;
\r
348 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
\r
349 return RegKey.GetValue("ProcessorNameString");
\r
353 /// Get the System screen size information.
\r
355 /// <returns>System.Windows.Forms.Scree</returns>
\r
356 public Screen screenBounds()
\r
358 return Screen.PrimaryScreen;
\r