From d1376abc6678ab2b53b9b682493523263525b108 Mon Sep 17 00:00:00 2001 From: sr55 Date: Mon, 26 Dec 2016 15:21:20 +0000 Subject: WinGui: Add support to make the windows UI more portable friendly. Configurable via portable.ini file. All temp, presets, settings, logs etc can be stored in a specific or current directory. --- .../HandBrakeWPF/Utilities/DirectoryUtilities.cs | 32 ++++- win/CS/HandBrakeWPF/Utilities/Portable.cs | 147 +++++++++++++++++++++ 2 files changed, 177 insertions(+), 2 deletions(-) create mode 100644 win/CS/HandBrakeWPF/Utilities/Portable.cs (limited to 'win/CS/HandBrakeWPF/Utilities') diff --git a/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs b/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs index 0f63ebfce..df7f2a1a3 100644 --- a/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs +++ b/win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs @@ -34,14 +34,25 @@ namespace HandBrakeWPF.Utilities { if (isNightly) { - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "HandBrake", "Nightly"); + return Path.Combine(GetStorageDirectory(), "HandBrake", "Nightly"); } else { - return Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "HandBrake"); + return Path.Combine(GetStorageDirectory(), "HandBrake"); } } + /// + /// Get the app default log directory. + /// + /// + /// The . + /// + public static string GetLogDirectory() + { + return Path.Combine(GetStorageDirectory(), "HandBrake", "logs"); + } + /// /// Simple way of checking if a directory is writeable. /// @@ -75,5 +86,22 @@ namespace HandBrakeWPF.Utilities return false; } } + + /// + /// The get storage directory. + /// + /// + /// The storage directory. Either AppData or portable location. + /// + private static string GetStorageDirectory() + { + string storagePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + if (Portable.IsPortable()) + { + storagePath = Portable.GetStorageDirectory(); + } + + return storagePath; + } } } diff --git a/win/CS/HandBrakeWPF/Utilities/Portable.cs b/win/CS/HandBrakeWPF/Utilities/Portable.cs new file mode 100644 index 000000000..9b5981675 --- /dev/null +++ b/win/CS/HandBrakeWPF/Utilities/Portable.cs @@ -0,0 +1,147 @@ +// -------------------------------------------------------------------------------------------------------------------- +// +// This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License. +// +// +// Defines the Portable type. +// +// -------------------------------------------------------------------------------------------------------------------- + +namespace HandBrakeWPF.Utilities +{ + using System.Collections.Generic; + using System; + using System.IO; + + /// + /// This class is responsible for reading the Portable.ini file that allows HandBrake to be run out of a directory. + /// + public class Portable + { + private static readonly string portableFile = Path.Combine(Environment.CurrentDirectory, "portable.ini"); + private static Dictionary keyPairs = new Dictionary(); + + /// + /// Initializes a new instance of the class. + /// + public static void Initialise() + { + if (!IsPortable()) + { + return; // Nothing to do. + } + + // Read the INI file + if (File.Exists(portableFile)) + { + using (StreamReader fileReader = new StreamReader(portableFile)) + { + string line; + while ((line = fileReader.ReadLine()) != null) + { + 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()); + } + } + } + } + + // Create any missing directories + if (!Directory.Exists(GetTempDirectory())) + { + Directory.CreateDirectory(GetTempDirectory()); + } + + if (!Directory.Exists(GetStorageDirectory())) + { + Directory.CreateDirectory(GetStorageDirectory()); + } + + // Setup enviroment variables for this instance. + Environment.SetEnvironmentVariable("TMP", GetTempDirectory()); + } + + /// + /// The is portable. + /// + /// + /// The . + /// + public static bool IsPortable() + { + if (!File.Exists(portableFile)) + { + return false; + } + + return true; + } + + /// + /// The get config directory. + /// + /// + /// The . + /// + public static string GetStorageDirectory() + { + // Default to App Data + string storagePath = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData); + if (keyPairs.ContainsKey("storage.dir")) + { + string directory = keyPairs["storage.dir"]; + + // If "cwd", then treat that as Current Working Directory. + if (directory == "cwd") + { + storagePath = Path.Combine(Environment.CurrentDirectory, "storage"); + } + + // Otherwise, the users set directory. + if (!string.IsNullOrEmpty(directory) && directory != "cwd") + { + storagePath = directory; + } + } + + // Return what path we figured out to use. + return storagePath; + } + + /// + /// The get temp directory. + /// + /// + /// The . + /// + private static string GetTempDirectory() + { + if (keyPairs.ContainsKey("tmp.dir")) + { + string directory = keyPairs["tmp.dir"]; + if (directory == "cwd") + { + return Path.Combine(Environment.CurrentDirectory, "tmp"); + } + + if (!string.IsNullOrEmpty(directory) && directory != "cwd") + { + return directory; + } + + return null; + } + + return null; + } + } +} \ No newline at end of file -- cgit v1.2.3