hammond-gtk: Change utils::get_pixbuf_from_path function to return a Result.
This commit is contained in:
parent
c6e426cbac
commit
7ed1cd8b26
@ -73,24 +73,25 @@ lazy_static! {
|
||||
// GObjects do not implement Send trait, so SendCell is a way around that.
|
||||
// Also lazy_static requires Sync trait, so that's what the mutexes are.
|
||||
// TODO: maybe use something that would just scale to requested size?
|
||||
pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Option<Pixbuf> {
|
||||
pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Result<Pixbuf, Error> {
|
||||
{
|
||||
let hashmap = CACHED_PIXBUFS.read().unwrap();
|
||||
let res = hashmap.get(&(pd.id(), size));
|
||||
if let Some(px) = res {
|
||||
let m = px.lock().unwrap();
|
||||
return Some(m.clone().into_inner());
|
||||
let hashmap = CACHED_PIXBUFS
|
||||
.read()
|
||||
.map_err(|_| format_err!("Failed to get a lock on the pixbuf cache mutex."))?;
|
||||
if let Some(px) = hashmap.get(&(pd.id(), size)) {
|
||||
let m = px.lock()
|
||||
.map_err(|_| format_err!("Failed to lock pixbuf mutex."))?;
|
||||
return Ok(m.clone().into_inner());
|
||||
}
|
||||
}
|
||||
|
||||
let img_path = downloader::cache_image(pd).ok()?;
|
||||
let px = Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true).ok();
|
||||
if let Some(px) = px {
|
||||
let mut hashmap = CACHED_PIXBUFS.write().unwrap();
|
||||
hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone())));
|
||||
return Some(px);
|
||||
}
|
||||
None
|
||||
let img_path = downloader::cache_image(pd)?;
|
||||
let px = Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true)?;
|
||||
let mut hashmap = CACHED_PIXBUFS
|
||||
.write()
|
||||
.map_err(|_| format_err!("Failed to lock pixbuf mutex."))?;
|
||||
hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone())));
|
||||
Ok(px)
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
@ -114,6 +115,6 @@ mod tests {
|
||||
// Get the Podcast
|
||||
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();
|
||||
let pxbuf = get_pixbuf_from_path(&pd.into(), 256);
|
||||
assert!(pxbuf.is_some());
|
||||
assert!(pxbuf.is_ok());
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,4 +1,5 @@
|
||||
use chrono::prelude::*;
|
||||
use failure::Error;
|
||||
use gtk;
|
||||
use gtk::prelude::*;
|
||||
|
||||
@ -202,18 +203,30 @@ impl EpisodesViewWidget {
|
||||
gtk::Builder::new_from_resource("/org/gnome/hammond/gtk/episodes_view_widget.ui");
|
||||
let container: gtk::Box = builder.get_object("container").unwrap();
|
||||
let image: gtk::Image = builder.get_object("cover").unwrap();
|
||||
|
||||
if let Ok(pd) = dbqueries::get_podcast_cover_from_id(episode.podcast_id()) {
|
||||
get_pixbuf_from_path(&pd, 64).map(|img| image.set_from_pixbuf(&img));
|
||||
}
|
||||
|
||||
let ep = EpisodeWidget::new(episode, sender.clone());
|
||||
container.pack_start(&ep.container, true, true, 6);
|
||||
|
||||
EpisodesViewWidget {
|
||||
let view = EpisodesViewWidget {
|
||||
container,
|
||||
image,
|
||||
episode: ep.container,
|
||||
};
|
||||
|
||||
view.init(episode);
|
||||
view
|
||||
}
|
||||
|
||||
fn init(&self, episode: &mut EpisodeWidgetQuery) {
|
||||
if let Err(err) = self.set_cover(episode) {
|
||||
error!("Failed to set a cover: {}", err)
|
||||
}
|
||||
|
||||
self.container.pack_start(&self.episode, true, true, 6);
|
||||
}
|
||||
|
||||
fn set_cover(&self, episode: &mut EpisodeWidgetQuery) -> Result<(), Error> {
|
||||
let pd = dbqueries::get_podcast_cover_from_id(episode.podcast_id())?;
|
||||
let img = get_pixbuf_from_path(&pd, 64)?;
|
||||
self.image.set_from_pixbuf(&img);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -1,3 +1,4 @@
|
||||
use failure::Error;
|
||||
use gtk;
|
||||
use gtk::prelude::*;
|
||||
|
||||
@ -114,11 +115,16 @@ impl ShowsChild {
|
||||
fn init(&self, pd: &Podcast) {
|
||||
self.container.set_tooltip_text(pd.title());
|
||||
|
||||
let cover = get_pixbuf_from_path(&pd.clone().into(), 256);
|
||||
if let Some(img) = cover {
|
||||
self.cover.set_from_pixbuf(&img);
|
||||
};
|
||||
if let Err(err) = self.set_cover(pd) {
|
||||
error!("Failed to set a cover: {}", err)
|
||||
}
|
||||
|
||||
WidgetExt::set_name(&self.child, &pd.id().to_string());
|
||||
}
|
||||
|
||||
fn set_cover(&self, pd: &Podcast) -> Result<(), Error> {
|
||||
let image = get_pixbuf_from_path(&pd.clone().into(), 256)?;
|
||||
self.cover.set_from_pixbuf(&image);
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
|
||||
@ -72,9 +72,12 @@ impl ShowWidget {
|
||||
}));
|
||||
|
||||
self.setup_listbox(pd, sender.clone());
|
||||
self.set_cover(pd);
|
||||
self.set_description(pd.description());
|
||||
|
||||
if let Err(err) = self.set_cover(pd) {
|
||||
error!("Failed to set a cover: {}", err)
|
||||
}
|
||||
|
||||
let link = pd.link().to_owned();
|
||||
self.link.set_tooltip_text(Some(link.as_str()));
|
||||
self.link.connect_clicked(move |_| {
|
||||
@ -91,11 +94,11 @@ impl ShowWidget {
|
||||
let listbox = episodes_listbox(pd, sender.clone());
|
||||
listbox.ok().map(|l| self.episodes.add(&l));
|
||||
}
|
||||
|
||||
/// Set the show cover.
|
||||
fn set_cover(&self, pd: &Podcast) {
|
||||
let img = get_pixbuf_from_path(&pd.clone().into(), 128);
|
||||
img.map(|i| self.cover.set_from_pixbuf(&i));
|
||||
fn set_cover(&self, pd: &Podcast) -> Result<(), Error> {
|
||||
let image = get_pixbuf_from_path(&pd.clone().into(), 128)?;
|
||||
self.cover.set_from_pixbuf(&image);
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Set the descripton text.
|
||||
|
||||
Loading…
Reference in New Issue
Block a user