summaryrefslogtreecommitdiffstats
path: root/win/CS/HandBrakeWPF/Commands/CancelScanCommand.cs
blob: 617778b089c4387f4445cd9f126bfe75965b1704 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
// --------------------------------------------------------------------------------------------------------------------
// <copyright file="CancelScanCommand.cs" company="HandBrake Project (http://handbrake.fr)">
//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
// </copyright>
// <summary>
//   Command to cancel a scan that is in progress
// </summary>
// --------------------------------------------------------------------------------------------------------------------

namespace HandBrakeWPF.Commands
{
    using System;
    using System.Windows.Input;

    using HandBrake.ApplicationServices.Services.Interfaces;

    /// <summary>
    /// Command to cancel a scan that is in progress
    /// </summary>
    public class CancelScanCommand : ICommand
    {
        /// <summary>
        /// The scan service wrapper.
        /// </summary>
        private readonly IScanServiceWrapper scanServiceWrapper;

        /// <summary>
        /// Initializes a new instance of the <see cref="CancelScanCommand"/> class.
        /// </summary>
        /// <param name="ssw">
        /// The scan service wrapper.
        /// </param>
        public CancelScanCommand(IScanServiceWrapper ssw)
        {
            this.scanServiceWrapper = ssw;
            this.scanServiceWrapper.ScanStared += this.ScanServiceWrapperScanStared;
            this.scanServiceWrapper.ScanCompleted += this.ScanServiceWrapperScanCompleted;
        }

        /// <summary>
        /// The scan service Scan Completed Event Handler.
        /// Fires CanExecuteChanged
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The ScanCompletedEventArgs.
        /// </param>
        private void ScanServiceWrapperScanCompleted(object sender, HandBrake.ApplicationServices.EventArgs.ScanCompletedEventArgs e)
        {
            Caliburn.Micro.Execute.OnUIThread(() => this.CanExecuteChanged(sender, EventArgs.Empty));    
        }

        /// <summary>
        /// The scan service scan started event handler.
        /// Fires CanExecuteChanged
        /// </summary>
        /// <param name="sender">
        /// The sender.
        /// </param>
        /// <param name="e">
        /// The EventArgs.
        /// </param>
        private void ScanServiceWrapperScanStared(object sender, EventArgs e)
        {
            Caliburn.Micro.Execute.OnUIThread(() => this.CanExecuteChanged(sender, EventArgs.Empty));    
        }

        #region Implementation of ICommand

        /// <summary>
        /// Defines the method to be called when the command is invoked.
        /// </summary>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public void Execute(object parameter)
        {
            this.scanServiceWrapper.Stop();
        }

        /// <summary>
        /// Defines the method that determines whether the command can execute in its current state.
        /// </summary>
        /// <returns>
        /// true if this command can be executed; otherwise, false.
        /// </returns>
        /// <param name="parameter">Data used by the command.  If the command does not require data to be passed, this object can be set to null.</param>
        public bool CanExecute(object parameter)
        {
            if (this.scanServiceWrapper != null)
            {
                return this.scanServiceWrapper.IsScanning;
            }

            return false;
        }

        /// <summary>
        /// The can execute changed.
        /// </summary>
        public event EventHandler CanExecuteChanged;

        #endregion
    }
}