// --------------------------------------------------------------------------------------------------------------------
//
// 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.Windows;
using HandBrakeWPF.Services.Scan.EventArgs;
using HandBrakeWPF.Services.Scan.Interfaces;
using HandBrakeWPF.ViewModels.Interfaces;
using EncodeCompletedEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeCompletedEventArgs;
using EncodeProgressEventArgs = HandBrakeWPF.Services.Encode.EventArgs.EncodeProgressEventArgs;
using IEncode = HandBrakeWPF.Services.Encode.Interfaces.IEncode;
///
/// The Log View Model
///
public class LogViewModel : ViewModelBase, ILogViewModel
{
#region Private Fields
///
/// Backing field for the encodeService service
///
private readonly IEncode encodeService;
///
/// Backing field for the Scan Service
///
private readonly IScan scanService;
///
/// The selected tab.
///
private int selectedTab;
///
/// The encode log index.
///
private int encodeLogIndex;
#endregion
///
/// Initializes a new instance of the class.
///
///
/// The encode service.
///
///
/// The scan service.
///
public LogViewModel(IEncode encodeService, IScan scanService)
{
this.encodeService = encodeService;
this.scanService = scanService;
this.Title = "Log Viewer";
this.encodeLogIndex = 0;
}
///
/// Gets or sets the selected tab.
///
public int SelectedTab
{
get
{
return this.selectedTab;
}
set
{
this.selectedTab = value;
this.NotifyOfPropertyChange(() => this.SelectedTab);
}
}
///
/// Gets Log.
///
public string ScanLog
{
get
{
return this.scanService.ActivityLog;
}
}
///
/// Gets the encodelog.
///
public string EncodeLog
{
get
{
return this.encodeService.ActivityLog;
}
}
///
/// Open the Log file directory
///
public void OpenLogDirectory()
{
string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
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()
{
Clipboard.SetDataObject(this.SelectedTab == 1 ? this.ScanLog : this.EncodeLog, true);
}
///
/// Handle the OnActivate Caliburn Event
///
protected override void OnActivate()
{
this.scanService.ScanCompleted += ScanServiceScanCompleted;
this.encodeService.EncodeCompleted += EncodeServiceEncodeCompleted;
this.encodeService.EncodeStatusChanged += this.EncodeServiceEncodeStatusChanged;
this.scanService.ScanStatusChanged += this.ScanServiceScanStatusChanged;
this.scanService.ScanStarted += this.scanService_ScanStared;
this.encodeService.EncodeStarted += this.encodeService_EncodeStarted;
base.OnActivate();
this.NotifyOfPropertyChange(() => this.ScanLog);
this.NotifyOfPropertyChange(() => this.EncodeLog);
}
///
/// Scan Status has changed, update log window.
///
///
/// The sender.
///
///
/// The e.
///
private void ScanServiceScanStatusChanged(object sender, ScanProgressEventArgs e)
{
this.NotifyOfPropertyChange(() => this.ScanLog);
}
///
/// Encode Status has changed, update log window
///
///
/// The sender.
///
///
/// The e.
///
private void EncodeServiceEncodeStatusChanged(object sender, EncodeProgressEventArgs e)
{
if (encodeLogIndex != this.encodeService.LogIndex || this.encodeService.LogIndex == -1)
{
this.NotifyOfPropertyChange(() => this.EncodeLog);
}
encodeLogIndex = this.encodeService.LogIndex;
}
///
/// Handle the OnDeactivate Caliburn Event
///
///
/// The close.
///
protected override void OnDeactivate(bool close)
{
this.scanService.ScanCompleted -= ScanServiceScanCompleted;
this.encodeService.EncodeCompleted -= EncodeServiceEncodeCompleted;
this.encodeService.EncodeStatusChanged -= this.EncodeServiceEncodeStatusChanged;
this.scanService.ScanStatusChanged -= this.ScanServiceScanStatusChanged;
this.scanService.ScanStarted -= this.scanService_ScanStared;
this.encodeService.EncodeStarted -= this.encodeService_EncodeStarted;
base.OnDeactivate(close);
}
///
/// Scan Completed Event Handler.
///
///
/// The sender.
///
///
/// The e.
///
private void ScanServiceScanCompleted(object sender, ScanCompletedEventArgs e)
{
this.NotifyOfPropertyChange(() => this.ScanLog);
}
///
/// Encode Completed Event Handler.
///
///
/// The sender.
///
///
/// The e.
///
private void EncodeServiceEncodeCompleted(object sender, EncodeCompletedEventArgs e)
{
this.NotifyOfPropertyChange(() => this.EncodeLog);
}
///
/// The encode service encode started.
///
///
/// The sender.
///
///
/// The e.
///
private void encodeService_EncodeStarted(object sender, EventArgs e)
{
this.encodeLogIndex = -1; // Reset the log index.
this.SelectedTab = 0;
}
///
/// The scan service scan stared.
///
///
/// The sender.
///
///
/// The e.
///
private void scanService_ScanStared(object sender, EventArgs e)
{
this.SelectedTab = 1;
}
}
}