// --------------------------------------------------------------------------------------------------------------------
//
// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
//
//
// We have multiple implementations of IScan. This is a wrapper class for the GUI so that the
// implementation used is controllable via user settings.
// Over time, this class will go away when the LibHB and process isolation code matures.
//
// --------------------------------------------------------------------------------------------------------------------
namespace HandBrakeWPF.Services
{
using System;
using HandBrake.ApplicationServices.Exceptions;
using HandBrake.ApplicationServices.Isolation;
using HandBrake.ApplicationServices.Parsing;
using HandBrake.ApplicationServices.Services;
using HandBrake.ApplicationServices.Services.Interfaces;
using HandBrake.Interop;
using HandBrake.Interop.Interfaces;
///
/// We have multiple implementations of IScan. This is a wrapper class for the GUI so that the
/// implementation used is controllable via user settings.
/// Over time, this class will go away when the LibHB and process isolation code matures.
///
public class ScanServiceWrapper : IScanServiceWrapper
{
#region Constants and Fields
///
/// The handbrake instance.
///
public static IHandBrakeInstance HandbrakeInstance;
///
/// The scan service.
///
private readonly IScan scanService;
#endregion
#region Constructors and Destructors
///
/// Initializes a new instance of the class.
///
///
/// The user setting service.
///
public ScanServiceWrapper(IUserSettingService userSettingService)
{
var useLibHb = userSettingService.GetUserSetting(UserSettingConstants.EnableLibHb);
var useProcessIsolation =
userSettingService.GetUserSetting(UserSettingConstants.EnableProcessIsolation);
string port = userSettingService.GetUserSetting(UserSettingConstants.ServerPort);
if (useLibHb)
{
try
{
if (useProcessIsolation)
{
this.scanService = new IsolatedScanService(port);
}
else
{
HandbrakeInstance = new HandBrakeInstance();
this.scanService = new LibScan(HandbrakeInstance);
}
}
catch(Exception exc)
{
// Try to recover from errors.
userSettingService.SetUserSetting(UserSettingConstants.EnableLibHb, false);
throw new GeneralApplicationException("Unable to initialise LibHB or Background worker service", "Falling back to using HandBrakeCLI.exe. Setting has been reset", exc);
}
}
else
{
this.scanService = new ScanService(userSettingService);
}
this.scanService.ScanCompleted += this.ScanServiceScanCompleted;
this.scanService.ScanStared += this.ScanServiceScanStared;
this.scanService.ScanStatusChanged += this.ScanServiceScanStatusChanged;
}
///
/// The scan service scan status changed event
///
///
/// The sender.
///
///
/// The ScanProgressEventArgs.
///
private void ScanServiceScanStatusChanged(object sender, HandBrake.ApplicationServices.EventArgs.ScanProgressEventArgs e)
{
this.ScanStatusChanged(sender, e);
}
///
/// The scan service scan stared event
///
///
/// The sender.
///
///
/// The EventArgs
///
private void ScanServiceScanStared(object sender, EventArgs e)
{
this.ScanStared(sender, e);
}
///
/// The scan service scan completed event
///
///
/// The sender.
///
///
/// The ScanCompletedEventArgs
///
private void ScanServiceScanCompleted(object sender, HandBrake.ApplicationServices.EventArgs.ScanCompletedEventArgs e)
{
this.ScanCompleted(sender, e);
}
#endregion
#region Events
///
/// The scan completed.
///
public event ScanCompletedStatus ScanCompleted;
///
/// The scan stared.
///
public event EventHandler ScanStared;
///
/// The scan status changed.
///
public event ScanProgessStatus ScanStatusChanged;
#endregion
#region Properties
///
/// Gets ActivityLog.
///
public string ActivityLog
{
get
{
return this.scanService.ActivityLog;
}
}
///
/// Gets a value indicating whether IsScanning.
///
public bool IsScanning
{
get
{
return this.scanService.IsScanning;
}
}
///
/// Gets the Souce Data.
///
public Source SouceData
{
get
{
return this.scanService.SouceData;
}
}
#endregion
#region Implemented Interfaces
#region IScan
///
/// Take a Scan Log file, and process it as if it were from the CLI.
///
///
/// The path to the log file.
///
public void DebugScanLog(string path)
{
this.scanService.DebugScanLog(path);
}
///
/// Shutdown the service.
///
public void Shutdown()
{
this.scanService.Shutdown();
}
///
/// Scan a Source Path.
/// Title 0: scan all
///
///
/// Path to the file to scan
///
///
/// int title number. 0 for scan all
///
///
/// The preview Count.
///
///
/// The post Action.
///
public void Scan(string sourcePath, int title, int previewCount, Action postAction)
{
this.scanService.Scan(sourcePath, title, previewCount, postAction);
}
///
/// Kill the scan
///
public void Stop()
{
this.scanService.Stop();
}
#endregion
#endregion
}
}