diff --git a/Source/AppScaffolding/LibationScaffolding.cs b/Source/AppScaffolding/LibationScaffolding.cs index 21d3126c..d287190f 100644 --- a/Source/AppScaffolding/LibationScaffolding.cs +++ b/Source/AppScaffolding/LibationScaffolding.cs @@ -122,6 +122,9 @@ namespace AppScaffolding if (!config.Exists(nameof(config.ChapterFileTemplate))) config.ChapterFileTemplate = Templates.ChapterFile.DefaultTemplate; + + if (!config.Exists(nameof(config.AutoScan))) + config.AutoScan = true; } /// Initialize logging. Run after migration diff --git a/Source/AudibleUtilities/AccountsSettingsPersister.cs b/Source/AudibleUtilities/AccountsSettingsPersister.cs index f3c95d38..914d2d8a 100644 --- a/Source/AudibleUtilities/AccountsSettingsPersister.cs +++ b/Source/AudibleUtilities/AccountsSettingsPersister.cs @@ -7,6 +7,12 @@ namespace AudibleUtilities { public class AccountsSettingsPersister : JsonFilePersister { + public static event EventHandler Saving; + public static event EventHandler Saved; + + protected override void OnSaving() => Saving?.Invoke(null, null); + protected override void OnSaved() => Saved?.Invoke(null, null); + /// Alias for Target public AccountsSettings AccountsSettings => Target; diff --git a/Source/AudibleUtilities/AudibleUtilities.csproj b/Source/AudibleUtilities/AudibleUtilities.csproj index ca3ccb51..8cb628c5 100644 --- a/Source/AudibleUtilities/AudibleUtilities.csproj +++ b/Source/AudibleUtilities/AudibleUtilities.csproj @@ -5,7 +5,7 @@ - + diff --git a/Source/FileManager/FileManager.csproj b/Source/FileManager/FileManager.csproj index e3ebc9a8..da074afe 100644 --- a/Source/FileManager/FileManager.csproj +++ b/Source/FileManager/FileManager.csproj @@ -5,7 +5,7 @@ - + diff --git a/Source/LibationFileManager/Configuration.cs b/Source/LibationFileManager/Configuration.cs index ff508e95..89ab5f9f 100644 --- a/Source/LibationFileManager/Configuration.cs +++ b/Source/LibationFileManager/Configuration.cs @@ -224,6 +224,22 @@ namespace LibationFileManager set => persistentDictionary.SetNonString(nameof(DownloadEpisodes), value); } + public event EventHandler AutoScanChanged; + + [Description("Automatically run periodic scans in the background?")] + public bool AutoScan + { + get => persistentDictionary.GetNonString(nameof(AutoScan)); + set + { + if (AutoScan != value) + { + persistentDictionary.SetNonString(nameof(AutoScan), value); + AutoScanChanged?.Invoke(null, null); + } + } + } + #region templates: custom file naming [Description("How to format the folders in which files will be saved")] diff --git a/Source/LibationWinForms/Dialogs/AccountsDialog.cs b/Source/LibationWinForms/Dialogs/AccountsDialog.cs index d6948759..3da3cdbd 100644 --- a/Source/LibationWinForms/Dialogs/AccountsDialog.cs +++ b/Source/LibationWinForms/Dialogs/AccountsDialog.cs @@ -122,7 +122,6 @@ namespace LibationWinForms.Dialogs persist(persister.AccountsSettings); persister.CommitTransation(); - _parent.RefreshImportMenu(); this.DialogResult = DialogResult.OK; this.Close(); } diff --git a/Source/LibationWinForms/Form1.cs b/Source/LibationWinForms/Form1.cs index cb9df32e..0f44a48e 100644 --- a/Source/LibationWinForms/Form1.cs +++ b/Source/LibationWinForms/Form1.cs @@ -31,7 +31,6 @@ namespace LibationWinForms // independent UI updates this.Load += (_, _) => this.RestoreSizeAndLocation(Configuration.Instance); - this.Load += RefreshImportMenu; this.FormClosing += (_, _) => this.SaveSizeAndLocation(Configuration.Instance); LibraryCommands.LibrarySizeChanged += reloadGridAndUpdateBottomNumbers; LibraryCommands.BookUserDefinedItemCommitted += setBackupCounts; @@ -39,13 +38,53 @@ namespace LibationWinForms LibraryCommands.ScanBegin += LibraryCommands_ScanBegin; LibraryCommands.ScanEnd += LibraryCommands_ScanEnd; + // accounts updated + this.Load += refreshImportMenu; + AccountsSettingsPersister.Saved += refreshImportMenu; + + // start autoscanner + this.Load += startAutoScan; + AccountsSettingsPersister.Saving += accountsPreSave; + AccountsSettingsPersister.Saved += accountsPostSave; + Configuration.Instance.AutoScanChanged += startAutoScan; + + // init default/placeholder cover art var format = System.Drawing.Imaging.ImageFormat.Jpeg; PictureStorage.SetDefaultImage(PictureSize._80x80, Properties.Resources.default_cover_80x80.ToBytes(format)); PictureStorage.SetDefaultImage(PictureSize._300x300, Properties.Resources.default_cover_300x300.ToBytes(format)); PictureStorage.SetDefaultImage(PictureSize._500x500, Properties.Resources.default_cover_500x500.ToBytes(format)); } - private void Form1_Load(object sender, EventArgs e) + private List<(string AccountId, string LocaleName)> preSaveDefaultAccounts; + private List<(string AccountId, string LocaleName)> getDefaultAccounts() + { + using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); + return persister.AccountsSettings + .GetAll() + .Where(a => a.LibraryScan) + .Select(a => (a.AccountId, a.Locale.Name)) + .ToList(); + } + private void accountsPreSave(object sender = null, EventArgs e = null) + => preSaveDefaultAccounts = getDefaultAccounts(); + private void accountsPostSave(object sender = null, EventArgs e = null) + { + var postSaveDefaultAccounts = getDefaultAccounts(); + var newDefaultAccounts = postSaveDefaultAccounts.Except(preSaveDefaultAccounts).ToList(); + + if (newDefaultAccounts.Any()) + startAutoScan(); + } + + private void startAutoScan(object sender = null, EventArgs e = null) + { + if (Configuration.Instance.AutoScan) + Console.WriteLine("autoScanner.StartScan();"); + else + Console.WriteLine("autoScanner.StopScan();"); + } + + private void Form1_Load(object sender, EventArgs e) { if (this.DesignMode) return; @@ -237,7 +276,7 @@ namespace LibationWinForms #endregion #region Import menu - public void RefreshImportMenu(object _ = null, EventArgs __ = null) + private void refreshImportMenu(object _ = null, EventArgs __ = null) { using var persister = AudibleApiStorage.GetAccountsSettingsPersister(); var count = persister.AccountsSettings.Accounts.Count; @@ -489,6 +528,6 @@ namespace LibationWinForms this.scanningToolStripMenuItem.Visible = false; } - #endregion - } + #endregion + } } diff --git a/Source/LibationWinForms/LibationWinForms.csproj b/Source/LibationWinForms/LibationWinForms.csproj index d386871a..afad1680 100644 --- a/Source/LibationWinForms/LibationWinForms.csproj +++ b/Source/LibationWinForms/LibationWinForms.csproj @@ -28,7 +28,7 @@ - +