create database for cards
This commit is contained in:
33
src/db.rs
Normal file
33
src/db.rs
Normal file
@@ -0,0 +1,33 @@
|
||||
use sqlite;
|
||||
use std::{path::PathBuf, process::Command};
|
||||
|
||||
pub fn init() {
|
||||
let program_home_path = if cfg!(target_os = "windows") {
|
||||
let binding = std::env::var("APP_DATA").expect("No APP_DATA directory");
|
||||
let mut appdata = PathBuf::from(&binding);
|
||||
|
||||
appdata = appdata.join("learn-hieroglyph");
|
||||
Command::new("mkdir").arg(&appdata);
|
||||
|
||||
appdata
|
||||
} else {
|
||||
let binding = std::env::var("HOME").expect("No HOME directory");
|
||||
let mut home = PathBuf::from(&binding);
|
||||
|
||||
home = home.join(".config/learn-hieroglyph");
|
||||
Command::new("mkdir").args(["-p", &home.to_str().unwrap()]).spawn().expect(&format!("Cannot create program's home: {}", &home.to_str().unwrap()));
|
||||
|
||||
home
|
||||
};
|
||||
|
||||
println!("Program's home: {}", program_home_path.as_path().to_str().unwrap());
|
||||
|
||||
let connection = sqlite::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();
|
||||
}
|
||||
@@ -1,6 +1,7 @@
|
||||
mod game;
|
||||
mod ui;
|
||||
mod widgets;
|
||||
mod db;
|
||||
|
||||
use crate::ui::menu::MenuScene;
|
||||
|
||||
@@ -12,6 +13,8 @@ const APP_ID: &str = "org.foxarmy.learn-hieroglyph";
|
||||
fn main() -> glib::ExitCode {
|
||||
gio::resources_register_include!("compiled.gresource").expect("Cannot include gresources");
|
||||
|
||||
db::init();
|
||||
|
||||
let app: Application = Application::builder().application_id(APP_ID).build();
|
||||
|
||||
app.connect_activate(test_ui);
|
||||
|
||||
@@ -6,6 +6,7 @@ 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;
|
||||
@@ -49,9 +50,7 @@ impl ObjectImpl for MemoryCardsEditScene {
|
||||
closure_local!(@strong binding => move |_b: &Button| {
|
||||
let new_win = Rc::new(MemoryCardsNewScene::new(&binding.application().unwrap()));
|
||||
new_win.get_file_choose_button().connect_clicked(clone!(@strong new_win => move |_| {
|
||||
let path = Rc::new(String::from(""));
|
||||
gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win), Rc::clone(&path)));
|
||||
println!("{}", path); // this is empty cuz it's not waiting for user
|
||||
gtk::glib::MainContext::default().spawn_local(file_choose_dialog(Rc::clone(&new_win)));
|
||||
}));
|
||||
new_win.present();
|
||||
}));
|
||||
@@ -63,14 +62,17 @@ impl ObjectImpl for MemoryCardsEditScene {
|
||||
}
|
||||
}
|
||||
|
||||
async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>, mut _p: Rc<String>) {
|
||||
async fn file_choose_dialog<W: IsA<gtk::Window>>(window: Rc<W>) {
|
||||
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();
|
||||
// _p = Rc::new(path.into_os_string().into_string().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(path);
|
||||
|
||||
let conn = sqlite::open("test.db").unwrap();
|
||||
|
||||
conn.execute("CREATE TABLE test(field_one TEXT, field_two INTEGER); INSERT INTO test VALUES ('Suka', 15);").unwrap();
|
||||
}
|
||||
|
||||
impl WidgetImpl for MemoryCardsEditScene {}
|
||||
|
||||
Reference in New Issue
Block a user