diff --git a/hammond-data/src/database.rs b/hammond-data/src/database.rs index bb18bda..00454a8 100644 --- a/hammond-data/src/database.rs +++ b/hammond-data/src/database.rs @@ -35,7 +35,8 @@ lazy_static! { static ref DB_PATH: PathBuf = TEMPDIR.path().join("hammond.db"); } -pub(crate) fn connection() -> Pool { +// FIXME: this should not be public +pub fn connection() -> Pool { POOL.clone() } diff --git a/hammond-data/src/lib.rs b/hammond-data/src/lib.rs index 21b70ac..f9ed95d 100644 --- a/hammond-data/src/lib.rs +++ b/hammond-data/src/lib.rs @@ -56,7 +56,9 @@ pub mod utils; pub mod feed; #[allow(missing_docs)] pub mod errors; -pub(crate) mod database; +// FIXME: this should not be public +#[allow(missing_docs)] +pub mod database; pub(crate) mod models; mod parser; mod schema; diff --git a/hammond-data/src/models/queryables.rs b/hammond-data/src/models/queryables.rs index d1a9cc0..99fc27c 100644 --- a/hammond-data/src/models/queryables.rs +++ b/hammond-data/src/models/queryables.rs @@ -215,6 +215,22 @@ pub struct EpisodeWidgetQuery { podcast_id: i32, } +impl From for EpisodeWidgetQuery { + fn from(e: Episode) -> EpisodeWidgetQuery { + EpisodeWidgetQuery { + rowid: e.rowid, + title: e.title, + uri: e.uri, + local_uri: e.local_uri, + epoch: e.epoch, + length: e.length, + duration: e.duration, + played: e.played, + podcast_id: e.podcast_id, + } + } +} + impl EpisodeWidgetQuery { /// Get the value of the sqlite's `ROW_ID` pub fn rowid(&self) -> i32 { diff --git a/hammond-downloader/src/downloader.rs b/hammond-downloader/src/downloader.rs index 14a205d..2f9e721 100644 --- a/hammond-downloader/src/downloader.rs +++ b/hammond-downloader/src/downloader.rs @@ -39,7 +39,7 @@ fn download_into(dir: &str, file_title: &str, url: &str) -> Result { ct_len.map(|x| info!("File Lenght: {}", x)); ct_type.map(|x| info!("Content Type: {}", x)); - let ext = get_ext(ct_type.cloned()).unwrap_or(String::from("unkown")); + let ext = get_ext(ct_type.cloned()).unwrap_or(String::from("unknown")); info!("Extension: {}", ext); // Construct a temp file to save desired content. diff --git a/hammond-downloader/src/lib.rs b/hammond-downloader/src/lib.rs index ef75fa5..31cc0b2 100644 --- a/hammond-downloader/src/lib.rs +++ b/hammond-downloader/src/lib.rs @@ -16,3 +16,4 @@ extern crate tempdir; pub mod downloader; pub mod errors; +pub mod manager; diff --git a/hammond-downloader/src/manager.rs b/hammond-downloader/src/manager.rs new file mode 100644 index 0000000..eeb4057 --- /dev/null +++ b/hammond-downloader/src/manager.rs @@ -0,0 +1,121 @@ +use hammond_data::Episode; +use hammond_data::dbqueries; + +use downloader::get_episode; + +use std::collections::HashSet; +use std::sync::{Arc, Mutex}; +use std::path::PathBuf; +use std::thread; + +struct DonwloadInstance { + uri: String, + // FIXME: MAKE ME A PATHBUF + local_uri: Option, + downloaded_bytes: u64, + total_bytes: u64, +} + +impl DonwloadInstance { + fn new(url: &str, total_bytes: u64) -> Self { + DonwloadInstance { + uri: url.into(), + local_uri: None, + downloaded_bytes: 0, + total_bytes, + } + } +} + +struct Manager { + active: Arc>>, +} + +impl Default for Manager { + fn default() -> Self { + Manager { + active: Arc::new(Mutex::new(HashSet::new())), + } + } +} + +impl Manager { + fn new() -> Self { + Manager::default() + } + + fn add(&self, id: i32, directory: &str) { + { + let mut m = self.active.lock().unwrap(); + m.insert(id); + } + + let dir = directory.to_owned(); + let list = self.active.clone(); + thread::spawn(move || { + let episode = dbqueries::get_episode_from_rowid(id).unwrap(); + let e = get_episode(&mut episode.into(), dir.as_str()); + if let Err(err) = e { + error!("Error: {}", err); + }; + + let mut m = list.lock().unwrap(); + m.remove(&id); + }); + } +} + +#[cfg(test)] +mod tests { + use super::*; + use downloader; + + use diesel::Identifiable; + + use hammond_data::database; + use hammond_data::feed::*; + use hammond_data::{Episode, Source}; + use hammond_data::dbqueries; + + use std::path::Path; + use std::{thread, time}; + + #[test] + // This test inserts an rss feed to your `XDG_DATA/hammond/hammond.db` so we make it explicit + // to run it. + #[ignore] + // THIS IS NOT A RELIABLE TEST + // Just quick sanity check + fn test_start_dl() { + let url = "http://www.newrustacean.com/feed.xml"; + + // Create and index a source + let source = Source::from_url(url).unwrap(); + // Copy it's id + let sid = source.id().clone(); + + // Convert Source it into a Feed and index it + let feed = source.into_feed(true).unwrap(); + index(&feed); + + // Get the Podcast + let pd = dbqueries::get_podcast_from_source_id(sid).unwrap(); + // Get an episode + let episode: Episode = { + let con = database::connection(); + dbqueries::get_episode_from_pk(&*con.get().unwrap(), "e000: Hello, world!", *pd.id()) + .unwrap() + }; + + let manager = Manager::new(); + let download_fold = downloader::get_download_folder(&pd.title()).unwrap(); + manager.add(episode.rowid(), download_fold.as_str()); + + // Give it soem time to download the file + thread::sleep(time::Duration::from_secs(20)); + + let final_path = format!("{}/{}.unknown", &download_fold, episode.rowid()); + println!("{}", &final_path); + assert!(Path::new(&final_path).exists()); + } +} diff --git a/hammond-gtk/src/widgets/episode.rs b/hammond-gtk/src/widgets/episode.rs index ae17d73..ce952be 100644 --- a/hammond-gtk/src/widgets/episode.rs +++ b/hammond-gtk/src/widgets/episode.rs @@ -179,7 +179,6 @@ impl EpisodeWidget { } } -// TODO: show notification when dl is finished. fn on_download_clicked( ep: &mut EpisodeWidgetQuery, download_bttn: >k::Button,