From b9c5daa5663a78f8d68af69f58cf476d0ccaa8b6 Mon Sep 17 00:00:00 2001 From: sr55 Date: Wed, 16 Nov 2016 19:55:22 +0000 Subject: WinGui: Prompt to create folder when the destination path does not exist. Fixes #394 --- .../HandBrakeWPF/Properties/Resources.Designer.cs | 45 +++++++++++++++++-- win/CS/HandBrakeWPF/Properties/Resources.resx | 19 ++++++-- .../HandBrakeWPF/Utilities/DirectoryUtilities.cs | 20 ++++++++- win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs | 52 ++++++++++++++++------ 4 files changed, 115 insertions(+), 21 deletions(-) (limited to 'win') diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs index e8e956363..b692aa83f 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs +++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs @@ -593,6 +593,25 @@ namespace HandBrakeWPF.Properties { } } + /// + /// Looks up a localized string similar to Create Folder?. + /// + public static string DirectoryUtils_CreateFolder { + get { + return ResourceManager.GetString("DirectoryUtils_CreateFolder", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to The folder you are trying to write to does not exist. Would you like HandBrake to create the following folder? + ///{0}. + /// + public static string DirectoryUtils_CreateFolderMsg { + get { + return ResourceManager.GetString("DirectoryUtils_CreateFolderMsg", resourceCulture); + } + } + /// /// Looks up a localized string similar to Error. /// @@ -684,7 +703,7 @@ namespace HandBrakeWPF.Properties { } /// - /// Looks up a localized string similar to The entered destination contained illegal characters. You must fix the path and filename before continuing.. + /// Looks up a localized string similar to The entered destination path contained illegal characters and will not be updated.. /// public static string Main_InvalidDestination { get { @@ -729,11 +748,11 @@ namespace HandBrakeWPF.Properties { } /// - /// Looks up a localized string similar to You do not have the appropriate folder permissions to write files into the directory you have chosen.. + /// Looks up a localized string similar to The output directory you have chosen either does not exist, or you do not have permissions to write files to it.. /// - public static string Main_NoPermissionsOnDirectory { + public static string Main_NoPermissionsOrMissingDirectory { get { - return ResourceManager.GetString("Main_NoPermissionsOnDirectory", resourceCulture); + return ResourceManager.GetString("Main_NoPermissionsOrMissingDirectory", resourceCulture); } } @@ -1106,6 +1125,24 @@ namespace HandBrakeWPF.Properties { } } + /// + /// Looks up a localized string similar to Unable to launch destination directory.. + /// + public static string MainViewModel_UnableToLaunchDestDir { + get { + return ResourceManager.GetString("MainViewModel_UnableToLaunchDestDir", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Please check that you have a valid destination directory.. + /// + public static string MainViewModel_UnableToLaunchDestDirSolution { + get { + return ResourceManager.GetString("MainViewModel_UnableToLaunchDestDirSolution", resourceCulture); + } + } + /// /// Looks up a localized string similar to Notice. /// diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx index be0adb41f..6791f06e6 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.resx @@ -465,7 +465,7 @@ Do you wish to proceed? You must first set the destination path for the output file before adding to the queue. - The entered destination contained illegal characters. You must fix the path and filename before continuing. + The entered destination path contained illegal characters and will not be updated. Preview @@ -809,7 +809,20 @@ Your preset file will be archived and new one created. You will need to re-creat Your user settings file appears to be inaccessible or corrupted. You may have to delete the file and let HandBrake generate a new one. - - You do not have the appropriate folder permissions to write files into the directory you have chosen. + + The output directory you have chosen either does not exist, or you do not have permissions to write files to it. + + + Create Folder? + + + The folder you are trying to write to does not exist. Would you like HandBrake to create the following folder? +{0} + + + Unable to launch destination directory. + + + Please check that you have a valid destination directory. \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs b/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs index 45450f4eb..0f63ebfce 100644 --- a/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs +++ b/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs @@ -11,6 +11,10 @@ namespace HandBrakeWPF.Utilities { using System; using System.IO; + using System.Windows; + + using HandBrakeWPF.Properties; + using HandBrakeWPF.Services.Interfaces; /// /// The directory utilities. @@ -42,11 +46,25 @@ namespace HandBrakeWPF.Utilities /// Simple way of checking if a directory is writeable. /// /// Path to check + /// + /// Prompt to create directory if it doesn't exist. + /// + /// + /// An instance of the error service to allow prompting. + /// /// True if writable - public static bool IsWritable(string dirPath) + public static bool IsWritable(string dirPath, bool createDirectoryPrompt, IErrorService errorService) { try { + if (!Directory.Exists(dirPath)) + { + MessageBoxResult result = errorService.ShowMessageBox(string.Format(Resources.DirectoryUtils_CreateFolderMsg, dirPath), Resources.DirectoryUtils_CreateFolder, MessageBoxButton.YesNo, MessageBoxImage.Question); + if (MessageBoxResult.Yes == result) + { + Directory.CreateDirectory(dirPath); + } + } using (File.Create(Path.Combine(dirPath, Path.GetRandomFileName()), 1, FileOptions.DeleteOnClose)) { } diff --git a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs index b1e231412..957ce4d29 100644 --- a/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs +++ b/win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs @@ -698,21 +698,25 @@ namespace HandBrakeWPF.ViewModels { if (!Equals(this.CurrentTask.Destination, value)) { - this.CurrentTask.Destination = value; - this.NotifyOfPropertyChange(() => this.Destination); - - if (!string.IsNullOrEmpty(this.CurrentTask.Destination)) + if (!string.IsNullOrEmpty(value)) { string ext = string.Empty; try { - ext = Path.GetExtension(this.CurrentTask.Destination); + ext = Path.GetExtension(value); } catch (ArgumentException) { - this.errorService.ShowMessageBox(Resources.Main_InvalidDestination, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); + this.errorService.ShowMessageBox( + Resources.Main_InvalidDestination, + Resources.Error, + MessageBoxButton.OK, + MessageBoxImage.Error); } + this.CurrentTask.Destination = value; + this.NotifyOfPropertyChange(() => this.Destination); + switch (ext) { case ".mkv": @@ -726,6 +730,11 @@ namespace HandBrakeWPF.ViewModels break; } } + else + { + this.CurrentTask.Destination = string.Empty; + this.NotifyOfPropertyChange(() => this.Destination); + } } } } @@ -1397,9 +1406,9 @@ namespace HandBrakeWPF.ViewModels return false; } - if (!DirectoryUtilities.IsWritable(Path.GetDirectoryName(this.CurrentTask.Destination))) + if (!DirectoryUtilities.IsWritable(Path.GetDirectoryName(this.CurrentTask.Destination), true, this.errorService)) { - this.errorService.ShowMessageBox(Resources.Main_NoPermissionsOnDirectory, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); + this.errorService.ShowMessageBox(Resources.Main_NoPermissionsOrMissingDirectory, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error); return false; } @@ -1794,14 +1803,31 @@ namespace HandBrakeWPF.ViewModels { if (!string.IsNullOrEmpty(this.Destination)) { - string directory = Path.GetDirectoryName(this.Destination); - if (!string.IsNullOrEmpty(directory)) + try { - Process.Start(directory); + string directory = Path.GetDirectoryName(this.Destination); + if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory)) + { + Process.Start(directory); + } + else + { + MessageBoxResult result = + errorService.ShowMessageBox( + string.Format(Resources.DirectoryUtils_CreateFolderMsg, directory), + Resources.DirectoryUtils_CreateFolder, + MessageBoxButton.YesNo, + MessageBoxImage.Question); + if (MessageBoxResult.Yes == result) + { + Directory.CreateDirectory(directory); + Process.Start(directory); + } + } } - else + catch (Exception exc) { - Process.Start(AppDomain.CurrentDomain.BaseDirectory); + this.errorService.ShowError(Resources.MainViewModel_UnableToLaunchDestDir, Resources.MainViewModel_UnableToLaunchDestDirSolution, exc); } } } -- cgit v1.2.3