Episode widgets update upon download.

This commit is contained in:
Jordan Petridis
2017-10-21 08:05:00 +03:00
parent 5fb783c0c0
commit 86019710a1
4 changed files with 70 additions and 26 deletions
+3 -12
View File
@@ -3,9 +3,7 @@
use gtk;
use gtk::prelude::*;
use gtk::StackTransitionType;
use gdk_pixbuf::Pixbuf;
use hammond_downloader::downloader;
use diesel::prelude::SqliteConnection;
use std::sync::{Arc, Mutex};
@@ -44,22 +42,14 @@ pub fn populate_podcasts_flowbox(
let description = pd_model.get_value(&iter, 2).get::<String>();
let image_uri = pd_model.get_value(&iter, 4).get::<String>();
let imgpath = downloader::cache_image(&title, image_uri.as_ref().map(|s| s.as_str()));
let pixbuf = if let Some(i) = imgpath {
Pixbuf::new_from_file_at_scale(&i, 200, 200, true).ok()
} else {
None
};
let pixbuf = get_pixbuf_from_path(image_uri.as_ref().map(|s| s.as_str()), &title);
let f = create_flowbox_child(&title, pixbuf.clone());
let stack_clone = stack.clone();
let db_clone = db.clone();
f.connect_activate(move |_| {
let pdw = stack_clone.get_child_by_name("pdw").unwrap();
stack_clone.remove(&pdw);
let old = stack_clone.get_child_by_name("pdw").unwrap();
let pdw = podcast_widget(
&db_clone.clone(),
Some(title.as_str()),
@@ -67,6 +57,7 @@ pub fn populate_podcasts_flowbox(
pixbuf.clone(),
);
stack_clone.remove(&old);
stack_clone.add_named(&pdw, "pdw");
stack_clone.set_visible_child(&pdw);
println!("Hello World!, child activated");
+23 -13
View File
@@ -13,9 +13,11 @@ use dissolve::strip_html_tags;
use std::thread;
use std::sync::{Arc, Mutex};
use std::path::Path;
use gtk;
use gtk::prelude::*;
use gtk::ContainerExt;
// use utils;
@@ -55,36 +57,44 @@ fn epidose_widget(
}
if episode.local_uri().is_some() {
dl_button.hide();
play_button.show();
let uri = episode.local_uri().unwrap().to_owned();
play_button.connect_clicked(move |_| {
let e = open::that(&uri);
if e.is_err() {
error!("Error while trying to open: {}", uri);
}
});
if Path::new(&uri).exists() {
dl_button.hide();
play_button.show();
play_button.connect_clicked(move |_| {
let e = open::that(&uri);
if e.is_err() {
error!("Error while trying to open: {}", uri);
}
});
}
}
let pd_title_cloned = pd_title.to_owned();
let db = connection.clone();
let ep_clone = episode.clone();
dl_button.connect_clicked(move |_| {
let play_button_clone = play_button.clone();
dl_button.connect_clicked(move |dl| {
// ugly hack to bypass the borrowchecker
let pd_title = pd_title_cloned.clone();
let db = db.clone();
let db_clone = db.clone();
let mut ep_clone = ep_clone.clone();
// TODO: emit a signal and show notification when dl is finished and block play_bttn till
// then.
thread::spawn(move || {
let dl_fold = downloader::get_dl_folder(&pd_title).unwrap();
let tempdb = db.lock().unwrap();
let dl_fold = downloader::get_dl_folder(&pd_title.clone()).unwrap();
let tempdb = db_clone.lock().unwrap();
let e = downloader::get_episode(&tempdb, &mut ep_clone, dl_fold.as_str());
drop(tempdb);
if let Err(err) = e {
error!("Error while trying to download: {}", ep_clone.uri());
error!("Error: {}", err);
};
// TODO: emit a signal in order to update the podcast widget.
});
dl.hide();
play_button_clone.show();
});
ep
+27
View File
@@ -4,6 +4,8 @@ use gdk_pixbuf::Pixbuf;
use diesel::prelude::SqliteConnection;
use hammond_data::dbqueries;
use hammond_data::models::Podcast;
use hammond_downloader::downloader;
use std::sync::{Arc, Mutex};
@@ -70,6 +72,7 @@ pub fn create_flowbox_child(title: &str, cover: Option<Pixbuf>) -> gtk::FlowBoxC
fbc
}
// Figure if its better to completly ditch stores and just create the views from diesel models.
pub fn podcast_liststore(connection: &SqliteConnection) -> gtk::ListStore {
let builder = include_str!("../../gtk/podcast_widget.ui");
let builder = gtk::Builder::new_from_string(builder);
@@ -94,3 +97,27 @@ pub fn podcast_liststore(connection: &SqliteConnection) -> gtk::ListStore {
podcast_model
}
// pub fn update_podcast_widget(db: &Arc<Mutex<SqliteConnection>>, stack: &gtk::Stack, pd:
// &Podcast){
// let old = stack.get_child_by_name("pdw").unwrap();
// let pdw = pd_widget_from_diesel_model(&db.clone(), pd, &stack.clone());
// stack.remove(&old);
// stack.add_named(&pdw, "pdw");
// stack.set_visible_child_full("pdw", StackTransitionType::None);
// }
pub fn pd_widget_from_diesel_model(db: &Arc<Mutex<SqliteConnection>>, pd: &Podcast) -> gtk::Box {
let img = get_pixbuf_from_path(pd.image_uri(), pd.title());
podcast_widget(&db.clone(), Some(pd.title()), Some(pd.description()), img)
}
pub fn get_pixbuf_from_path(img_path: Option<&str>, pd_title: &str) -> Option<Pixbuf> {
let img_path = downloader::cache_image(pd_title, img_path);
if let Some(i) = img_path {
Pixbuf::new_from_file_at_scale(&i, 200, 200, true).ok()
} else {
None
}
}