diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index ad7516d..80d9b00 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -59,10 +59,7 @@ pub fn get_episode(con: &SqliteConnection, ep_id: i32) -> QueryResult { ep } -pub fn get_episode_from_local_uri( - con: &SqliteConnection, - ep_id: i32, -) -> QueryResult> { +pub fn get_episode_local_uri(con: &SqliteConnection, ep_id: i32) -> QueryResult> { use schema::episode::dsl::*; let ep = episode diff --git a/hammond-gtk/gtk/podcasts_child.ui b/hammond-gtk/gtk/podcasts_child.ui index a3c89b3..ad8158a 100644 --- a/hammond-gtk/gtk/podcasts_child.ui +++ b/hammond-gtk/gtk/podcasts_child.ui @@ -37,7 +37,7 @@ True - False + True 0 diff --git a/hammond-gtk/src/main.rs b/hammond-gtk/src/main.rs index 4c98a68..cfc6d74 100644 --- a/hammond-gtk/src/main.rs +++ b/hammond-gtk/src/main.rs @@ -54,6 +54,7 @@ fn build_ui(app: >k::Application) { Inhibit(false) }); + // Setup quit in the app menu since default is overwritten. let quit = gio::SimpleAction::new("quit", None); let window2 = window.clone(); quit.connect_activate(move |_, _| { @@ -61,6 +62,7 @@ fn build_ui(app: >k::Application) { }); app.add_action(&quit); + // Setup the dbcheckup in the app menu. let db2 = Arc::clone(&db); let check = gio::SimpleAction::new("check", None); check.connect_activate(move |_, _| { diff --git a/hammond-gtk/src/views/podcasts_view.rs b/hammond-gtk/src/views/podcasts_view.rs index 0c32d03..ed9f106 100644 --- a/hammond-gtk/src/views/podcasts_view.rs +++ b/hammond-gtk/src/views/podcasts_view.rs @@ -45,10 +45,10 @@ fn populate_flowbox(db: &Database, stack: >k::Stack, flowbox: >k::FlowBox) { let title = parent.title(); let img = parent.image_uri(); let pixbuf = get_pixbuf_from_path(img, title); - let f = create_flowbox_child(title, pixbuf.clone()); + let f = create_flowbox_child(title, pixbuf); f.connect_activate(clone!(db, stack, parent => move |_| { - on_flowbox_child_activate(&db, &stack, &parent, pixbuf.clone()); + on_flowbox_child_activate(&db, &stack, &parent); })); flowbox.add(&f); }); @@ -58,8 +58,11 @@ fn populate_flowbox(db: &Database, stack: >k::Stack, flowbox: >k::FlowBox) { flowbox.show_all(); } -fn setup_podcast_widget(db: &Database, stack: >k::Stack) { - let pd_widget = podcast_widget(db, stack, None, None, None); +fn setup_podcast_widget(stack: >k::Stack) { + let pd_widget_source = include_str!("../../gtk/podcast_widget.ui"); + let pd_widget_buidler = gtk::Builder::new_from_string(pd_widget_source); + let pd_widget: gtk::Box = pd_widget_buidler.get_object("podcast_widget").unwrap(); + stack.add_named(&pd_widget, "pdw"); } @@ -80,7 +83,7 @@ fn setup_podcasts_grid(db: &Database, stack: >k::Stack) { pub fn setup_stack(db: &Database) -> gtk::Stack { let stack = gtk::Stack::new(); - setup_podcast_widget(db, &stack); + setup_podcast_widget(&stack); setup_podcasts_grid(db, &stack); stack } diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index c8f510a..3875b68 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -167,7 +167,7 @@ fn on_download_clicked( fn on_play_bttn_clicked(db: &Database, episode_id: i32) { let local_uri = { let tempdb = db.lock().unwrap(); - dbqueries::get_episode_from_local_uri(&tempdb, episode_id).unwrap() + dbqueries::get_episode_local_uri(&tempdb, episode_id).unwrap() }; if let Some(uri) = local_uri { diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 54eb58b..ba22ea3 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -30,14 +30,7 @@ macro_rules! clone { ); } -// TODO: Refacto to take a Podcast Stuct as argument instead. -pub fn podcast_widget( - db: &Database, - stack: >k::Stack, - title: Option<&str>, - description: Option<&str>, - image: Option, -) -> gtk::Box { +pub fn podcast_widget(db: &Database, stack: >k::Stack, pd: &Podcast) -> gtk::Box { // Adapted from gnome-music AlbumWidget let pd_widget_source = include_str!("../../gtk/podcast_widget.ui"); let pd_widget_buidler = gtk::Builder::new_from_string(pd_widget_source); @@ -52,77 +45,78 @@ pub fn podcast_widget( .get_object("mark_all_played_button") .unwrap(); - // TODO: refactor, splitoff, spawn a thread probably. - if title.is_some() { - let t = title.unwrap().to_owned(); - unsub_button.connect_clicked(clone!(db, stack, t => move |bttn| { - let pd = { - let tempdb = db.lock().unwrap(); - dbqueries::load_podcast_from_title(&tempdb, &t)}; - if let Ok(pd) = pd { - - let res = dbqueries::remove_feed(&db, &pd); - if res.is_ok(){ - info!("{} was removed succesfully.", &t); - // hack to get away without properly checking for none. - // if pressed twice would panic. - bttn.hide(); - - let dl_fold = downloader::get_download_folder(&t); - if let Ok(fold) = dl_fold{ - let res3 = fs::remove_dir_all(&fold); - if res3.is_ok(){ - info!("All the content at, {} was removed succesfully", &fold); - - } - }; - } - } - update_podcasts_view(&db, &stack); - stack.set_visible_child_name("pd_grid") + // TODO: spawn a thread to avoid locking the UI probably. + unsub_button.connect_clicked(clone!(db, stack, pd => move |bttn| { + on_unsub_button_clicked(&db, &stack, &pd, bttn); })); - } - if let Some(t) = title { - title_label.set_text(t); - let listbox = episodes_listbox(db, t); - view.add(&listbox); - } + title_label.set_text(pd.title()); + let listbox = episodes_listbox(db, pd.title()); + view.add(&listbox); - if let Some(d) = description { - desc_label.set_text(d); - } + desc_label.set_text(pd.description()); - if let Some(i) = image { + let img = get_pixbuf_from_path(pd.image_uri(), pd.title()); + if let Some(i) = img { cover.set_from_pixbuf(&i); } - // TODO: refactor - if let Some(t) = title { - let t = t.to_owned(); - played_button.connect_clicked(clone!(db, stack, t => move |_| { - let tempdb = db.lock().unwrap(); - let parent = dbqueries::load_podcast_from_title(&tempdb, &t).unwrap(); - let _ = dbqueries::update_none_to_played_now(&tempdb, &parent); - drop(tempdb); - update_podcast_widget(&db, &stack, &parent); - })); - } + played_button.connect_clicked(clone!(db, stack, pd => move |_| { + on_played_button_clicked(&db, &stack, &pd); + })); - if let Some(t) = title { - let tempdb = db.lock().unwrap(); - let parent = dbqueries::load_podcast_from_title(&tempdb, t).unwrap(); - let f = dbqueries::get_pd_unplayed_episodes(&tempdb, &parent); - if let Ok(l) = f { - if !l.is_empty() { - played_button.show() - } - } - } + show_played_button(db, pd, &played_button); pd_widget } +fn on_unsub_button_clicked( + db: &Database, + stack: >k::Stack, + pd: &Podcast, + unsub_button: >k::Button, +) { + let res = dbqueries::remove_feed(db, pd); + if res.is_ok() { + info!("{} was removed succesfully.", pd.title()); + // hack to get away without properly checking for none. + // if pressed twice would panic. + unsub_button.hide(); + + let dl_fold = downloader::get_download_folder(pd.title()); + if let Ok(fold) = dl_fold { + let res3 = fs::remove_dir_all(&fold); + if res3.is_ok() { + info!("All the content at, {} was removed succesfully", &fold); + } + }; + } + update_podcasts_view(db, stack); + stack.set_visible_child_name("pd_grid") +} + +fn on_played_button_clicked(db: &Database, stack: >k::Stack, pd: &Podcast) { + { + let tempdb = db.lock().unwrap(); + let _ = dbqueries::update_none_to_played_now(&tempdb, pd); + } + + update_podcast_widget(db, stack, pd); +} + +fn show_played_button(db: &Database, pd: &Podcast, played_button: >k::Button) { + let new_episodes = { + let tempdb = db.lock().unwrap(); + dbqueries::get_pd_unplayed_episodes(&tempdb, pd) + }; + + if let Ok(n) = new_episodes { + if !n.is_empty() { + played_button.show() + } + } +} + pub fn create_flowbox_child(title: &str, cover: Option) -> gtk::FlowBoxChild { let build_src = include_str!("../../gtk/podcasts_child.ui"); let builder = gtk::Builder::new_from_string(build_src); @@ -151,20 +145,9 @@ pub fn create_flowbox_child(title: &str, cover: Option) -> gtk::FlowBoxC fbc } -pub fn on_flowbox_child_activate( - db: &Database, - stack: >k::Stack, - parent: &Podcast, - pixbuf: Option, -) { +pub fn on_flowbox_child_activate(db: &Database, stack: >k::Stack, parent: &Podcast) { let old = stack.get_child_by_name("pdw").unwrap(); - let pdw = podcast_widget( - db, - stack, - Some(parent.title()), - Some(parent.description()), - pixbuf, - ); + let pdw = podcast_widget(db, stack, parent); stack.remove(&old); stack.add_named(&pdw, "pdw"); @@ -187,15 +170,10 @@ pub fn get_pixbuf_from_path(img_path: Option<&str>, pd_title: &str) -> Option gtk::Box { - let img = get_pixbuf_from_path(pd.image_uri(), pd.title()); - podcast_widget(db, stack, Some(pd.title()), Some(pd.description()), img) -}