diff --git a/Sources/BandcampDownloader/MainWindow.xaml.cs b/Sources/BandcampDownloader/MainWindow.xaml.cs index 9b6917d1..07f79281 100644 --- a/Sources/BandcampDownloader/MainWindow.xaml.cs +++ b/Sources/BandcampDownloader/MainWindow.xaml.cs @@ -18,21 +18,38 @@ namespace BandcampDownloader { /// public partial class MainWindow: Window { - // The files to download, or being downloaded, or downloaded - // Used to compute the current received bytes and the total bytes to download - private List filesToDownload; + #region Fields + /// + /// The files to download, or being downloaded, or downloaded + /// Used to compute the current received bytes and the total bytes to download + /// + private List filesDownload; + + /// + /// Used to compute and display the download speed + /// + private DateTime lastDownloadSpeedUpdate; + + /// + /// Used to compute and display the download speed + /// + private long lastTotalReceivedBytes = 0; - // Used when user clicks on 'Stop' to abort all current downloads + /// + /// Used when user clicks on 'Stop' to abort all current downloads + /// private List pendingDownloads; - // Used when user clicks on 'Stop' to manage the cancelation (UI...) - private Boolean userCancelled; - - // Used to compute and display the download speed - private long lastTotalReceivedBytes = 0; - private DateTime lastDownloadSpeedUpdate; + /// + /// Used when user clicks on 'Stop' to manage the cancelation (UI...) + /// + private Boolean userCancelled; + #endregion #region Constructor + /// + /// Initializes a new instance of the class. + /// public MainWindow() { InitializeComponent(); // Increase the maximum of concurrent connections to be able to download more than 2 @@ -49,6 +66,16 @@ public MainWindow() { #endregion Constructor #region Methods + /// + /// Downloads an album. + /// + /// The album. + /// The downloads folder. + /// if set to true, tracks will be tagged. + /// if set to true, cover art will be saved in + /// tags. + /// if set to true, cover art will be saved in + /// the downloads folder. private void DownloadAlbum(Album album, String downloadsFolder, Boolean tagTracks, Boolean saveCoverArtInTags, Boolean saveCovertArtInFolder) { if (this.userCancelled) { @@ -117,8 +144,18 @@ private void DownloadAlbum(Album album, String downloadsFolder, Boolean tagTrack } } + /// + /// Downloads and tags a track. + /// + /// The album of the track to download. + /// The cover art. + /// The path where to save the tracks. + /// The track to download. + /// if set to true, the track will be tagged.. + /// if set to true, the cover art will be saved in + /// the track tags. private void DownloadAndTagTrack(Album album, TagLib.Picture artwork, - String albumDirectoryPath, Track track, Boolean tagTracks, Boolean saveCoverArtInTags) { + String albumDirectoryPath, Track track, Boolean tagTrack, Boolean saveCoverArtInTags) { // Set location to save the file String trackPath = albumDirectoryPath + track.GetFileName(album.Artist); @@ -132,7 +169,7 @@ private void DownloadAndTagTrack(Album album, TagLib.Picture artwork, webClient.DownloadFileCompleted += (s, e) => { if (!e.Cancelled) { - if (tagTracks) { + if (tagTrack) { // Tag (ID3V2) the file when downloaded TagLib.File tagFile = TagLib.File.Create(trackPath); tagFile.Tag.Album = album.Title; @@ -175,6 +212,11 @@ private void DownloadAndTagTrack(Album album, TagLib.Picture artwork, doneEvent.WaitOne(); } + /// + /// Returns the albums located at the provided URLs. + /// + /// The URLs. + /// The albums located at the provided URLs. private List GetAlbums(List urls) { var albums = new List(); @@ -202,6 +244,13 @@ private List GetAlbums(List urls) { return albums; } + /// + /// Returns the size of the file located at the provided URL. + /// + /// The URL. + /// The protocol method to use in order to retrieve the file + /// size. + /// The size of the file located at the provided URL. private long GetFileSize(String url, String protocolMethod) { var webRequest = HttpWebRequest.Create(url); webRequest.Method = protocolMethod; @@ -212,6 +261,11 @@ private long GetFileSize(String url, String protocolMethod) { return fileSize; } + /// + /// Returns the files to download from a list of albums. + /// + /// The albums. + /// The files to download. private List GetFilesToDownload(List albums) { var files = new List(); foreach (Album album in albums) { @@ -230,6 +284,10 @@ private List GetFilesToDownload(List albums) { return files; } + /// + /// Displays the specified message in the log textbox. + /// + /// The message. private void Log(String message) { this.Dispatcher.Invoke(new Action(() => { textBoxLog.AppendText(DateTime.Now.ToString("HH:mm:ss") + " " + message + @@ -240,6 +298,11 @@ private void Log(String message) { })); } + /// + /// Updates the state of the controls. + /// + /// True if the download just started, false if it just + /// stopped. private void UpdateControlsState(Boolean downloadStarted) { this.Dispatcher.Invoke(new Action(() => { if (downloadStarted) { @@ -277,15 +340,20 @@ private void UpdateControlsState(Boolean downloadStarted) { })); } + /// + /// Updates the progress messages and the progressbar. + /// + /// The URL of the file that just progressed. + /// The received bytes for the specified file. private void UpdateProgress(String fileUrl, long bytesReceived) { DateTime now = DateTime.Now; - lock (this.filesToDownload) { + lock (this.filesDownload) { // Compute new progress values - File currentFile = this.filesToDownload.Where(f => f.Url == fileUrl).First(); + File currentFile = this.filesDownload.Where(f => f.Url == fileUrl).First(); currentFile.BytesReceived = bytesReceived; - long totalReceivedBytes = this.filesToDownload.Sum(f => f.BytesReceived); - long bytesToDownload = this.filesToDownload.Sum(f => f.Size); + long totalReceivedBytes = this.filesDownload.Sum(f => f.BytesReceived); + long bytesToDownload = this.filesDownload.Sum(f => f.Size); Double bytesPerSecond; if (this.lastTotalReceivedBytes == 0) { @@ -293,17 +361,16 @@ private void UpdateProgress(String fileUrl, long bytesReceived) { bytesPerSecond = 0; this.lastTotalReceivedBytes = totalReceivedBytes; this.lastDownloadSpeedUpdate = now; - } else if (( now - this.lastDownloadSpeedUpdate ).TotalMilliseconds > 500) { // Last update of progress happened more than 500 milliseconds ago - bytesPerSecond = ((Double) (totalReceivedBytes - this.lastTotalReceivedBytes)) / + bytesPerSecond = ( (Double) ( totalReceivedBytes - this.lastTotalReceivedBytes ) ) / ( now - this.lastDownloadSpeedUpdate ).TotalSeconds; this.lastTotalReceivedBytes = totalReceivedBytes; this.lastDownloadSpeedUpdate = now; // Update UI this.Dispatcher.Invoke(new Action(() => { - labelDownloadSpeed.Content = (bytesPerSecond / 1024).ToString("0.0") + + labelDownloadSpeed.Content = ( bytesPerSecond / 1024 ).ToString("0.0") + " kB/s"; })); } @@ -312,7 +379,7 @@ private void UpdateProgress(String fileUrl, long bytesReceived) { this.Dispatcher.Invoke(new Action(() => { if (!this.userCancelled) { // Update progress label - labelProgress.Content = + labelProgress.Content = ( (Double) totalReceivedBytes / ( 1024 * 1024 ) ).ToString("0.00") + " MB / " + ( (Double) bytesToDownload / ( 1024 * 1024 ) ).ToString("0.00") + @@ -367,10 +434,10 @@ private void buttonStart_Click(object sender, RoutedEventArgs e) { albums = GetAlbums(urls); }).ContinueWith(x => { // Save files to download (we'll need the list to update the progressBar) - this.filesToDownload = GetFilesToDownload(albums); + this.filesDownload = GetFilesToDownload(albums); }).ContinueWith(x => { // Set progressBar max value - long bytesToDownload = this.filesToDownload.Sum(f => f.Size); + long bytesToDownload = this.filesDownload.Sum(f => f.Size); if (bytesToDownload > 0) { this.Dispatcher.Invoke(new Action(() => { progressBar.IsIndeterminate = false;