diff --git a/hammond-downloader/src/manager.rs b/hammond-downloader/src/manager.rs index eeb4057..53a914a 100644 --- a/hammond-downloader/src/manager.rs +++ b/hammond-downloader/src/manager.rs @@ -27,8 +27,10 @@ impl DonwloadInstance { } } -struct Manager { - active: Arc>>, +#[derive(Debug, Clone)] +// FIXME: privacy stuff +pub struct Manager { + pub active: Arc>>, } impl Default for Manager { @@ -40,11 +42,11 @@ impl Default for Manager { } impl Manager { - fn new() -> Self { + pub fn new() -> Self { Manager::default() } - fn add(&self, id: i32, directory: &str) { + pub fn add(&self, id: i32, directory: &str) { { let mut m = self.active.lock().unwrap(); m.insert(id); diff --git a/hammond-gtk/src/app.rs b/hammond-gtk/src/app.rs index 99415d9..35fe044 100644 --- a/hammond-gtk/src/app.rs +++ b/hammond-gtk/src/app.rs @@ -5,12 +5,18 @@ use gtk::prelude::*; use gio::{ActionMapExt, ApplicationExt, ApplicationExtManual, SimpleActionExt}; use hammond_data::utils::checkup; +use hammond_downloader::manager::Manager; use headerbar::Header; use content::Content; use utils; use std::rc::Rc; +use std::sync::{Arc, Mutex}; + +lazy_static! { + pub static ref DOWNLOADS_MANAGER: Arc> = Arc::new(Mutex::new(Manager::new())); +} #[derive(Debug, Clone)] pub struct App { diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index ce952be..a318488 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -13,6 +13,8 @@ use hammond_data::{EpisodeWidgetQuery, Podcast}; use hammond_data::errors::*; use hammond_downloader::downloader; +use app::DOWNLOADS_MANAGER; + use std::thread; use std::cell::RefCell; use std::sync::mpsc::{channel, Receiver}; @@ -105,6 +107,14 @@ impl EpisodeWidget { .map(|c| c.add_class("dim-label")); } + { + let m = DOWNLOADS_MANAGER.lock().unwrap(); + let list = m.active.lock().unwrap(); + if list.contains(&episode.rowid()) { + self.show_progess_bar() + }; + } + // Declare a custom humansize option struct // See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html let custom_options = size_opts::FileSizeOpts { @@ -177,6 +187,16 @@ impl EpisodeWidget { ); })); } + + fn show_progess_bar(&self) { + let progress_bar = self.progress.clone(); + timeout_add(200, move || { + progress_bar.pulse(); + glib::Continue(true) + }); + + self.progress.show(); + } } fn on_download_clicked( @@ -194,37 +214,18 @@ fn on_download_clicked( glib::Continue(true) }); - // Create a async channel. - let (sender, receiver) = channel(); - - // Pass the desired arguments into the Local Thread Storage. - GLOBAL.with( - clone!(download_bttn, play_bttn, cancel_bttn, progress => move |global| { - *global.borrow_mut() = Some(( - download_bttn, - play_bttn, - cancel_bttn, - progress, - receiver)); - }), - ); - let pd = dbqueries::get_podcast_from_id(ep.podcast_id()).unwrap(); let pd_title = pd.title().to_owned(); let mut ep = ep.clone(); cancel_bttn.show(); progress.show(); download_bttn.hide(); - thread::spawn(move || { - let download_fold = downloader::get_download_folder(&pd_title).unwrap(); - let e = downloader::get_episode(&mut ep, download_fold.as_str()); - if let Err(err) = e { - error!("Error while trying to download: {:?}", ep.uri()); - error!("Error: {}", err); - }; - sender.send(true).expect("Couldn't send data to channel");; - glib::idle_add(receive); - }); + let download_fold = downloader::get_download_folder(&pd_title).unwrap(); + { + let m = DOWNLOADS_MANAGER.clone(); + let man = m.lock().unwrap(); + man.add(ep.rowid(), &download_fold); + } } fn on_play_bttn_clicked(episode_id: i32) {