mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-09-11 22:42:00 +00:00
draft app database model
This commit is contained in:
parent
4191227db4
commit
8aa5474c20
33
src/app.rs
33
src/app.rs
@ -1,8 +1,10 @@
|
|||||||
mod action;
|
mod action;
|
||||||
mod browser;
|
mod browser;
|
||||||
|
mod database;
|
||||||
|
|
||||||
use action::Action;
|
use action::Action;
|
||||||
use browser::Browser;
|
use browser::Browser;
|
||||||
|
use database::Database;
|
||||||
|
|
||||||
use gtk::{
|
use gtk::{
|
||||||
glib::{user_config_dir, ExitCode},
|
glib::{user_config_dir, ExitCode},
|
||||||
@ -19,30 +21,33 @@ pub struct App {
|
|||||||
app: Application,
|
app: Application,
|
||||||
// Components
|
// Components
|
||||||
//browser: Arc<Browser>,
|
//browser: Arc<Browser>,
|
||||||
|
database: Arc<Database>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl App {
|
impl App {
|
||||||
// Construct
|
// Construct
|
||||||
pub fn new() -> Self {
|
pub fn new() -> Self {
|
||||||
// Init profile directory
|
// Init profile directory path
|
||||||
let mut fs = user_config_dir();
|
let mut profile_path = user_config_dir();
|
||||||
|
|
||||||
fs.push(APPLICATION_ID);
|
profile_path.push(APPLICATION_ID);
|
||||||
|
|
||||||
if let Err(e) = create_dir_all(&fs) {
|
if let Err(e) = create_dir_all(&profile_path) {
|
||||||
panic!("Failed to create profile directory: {e}")
|
panic!("Failed to create profile directory: {e}")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Init profile database
|
// Init profile database path
|
||||||
/* @TODO
|
let mut database_path = profile_path.clone();
|
||||||
let mut db = fs.clone();
|
|
||||||
|
|
||||||
db.push("database.sqlite3");
|
database_path.push("database.sqlite3");
|
||||||
|
|
||||||
let db = match sqlite::open(db) {
|
let connection = match sqlite::open(database_path) {
|
||||||
Ok(db) => Arc::new(db),
|
Ok(connection) => Arc::new(connection),
|
||||||
Err(e) => panic!("Failed to connect profile database: {e}"),
|
Err(e) => panic!("Failed to connect profile database: {e}"),
|
||||||
};*/
|
};
|
||||||
|
|
||||||
|
// Init database model
|
||||||
|
let database = Arc::new(Database::init(connection, env!("CARGO_PKG_VERSION")));
|
||||||
|
|
||||||
// Init actions
|
// Init actions
|
||||||
let action_debug = Action::new("win", true);
|
let action_debug = Action::new("win", true);
|
||||||
@ -88,7 +93,11 @@ impl App {
|
|||||||
|
|
||||||
// Init events
|
// Init events
|
||||||
app.connect_activate({
|
app.connect_activate({
|
||||||
|
let database = database.clone();
|
||||||
move |application| {
|
move |application| {
|
||||||
|
// Restore previous session
|
||||||
|
database.restore();
|
||||||
|
|
||||||
// Init components
|
// Init components
|
||||||
let browser = Arc::new(Browser::new(
|
let browser = Arc::new(Browser::new(
|
||||||
&application,
|
&application,
|
||||||
@ -115,7 +124,7 @@ impl App {
|
|||||||
});
|
});
|
||||||
|
|
||||||
// Return activated struct
|
// Return activated struct
|
||||||
Self { app }
|
Self { app, database }
|
||||||
}
|
}
|
||||||
|
|
||||||
// Actions
|
// Actions
|
||||||
|
43
src/app/database.rs
Normal file
43
src/app/database.rs
Normal file
@ -0,0 +1,43 @@
|
|||||||
|
use std::sync::Arc;
|
||||||
|
|
||||||
|
pub struct Database {
|
||||||
|
connection: Arc<sqlite::Connection>,
|
||||||
|
// Autostart migrate feature on app and db versions mismatch
|
||||||
|
version: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl Database {
|
||||||
|
// Construct new application DB
|
||||||
|
pub fn init(connection: Arc<sqlite::Connection>, version: &str) -> Database {
|
||||||
|
// Create app table if not exist yet
|
||||||
|
/*
|
||||||
|
connection
|
||||||
|
.execute(
|
||||||
|
r"
|
||||||
|
CREATE TABLE IF NOT EXISTS `app`
|
||||||
|
(
|
||||||
|
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||||
|
`time` INTEGER NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||||
|
`version` VARCHAR NOT NULL
|
||||||
|
)
|
||||||
|
",
|
||||||
|
)
|
||||||
|
.unwrap(); // @TODO handle errors */
|
||||||
|
|
||||||
|
// Return struct
|
||||||
|
Self {
|
||||||
|
connection,
|
||||||
|
version: String::from(version),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Restore previous browser session from DB
|
||||||
|
pub fn restore(&self) {
|
||||||
|
// @TODO migration test
|
||||||
|
}
|
||||||
|
|
||||||
|
// Save browser session to DB
|
||||||
|
pub fn save(&self) {
|
||||||
|
// @TODO migration test
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user