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
9 using System.Windows.Forms;
\r
11 using System.Threading;
\r
12 using Handbrake.EncodeQueue;
\r
13 using Handbrake.Functions;
\r
14 using Microsoft.Win32;
\r
19 public partial class frmActivityWindow : Form
\r
21 private delegate void setTextCallback(string text);
\r
22 private String _readFile;
\r
23 private readonly EncodeAndQueueHandler _encodeQueue;
\r
24 private int _position; // Position in the arraylist reached by the current log output in the rtf box.
\r
25 private readonly frmMain _mainWin;
\r
26 private Boolean _lastUpdate;
\r
27 private Boolean fileNotFoundQuickFix;
\r
29 public frmActivityWindow(string file, EncodeAndQueueHandler eh, frmMain mw)
\r
31 InitializeComponent();
\r
36 fileNotFoundQuickFix = false;
\r
39 if (file == "dvdinfo.dat")
\r
40 txt_log.Text = "
\83X
\83L
\83\83\83\93\83\8d\83O";
\r
41 else if (file == "hb_encode_log.dat")
\r
42 txt_log.Text = "
\83G
\83\93\83R
\81[
\83h
\83\8d\83O";
\r
44 if (file == "last_scan_log.txt")
\r
49 // Start a new thread which will montior and keep the log window up to date if required/
\r
52 Thread monitor = new Thread(AutoUpdate) { IsBackground = true };
\r
55 catch (Exception exc)
\r
57 MessageBox.Show("startLogThread(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
62 /// Set the view which the Log window displays.
\r
66 /// <param name="scan">Boolean. Scan = true</param>
\r
67 public void SetLogView(Boolean scan)
\r
71 rtf_actLog.Text = String.Empty;
\r
73 // Print the log header
\r
74 rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
75 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
76 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));
\r
77 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));
\r
78 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));
\r
79 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
80 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
81 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
82 rtf_actLog.AppendText("#########################################\n\n");
\r
84 // Seutp the log file
\r
87 txt_log.Text = "Scan Log";
\r
88 _readFile = "last_scan_log.txt";
\r
92 _readFile = "last_encode_log.txt";
\r
93 txt_log.Text = "Encode Log";
\r
94 if (_encodeQueue.isEncoding)
\r
95 if ((!_encodeQueue.LastEncode.IsEmpty) && _encodeQueue.LastEncode.Query != String.Empty)
\r
97 rtf_actLog.AppendText("### CLI Query: " + _encodeQueue.LastEncode.Query + "\n");
\r
98 rtf_actLog.AppendText("### Custom Query: " + _encodeQueue.LastEncode.CustomQuery + "\n\n");
\r
99 rtf_actLog.AppendText("#########################################\n\n");
\r
102 _lastUpdate = false;
\r
107 /// Change the log file to be displayed to hb_encode_log.dat
\r
109 /// <param name="sender"></param>
\r
110 /// <param name="e"></param>
\r
111 private void btn_scan_log_Click(object sender, EventArgs e)
\r
113 if (monitor != null)
\r
116 rtf_actLog.Clear();
\r
117 read_file = "dvdinfo.dat";
\r
118 displayLogHeader();
\r
119 startLogThread(read_file);
\r
120 txt_log.Text = "
\83X
\83L
\83\83\83\93\83\8d\83O";
\r
124 /// Change the log file to be displayed to dvdinfo.dat
\r
126 /// <param name="sender"></param>
\r
127 /// <param name="e"></param>
\r
128 private void btn_encode_log_Click(object sender, EventArgs e)
\r
130 if (monitor != null)
\r
133 rtf_actLog.Clear();
\r
134 read_file = "hb_encode_log.dat";
\r
136 displayLogHeader();
\r
137 startLogThread(read_file);
\r
138 txt_log.Text = "
\83G
\83\93\83R
\81[
\83h
\83\8d\83O";
\r
142 /// Copy to Clipboard
\r
144 /// <param name="sender"></param>
\r
145 /// <param name="e"></param>
\r
146 private void btn_copy_Click(object sender, EventArgs e)
\r
148 if (rtf_actLog.SelectedText != "")
\r
149 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
151 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
155 /// Updates the log window with any new data which is in the log file.
\r
156 /// This is done every 5 seconds.
\r
158 /// <param name="state"></param>
\r
159 private void autoUpdate(object state)
\r
161 private void AutoUpdate(object state)
\r
165 _lastUpdate = false;
\r
166 UpdateTextFromThread();
\r
169 if (IsHandleCreated)
\r
171 if (_encodeQueue.isEncoding || _mainWin.isScanning)
\r
172 UpdateTextFromThread();
\r
175 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
176 if (_lastUpdate == false)
\r
177 UpdateTextFromThread();
\r
179 _lastUpdate = true; // Prevents the log window from being updated when there is no encode going.
\r
180 _position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused
\r
183 Thread.Sleep(1000);
\r
186 catch (Exception exc)
\r
188 MessageBox.Show("AutoUpdate(): Exception: \n" + exc);
\r
191 private void UpdateTextFromThread()
\r
195 String info = ReadFile();
\r
196 if (info.Contains("has exited"))
\r
197 info += "\n ############ End of Log ############## \n";
\r
201 catch (Exception exc)
\r
203 MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc);
\r
206 private void SetText(string text)
\r
210 if (IsHandleCreated)
\r
212 if (rtf_actLog.InvokeRequired)
\r
214 IAsyncResult invoked = BeginInvoke(new setTextCallback(SetText), new object[] { text });
\r
215 EndInvoke(invoked);
\r
218 rtf_actLog.AppendText(text);
\r
221 catch (Exception exc)
\r
223 MessageBox.Show("SetText(): Exception: \n" + exc);
\r
226 private String ReadFile()
\r
228 String appendText = String.Empty;
\r
231 // 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
232 // we'll need to make a copy of it.
\r
233 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
234 string logFile = Path.Combine(logDir, _readFile);
\r
235 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
237 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
238 if (File.Exists(logFile2))
\r
239 File.Delete(logFile2);
\r
241 // Copy the log file.
\r
242 if (File.Exists(logFile))
\r
243 File.Copy(logFile, logFile2, true);
\r
246 if (fileNotFoundQuickFix)
\r
248 fileNotFoundQuickFix = true;
\r
249 return "\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
252 StreamReader sr = new StreamReader(logFile2);
\r
255 while ((line = sr.ReadLine()) != null)
\r
259 appendText += line + Environment.NewLine;
\r
269 catch (Exception exc)
\r
271 return "Error in ReadFile() \n Unable to read the log file.\n You may have to restart HandBrake. Will try reading the file again in a few seconds... \n Error Information: \n\n" + exc;
\r
275 protected override void OnClosing(CancelEventArgs e)
\r
282 #region User Interface
\r
283 private void mnu_copy_log_Click(object sender, EventArgs e)
\r
285 if (rtf_actLog.SelectedText != "")
\r
286 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
288 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
290 private void mnu_openLogFolder_Click(object sender, EventArgs e)
\r
292 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
293 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
294 System.Diagnostics.Process prc = new System.Diagnostics.Process();
\r
295 prc.StartInfo.FileName = windir + @"\explorer.exe";
\r
296 prc.StartInfo.Arguments = logDir;
\r
299 private void btn_copy_Click(object sender, EventArgs e)
\r
301 if (rtf_actLog.SelectedText != "")
\r
302 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
304 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
306 private void btn_scan_log_Click(object sender, EventArgs e)
\r
310 private void btn_encode_log_Click(object sender, EventArgs e)
\r
316 #region System Information
\r
318 /// Returns the total physical ram in a system
\r
320 /// <returns></returns>
\r
321 public uint TotalPhysicalMemory()
\r
323 Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();
\r
324 Win32.GlobalMemoryStatus(ref memStatus);
\r
326 uint MemoryInfo = memStatus.dwTotalPhys;
\r
327 MemoryInfo = MemoryInfo / 1024 / 1024;
\r
333 /// Get the number of CPU Cores
\r
335 /// <returns>Object</returns>
\r
336 public Object getCpuCount()
\r
338 RegistryKey RegKey = Registry.LocalMachine;
\r
339 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
\r
340 return RegKey.GetValue("ProcessorNameString");
\r
344 /// Get the System screen size information.
\r
346 /// <returns>System.Windows.Forms.Scree</returns>
\r
347 public Screen screenBounds()
\r
349 return Screen.PrimaryScreen;
\r