h-gtk: Change the signature of utils::set_image_from_path to not require a Podcast.

It was only used to call the podcast.id() method. This allows EpisodeViewWidget
to be created whithout the need for a call to the database to get a Podcst
Object for each widget.
This commit is contained in:
Jordan Petridis 2018-04-19 07:15:12 +03:00
parent df417fa619
commit 5336981154
No known key found for this signature in database
GPG Key ID: CEABAD9F5683B9A6
4 changed files with 24 additions and 29 deletions

View File

@ -18,7 +18,7 @@ use serde_json::Value;
use hammond_data::dbqueries;
use hammond_data::pipeline;
use hammond_data::utils::checkup;
use hammond_data::{PodcastCoverQuery, Source};
use hammond_data::Source;
use hammond_downloader::downloader;
use std::collections::{HashMap, HashSet};
@ -212,19 +212,15 @@ lazy_static! {
// Also lazy_static requires Sync trait, so that's what the mutexes are.
// TODO: maybe use something that would just scale to requested size?
#[inline]
pub fn set_image_from_path(
image: &gtk::Image,
pd: Arc<PodcastCoverQuery>,
size: u32,
) -> Result<(), Error> {
pub fn set_image_from_path(image: &gtk::Image, podcast_id: i32, size: u32) -> Result<(), Error> {
// Check if there's an active download about this show cover.
// If there is, a callback will be set so this function will be called again.
// If the download succedes, there should be a quick return from the pixbuf cache_image
// If it fails another download will be scheduled.
if let Ok(guard) = COVER_DL_REGISTRY.read() {
if guard.contains(&pd.id()) {
let callback = clone!(image, pd => move || {
let _ = set_image_from_path(&image, pd.clone(), size);
if guard.contains(&podcast_id) {
let callback = clone!(image => move || {
let _ = set_image_from_path(&image, podcast_id, size);
glib::Continue(false)
});
gtk::timeout_add(250, callback);
@ -235,7 +231,7 @@ pub fn set_image_from_path(
if let Ok(hashmap) = CACHED_PIXBUFS.read() {
// Check if the requested (cover + size) is already in the chache
// and if so do an early return after that.
if let Some(guard) = hashmap.get(&(pd.id(), size)) {
if let Some(guard) = hashmap.get(&(podcast_id, size)) {
guard
.lock()
.map_err(|err| format_err!("SendCell Mutex: {}", err))
@ -251,19 +247,20 @@ pub fn set_image_from_path(
}
let (sender, receiver) = channel();
let pd_ = pd.clone();
THREADPOOL.spawn(move || {
if let Ok(mut guard) = COVER_DL_REGISTRY.write() {
guard.insert(pd_.id());
guard.insert(podcast_id);
}
sender
.send(downloader::cache_image(&pd_))
.map_err(|err| error!("Action Sender: {}", err))
.ok();
if let Ok(pd) = dbqueries::get_podcast_cover_from_id(podcast_id) {
sender
.send(downloader::cache_image(&pd))
.map_err(|err| error!("Action Sender: {}", err))
.ok();
}
if let Ok(mut guard) = COVER_DL_REGISTRY.write() {
guard.remove(&pd_.id());
guard.remove(&podcast_id);
}
});
@ -274,7 +271,7 @@ pub fn set_image_from_path(
if let Ok(path) = path {
if let Ok(px) = Pixbuf::new_from_file_at_scale(&path, s, s, true) {
if let Ok(mut hashmap) = CACHED_PIXBUFS.write() {
hashmap.insert((pd.id(), size), Mutex::new(SendCell::new(px.clone())));
hashmap.insert((podcast_id, size), Mutex::new(SendCell::new(px.clone())));
image.set_from_pixbuf(&px);
}
}

View File

@ -13,7 +13,6 @@ use widgets::EpisodeWidget;
use std::rc::Rc;
use std::sync::mpsc::Sender;
use std::sync::Arc;
#[derive(Debug, Clone)]
enum ListSplit {
@ -213,7 +212,6 @@ impl EpisodesViewWidget {
#[inline]
fn set_cover(&self, podcast_id: i32) -> Result<(), Error> {
let pd = Arc::new(dbqueries::get_podcast_cover_from_id(podcast_id)?);
set_image_from_path(&self.image, pd, 64)
set_image_from_path(&self.image, podcast_id, 64)
}
}

View File

@ -3,7 +3,7 @@ use gtk;
use gtk::prelude::*;
use hammond_data::dbqueries;
use hammond_data::{Podcast, PodcastCoverQuery};
use hammond_data::Podcast;
use app::Action;
use utils::{get_ignored_shows, set_image_from_path};
@ -58,7 +58,7 @@ impl ShowsPopulated {
let ignore = get_ignored_shows()?;
let podcasts = dbqueries::get_podcasts_filter(&ignore)?;
podcasts.into_iter().for_each(|parent| {
podcasts.iter().for_each(|parent| {
let flowbox_child = ShowsChild::new(parent);
self.flowbox.add(&flowbox_child.child);
});
@ -122,24 +122,24 @@ impl Default for ShowsChild {
impl ShowsChild {
#[inline]
pub fn new(pd: Podcast) -> ShowsChild {
pub fn new(pd: &Podcast) -> ShowsChild {
let child = ShowsChild::default();
child.init(pd);
child
}
#[inline]
fn init(&self, pd: Podcast) {
fn init(&self, pd: &Podcast) {
self.container.set_tooltip_text(pd.title());
WidgetExt::set_name(&self.child, &pd.id().to_string());
self.set_cover(Arc::new(pd.into()))
self.set_cover(pd.id())
.map_err(|err| error!("Failed to set a cover: {}", err))
.ok();
}
#[inline]
fn set_cover(&self, pd: Arc<PodcastCoverQuery>) -> Result<(), Error> {
set_image_from_path(&self.cover, pd, 256)
fn set_cover(&self, podcast_id: i32) -> Result<(), Error> {
set_image_from_path(&self.cover, podcast_id, 256)
}
}

View File

@ -115,7 +115,7 @@ impl ShowWidget {
#[inline]
/// Set the show cover.
fn set_cover(&self, pd: Arc<Podcast>) -> Result<(), Error> {
utils::set_image_from_path(&self.cover, Arc::new(pd.into()), 128)
utils::set_image_from_path(&self.cover, pd.id(), 128)
}
#[inline]