From d66723d510ab91dca725a6d15ea32ec0082632c5 Mon Sep 17 00:00:00 2001 From: Tichau Date: Fri, 6 Nov 2015 08:20:29 +0100 Subject: [PATCH] Refactor input types system. --- .../ConversionPreset/ConversionPreset.cs | 24 ++++-- .../FileConverter/FileConverter.csproj | 4 + Application/FileConverter/PathHelpers.cs | 21 +++++ .../CollectionContainsElementToBool.cs | 41 +++++++++ .../FileConverter/Windows/InputExtension.cs | 40 +++++++++ .../Windows/InputExtensionCategory.cs | 61 ++++++++++++++ .../FileConverter/Windows/SettingsWindow.xaml | 84 ++++++++++--------- .../Windows/SettingsWindow.xaml.cs | 62 +++++++++++++- .../FileConverterExtension.cs | 32 +++++-- 9 files changed, 314 insertions(+), 55 deletions(-) create mode 100644 Application/FileConverter/ValueConverters/Generic/CollectionContainsElementToBool.cs create mode 100644 Application/FileConverter/Windows/InputExtension.cs create mode 100644 Application/FileConverter/Windows/InputExtensionCategory.cs diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index 7cc866b..6f576c7 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -1,7 +1,5 @@ // License: http://www.gnu.org/licenses/gpl.html GPL version 3. -using FileConverter.ValueConverters; - namespace FileConverter { using System; @@ -12,10 +10,11 @@ namespace FileConverter using System.Xml.Serialization; using FileConverter.Annotations; + using FileConverter.ValueConverters; [XmlRoot] [XmlType] - public class ConversionPreset : INotifyPropertyChanged, IDataErrorInfo + public class ConversionPreset : INotifyPropertyChanged, IDataErrorInfo, IXmlSerializable { private string name; private OutputType outputType; @@ -34,8 +33,10 @@ namespace FileConverter { this.Name = name; this.OutputType = outputType; - this.InputTypes = new List(); - this.InputTypes.AddRange(inputTypes); + List inputTypeList = new List(); + inputTypeList.AddRange(inputTypes); + this.InputTypes = inputTypeList; + this.outputFileNameTemplate = "(p)(f)"; } @@ -83,6 +84,11 @@ namespace FileConverter set { this.inputTypes = value; + for (int index = 0; index < this.inputTypes.Count; index++) + { + this.inputTypes[index] = this.inputTypes[index].ToLowerInvariant(); + } + this.OnPropertyChanged(); } } @@ -215,6 +221,14 @@ namespace FileConverter } } + public void OnDeserializationComplete() + { + for (int index = 0; index < this.InputTypes.Count; index++) + { + this.InputTypes[index] = this.InputTypes[index].ToLowerInvariant(); + } + } + public string GenerateOutputFilePath(string inputFilePath) { return (string)this.outputFileNameConverter.Convert(new object[] {inputFilePath, this.OutputType, this.OutputFileNameTemplate}, typeof (string), null, null); diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index 1accfe1..9c788ee 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -83,6 +83,7 @@ + Resources.en.resx @@ -97,6 +98,7 @@ + @@ -107,6 +109,8 @@ DiagnosticsWindow.xaml + + SettingsWindow.xaml diff --git a/Application/FileConverter/PathHelpers.cs b/Application/FileConverter/PathHelpers.cs index e09c6aa..6078203 100644 --- a/Application/FileConverter/PathHelpers.cs +++ b/Application/FileConverter/PathHelpers.cs @@ -58,5 +58,26 @@ namespace FileConverter return path; } + + public static string GetExtensionCategory(string extension) + { + switch (extension) + { + case "ape": + case "flac": + case "mp3": + case "m4a": + case "ogg": + case "wav": + case "wma": + return "Audio"; + + case "avi": + case "mp4": + return "Video"; + } + + return "Misc"; + } } } diff --git a/Application/FileConverter/ValueConverters/Generic/CollectionContainsElementToBool.cs b/Application/FileConverter/ValueConverters/Generic/CollectionContainsElementToBool.cs new file mode 100644 index 0000000..43b49b1 --- /dev/null +++ b/Application/FileConverter/ValueConverters/Generic/CollectionContainsElementToBool.cs @@ -0,0 +1,41 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +using System; +using System.Collections.Generic; +using System.Globalization; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows; +using System.Windows.Data; + +namespace FileConverter.ValueConverters.Generic +{ + public class CollectionContainsElementToBool : 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."); + } + + // TODO: Make this converter generic. + + if (!(values[0] is ICollection)) + { + return false; + } + + ICollection collection = values[0] as ICollection; + string objectToFind = values[1] as string; + + return collection.Contains(objectToFind); + } + + public object[] ConvertBack(object value, Type[] targetType, object parameter, CultureInfo culture) + { + throw new NotImplementedException(); + } + } +} diff --git a/Application/FileConverter/Windows/InputExtension.cs b/Application/FileConverter/Windows/InputExtension.cs new file mode 100644 index 0000000..70c76cd --- /dev/null +++ b/Application/FileConverter/Windows/InputExtension.cs @@ -0,0 +1,40 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +namespace FileConverter +{ + using System.ComponentModel; + using System.Runtime.CompilerServices; + using FileConverter.Annotations; + + public class InputExtension : INotifyPropertyChanged + { + public string name; + + public event PropertyChangedEventHandler PropertyChanged; + + public InputExtension(string name) + { + this.Name = name; + } + + public string Name + { + get + { + return this.name; + } + + set + { + this.name = value; + this.OnPropertyChanged(); + } + } + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} \ No newline at end of file diff --git a/Application/FileConverter/Windows/InputExtensionCategory.cs b/Application/FileConverter/Windows/InputExtensionCategory.cs new file mode 100644 index 0000000..79ea898 --- /dev/null +++ b/Application/FileConverter/Windows/InputExtensionCategory.cs @@ -0,0 +1,61 @@ +// License: http://www.gnu.org/licenses/gpl.html GPL version 3. + +using System.Collections.Generic; +using System.ComponentModel; +using System.Runtime.CompilerServices; +using FileConverter.Annotations; + +namespace FileConverter +{ + public class InputExtensionCategory : INotifyPropertyChanged + { + private string name; + private List inputExtensions = new List(); + + public event PropertyChangedEventHandler PropertyChanged; + + public InputExtensionCategory(string name) + { + this.Name = name; + } + + public string Name + { + get + { + return this.name; + } + + set + { + this.name = value; + this.OnPropertyChanged(); + } + } + + public IEnumerable InputExtensions + { + get + { + return this.inputExtensions; + } + } + + public void AddExtension(string extension) + { + InputExtension inputExtension = this.inputExtensions.Find(match => match.Name == extension); + if (inputExtension == null) + { + inputExtension = new InputExtension(extension); + this.inputExtensions.Add(inputExtension); + this.OnPropertyChanged("InputExtensions"); + } + } + + [NotifyPropertyChangedInvocator] + protected virtual void OnPropertyChanged([CallerMemberName] string propertyName = null) + { + this.PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName)); + } + } +} \ No newline at end of file diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml b/Application/FileConverter/Windows/SettingsWindow.xaml index 9dcd765..6e82a5c 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml +++ b/Application/FileConverter/Windows/SettingsWindow.xaml @@ -1,4 +1,4 @@ - @@ -15,6 +16,8 @@ + + @@ -81,8 +84,8 @@ @@ -92,7 +95,7 @@ @@ -127,11 +130,7 @@