diff --git a/Application/FileConverter/Application.xaml.cs b/Application/FileConverter/Application.xaml.cs index 7940ace..1118e1e 100644 --- a/Application/FileConverter/Application.xaml.cs +++ b/Application/FileConverter/Application.xaml.cs @@ -211,6 +211,11 @@ namespace FileConverter for (int index = 0; index < this.conversionJobs.Count; index++) { ConversionJob conversionJob = this.conversionJobs[index]; + if (conversionJob.State != ConversionJob.ConversionState.Ready) + { + continue; + } + conversionJob.StartConvertion(); if (System.IO.File.Exists(conversionJob.OutputFilePath)) diff --git a/Application/FileConverter/ConversionJobs/ConversionJob.cs b/Application/FileConverter/ConversionJobs/ConversionJob.cs index c4b1e37..471051b 100644 --- a/Application/FileConverter/ConversionJobs/ConversionJob.cs +++ b/Application/FileConverter/ConversionJobs/ConversionJob.cs @@ -114,7 +114,14 @@ namespace FileConverter.ConversionJobs } this.InputFilePath = inputFilePath; - this.OutputFilePath = this.GenerateOutputPath(this.InputFilePath); + this.OutputFilePath = this.ConversionPreset.GenerateOutputFilePath(inputFilePath); + string presetError = this.ConversionPreset.Error; + if (!string.IsNullOrEmpty(presetError)) + { + this.State = ConversionState.Failed; + this.ErrorMessage = presetError; + return; + } this.Initialize(); @@ -160,17 +167,6 @@ namespace FileConverter.ConversionJobs { this.State = ConversionState.Failed; } - - protected virtual string GenerateOutputPath(string inputFilePath) - { - if (this.ConversionPreset == null) - { - throw new Exception("The conversion preset must be valid."); - } - - string extension = System.IO.Path.GetExtension(inputFilePath); - return inputFilePath.Substring(0, inputFilePath.Length - extension.Length) + "." + this.ConversionPreset.OutputType.ToString().ToLowerInvariant(); - } protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") { diff --git a/Application/FileConverter/ConversionPreset/ConversionPreset.cs b/Application/FileConverter/ConversionPreset/ConversionPreset.cs index 2cc6d28..ba49a72 100644 --- a/Application/FileConverter/ConversionPreset/ConversionPreset.cs +++ b/Application/FileConverter/ConversionPreset/ConversionPreset.cs @@ -1,5 +1,7 @@ // License: http://www.gnu.org/licenses/gpl.html GPL version 3. +using FileConverter.ValueConverters; + namespace FileConverter { using System; @@ -20,6 +22,7 @@ namespace FileConverter private List inputTypes; private ConversionSettings settings = new ConversionSettings(); private string outputFileNameTemplate; + FileNameConverter outputFileNameConverter = new FileNameConverter(); public ConversionPreset() { @@ -32,6 +35,7 @@ namespace FileConverter this.OutputType = outputType; this.InputTypes = new List(); this.InputTypes.AddRange(inputTypes); + this.outputFileNameTemplate = "(p)(f)"; } public event PropertyChangedEventHandler PropertyChanged; @@ -170,10 +174,27 @@ namespace FileConverter { get { - return this.Validate("Name"); + string errorString = this.Validate("Name"); + if (!string.IsNullOrEmpty(errorString)) + { + return errorString; + } + + errorString = this.Validate("OutputFileNameTemplate"); + if (!string.IsNullOrEmpty(errorString)) + { + return errorString; + } + + return string.Empty; } } + public string GenerateOutputFilePath(string inputFilePath) + { + return (string)this.outputFileNameConverter.Convert(new object[] {inputFilePath, this.OutputType, this.OutputFileNameTemplate}, typeof (string), null, null); + } + public void SetSettingsValue(string settingsKey, string value) { if (string.IsNullOrEmpty(settingsKey)) @@ -338,6 +359,71 @@ namespace FileConverter } } + break; + + case "OutputFileNameTemplate": + { + string sampleOutputFilePath = this.GenerateOutputFilePath(FileConverter.Properties.Resources.OuputFileNameTemplateSample); + if (string.IsNullOrEmpty(sampleOutputFilePath)) + { + return "The output filename template must produce a non empty result."; + } + + if (!PathHelpers.IsPathValid(sampleOutputFilePath)) + { + // Diagnostic to feedback purpose. + // Drive letter. + if (!PathHelpers.IsPathDriveLetterValid(sampleOutputFilePath)) + { + return "The output filename template must define a root (for example c:\\, use (p) to use the input file path)."; + } + + // File name. + string filename = PathHelpers.GetFileName(sampleOutputFilePath); + if (filename == null) + { + return "The output file name must not be empty (use (f) to use the name of the input file)."; + } + + char[] invalidFileNameChars = System.IO.Path.GetInvalidFileNameChars(); + for (int index = 0; index < invalidFileNameChars.Length; index++) + { + if (filename.Contains(invalidFileNameChars[index])) + { + return "The output file name must not contains the character '" + invalidFileNameChars[index] + "'."; + } + } + + // Directory names. + string path = sampleOutputFilePath.Substring(3, sampleOutputFilePath.Length - 3 - filename.Length); + char[] invalidPathChars = System.IO.Path.GetInvalidPathChars(); + for (int index = 0; index < invalidPathChars.Length; index++) + { + if (string.IsNullOrEmpty(path)) + { + return "The output directory name must not be empty (use (d0), (d1), ... to use the name of the parent directories of the input file)."; + } + + if (path.Contains(invalidPathChars[index])) + { + return "The output directory name must not contains the character '" + invalidPathChars[index] + "'."; + } + } + + string[] directories = path.Split('\\'); + for (int index = 0; index < directories.Length; ++index) + { + string directoryName = directories[index]; + if (string.IsNullOrEmpty(directoryName)) + { + return "The output directory name must not be empty (use (d0), (d1), ... to use the name of the parent directories of the input file)."; + } + } + + return "The output filename template is invalid"; + } + } + break; } diff --git a/Application/FileConverter/FileConverter.csproj b/Application/FileConverter/FileConverter.csproj index ba72c0f..e6c7270 100644 --- a/Application/FileConverter/FileConverter.csproj +++ b/Application/FileConverter/FileConverter.csproj @@ -82,6 +82,7 @@ + Resources.en.resx True diff --git a/Application/FileConverter/PathHelpers.cs b/Application/FileConverter/PathHelpers.cs new file mode 100644 index 0000000..d75ba8a --- /dev/null +++ b/Application/FileConverter/PathHelpers.cs @@ -0,0 +1,33 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Text; +using System.Text.RegularExpressions; +using System.Threading.Tasks; + +namespace FileConverter +{ + public static class PathHelpers + { + private static Regex driveLetterRegex = new Regex(@"[a-zA-Z]:\\"); + private static Regex pathRegex = new Regex(@"^[a-zA-Z]:\\(?:[^\\/:*?""<>|\r\n]+\\)*[^\.\\/:*?""<>|\r\n][^\\/:*?""<>|\r\n]*$"); + private static Regex filenameRegex = new Regex(@"[^\\]*", RegexOptions.RightToLeft); + + public static bool IsPathDriveLetterValid(string path) + { + return PathHelpers.driveLetterRegex.IsMatch(path); + } + + public static bool IsPathValid(string path) + { + return PathHelpers.pathRegex.IsMatch(path); + } + + public static string GetFileName(string path) + { + MatchCollection matchCollection = PathHelpers.filenameRegex.Matches(path); + Match filenameMatch = matchCollection.Count > 0 ? matchCollection[0] : null; + return filenameMatch?.Groups[0].Value; + } + } +} diff --git a/Application/FileConverter/Windows/SettingsWindow.xaml b/Application/FileConverter/Windows/SettingsWindow.xaml index 516861b..c074451 100644 --- a/Application/FileConverter/Windows/SettingsWindow.xaml +++ b/Application/FileConverter/Windows/SettingsWindow.xaml @@ -6,7 +6,7 @@ xmlns:generic="clr-namespace:FileConverter.ValueConverters.Generic" xmlns:windows="clr-namespace:FileConverter.Windows" xmlns:project="clr-namespace:FileConverter.Properties" - Height="500" Width="700" MinHeight="370" MinWidth="440" + Height="500" Width="700" MinHeight="400" MinWidth="600" WindowStartupLocation="CenterScreen" Icon="/FileConverter;component/Resources/ApplicationIcon-16x16.ico" Closing="SettingsWindow_OnClosing"> @@ -157,7 +157,7 @@ - + @@ -165,7 +165,7 @@