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;
\r
9 using System.ComponentModel;
\r
11 using System.Drawing;
\r
13 using System.Windows.Forms;
\r
15 using System.Threading;
\r
16 using System.Diagnostics;
\r
17 using System.Runtime.InteropServices;
\r
22 public partial class frmActivityWindow : Form
\r
27 frmQueue queueWindow;
\r
28 int position = 0; // Position in the arraylist reached by the current log output in the rtf box.
\r
31 /// This window should be used to display the RAW output of the handbrake CLI which is produced during an encode.
\r
34 public frmActivityWindow(string file, frmMain fm, frmQueue fq)
\r
36 InitializeComponent();
\r
37 this.rtf_actLog.Text = string.Empty;
\r
44 // System Information
\r
45 Functions.SystemInfo info = new Functions.SystemInfo();
\r
47 // Add a header to the log file indicating that it's from the Windows GUI and display the windows version
\r
48 rtf_actLog.AppendText("### Windows GUI \n");
\r
49 rtf_actLog.AppendText(String.Format("### Running: {0} \n###\n", Environment.OSVersion.ToString()));
\r
50 rtf_actLog.AppendText(String.Format("### CPU: {0} \n", info.getCpuCount()));
\r
51 rtf_actLog.AppendText(String.Format("### Ram: {0} MB \n", info.TotalPhysicalMemory()));
\r
52 rtf_actLog.AppendText(String.Format("### Screen: {0}x{1} \n", info.screenBounds().Bounds.Width, info.screenBounds().Bounds.Height));
\r
53 rtf_actLog.AppendText(String.Format("### Temp Dir: {0} \n", Path.GetTempPath()));
\r
54 rtf_actLog.AppendText(String.Format("### Install Dir: {0} \n", Application.StartupPath));
\r
55 rtf_actLog.AppendText(String.Format("### Data Dir: {0} \n", Application.UserAppDataPath));
\r
56 rtf_actLog.AppendText("#########################################\n\n");
\r
58 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
59 if (File.Exists(logFile))
\r
61 // Start a new thread to run the autoUpdate process
\r
62 monitor = new Thread(autoUpdate);
\r
63 monitor.IsBackground = true;
\r
67 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
69 // When the window closes, we want to abort the monitor thread.
\r
70 this.Disposed += new EventHandler(forceQuit);
\r
73 private void forceQuit(object sender, EventArgs e)
\r
75 if (monitor != null)
\r
81 // Update the Activity window every 5 seconds with the latest log data.
\r
82 private void autoUpdate(object state)
\r
84 Boolean lastUpdate = false;
\r
85 updateTextFromThread();
\r
88 if ((mainWindow.isEncoding() == true) || (queueWindow.isEncoding() == true))
\r
89 updateTextFromThread();
\r
92 // The encode may just have stoped, so, refresh the log one more time before restarting it.
\r
93 if (lastUpdate == false)
\r
94 updateTextFromThread();
\r
96 lastUpdate = true; // Prevents the log window from being updated when there is no encode going.
\r
97 position = 0; // There is no encoding, so reset the log position counter to 0 so it can be reused
\r
103 private void updateTextFromThread()
\r
106 ArrayList data = readFile();
\r
108 while (position < data.Count)
\r
110 text = data[position].ToString();
\r
111 if (data[position].ToString().Contains("has exited"))
\r
112 text = "\n ############ End of Encode ############## \n";
\r
118 delegate void SetTextCallback(string text);
\r
119 private void SetText(string text)
\r
121 // InvokeRequired required compares the thread ID of the
\r
122 // calling thread to the thread ID of the creating thread.
\r
123 // If these threads are different, it returns true.
\r
124 if (this.rtf_actLog.InvokeRequired)
\r
126 SetTextCallback d = new SetTextCallback(SetText);
\r
127 this.Invoke(d, new object[] { text });
\r
131 this.rtf_actLog.AppendText(text);
\r
135 private ArrayList readFile()
\r
137 // Ok, the task here is to, Get an arraylist of log data.
\r
138 // And update some global varibles which are pointers to the last displayed log line.
\r
139 ArrayList logData = new ArrayList();
\r
143 // 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
144 // we'll need to make a copy of it.
\r
145 string logFile = Path.Combine(Path.GetTempPath(), read_file);
\r
146 string logFile2 = Path.Combine(Path.GetTempPath(), "hb_encode_log_AppReadable.dat");
\r
148 // Make sure the application readable log file does not already exist. FileCopy fill fail if it does.
\r
149 if (File.Exists(logFile2))
\r
150 File.Delete(logFile2);
\r
152 // Copy the log file.
\r
153 File.Copy(logFile, logFile2);
\r
155 // Open the copied log file for reading
\r
156 StreamReader sr = new StreamReader(logFile2);
\r
157 string line = sr.ReadLine();
\r
158 while (line != null)
\r
160 if (line.Trim() != "")
\r
161 logData.Add(line + System.Environment.NewLine);
\r
163 line = sr.ReadLine();
\r
170 catch (Exception exc)
\r
172 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.ToString(), "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
\r