Implement download cancel action. #24

This commit is contained in:
Jordan Petridis 2018-01-10 09:43:38 +02:00
parent 77f005caab
commit 8a90de3c0e
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 33 additions and 11 deletions

View File

@ -20,6 +20,7 @@ use hammond_data::xdg_dirs::HAMMOND_CACHE;
pub trait DownloadProgress { pub trait DownloadProgress {
fn set_downloaded(&mut self, downloaded: u64); fn set_downloaded(&mut self, downloaded: u64);
fn set_size(&mut self, bytes: u64); fn set_size(&mut self, bytes: u64);
fn should_cancel(&self) -> bool;
} }
// Adapted from https://github.com/mattgathu/rget . // Adapted from https://github.com/mattgathu/rget .
@ -113,14 +114,19 @@ fn save_io(
buffer.truncate(bcount); buffer.truncate(bcount);
if !buffer.is_empty() { if !buffer.is_empty() {
writer.write_all(buffer.as_slice())?; writer.write_all(buffer.as_slice())?;
if let Some(prog) = progress.clone() {
// This sucks. // This sucks.
// Actually the whole download module is hack, so w/e.
if let Some(prog) = progress.clone() {
let len = writer.get_ref().metadata().map(|x| x.len()); let len = writer.get_ref().metadata().map(|x| x.len());
if let Ok(l) = len { if let Ok(l) = len {
let mut m = prog.lock().unwrap(); if let Ok(mut m) = prog.lock() {
if m.should_cancel() {
bail!("Download was cancelled.");
}
m.set_downloaded(l); m.set_downloaded(l);
} }
} }
}
} else { } else {
break; break;
} }

View File

@ -221,7 +221,6 @@ Tobias Bernard
<child> <child>
<object class="GtkButton" id="cancel_button"> <object class="GtkButton" id="cancel_button">
<property name="label" translatable="yes">Cancel</property> <property name="label" translatable="yes">Cancel</property>
<property name="sensitive">False</property>
<property name="can_focus">True</property> <property name="can_focus">True</property>
<property name="receives_default">False</property> <property name="receives_default">False</property>
<property name="no_show_all">True</property> <property name="no_show_all">True</property>

View File

@ -16,6 +16,17 @@ use std::thread;
pub struct Progress { pub struct Progress {
total_bytes: u64, total_bytes: u64,
downloaded_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 { impl Progress {
@ -37,14 +48,9 @@ impl Progress {
pub fn get_downloaded(&self) -> u64 { pub fn get_downloaded(&self) -> u64 {
self.downloaded_bytes self.downloaded_bytes
} }
}
impl Default for Progress { pub fn cancel(&mut self) {
fn default() -> Self { self.cancel = true;
Progress {
total_bytes: 0,
downloaded_bytes: 0,
}
} }
} }
@ -56,6 +62,10 @@ impl DownloadProgress for Progress {
fn set_size(&mut self, bytes: u64) { fn set_size(&mut self, bytes: u64) {
self.total_bytes = bytes; self.total_bytes = bytes;
} }
fn should_cancel(&self) -> bool {
self.cancel
}
} }
lazy_static! { lazy_static! {

View File

@ -240,6 +240,13 @@ impl EpisodeWidget {
// with the http ContentLength header number rather than // with the http ContentLength header number rather than
// relying to the RSS feed. // relying to the RSS feed.
update_total_size_callback(prog.clone(), total_size); 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);
}
}));
} }
} }
} }