diff --git a/Cargo.toml b/Cargo.toml
index b3f78be..3b57399 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -9,7 +9,8 @@ edition = "2021"
gtk = { version = "0.8.1", package = "gtk4", features = ["v4_12"] }
rand = "0.8.5"
-sqlite = "0.34.0"
+rusqlite = "0.31.0"
+sha256 = "1.5.0"
[build-dependencies]
dirs = "5.0.1"
diff --git a/resources/cards/new/ui.xml b/resources/cards/new/ui.xml
index b78ad3b..fe87726 100644
--- a/resources/cards/new/ui.xml
+++ b/resources/cards/new/ui.xml
@@ -31,10 +31,10 @@
-
diff --git a/src/db.rs b/src/db.rs
index c494340..9277871 100644
--- a/src/db.rs
+++ b/src/db.rs
@@ -1,5 +1,8 @@
-use sqlite;
-use std::{path::PathBuf, process::Command};
+use rusqlite::Connection;
+
+use std::{sync::RwLock, path::PathBuf, process::Command};
+
+pub static PROGRAM_HOME_PATH: RwLock = RwLock::new(String::new());
pub fn init() {
let program_home_path = if cfg!(target_os = "windows") {
@@ -19,15 +22,21 @@ pub fn init() {
home
};
+ let mut p = PROGRAM_HOME_PATH.write().unwrap();
+ *p = program_home_path.as_path().to_str().unwrap().to_string();
println!("Program's home: {}", program_home_path.as_path().to_str().unwrap());
- let connection = sqlite::open(program_home_path.join("db.sqlite")).unwrap();
+ let connection = Connection::open(program_home_path.join("db.sqlite")).unwrap();
connection.execute("CREATE TABLE IF NOT EXISTS cards (
id INTEGER PRIMARY KEY AUTOINCREMENT,
image_hash CHAR(64),
hieroglyph VARCHAR(2),
translation VARCHAR(128)
- )").unwrap();
+ )",(),).unwrap();
}
+
+pub fn get_program_home_path() -> &'static RwLock {
+ &PROGRAM_HOME_PATH
+}
\ No newline at end of file
diff --git a/src/ui/cards/edit/imp.rs b/src/ui/cards/edit/imp.rs
index e8bf643..6bd6551 100644
--- a/src/ui/cards/edit/imp.rs
+++ b/src/ui/cards/edit/imp.rs
@@ -1,15 +1,16 @@
+use rusqlite::Connection;
+use std::path::{Path, PathBuf};
use std::rc::Rc;
+use crate::ui::cards::new::*;
+use crate::widgets::card_entry::CardEntry;
use glib::subclass::InitializingObject;
use gtk::glib::object::ObjectExt;
use gtk::glib::{clone, closure_local};
use gtk::subclass::prelude::*;
use gtk::{glib, Button, CompositeTemplate, ScrolledWindow, SearchEntry, Window};
use gtk::{prelude::*, FileDialog};
-use sqlite;
-
-use crate::ui::cards::new::*;
-use crate::widgets::card_entry::CardEntry;
+use sha256::try_digest;
#[derive(CompositeTemplate, Default)]
#[template(resource = "/org/foxarmy/learn-hieroglyph/cards/edit/ui.xml")]
@@ -66,13 +67,24 @@ async fn file_choose_dialog>(window: Rc) {
let dialog: FileDialog = gtk::FileDialog::builder().build();
let answer = dialog.open_future(Some(&*window)).await;
let path = answer.unwrap().path().unwrap();
- let path = path.as_path().to_str();
- let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref()).downcast_ref().unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
- w.get_image_widget().set_file(path);
+ let path: &str = path.as_path().to_str().unwrap();
+ let w: &MemoryCardsNewScene = Into::<&Window>::into(window.upcast_ref())
+ .downcast_ref()
+ .unwrap(); // Weird casting from &Window as passed in func to &MemoryCardsNewScene
+ w.get_image_widget().set_file(Some(path));
+ let binding = Path::new(path);
+ let hash = try_digest(binding).unwrap();
- let conn = sqlite::open("test.db").unwrap();
+ w.get_done_button().connect_closure("clicked", false, closure_local!(@strong w => move |_b: &Button| {
+ let hieroglyph = w.get_hieroglyph_input();
+ let reading = w.get_reading_input();
+ let program_home_path = &*crate::db::get_program_home_path().read().unwrap().clone();
+ let conn = Connection::open(PathBuf::new().join(program_home_path).join("db.sqlite")).unwrap();
- conn.execute("CREATE TABLE test(field_one TEXT, field_two INTEGER); INSERT INTO test VALUES ('Suka', 15);").unwrap();
+ let query = "INSERT INTO cards (image_hash, hieroglyph, translation) VALUES(?1, ?2, ?3)";
+ conn.execute(query, (&hash, &hieroglyph, &reading)).unwrap();
+ w.close();
+ }));
}
impl WidgetImpl for MemoryCardsEditScene {}
diff --git a/src/ui/cards/new/imp.rs b/src/ui/cards/new/imp.rs
index b2ca6a6..5a2f6de 100644
--- a/src/ui/cards/new/imp.rs
+++ b/src/ui/cards/new/imp.rs
@@ -12,7 +12,7 @@ pub struct MemoryCardsNewScene {
#[template_child]
pub heiroglyph_entry: TemplateChild,
#[template_child]
- pub translation_entry: TemplateChild,
+ pub reading_entry: TemplateChild,
#[template_child]
pub done_button: TemplateChild