EpisodeWidget: Implement shared download state.
This commit is contained in:
parent
37e9b6fbf0
commit
c61d322569
@ -27,8 +27,10 @@ impl DonwloadInstance {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
struct Manager {
|
#[derive(Debug, Clone)]
|
||||||
active: Arc<Mutex<HashSet<i32>>>,
|
// FIXME: privacy stuff
|
||||||
|
pub struct Manager {
|
||||||
|
pub active: Arc<Mutex<HashSet<i32>>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Default for Manager {
|
impl Default for Manager {
|
||||||
@ -40,11 +42,11 @@ impl Default for Manager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Manager {
|
impl Manager {
|
||||||
fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
Manager::default()
|
Manager::default()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn add(&self, id: i32, directory: &str) {
|
pub fn add(&self, id: i32, directory: &str) {
|
||||||
{
|
{
|
||||||
let mut m = self.active.lock().unwrap();
|
let mut m = self.active.lock().unwrap();
|
||||||
m.insert(id);
|
m.insert(id);
|
||||||
|
|||||||
@ -5,12 +5,18 @@ use gtk::prelude::*;
|
|||||||
use gio::{ActionMapExt, ApplicationExt, ApplicationExtManual, SimpleActionExt};
|
use gio::{ActionMapExt, ApplicationExt, ApplicationExtManual, SimpleActionExt};
|
||||||
|
|
||||||
use hammond_data::utils::checkup;
|
use hammond_data::utils::checkup;
|
||||||
|
use hammond_downloader::manager::Manager;
|
||||||
|
|
||||||
use headerbar::Header;
|
use headerbar::Header;
|
||||||
use content::Content;
|
use content::Content;
|
||||||
use utils;
|
use utils;
|
||||||
|
|
||||||
use std::rc::Rc;
|
use std::rc::Rc;
|
||||||
|
use std::sync::{Arc, Mutex};
|
||||||
|
|
||||||
|
lazy_static! {
|
||||||
|
pub static ref DOWNLOADS_MANAGER: Arc<Mutex<Manager>> = Arc::new(Mutex::new(Manager::new()));
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct App {
|
pub struct App {
|
||||||
|
|||||||
@ -13,6 +13,8 @@ use hammond_data::{EpisodeWidgetQuery, Podcast};
|
|||||||
use hammond_data::errors::*;
|
use hammond_data::errors::*;
|
||||||
use hammond_downloader::downloader;
|
use hammond_downloader::downloader;
|
||||||
|
|
||||||
|
use app::DOWNLOADS_MANAGER;
|
||||||
|
|
||||||
use std::thread;
|
use std::thread;
|
||||||
use std::cell::RefCell;
|
use std::cell::RefCell;
|
||||||
use std::sync::mpsc::{channel, Receiver};
|
use std::sync::mpsc::{channel, Receiver};
|
||||||
@ -105,6 +107,14 @@ impl EpisodeWidget {
|
|||||||
.map(|c| c.add_class("dim-label"));
|
.map(|c| c.add_class("dim-label"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
let m = DOWNLOADS_MANAGER.lock().unwrap();
|
||||||
|
let list = m.active.lock().unwrap();
|
||||||
|
if list.contains(&episode.rowid()) {
|
||||||
|
self.show_progess_bar()
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
// Declare a custom humansize option struct
|
// Declare a custom humansize option struct
|
||||||
// See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html
|
// See: https://docs.rs/humansize/1.0.2/humansize/file_size_opts/struct.FileSizeOpts.html
|
||||||
let custom_options = size_opts::FileSizeOpts {
|
let custom_options = size_opts::FileSizeOpts {
|
||||||
@ -177,6 +187,16 @@ impl EpisodeWidget {
|
|||||||
);
|
);
|
||||||
}));
|
}));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn show_progess_bar(&self) {
|
||||||
|
let progress_bar = self.progress.clone();
|
||||||
|
timeout_add(200, move || {
|
||||||
|
progress_bar.pulse();
|
||||||
|
glib::Continue(true)
|
||||||
|
});
|
||||||
|
|
||||||
|
self.progress.show();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_download_clicked(
|
fn on_download_clicked(
|
||||||
@ -194,37 +214,18 @@ fn on_download_clicked(
|
|||||||
glib::Continue(true)
|
glib::Continue(true)
|
||||||
});
|
});
|
||||||
|
|
||||||
// Create a async channel.
|
|
||||||
let (sender, receiver) = channel();
|
|
||||||
|
|
||||||
// Pass the desired arguments into the Local Thread Storage.
|
|
||||||
GLOBAL.with(
|
|
||||||
clone!(download_bttn, play_bttn, cancel_bttn, progress => move |global| {
|
|
||||||
*global.borrow_mut() = Some((
|
|
||||||
download_bttn,
|
|
||||||
play_bttn,
|
|
||||||
cancel_bttn,
|
|
||||||
progress,
|
|
||||||
receiver));
|
|
||||||
}),
|
|
||||||
);
|
|
||||||
|
|
||||||
let pd = dbqueries::get_podcast_from_id(ep.podcast_id()).unwrap();
|
let pd = dbqueries::get_podcast_from_id(ep.podcast_id()).unwrap();
|
||||||
let pd_title = pd.title().to_owned();
|
let pd_title = pd.title().to_owned();
|
||||||
let mut ep = ep.clone();
|
let mut ep = ep.clone();
|
||||||
cancel_bttn.show();
|
cancel_bttn.show();
|
||||||
progress.show();
|
progress.show();
|
||||||
download_bttn.hide();
|
download_bttn.hide();
|
||||||
thread::spawn(move || {
|
let download_fold = downloader::get_download_folder(&pd_title).unwrap();
|
||||||
let download_fold = downloader::get_download_folder(&pd_title).unwrap();
|
{
|
||||||
let e = downloader::get_episode(&mut ep, download_fold.as_str());
|
let m = DOWNLOADS_MANAGER.clone();
|
||||||
if let Err(err) = e {
|
let man = m.lock().unwrap();
|
||||||
error!("Error while trying to download: {:?}", ep.uri());
|
man.add(ep.rowid(), &download_fold);
|
||||||
error!("Error: {}", err);
|
}
|
||||||
};
|
|
||||||
sender.send(true).expect("Couldn't send data to channel");;
|
|
||||||
glib::idle_add(receive);
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
fn on_play_bttn_clicked(episode_id: i32) {
|
fn on_play_bttn_clicked(episode_id: i32) {
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user