From 8a90de3c0ef0f6545762fd8030f5bd4495fb2609 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Wed, 10 Jan 2018 09:43:38 +0200 Subject: [PATCH] Implement download cancel action. #24 --- hammond-downloader/src/downloader.rs | 12 ++++++++--- hammond-gtk/resources/gtk/episode_widget.ui | 1 - hammond-gtk/src/manager.rs | 24 +++++++++++++++------ hammond-gtk/src/widgets/episode.rs | 7 ++++++ 4 files changed, 33 insertions(+), 11 deletions(-) diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 180e586..13f3117 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -20,6 +20,7 @@ use hammond_data::xdg_dirs::HAMMOND_CACHE; pub trait DownloadProgress { fn set_downloaded(&mut self, downloaded: u64); fn set_size(&mut self, bytes: u64); + fn should_cancel(&self) -> bool; } // Adapted from https://github.com/mattgathu/rget . @@ -113,12 +114,17 @@ fn save_io( buffer.truncate(bcount); if !buffer.is_empty() { writer.write_all(buffer.as_slice())?; + // This sucks. + // Actually the whole download module is hack, so w/e. if let Some(prog) = progress.clone() { - // This sucks. let len = writer.get_ref().metadata().map(|x| x.len()); if let Ok(l) = len { - let mut m = prog.lock().unwrap(); - m.set_downloaded(l); + if let Ok(mut m) = prog.lock() { + if m.should_cancel() { + bail!("Download was cancelled."); + } + m.set_downloaded(l); + } } } } else { diff --git a/hammond-gtk/resources/gtk/episode_widget.ui b/hammond-gtk/resources/gtk/episode_widget.ui index 5752192..d270cb0 100644 --- a/hammond-gtk/resources/gtk/episode_widget.ui +++ b/hammond-gtk/resources/gtk/episode_widget.ui @@ -221,7 +221,6 @@ Tobias Bernard Cancel - False True False True diff --git a/hammond-gtk/src/manager.rs b/hammond-gtk/src/manager.rs index b0b27f8..d054fa7 100644 --- a/hammond-gtk/src/manager.rs +++ b/hammond-gtk/src/manager.rs @@ -16,6 +16,17 @@ use std::thread; pub struct Progress { total_bytes: u64, downloaded_bytes: u64, + cancel: bool, +} + +impl Default for Progress { + fn default() -> Self { + Progress { + total_bytes: 0, + downloaded_bytes: 0, + cancel: false, + } + } } impl Progress { @@ -37,14 +48,9 @@ impl Progress { pub fn get_downloaded(&self) -> u64 { self.downloaded_bytes } -} -impl Default for Progress { - fn default() -> Self { - Progress { - total_bytes: 0, - downloaded_bytes: 0, - } + pub fn cancel(&mut self) { + self.cancel = true; } } @@ -56,6 +62,10 @@ impl DownloadProgress for Progress { fn set_size(&mut self, bytes: u64) { self.total_bytes = bytes; } + + fn should_cancel(&self) -> bool { + self.cancel + } } lazy_static! { diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index b17c9ce..73bd8bd 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -240,6 +240,13 @@ impl EpisodeWidget { // with the http ContentLength header number rather than // relying to the RSS feed. update_total_size_callback(prog.clone(), total_size); + + self.cancel.connect_clicked(clone!(prog => move |cancel| { + if let Ok(mut m) = prog.lock() { + m.cancel(); + cancel.set_sensitive(false); + } + })); } } }