mod bookmark;
mod database;
mod history;
mod identity;
mod search;
use bookmark::Bookmark;
use database::Database;
use history::History;
use identity::Identity;
use search::Search;
use anyhow::Result;
use gtk::glib::{user_config_dir, DateTime};
use sqlite::{Connection, Transaction};
use std::{fs::create_dir_all, path::PathBuf, rc::Rc, sync::RwLock};
const VENDOR: &str = "YGGverse";
const APP_ID: &str = "Yoda";
const BRANCH: &str = "master";
const DB_NAME: &str = "database.sqlite3";
pub struct Profile {
pub bookmark: Rc<Bookmark>,
pub database: Rc<Database>,
pub history: Rc<History>,
pub identity: Rc<Identity>,
pub search: Rc<Search>,
pub config_path: PathBuf,
}
impl Profile {
pub fn init() -> Result<Self> {
let mut config_path = user_config_dir();
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")
));
create_dir_all(&config_path)?;
let mut database_path = config_path.clone();
database_path.push(DB_NAME);
let connection = Rc::new(RwLock::new(Connection::open(database_path.as_path())?));
{
let mut connection = connection.write().unwrap();
let transaction = connection.transaction()?;
migrate(&transaction)?;
transaction.commit()?;
}
let database = Rc::new(Database::build(&connection));
let profile_id = Rc::new(match database.active()? {
Some(profile) => profile.id,
None => database.add(true, DateTime::now_local()?, None)?,
});
let bookmark = Rc::new(Bookmark::build(&connection, &profile_id)?);
let history = Rc::new(History::build(&connection, &profile_id)?);
let search = Rc::new(Search::build(&connection, &profile_id)?);
let identity = Rc::new(Identity::build(&connection, &profile_id)?);
Ok(Self {
bookmark,
database,
history,
identity,
search,
config_path,
})
}
pub fn save(&self) -> Result<()> {
self.history.save()
}
}
pub fn migrate(tx: &Transaction) -> Result<()> {
database::init(tx)?;
bookmark::migrate(tx)?;
identity::migrate(tx)?;
search::migrate(tx)?;
history::migrate(tx)?;
Ok(())
}