Files
Libation/Source/LibationWinForms/Dialogs/FindBetterQualityBooksDialog.cs
William Tanksley Jr 3e40e0f268 feat: first-class account re-registration lifecycle with automatic throttling recovery (#2126) (#2145)
* feat(domain): add RequiresReRegistration and ThrottledDeviceRecovery policy

- Add Account.RequiresReRegistration property and HasObsoleteDeviceRegistration predicate
- Update AccountCredentialStatus to treat RequiresReRegistration as missing credentials
- Introduce ThrottledDeviceRecovery service to detect throttling on obsolete profiles and mark accounts for re-registration
- Add unit tests for Account, AccountCredentialStatus, and ThrottledDeviceRecovery

* feat(core): enforce interactive re-registration across API and download queue

- Intercept RequiresReRegistration in ApiExtended, invalidate tokens during login, and reset flag upon success
- Add LicenseDeniedThrottledRecovered queue status and user message
- Update ProcessBookViewModel, ProcessQueueViewModel, and _ProcessableOptionsBase to handle throttled recovery during liberation

* feat(ui): add Widevine re-registration prompts and CLI status reporting

- Prompt to mark accounts for re-registration when Widevine is enabled in Avalonia and WinForms
- Guard settings toggle against passive navigation/loading events and revert cleanly on cancel
- Show 're-login needed' in 'list-accounts' and automatically re-register in 'login-external'
- Add CLI tests for re-registration detection and flow in LoginExternalOptionsTests

* docs: document automatic registration recovery and rate limiting guidance

- Clarify distinction between device-registration throttling and volume rate limits
- Document automatic settings migration and re-registration flow across GUI and CLI
- Advise using Daily Download Limit when bulk re-downloading after scanning for higher quality audio
- Remove obsolete manual account deletion and JSON editing instructions

* feat(ui): automatically enable Widevine and re-register accounts when upgrading book quality

When upgrading audio quality via 'Scan Audible for Higher Quality Audio',
marking books for re-download previously left Widevine disabled and accounts
under legacy registrations, causing subsequent downloads to re-acquire the
existing lower-quality AAC-LC stream.

- Update FindBetterQualityBooksViewModel.MarkBooksAsync() to return a
  MarkBooksResult and automatically enable Widevine and Request xHE-AAC
  when higher-quality streams are selected.
- Thunk accounts to CurrentAndroid and mark them for re-registration via
  ThrottledDeviceRecovery so that Widevine-protected and higher-quality
  codecs can be licensed without manual troubleshooting.
- Alert user in Avalonia and WinForms dialogs when settings and
  registrations have been automatically upgraded.
- Fix broken sentence and note daily download limits and rate limiting in
  FindBetterQualityBooksViewModel.InitialMessage.
- Add unit tests in FindBetterQualityBooksViewModelTests covering clean and
  upgraded re-registration paths.

* feat(accounts): add 'Signed in?' checkbox to mark accounts for re-registration on save

* docs & fix(recovery): modernize license recovery guidance and document 'Signed in?'

* feat(recovery): automatically enable Widevine and thunk account when ADRM fails (#2083)

- Detects missing ADRM asset (Sable 000307 acr:null) and missing AAX fallback codecs in titles that only deliver via Widevine.
- Instead of merely advising the user to change settings manually, automatically applies recovery via ThrottledDeviceRecovery.ApplyRecovery (sets UseWidevine = true, CurrentAndroid, and marks the account for re-registration).
- Modernizes user-facing dialogs, process queue status, and CLI feedback to explain that Widevine DRM was enabled and a fresh login is required.
2026-11-12 09:54:23 -07:00

186 lines
5.6 KiB
C#

using ApplicationServices;
using LibationUiBase;
using System;
using System.Data;
using System.Linq;
using System.Reflection;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LibationWinForms.Dialogs;
public partial class FindBetterQualityBooksDialog : Form
{
private FindBetterQualityBooksViewModel VM { get; }
private Task? scanTask;
public FindBetterQualityBooksDialog()
{
InitializeComponent();
dataGridView1.EnableHeadersVisualStyles = !Application.IsDarkModeEnabled;
lblScanCount.Visible = btnMarkBooks.Visible = false;
DataContext = VM = new FindBetterQualityBooksViewModel();
VM.PropertyChanged += VM_PropertyChanged;
VM.BookScanned += VM_BookScanned;
cboxUseWidevine.Text = FindBetterQualityBooksViewModel.UseWidevineSboxText;
cboxUseWidevine.DataBindings.Add(new Binding(nameof(CheckBox.Checked), VM, nameof(FindBetterQualityBooksViewModel.ScanWidevine)));
btnScan.DataBindings.Add(new Binding(nameof(Button.Text), VM, nameof(FindBetterQualityBooksViewModel.ScanButtonText)));
btnScan.Enabled = false;
this.RestoreSizeAndLocation(LibationFileManager.Configuration.Instance);
this.SetLibationIcon();
Shown += Shown_LoadLibrary;
Shown += Shown_ShowInitialMessage;
FormClosing += FindBetterQualityBooksDialog_FormClosing;
SetDoubleBuffer(dataGridView1, true);
}
static void SetDoubleBuffer(Control control, bool DoubleBuffered)
{
typeof(Control).InvokeMember("DoubleBuffered", BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.SetProperty, null, control, [DoubleBuffered]);
}
private void Shown_ShowInitialMessage(object? sender, EventArgs e)
{
if (!VM.ShowFindBetterQualityBooksHelp)
return;
var result = MessageBox.Show(this, FindBetterQualityBooksViewModel.InitialMessage, Text, MessageBoxButtons.YesNo, MessageBoxIcon.Information, MessageBoxDefaultButton.Button1);
if (result == DialogResult.No)
{
VM.ShowFindBetterQualityBooksHelp = false;
}
}
private async void Shown_LoadLibrary(object? sender, EventArgs e)
{
var library = await Task.Run(() => DbContexts.GetLibrary_Flat_NoTracking());
var list = library.Where(FindBetterQualityBooksViewModel.ShouldScan).Select(lb => new BookDataViewModel(lb)).ToList();
VM.Books = new SortBindingList<BookDataViewModel>(list);
Invoke(() =>
{
bookDataViewModelBindingSource.DataSource = VM.Books;
foreach (DataGridViewRow r in dataGridView1.Rows)
{
//Force creation of DefaultCellStyle to speed up later coloring
//_ = r.DefaultCellStyle;
}
btnScan.Enabled = true;
dataGridView1.CellFormatting += dataGridView1_CellFormatting;
});
}
private void VM_BookScanned(object? sender, BookDataViewModel e)
{
Invoke(() => dataGridView1.CurrentCell = dataGridView1.Rows
.Cast<DataGridViewRow>()
.FirstOrDefault(r => r.DataBoundItem == e)?
.Cells[foundFileDataGridViewTextBoxColumn.Index]);
}
private void VM_PropertyChanged(object? sender, System.ComponentModel.PropertyChangedEventArgs e)
{
switch (e.PropertyName)
{
case nameof(FindBetterQualityBooksViewModel.IsScanning):
btnScan.Enabled = true;
break;
case nameof(FindBetterQualityBooksViewModel.ScanCount):
lblScanCount.Visible = !string.IsNullOrEmpty(VM.ScanCount);
lblScanCount.Text = VM.ScanCount;
break;
case nameof(FindBetterQualityBooksViewModel.MarkBooksButtonText):
btnMarkBooks.Visible = !string.IsNullOrEmpty(VM.MarkBooksButtonText);
btnMarkBooks.Text = VM.MarkBooksButtonText;
break;
}
}
private async void FindBetterQualityBooksDialog_FormClosing(object? sender, FormClosingEventArgs e)
{
if (scanTask is not null)
{
await scanTask;
scanTask = null;
//give the UI a moment to update after cancelling the first close
await Task.Delay(100);
Invoke(Close);
}
}
protected override void OnFormClosing(FormClosingEventArgs e)
{
if (scanTask is not null)
{
this.SaveSizeAndLocation(LibationFileManager.Configuration.Instance);
e.Cancel = true;
VM.StopScan();
}
base.OnFormClosing(e);
}
private void btnScan_Click(object sender, EventArgs e)
{
btnScan.Enabled = false;
scanTask = Task.Run(async () =>
{
try
{
if (VM.IsScanning)
VM.StopScan();
else
await VM.ScanAsync();
}
catch (Exception ex)
{
Serilog.Log.Error(ex, "Failed to scan for better quality books");
MessageBox.Show(this, "An error occurred while scanning for better quality books. Please see the logs for more information.", "Error Scanning Books", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Invoke(() =>
{
VM.IsScanning = false;
btnScan.Enabled = true;
});
}
});
}
private async void btnMarkBooks_Click(object sender, EventArgs e)
{
btnMarkBooks.Enabled = false;
try
{
var result = await VM.MarkBooksAsync();
if (result.GetSummaryMessage() is string message)
{
MessageBox.Show(this, message, "Books Marked for Download", MessageBoxButtons.OK, MessageBoxIcon.Information);
}
}
catch (Exception ex)
{
Serilog.Log.Error(ex, "Failed to mark books as Download Pending");
MessageBox.Show(this, "An error occurred while marking books as Download Pending. Please see the logs for more information.", "Error Marking Books", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
Invoke(() => btnMarkBooks.Enabled = true);
}
}
private void dataGridView1_CellFormatting(object? sender, DataGridViewCellFormattingEventArgs e)
{
if (e.RowIndex < 0 || e.RowIndex >= dataGridView1.Rows.Count)
return;
var row = dataGridView1.Rows[e.RowIndex];
if (row.DataBoundItem is BookDataViewModel bvm)
{
row.DefaultCellStyle.BackColor = bvm.ScanStatus.GetColor();
}
}
}