1 /* frmActivityWindow.cs $
\r
2 This file is part of the HandBrake source code.
\r
3 Homepage: <http://handbrake.fr>.
\r
4 It may be used under the terms of the GNU General Public License. */
\r
9 using System.ComponentModel;
\r
10 using System.Diagnostics;
\r
13 using System.Threading;
\r
14 using System.Windows.Forms;
\r
16 using HandBrake.Framework.Services;
\r
17 using HandBrake.Framework.Services.Interfaces;
\r
18 using HandBrake.ApplicationServices.Services.Interfaces;
\r
21 using Timer = System.Threading.Timer;
\r
24 /// The Activity Log Window
\r
26 public partial class frmActivityWindow : Form
\r
28 /* Private Variables */
\r
31 /// The Encode Object
\r
33 private readonly IQueue encode;
\r
38 private readonly IScan scan;
\r
41 /// The Error service
\r
43 private readonly IErrorService errorService = new ErrorService();
\r
46 /// The current position in the log file
\r
48 private int position;
\r
51 /// A Timer for this window
\r
53 private Timer windowTimer;
\r
56 /// The Type of log that the window is currently dealing with
\r
58 private ActivityLogMode mode;
\r
63 /// Initializes a new instance of the <see cref="frmActivityWindow"/> class.
\r
65 /// <param name="encode">
\r
68 /// <param name="scan">
\r
71 public frmActivityWindow(IQueue encode, IScan scan)
\r
73 InitializeComponent();
\r
75 this.encode = encode;
\r
79 // Listen for Scan and Encode Starting Events
\r
80 scan.ScanStared += scan_ScanStared;
\r
81 encode.EncodeStarted += encode_EncodeStarted;
\r
87 /// A callback function for updating the ui
\r
89 /// <param name="text">
\r
92 private delegate void SetTextCallback(StringBuilder text);
\r
95 /// Clear text callback
\r
97 private delegate void SetTextClearCallback();
\r
100 /// Set mode callback
\r
102 /// <param name="setMode">
\r
105 private delegate void SetModeCallback(ActivityLogMode setMode);
\r
107 /* Private Methods */
\r
110 /// Set the window to scan mode
\r
112 /// <param name="setMode">
\r
115 private void SetMode(ActivityLogMode setMode)
\r
117 if (IsHandleCreated)
\r
119 if (rtf_actLog.InvokeRequired)
\r
121 IAsyncResult invoked = BeginInvoke(new SetModeCallback(SetMode), new object[] { setMode });
\r
122 EndInvoke(invoked);
\r
127 this.mode = setMode;
\r
129 Array values = Enum.GetValues(typeof(ActivityLogMode));
\r
130 Properties.Settings.Default.ActivityWindowLastMode = (int)values.GetValue(Convert.ToInt32(setMode));
\r
131 Properties.Settings.Default.Save();
\r
133 this.Text = mode == ActivityLogMode.Scan
\r
134 ? "Activity Window (Scan Log)"
\r
135 : "Activity Window (Encode Log)";
\r
137 if (mode == ActivityLogMode.Scan)
\r
139 scan.ScanCompleted += stopWindowRefresh;
\r
140 encode.EncodeEnded -= stopWindowRefresh;
\r
144 scan.ScanCompleted -= stopWindowRefresh;
\r
145 encode.EncodeEnded += stopWindowRefresh;
\r
148 // Start a fresh window timer
\r
149 windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);
\r
155 /// On Window load, start a new timer
\r
157 /// <param name="sender">
\r
160 /// <param name="e">
\r
163 private void ActivityWindowLoad(object sender, EventArgs e)
\r
167 // Set the inital log file.
\r
168 if (encode.IsEncoding)
\r
170 this.logSelector.SelectedIndex = 1;
\r
172 else if (scan.IsScanning)
\r
174 this.logSelector.SelectedIndex = 0;
\r
178 // Otherwise, use the last mode the window was in.
\r
179 ActivityLogMode activitLogMode = (ActivityLogMode)Enum.ToObject(typeof(ActivityLogMode), Properties.Settings.Default.ActivityWindowLastMode);
\r
180 this.logSelector.SelectedIndex = activitLogMode == ActivityLogMode.Scan ? 0 : 1;
\r
183 catch (Exception exc)
\r
185 errorService.ShowError("Error during load.", exc.ToString());
\r
190 /// Set the Log window to encode mode when an encode starts.
\r
192 /// <param name="sender">
\r
195 /// <param name="e">
\r
198 private void encode_EncodeStarted(object sender, EventArgs e)
\r
200 SetMode(ActivityLogMode.Encode);
\r
204 /// Set the log widow to scan mode when a scan starts
\r
206 /// <param name="sender">
\r
209 /// <param name="e">
\r
212 private void scan_ScanStared(object sender, EventArgs e)
\r
214 SetMode(ActivityLogMode.Scan);
\r
218 /// Stop refreshing the window when no scanning or encoding is happening.
\r
220 /// <param name="sender">
\r
223 /// <param name="e">
\r
226 private void stopWindowRefresh(object sender, EventArgs e)
\r
228 windowTimer.Dispose();
\r
234 /// Append new text to the window
\r
236 /// <param name="n">
\r
239 private void LogMonitor(object n)
\r
241 AppendWindowText(GetLog());
\r
245 /// New Code for getting the Activity log from the Services rather than reading a file.
\r
248 /// The StringBuilder containing a log
\r
250 private StringBuilder GetLog()
\r
252 StringBuilder appendText = new StringBuilder();
\r
256 if (this.mode == ActivityLogMode.Scan)
\r
258 if (scan == null || scan.ActivityLog == string.Empty)
\r
260 appendText.AppendFormat("Waiting for the log to be generated ...\n");
\r
266 using (StringReader reader = new StringReader(scan.ActivityLog))
\r
268 LogReader(reader, appendText);
\r
273 if (encode == null || encode.ActivityLog == string.Empty)
\r
275 appendText.AppendFormat("Waiting for the log to be generated ...\n");
\r
281 using (StringReader reader = new StringReader(encode.ActivityLog))
\r
283 LogReader(reader, appendText);
\r
287 catch (Exception exc)
\r
289 windowTimer.Dispose();
\r
290 errorService.ShowError("GetLog() Error", exc.ToString());
\r
297 /// Reads the log data from a Scan or Encode object
\r
299 /// <param name="reader">
\r
302 /// <param name="appendText">
\r
303 /// The append text.
\r
305 private void LogReader(StringReader reader, StringBuilder appendText)
\r
309 while ((line = reader.ReadLine()) != null)
\r
313 appendText.AppendLine(line);
\r
321 /// Append text to the RTF box
\r
323 /// <param name="text">
\r
326 private void AppendWindowText(StringBuilder text)
\r
330 if (IsHandleCreated)
\r
332 if (rtf_actLog.InvokeRequired)
\r
334 IAsyncResult invoked = BeginInvoke(new SetTextCallback(AppendWindowText), new object[] { text });
\r
335 EndInvoke(invoked);
\r
339 rtf_actLog.AppendText(text.ToString());
\r
341 // Stop the refresh process if log has finished.
\r
342 if (text.ToString().Contains("HandBrake has Exited"))
\r
344 windowTimer.Dispose();
\r
355 /// Clear the contents of the log window
\r
357 private void ClearWindowText()
\r
361 if (IsHandleCreated)
\r
363 if (rtf_actLog.InvokeRequired)
\r
365 IAsyncResult invoked = BeginInvoke(new SetTextClearCallback(ClearWindowText));
\r
366 EndInvoke(invoked);
\r
370 rtf_actLog.Clear();
\r
380 /// Reset Everything
\r
382 private void Reset()
\r
384 if (windowTimer != null)
\r
385 windowTimer.Dispose();
\r
388 windowTimer = new Timer(new TimerCallback(LogMonitor), null, 1000, 1000);
\r
391 /* Menus and Buttons */
\r
394 /// Copy log to clipboard
\r
396 /// <param name="sender">
\r
399 /// <param name="e">
\r
402 private void MnuCopyLogClick(object sender, EventArgs e)
\r
404 Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);
\r
408 /// Open the log folder
\r
410 /// <param name="sender">
\r
413 /// <param name="e">
\r
416 private void MnuOpenLogFolderClick(object sender, EventArgs e)
\r
418 string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
\r
419 string windir = Environment.GetEnvironmentVariable("WINDIR");
\r
420 Process prc = new Process
\r
424 FileName = windir + @"\explorer.exe",
\r
434 /// <param name="sender">
\r
437 /// <param name="e">
\r
440 private void BtnCopyClick(object sender, EventArgs e)
\r
442 Clipboard.SetDataObject(rtf_actLog.SelectedText != string.Empty ? rtf_actLog.SelectedText : rtf_actLog.Text, true);
\r
446 /// Change the Log file in the viewer
\r
448 /// <param name="sender">The Sender </param>
\r
449 /// <param name="e">The EventArgs</param>
\r
450 private void LogSelectorClick(object sender, EventArgs e)
\r
452 this.SetMode((string)this.logSelector.SelectedItem == "Scan Log" ? ActivityLogMode.Scan : ActivityLogMode.Encode);
\r
458 /// override onclosing
\r
460 /// <param name="e">
\r
463 protected override void OnClosing(CancelEventArgs e)
\r
465 scan.ScanStared -= scan_ScanStared;
\r
466 encode.EncodeStarted -= encode_EncodeStarted;
\r
468 scan.ScanCompleted -= stopWindowRefresh;
\r
469 encode.EncodeEnded -= stopWindowRefresh;
\r
471 windowTimer.Dispose();
\r