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 if (file == "last_scan_log.txt")
\r
41 // Start a new thread which will montior and keep the log window up to date if required/
\r
44 Thread monitor = new Thread(AutoUpdate) { IsBackground = true };
\r
47 catch (Exception exc)
\r
49 MessageBox.Show("startLogThread(): Exception: \n" + exc, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
\r
54 /// Set the view which the Log window displays.
\r
58 /// <param name="scan">Boolean. Scan = true</param>
\r
59 public void SetLogView(Boolean scan)
\r
63 rtf_actLog.Text = String.Empty;
\r
65 // Print the log header
\r
66 rtf_actLog.AppendText(String.Format("### Windows GUI {1} {0} \n", Properties.Settings.Default.hb_build, Properties.Settings.Default.hb_version));
\r
67 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion));
\r
68 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", getCpuCount()));
\r
69 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", TotalPhysicalMemory()));
\r
70 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", screenBounds().Bounds.Width, screenBounds().Bounds.Height));
\r
71 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
72 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
73 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
74 rtf_actLog.AppendText("#########################################\n\n");
\r
75 if ((!_encodeQueue.LastEncode.IsEmpty) && _encodeQueue.LastEncode.Query != String.Empty)
\r
77 rtf_actLog.AppendText("### CLI Query: " + _encodeQueue.LastEncode.Query + "\n\n");
\r
78 rtf_actLog.AppendText("#########################################\n\n");
\r
81 // Seutp the log file
\r
84 txt_log.Text = "Scan Log";
\r
85 _readFile = "last_scan_log.txt";
\r
89 _readFile = "last_encode_log.txt";
\r
90 txt_log.Text = "Encode Log";
\r
92 _lastUpdate = false;
\r
95 private void AutoUpdate(object state)
\r
99 _lastUpdate = false;
\r
100 UpdateTextFromThread();
\r
103 if (IsHandleCreated)
\r
105 if (_encodeQueue.isEncoding || _mainWin.isScanning)
\r
106 UpdateTextFromThread();
\r
109 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
110 if (_lastUpdate == false)
\r
111 UpdateTextFromThread();
\r
113 _lastUpdate = true; // Prevents the log window from being updated when there is no encode going.
\r
114 _position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused
\r
117 Thread.Sleep(1000);
\r
120 catch (Exception exc)
\r
122 MessageBox.Show("AutoUpdate(): Exception: \n" + exc);
\r
125 private void UpdateTextFromThread()
\r
129 String info = ReadFile();
\r
130 if (info.Contains("has exited"))
\r
131 info += "\n ############ End of Log ############## \n";
\r
135 catch (Exception exc)
\r
137 MessageBox.Show("UpdateTextFromThread(): Exception: \n" + exc);
\r
140 private void SetText(string text)
\r
144 if (IsHandleCreated)
\r
146 if (rtf_actLog.InvokeRequired)
\r
148 IAsyncResult invoked = BeginInvoke(new setTextCallback(SetText), new object[] { text });
\r
149 EndInvoke(invoked);
\r
152 rtf_actLog.AppendText(text);
\r
155 catch (Exception exc)
\r
157 MessageBox.Show("SetText(): Exception: \n" + exc);
\r
160 private String ReadFile()
\r
162 String appendText = String.Empty;
\r
165 // 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
166 // we'll need to make a copy of it.
\r
167 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
168 string logFile = Path.Combine(logDir, _readFile);
\r
169 string logFile2 = Path.Combine(logDir, "tmp_appReadable_log.txt");
\r
171 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
172 if (File.Exists(logFile2))
\r
173 File.Delete(logFile2);
\r
175 // Copy the log file.
\r
176 if (File.Exists(logFile))
\r
177 File.Copy(logFile, logFile2);
\r
180 if (fileNotFoundQuickFix)
\r
182 fileNotFoundQuickFix = true;
\r
183 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
187 // Open the copied log file for reading
\r
188 StreamReader sr = new StreamReader(logFile2);
\r
191 while ((line = sr.ReadLine()) != null)
\r
195 appendText += line + Environment.NewLine;
\r
205 catch (Exception exc)
\r
207 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
212 protected override void OnClosing(CancelEventArgs e)
\r
219 #region User Interface
\r
220 private void mnu_copy_log_Click(object sender, EventArgs e)
\r
222 if (rtf_actLog.SelectedText != "")
\r
223 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
225 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
227 private void mnu_openLogFolder_Click(object sender, EventArgs e)
\r
229 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
230 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
231 System.Diagnostics.Process prc = new System.Diagnostics.Process();
\r
232 prc.StartInfo.FileName = windir + @"\explorer.exe";
\r
233 prc.StartInfo.Arguments = logDir;
\r
236 private void btn_copy_Click(object sender, EventArgs e)
\r
238 if (rtf_actLog.SelectedText != "")
\r
239 Clipboard.SetDataObject(rtf_actLog.SelectedText, true);
\r
241 Clipboard.SetDataObject(rtf_actLog.Text, true);
\r
243 private void btn_scan_log_Click(object sender, EventArgs e)
\r
247 private void btn_encode_log_Click(object sender, EventArgs e)
\r
253 #region System Information
\r
255 /// Returns the total physical ram in a system
\r
257 /// <returns></returns>
\r
258 public uint TotalPhysicalMemory()
\r
260 Win32.MEMORYSTATUS memStatus = new Win32.MEMORYSTATUS();
\r
261 Win32.GlobalMemoryStatus(ref memStatus);
\r
263 uint MemoryInfo = memStatus.dwTotalPhys;
\r
264 MemoryInfo = MemoryInfo / 1024 / 1024;
\r
270 /// Get the number of CPU Cores
\r
272 /// <returns>Object</returns>
\r
273 public Object getCpuCount()
\r
275 RegistryKey RegKey = Registry.LocalMachine;
\r
276 RegKey = RegKey.OpenSubKey("HARDWARE\\DESCRIPTION\\System\\CentralProcessor\\0");
\r
277 return RegKey.GetValue("ProcessorNameString");
\r
281 /// Get the System screen size information.
\r
283 /// <returns>System.Windows.Forms.Scree</returns>
\r
284 public Screen screenBounds()
\r
286 return Screen.PrimaryScreen;
\r