From 72ac709cda4a16cde7615dbb0831ca17f02ca259 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Fri, 3 Nov 2017 17:42:48 +0200 Subject: [PATCH] Removed the multiple declarations of the clone macro. --- hammond-gtk/src/headerbar.rs | 18 ----------------- hammond-gtk/src/main.rs | 28 ++++++++++++++++++++------ hammond-gtk/src/utils.rs | 23 ++------------------- hammond-gtk/src/views/podcasts_view.rs | 18 ----------------- hammond-gtk/src/widgets/episode.rs | 27 +++---------------------- hammond-gtk/src/widgets/podcast.rs | 20 +----------------- 6 files changed, 28 insertions(+), 106 deletions(-) diff --git a/hammond-gtk/src/headerbar.rs b/hammond-gtk/src/headerbar.rs index f5472b0..58e4a19 100644 --- a/hammond-gtk/src/headerbar.rs +++ b/hammond-gtk/src/headerbar.rs @@ -7,24 +7,6 @@ use hammond_data::index_feed::Database; use podcasts_view::update_podcasts_view; use utils; -// 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 get_headerbar(db: &Database, stack: >k::Stack) -> gtk::HeaderBar { let builder = include_str!("../gtk/headerbar.ui"); let builder = gtk::Builder::new_from_string(builder); diff --git a/hammond-gtk/src/main.rs b/hammond-gtk/src/main.rs index 3d19ebb..78b1950 100644 --- a/hammond-gtk/src/main.rs +++ b/hammond-gtk/src/main.rs @@ -21,6 +21,25 @@ use std::sync::{Arc, Mutex}; use gtk::prelude::*; use gio::{ActionMapExt, ApplicationExt, MenuExt, SimpleActionExt}; +// http://gtk-rs.org/tuto/closures +#[macro_export] +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 + } + ); +} + mod views; mod widgets; mod headerbar; @@ -62,18 +81,15 @@ 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 |_, _| { - let _ = dbcheckup::run(&db2); - }); + check.connect_activate(clone!(db => move |_, _| { + let _ = dbcheckup::run(&db); + })); app.add_action(&check); // Get the headerbar let header = headerbar::get_headerbar(&db, &stack); - // TODO: add delay, cause else there's lock contention for the db obj. - // utils::refresh_db(db.clone(), stack.clone()); window.set_titlebar(&header); window.show_all(); diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index ad907fe..cdd6067 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -13,24 +13,6 @@ use std::sync::mpsc::{channel, Receiver}; use views::podcasts_view; -// 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 - } - ); -} - // Create a thread local storage that will store the arguments to be transfered. thread_local!( static GLOBAL: RefCell move || { - let foo_ = hammond_data::index_feed::refresh_source(&db, &mut source); + thread::spawn(clone!(db, source => move || { + let foo_ = hammond_data::index_feed::refresh_source(&db, &mut source.clone()); if let Ok(x) = foo_ { let Feed(mut req, s) = x; diff --git a/hammond-gtk/src/views/podcasts_view.rs b/hammond-gtk/src/views/podcasts_view.rs index 46b807e..dd4c4b9 100644 --- a/hammond-gtk/src/views/podcasts_view.rs +++ b/hammond-gtk/src/views/podcasts_view.rs @@ -6,24 +6,6 @@ use hammond_data::index_feed::Database; use widgets::podcast::*; -// 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 - } - ); -} - fn show_empty_view(stack: >k::Stack) { let builder = include_str!("../../gtk/empty_view.ui"); let builder = gtk::Builder::new_from_string(builder); diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index 5a54a35..038c1ec 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -1,7 +1,7 @@ use open; use hammond_data::dbqueries; -use hammond_data::models::Episode; +use hammond_data::models::{Episode, Podcast}; use hammond_downloader::downloader; use hammond_data::index_feed::Database; use hammond_data::dbcheckup::*; @@ -19,26 +19,6 @@ use gtk; use gtk::prelude::*; use gtk::{ContainerExt, TextBufferExt}; -// http://gtk-rs.org/tuto/closures -// FIXME: Atm this macro is copied into every module. -// Figure out how to propely define once and export it instead. -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 - } - ); -} - thread_local!( static GLOBAL: RefCell, @@ -218,15 +198,14 @@ fn receive() -> glib::Continue { glib::Continue(false) } -pub fn episodes_listbox(db: &Database, pd_title: &str) -> Result { +pub fn episodes_listbox(db: &Database, pd: &Podcast) -> Result { let conn = db.lock().unwrap(); - let pd = dbqueries::load_podcast_from_title(&conn, pd_title)?; let mut episodes = dbqueries::get_pd_episodes(&conn, &pd)?; drop(conn); let list = gtk::ListBox::new(); episodes.iter_mut().for_each(|ep| { - let w = epidose_widget(db, ep, pd_title); + let w = epidose_widget(db, ep, pd.title()); list.add(&w) }); diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index 7aaf613..a1e4123 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -12,24 +12,6 @@ use hammond_data::dbqueries; use widgets::episode::episodes_listbox; use podcasts_view::update_podcasts_view; -// 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 - } - ); -} - 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"); @@ -52,7 +34,7 @@ fn podcast_widget(db: &Database, stack: >k::Stack, pd: &Podcast) -> gtk::Box { })); title_label.set_text(pd.title()); - let listbox = episodes_listbox(db, pd.title()); + let listbox = episodes_listbox(db, &pd); if let Ok(l) = listbox { view.add(&l); }