2024-11-13 03:40:29 +02:00
|
|
|
mod bookmark;
|
2024-11-08 07:46:25 +02:00
|
|
|
mod database;
|
2024-11-14 10:51:35 +02:00
|
|
|
//mod history;
|
|
|
|
//mod identity;
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-14 06:00:41 +02:00
|
|
|
use bookmark::Bookmark;
|
2024-11-13 08:03:48 +02:00
|
|
|
use database::Database;
|
|
|
|
|
2024-11-14 06:00:41 +02:00
|
|
|
use gtk::glib::{user_config_dir, DateTime};
|
2024-11-13 03:40:29 +02:00
|
|
|
use sqlite::{Connection, Transaction};
|
2024-11-13 08:03:48 +02:00
|
|
|
use std::{fs::create_dir_all, path::PathBuf, rc::Rc, sync::RwLock};
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-08 08:43:02 +02:00
|
|
|
const VENDOR: &str = "YGGverse";
|
|
|
|
const APP_ID: &str = "Yoda";
|
|
|
|
const BRANCH: &str = "master";
|
|
|
|
|
2024-11-13 08:03:48 +02:00
|
|
|
const DB_NAME: &str = "database.sqlite3";
|
2024-11-08 07:46:25 +02:00
|
|
|
|
|
|
|
pub struct Profile {
|
2024-11-14 06:00:41 +02:00
|
|
|
pub bookmark: Rc<Bookmark>,
|
2024-11-13 08:03:48 +02:00
|
|
|
pub database: Rc<Database>,
|
|
|
|
pub config_path: PathBuf,
|
2024-11-08 07:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Profile {
|
|
|
|
// Constructors
|
|
|
|
|
2024-11-08 08:43:02 +02:00
|
|
|
pub fn new() -> Self {
|
2024-11-08 07:46:25 +02:00
|
|
|
// Init profile path
|
|
|
|
let mut config_path = user_config_dir();
|
|
|
|
|
2024-11-08 08:43:02 +02:00
|
|
|
config_path.push(VENDOR);
|
|
|
|
config_path.push(APP_ID);
|
|
|
|
config_path.push(BRANCH);
|
|
|
|
config_path.push(format!(
|
|
|
|
"{}.{}",
|
|
|
|
env!("CARGO_PKG_VERSION_MAJOR"),
|
|
|
|
env!("CARGO_PKG_VERSION_MINOR")
|
|
|
|
)); // @TODO remove after auto-migrate feature implementation
|
2024-11-08 07:46:25 +02:00
|
|
|
|
2024-11-08 08:47:52 +02:00
|
|
|
if let Err(reason) = create_dir_all(&config_path) {
|
|
|
|
panic!("{reason}")
|
2024-11-08 07:46:25 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init database path
|
|
|
|
let mut database_path = config_path.clone();
|
|
|
|
database_path.push(DB_NAME);
|
|
|
|
|
2024-11-13 03:40:29 +02:00
|
|
|
// Init database connection
|
|
|
|
let connection = match Connection::open(database_path.as_path()) {
|
|
|
|
Ok(connection) => Rc::new(RwLock::new(connection)),
|
|
|
|
Err(reason) => panic!("{reason}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Init profile components
|
|
|
|
{
|
|
|
|
// Init writable connection
|
|
|
|
let mut connection = match connection.write() {
|
|
|
|
Ok(connection) => connection,
|
|
|
|
Err(e) => todo!("{e}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Init new transaction
|
|
|
|
let transaction = match connection.transaction() {
|
|
|
|
Ok(transaction) => transaction,
|
|
|
|
Err(e) => todo!("{e}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Begin migration
|
|
|
|
match migrate(&transaction) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Confirm changes
|
|
|
|
match transaction.commit() {
|
|
|
|
Ok(_) => {} // @TODO
|
|
|
|
Err(e) => todo!("{e}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => todo!("{e}"),
|
|
|
|
}
|
|
|
|
} // unlock database
|
|
|
|
|
2024-11-14 06:00:41 +02:00
|
|
|
// Init model
|
|
|
|
let database = Rc::new(Database::new(connection.clone()));
|
|
|
|
|
|
|
|
// Get active profile or create new one
|
|
|
|
let profile_id = match database.active() {
|
|
|
|
Some(profile) => profile.id,
|
|
|
|
None => match database.add(true, DateTime::now_local().unwrap(), None) {
|
|
|
|
Ok(id) => id,
|
|
|
|
Err(_) => todo!(),
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
2024-11-08 07:46:25 +02:00
|
|
|
// Result
|
|
|
|
Self {
|
2024-11-14 06:00:41 +02:00
|
|
|
bookmark: Rc::new(Bookmark::new(connection, profile_id)),
|
|
|
|
database,
|
2024-11-08 07:46:25 +02:00
|
|
|
config_path,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-11-13 03:40:29 +02:00
|
|
|
|
|
|
|
pub fn migrate(tx: &Transaction) -> Result<(), String> {
|
|
|
|
// Migrate self components
|
|
|
|
if let Err(e) = database::init(tx) {
|
|
|
|
return Err(e.to_string());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delegate migration to children components
|
|
|
|
bookmark::migrate(tx)?;
|
2024-11-14 10:51:35 +02:00
|
|
|
// @TODO not in use yet
|
|
|
|
// history::migrate(tx)?;
|
|
|
|
// identity::migrate(tx)?;
|
2024-11-13 03:40:29 +02:00
|
|
|
|
|
|
|
// Success
|
|
|
|
Ok(())
|
|
|
|
}
|