From 7f08da96bb10ca1196e713c6e88efd9fce892f3a Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 14 May 2022 11:20:19 -0600 Subject: [PATCH] Documentation and organization --- ...ner.cs => ProcessQueueControl.Designer.cs} | 30 +- ...essBookQueue.cs => ProcessQueueControl.cs} | 291 +++++++++++------- ...ookQueue.resx => ProcessQueueControl.resx} | 3 + 3 files changed, 213 insertions(+), 111 deletions(-) rename Source/LibationWinForms/ProcessQueue/{ProcessBookQueue.Designer.cs => ProcessQueueControl.Designer.cs} (91%) rename Source/LibationWinForms/ProcessQueue/{ProcessBookQueue.cs => ProcessQueueControl.cs} (67%) rename Source/LibationWinForms/ProcessQueue/{ProcessBookQueue.resx => ProcessQueueControl.resx} (99%) diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBookQueue.Designer.cs b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.Designer.cs similarity index 91% rename from Source/LibationWinForms/ProcessQueue/ProcessBookQueue.Designer.cs rename to Source/LibationWinForms/ProcessQueue/ProcessQueueControl.Designer.cs index aed3aa67..0b5b003b 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBookQueue.Designer.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.Designer.cs @@ -1,6 +1,6 @@ namespace LibationWinForms.ProcessQueue { - partial class ProcessBookQueue + partial class ProcessQueueControl { /// /// Required designer variable. @@ -28,13 +28,15 @@ /// private void InitializeComponent() { - System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProcessBookQueue)); + this.components = new System.ComponentModel.Container(); + System.ComponentModel.ComponentResourceManager resources = new System.ComponentModel.ComponentResourceManager(typeof(ProcessQueueControl)); this.statusStrip1 = new System.Windows.Forms.StatusStrip(); this.toolStripProgressBar1 = new System.Windows.Forms.ToolStripProgressBar(); this.queueNumberLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.completedNumberLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.errorNumberLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.toolStripStatusLabel1 = new System.Windows.Forms.ToolStripStatusLabel(); + this.runningTimeLbl = new System.Windows.Forms.ToolStripStatusLabel(); this.tabControl1 = new System.Windows.Forms.TabControl(); this.tabPage1 = new System.Windows.Forms.TabPage(); this.panel3 = new System.Windows.Forms.Panel(); @@ -47,6 +49,7 @@ this.panel2 = new System.Windows.Forms.Panel(); this.clearLogBtn = new System.Windows.Forms.Button(); this.logMeTbox = new System.Windows.Forms.TextBox(); + this.counterTimer = new System.Windows.Forms.Timer(this.components); this.statusStrip1.SuspendLayout(); this.tabControl1.SuspendLayout(); this.tabPage1.SuspendLayout(); @@ -63,12 +66,13 @@ this.queueNumberLbl, this.completedNumberLbl, this.errorNumberLbl, - this.toolStripStatusLabel1}); + this.toolStripStatusLabel1, + this.runningTimeLbl}); this.statusStrip1.Location = new System.Drawing.Point(0, 483); this.statusStrip1.Name = "statusStrip1"; this.statusStrip1.Size = new System.Drawing.Size(404, 25); this.statusStrip1.TabIndex = 1; - this.statusStrip1.Text = "statusStrip1"; + this.statusStrip1.Text = "baseStatusStrip"; // // toolStripProgressBar1 // @@ -99,9 +103,16 @@ // toolStripStatusLabel1 // this.toolStripStatusLabel1.Name = "toolStripStatusLabel1"; - this.toolStripStatusLabel1.Size = new System.Drawing.Size(118, 20); + this.toolStripStatusLabel1.Size = new System.Drawing.Size(77, 20); this.toolStripStatusLabel1.Spring = true; // + // runningTimeLbl + // + this.runningTimeLbl.AutoSize = false; + this.runningTimeLbl.Name = "runningTimeLbl"; + this.runningTimeLbl.Size = new System.Drawing.Size(41, 20); + this.runningTimeLbl.Text = "[TIME]"; + // // tabControl1 // this.tabControl1.Controls.Add(this.tabPage1); @@ -167,7 +178,7 @@ this.btnCleanFinished.TabIndex = 3; this.btnCleanFinished.Text = "Clear Finished"; this.btnCleanFinished.UseVisualStyleBackColor = true; - this.btnCleanFinished.Click += new System.EventHandler(this.btnCleanFinished_Click); + this.btnCleanFinished.Click += new System.EventHandler(this.btnClearFinished_Click); // // cancelAllBtn // @@ -236,6 +247,11 @@ this.logMeTbox.Size = new System.Drawing.Size(390, 449); this.logMeTbox.TabIndex = 0; // + // counterTimer + // + this.counterTimer.Interval = 950; + this.counterTimer.Tick += new System.EventHandler(this.CounterTimer_Tick); + // // ProcessBookQueue // this.AutoScaleDimensions = new System.Drawing.SizeF(7F, 15F); @@ -277,5 +293,7 @@ private System.Windows.Forms.ToolStripStatusLabel errorNumberLbl; private System.Windows.Forms.Panel panel3; private System.Windows.Forms.Panel panel4; + private System.Windows.Forms.ToolStripStatusLabel runningTimeLbl; + private System.Windows.Forms.Timer counterTimer; } } diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBookQueue.cs b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs similarity index 67% rename from Source/LibationWinForms/ProcessQueue/ProcessBookQueue.cs rename to Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs index 29ac5ce0..cfa89137 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBookQueue.cs +++ b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.cs @@ -8,7 +8,7 @@ using System.Windows.Forms; namespace LibationWinForms.ProcessQueue { - internal partial class ProcessBookQueue : UserControl, ILogForm + internal partial class ProcessQueueControl : UserControl, ILogForm { private TrackedQueue Queue = new(); private readonly LogMe Logger; @@ -45,15 +45,12 @@ namespace LibationWinForms.ProcessQueue public ToolStripButton popoutBtn = new(); - private int FirstVisible = 0; - private int NumVisible = 0; - private IReadOnlyList Panels; - - public ProcessBookQueue() + public ProcessQueueControl() { InitializeComponent(); Logger = LogMe.RegisterForm(this); + runningTimeLbl.Text = string.Empty; popoutBtn.DisplayStyle = ToolStripItemDisplayStyle.Text; popoutBtn.Name = "popoutBtn"; popoutBtn.Text = "Pop Out"; @@ -74,6 +71,85 @@ namespace LibationWinForms.ProcessQueue CompletedCount = 0; } + public void AddDownloadDecrypt(IEnumerable entries) + { + foreach (var entry in entries) + AddDownloadDecrypt(entry); + } + + public void AddConvertMp3(IEnumerable entries) + { + foreach (var entry in entries) + AddConvertMp3(entry); + } + + public void AddDownloadDecrypt(GridEntry gridEntry) + { + if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == gridEntry.AudibleProductId)) + return; + + ProcessBook pbook = new(gridEntry.LibraryBook, gridEntry.Cover, Logger); + pbook.DataAvailable += Pbook_DataAvailable; + + pbook.AddDownloadDecryptBook(); + pbook.AddDownloadPdf(); + + Queue.Enqueue(pbook); + + if (!Running) + { + QueueRunner = QueueLoop(); + } + } + + public void AddConvertMp3(GridEntry gridEntry) + { + if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == gridEntry.AudibleProductId)) + return; + + ProcessBook pbook = new(gridEntry.LibraryBook, gridEntry.Cover, Logger); + pbook.DataAvailable += Pbook_DataAvailable; + + pbook.AddConvertToMp3(); + + Queue.Enqueue(pbook); + + if (!Running) + { + QueueRunner = QueueLoop(); + } + } + + DateTime StartintTime; + private async Task QueueLoop() + { + StartintTime = DateTime.Now; + counterTimer.Start(); + + while (Queue.MoveNext()) + { + var nextBook = Queue.Current; + + var result = await nextBook.ProcessOneAsync(); + + if (result == ProcessBookResult.FailedRetry) + Queue.Enqueue(nextBook); + else if (result == ProcessBookResult.FailedAbort) + return; + } + + Queue_CompletedCountChanged(this, 0); + counterTimer.Stop(); + } + + public void WriteLine(string text) + { + if (!IsDisposed) + logMeTbox.UIThreadAsync(() => logMeTbox.AppendText($"{DateTime.Now} {text}{Environment.NewLine}")); + } + + #region Control event handlers + private void Queue_CompletedCountChanged(object sender, int e) { int errCount = Queue.Completed.Count(p => p.Result is ProcessBookResult.FailedAbort or ProcessBookResult.FailedSkip or ProcessBookResult.ValidationFail); @@ -94,45 +170,70 @@ namespace LibationWinForms.ProcessQueue toolStripProgressBar1.Maximum = Queue.Count; toolStripProgressBar1.Value = Queue.Completed.Count; } - private void VirtualFlowControl2_ButtonClicked(int itemIndex, string buttonName, ProcessBookControl panelClicked) + + private void cancelAllBtn_Click(object sender, EventArgs e) { - ProcessBook item = Queue[itemIndex]; - if (buttonName == "cancelBtn") - { - item.Cancel(); - Queue.RemoveQueued(item); - virtualFlowControl2.VirtualControlCount = Queue.Count; - UpdateControl(itemIndex); - } - else if (buttonName == "moveFirstBtn") - { - Queue.MoveQueuePosition(item, QueuePosition.Fisrt); - UpdateAllControls(); - } - else if (buttonName == "moveUpBtn") - { - Queue.MoveQueuePosition(item, QueuePosition.OneUp); - UpdateControl(itemIndex - 1); - UpdateControl(itemIndex); - } - else if (buttonName == "moveDownBtn") - { - Queue.MoveQueuePosition(item, QueuePosition.OneDown); - UpdateControl(itemIndex + 1); - UpdateControl(itemIndex); - } - else if (buttonName == "moveLastBtn") - { - Queue.MoveQueuePosition(item, QueuePosition.Last); - UpdateAllControls(); - } + Queue.ClearQueue(); + Queue.Current?.Cancel(); + virtualFlowControl2.VirtualControlCount = Queue.Count; + UpdateAllControls(); } + private void btnClearFinished_Click(object sender, EventArgs e) + { + Queue.ClearCompleted(); + virtualFlowControl2.VirtualControlCount = Queue.Count; + UpdateAllControls(); + + if (!Running) + runningTimeLbl.Text = string.Empty; + } + + private void CounterTimer_Tick(object sender, EventArgs e) + { + string timeToStr(TimeSpan time) + { + string minsSecs = $"{time:mm\\:ss}"; + if (time.TotalHours >= 1) + return $"{time.TotalHours:F0}:{minsSecs}"; + return minsSecs; + } + + if (Running) + runningTimeLbl.Text = timeToStr(DateTime.Now - StartintTime); + } + + private void clearLogBtn_Click(object sender, EventArgs e) + { + logMeTbox.Clear(); + } + + #endregion + + #region View-Model update event handling + + /// + /// Index of the first visible in the + /// + private int FirstVisible = 0; + /// + /// Number of visible in the + /// + private int NumVisible = 0; + /// + /// Controls displaying the state, starting with + /// + private IReadOnlyList Panels; + + /// + /// Updates the display of a single at within + /// + /// index of the within the private void UpdateControl(int queueIndex) { int i = queueIndex - FirstVisible; - if (i < 0 || i > NumVisible) return; + if (i > NumVisible || i < 0) return; var proc = Queue[queueIndex]; @@ -163,6 +264,49 @@ namespace LibationWinForms.ProcessQueue UpdateControl(FirstVisible + i); } + + /// + /// View notified the model that a botton was clicked + /// + /// index of the within + /// The clicked control to update + private void VirtualFlowControl2_ButtonClicked(int queueIndex, string buttonName, ProcessBookControl panelClicked) + { + ProcessBook item = Queue[queueIndex]; + if (buttonName == "cancelBtn") + { + item.Cancel(); + Queue.RemoveQueued(item); + virtualFlowControl2.VirtualControlCount = Queue.Count; + UpdateControl(queueIndex); + } + else if (buttonName == "moveFirstBtn") + { + Queue.MoveQueuePosition(item, QueuePosition.Fisrt); + UpdateAllControls(); + } + else if (buttonName == "moveUpBtn") + { + Queue.MoveQueuePosition(item, QueuePosition.OneUp); + UpdateControl(queueIndex - 1); + UpdateControl(queueIndex); + } + else if (buttonName == "moveDownBtn") + { + Queue.MoveQueuePosition(item, QueuePosition.OneDown); + UpdateControl(queueIndex + 1); + UpdateControl(queueIndex); + } + else if (buttonName == "moveLastBtn") + { + Queue.MoveQueuePosition(item, QueuePosition.Last); + UpdateAllControls(); + } + } + + /// + /// View needs updating + /// private void VirtualFlowControl1_RequestData(int firstIndex, int numVisible, IReadOnlyList panelsToFill) { FirstVisible = firstIndex; @@ -171,78 +315,15 @@ namespace LibationWinForms.ProcessQueue UpdateAllControls(); } - public void AddDownloadDecrypt(IEnumerable entries) - { - foreach (var entry in entries) - AddDownloadDecrypt(entry); - } - - public void AddDownloadDecrypt(GridEntry gridEntry) - { - if (Queue.Any(b => b?.LibraryBook?.Book?.AudibleProductId == gridEntry.AudibleProductId)) - return; - - ProcessBook pbook = new(gridEntry.LibraryBook, gridEntry.Cover, Logger); - pbook.DataAvailable += Pbook_DataAvailable; - - pbook.AddDownloadDecryptBook(); - pbook.AddDownloadPdf(); - - Queue.Enqueue(pbook); - - if (!Running) - { - QueueRunner = QueueLoop(); - } - } - + /// + /// Model updates the view + /// private void Pbook_DataAvailable(object sender, EventArgs e) { int index = Queue.IndexOf((ProcessBook)sender); UpdateControl(index); } - private async Task QueueLoop() - { - while (Queue.MoveNext()) - { - var nextBook = Queue.Current; - - var result = await nextBook.ProcessOneAsync(); - - if (result == ProcessBookResult.FailedRetry) - Queue.Enqueue(nextBook); - else if (result == ProcessBookResult.FailedAbort) - return; - } - - Queue_CompletedCountChanged(this, 0); - } - - private void cancelAllBtn_Click(object sender, EventArgs e) - { - Queue.ClearQueue(); - Queue.Current?.Cancel(); - virtualFlowControl2.VirtualControlCount = Queue.Count; - UpdateAllControls(); - } - - private void btnCleanFinished_Click(object sender, EventArgs e) - { - Queue.ClearCompleted(); - virtualFlowControl2.VirtualControlCount = Queue.Count; - UpdateAllControls(); - } - - private void clearLogBtn_Click(object sender, EventArgs e) - { - logMeTbox.Clear(); - } - - public void WriteLine(string text) - { - if (!IsDisposed) - logMeTbox.UIThreadAsync(() => logMeTbox.AppendText($"{DateTime.Now} {text}{Environment.NewLine}")); - } + #endregion } } diff --git a/Source/LibationWinForms/ProcessQueue/ProcessBookQueue.resx b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.resx similarity index 99% rename from Source/LibationWinForms/ProcessQueue/ProcessBookQueue.resx rename to Source/LibationWinForms/ProcessQueue/ProcessQueueControl.resx index 445745d5..15c031bb 100644 --- a/Source/LibationWinForms/ProcessQueue/ProcessBookQueue.resx +++ b/Source/LibationWinForms/ProcessQueue/ProcessQueueControl.resx @@ -630,4 +630,7 @@ w1EmAAAAAElFTkSuQmCC + + 133, 17 + \ No newline at end of file