]> granicus.if.org Git - handbrake/commitdiff
WinGui: Add support to make the windows UI more portable friendly. Configurable via...
authorsr55 <sr55.hb@outlook.com>
Mon, 26 Dec 2016 15:21:20 +0000 (15:21 +0000)
committersr55 <sr55.hb@outlook.com>
Mon, 26 Dec 2016 15:21:20 +0000 (15:21 +0000)
win/CS/HandBrakeWPF/App.xaml.cs
win/CS/HandBrakeWPF/HandBrakeWPF.csproj
win/CS/HandBrakeWPF/Helpers/LogManager.cs
win/CS/HandBrakeWPF/Utilities/DirectoryUtilities.cs
win/CS/HandBrakeWPF/Utilities/Portable.cs [new file with mode: 0644]
win/CS/HandBrakeWPF/portable.ini.template [new file with mode: 0644]

index 7f7d7ef47b0fd4bd96b5af30ff959ce7425d32d2..73971ff08d1e63c4a00a163cb1c8e4eeb5927962 100644 (file)
@@ -66,6 +66,12 @@ namespace HandBrakeWPF
                 StartupOptions.AutoRestartQueue = true;\r
             }\r
 \r
+            // Portable Mode\r
+            if (Portable.IsPortable())\r
+            {\r
+                Portable.Initialise();\r
+            }\r
+\r
             base.OnStartup(e);\r
 \r
             // If we have a file dropped on the icon, try scanning it.\r
index 5537b404514666bce0514ea7008de7348733e08c..207083bd6f2ef07385bb6b64c0c075a2e2271940 100644 (file)
     <Compile Include="Utilities\Input\ChapterImporterXml.cs" />\r
     <Compile Include="Utilities\Interfaces\INotifyPropertyChangedEx.cs" />\r
     <Compile Include="Utilities\Output\CsvHelper.cs" />\r
+    <Compile Include="Utilities\Portable.cs" />\r
     <Compile Include="Utilities\PropertyChangedBase.cs" />\r
     <Compile Include="Utilities\DirectoryUtilities.cs" />\r
     <Compile Include="Utilities\SystemInfo.cs" />\r
     <None Include="Installer\MakeNightly64.nsi" />\r
     <AppDesigner Include="Properties\" />\r
     <EmbeddedResource Include="public.key" />\r
+    <None Include="portable.ini.template">\r
+      <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>\r
+    </None>\r
   </ItemGroup>\r
   <ItemGroup>\r
     <Page Include="Controls\AlertPanel.xaml">\r
index f551af10eaa8b0bdfc73276fe8f644d84baf08aa..2cd94a25d965ecce903968f6dab7abba4f51342d 100644 (file)
@@ -29,7 +29,7 @@ namespace HandBrakeWPF.Helpers
         public static void Init()
         {
             ILog log = LogService.GetLogger();
-            string logDir = Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData) + "\\HandBrake\\logs";
+            string logDir = DirectoryUtilities.GetLogDirectory();
             string logFile = Path.Combine(logDir, string.Format("activity_log{0}.txt", GeneralUtilities.ProcessId));
             if (!Directory.Exists(Path.GetDirectoryName(logFile)))
             {
index 0f63ebfce9d6a1f0bbc44d08634c55b7182e3ced..df7f2a1a3a5fff2f4a57b8d2050573841666cd75 100644 (file)
@@ -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");
             }
         }
 
+        /// <summary>
+        /// Get the app default log directory.
+        /// </summary>
+        /// <returns>
+        /// The <see cref="string"/>.
+        /// </returns>
+        public static string GetLogDirectory()
+        {
+            return Path.Combine(GetStorageDirectory(), "HandBrake", "logs");
+        }
+
         /// <summary>
         /// Simple way of checking if a directory is writeable.
         /// </summary>
@@ -75,5 +86,22 @@ namespace HandBrakeWPF.Utilities
                 return false;
             }
         }
+
+        /// <summary>
+        /// The get storage directory.
+        /// </summary>
+        /// <returns>
+        /// The storage directory. Either AppData or portable location.
+        /// </returns>
+        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 (file)
index 0000000..9b59816
--- /dev/null
@@ -0,0 +1,147 @@
+// --------------------------------------------------------------------------------------------------------------------
+// <copyright file="Portable.cs" company="HandBrake Project (http://handbrake.fr)">
+//   This file is part of the HandBrake source code - It may be used under the terms of the GNU General Public License.
+// </copyright>
+// <summary>
+//   Defines the Portable type.
+// </summary>
+// --------------------------------------------------------------------------------------------------------------------
+
+namespace HandBrakeWPF.Utilities
+{
+    using System.Collections.Generic;
+    using System;
+    using System.IO;
+
+    /// <summary>
+    /// This class is responsible for reading the Portable.ini file that allows HandBrake to be run out of a directory.
+    /// </summary>
+    public class Portable
+    {
+        private static readonly string portableFile = Path.Combine(Environment.CurrentDirectory, "portable.ini");
+        private static Dictionary<string, string> keyPairs = new Dictionary<string, string>();
+
+        /// <summary>
+        /// Initializes a new instance of the <see cref="Portable"/> class.
+        /// </summary>
+        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());
+        }
+
+        /// <summary>
+        /// The is portable.
+        /// </summary>
+        /// <returns>
+        /// The <see cref="bool"/>.
+        /// </returns>
+        public static bool IsPortable()
+        {
+            if (!File.Exists(portableFile))
+            {
+                return false;
+            }
+
+            return true;
+        }
+
+        /// <summary>
+        /// The get config directory.
+        /// </summary>
+        /// <returns>
+        /// The <see cref="string"/>.
+        /// </returns>
+        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;
+        }
+
+        /// <summary>
+        /// The get temp directory.
+        /// </summary>
+        /// <returns>
+        /// The <see cref="string"/>.
+        /// </returns>
+        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
diff --git a/win/CS/HandBrakeWPF/portable.ini.template b/win/CS/HandBrakeWPF/portable.ini.template
new file mode 100644 (file)
index 0000000..4c62e6e
--- /dev/null
@@ -0,0 +1,14 @@
+#################################
+# HandBrake Portable
+#################################
+# Notes:
+# - Rename this file to portable.ini to activate feature.
+# - storage.dir => Stores Presets, Settings and Log Files.
+# - tmp.dir => temporary files only. (i.e Preview images)
+#     
+# Set to 'cwd' to use the current applications directory. It will automatically create "storage" and "tmp" folders in this instance.
+# Leave blank to use the system "TMP" directory and the "AppData" user profile folder.
+#################################
+
+storage.dir = cwd
+tmp.dir = cwd
\ No newline at end of file