]> granicus.if.org Git - handbrake/commitdiff
WinGui: Add some error checking into the Portable mode to guide users when there...
authorsr55 <sr55.hb@outlook.com>
Sat, 9 Mar 2019 21:09:06 +0000 (21:09 +0000)
committersr55 <sr55.hb@outlook.com>
Sat, 9 Mar 2019 21:14:23 +0000 (21:14 +0000)
win/CS/HandBrakeWPF/App.xaml.cs
win/CS/HandBrakeWPF/Properties/Resources.Designer.cs
win/CS/HandBrakeWPF/Properties/Resources.resx
win/CS/HandBrakeWPF/Utilities/Portable.cs

index e6b063eacf5c79d48624f4d8d3e05f72d8f64497..7aa5381379580466d4a13753f0b3f687924660b6 100644 (file)
@@ -97,7 +97,11 @@ namespace HandBrakeWPF
             // Portable Mode\r
             if (Portable.IsPortable())\r
             {\r
-                Portable.Initialise();\r
+                if(!Portable.Initialise())\r
+                {\r
+                    Application.Current.Shutdown();\r
+                    return;\r
+                }\r
             }\r
 \r
             // Setup the UI Language\r
index afb24dd5f0ae61d58da6c90ead54261f2a149b0d..14d2c2732a9b9284661644afbdf0ed67110423aa 100644 (file)
@@ -3422,6 +3422,37 @@ namespace HandBrakeWPF.Properties {
             }
         }
         
+        /// <summary>
+        ///   Looks up a localized string similar to Portable Mode: Unable to read portable.ini. There may be an error in this file. Please retry using portable.ini.template as a guide..
+        /// </summary>
+        public static string Portable_IniFileError {
+            get {
+                return ResourceManager.GetString("Portable_IniFileError", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Portable Mode: Unable to create Storage directory. Please check the path is correct and is writable.
+        ///
+        ///    {0}.
+        /// </summary>
+        public static string Portable_StorageNotWritable {
+            get {
+                return ResourceManager.GetString("Portable_StorageNotWritable", resourceCulture);
+            }
+        }
+        
+        /// <summary>
+        ///   Looks up a localized string similar to Portable Mode: Unable to create TMP directory. Please check the path is correct and is writable.
+        ///
+        ///    {0}.
+        /// </summary>
+        public static string Portable_TmpNotWritable {
+            get {
+                return ResourceManager.GetString("Portable_TmpNotWritable", resourceCulture);
+            }
+        }
+        
         /// <summary>
         ///   Looks up a localized string similar to Change the behaviour of the audio track selection for this preset.  
         ///This will not affect your current settings in the Audio tab..
index 21de9c96c9486f2ed8a8a70ca927da916208e83e..066b249abc3a591ea4e7a96234d865813cee8f9c 100644 (file)
@@ -1925,4 +1925,17 @@ Time Remaining: {5},  Elapsed: {6:d\:hh\:mm\:ss} {7}</value>
   <data name="QueueView_DeleteSelected" xml:space="preserve">\r
     <value>Delete Selected</value>\r
   </data>\r
+  <data name="Portable_TmpNotWritable" xml:space="preserve">\r
+    <value>Portable Mode: Unable to create TMP directory. Please check the path is correct and is writable.\r
+\r
+    {0}</value>\r
+  </data>\r
+  <data name="Portable_StorageNotWritable" xml:space="preserve">\r
+    <value>Portable Mode: Unable to create Storage directory. Please check the path is correct and is writable.\r
+\r
+    {0}</value>\r
+  </data>\r
+  <data name="Portable_IniFileError" xml:space="preserve">\r
+    <value>Portable Mode: Unable to read portable.ini. There may be an error in this file. Please retry using portable.ini.template as a guide.</value>\r
+  </data>\r
 </root>
\ No newline at end of file
index 243f8e5f22015c6afb511bdb6c57f626195e0998..2cfbce6e9a85578076485f6dd4dc940be114d803 100644 (file)
@@ -12,6 +12,7 @@ namespace HandBrakeWPF.Utilities
     using System;
     using System.Collections.Generic;
     using System.IO;
+    using System.Windows;
 
     /// <summary>
     /// This class is responsible for reading the Portable.ini file that allows HandBrake to be run out of a directory.
@@ -24,50 +25,93 @@ namespace HandBrakeWPF.Utilities
         /// <summary>
         /// Initializes a new instance of the <see cref="Portable"/> class.
         /// </summary>
-        public static void Initialise()
+        public static bool Initialise()
         {
             if (!IsPortable())
             {
-                return; // Nothing to do.
+                return true;
             }
 
             // Read the INI file     
             if (File.Exists(portableFile))
             {
-                using (StreamReader fileReader = new StreamReader(portableFile))
+                try
                 {
-                    string line;
-                    while ((line = fileReader.ReadLine()) != null)
+                    using (StreamReader fileReader = new StreamReader(portableFile))
                     {
-                        line = line.Trim();
-
-                        if (line.StartsWith("#"))
+                        string line;
+                        while ((line = fileReader.ReadLine()) != null)
                         {
-                            continue; // Ignore Comments
-                        }
-
-                        string[] setting = line.Split('=');
-                        if (setting.Length == 2)
-                        {
-                            keyPairs.Add(setting[0].Trim(), setting[1].Trim());
+                            line = line.Trim();
+
+                            if (line.StartsWith("#"))
+                            {
+                                continue; // Ignore Comments
+                            }
+
+                            string[] setting = line.Split('=');
+                            if (setting.Length == 2)
+                            {
+                                keyPairs.Add(setting[0].Trim(), setting[1].Trim());
+                            }
                         }
                     }
                 }
+                catch 
+                {
+                    MessageBox.Show(
+                        HandBrakeWPF.Properties.Resources.Portable_IniFileError,
+                        Properties.Resources.Error,
+                        MessageBoxButton.OK,
+                        MessageBoxImage.Error);
+                    return false;
+                }
             }
 
             // Create any missing directories
-            if (!Directory.Exists(GetTempDirectory()))
+            string tmpDir = GetTempDirectory();
+            if (!string.IsNullOrEmpty(tmpDir) && !Directory.Exists(tmpDir))
             {
-                Directory.CreateDirectory(GetTempDirectory());
+                try
+                {
+                    Directory.CreateDirectory(tmpDir);
+                }
+                catch (Exception)
+                {
+                    MessageBox.Show(
+                        string.Format(Properties.Resources.Portable_TmpNotWritable, tmpDir),
+                        Properties.Resources.Error,
+                        MessageBoxButton.OK,
+                        MessageBoxImage.Error);
+                    return false;
+                }
             }
 
-            if (!Directory.Exists(GetStorageDirectory()))
+            string stroageDir = GetStorageDirectory();
+            if (!Directory.Exists(stroageDir))
             {
-                Directory.CreateDirectory(GetStorageDirectory());
+                try
+                {
+                    Directory.CreateDirectory(stroageDir);
+                }
+                catch (Exception)
+                {
+                    MessageBox.Show(
+                        string.Format(HandBrakeWPF.Properties.Resources.Portable_StorageNotWritable, stroageDir),
+                        Properties.Resources.Error,
+                        MessageBoxButton.OK,
+                        MessageBoxImage.Error);
+                    return false;
+                }
             }
 
             // Setup environment variables for this instance.
-            Environment.SetEnvironmentVariable("TMP", GetTempDirectory());
+            if (!string.IsNullOrEmpty(tmpDir))
+            {
+                Environment.SetEnvironmentVariable("TMP", GetTempDirectory());
+            }
+
+            return true;
         }
 
         /// <summary>
@@ -101,7 +145,7 @@ namespace HandBrakeWPF.Utilities
                 string directory = keyPairs["storage.dir"];
 
                 // If "cwd", then treat that as Current Working Directory.
-                if (directory == "cwd")
+                if (!string.IsNullOrEmpty(directory) && directory == "cwd")
                 {
                     storagePath = Path.Combine(Environment.CurrentDirectory, "storage");
                 }
@@ -111,7 +155,7 @@ namespace HandBrakeWPF.Utilities
                 {
                     storagePath = directory;
                 }
-            }
+            } 
 
             // Return what path we figured out to use.
             return storagePath;