mirror of
https://github.com/rmcrackan/Libation.git
synced 2026-09-13 06:07:30 -04:00
Ported from #1949. The reporter's log paused auto-scan on a second account that had never been logged in, while the dialog blamed an expired session and named no account, so there was nothing to act on. AccountCredentialStatus tells a never-registered account apart from one holding an expired access token, by looking for a refresh token to renew from. AutoScanRunner now hands the AuthenticationRequiredException to the notification so the prompt can name the account, which means digging that exception back out of the wrappers the scan adds on the way up. Same distinction in the log line and in the exception message ApiExtended throws when interactive login is unavailable, which is what the CLI and Docker users see. Co-authored-by: rmcrackan <rmcrackan@gmail.com>
107 lines
3.4 KiB
C#
107 lines
3.4 KiB
C#
using ApplicationServices;
|
|
using AudibleUtilities;
|
|
using Dinah.Core;
|
|
using LibationFileManager;
|
|
using LibationUiBase;
|
|
using System;
|
|
using System.Collections.Generic;
|
|
using System.Linq;
|
|
using System.Threading.Tasks;
|
|
using System.Windows.Forms;
|
|
|
|
namespace LibationWinForms;
|
|
|
|
// This is for the auto-scanner. It is unrelated to manual scanning/import
|
|
public partial class Form1
|
|
{
|
|
private InterruptableTimer? autoScanTimer;
|
|
private AutoScanRunner? autoScanRunner;
|
|
|
|
private void Configure_ScanAuto()
|
|
{
|
|
// creating InterruptableTimer inside 'Configure_' is a break from the pattern. As long as no one else needs to access or subscribe to it, this is ok
|
|
|
|
autoScanTimer = new InterruptableTimer(TimeSpan.FromMinutes(5));
|
|
autoScanRunner = new AutoScanRunner(
|
|
isAutoScanEnabled: () => Configuration.Instance.AutoScan,
|
|
pauseTimer: () => autoScanTimer?.Stop(),
|
|
resumeTimer: () => autoScanTimer?.PerformNow(),
|
|
notifyAuthRequired: notifyAutoScanAuthRequiredAsync);
|
|
|
|
autoScanTimer.Elapsed += async (_, __) => await autoScanRunner.RunAsync();
|
|
|
|
Load += updateAutoScanLibraryToolStripMenuItem;
|
|
Shown += startAutoScan;
|
|
|
|
AccountsSettingsPersister.Saving += accountsPreSave;
|
|
AccountsSettingsPersister.Saved += accountsPostSave;
|
|
|
|
Configuration.Instance.PropertyChanged += Configuration_PropertyChanged;
|
|
}
|
|
|
|
private void notifyAutoScanAuthRequired(AuthenticationRequiredException ex)
|
|
{
|
|
MessageBox.Show(
|
|
this,
|
|
AutoScanAuthPrompt.FormatBody(ex),
|
|
AutoScanAuthPrompt.Caption,
|
|
MessageBoxButtons.OK,
|
|
MessageBoxIcon.Warning);
|
|
}
|
|
|
|
private Task notifyAutoScanAuthRequiredAsync(AuthenticationRequiredException ex)
|
|
{
|
|
if (InvokeRequired)
|
|
{
|
|
Invoke(() => notifyAutoScanAuthRequired(ex));
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
notifyAutoScanAuthRequired(ex);
|
|
return Task.CompletedTask;
|
|
}
|
|
|
|
[PropertyChangeFilter(nameof(Configuration.AutoScan))]
|
|
private void Configuration_PropertyChanged(object? sender, PropertyChangedEventArgsEx e)
|
|
{
|
|
// when autoscan setting is changed, update menu checkbox and run autoscan
|
|
updateAutoScanLibraryToolStripMenuItem(sender, e);
|
|
startAutoScan(sender, 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))
|
|
.OfType<(string, string)>()
|
|
.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)
|
|
{
|
|
autoScanRunner?.OnAutoScanSettingChanged();
|
|
if (Configuration.Instance.AutoScan)
|
|
autoScanTimer?.PerformNow();
|
|
else
|
|
autoScanTimer?.Stop();
|
|
}
|
|
|
|
private void updateAutoScanLibraryToolStripMenuItem(object? sender, EventArgs e) => autoScanLibraryToolStripMenuItem.Checked = Configuration.Instance.AutoScan;
|
|
|
|
private void autoScanLibraryToolStripMenuItem_Click(object? sender, EventArgs e) => Configuration.Instance.AutoScan = !autoScanLibraryToolStripMenuItem.Checked;
|
|
}
|