diff --git a/FileManager/AudibleFileStorage.cs b/FileManager/AudibleFileStorage.cs index d7db110c..36797785 100644 --- a/FileManager/AudibleFileStorage.cs +++ b/FileManager/AudibleFileStorage.cs @@ -28,36 +28,9 @@ namespace FileManager public static AudibleFileStorage AAXC { get; } = new AaxcFileStorage(); public static AudibleFileStorage PDF { get; } = new PdfFileStorage(); - public static string DownloadsInProgress - { - get - { - if (!Configuration.Instance.DownloadsInProgressEnum.In("WinTemp", "LibationFiles")) - Configuration.Instance.DownloadsInProgressEnum = "WinTemp"; - var AaxRootDir - = Configuration.Instance.DownloadsInProgressEnum == "WinTemp" - ? Configuration.WinTemp - : Configuration.Instance.LibationFiles; + public static string DownloadsInProgress => Directory.CreateDirectory(Path.Combine(Configuration.Instance.DownloadsInProgressEnum, "DownloadsInProgress")).FullName; - return Directory.CreateDirectory(Path.Combine(AaxRootDir, "DownloadsInProgress")).FullName; - } - } - - public static string DecryptInProgress - { - get - { - if (!Configuration.Instance.DecryptInProgressEnum.In("WinTemp", "LibationFiles")) - Configuration.Instance.DecryptInProgressEnum = "WinTemp"; - - var M4bRootDir - = Configuration.Instance.DecryptInProgressEnum == "WinTemp" - ? Configuration.WinTemp - : Configuration.Instance.LibationFiles; - - return Directory.CreateDirectory(Path.Combine(M4bRootDir, "DecryptInProgress")).FullName; - } - } + public static string DecryptInProgress => Directory.CreateDirectory(Path.Combine(Configuration.Instance.DecryptInProgressEnum, "DecryptInProgress")).FullName; // not customizable. don't move to config public static string DownloadsFinal => new DirectoryInfo(Configuration.Instance.LibationFiles).CreateSubdirectory("DownloadsFinal").FullName; diff --git a/FileManager/Configuration.cs b/FileManager/Configuration.cs index 1a946063..3e0cb752 100644 --- a/FileManager/Configuration.cs +++ b/FileManager/Configuration.cs @@ -50,18 +50,62 @@ namespace FileManager set => persistentDictionary.Set(nameof(Books), value); } - private const string APP_DIR = "AppDir"; - public static string AppDir { get; } = Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Exe.FileLocationOnDisk), LIBATION_FILES)); - public static string MyDocs { get; } = Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), LIBATION_FILES)); - public static string WinTemp { get; } = Path.GetFullPath(Path.Combine(Path.GetTempPath(), "Libation")); + #region known directories + public const string WIN_TEMP_LABEL = "WinTemp"; + public const string LIBATION_FILES_LABEL = "LibationFiles"; + public const string USER_PROFILE_LABEL = "UserProfile"; + + public static string AppDir_Relative => @".\LibationFiles"; + public static string AppDir_Absolute => Path.GetFullPath(Path.Combine(Path.GetDirectoryName(Exe.FileLocationOnDisk), LIBATION_FILES)); + public static string MyDocs => Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments), "LibationFiles")); + public static string WinTemp => Path.GetFullPath(Path.Combine(Path.GetTempPath(), "Libation")); + public static string UserProfile => Path.GetFullPath(Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.UserProfile), "Libation")); - private Dictionary wellKnownPaths { get; } = new Dictionary + public enum KnownDirectories { - [APP_DIR] = AppDir, - ["MyDocs"] = MyDocs, - ["WinTemp"] = WinTemp + None = 0, + + [Description("In my Users folder")] + UserProfile = 1, + + [Description("In the same folder that Libation is running from")] + AppDir = 2, + + [Description("In your Windows temporary folder")] + WinTemp = 3, + + [Description("In My Documents")] + MyDocs = 4, + + [Description("In your settings folder (aka: Libation Files)")] + LibationFiles = 5 + } + // use func calls so we always get the latest value of LibationFiles + private static List<(KnownDirectories directory, Func getPathFunc)> directoryOptionsPaths { get; } = new() + { + (KnownDirectories.None, () => null), + (KnownDirectories.UserProfile, () => UserProfile), + (KnownDirectories.AppDir, () => AppDir_Relative), + (KnownDirectories.WinTemp, () => WinTemp), + (KnownDirectories.MyDocs, () => MyDocs), + // this is important to not let very early calls try to accidentally load LibationFiles too early + (KnownDirectories.LibationFiles, () => libationFilesPathCache) }; - private string libationFilesPathCache; + public static string GetKnownDirectoryPath(KnownDirectories directory) + { + var dirFunc = directoryOptionsPaths.SingleOrDefault(dirFunc => dirFunc.directory == directory); + return dirFunc == default ? null : dirFunc.getPathFunc(); + } + public static KnownDirectories GetKnownDirectory(string directory) + { + // especially important so a very early call doesn't match null => LibationFiles + if (string.IsNullOrWhiteSpace(directory)) + return KnownDirectories.None; + + var dirFunc = directoryOptionsPaths.SingleOrDefault(dirFunc => dirFunc.getPathFunc() == directory); + return dirFunc == default ? KnownDirectories.None : dirFunc.directory; + } + #endregion // default setting and directory creation occur in class responsible for files. // config class is only responsible for path. not responsible for setting defaults, dir validation, or dir creation @@ -96,26 +140,29 @@ namespace FileManager private Configuration() { } private const string APPSETTINGS_JSON = "appsettings.json"; + // this is the key in appsettings. Happens to match the metadirectory name but separate concern. keep separate private const string LIBATION_FILES = "LibationFiles"; [Description("Location for storage of program-created files")] - public string LibationFiles => libationFilesPathCache ?? getLibationFiles(); - private string getLibationFiles() + public string LibationFiles { - var value = getLiberationFilesSettingFromJson(); + get + { + if (libationFilesPathCache is not null) + return libationFilesPathCache; - // this looks weird but is correct for translating wellKnownPaths - if (wellKnownPaths.ContainsKey(value)) - value = wellKnownPaths[value]; + // must write here before SettingsFilePath in next step reads cache + libationFilesPathCache = getLiberationFilesSettingFromJson(); - // must write here before SettingsFilePath in next step reads cache - libationFilesPathCache = value; + // load json values into memory. create settings if not exists + persistentDictionary = new PersistentDictionary(SettingsFilePath); - // load json values into memory. create if not exists - persistentDictionary = new PersistentDictionary(SettingsFilePath); - - return libationFilesPathCache; + return libationFilesPathCache; + } } + + private static string libationFilesPathCache; + private string getLiberationFilesSettingFromJson() { string startingContents = null; @@ -124,26 +171,33 @@ namespace FileManager if (File.Exists(APPSETTINGS_JSON)) { startingContents = File.ReadAllText(APPSETTINGS_JSON); - var jObj = JObject.Parse(startingContents); + var startingJObj = JObject.Parse(startingContents); - if (jObj.ContainsKey(LIBATION_FILES)) + if (startingJObj.ContainsKey(LIBATION_FILES)) { - var value = jObj[LIBATION_FILES].Value(); + var startingValue = startingJObj[LIBATION_FILES].Value(); // do not check whether directory exists. special/meta directory (eg: AppDir) is valid - if (!string.IsNullOrWhiteSpace(value)) - return value; + if (!string.IsNullOrWhiteSpace(startingValue)) + return startingValue; } } } catch { } - var endingContents = new JObject { { LIBATION_FILES, APP_DIR } }.ToString(Formatting.Indented); - + // not found. write to file. read from file + var endingContents = new JObject { { LIBATION_FILES, UserProfile } }.ToString(Formatting.Indented); if (startingContents != endingContents) + { File.WriteAllText(APPSETTINGS_JSON, endingContents); + System.Threading.Thread.Sleep(100); + } - return APP_DIR; + // do not check whether directory exists. special/meta directory (eg: AppDir) is valid + // verify from live file. no try/catch. want failures to be visible + var jObjFinal = JObject.Parse(File.ReadAllText(APPSETTINGS_JSON)); + var valueFinal = jObjFinal[LIBATION_FILES].Value(); + return valueFinal; } public object GetObject(string propertyName) => persistentDictionary.GetObject(propertyName); @@ -163,17 +217,16 @@ namespace FileManager public bool TrySetLibationFiles(string directory) { - if (!Directory.Exists(directory) && !wellKnownPaths.ContainsKey(directory)) - return false; + // this is WRONG. need to MOVE settings; not DELETE them - // if moving from default, delete old settings file and dir (if empty) - if (LibationFiles.EqualsInsensitive(AppDir)) - { - File.Delete(SettingsFilePath); - System.Threading.Thread.Sleep(100); - if (!Directory.EnumerateDirectories(AppDir).Any() && !Directory.EnumerateFiles(AppDir).Any()) - Directory.Delete(AppDir); - } + //// if moving from default, delete old settings file and dir (if empty) + //if (LibationFiles.EqualsInsensitive(AppDir)) + //{ + // File.Delete(SettingsFilePath); + // System.Threading.Thread.Sleep(100); + // if (!Directory.EnumerateDirectories(AppDir).Any() && !Directory.EnumerateFiles(AppDir).Any()) + // Directory.Delete(AppDir); + //} libationFilesPathCache = null; diff --git a/LibationLauncher/Program.cs b/LibationLauncher/Program.cs index d41c1396..6290b13e 100644 --- a/LibationLauncher/Program.cs +++ b/LibationLauncher/Program.cs @@ -32,6 +32,7 @@ namespace LibationLauncher migrate_to_v4_0_0(); migrate_to_v5_0_0(); + migrate_to_v5_2_0(); ensureSerilogConfig(); configureLogging(); @@ -57,9 +58,9 @@ namespace LibationLauncher var setupDialog = new SetupDialog(); setupDialog.NoQuestionsBtn_Click += (_, __) => { - config.DownloadsInProgressEnum ??= "WinTemp"; - config.DecryptInProgressEnum ??= "WinTemp"; - config.Books ??= Configuration.AppDir; + config.DownloadsInProgressEnum ??= Configuration.WIN_TEMP_LABEL; + config.DecryptInProgressEnum ??= Configuration.WIN_TEMP_LABEL; + config.Books ??= Configuration.AppDir_Relative; config.AllowLibationFixup = true; }; // setupDialog.BasicBtn_Click += (_, __) => // no action needed @@ -256,9 +257,53 @@ namespace LibationLauncher } } } - #endregion + #endregion - private static void ensureSerilogConfig() + #region migrate to v5.2.0 : get rid of meta-directories + private static void migrate_to_v5_2_0() + { + var config = Configuration.Instance; + + { + var newPath = TranslatePath(config.DecryptInProgressEnum); + if (newPath != config.DecryptInProgressEnum) + config.DecryptInProgressEnum = newPath; + } + + { + var newPath = TranslatePath(config.DownloadsInProgressEnum); + if (newPath != config.DownloadsInProgressEnum) + config.DownloadsInProgressEnum = newPath; + } + + { + var appsettings = "appsettings.json"; + var libationFiles = "LibationFiles"; + + var jObj = JObject.Parse(File.ReadAllText(appsettings)); + var origVlaue = jObj[libationFiles].Value(); + var newValue = TranslatePath(origVlaue); + + if (newValue != origVlaue) + { + var contents = new JObject { { libationFiles, newValue } }.ToString(Formatting.Indented); + File.WriteAllText(appsettings, contents); + } + } + } + + private static string TranslatePath(string path) + => path switch + { + "AppDir" => Configuration.AppDir_Relative, + "MyDocs" => Configuration.MyDocs, + Configuration.USER_PROFILE_LABEL => Configuration.UserProfile, + Configuration.WIN_TEMP_LABEL => Configuration.WinTemp, + _ => path + }; + #endregion + + private static void ensureSerilogConfig() { var config = Configuration.Instance; diff --git a/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.Designer.cs b/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.Designer.cs new file mode 100644 index 00000000..7fc9170a --- /dev/null +++ b/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.Designer.cs @@ -0,0 +1,113 @@ + +namespace LibationWinForms.Dialogs +{ + partial class DirectoryOrCustomSelectControl + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.knownDirectoryRb = new System.Windows.Forms.RadioButton(); + this.customDirectoryRb = new System.Windows.Forms.RadioButton(); + this.customTb = new System.Windows.Forms.TextBox(); + this.customBtn = new System.Windows.Forms.Button(); + this.directorySelectControl = new LibationWinForms.Dialogs.DirectorySelectControl(); + this.SuspendLayout(); + // + // knownDirectoryRb + // + this.knownDirectoryRb.AutoSize = true; + this.knownDirectoryRb.Location = new System.Drawing.Point(3, 3); + this.knownDirectoryRb.Name = "knownDirectoryRb"; + this.knownDirectoryRb.Size = new System.Drawing.Size(14, 13); + this.knownDirectoryRb.TabIndex = 0; + this.knownDirectoryRb.UseVisualStyleBackColor = true; + this.knownDirectoryRb.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); + // + // customDirectoryRb + // + this.customDirectoryRb.AutoSize = true; + this.customDirectoryRb.Location = new System.Drawing.Point(2, 56); + this.customDirectoryRb.Name = "customDirectoryRb"; + this.customDirectoryRb.Size = new System.Drawing.Size(14, 13); + this.customDirectoryRb.TabIndex = 2; + this.customDirectoryRb.UseVisualStyleBackColor = true; + this.customDirectoryRb.CheckedChanged += new System.EventHandler(this.radioButton_CheckedChanged); + // + // customTb + // + this.customTb.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.customTb.Location = new System.Drawing.Point(22, 52); + this.customTb.Name = "customTb"; + this.customTb.Size = new System.Drawing.Size(588, 23); + this.customTb.TabIndex = 3; + // + // customBtn + // + this.customBtn.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right))); + this.customBtn.Location = new System.Drawing.Point(616, 52); + this.customBtn.Name = "customBtn"; + this.customBtn.Size = new System.Drawing.Size(41, 27); + this.customBtn.TabIndex = 4; + this.customBtn.Text = "..."; + this.customBtn.UseVisualStyleBackColor = true; + this.customBtn.Click += new System.EventHandler(this.customBtn_Click); + // + // directorySelectControl + // + this.directorySelectControl.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.directorySelectControl.Location = new System.Drawing.Point(23, 0); + this.directorySelectControl.Name = "directorySelectControl"; + this.directorySelectControl.Size = new System.Drawing.Size(635, 46); + this.directorySelectControl.TabIndex = 5; + // + // DirectoryOrCustomSelectControl + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.directorySelectControl); + this.Controls.Add(this.customBtn); + this.Controls.Add(this.customTb); + this.Controls.Add(this.customDirectoryRb); + this.Controls.Add(this.knownDirectoryRb); + this.Name = "DirectoryOrCustomSelectControl"; + this.Size = new System.Drawing.Size(660, 81); + this.Load += new System.EventHandler(this.DirectoryOrCustomSelectControl_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.RadioButton knownDirectoryRb; + private System.Windows.Forms.RadioButton customDirectoryRb; + private System.Windows.Forms.TextBox customTb; + private System.Windows.Forms.Button customBtn; + private DirectorySelectControl directorySelectControl; + } +} diff --git a/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.cs b/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.cs new file mode 100644 index 00000000..4783db0e --- /dev/null +++ b/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.cs @@ -0,0 +1,88 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; +using Dinah.Core; +using FileManager; + +namespace LibationWinForms.Dialogs +{ + public partial class DirectoryOrCustomSelectControl : UserControl + { + public string SelectedDirectory + => customDirectoryRb.Checked ? customTb.Text.Trim() + : knownDirectoryRb.Checked ? directorySelectControl.SelectedDirectory + : null; + + public DirectoryOrCustomSelectControl() + { + InitializeComponent(); + + // doing this after InitializeComponent will fire event + this.knownDirectoryRb.Checked = true; + } + + /// Set items for combobox + /// List rather than IEnumerable so that client can determine display order + /// + public void SetDirectoryItems(List knownDirectories, Configuration.KnownDirectories? defaultDirectory = Configuration.KnownDirectories.UserProfile) + => this.directorySelectControl.SetDirectoryItems(knownDirectories, defaultDirectory); + + /// select, set default, or rehydrate + /// + public void SelectDirectory(Configuration.KnownDirectories directory) + { + // if None: take no action + if (directory != Configuration.KnownDirectories.None) + selectDir(directory, null); + } + + /// select, set default, or rehydrate + public void SelectDirectory(string directory) + { + directory = directory?.Trim() ?? ""; + selectDir(Configuration.GetKnownDirectory(directory), directory); + } + + private void selectDir(Configuration.KnownDirectories knownDir, string customDir) + { + var unknown = knownDir == Configuration.KnownDirectories.None; + customDirectoryRb.Checked = unknown; + knownDirectoryRb.Checked = !unknown; + this.directorySelectControl.SelectDirectory(knownDir); + this.customTb.Text = unknown ? customDir : ""; + } + + private string dirSearchTitle; + public void SetSearchTitle(string dirSearchTitle) => this.dirSearchTitle = dirSearchTitle?.Trim(); + + private void customBtn_Click(object sender, EventArgs e) + { + using var dialog = new FolderBrowserDialog + { + Description = string.IsNullOrWhiteSpace(dirSearchTitle) ? "Search" : $"Search for {dirSearchTitle}", + SelectedPath = this.customTb.Text + }; + dialog.ShowDialog(); + if (!string.IsNullOrWhiteSpace(dialog.SelectedPath)) + this.customTb.Text = dialog.SelectedPath; + } + + private void radioButton_CheckedChanged(object sender, EventArgs e) + { + var isCustom = this.customDirectoryRb.Checked; + + customTb.Enabled = isCustom; + customBtn.Enabled = isCustom; + + directorySelectControl.Enabled = !isCustom; + } + + private void DirectoryOrCustomSelectControl_Load(object sender, EventArgs e) + { + if (this.DesignMode) + return; + + } + } +} diff --git a/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.resx b/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.resx new file mode 100644 index 00000000..f298a7be --- /dev/null +++ b/LibationWinForms/Dialogs/DirectoryOrCustomSelectControl.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/LibationWinForms/Dialogs/DirectorySelectControl.Designer.cs b/LibationWinForms/Dialogs/DirectorySelectControl.Designer.cs new file mode 100644 index 00000000..9527062e --- /dev/null +++ b/LibationWinForms/Dialogs/DirectorySelectControl.Designer.cs @@ -0,0 +1,76 @@ + +namespace LibationWinForms.Dialogs +{ + partial class DirectorySelectControl + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Component Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.directoryComboBox = new System.Windows.Forms.ComboBox(); + this.label1 = new System.Windows.Forms.Label(); + this.SuspendLayout(); + // + // directoryComboBox + // + this.directoryComboBox.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left) + | System.Windows.Forms.AnchorStyles.Right))); + this.directoryComboBox.DropDownStyle = System.Windows.Forms.ComboBoxStyle.DropDownList; + this.directoryComboBox.FormattingEnabled = true; + this.directoryComboBox.Location = new System.Drawing.Point(0, 0); + this.directoryComboBox.Name = "directoryComboBox"; + this.directoryComboBox.Size = new System.Drawing.Size(647, 23); + this.directoryComboBox.TabIndex = 0; + this.directoryComboBox.SelectedIndexChanged += new System.EventHandler(this.directoryComboBox_SelectedIndexChanged); + // + // label1 + // + this.label1.AutoSize = true; + this.label1.Location = new System.Drawing.Point(0, 26); + this.label1.Name = "label1"; + this.label1.Size = new System.Drawing.Size(97, 15); + this.label1.TabIndex = 1; + this.label1.Text = "Select a directory"; + // + // DirectorySelectControl + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.Controls.Add(this.label1); + this.Controls.Add(this.directoryComboBox); + this.Name = "DirectorySelectControl"; + this.Size = new System.Drawing.Size(647, 46); + this.Load += new System.EventHandler(this.DirectorySelectControl_Load); + this.ResumeLayout(false); + this.PerformLayout(); + + } + + #endregion + + private System.Windows.Forms.ComboBox directoryComboBox; + private System.Windows.Forms.Label label1; + } +} diff --git a/LibationWinForms/Dialogs/DirectorySelectControl.cs b/LibationWinForms/Dialogs/DirectorySelectControl.cs new file mode 100644 index 00000000..d2497ac9 --- /dev/null +++ b/LibationWinForms/Dialogs/DirectorySelectControl.cs @@ -0,0 +1,86 @@ +using System; +using System.Collections.Generic; +using System.Linq; +using System.Windows.Forms; +using Dinah.Core; +using FileManager; + +namespace LibationWinForms.Dialogs +{ + public partial class DirectorySelectControl : UserControl + { + private class DirectoryComboBoxItem + { + public string Description { get; } + public Configuration.KnownDirectories Value { get; } + + public string FullPath => Configuration.GetKnownDirectoryPath(Value); + + /// Displaying relative paths is confusing. UI should display absolute equivalent + public string UiDisplayPath => Value == Configuration.KnownDirectories.AppDir ? Configuration.AppDir_Absolute : FullPath; + + public DirectoryComboBoxItem(Configuration.KnownDirectories knownDirectory) + { + Value = knownDirectory; + Description = Value.GetDescription(); + } + + public override string ToString() => Description; + } + + private DirectoryComboBoxItem selectedItem => (DirectoryComboBoxItem)this.directoryComboBox.SelectedItem; + public string SelectedDirectory => selectedItem?.FullPath; + + public DirectorySelectControl() => InitializeComponent(); + + /// Set items for combobox + /// List rather than IEnumerable so that client can determine display order + /// Optional default item to select + public void SetDirectoryItems(List knownDirectories, Configuration.KnownDirectories? defaultDirectory = Configuration.KnownDirectories.UserProfile) + { + this.directoryComboBox.Items.Clear(); + + foreach (var dir in knownDirectories.Where(d => d != Configuration.KnownDirectories.None).Distinct()) + this.directoryComboBox.Items.Add(new DirectoryComboBoxItem(dir)); + + SelectDirectory(defaultDirectory); + } + + /// select, set default, or rehydrate + /// + /// True is there was a matching entry + public bool SelectDirectory(string directory) => SelectDirectory(Configuration.GetKnownDirectory(directory)); + + /// select, set default, or rehydrate + /// + /// True is there was a matching entry + public bool SelectDirectory(Configuration.KnownDirectories? directory) + { + if (directory is null || directory == Configuration.KnownDirectories.None) + { + this.directoryComboBox.SelectedIndex = 0; + return false; + } + + // set default + var item = this.directoryComboBox.Items.Cast().SingleOrDefault(item => item.Value == directory.Value); + if (item is null) + { + this.directoryComboBox.SelectedIndex = 0; + return false; + } + + this.directoryComboBox.SelectedItem = item; + return true; + } + + private void DirectorySelectControl_Load(object sender, EventArgs e) + { + if (this.DesignMode) + return; + + } + + private void directoryComboBox_SelectedIndexChanged(object sender, EventArgs e) => this.label1.Text = selectedItem.UiDisplayPath; + } +} diff --git a/LibationWinForms/Dialogs/DirectorySelectControl.resx b/LibationWinForms/Dialogs/DirectorySelectControl.resx new file mode 100644 index 00000000..f298a7be --- /dev/null +++ b/LibationWinForms/Dialogs/DirectorySelectControl.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file diff --git a/LibationWinForms/Dialogs/LibationFilesDialog.cs b/LibationWinForms/Dialogs/LibationFilesDialog.cs index c35013ae..3b2799a1 100644 --- a/LibationWinForms/Dialogs/LibationFilesDialog.cs +++ b/LibationWinForms/Dialogs/LibationFilesDialog.cs @@ -26,9 +26,9 @@ namespace LibationWinForms.Dialogs return; libationFilesDescLbl.Text = desc(nameof(config.LibationFiles)); - this.libationFilesRootRb.Text = "In the same folder that Libation is running from\r\n" + Configuration.AppDir; + this.libationFilesRootRb.Text = "In the same folder that Libation is running from\r\n" + Configuration.AppDir_Relative; this.libationFilesMyDocsRb.Text = "In My Documents\r\n" + Configuration.MyDocs; - if (config.LibationFiles == Configuration.AppDir) + if (config.LibationFiles == Configuration.AppDir_Relative) libationFilesRootRb.Checked = true; else if (config.LibationFiles == Configuration.MyDocs) libationFilesMyDocsRb.Checked = true; @@ -52,7 +52,7 @@ namespace LibationWinForms.Dialogs private void saveBtn_Click(object sender, EventArgs e) { var libationDir - = libationFilesRootRb.Checked ? Configuration.AppDir + = libationFilesRootRb.Checked ? Configuration.AppDir_Relative : libationFilesMyDocsRb.Checked ? Configuration.MyDocs : libationFilesCustomTb.Text; if (!config.TrySetLibationFiles(libationDir)) diff --git a/LibationWinForms/Dialogs/SettingsDialog.cs b/LibationWinForms/Dialogs/SettingsDialog.cs index 5a42291a..12334ab3 100644 --- a/LibationWinForms/Dialogs/SettingsDialog.cs +++ b/LibationWinForms/Dialogs/SettingsDialog.cs @@ -43,10 +43,10 @@ namespace LibationWinForms.Dialogs switch (config.DownloadsInProgressEnum) { - case "LibationFiles": + case Configuration.LIBATION_FILES_LABEL: downloadsInProgressLibationFilesRb.Checked = true; break; - case "WinTemp": + case Configuration.WIN_TEMP_LABEL: default: downloadsInProgressWinTempRb.Checked = true; break; @@ -54,10 +54,10 @@ namespace LibationWinForms.Dialogs switch (config.DecryptInProgressEnum) { - case "LibationFiles": + case Configuration.LIBATION_FILES_LABEL: decryptInProgressLibationFilesRb.Checked = true; break; - case "WinTemp": + case Configuration.WIN_TEMP_LABEL: default: decryptInProgressWinTempRb.Checked = true; break; @@ -77,8 +77,8 @@ namespace LibationWinForms.Dialogs private void saveBtn_Click(object sender, EventArgs e) { config.AllowLibationFixup = allowLibationFixupCbox.Checked; - config.DownloadsInProgressEnum = downloadsInProgressLibationFilesRb.Checked ? "LibationFiles" : "WinTemp"; - config.DecryptInProgressEnum = decryptInProgressLibationFilesRb.Checked ? "LibationFiles" : "WinTemp"; + config.DownloadsInProgressEnum = downloadsInProgressLibationFilesRb.Checked ? Configuration.LIBATION_FILES_LABEL : Configuration.WIN_TEMP_LABEL; + config.DecryptInProgressEnum = decryptInProgressLibationFilesRb.Checked ? Configuration.LIBATION_FILES_LABEL : Configuration.WIN_TEMP_LABEL; var newBooks = this.booksLocationTb.Text; if (!Directory.Exists(newBooks)) diff --git a/LibationWinForms/Dialogs/TEMP_TestNewControls.Designer.cs b/LibationWinForms/Dialogs/TEMP_TestNewControls.Designer.cs new file mode 100644 index 00000000..e66458d4 --- /dev/null +++ b/LibationWinForms/Dialogs/TEMP_TestNewControls.Designer.cs @@ -0,0 +1,81 @@ + +namespace LibationWinForms.Dialogs +{ + partial class TEMP_TestNewControls + { + /// + /// Required designer variable. + /// + private System.ComponentModel.IContainer components = null; + + /// + /// Clean up any resources being used. + /// + /// true if managed resources should be disposed; otherwise, false. + protected override void Dispose(bool disposing) + { + if (disposing && (components != null)) + { + components.Dispose(); + } + base.Dispose(disposing); + } + + #region Windows Form Designer generated code + + /// + /// Required method for Designer support - do not modify + /// the contents of this method with the code editor. + /// + private void InitializeComponent() + { + this.button1 = new System.Windows.Forms.Button(); + this.directorySelectControl1 = new LibationWinForms.Dialogs.DirectorySelectControl(); + this.directoryOrCustomSelectControl1 = new LibationWinForms.Dialogs.DirectoryOrCustomSelectControl(); + this.SuspendLayout(); + // + // button1 + // + this.button1.Location = new System.Drawing.Point(438, 375); + this.button1.Name = "button1"; + this.button1.Size = new System.Drawing.Size(75, 23); + this.button1.TabIndex = 2; + this.button1.Text = "button1"; + this.button1.UseVisualStyleBackColor = true; + this.button1.Click += new System.EventHandler(this.button1_Click); + // + // directorySelectControl1 + // + this.directorySelectControl1.Location = new System.Drawing.Point(30, 54); + this.directorySelectControl1.Name = "directorySelectControl1"; + this.directorySelectControl1.Size = new System.Drawing.Size(758, 46); + this.directorySelectControl1.TabIndex = 4; + // + // directoryOrCustomSelectControl1 + // + this.directoryOrCustomSelectControl1.Location = new System.Drawing.Point(128, 199); + this.directoryOrCustomSelectControl1.Name = "directoryOrCustomSelectControl1"; + this.directoryOrCustomSelectControl1.Size = new System.Drawing.Size(660, 81); + this.directoryOrCustomSelectControl1.TabIndex = 5; + // + // TEMP_TestNewControls + // + this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); + this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font; + this.ClientSize = new System.Drawing.Size(800, 450); + this.Controls.Add(this.directoryOrCustomSelectControl1); + this.Controls.Add(this.directorySelectControl1); + this.Controls.Add(this.button1); + this.Name = "TEMP_TestNewControls"; + this.Text = "TEMP_TestNewControls"; + this.Load += new System.EventHandler(this.TEMP_TestNewControls_Load); + this.ResumeLayout(false); + + } + + #endregion + private System.Windows.Forms.Button button1; + private DirectorySelectControl directorySelectControl1; + private DirectoryOrCustomSelectControl directoryOrCustomSelectControl1; + } +} \ No newline at end of file diff --git a/LibationWinForms/Dialogs/TEMP_TestNewControls.cs b/LibationWinForms/Dialogs/TEMP_TestNewControls.cs new file mode 100644 index 00000000..61890d92 --- /dev/null +++ b/LibationWinForms/Dialogs/TEMP_TestNewControls.cs @@ -0,0 +1,76 @@ +using System; +using System.Collections.Generic; +using System.ComponentModel; +using System.Data; +using System.Drawing; +using System.Linq; +using System.Text; +using System.Threading.Tasks; +using System.Windows.Forms; + +namespace LibationWinForms.Dialogs +{ + public partial class TEMP_TestNewControls : Form + { + public TEMP_TestNewControls() + { + InitializeComponent(); + } + + private void TEMP_TestNewControls_Load(object sender, EventArgs e) + { + + if (this.DesignMode) + return; + + { + var dirCtrl = this.directorySelectControl1; + dirCtrl.SetDirectoryItems(new() + { + FileManager.Configuration.KnownDirectories.AppDir, + FileManager.Configuration.KnownDirectories.MyDocs, + FileManager.Configuration.KnownDirectories.LibationFiles, + FileManager.Configuration.KnownDirectories.MyDocs, + FileManager.Configuration.KnownDirectories.None, + FileManager.Configuration.KnownDirectories.WinTemp, + FileManager.Configuration.KnownDirectories.UserProfile + } + , + FileManager.Configuration.KnownDirectories.MyDocs + ); + } + + { + var dirOrCustCtrl = this.directoryOrCustomSelectControl1; + dirOrCustCtrl.SetSearchTitle("Libation Files"); + dirOrCustCtrl.SetDirectoryItems(new() + { + FileManager.Configuration.KnownDirectories.AppDir, + FileManager.Configuration.KnownDirectories.MyDocs, + FileManager.Configuration.KnownDirectories.LibationFiles, + FileManager.Configuration.KnownDirectories.MyDocs, + FileManager.Configuration.KnownDirectories.None, + FileManager.Configuration.KnownDirectories.WinTemp, + FileManager.Configuration.KnownDirectories.UserProfile + } + , + FileManager.Configuration.KnownDirectories.MyDocs + ); + } + + + } + + private void button1_Click(object sender, EventArgs e) + { + var dirCtrl = this.directorySelectControl1; + var dirOrCustCtrl = this.directoryOrCustomSelectControl1; + + var x = dirCtrl.SelectedDirectory; + var y = dirOrCustCtrl.SelectedDirectory; + + dirCtrl.SelectDirectory(FileManager.Configuration.KnownDirectories.UserProfile); + dirOrCustCtrl.SelectDirectory(FileManager.Configuration.KnownDirectories.UserProfile); + } + } +} \ No newline at end of file diff --git a/LibationWinForms/Dialogs/TEMP_TestNewControls.resx b/LibationWinForms/Dialogs/TEMP_TestNewControls.resx new file mode 100644 index 00000000..f298a7be --- /dev/null +++ b/LibationWinForms/Dialogs/TEMP_TestNewControls.resx @@ -0,0 +1,60 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + text/microsoft-resx + + + 2.0 + + + System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + + System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089 + + \ No newline at end of file