diff --git a/Cargo.toml b/Cargo.toml index 9f4cb6a..a571876 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,8 +9,10 @@ edition = "2021" gtk = { version = "0.8.1", package = "gtk4", features = ["v4_8"] } rand = "0.8.5" +reqwest = "0.12.4" rusqlite = "0.31.0" sha256 = "1.5.0" +tokio = { version = "1.37.0", features = ["full"] } [build-dependencies] dirs = "5.0.1" diff --git a/src/dictionary.rs b/src/dictionary.rs index 891c2c7..a8c8fa9 100644 --- a/src/dictionary.rs +++ b/src/dictionary.rs @@ -1,8 +1,9 @@ use rusqlite::Connection; -use std::fs; -use std::io::ErrorKind; +use core::panic; +use std::fs::{self, File}; +use std::io::{ErrorKind, Write}; -use crate::db::get_db_path; +use crate::db::{get_db_path, get_program_home_path}; #[derive(PartialEq)] pub struct DictionaryEntry { @@ -40,6 +41,19 @@ fn get_sym_pos_or_panic(path: &String, line: &str, sym: &str, line_number: usize ), } } +#[tokio::main] +async fn download_dictionary(path: String) { + let link = "https://git.foxarmy.org/leca/learn-hieroglyphs/raw/branch/master/dictionary.txt"; + let response = reqwest::get(link).await; + + let mut file = match File::create(&path) { + Err(e) => panic!("Could not create dictionary file. {}", e), + Ok(file) => file + }; + let content = response.unwrap().bytes().await.unwrap(); + file.write_all(&content).unwrap(); + import(&path); +} fn read_and_parse(path: &String) -> Vec { let mut result: Vec = Vec::new(); @@ -47,7 +61,8 @@ fn read_and_parse(path: &String) -> Vec { Ok(string) => string, Err(kind) => match kind.kind() { ErrorKind::NotFound => { - println!("No dictionary file found. Assuming it's empty. Perhaps, you want to download it from my git? https://git.foxarmy.org/leca/learn-hieroglyphs"); + println!("No dictionary file found. Downloading it to {}/dictionary.txt from https://git.foxarmy.org/leca/learn-hieroglyphs", get_program_home_path()); + download_dictionary(path.to_owned()); return Vec::new(); } ErrorKind::PermissionDenied => { diff --git a/src/main.rs b/src/main.rs index 457c046..c62157b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,6 +10,7 @@ mod card_gobject; use crate::ui::menu::MenuScene; +use db::get_program_home_path; use gtk::glib::closure_local; use gtk::glib::subclass::types::ObjectSubclassIsExt; use gtk::{gio, glib, Application, Button}; @@ -38,7 +39,7 @@ fn main() -> glib::ExitCode { db::init(); // Create database if not exists, create program's home if not exists, etc... - dictionary::import(&String::from("./dictionary.txt")); // Read file, parse it, deduplicate and append to the database. + dictionary::import(&(get_program_home_path() + "/dictionary.txt")); // Read file, parse it, deduplicate and append to the database. let app: Application = Application::builder().application_id(APP_ID).build();