Add Locate Audiobooks function (#485)

This commit is contained in:
Mbucari 2023-02-10 09:35:21 -07:00
parent c4acd5d208
commit bba9c2ba7b
16 changed files with 530 additions and 20 deletions

View file

@ -0,0 +1,107 @@
namespace LibationWinForms.Dialogs
{
partial class LocateAudiobooksDialog
{
/// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.IContainer components = null;
/// <summary>
/// Clean up any resources being used.
/// </summary>
/// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
protected override void Dispose(bool disposing)
{
if (disposing && (components != null))
{
components.Dispose();
}
base.Dispose(disposing);
}
#region Windows Form Designer generated code
/// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
{
this.label1 = new System.Windows.Forms.Label();
this.foundAudiobooksLV = new System.Windows.Forms.ListView();
this.columnHeader1 = new System.Windows.Forms.ColumnHeader();
this.columnHeader2 = new System.Windows.Forms.ColumnHeader();
this.booksFoundLbl = new System.Windows.Forms.Label();
this.SuspendLayout();
//
// label1
//
this.label1.AutoSize = true;
this.label1.Location = new System.Drawing.Point(12, 9);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(108, 15);
this.label1.TabIndex = 1;
this.label1.Text = "Found Audiobooks";
//
// foundAudiobooksLV
//
this.foundAudiobooksLV.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
| System.Windows.Forms.AnchorStyles.Left)
| System.Windows.Forms.AnchorStyles.Right)));
this.foundAudiobooksLV.Columns.AddRange(new System.Windows.Forms.ColumnHeader[] {
this.columnHeader1,
this.columnHeader2});
this.foundAudiobooksLV.FullRowSelect = true;
this.foundAudiobooksLV.Location = new System.Drawing.Point(12, 33);
this.foundAudiobooksLV.Name = "foundAudiobooksLV";
this.foundAudiobooksLV.Size = new System.Drawing.Size(321, 261);
this.foundAudiobooksLV.TabIndex = 2;
this.foundAudiobooksLV.UseCompatibleStateImageBehavior = false;
this.foundAudiobooksLV.View = System.Windows.Forms.View.Details;
//
// columnHeader1
//
this.columnHeader1.Text = "Book ID";
this.columnHeader1.Width = 85;
//
// columnHeader2
//
this.columnHeader2.Text = "Title";
//
// booksFoundLbl
//
this.booksFoundLbl.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
this.booksFoundLbl.AutoSize = true;
this.booksFoundLbl.Location = new System.Drawing.Point(253, 9);
this.booksFoundLbl.Name = "booksFoundLbl";
this.booksFoundLbl.Size = new System.Drawing.Size(80, 15);
this.booksFoundLbl.TabIndex = 3;
this.booksFoundLbl.Text = "IDs Found: {0}";
this.booksFoundLbl.TextAlign = System.Drawing.ContentAlignment.TopRight;
//
// LocateAudiobooksDialog
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(345, 306);
this.Controls.Add(this.booksFoundLbl);
this.Controls.Add(this.foundAudiobooksLV);
this.Controls.Add(this.label1);
this.FormBorderStyle = System.Windows.Forms.FormBorderStyle.SizableToolWindow;
this.Name = "LocateAudiobooksDialog";
this.Text = "Locate Audiobooks";
this.ResumeLayout(false);
this.PerformLayout();
}
#endregion
private System.Windows.Forms.Label label1;
private System.Windows.Forms.ListView foundAudiobooksLV;
private System.Windows.Forms.ColumnHeader columnHeader1;
private System.Windows.Forms.ColumnHeader columnHeader2;
private System.Windows.Forms.Label booksFoundLbl;
}
}

View file

@ -0,0 +1,98 @@
using ApplicationServices;
using DataLayer;
using LibationFileManager;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace LibationWinForms.Dialogs
{
public partial class LocateAudiobooksDialog : Form
{
private event EventHandler<FilePathCache.CacheEntry> FileFound;
private readonly CancellationTokenSource tokenSource = new();
private readonly List<string> foundAsins = new();
private readonly string labelFormatText;
public LocateAudiobooksDialog()
{
InitializeComponent();
labelFormatText = booksFoundLbl.Text;
setFoundBookCount(0);
this.SetLibationIcon();
this.RestoreSizeAndLocation(Configuration.Instance);
Shown += LocateAudiobooks_Shown;
FileFound += LocateAudiobooks_FileFound;
FormClosing += LocateAudiobooks_FormClosing;
}
private void setFoundBookCount(int count)
=> booksFoundLbl.Text = string.Format(labelFormatText, count);
private void LocateAudiobooks_FileFound(object sender, FilePathCache.CacheEntry e)
{
foundAudiobooksLV.Items
.Add(new ListViewItem(new string[] { $"[{e.Id}]", Path.GetFileName(e.Path) }))
.EnsureVisible();
foundAudiobooksLV.AutoResizeColumn(1, ColumnHeaderAutoResizeStyle.ColumnContent);
if (!foundAsins.Any(asin => asin == e.Id))
{
foundAsins.Add(e.Id);
setFoundBookCount(foundAsins.Count);
}
}
private void LocateAudiobooks_FormClosing(object sender, FormClosingEventArgs e)
{
tokenSource.Cancel();
this.SaveSizeAndLocation(Configuration.Instance);
}
private async void LocateAudiobooks_Shown(object sender, EventArgs e)
{
var fbd = new FolderBrowserDialog
{
Description = "Select the folder to search for audiobooks",
UseDescriptionForTitle = true,
InitialDirectory = Configuration.Instance.Books
};
if (fbd.ShowDialog() != DialogResult.OK || !Directory.Exists(fbd.SelectedPath))
{
Close();
return;
}
using var context = DbContexts.GetContext();
await foreach (var book in AudioFileLocator.FindAudiobooks(fbd.SelectedPath, tokenSource.Token))
{
try
{
FilePathCache.Insert(book);
var lb = context.GetLibraryBook_Flat_NoTracking(book.Id);
if (lb.Book.UserDefinedItem.BookStatus is not LiberatedStatus.Liberated)
await Task.Run(() => lb.UpdateBookStatus(LiberatedStatus.Liberated));
this.Invoke(FileFound, this, book);
}
catch(Exception ex)
{
Serilog.Log.Error(ex, "Error adding found audiobook file to Libation. {@audioFile}", book);
}
}
MessageBox.Show(this, $"Libation has found {foundAsins.Count} unique audiobooks and added them to its database. ", $"Found {foundAsins.Count} Audiobooks");
Close();
}
}
}

View file

@ -0,0 +1,60 @@
<root>
<xsd:schema id="root" xmlns="" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xsd:import namespace="http://www.w3.org/XML/1998/namespace" />
<xsd:element name="root" msdata:IsDataSet="true">
<xsd:complexType>
<xsd:choice maxOccurs="unbounded">
<xsd:element name="metadata">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" />
</xsd:sequence>
<xsd:attribute name="name" use="required" type="xsd:string" />
<xsd:attribute name="type" type="xsd:string" />
<xsd:attribute name="mimetype" type="xsd:string" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="assembly">
<xsd:complexType>
<xsd:attribute name="alias" type="xsd:string" />
<xsd:attribute name="name" type="xsd:string" />
</xsd:complexType>
</xsd:element>
<xsd:element name="data">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
<xsd:element name="comment" type="xsd:string" minOccurs="0" msdata:Ordinal="2" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" msdata:Ordinal="1" />
<xsd:attribute name="type" type="xsd:string" msdata:Ordinal="3" />
<xsd:attribute name="mimetype" type="xsd:string" msdata:Ordinal="4" />
<xsd:attribute ref="xml:space" />
</xsd:complexType>
</xsd:element>
<xsd:element name="resheader">
<xsd:complexType>
<xsd:sequence>
<xsd:element name="value" type="xsd:string" minOccurs="0" msdata:Ordinal="1" />
</xsd:sequence>
<xsd:attribute name="name" type="xsd:string" use="required" />
</xsd:complexType>
</xsd:element>
</xsd:choice>
</xsd:complexType>
</xsd:element>
</xsd:schema>
<resheader name="resmimetype">
<value>text/microsoft-resx</value>
</resheader>
<resheader name="version">
<value>2.0</value>
</resheader>
<resheader name="reader">
<value>System.Resources.ResXResourceReader, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
<resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader>
</root>

View file

@ -60,8 +60,10 @@
this.setBookDownloadedManualToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.setPdfDownloadedManualToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.setDownloadedAutoToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator3 = new System.Windows.Forms.ToolStripSeparator();
this.removeToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.locateAudiobooksToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.settingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.accountsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.basicSettingsToolStripMenuItem = new System.Windows.Forms.ToolStripMenuItem();
this.toolStripSeparator2 = new System.Windows.Forms.ToolStripSeparator();
@ -149,7 +151,9 @@
this.scanLibraryToolStripMenuItem,
this.scanLibraryOfAllAccountsToolStripMenuItem,
this.scanLibraryOfSomeAccountsToolStripMenuItem,
this.removeLibraryBooksToolStripMenuItem});
this.removeLibraryBooksToolStripMenuItem,
this.toolStripSeparator3,
this.locateAudiobooksToolStripMenuItem});
this.importToolStripMenuItem.Name = "importToolStripMenuItem";
this.importToolStripMenuItem.Size = new System.Drawing.Size(55, 20);
this.importToolStripMenuItem.Text = "&Import";
@ -560,10 +564,22 @@
this.processBookQueue1.Name = "processBookQueue1";
this.processBookQueue1.Size = new System.Drawing.Size(430, 640);
this.processBookQueue1.TabIndex = 0;
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
//
// locateAudiobooksToolStripMenuItem
//
this.locateAudiobooksToolStripMenuItem.Name = "locateAudiobooksToolStripMenuItem";
this.locateAudiobooksToolStripMenuItem.Size = new System.Drawing.Size(247, 22);
this.locateAudiobooksToolStripMenuItem.Text = "Locate Audiobooks";
this.locateAudiobooksToolStripMenuItem.Click += new System.EventHandler(this.locateAudiobooksToolStripMenuItem_Click);
//
// toolStripSeparator3
//
this.toolStripSeparator3.Name = "toolStripSeparator3";
this.toolStripSeparator3.Size = new System.Drawing.Size(244, 6);
//
// Form1
//
this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F);
this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
this.ClientSize = new System.Drawing.Size(1463, 640);
this.Controls.Add(this.splitContainer1);
@ -630,6 +646,8 @@
private System.Windows.Forms.ToolStripMenuItem setBookDownloadedManualToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem setDownloadedAutoToolStripMenuItem;
private System.Windows.Forms.ToolStripMenuItem removeToolStripMenuItem;
private System.Windows.Forms.ToolStripSeparator toolStripSeparator3;
private System.Windows.Forms.ToolStripMenuItem locateAudiobooksToolStripMenuItem;
private LibationWinForms.FormattableToolStripMenuItem liberateVisibleToolStripMenuItem_LiberateMenu;
private System.Windows.Forms.SplitContainer splitContainer1;
private LibationWinForms.ProcessQueue.ProcessQueueControl processBookQueue1;

View file

@ -89,5 +89,10 @@ namespace LibationWinForms
ex);
}
}
private void locateAudiobooksToolStripMenuItem_Click(object sender, EventArgs e)
{
new LocateAudiobooksDialog().ShowDialog();
}
}
}

View file

@ -7,6 +7,7 @@ using ApplicationServices;
using Dinah.Core;
using Dinah.Core.Threading;
using LibationFileManager;
using LibationWinForms.Dialogs;
namespace LibationWinForms
{