// -------------------------------------------------------------------------------------------------------------------- // // This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. // // // Command to cancel a scan that is in progress // // -------------------------------------------------------------------------------------------------------------------- namespace HandBrakeWPF.Commands { using System; using System.Windows.Input; using HandBrake.ApplicationServices.Services.Interfaces; using HandBrake.ApplicationServices.Services.Scan.EventArgs; using HandBrake.ApplicationServices.Services.Scan.Interfaces; /// /// Command to cancel a scan that is in progress /// public class CancelScanCommand : ICommand { /// /// The scan service wrapper. /// private readonly IScan scanServiceWrapper; /// /// Initializes a new instance of the class. /// /// /// The scan service wrapper. /// public CancelScanCommand(IScan ssw) { this.scanServiceWrapper = ssw; this.scanServiceWrapper.ScanStared += this.ScanServiceWrapperScanStared; this.scanServiceWrapper.ScanCompleted += this.ScanServiceWrapperScanCompleted; } /// /// The scan service Scan Completed Event Handler. /// Fires CanExecuteChanged /// /// /// The sender. /// /// /// The ScanCompletedEventArgs. /// private void ScanServiceWrapperScanCompleted(object sender, ScanCompletedEventArgs e) { Caliburn.Micro.Execute.OnUIThread(this.OnCanExecuteChanged); } /// /// The scan service scan started event handler. /// Fires CanExecuteChanged /// /// /// The sender. /// /// /// The EventArgs. /// private void ScanServiceWrapperScanStared(object sender, EventArgs e) { Caliburn.Micro.Execute.OnUIThread(this.OnCanExecuteChanged); } #region Implementation of ICommand /// /// Defines the method to be called when the command is invoked. /// /// Data used by the command. If the command does not require data to be passed, this object can be set to null. public void Execute(object parameter) { this.scanServiceWrapper.Stop(); } /// /// Defines the method that determines whether the command can execute in its current state. /// /// /// true if this command can be executed; otherwise, false. /// /// Data used by the command. If the command does not require data to be passed, this object can be set to null. public bool CanExecute(object parameter) { if (this.scanServiceWrapper != null) { return this.scanServiceWrapper.IsScanning; } return false; } /// /// The can execute changed. /// public event EventHandler CanExecuteChanged; /// /// The on can execute changed. /// protected virtual void OnCanExecuteChanged() { EventHandler handler = this.CanExecuteChanged; if (handler != null) { handler(this, EventArgs.Empty); } } #endregion } }