From f5089e7e298f30093e93234a0cef033213e782a5 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 14:53:12 -0600 Subject: [PATCH 01/12] Use local rowIndex instead of DataGridViewCell.RowIndex --- LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs b/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs index f702d3fc..e2c247ae 100644 --- a/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs +++ b/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs @@ -22,9 +22,9 @@ namespace LibationWinForms var foreColor = tagsString?.Contains("hidden") == true ? HiddenForeColor : DataGridView.DefaultCellStyle.ForeColor; - if (DataGridView.Rows[RowIndex].DefaultCellStyle.ForeColor != foreColor) + if (DataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor != foreColor) { - DataGridView.Rows[RowIndex].DefaultCellStyle.ForeColor = foreColor; + DataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = foreColor; } if (tagsString.Length == 0) From 8386da5ec6f1bb0082825deaaf40e8fb5780e2ce Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 14:56:52 -0600 Subject: [PATCH 02/12] Make gridview update the row after details changed. --- LibationWinForms/GridEntry.cs | 1 + LibationWinForms/ProductsGrid.cs | 9 +++++---- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/LibationWinForms/GridEntry.cs b/LibationWinForms/GridEntry.cs index 857469c6..35f08030 100644 --- a/LibationWinForms/GridEntry.cs +++ b/LibationWinForms/GridEntry.cs @@ -60,6 +60,7 @@ namespace LibationWinForms //DisplayTags and Liberate properties are live. } + public void NotifyChanged() => NotifyPropertyChanged(nameof(GridEntry)); private void PictureStorage_PictureCached(object sender, FileManager.PictureCachedEventArgs e) { diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index 1aea5520..246c97c8 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -67,7 +67,7 @@ namespace LibationWinForms await Liberate_Click(liveGridEntry); break; case nameof(liveGridEntry.DisplayTags): - Details_Click(liveGridEntry.LibraryBook); + Details_Click(liveGridEntry); break; } } @@ -89,18 +89,19 @@ namespace LibationWinForms await BookLiberation.ProcessorAutomationController.BackupSingleBookAsync(libraryBook, (_, __) => RefreshRow(libraryBook.Book.AudibleProductId)); } - private void Details_Click(LibraryBook libraryBook) + private void Details_Click(GridEntry liveGridEntry) { - var bookDetailsForm = new BookDetailsDialog(libraryBook); + var bookDetailsForm = new BookDetailsDialog(liveGridEntry.LibraryBook); if (bookDetailsForm.ShowDialog() != DialogResult.OK) return; - var qtyChanges = LibraryCommands.UpdateUserDefinedItem(libraryBook.Book, bookDetailsForm.NewTags, bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus); + var qtyChanges = LibraryCommands.UpdateUserDefinedItem(liveGridEntry.LibraryBook.Book, bookDetailsForm.NewTags, bookDetailsForm.BookLiberatedStatus, bookDetailsForm.PdfLiberatedStatus); if (qtyChanges == 0) return; //Re-apply filters Filter(); + liveGridEntry.NotifyChanged(); } #endregion From a8d609676eeec693aa07fa0902a1509a677ac32a Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 14:57:23 -0600 Subject: [PATCH 03/12] Null check. --- LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs b/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs index e2c247ae..46ee85bd 100644 --- a/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs +++ b/LibationWinForms/EditTagsDataGridViewImageButtonColumn.cs @@ -27,7 +27,7 @@ namespace LibationWinForms DataGridView.Rows[rowIndex].DefaultCellStyle.ForeColor = foreColor; } - if (tagsString.Length == 0) + if (tagsString?.Length == 0) { base.Paint(graphics, clipBounds, cellBounds, rowIndex, elementState, null, null, null, cellStyle, advancedBorderStyle, paintParts); DrawButtonImage(graphics, ButtonImage, cellBounds); From d0d66c6135ade14750c3844146197a24690cf13e Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 15:38:30 -0600 Subject: [PATCH 04/12] Update using NotifyPropertyChanged instead of Row.Invalidate --- LibationWinForms/AsyncNotifyPropertyChanged.cs | 2 +- LibationWinForms/GridEntry.cs | 1 - LibationWinForms/ProductsGrid.cs | 14 +++++++++----- 3 files changed, 10 insertions(+), 7 deletions(-) diff --git a/LibationWinForms/AsyncNotifyPropertyChanged.cs b/LibationWinForms/AsyncNotifyPropertyChanged.cs index 96f007ed..136930aa 100644 --- a/LibationWinForms/AsyncNotifyPropertyChanged.cs +++ b/LibationWinForms/AsyncNotifyPropertyChanged.cs @@ -8,7 +8,7 @@ namespace LibationWinForms { public event PropertyChangedEventHandler PropertyChanged; - protected void NotifyPropertyChanged([CallerMemberName] string propertyName = "") + public void NotifyPropertyChanged([CallerMemberName] string propertyName = "") => this.UIThread(() => PropertyChanged?.Invoke(this, new PropertyChangedEventArgs(propertyName))); } } diff --git a/LibationWinForms/GridEntry.cs b/LibationWinForms/GridEntry.cs index 35f08030..857469c6 100644 --- a/LibationWinForms/GridEntry.cs +++ b/LibationWinForms/GridEntry.cs @@ -60,7 +60,6 @@ namespace LibationWinForms //DisplayTags and Liberate properties are live. } - public void NotifyChanged() => NotifyPropertyChanged(nameof(GridEntry)); private void PictureStorage_PictureCached(object sender, FileManager.PictureCachedEventArgs e) { diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index 246c97c8..238f102d 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -101,7 +101,9 @@ namespace LibationWinForms //Re-apply filters Filter(); - liveGridEntry.NotifyChanged(); + + //Update whole GridEntry row + liveGridEntry.NotifyPropertyChanged(); } #endregion @@ -150,10 +152,10 @@ namespace LibationWinForms public void RefreshRow(string productId) { - var rowIndex = getRowIndex((ge) => ge.AudibleProductId == productId); + var liveGridEntry = getRowItem((ge) => ge.AudibleProductId == productId); - // update cells incl Liberate button text - _dataGridView.InvalidateRow(rowIndex); + // update GridEntry Liberate cell + liveGridEntry?.NotifyPropertyChanged(nameof(liveGridEntry.Liberate)); // needed in case filtering by -IsLiberated and it gets changed to Liberated. want to immediately show the change Filter(); @@ -194,7 +196,9 @@ namespace LibationWinForms #region DataGridView Macro - private int getRowIndex(Func func) => _dataGridView.GetRowIdOfBoundItem(func); + private GridEntry getRowItem(Func predicate) + => ((SortableBindingList)gridEntryBindingSource.DataSource).FirstOrDefault(predicate); + private GridEntry getGridEntry(int rowIndex) => _dataGridView.GetBoundItem(rowIndex); #endregion From 813d91dfa43fe4cea9537cd84ba56c49c82d684b Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 15:40:16 -0600 Subject: [PATCH 05/12] Better naming --- LibationWinForms/ProductsGrid.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibationWinForms/ProductsGrid.cs b/LibationWinForms/ProductsGrid.cs index 238f102d..4b9f9c03 100644 --- a/LibationWinForms/ProductsGrid.cs +++ b/LibationWinForms/ProductsGrid.cs @@ -152,7 +152,7 @@ namespace LibationWinForms public void RefreshRow(string productId) { - var liveGridEntry = getRowItem((ge) => ge.AudibleProductId == productId); + var liveGridEntry = getGridEntry((ge) => ge.AudibleProductId == productId); // update GridEntry Liberate cell liveGridEntry?.NotifyPropertyChanged(nameof(liveGridEntry.Liberate)); @@ -196,7 +196,7 @@ namespace LibationWinForms #region DataGridView Macro - private GridEntry getRowItem(Func predicate) + private GridEntry getGridEntry(Func predicate) => ((SortableBindingList)gridEntryBindingSource.DataSource).FirstOrDefault(predicate); private GridEntry getGridEntry(int rowIndex) => _dataGridView.GetBoundItem(rowIndex); From 8c620c25ab67912650ef920faf86cfebc0f069d8 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 16:10:05 -0600 Subject: [PATCH 06/12] Separate concerns. --- LibationWinForms/GridEntry.cs | 32 +------------------ .../LiberateDataGridViewImageButtonColumn.cs | 32 ++++++++++++++++++- 2 files changed, 32 insertions(+), 32 deletions(-) diff --git a/LibationWinForms/GridEntry.cs b/LibationWinForms/GridEntry.cs index 857469c6..7a90c437 100644 --- a/LibationWinForms/GridEntry.cs +++ b/LibationWinForms/GridEntry.cs @@ -155,37 +155,7 @@ namespace LibationWinForms #endregion #region Static library display functions - - public static (string mouseoverText, Bitmap buttonImage) GetLiberateDisplay(LiberatedState liberatedStatus, PdfState pdfStatus) - { - (string libState, string image_lib) = liberatedStatus switch - { - LiberatedState.Liberated => ("Liberated", "green"), - LiberatedState.PartialDownload => ("File has been at least\r\npartially downloaded", "yellow"), - LiberatedState.NotDownloaded => ("Book NOT downloaded", "red"), - _ => throw new Exception("Unexpected liberation state") - }; - - (string pdfState, string image_pdf) = pdfStatus switch - { - PdfState.Downloaded => ("\r\nPDF downloaded", "_pdf_yes"), - PdfState.NotDownloaded => ("\r\nPDF NOT downloaded", "_pdf_no"), - PdfState.NoPdf => ("", ""), - _ => throw new Exception("Unexpected PDF state") - }; - - var mouseoverText = libState + pdfState; - - if (liberatedStatus == LiberatedState.NotDownloaded || - liberatedStatus == LiberatedState.PartialDownload || - pdfStatus == PdfState.NotDownloaded) - mouseoverText += "\r\nClick to complete"; - - var buttonImage = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}"); - - return (mouseoverText, buttonImage); - } - + /// /// This information should not change during lifetime, so call only once. /// diff --git a/LibationWinForms/LiberateDataGridViewImageButtonColumn.cs b/LibationWinForms/LiberateDataGridViewImageButtonColumn.cs index 6427677c..b8509e5d 100644 --- a/LibationWinForms/LiberateDataGridViewImageButtonColumn.cs +++ b/LibationWinForms/LiberateDataGridViewImageButtonColumn.cs @@ -22,12 +22,42 @@ namespace LibationWinForms if (value is (LiberatedState liberatedState, PdfState pdfState)) { - (string mouseoverText, Bitmap buttonImage) = GridEntry.GetLiberateDisplay(liberatedState, pdfState); + (string mouseoverText, Bitmap buttonImage) = GetLiberateDisplay(liberatedState, pdfState); DrawButtonImage(graphics, buttonImage, cellBounds); ToolTipText = mouseoverText; } } + + private static (string mouseoverText, Bitmap buttonImage) GetLiberateDisplay(LiberatedState liberatedStatus, PdfState pdfStatus) + { + (string libState, string image_lib) = liberatedStatus switch + { + LiberatedState.Liberated => ("Liberated", "green"), + LiberatedState.PartialDownload => ("File has been at least\r\npartially downloaded", "yellow"), + LiberatedState.NotDownloaded => ("Book NOT downloaded", "red"), + _ => throw new Exception("Unexpected liberation state") + }; + + (string pdfState, string image_pdf) = pdfStatus switch + { + PdfState.Downloaded => ("\r\nPDF downloaded", "_pdf_yes"), + PdfState.NotDownloaded => ("\r\nPDF NOT downloaded", "_pdf_no"), + PdfState.NoPdf => ("", ""), + _ => throw new Exception("Unexpected PDF state") + }; + + var mouseoverText = libState + pdfState; + + if (liberatedStatus == LiberatedState.NotDownloaded || + liberatedStatus == LiberatedState.PartialDownload || + pdfStatus == PdfState.NotDownloaded) + mouseoverText += "\r\nClick to complete"; + + var buttonImage = (Bitmap)Properties.Resources.ResourceManager.GetObject($"liberate_{image_lib}{image_pdf}"); + + return (mouseoverText, buttonImage); + } } } From 85a6e21dcf2bad61e1187d5c33661f430213787b Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 17:03:15 -0600 Subject: [PATCH 07/12] Make sure network file isn't left open. --- AaxDecrypter/AaxcDownloadConverter.cs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/AaxDecrypter/AaxcDownloadConverter.cs b/AaxDecrypter/AaxcDownloadConverter.cs index f9490ecc..c295f2e0 100644 --- a/AaxDecrypter/AaxcDownloadConverter.cs +++ b/AaxDecrypter/AaxcDownloadConverter.cs @@ -118,6 +118,12 @@ namespace AaxDecrypter RetrievedTags?.Invoke(this, aaxFile.AppleTags); RetrievedCoverArt?.Invoke(this, aaxFile.AppleTags.Cover); + if (isCanceled) + { + aaxFile.Dispose(); + nfsPersister.Dispose(); + } + return !isCanceled; } private NetworkFileStreamPersister NewNetworkFilePersister() From 0b129fcf7cf535d8828713c75bbfc8cdfc1d2bea Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Fri, 20 Aug 2021 21:05:29 -0600 Subject: [PATCH 08/12] Fixed NetworkFileStream not resuming from cancellation. --- AaxDecrypter/AaxcDownloadConverter.cs | 8 ++------ AaxDecrypter/NetworkFileStream.cs | 6 ++++-- 2 files changed, 6 insertions(+), 8 deletions(-) diff --git a/AaxDecrypter/AaxcDownloadConverter.cs b/AaxDecrypter/AaxcDownloadConverter.cs index c295f2e0..ef24b03e 100644 --- a/AaxDecrypter/AaxcDownloadConverter.cs +++ b/AaxDecrypter/AaxcDownloadConverter.cs @@ -118,12 +118,6 @@ namespace AaxDecrypter RetrievedTags?.Invoke(this, aaxFile.AppleTags); RetrievedCoverArt?.Invoke(this, aaxFile.AppleTags.Cover); - if (isCanceled) - { - aaxFile.Dispose(); - nfsPersister.Dispose(); - } - return !isCanceled; } private NetworkFileStreamPersister NewNetworkFilePersister() @@ -230,6 +224,8 @@ namespace AaxDecrypter isCanceled = true; aaxFile?.Cancel(); aaxFile?.Dispose(); + nfsPersister?.NetworkFileStream?.Close(); + nfsPersister?.Dispose(); } } } diff --git a/AaxDecrypter/NetworkFileStream.cs b/AaxDecrypter/NetworkFileStream.cs index 5d7544b5..01b63781 100644 --- a/AaxDecrypter/NetworkFileStream.cs +++ b/AaxDecrypter/NetworkFileStream.cs @@ -241,9 +241,12 @@ namespace AaxDecrypter } while (downloadPosition < ContentLength && !isCancelled); _writeFile.Close(); + _networkStream.Close(); WritePosition = downloadPosition; Update(); - _networkStream.Close(); + + downloadedPiece.Set(); + downloadEnded.Set(); if (!isCancelled && WritePosition < ContentLength) throw new WebException($"Downloaded size (0x{WritePosition:X10}) is less than {nameof(ContentLength)} (0x{ContentLength:X10})."); @@ -251,7 +254,6 @@ namespace AaxDecrypter if (WritePosition > ContentLength) throw new WebException($"Downloaded size (0x{WritePosition:X10}) is greater than {nameof(ContentLength)} (0x{ContentLength:X10})."); - downloadEnded.Set(); } #endregion From 7fbe8ae769277480fb8119a5687434f2b3aec849 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 21 Aug 2021 08:03:40 -0600 Subject: [PATCH 09/12] Fixed PDF download form disposed error --- .../BookLiberation/ProcessorAutomationController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index f30618ae..4493e59c 100644 --- a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -98,11 +98,11 @@ namespace LibationWinForms.BookLiberation private static IProcessable CreateBackupBook(EventHandler completedAction, LogMe logMe) { - var downloadPdf = CreateProcessable(logMe); - //Chain pdf download on DownloadDecryptBook.Completed async void onDownloadDecryptBookCompleted(object sender, LibraryBook e) { + var downloadPdf = CreateProcessable(logMe); + await downloadPdf.TryProcessAsync(e); completedAction(sender, e); } From 2a22cff67c253b9a0bd745156db7861b349c8c15 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 21 Aug 2021 08:14:10 -0600 Subject: [PATCH 10/12] Revert "Fixed PDF download form disposed error" This reverts commit 7fbe8ae769277480fb8119a5687434f2b3aec849. --- .../BookLiberation/ProcessorAutomationController.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs index 4493e59c..f30618ae 100644 --- a/LibationWinForms/BookLiberation/ProcessorAutomationController.cs +++ b/LibationWinForms/BookLiberation/ProcessorAutomationController.cs @@ -98,11 +98,11 @@ namespace LibationWinForms.BookLiberation private static IProcessable CreateBackupBook(EventHandler completedAction, LogMe logMe) { + var downloadPdf = CreateProcessable(logMe); + //Chain pdf download on DownloadDecryptBook.Completed async void onDownloadDecryptBookCompleted(object sender, LibraryBook e) { - var downloadPdf = CreateProcessable(logMe); - await downloadPdf.TryProcessAsync(e); completedAction(sender, e); } From 3183f991538628dc96550b9f7b343442cca2bf56 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 21 Aug 2021 08:45:43 -0600 Subject: [PATCH 11/12] Remove unnecessary overrides. --- LibationWinForms/BookLiberation/AudioDecodeForm.cs | 2 -- 1 file changed, 2 deletions(-) diff --git a/LibationWinForms/BookLiberation/AudioDecodeForm.cs b/LibationWinForms/BookLiberation/AudioDecodeForm.cs index 4d8f66b8..aa3cfa3e 100644 --- a/LibationWinForms/BookLiberation/AudioDecodeForm.cs +++ b/LibationWinForms/BookLiberation/AudioDecodeForm.cs @@ -39,7 +39,6 @@ namespace LibationWinForms.BookLiberation #endregion #region IStreamable event handler overrides - public override void OnStreamingBegin(object sender, string beginString) { } public override void OnStreamingProgressChanged(object sender, DownloadProgress downloadProgress) { if (!downloadProgress.ProgressPercentage.HasValue) @@ -54,7 +53,6 @@ namespace LibationWinForms.BookLiberation public override void OnStreamingTimeRemaining(object sender, TimeSpan timeRemaining) => updateRemainingTime((int)timeRemaining.TotalSeconds); - public override void OnStreamingCompleted(object sender, string completedString) { } #endregion #region IAudioDecodable event handlers From f60964f4c79c9ff9a0d0abc54e28c8de564a03d2 Mon Sep 17 00:00:00 2001 From: Michael Bucari-Tovo Date: Sat, 21 Aug 2021 08:46:15 -0600 Subject: [PATCH 12/12] Unsubscribe IStreamable events from Disposed instead of FormClosed. --- .../BookLiberation/BaseForms/LiberationBaseForm.cs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/LibationWinForms/BookLiberation/BaseForms/LiberationBaseForm.cs b/LibationWinForms/BookLiberation/BaseForms/LiberationBaseForm.cs index dbf80104..8df2b4d5 100644 --- a/LibationWinForms/BookLiberation/BaseForms/LiberationBaseForm.cs +++ b/LibationWinForms/BookLiberation/BaseForms/LiberationBaseForm.cs @@ -48,7 +48,7 @@ namespace LibationWinForms.BookLiberation.BaseForms streamable.StreamingCompleted += OnStreamingCompleted; streamable.StreamingCompleted += OnStreamingCompletedClose; - FormClosed += UnsubscribeStreamable; + Disposed += UnsubscribeStreamable; } private void Subscribe(IProcessable processable) { @@ -81,7 +81,7 @@ namespace LibationWinForms.BookLiberation.BaseForms } private void UnsubscribeStreamable(object sender, EventArgs e) { - FormClosed -= UnsubscribeStreamable; + Disposed -= UnsubscribeStreamable; Streamable.StreamingBegin -= OnStreamingBeginShow; Streamable.StreamingBegin -= OnStreamingBegin;