]> granicus.if.org Git - handbrake/commitdiff
WinGui: Prompt to create folder when the destination path does not exist. Fixes #394
authorsr55 <sr55.hb@outlook.com>
Wed, 16 Nov 2016 19:55:22 +0000 (19:55 +0000)
committersr55 <sr55.hb@outlook.com>
Wed, 16 Nov 2016 19:55:31 +0000 (19:55 +0000)
win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
win/CS/HandBrakeWPF/Properties/Resources.resx
win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs
win/CS/HandBrakeWPF/ViewModels/MainViewModel.cs

index e8e956363a62f7b75abb278c3ed981a10e3d8873..b692aa83f7a24b41e855d98a3df38af3b170d52f 100644 (file)
@@ -593,6 +593,25 @@ namespace HandBrakeWPF.Properties {
             }\r
         }\r
         \r
+        /// <summary>\r
+        ///   Looks up a localized string similar to Create Folder?.\r
+        /// </summary>\r
+        public static string DirectoryUtils_CreateFolder {\r
+            get {\r
+                return ResourceManager.GetString("DirectoryUtils_CreateFolder", resourceCulture);\r
+            }\r
+        }\r
+        \r
+        /// <summary>\r
+        ///   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?\r
+        ///{0}.\r
+        /// </summary>\r
+        public static string DirectoryUtils_CreateFolderMsg {\r
+            get {\r
+                return ResourceManager.GetString("DirectoryUtils_CreateFolderMsg", resourceCulture);\r
+            }\r
+        }\r
+        \r
         /// <summary>\r
         ///   Looks up a localized string similar to Error.\r
         /// </summary>\r
@@ -684,7 +703,7 @@ namespace HandBrakeWPF.Properties {
         }\r
         \r
         /// <summary>\r
-        ///   Looks up a localized string similar to The entered destination contained illegal characters. You must fix the path and filename before continuing..\r
+        ///   Looks up a localized string similar to The entered destination path contained illegal characters and will not be updated..\r
         /// </summary>\r
         public static string Main_InvalidDestination {\r
             get {\r
@@ -729,11 +748,11 @@ namespace HandBrakeWPF.Properties {
         }\r
         \r
         /// <summary>\r
-        ///   Looks up a localized string similar to You do not have the appropriate folder permissions to write files into the directory you have chosen..\r
+        ///   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..\r
         /// </summary>\r
-        public static string Main_NoPermissionsOnDirectory {\r
+        public static string Main_NoPermissionsOrMissingDirectory {\r
             get {\r
-                return ResourceManager.GetString("Main_NoPermissionsOnDirectory", resourceCulture);\r
+                return ResourceManager.GetString("Main_NoPermissionsOrMissingDirectory", resourceCulture);\r
             }\r
         }\r
         \r
@@ -1106,6 +1125,24 @@ namespace HandBrakeWPF.Properties {
             }\r
         }\r
         \r
+        /// <summary>\r
+        ///   Looks up a localized string similar to Unable to launch destination directory..\r
+        /// </summary>\r
+        public static string MainViewModel_UnableToLaunchDestDir {\r
+            get {\r
+                return ResourceManager.GetString("MainViewModel_UnableToLaunchDestDir", resourceCulture);\r
+            }\r
+        }\r
+        \r
+        /// <summary>\r
+        ///   Looks up a localized string similar to Please check that you have a valid destination directory..\r
+        /// </summary>\r
+        public static string MainViewModel_UnableToLaunchDestDirSolution {\r
+            get {\r
+                return ResourceManager.GetString("MainViewModel_UnableToLaunchDestDirSolution", resourceCulture);\r
+            }\r
+        }\r
+        \r
         /// <summary>\r
         ///   Looks up a localized string similar to Notice.\r
         /// </summary>\r
index be0adb41f64a7f39a56692c1e4ec74df5089b386..6791f06e6764bb1ecd23852f5d73f2020e1c9315 100644 (file)
@@ -465,7 +465,7 @@ Do you wish to proceed?</value>
     <value>You must first set the destination path for the output file before adding to the queue.</value>\r
   </data>\r
   <data name="Main_InvalidDestination" xml:space="preserve">\r
-    <value>The entered destination contained illegal characters. You must fix the path and filename before continuing.</value>\r
+    <value>The entered destination path contained illegal characters and will not be updated.</value>\r
   </data>\r
   <data name="Preview" xml:space="preserve">\r
     <value>Preview</value>\r
@@ -809,7 +809,20 @@ Your preset file will be archived and new one created. You will need to re-creat
   <data name="UserSettings_UnableToLoadSolution" xml:space="preserve">\r
     <value>Your user settings file appears to be inaccessible or corrupted. You may have to delete the file and let HandBrake generate a new one.</value>\r
   </data>\r
-  <data name="Main_NoPermissionsOnDirectory" xml:space="preserve">\r
-    <value>You do not have the appropriate folder permissions to write files into the directory you have chosen.</value>\r
+  <data name="Main_NoPermissionsOrMissingDirectory" xml:space="preserve">\r
+    <value>The output directory you have chosen either does not exist, or you do not have permissions to write files to it.</value>\r
+  </data>\r
+  <data name="DirectoryUtils_CreateFolder" xml:space="preserve">\r
+    <value>Create Folder?</value>\r
+  </data>\r
+  <data name="DirectoryUtils_CreateFolderMsg" xml:space="preserve">\r
+    <value>The folder you are trying to write to does not exist. Would you like HandBrake to create the following folder?\r
+{0}</value>\r
+  </data>\r
+  <data name="MainViewModel_UnableToLaunchDestDir" xml:space="preserve">\r
+    <value>Unable to launch destination directory.</value>\r
+  </data>\r
+  <data name="MainViewModel_UnableToLaunchDestDirSolution" xml:space="preserve">\r
+    <value>Please check that you have a valid destination directory.</value>\r
   </data>\r
 </root>
\ No newline at end of file
index 45450f4eb5e7883622a98da01945d289f2b421c6..0f63ebfce9d6a1f0bbc44d08634c55b7182e3ced 100644 (file)
@@ -11,6 +11,10 @@ namespace HandBrakeWPF.Utilities
 {
     using System;
     using System.IO;
+    using System.Windows;
+
+    using HandBrakeWPF.Properties;
+    using HandBrakeWPF.Services.Interfaces;
 
     /// <summary>
     /// The directory utilities.
@@ -42,11 +46,25 @@ namespace HandBrakeWPF.Utilities
         /// Simple way of checking if a directory is writeable.
         /// </summary>
         /// <param name="dirPath">Path to check</param>
+        /// <param name="createDirectoryPrompt">
+        /// Prompt to create directory if it doesn't exist.
+        /// </param>
+        /// <param name="errorService">
+        /// An instance of the error service to allow prompting.
+        /// </param>
         /// <returns>True if writable</returns>
-        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))
                 {
                 }
index b1e2314126d3c21713d644bf6ea2038371465355..957ce4d296f0cd3861f84a2d5f3b2db233845025 100644 (file)
@@ -698,21 +698,25 @@ namespace HandBrakeWPF.ViewModels
             {\r
                 if (!Equals(this.CurrentTask.Destination, value))\r
                 {\r
-                    this.CurrentTask.Destination = value;\r
-                    this.NotifyOfPropertyChange(() => this.Destination);\r
-\r
-                    if (!string.IsNullOrEmpty(this.CurrentTask.Destination))\r
+                    if (!string.IsNullOrEmpty(value))\r
                     {\r
                         string ext = string.Empty;\r
                         try\r
                         {\r
-                            ext = Path.GetExtension(this.CurrentTask.Destination);\r
+                            ext = Path.GetExtension(value);\r
                         }\r
                         catch (ArgumentException)\r
                         {\r
-                            this.errorService.ShowMessageBox(Resources.Main_InvalidDestination, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);\r
+                            this.errorService.ShowMessageBox(\r
+                                Resources.Main_InvalidDestination,\r
+                                Resources.Error,\r
+                                MessageBoxButton.OK,\r
+                                MessageBoxImage.Error);\r
                         }\r
 \r
+                        this.CurrentTask.Destination = value;\r
+                        this.NotifyOfPropertyChange(() => this.Destination);\r
+\r
                         switch (ext)\r
                         {\r
                             case ".mkv":\r
@@ -726,6 +730,11 @@ namespace HandBrakeWPF.ViewModels
                                 break;\r
                         }\r
                     }\r
+                    else\r
+                    {\r
+                        this.CurrentTask.Destination = string.Empty;\r
+                        this.NotifyOfPropertyChange(() => this.Destination);\r
+                    }\r
                 }\r
             }\r
         }\r
@@ -1397,9 +1406,9 @@ namespace HandBrakeWPF.ViewModels
                 return false;\r
             }\r
 \r
-            if (!DirectoryUtilities.IsWritable(Path.GetDirectoryName(this.CurrentTask.Destination)))\r
+            if (!DirectoryUtilities.IsWritable(Path.GetDirectoryName(this.CurrentTask.Destination), true, this.errorService))\r
             {\r
-                this.errorService.ShowMessageBox(Resources.Main_NoPermissionsOnDirectory, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);\r
+                this.errorService.ShowMessageBox(Resources.Main_NoPermissionsOrMissingDirectory, Resources.Error, MessageBoxButton.OK, MessageBoxImage.Error);\r
                 return false;\r
             }\r
 \r
@@ -1794,14 +1803,31 @@ namespace HandBrakeWPF.ViewModels
         {\r
             if (!string.IsNullOrEmpty(this.Destination))\r
             {\r
-                string directory = Path.GetDirectoryName(this.Destination);\r
-                if (!string.IsNullOrEmpty(directory))\r
+                try\r
                 {\r
-                    Process.Start(directory);\r
+                    string directory = Path.GetDirectoryName(this.Destination);\r
+                    if (!string.IsNullOrEmpty(directory) && Directory.Exists(directory))\r
+                    {\r
+                        Process.Start(directory);\r
+                    }\r
+                    else\r
+                    {\r
+                        MessageBoxResult result =\r
+                            errorService.ShowMessageBox(\r
+                                string.Format(Resources.DirectoryUtils_CreateFolderMsg, directory),\r
+                                Resources.DirectoryUtils_CreateFolder,\r
+                                MessageBoxButton.YesNo,\r
+                                MessageBoxImage.Question);\r
+                        if (MessageBoxResult.Yes == result)\r
+                        {\r
+                            Directory.CreateDirectory(directory);\r
+                            Process.Start(directory);\r
+                        }\r
+                    }\r
                 }\r
-                else\r
+                catch (Exception exc)\r
                 {\r
-                    Process.Start(AppDomain.CurrentDomain.BaseDirectory);\r
+                    this.errorService.ShowError(Resources.MainViewModel_UnableToLaunchDestDir, Resources.MainViewModel_UnableToLaunchDestDirSolution, exc);\r
                 }\r
             }\r
         }\r