// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// Defines the LogViewModel type.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.ViewModels
{
using System;
using System.Diagnostics;
using System.Text;
using System.Windows;
using Caliburn.Micro;
using HandBrakeWPF.Properties;
using HandBrakeWPF.Services.Interfaces;
using HandBrakeWPF.Utilities;
using HandBrakeWPF.ViewModels.Interfaces;
using ILog = HandBrakeWPF.Services.Logging.Interfaces.ILog;
using LogEventArgs = HandBrakeWPF.Services.Logging.EventArgs.LogEventArgs;
using LogMessage = HandBrakeWPF.Services.Logging.Model.LogMessage;
using LogService = HandBrakeWPF.Services.Logging.LogService;
///
/// The Log View Model
///
public class LogViewModel : ViewModelBase, ILogViewModel
{
private readonly IErrorService errorService;
private readonly ILog logService;
private StringBuilder log = new StringBuilder();
private long lastReadIndex;
public LogViewModel(IErrorService errorService, ILog logService)
{
this.errorService = errorService;
this.logService = logService;
this.Title = Resources.LogViewModel_Title;
}
///
/// Gets Log.
///
public string ActivityLog
{
get
{
return this.log.ToString();
}
}
///
/// The log message received.
///
public event EventHandler LogMessageReceived;
///
/// Open the Log file directory
///
public void OpenLogDirectory()
{
string logDir = DirectoryUtilities.GetLogDirectory();
string windir = Environment.GetEnvironmentVariable("WINDIR");
Process prc = new Process { StartInfo = { FileName = windir + @"\explorer.exe", Arguments = logDir } };
prc.Start();
}
///
/// Copy the log file to the system clipboard
///
public void CopyLog()
{
try
{
Clipboard.SetDataObject(this.ActivityLog, true);
}
catch (Exception exc)
{
this.errorService.ShowError(Resources.Clipboard_Unavailable, Resources.Clipboard_Unavailable_Solution, exc);
}
}
///
/// Handle the OnActivate Caliburn Event
///
protected override void OnActivate()
{
this.logService.MessageLogged += this.LogService_MessageLogged;
this.logService.LogReset += LogService_LogReset;
// Refresh the Log Display
this.log.Clear();
foreach (LogMessage logMessage in this.logService.LogMessages)
{
this.log.AppendLine(logMessage.Content);
this.lastReadIndex = logMessage.MessageIndex;
if (this.lastReadIndex > logMessage.MessageIndex)
{
throw new Exception("Log Message Index Error");
}
}
this.OnLogMessageReceived(null);
this.NotifyOfPropertyChange(() => this.ActivityLog);
base.OnActivate();
}
///
/// Trigger a faster / smoother way of updating the log window.
///
///
/// The e.
///
protected virtual void OnLogMessageReceived(LogEventArgs e)
{
var onLogMessageReceived = this.LogMessageReceived;
if (onLogMessageReceived != null)
{
onLogMessageReceived.Invoke(this, e);
}
}
///
/// Handle the OnDeactivate Caliburn Event
///
///
/// The close.
///
protected override void OnDeactivate(bool close)
{
this.logService.MessageLogged -= this.LogService_MessageLogged;
this.logService.LogReset -= this.LogService_LogReset;
base.OnDeactivate(close);
}
///
/// The log service_ log reset.
///
///
/// The sender.
///
///
/// The e.
///
private void LogService_LogReset(object sender, EventArgs e)
{
this.log.Clear();
this.lastReadIndex = 0;
foreach (LogMessage logMessage in this.logService.LogMessages)
{
this.log.AppendLine(logMessage.Content);
this.lastReadIndex = logMessage.MessageIndex;
if (this.lastReadIndex > logMessage.MessageIndex)
{
throw new Exception("Log Message Index Error");
}
}
this.NotifyOfPropertyChange(() => this.ActivityLog);
this.OnLogMessageReceived(null);
}
///
/// The log service_ message logged.
///
///
/// The sender.
///
///
/// The e.
///
private void LogService_MessageLogged(object sender, LogEventArgs e)
{
if (this.lastReadIndex < e.Log.MessageIndex)
{
Execute.OnUIThreadAsync(() =>
{
this.lastReadIndex = e.Log.MessageIndex;
this.log.AppendLine(e.Log.Content);
this.OnLogMessageReceived(e);
this.NotifyOfPropertyChange(() => this.ActivityLog);
});
}
}
}
}