Yoda/src/app.rs

265 lines
9.9 KiB
Rust
Raw Normal View History

2024-10-02 02:14:00 +03:00
mod action;
mod browser;
2024-10-02 15:22:50 +03:00
mod database;
2024-10-02 02:14:00 +03:00
use action::Action;
use browser::Browser;
2024-10-02 15:22:50 +03:00
use database::Database;
2024-10-02 02:14:00 +03:00
use gtk::{
glib::ExitCode,
2024-10-02 02:14:00 +03:00
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt},
Application,
};
use sqlite::{Connection, Transaction};
2024-10-02 02:14:00 +03:00
2024-10-06 00:43:35 +03:00
use std::{
path::PathBuf,
sync::{Arc, RwLock},
};
2024-10-02 02:14:00 +03:00
const APPLICATION_ID: &str = "io.github.yggverse.Yoda";
pub struct App {
profile_database_connection: Arc<RwLock<Connection>>,
// database: Arc<Database>,
2024-10-03 01:30:13 +03:00
// Actions
2024-10-03 20:03:43 +03:00
// action_update: Arc<SimpleAction>,
// Components
2024-10-03 20:03:43 +03:00
// browser: Arc<Browser>,
// GTK
2024-10-06 05:11:52 +03:00
gobject: Application,
2024-10-02 02:14:00 +03:00
}
impl App {
// Construct
2024-10-06 00:43:35 +03:00
pub fn new(
profile_database_connection: Arc<RwLock<Connection>>,
profile_path: PathBuf,
) -> Self {
2024-10-02 02:14:00 +03:00
// Init actions
let action_tool_debug = Action::new("win", true);
let action_tool_profile_directory = Action::new("win", true);
2024-10-02 02:14:00 +03:00
let action_quit = Action::new("win", true);
let action_update = Action::new("win", true);
let action_tab_append = Action::new("win", true);
let action_tab_close = Action::new("win", true);
let action_tab_close_all = Action::new("win", true);
let action_tab_page_navigation_base = Action::new("win", false);
let action_tab_page_navigation_history_back = Action::new("win", false);
let action_tab_page_navigation_history_forward = Action::new("win", false);
let action_tab_page_navigation_reload = Action::new("win", true);
let action_tab_pin = Action::new("win", true);
// Init GTK
2024-10-06 05:11:52 +03:00
let gobject = Application::builder()
2024-10-02 02:14:00 +03:00
.application_id(APPLICATION_ID)
.build();
// Init accels
2024-10-06 05:11:52 +03:00
gobject.set_accels_for_action(&action_tool_debug.detailed_name(), &["<Primary>i"]);
gobject.set_accels_for_action(&action_update.detailed_name(), &["<Primary>u"]);
gobject.set_accels_for_action(&action_quit.detailed_name(), &["<Primary>Escape"]);
gobject.set_accels_for_action(&action_tab_append.detailed_name(), &["<Primary>t"]);
gobject.set_accels_for_action(&action_tab_pin.detailed_name(), &["<Primary>p"]);
gobject.set_accels_for_action(&action_tab_close.detailed_name(), &["<Primary>q"]);
gobject.set_accels_for_action(
2024-10-02 02:14:00 +03:00
&action_tab_page_navigation_base.detailed_name(),
&["<Primary>h"],
);
2024-10-06 05:11:52 +03:00
gobject.set_accels_for_action(
2024-10-02 02:14:00 +03:00
&action_tab_page_navigation_history_back.detailed_name(),
&["<Primary>Left"],
);
2024-10-06 05:11:52 +03:00
gobject.set_accels_for_action(
2024-10-02 02:14:00 +03:00
&action_tab_page_navigation_history_forward.detailed_name(),
&["<Primary>Right"],
);
2024-10-06 05:11:52 +03:00
gobject.set_accels_for_action(
2024-10-02 02:14:00 +03:00
&action_tab_page_navigation_reload.detailed_name(),
&["<Primary>r"],
);
// Init components
let browser = Arc::new(Browser::new(
2024-10-04 17:54:25 +03:00
profile_path,
action_tool_debug.simple(),
action_tool_profile_directory.simple(),
action_quit.simple(),
action_update.simple(),
action_tab_append.simple(),
action_tab_close.simple(),
action_tab_close_all.simple(),
action_tab_page_navigation_base.simple(),
action_tab_page_navigation_history_back.simple(),
action_tab_page_navigation_history_forward.simple(),
action_tab_page_navigation_reload.simple(),
action_tab_pin.simple(),
));
2024-10-03 20:03:43 +03:00
// Init events
2024-10-06 05:11:52 +03:00
gobject.connect_activate({
2024-10-03 20:03:43 +03:00
let action_update = action_update.simple();
2024-10-04 20:12:52 +03:00
move |_| {
// Make initial update
action_update.activate(None);
}
});
2024-10-06 05:11:52 +03:00
gobject.connect_startup({
2024-10-03 20:03:43 +03:00
let browser = browser.clone();
2024-10-06 00:43:35 +03:00
let profile_database_connection = profile_database_connection.clone();
move |this| {
2024-10-06 00:43:35 +03:00
// Init readable connection
match profile_database_connection.read() {
Ok(connection) => {
// Create transaction
match connection.unchecked_transaction() {
Ok(transaction) => {
// Restore previous session from DB
2024-10-07 20:34:48 +03:00
match Database::records(&transaction) {
2024-10-06 00:43:35 +03:00
Ok(records) => {
for record in records {
if let Err(e) =
browser.restore(&transaction, &record.id)
{
todo!("{e}")
}
2024-10-06 00:43:35 +03:00
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-06 00:43:35 +03:00
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
}
2024-10-02 02:14:00 +03:00
2024-10-04 20:20:53 +03:00
// Assign browser window to this application
2024-10-06 05:16:29 +03:00
browser.gobject().set_application(Some(this));
2024-10-03 01:30:13 +03:00
2024-10-02 02:14:00 +03:00
// Show main widget
2024-10-06 05:16:29 +03:00
browser.gobject().present();
2024-10-02 02:14:00 +03:00
}
});
2024-10-06 05:11:52 +03:00
gobject.connect_shutdown({
2024-10-04 19:21:37 +03:00
// let browser = browser.clone();
2024-10-06 00:43:35 +03:00
let profile_database_connection = profile_database_connection.clone();
let browser = browser.clone();
2024-10-04 20:12:52 +03:00
move |_| {
2024-10-06 00:43:35 +03:00
// Init writable connection
match profile_database_connection.write() {
Ok(mut connection) => {
// Create transaction
match connection.transaction() {
Ok(transaction) => {
2024-10-07 20:34:48 +03:00
match Database::records(&transaction) {
2024-10-06 00:43:35 +03:00
Ok(records) => {
// Cleanup previous session records
for record in records {
2024-10-07 20:34:48 +03:00
match Database::delete(&transaction, &record.id) {
2024-10-06 00:43:35 +03:00
Ok(_) => {
// Delegate clean action to childs
if let Err(e) =
browser.clean(&transaction, &record.id)
{
todo!("{e}")
}
2024-10-06 00:43:35 +03:00
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-06 00:43:35 +03:00
}
}
// Save current session to DB
2024-10-07 20:34:48 +03:00
match Database::add(&transaction) {
2024-10-06 00:43:35 +03:00
Ok(_) => {
// Delegate save action to childs
if let Err(e) = browser.save(
2024-10-06 00:43:35 +03:00
&transaction,
2024-10-07 20:34:48 +03:00
&Database::last_insert_id(&transaction),
) {
todo!("{e}")
}
2024-10-06 00:43:35 +03:00
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-06 00:43:35 +03:00
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-04 19:21:37 +03:00
}
2024-10-06 00:43:35 +03:00
// Confirm changes
2024-10-06 00:49:43 +03:00
if let Err(e) = transaction.commit() {
todo!("{e}")
2024-10-06 00:43:35 +03:00
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
}
}
});
2024-10-03 20:03:43 +03:00
// Return activated App struct
Self {
// database,
profile_database_connection,
2024-10-03 20:03:43 +03:00
// Actions (SimpleAction)
// action_update: action_update.simple(),
// Components
// browser,
// GTK
2024-10-06 05:11:52 +03:00
gobject,
2024-10-03 20:03:43 +03:00
}
2024-10-02 02:14:00 +03:00
}
2024-10-03 20:03:43 +03:00
// Actions
2024-10-02 02:14:00 +03:00
pub fn run(&self) -> ExitCode {
// Begin database migration @TODO
{
// Init writable connection
let mut connection = match self.profile_database_connection.try_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 App::migrate(&transaction) {
Ok(_) => {
// Confirm changes
match transaction.commit() {
Ok(_) => {} // @TODO
Err(e) => todo!("{e}"),
}
}
Err(e) => todo!("{e}"),
}
} // unlock database
// Start application
2024-10-06 05:11:52 +03:00
self.gobject.run()
2024-10-02 02:14:00 +03:00
}
// Tools
2024-10-08 04:14:24 +03:00
fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(&tx) {
return Err(e.to_string());
}
// Delegate migration to childs
2024-10-08 03:41:51 +03:00
Browser::migrate(&tx)?;
// Success
Ok(())
}
2024-10-02 02:14:00 +03:00
}