Define a new Diesel Model for PodcastCover querries.
Define new Diesel Model and impl From<Podcast> trait, Change the signature of downloader::cache_image function, Change and merge hammond-gtk::utils::get_pixbuf_from_path functions.
This commit is contained in:
parent
c070fc3032
commit
2e06205eda
@ -2,7 +2,7 @@
|
|||||||
|
|
||||||
use diesel::prelude::*;
|
use diesel::prelude::*;
|
||||||
use diesel;
|
use diesel;
|
||||||
use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source};
|
use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source};
|
||||||
use chrono::prelude::*;
|
use chrono::prelude::*;
|
||||||
use errors::*;
|
use errors::*;
|
||||||
|
|
||||||
@ -114,6 +114,17 @@ pub fn get_podcast_from_id(pid: i32) -> Result<Podcast> {
|
|||||||
Ok(podcast.filter(id.eq(pid)).get_result::<Podcast>(&*con)?)
|
Ok(podcast.filter(id.eq(pid)).get_result::<Podcast>(&*con)?)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn get_podcast_cover_from_id(pid: i32) -> Result<PodcastCoverQuery> {
|
||||||
|
use schema::podcast::dsl::*;
|
||||||
|
|
||||||
|
let db = connection();
|
||||||
|
let con = db.get()?;
|
||||||
|
Ok(podcast
|
||||||
|
.select((id, title, image_uri))
|
||||||
|
.filter(id.eq(pid))
|
||||||
|
.get_result::<PodcastCoverQuery>(&*con)?)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
pub fn get_pd_episodes(parent: &Podcast) -> Result<Vec<Episode>> {
|
||||||
use schema::episode::dsl::*;
|
use schema::episode::dsl::*;
|
||||||
|
|
||||||
|
|||||||
@ -61,7 +61,7 @@ pub(crate) mod models;
|
|||||||
mod parser;
|
mod parser;
|
||||||
mod schema;
|
mod schema;
|
||||||
|
|
||||||
pub use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, Source};
|
pub use models::queryables::{Episode, EpisodeWidgetQuery, Podcast, PodcastCoverQuery, Source};
|
||||||
|
|
||||||
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.
|
/// [XDG Base Direcotory](https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html) Paths.
|
||||||
#[allow(missing_debug_implementations)]
|
#[allow(missing_debug_implementations)]
|
||||||
|
|||||||
@ -427,6 +427,39 @@ impl Podcast {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Queryable, Debug, Clone)]
|
||||||
|
/// Diesel Model of the podcast cover query.
|
||||||
|
/// Used for fetching information about a Podcast's cover.
|
||||||
|
pub struct PodcastCoverQuery {
|
||||||
|
id: i32,
|
||||||
|
title: String,
|
||||||
|
image_uri: Option<String>,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl From<Podcast> for PodcastCoverQuery {
|
||||||
|
fn from(p: Podcast) -> PodcastCoverQuery {
|
||||||
|
PodcastCoverQuery {
|
||||||
|
id: *p.id(),
|
||||||
|
title: p.title,
|
||||||
|
image_uri: p.image_uri,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl PodcastCoverQuery {
|
||||||
|
/// Get the Feed `title`.
|
||||||
|
pub fn title(&self) -> &str {
|
||||||
|
&self.title
|
||||||
|
}
|
||||||
|
|
||||||
|
/// Get the `image_uri`.
|
||||||
|
///
|
||||||
|
/// Represents the uri(url usually) that the Feed cover image is located at.
|
||||||
|
pub fn image_uri(&self) -> Option<&str> {
|
||||||
|
self.image_uri.as_ref().map(|s| s.as_str())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Queryable, Identifiable, AsChangeset, PartialEq)]
|
#[derive(Queryable, Identifiable, AsChangeset, PartialEq)]
|
||||||
#[table_name = "source"]
|
#[table_name = "source"]
|
||||||
#[changeset_options(treat_none_as_null = "true")]
|
#[changeset_options(treat_none_as_null = "true")]
|
||||||
|
|||||||
@ -8,7 +8,7 @@ use std::io::{BufWriter, Read, Write};
|
|||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
use errors::*;
|
use errors::*;
|
||||||
use hammond_data::{EpisodeWidgetQuery, Podcast};
|
use hammond_data::{EpisodeWidgetQuery, PodcastCoverQuery};
|
||||||
use hammond_data::xdg_dirs::{DL_DIR, HAMMOND_CACHE};
|
use hammond_data::xdg_dirs::{DL_DIR, HAMMOND_CACHE};
|
||||||
|
|
||||||
// TODO: Replace path that are of type &str with std::path.
|
// TODO: Replace path that are of type &str with std::path.
|
||||||
@ -131,7 +131,7 @@ pub fn get_episode(ep: &mut EpisodeWidgetQuery, download_folder: &str) -> Result
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn cache_image(pd: &Podcast) -> Option<String> {
|
pub fn cache_image(pd: &PodcastCoverQuery) -> Option<String> {
|
||||||
let url = pd.image_uri()?.to_owned();
|
let url = pd.image_uri()?.to_owned();
|
||||||
if url == "" {
|
if url == "" {
|
||||||
return None;
|
return None;
|
||||||
@ -207,7 +207,7 @@ mod tests {
|
|||||||
index(vec![feed]);
|
index(vec![feed]);
|
||||||
|
|
||||||
// Get the Podcast
|
// Get the Podcast
|
||||||
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();
|
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap().into();
|
||||||
|
|
||||||
let img_path = cache_image(&pd);
|
let img_path = cache_image(&pd);
|
||||||
let foo_ = format!(
|
let foo_ = format!(
|
||||||
|
|||||||
@ -2,7 +2,7 @@ use glib;
|
|||||||
use gdk_pixbuf::Pixbuf;
|
use gdk_pixbuf::Pixbuf;
|
||||||
|
|
||||||
use hammond_data::feed;
|
use hammond_data::feed;
|
||||||
use hammond_data::{Podcast, Source};
|
use hammond_data::{PodcastCoverQuery, Source};
|
||||||
use hammond_downloader::downloader;
|
use hammond_downloader::downloader;
|
||||||
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
@ -60,21 +60,9 @@ fn refresh_podcasts_view() -> glib::Continue {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// FIXME: use something that would just scale?
|
// FIXME: use something that would just scale?
|
||||||
// TODO: make a diesel model with only title, local_uri
|
pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Option<Pixbuf> {
|
||||||
|
|
||||||
pub fn get_pixbuf_from_path(pd: &Podcast) -> Option<Pixbuf> {
|
|
||||||
let img_path = downloader::cache_image(pd)?;
|
let img_path = downloader::cache_image(pd)?;
|
||||||
Pixbuf::new_from_file_at_scale(&img_path, 256, 256, true).ok()
|
Pixbuf::new_from_file_at_scale(&img_path, size as i32, size as i32, true).ok()
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_pixbuf_from_path_128(pd: &Podcast) -> Option<Pixbuf> {
|
|
||||||
let img_path = downloader::cache_image(pd)?;
|
|
||||||
Pixbuf::new_from_file_at_scale(&img_path, 128, 128, true).ok()
|
|
||||||
}
|
|
||||||
|
|
||||||
pub fn get_pixbuf_from_path_64(pd: &Podcast) -> Option<Pixbuf> {
|
|
||||||
let img_path = downloader::cache_image(pd)?;
|
|
||||||
Pixbuf::new_from_file_at_scale(&img_path, 64, 64, true).ok()
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
@ -100,7 +88,7 @@ mod tests {
|
|||||||
|
|
||||||
// Get the Podcast
|
// Get the Podcast
|
||||||
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();
|
let pd = dbqueries::get_podcast_from_source_id(sid).unwrap();
|
||||||
let pxbuf = get_pixbuf_from_path(&pd);
|
let pxbuf = get_pixbuf_from_path(&pd.into(), 256);
|
||||||
assert!(pxbuf.is_some());
|
assert!(pxbuf.is_some());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -6,7 +6,7 @@ use hammond_data::dbqueries;
|
|||||||
use hammond_data::EpisodeWidgetQuery;
|
use hammond_data::EpisodeWidgetQuery;
|
||||||
|
|
||||||
use widgets::episode::EpisodeWidget;
|
use widgets::episode::EpisodeWidget;
|
||||||
use utils::get_pixbuf_from_path_64;
|
use utils::get_pixbuf_from_path;
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
|
||||||
@ -216,8 +216,8 @@ impl EpisodesViewWidget {
|
|||||||
let image: gtk::Image = builder.get_object("cover").unwrap();
|
let image: gtk::Image = builder.get_object("cover").unwrap();
|
||||||
|
|
||||||
// FIXME:
|
// FIXME:
|
||||||
if let Ok(pd) = dbqueries::get_podcast_from_id(episode.podcast_id()) {
|
if let Ok(pd) = dbqueries::get_podcast_cover_from_id(episode.podcast_id()) {
|
||||||
let img = get_pixbuf_from_path_64(&pd);
|
let img = get_pixbuf_from_path(&pd, 64);
|
||||||
if let Some(i) = img {
|
if let Some(i) = img {
|
||||||
image.set_from_pixbuf(&i);
|
image.set_from_pixbuf(&i);
|
||||||
}
|
}
|
||||||
|
|||||||
@ -111,7 +111,7 @@ impl ShowsChild {
|
|||||||
fn init(&self, pd: &Podcast) {
|
fn init(&self, pd: &Podcast) {
|
||||||
self.container.set_tooltip_text(pd.title());
|
self.container.set_tooltip_text(pd.title());
|
||||||
|
|
||||||
let cover = get_pixbuf_from_path(pd);
|
let cover = get_pixbuf_from_path(&pd.clone().into(), 256);
|
||||||
if let Some(img) = cover {
|
if let Some(img) = cover {
|
||||||
self.cover.set_from_pixbuf(&img);
|
self.cover.set_from_pixbuf(&img);
|
||||||
};
|
};
|
||||||
|
|||||||
@ -10,7 +10,7 @@ use hammond_data::utils::replace_extra_spaces;
|
|||||||
use hammond_downloader::downloader;
|
use hammond_downloader::downloader;
|
||||||
|
|
||||||
use widgets::episode::episodes_listbox;
|
use widgets::episode::episodes_listbox;
|
||||||
use utils::get_pixbuf_from_path_128;
|
use utils::get_pixbuf_from_path;
|
||||||
use content::ShowStack;
|
use content::ShowStack;
|
||||||
use headerbar::Header;
|
use headerbar::Header;
|
||||||
|
|
||||||
@ -77,7 +77,7 @@ impl ShowWidget {
|
|||||||
let desc = dissolve::strip_html_tags(pd.description()).join(" ");
|
let desc = dissolve::strip_html_tags(pd.description()).join(" ");
|
||||||
self.description.set_text(&replace_extra_spaces(&desc));
|
self.description.set_text(&replace_extra_spaces(&desc));
|
||||||
|
|
||||||
let img = get_pixbuf_from_path_128(pd);
|
let img = get_pixbuf_from_path(&pd.clone().into(), 128);
|
||||||
if let Some(i) = img {
|
if let Some(i) = img {
|
||||||
self.cover.set_from_pixbuf(&i);
|
self.cover.set_from_pixbuf(&i);
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user