From 73d3255e768d66ed20325c410993c002ee2fbe0a Mon Sep 17 00:00:00 2001 From: Tichau Date: Tue, 17 Nov 2015 08:40:34 +0100 Subject: [PATCH] Disable input extensions that are not compatible with output type. --- .../ConversionPreset/ConversionPreset.cs | 19 ++++++- .../FileConverter/FileConverter.csproj | 1 + ...tputTypeCompatibleWithCategoryConverter.cs | 56 +++++++++++++++++++ .../FileConverter/Windows/SettingsWindow.xaml | 22 +++++++- 4 files changed, 96 insertions(+), 2 deletions(-) create mode 100644 Application/FileConverter/ValueConverters/IsOutputTypeCompatibleWithCategoryConverter.cs diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index 0d2a83f..d69b324 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -70,6 +70,7 @@ namespace FileConverter this.outputType = value; this.InitializeDefaultSettings(this.outputType); this.OnPropertyChanged(); + this.CoerceInputTypes(); } } @@ -88,7 +89,7 @@ namespace FileConverter { this.inputTypes[index] = this.inputTypes[index].ToLowerInvariant(); } - + this.OnPropertyChanged(); } } @@ -241,6 +242,8 @@ namespace FileConverter { this.InputTypes[index] = this.InputTypes[index].ToLowerInvariant(); } + + this.CoerceInputTypes(); } public void AddInputType(string inputType) @@ -328,6 +331,20 @@ namespace FileConverter this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); } + private void CoerceInputTypes() + { + for (int index = 0; index < this.inputTypes.Count; index++) + { + string inputType = this.inputTypes[index]; + string inputCategory = PathHelpers.GetExtensionCategory(inputType); + if (!PathHelpers.IsOutputTypeCompatibleWithCategory(this.OutputType, inputCategory)) + { + this.RemoveInputType(inputType); + index--; + } + } + } + private bool IsRelevantSetting(string settingsKey) { switch (this.OutputType) diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 138a8fe..339dd73 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -103,6 +103,7 @@ + diff --git a/Application/FileConverter/ValueConverters/IsOutputTypeCompatibleWithCategoryConverter.cs b/Application/FileConverter/ValueConverters/IsOutputTypeCompatibleWithCategoryConverter.cs new file mode 100644 index 0000000..66e86e9 --- /dev/null +++ b/Application/FileConverter/ValueConverters/IsOutputTypeCompatibleWithCategoryConverter.cs @@ -0,0 +1,56 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter.ValueConverters +{ + using System; + using System.Globalization; + using System.Windows.Data; + + public class IsOutputTypeCompatibleWithCategoryConverter : IMultiValueConverter + { + public object Convert(object[] values, Type targetType, object parameter, CultureInfo culture) + { + if (values == null || values.Length != 2) + { + throw new ArgumentException("The values must contains the collection of elements and the researched element."); + } + + if (!(values[0] is OutputType)) + { + return false; + } + + OutputType outputType = (OutputType)values[0]; + + string category = null; + + string input = values[1] as string; + if (input == null) + { + return false; + } + + string stringParameter = parameter as string; + if (stringParameter != null && stringParameter == "InputType") + { + category = PathHelpers.GetExtensionCategory(input); + } + else if (stringParameter != null && stringParameter == "Category") + { + category = input; + } + + if (category == null) + { + return false; + } + + return PathHelpers.IsOutputTypeCompatibleWithCategory(outputType, category); + } + + public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml b/Application/FileConverter/Windows/SettingsWindow.xaml index c1f3723..d19861f 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml +++ b/Application/FileConverter/Windows/SettingsWindow.xaml @@ -17,6 +17,7 @@ + @@ -247,6 +248,12 @@ + + + + + + @@ -257,6 +264,12 @@ + + + + + + @@ -268,7 +281,14 @@