Skip to content

Commit

Permalink
Implement 'Download All' feature for search results
Browse files Browse the repository at this point in the history
  • Loading branch information
formeo14 committed Oct 20, 2024
1 parent e7b3986 commit 1da3ac7
Show file tree
Hide file tree
Showing 4 changed files with 200 additions and 125 deletions.
2 changes: 1 addition & 1 deletion QobuzDownloaderX/View/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public QobuzDownloaderX()
this.AutoSize = true;
InitializeComponent();
logger = new DownloadLogger(output, "MainForm");
RequestHandler.MainRequestHandlers[0].StaticDegreeOfParallelism = 1;
RequestHandler.MainRequestHandlers[0].StaticDegreeOfParallelism = 2;
// Remove previous download error log
logger.RemovePreviousErrorLog();
_requests.SpeedReporter.Timeout = 1000;
Expand Down
221 changes: 124 additions & 97 deletions QobuzDownloaderX/View/SearchForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

48 changes: 48 additions & 0 deletions QobuzDownloaderX/View/SearchForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Diagnostics;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;
Expand All @@ -34,6 +35,38 @@ public SearchForm()
searchTypeSelect.SelectedItem = "Album";
}

private void DownloadAllButton_Click(object sender, EventArgs e)
{
try
{
downloadAllButton.Enabled = false;
List<string> downloadLinks = [];
for (int row = 0; row < resultsTableLayoutPanel.RowCount; row++)
{
if (resultsTableLayoutPanel.GetControlFromPosition(3, row) is Button downloadButton)
{
// Get the download link from the button's Tag property
string downloadLink = downloadButton.Tag?.ToString();
if (!string.IsNullOrEmpty(downloadLink))
downloadLinks.Add(downloadLink);
}
}
Globals.QbdlxForm.downloadUrl.Invoke(new Action(() => Globals.QbdlxForm.downloadUrl.Text = downloadLinks.FirstOrDefault()));
this.Close();
Globals.QbdlxForm.StartLinkItemDownload([.. downloadLinks]);
}
catch (Exception ex)
{
List<string> errorLines = ["Error starting download for all items", ex.ToString(), ""];
System.IO.File.AppendAllLines(errorLog, errorLines);
MessageBox.Show("Error starting download for all items\nlog saved to " + errorLog, "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
}
finally
{
downloadAllButton.Enabled = true;
}
}

private void ResetResultsTableLayoutPanel()
{
if (resultsTableLayoutPanel != null)
Expand Down Expand Up @@ -61,6 +94,8 @@ private void ResetResultsTableLayoutPanel()
resultsTableLayoutPanel.CellPaint += ResultsTableLayoutPanel_CellPaint;

containerScrollPanel.Controls.Add(resultsTableLayoutPanel);
downloadAllButton.Visible = false;
downloadAllButton.Enabled = false;
}

private void ShowAndLogSearchResultError(Exception ex)
Expand Down Expand Up @@ -133,6 +168,12 @@ private void Fill_AlbumResultsTablePanel(SearchResult searchResult)
HiRes = album.Hires.GetValueOrDefault(),
TrackCount = album.TracksCount.GetValueOrDefault()
});
// Show and enable the "Download All" button if there are rows
if (resultsTableLayoutPanel.RowCount > 0)
{
downloadAllButton.Visible = true;
downloadAllButton.Enabled = true;
}
}

private void Fill_TrackResultsTablePanel(SearchResult searchResult)
Expand All @@ -150,6 +191,12 @@ private void Fill_TrackResultsTablePanel(SearchResult searchResult)
ReleaseDate = track.Album.ReleaseDateOriginal.GetValueOrDefault().ToString("yyyy-MM-dd"),
HiRes = track.Hires.GetValueOrDefault()
});
// Show and enable the "Download All" button if there are rows
if (resultsTableLayoutPanel.RowCount > 0)
{
downloadAllButton.Visible = true;
downloadAllButton.Enabled = true;
}
}

private void FillResultsTablePanel<T>(IEnumerable<T> items, Func<T, SearchResultRow> createSearchResultRow)
Expand Down Expand Up @@ -365,6 +412,7 @@ private Button CreateDownloadButton(string webPlayerUrl)
UseVisualStyleBackColor = false,
Height = 23,
Anchor = AnchorStyles.None,
Tag = webPlayerUrl,
TextAlign = ContentAlignment.MiddleCenter
};
downloadButton.FlatAppearance.BorderSize = 0;
Expand Down
Loading

0 comments on commit 1da3ac7

Please sign in to comment.