From 0137e1e49bcd889957027aabc55cc80f9fe7a79b Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 26 Oct 2017 15:02:44 +0300 Subject: [PATCH] Implemented Partial feed deletion. Cleans up the db but it leaves behind the downloaded content atm. --- hammond-data/src/dbqueries.rs | 38 +++++++++++++++++++++++++++++ hammond-data/src/models.rs | 4 +++ hammond-gtk/src/widgets/episode.rs | 3 +-- hammond-gtk/src/widgets/podcast.rs | 39 ++++++++++++++++++++++++++++++ 4 files changed, 82 insertions(+), 2 deletions(-) diff --git a/hammond-data/src/dbqueries.rs b/hammond-data/src/dbqueries.rs index 3d6fc23..382788e 100644 --- a/hammond-data/src/dbqueries.rs +++ b/hammond-data/src/dbqueries.rs @@ -1,7 +1,10 @@ #![cfg_attr(feature = "cargo-clippy", allow(let_and_return))] use diesel::prelude::*; +use diesel; use models::{Episode, Podcast, Source}; +use index_feed::Database; +use errors::*; // TODO: Needs cleanup. @@ -110,3 +113,38 @@ pub fn load_episode_from_uri(con: &SqliteConnection, uri_: &str) -> QueryResult< let ep = episode.filter(uri.eq(uri_)).get_result::(con); ep } + +pub fn remove_feed(db: &Database, pd: &Podcast) -> Result<()> { + let s_id = pd.source_id(); + let pd_id = pd.id(); + let tempdb = db.lock().unwrap(); + + tempdb.transaction(|| -> Result<()> { + delete_source(&tempdb, s_id)?; + delete_podcast(&tempdb, pd_id)?; + delete_podcast_episodes(&tempdb, pd_id)?; + Ok(()) + })?; + Ok(()) +} + +pub fn delete_source(connection: &SqliteConnection, source_id: i32) -> Result<()> { + use schema::source::dsl::*; + + diesel::delete(source.filter(id.eq(source_id))).execute(connection)?; + Ok(()) +} + +pub fn delete_podcast(connection: &SqliteConnection, podcast_id: i32) -> Result<()> { + use schema::podcast::dsl::*; + + diesel::delete(podcast.filter(id.eq(podcast_id))).execute(connection)?; + Ok(()) +} + +pub fn delete_podcast_episodes(connection: &SqliteConnection, parent_id: i32) -> Result<()> { + use schema::episode::dsl::*; + + diesel::delete(episode.filter(podcast_id.eq(parent_id))).execute(connection)?; + Ok(()) +} diff --git a/hammond-data/src/models.rs b/hammond-data/src/models.rs index 692a395..5dc6bf6 100644 --- a/hammond-data/src/models.rs +++ b/hammond-data/src/models.rs @@ -118,6 +118,10 @@ impl Podcast { self.id } + pub fn source_id(&self) -> i32 { + self.source_id + } + pub fn title(&self) -> &str { &self.title } diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 3f7e051..d56fe25 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -86,12 +86,11 @@ fn epidose_widget(db: &Database, episode: &mut Episode, pd_title: &str) -> gtk:: })); delete_button.connect_clicked( - clone!(episode, db, play_button, download_button => move |del|{ + clone!(episode, db, play_button, download_button => move |del| { on_delete_bttn_clicked(&db, episode.id()); del.hide(); play_button.hide(); download_button.show(); - // TODO: reload the widget }), ); diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 14f1145..e53386e 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -5,9 +5,28 @@ use gdk_pixbuf::Pixbuf; use hammond_data::models::Podcast; use hammond_downloader::downloader; use hammond_data::index_feed::Database; +use hammond_data::dbqueries::{load_podcast_from_title, remove_feed}; use widgets::episode::episodes_listbox; +// http://gtk-rs.org/tuto/closures +macro_rules! clone { + (@param _) => ( _ ); + (@param $x:ident) => ( $x ); + ($($n:ident),+ => move || $body:expr) => ( + { + $( let $n = $n.clone(); )+ + move || $body + } + ); + ($($n:ident),+ => move |$($p:tt),+| $body:expr) => ( + { + $( let $n = $n.clone(); )+ + move |$(clone!(@param $p),)+| $body + } + ); +} + pub fn podcast_widget( db: &Database, title: Option<&str>, @@ -23,6 +42,26 @@ pub fn podcast_widget( let title_label: gtk::Label = pd_widget_buidler.get_object("title_label").unwrap(); let desc_label: gtk::Label = pd_widget_buidler.get_object("description_label").unwrap(); let view: gtk::Viewport = pd_widget_buidler.get_object("view").unwrap(); + let unsub_button: gtk::Button = pd_widget_buidler.get_object("unsub_button").unwrap(); + + // TODO: handle unwraps properly. + // TODO: also remove the downloaded content. + if title.is_some() { + let t = title.unwrap().to_owned(); + // TODO: update the stack views after. + unsub_button.connect_clicked(clone!(db, t => move |bttn| { + let pd = { + let tempdb = db.lock().unwrap(); + load_podcast_from_title(&tempdb, &t).unwrap()}; + let res = 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(); + } + })); + } if let Some(t) = title { title_label.set_text(t);