Verified Commit e42cb49c authored by Jordan Petridis's avatar Jordan Petridis 🌱

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
parent b40c12ef
......@@ -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);
}
}
});
......
......@@ -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 {
impl DownloadProgress for Progress {
fn get_downloaded(&self) -> u64 {
self.downloaded_bytes
}
pub(crate) fn cancel(&mut self) {
self.cancel = true;
}
}
impl DownloadProgress for Progress {
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! {
......
......@@ -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::<i32>().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")),
};
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment