Merge branch 'master' into 33-downloader-re-work

This commit is contained in:
Jordan Petridis
2018-01-09 12:06:38 +02:00
11 changed files with 171 additions and 75 deletions
+23 -14
View File
@@ -174,21 +174,30 @@ impl ShowStack {
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&old));
let scrolled_window = old.get_children()
.first()
// This is guaranted to exist based on the show_widget.ui file.
.unwrap()
.clone()
.downcast::<gtk::ScrolledWindow>()
// This is guaranted based on the show_widget.ui file.
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
let new = ShowWidget::new(Arc::new(self.clone()), pd, self.sender.clone());
// Copy the vertical scrollbar adjustment from the old view into the new one.
scrolled_window
.get_vadjustment()
.map(|x| new.set_vadjustment(&x));
// Each composite ShowWidget is a gtkBox with the Podcast.id encoded in the gtk::Widget
// name. It's a hack since we can't yet subclass GObject easily.
let oldid = WidgetExt::get_name(&old);
let newid = WidgetExt::get_name(&new.container);
debug!("Old widget Name: {:?}\nNew widget Name: {:?}", oldid, newid);
// Only copy the old scrollbar if both widget's represent the same podcast.
if newid == oldid {
let scrolled_window = old.get_children()
.first()
// This is guaranted to exist based on the show_widget.ui file.
.unwrap()
.clone()
.downcast::<gtk::ScrolledWindow>()
// This is guaranted based on the show_widget.ui file.
.unwrap();
debug!("Name: {:?}", WidgetExt::get_name(&scrolled_window));
// Copy the vertical scrollbar adjustment from the old view into the new one.
scrolled_window
.get_vadjustment()
.map(|x| new.set_vadjustment(&x));
}
self.stack.remove(&old);
self.stack.add_named(&new.container, "widget");
+2 -3
View File
@@ -106,11 +106,10 @@ pub fn add(id: i32, directory: &str, sender: Sender<Action>) {
#[cfg(test)]
mod tests {
use super::*;
use hammond_downloader::downloader;
use diesel::Identifiable;
use hammond_data::database;
use hammond_data::utils::get_download_folder;
use hammond_data::feed::*;
use hammond_data::{Episode, Source};
use hammond_data::dbqueries;
@@ -148,7 +147,7 @@ mod tests {
let (sender, _rx) = channel();
let download_fold = downloader::get_download_folder(&pd.title()).unwrap();
let download_fold = get_download_folder(&pd.title()).unwrap();
add(episode.rowid(), download_fold.as_str(), sender);
// Give it soem time to download the file
+8 -9
View File
@@ -7,7 +7,7 @@ use hammond_downloader::downloader;
use std::thread;
use std::sync::mpsc::Sender;
use std::sync::{Arc, Mutex};
use std::sync::{Arc, Mutex, RwLock};
use std::collections::HashMap;
use headerbar::Header;
@@ -22,11 +22,9 @@ pub fn refresh_feed(headerbar: Arc<Header>, source: Option<Vec<Source>>, sender:
thread::spawn(move || {
if let Some(s) = source {
feed::index_loop(s);
} else {
if let Err(err) = feed::index_all() {
error!("Error While trying to update the database.");
error!("Error msg: {}", err);
}
} else if let Err(err) = feed::index_all() {
error!("Error While trying to update the database.");
error!("Error msg: {}", err);
};
sender.send(Action::HeaderBarHideUpdateIndicator).unwrap();
@@ -35,8 +33,8 @@ pub fn refresh_feed(headerbar: Arc<Header>, source: Option<Vec<Source>>, sender:
}
lazy_static! {
static ref CACHED_PIXBUFS: Mutex<HashMap<(i32, u32), Mutex<SendCell<Pixbuf>>>> = {
Mutex::new(HashMap::new())
static ref CACHED_PIXBUFS: RwLock<HashMap<(i32, u32), Mutex<SendCell<Pixbuf>>>> = {
RwLock::new(HashMap::new())
};
}
@@ -48,8 +46,8 @@ 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?
pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Option<Pixbuf> {
let mut hashmap = CACHED_PIXBUFS.lock().unwrap();
{
let hashmap = CACHED_PIXBUFS.read().unwrap();
let res = hashmap.get(&(pd.id(), size));
if let Some(px) = res {
let m = px.lock().unwrap();
@@ -60,6 +58,7 @@ pub fn get_pixbuf_from_path(pd: &PodcastCoverQuery, size: u32) -> Option<Pixbuf>
let img_path = downloader::cache_image(pd)?;
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);
}
+13 -7
View File
@@ -9,9 +9,8 @@ use humansize::{file_size_opts as size_opts, FileSize};
use hammond_data::dbqueries;
use hammond_data::{EpisodeWidgetQuery, Podcast};
// use hammond_data::utils::*;
use hammond_data::utils::get_download_folder;
use hammond_data::errors::*;
use hammond_downloader::downloader;
use app::Action;
use manager;
@@ -94,6 +93,10 @@ impl Default for EpisodeWidget {
}
}
lazy_static! {
static ref NOW: DateTime<Utc> = Utc::now();
}
impl EpisodeWidget {
pub fn new(episode: &mut EpisodeWidgetQuery, sender: Sender<Action>) -> EpisodeWidget {
let widget = EpisodeWidget::default();
@@ -166,18 +169,21 @@ impl EpisodeWidget {
/// Set the date label depending on the current time.
fn set_date(&self, epoch: i32) {
let now = Utc::now();
let date = Utc.timestamp(i64::from(epoch), 0);
if now.year() == date.year() {
self.date.set_text(&date.format("%e %b").to_string().trim());
if NOW.year() == date.year() {
self.date.set_text(date.format("%e %b").to_string().trim());
} else {
self.date
.set_text(&date.format("%e %b %Y").to_string().trim());
.set_text(date.format("%e %b %Y").to_string().trim());
};
}
/// Set the duration label.
fn set_duration(&self, seconds: Option<i32>) {
if (seconds == Some(0)) || seconds.is_none() {
return;
};
if let Some(secs) = seconds {
self.duration.set_text(&format!("{} min", secs / 60));
self.duration.show();
@@ -241,7 +247,7 @@ impl EpisodeWidget {
fn on_download_clicked(ep: &EpisodeWidgetQuery, sender: Sender<Action>) {
let download_fold = dbqueries::get_podcast_from_id(ep.podcast_id())
.ok()
.map(|pd| downloader::get_download_folder(&pd.title().to_owned()).ok())
.map(|pd| get_download_folder(&pd.title().to_owned()).ok())
.and_then(|x| x);
// Start a new download.
+5 -14
View File
@@ -6,8 +6,7 @@ use dissolve;
use hammond_data::dbqueries;
use hammond_data::Podcast;
use hammond_data::utils::replace_extra_spaces;
use hammond_downloader::downloader;
use hammond_data::utils::{delete_show, replace_extra_spaces};
use widgets::episode::episodes_listbox;
use utils::get_pixbuf_from_path;
@@ -17,7 +16,6 @@ use app::Action;
use std::sync::mpsc::Sender;
use std::sync::Arc;
use std::thread;
use std::fs;
#[derive(Debug, Clone)]
pub struct ShowWidget {
@@ -123,17 +121,10 @@ fn on_unsub_button_clicked(
unsub_button.hide();
// Spawn a thread so it won't block the ui.
thread::spawn(clone!(pd => move || {
dbqueries::remove_feed(&pd).ok().map(|_| {
info!("{} was removed succesfully.", pd.title());
downloader::get_download_folder(pd.title()).ok().map(|fold| {
let res3 = fs::remove_dir_all(&fold);
// TODO: Show errors?
if res3.is_ok() {
info!("All the content at, {} was removed succesfully", &fold);
}
});
});
if let Err(err) = delete_show(&pd) {
error!("Something went wrong trying to remove {}", pd.title());
error!("Error: {}", err);
}
}));
shows.switch_podcasts_animated();
sender.send(Action::HeaderBarNormal).unwrap();