From 94879b4256ca52817b3d0203ebbe48744c3d1f14 Mon Sep 17 00:00:00 2001 From: Jordan Petridis Date: Thu, 30 Nov 2017 21:38:21 +0200 Subject: [PATCH] Move get_pixbuf_from_path into hammond_gtk/src/utils.rs module. --- hammond-gtk/src/utils.rs | 41 +++++++++++++++++++++++++++++- hammond-gtk/src/views/podcasts.rs | 1 + hammond-gtk/src/widgets/podcast.rs | 38 +-------------------------- 3 files changed, 42 insertions(+), 38 deletions(-) diff --git a/hammond-gtk/src/utils.rs b/hammond-gtk/src/utils.rs index 2799f6d..794765c 100644 --- a/hammond-gtk/src/utils.rs +++ b/hammond-gtk/src/utils.rs @@ -1,8 +1,10 @@ use glib; use gtk; +use gdk_pixbuf::Pixbuf; use hammond_data::feed; -use hammond_data::Source; +use hammond_data::{Podcast, Source}; +use hammond_downloader::downloader; use std::{thread, time}; use std::cell::RefCell; @@ -61,3 +63,40 @@ fn refresh_podcasts_view() -> glib::Continue { }); glib::Continue(false) } + +pub fn get_pixbuf_from_path(pd: &Podcast) -> Option { + let img_path = downloader::cache_image(pd); + if let Some(i) = img_path { + Pixbuf::new_from_file_at_scale(&i, 256, 256, true).ok() + } else { + None + } +} + +#[cfg(test)] +mod tests { + use hammond_data::Source; + use hammond_data::feed::index; + use hammond_data::dbqueries; + use diesel::Identifiable; + use super::*; + + #[test] + fn test_get_pixbuf_from_path() { + let url = "http://www.newrustacean.com/feed.xml"; + + // Create and index a source + let source = Source::from_url(url).unwrap(); + // Copy it's id + let sid = source.id().clone(); + + // Convert Source it into a Feed and index it + let feed = source.into_feed().unwrap(); + index(vec![feed]); + + // Get the Podcast + let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); + let pxbuf = get_pixbuf_from_path(&pd); + assert!(pxbuf.is_some()); + } +} diff --git a/hammond-gtk/src/views/podcasts.rs b/hammond-gtk/src/views/podcasts.rs index 44de32e..2b55a96 100644 --- a/hammond-gtk/src/views/podcasts.rs +++ b/hammond-gtk/src/views/podcasts.rs @@ -7,6 +7,7 @@ use hammond_data::dbqueries; use hammond_data::Podcast; use widgets::podcast::*; +use utils::get_pixbuf_from_path; fn setup_empty_view(stack: >k::Stack) { let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/empty_view.ui"); diff --git a/hammond-gtk/src/widgets/podcast.rs b/hammond-gtk/src/widgets/podcast.rs index e696dec..9e9afd6 100644 --- a/hammond-gtk/src/widgets/podcast.rs +++ b/hammond-gtk/src/widgets/podcast.rs @@ -1,6 +1,5 @@ use gtk::prelude::*; use gtk; -use gdk_pixbuf::Pixbuf; use std::fs; @@ -10,6 +9,7 @@ use hammond_downloader::downloader; use widgets::episode::episodes_listbox; use views::podcasts::update_podcasts_view; +use utils::get_pixbuf_from_path; #[derive(Debug)] struct PodcastWidget { @@ -154,15 +154,6 @@ fn show_played_button(pd: &Podcast, played_button: >k::Button) { } } -pub fn get_pixbuf_from_path(pd: &Podcast) -> Option { - let img_path = downloader::cache_image(pd); - if let Some(i) = img_path { - Pixbuf::new_from_file_at_scale(&i, 256, 256, true).ok() - } else { - None - } -} - pub fn setup_podcast_widget(stack: >k::Stack) { let builder = gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/podcast_widget.ui"); let pd_widget: gtk::Box = builder.get_object("podcast_widget").unwrap(); @@ -180,30 +171,3 @@ pub fn update_podcast_widget(stack: >k::Stack, pd: &Podcast) { stack.set_visible_child_name(&vis); old.destroy(); } - -#[cfg(test)] -mod tests { - use hammond_data::Source; - use hammond_data::feed::index; - use diesel::Identifiable; - use super::*; - - #[test] - fn test_get_pixbuf_from_path() { - let url = "http://www.newrustacean.com/feed.xml"; - - // Create and index a source - let source = Source::from_url(url).unwrap(); - // Copy it's id - let sid = source.id().clone(); - - // Convert Source it into a Feed and index it - let feed = source.into_feed().unwrap(); - index(vec![feed]); - - // Get the Podcast - let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); - let pxbuf = get_pixbuf_from_path(&pd); - assert!(pxbuf.is_some()); - } -}