From 033edbf0ef660ec02f219d7ef429463d732015c0 Mon Sep 17 00:00:00 2001 From: sr55 Date: Sat, 9 Mar 2019 21:09:06 +0000 Subject: [PATCH] WinGui: Add some error checking into the Portable mode to guide users when there are problems with probable.ini --- win/CS/HandBrakeWPF/App.xaml.cs | 6 +- .../Properties/Resources.Designer.cs | 31 +++++++ win/CS/HandBrakeWPF/Properties/Resources.resx | 13 +++ win/CS/HandBrakeWPF/Utilities/Portable.cs | 88 ++++++++++++++----- 4 files changed, 115 insertions(+), 23 deletions(-) diff --git a/win/CS/HandBrakeWPF/App.xaml.cs b/win/CS/HandBrakeWPF/App.xaml.cs index e6b063eac..7aa538137 100644 --- a/win/CS/HandBrakeWPF/App.xaml.cs +++ b/win/CS/HandBrakeWPF/App.xaml.cs @@ -97,7 +97,11 @@ namespace HandBrakeWPF // Portable Mode if (Portable.IsPortable()) { - Portable.Initialise(); + if(!Portable.Initialise()) + { + Application.Current.Shutdown(); + return; + } } // Setup the UI Language diff --git a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs index afb24dd5f..14d2c2732 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs +++ b/win/CS/HandBrakeWPF/Properties/Resources.Designer.cs @@ -3422,6 +3422,37 @@ namespace HandBrakeWPF.Properties { } } + /// + /// 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.. + /// + public static string Portable_IniFileError { + get { + return ResourceManager.GetString("Portable_IniFileError", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Portable Mode: Unable to create Storage directory. Please check the path is correct and is writable. + /// + /// {0}. + /// + public static string Portable_StorageNotWritable { + get { + return ResourceManager.GetString("Portable_StorageNotWritable", resourceCulture); + } + } + + /// + /// Looks up a localized string similar to Portable Mode: Unable to create TMP directory. Please check the path is correct and is writable. + /// + /// {0}. + /// + public static string Portable_TmpNotWritable { + get { + return ResourceManager.GetString("Portable_TmpNotWritable", resourceCulture); + } + } + /// /// 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.. diff --git a/win/CS/HandBrakeWPF/Properties/Resources.resx b/win/CS/HandBrakeWPF/Properties/Resources.resx index 21de9c96c..066b249ab 100644 --- a/win/CS/HandBrakeWPF/Properties/Resources.resx +++ b/win/CS/HandBrakeWPF/Properties/Resources.resx @@ -1925,4 +1925,17 @@ Time Remaining: {5}, Elapsed: {6:d\:hh\:mm\:ss} {7} Delete Selected + + Portable Mode: Unable to create TMP directory. Please check the path is correct and is writable. + + {0} + + + Portable Mode: Unable to create Storage directory. Please check the path is correct and is writable. + + {0} + + + Portable Mode: Unable to read portable.ini. There may be an error in this file. Please retry using portable.ini.template as a guide. + \ No newline at end of file diff --git a/win/CS/HandBrakeWPF/Utilities/Portable.cs b/win/CS/HandBrakeWPF/Utilities/Portable.cs index 243f8e5f2..2cfbce6e9 100644 --- a/win/CS/HandBrakeWPF/Utilities/Portable.cs +++ b/win/CS/HandBrakeWPF/Utilities/Portable.cs @@ -12,6 +12,7 @@ namespace HandBrakeWPF.Utilities using System; using System.Collections.Generic; using System.IO; + using System.Windows; /// /// 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 /// /// Initializes a new instance of the class. /// - 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; } /// @@ -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; -- 2.40.0