From e42cb49cbebf06fa1e0016af9b35db896028a99b Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Tue, 4 Sep 2018 22:29:21 +0300 Subject: [PATCH] EpisodeWidget: Hide total_size if request fails This moves the rest of the methods of Progress struct to the downloader trait and cancels the Progress if the request does nto succed. Close #90 --- podcasts-downloader/src/downloader.rs | 16 +++++++++++++--- podcasts-gtk/src/manager.rs | 24 ++++++++++++------------ podcasts-gtk/src/widgets/episode.rs | 25 +++++++++++++++++++++---- 3 files changed, 46 insertions(+), 19 deletions(-) diff --git a/podcasts-downloader/src/downloader.rs b/podcasts-downloader/src/downloader.rs index 86e44ba..896959d 100644 --- a/podcasts-downloader/src/downloader.rs +++ b/podcasts-downloader/src/downloader.rs @@ -22,9 +22,12 @@ use errors::DownloadError; // with / or not. pub trait DownloadProgress { + fn get_downloaded(&self) -> u64; fn set_downloaded(&mut self, downloaded: u64); + fn get_size(&self) -> u64; fn set_size(&mut self, bytes: u64); fn should_cancel(&self) -> bool; + fn cancel(&mut self); } // Adapted from https://github.com/mattgathu/rget . @@ -63,6 +66,12 @@ fn download_into( info!("Status Resp: {}", resp.status()); if !resp.status().is_success() { + if let Some(ref prog) = progress { + if let Ok(mut m) = prog.lock() { + m.cancel(); + } + } + return Err(DownloadError::UnexpectedResponse(resp.status())); } @@ -82,9 +91,10 @@ fn download_into( let out_file = format!("{}/temp.part", tempdir.path().to_str().unwrap(),); ct_len.map(|x| { - if let Some(p) = progress.clone() { - let mut m = p.lock().unwrap(); - m.set_size(x); + if let Some(ref p) = progress { + if let Ok(mut m) = p.lock() { + m.set_size(x); + } } }); diff --git a/podcasts-gtk/src/manager.rs b/podcasts-gtk/src/manager.rs index af932f9..c0a3171 100644 --- a/podcasts-gtk/src/manager.rs +++ b/podcasts-gtk/src/manager.rs @@ -41,21 +41,13 @@ impl Progress { }; ratio } - - pub(crate) fn get_total_size(&self) -> u64 { - self.total_bytes - } - - pub(crate) fn get_downloaded(&self) -> u64 { - self.downloaded_bytes - } - - pub(crate) fn cancel(&mut self) { - self.cancel = true; - } } impl DownloadProgress for Progress { + fn get_downloaded(&self) -> u64 { + self.downloaded_bytes + } + fn set_downloaded(&mut self, downloaded: u64) { self.downloaded_bytes = downloaded } @@ -64,9 +56,17 @@ impl DownloadProgress for Progress { self.total_bytes = bytes; } + fn get_size(&self) -> u64 { + self.total_bytes + } + fn should_cancel(&self) -> bool { self.cancel } + + fn cancel(&mut self) { + self.cancel = true; + } } lazy_static! { diff --git a/podcasts-gtk/src/widgets/episode.rs b/podcasts-gtk/src/widgets/episode.rs index 7dd9bf9..940148a 100644 --- a/podcasts-gtk/src/widgets/episode.rs +++ b/podcasts-gtk/src/widgets/episode.rs @@ -13,6 +13,7 @@ use open; use podcasts_data::dbqueries; use podcasts_data::utils::get_download_folder; use podcasts_data::EpisodeWidgetModel; +use podcasts_downloader::downloader::DownloadProgress; use app::Action; use manager; @@ -519,8 +520,12 @@ fn progress_bar_helper( None => return Ok(glib::Continue(false)), }; - let (fraction, downloaded) = match prog.try_lock() { - Ok(guard) => (guard.get_fraction(), guard.get_downloaded()), + let (fraction, downloaded, cancel) = match prog.try_lock() { + Ok(guard) => ( + guard.get_fraction(), + guard.get_downloaded(), + guard.should_cancel(), + ), Err(TryLockError::WouldBlock) => return Ok(glib::Continue(true)), Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")), }; @@ -547,7 +552,19 @@ fn progress_bar_helper( if (fraction >= 1.0) && (!fraction.is_nan()) { Ok(glib::Continue(false)) - } else if !active { + } else if !active || cancel { + // if the total size is not a number, hide it + if widget + .info + .total_size + .get_text() + .as_ref() + .map(|s| s.trim_right_matches(" MB")) + .and_then(|s| s.parse::().ok()) + .is_none() + { + widget.info.total_size.hide(); + } Ok(glib::Continue(false)) } else { Ok(glib::Continue(true)) @@ -576,7 +593,7 @@ fn total_size_helper( // Get the total_bytes. let total_bytes = match prog.try_lock() { - Ok(guard) => guard.get_total_size(), + Ok(guard) => guard.get_size(), Err(TryLockError::WouldBlock) => return Ok(glib::Continue(true)), Err(TryLockError::Poisoned(_)) => return Err(format_err!("Progress Mutex is poisoned")), };