private void UpdateProgressUI() { var albumAbs = string.Format("{0:0.00} MB of {1:0.00} MB", (DownloadData.AlbumBytes / 1024.0f / 1024.0f), (CurrentAlbum.SizeInBytes / 1024.0f / 1024.0f)); var albumProgress = Math.Min(100, (int)(DownloadData.AlbumBytes * 1.0f / CurrentAlbum.SizeInBytes * 100)); ProgressOverallProgressBar.Value = albumProgress; ProgressOverallValueLabel.Text = string.Format("{0} % ({1})", albumProgress, albumAbs); var trackTotal = DownloadData.TrackTotalBytes > 0 ? DownloadData.TrackTotalBytes : DownloadData.CurrentTrack.SizeInBytes; var trackProgress = Math.Min(100, (int)(DownloadData.TrackBytes * 1.0f / trackTotal * 100)); ProgressTrackProgressBar.Value = trackProgress; ProgressTrackValueLabel.Text = string.Format("{0} % of {1}", trackProgress, DownloadData.CurrentTrack.Title); TaskbarUtils.SetProgressValue(Handle, (ulong)DownloadData.AlbumBytes, (ulong)CurrentAlbum.SizeInBytes); }
void TrackDownloadClient_DownloadDataCompleted(object sender, DownloadDataCompletedEventArgs e) { if (e.Cancelled) { TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.NoProgress); SetFormEnabled(true); SetDataGridRowColor(Color.LightCoral, null, DownloadData.CurrentTrack.Title); } else if (e.Error != null) { TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.Error); TaskbarUtils.Flash(Handle); ShowEx("Failed to download track", e.Error); TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.NoProgress); SetFormEnabled(true); } else { if (DownloadData.Step++ == 0) { string page = Encoding.ASCII.GetString(e.Result); var match = TrackUriRegex.Match(page); if (match.Success) { var filepath = match.Groups["file"].Value; var index = filepath.LastIndexOf('/'); if (index >= 0) { DownloadData.Filename = Uri.UnescapeDataString(filepath.Substring(index + 1)); TrackDownloadClient.DownloadDataAsync(new Uri(filepath)); } } } else { // write track to disk File.WriteAllBytes(Path.Combine(DownloadData.FolderPath, DownloadData.Filename), e.Result); // mark track in list as finished SetDataGridRowColor(ProgressTrackProgressBar.ForeColor, null, DownloadData.CurrentTrack.Title); // download next StartDownload(); } } }
private void TracksDownloadButton_Click(object sender, EventArgs e) { try { // reset track list colors foreach (DataGridViewRow row in TracksListDataGridView.Rows) { SetDataGridRowColor(SystemColors.Control, row, null); } // determine and create target output folder var folder = DownloadLocationTextBox.Text; if (DownloadCreateSubfolderCheckBox.Checked) { var name = CurrentAlbum.Name; foreach (var c in Path.GetInvalidPathChars()) { name = name.Replace(c, '_'); } folder = Path.Combine(folder, name); } if (!Directory.Exists(folder)) { Directory.CreateDirectory(folder); } // create object for holding progress relevant data during download DownloadData = new DownloadProgressData() { FolderPath = folder, RemainingTracks = new List <Track>(CurrentAlbum.Tracks) }; // deactivate form and start TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.Normal); TaskbarUtils.SetProgressValue(Handle, 0, (ulong)CurrentAlbum.SizeInBytes); SetFormEnabled(false); StartDownload(); } catch (Exception ex) { ShowEx("Failed to start download", ex); } }
private void StartDownload() { if (DownloadData.RemainingTracks.Count > 0) { DownloadData.CurrentTrack = DownloadData.RemainingTracks[0]; DownloadData.RemainingTracks.RemoveAt(0); DownloadData.TrackBytes = 0; DownloadData.Step = 0; UpdateProgressUI(); TrackDownloadClient.DownloadDataAsync(DownloadData.CurrentTrack.Address); } else { ProgressOverallValueLabel.Text = ProgressTrackValueLabel.Text = "100 %"; ProgressOverallProgressBar.Value = ProgressTrackProgressBar.Value = 100; SetFormEnabled(true); TaskbarUtils.SetProgressState(Handle, TaskbarUtils.ProgressState.NoProgress); TaskbarUtils.Flash(Handle); } }