diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 284a2a8..b587625 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -214,6 +214,11 @@ namespace FileConverter for (int index = 1; index < args.Length; index++) { string argument = args[index]; + if (string.IsNullOrEmpty(argument)) + { + continue; + } + if (argument.StartsWith("--")) { // This is an optional parameter. diff --git a/Application/FileConverter/ConversionJobs/CancelConversionJobCommand.cs b/Application/FileConverter/ConversionJobs/CancelConversionJobCommand.cs new file mode 100644 index 0000000..998c898 --- /dev/null +++ b/Application/FileConverter/ConversionJobs/CancelConversionJobCommand.cs @@ -0,0 +1,45 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter.ConversionJobs +{ + using System; + using System.Windows.Input; + + public class CancelConversionJobCommand : ICommand + { + private readonly ConversionJob conversionJob; + + public CancelConversionJobCommand(ConversionJob conversionJob) + { + this.conversionJob = conversionJob; + this.conversionJob.PropertyChanged += this.ConversionJob_PropertyChanged; + } + + public event EventHandler CanExecuteChanged; + + public bool CanExecute(object parameter) + { + if (this.conversionJob == null) + { + return false; + } + + return this.conversionJob.IsCancelable && this.conversionJob.State == ConversionJob.ConversionState.InProgress; + } + + public void Execute(object parameter) + { + this.conversionJob?.Cancel(); + } + + private void ConversionJob_PropertyChanged(object sender, System.ComponentModel.PropertyChangedEventArgs eventArgs) + { + if (eventArgs.PropertyName != "State") + { + return; + } + + this.CanExecuteChanged?.Invoke(this, new EventArgs()); + } + } +} \ No newline at end of file diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index 0ec5193..8fd9516 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -16,6 +16,7 @@ namespace FileConverter.ConversionJobs private string errorMessage = string.Empty; private string initialInputPath = string.Empty; private string userState = string.Empty; + private CancelConversionJobCommand cancelCommand; public ConversionJob() { @@ -126,6 +127,31 @@ namespace FileConverter.ConversionJobs protected set; } + public CancelConversionJobCommand CancelCommand + { + get + { + if (this.cancelCommand == null) + { + this.cancelCommand = new CancelConversionJobCommand(this); + } + + return this.cancelCommand; + } + } + + public bool IsCancelable + { + get; + protected set; + } + + protected bool CancelIsRequested + { + get; + private set; + } + protected virtual InputPostConversionAction InputPostConversionAction { get @@ -280,6 +306,17 @@ namespace FileConverter.ConversionJobs } } + public virtual void Cancel() + { + if (!this.IsCancelable || this.State != ConversionState.InProgress) + { + return; + } + + this.CancelIsRequested = true; + this.ConversionFailed("Canceled."); + } + protected virtual void Convert() { } diff --git a/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs b/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs index ca9e6f8..7e3405d 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob_FFMPEG.cs @@ -25,12 +25,14 @@ namespace FileConverter.ConversionJobs public ConversionJob_FFMPEG() : base() { + this.IsCancelable = true; } public ConversionJob_FFMPEG(ConversionPreset conversionPreset) : base(conversionPreset) { + this.IsCancelable = true; } - + protected virtual string FfmpegPath { get @@ -363,6 +365,11 @@ namespace FileConverter.ConversionJobs { while (!reader.EndOfStream) { + if (this.CancelIsRequested && !exeProcess.HasExited) + { + exeProcess.Kill(); + } + string result = reader.ReadLine(); this.ParseFFMPEGOutput(result); diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 1e97239..d4b6edd 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -91,6 +91,7 @@ EncodingQualitySliderControl.xaml + @@ -104,6 +105,7 @@ + @@ -127,6 +129,7 @@ + diff --git a/Application/FileConverter/ValueConverters/Generic/ValueToString.cs b/Application/FileConverter/ValueConverters/Generic/ValueToString.cs new file mode 100644 index 0000000..e0fc089 --- /dev/null +++ b/Application/FileConverter/ValueConverters/Generic/ValueToString.cs @@ -0,0 +1,21 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter.ValueConverters.Generic +{ + using System; + using System.Globalization; + using System.Windows.Data; + + public class ValueToString : IValueConverter + { + public object Convert(object value, Type targetType, object parameter, CultureInfo culture) + { + return value?.ToString(); + } + + public object ConvertBack(object value, Type targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Application/FileConverter/Windows/MainWindow.xaml b/Application/FileConverter/Windows/MainWindow.xaml index 2d0f220..24af190 100644 --- a/Application/FileConverter/Windows/MainWindow.xaml +++ b/Application/FileConverter/Windows/MainWindow.xaml @@ -4,11 +4,20 @@ xmlns:d="http://schemas.microsoft.com/expression/blend/2008" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" xmlns:local="clr-namespace:FileConverter" xmlns:valueConverters="clr-namespace:FileConverter.ValueConverters" - xmlns:conversionJobs="clr-namespace:FileConverter.ConversionJobs" mc:Ignorable="d" x:Class="FileConverter.MainWindow" - x:Name="FileConverterMainWindow" Height="500" Width="950" MinHeight="250" MinWidth="450" WindowStartupLocation="CenterScreen" Icon="/FileConverter;component/Resources/ApplicationIcon.ico"> + xmlns:conversionJobs="clr-namespace:FileConverter.ConversionJobs" + xmlns:generic="clr-namespace:FileConverter.ValueConverters.Generic" + mc:Ignorable="d" x:Class="FileConverter.MainWindow" + x:Name="FileConverterMainWindow" Height="500" Width="950" MinHeight="250" MinWidth="500" WindowStartupLocation="CenterScreen" Icon="/FileConverter;component/Resources/ApplicationIcon.ico"> + + + + + + + @@ -39,8 +48,13 @@ - - + + + + + + + @@ -64,7 +78,9 @@ - + +