Yoda/src/app.rs

289 lines
11 KiB
Rust
Raw Normal View History

2024-10-02 02:14:00 +03:00
mod browser;
2024-10-02 15:22:50 +03:00
mod database;
2024-10-02 02:14:00 +03:00
use browser::Browser;
2024-10-02 15:22:50 +03:00
use database::Database;
2024-10-02 02:14:00 +03:00
2024-10-09 10:50:41 +03:00
use adw::Application;
2024-10-02 02:14:00 +03:00
use gtk::{
2024-11-05 18:48:00 +02:00
gio::SimpleAction,
glib::{gformat, uuid_string_random, ExitCode},
2024-10-15 08:45:44 +03:00
prelude::{
ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt,
StaticVariantType, ToVariant,
},
2024-10-02 02:14:00 +03:00
};
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-27 12:50:36 +02:00
// action_update: 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-11-05 03:23:10 +02:00
// Init defaults
let default_state = (-1).to_variant();
2024-11-05 03:23:10 +02:00
2024-10-02 02:14:00 +03:00
// Init actions
2024-11-05 18:48:00 +02:00
let action_about = SimpleAction::new(&uuid_string_random(), None);
let action_debug = SimpleAction::new(&uuid_string_random(), None);
let action_profile = SimpleAction::new(&uuid_string_random(), None);
let action_quit = SimpleAction::new(&uuid_string_random(), None);
let action_update =
SimpleAction::new(&uuid_string_random(), Some(&String::static_variant_type()));
let action_page_new = SimpleAction::new(&uuid_string_random(), None);
let action_page_close =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_close_all = SimpleAction::new(&uuid_string_random(), None);
let action_page_home =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_history_back =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_history_forward =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_reload =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
let action_page_pin =
SimpleAction::new_stateful(&uuid_string_random(), None, &default_state);
2024-10-02 02:14:00 +03:00
// 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-11-05 04:30:00 +02:00
let accels_config = &[
(
2024-11-05 18:48:00 +02:00
gformat!("win.{}", action_page_reload.name()),
&["<Primary>r"],
),
(gformat!("win.{}", action_debug.name()), &["<Primary>i"]),
(
gformat!("win.{}", action_page_close.name()),
&["<Primary>q"],
),
(
gformat!("win.{}", action_page_history_back.name()),
&["<Primary>Left"],
),
(
gformat!("win.{}", action_page_history_forward.name()),
2024-11-05 04:30:00 +02:00
&["<Primary>Right"],
),
2024-11-05 18:48:00 +02:00
(gformat!("win.{}", action_page_home.name()), &["<Primary>h"]),
(gformat!("win.{}", action_page_new.name()), &["<Primary>t"]),
(gformat!("win.{}", action_page_pin.name()), &["<Primary>p"]),
(gformat!("win.{}", action_quit.name()), &["<Primary>Escape"]),
(gformat!("win.{}", action_update.name()), &["<Primary>u"]),
2024-11-05 04:30:00 +02:00
]; // @TODO config
for (detailed_action_name, &accels) in accels_config {
gobject.set_accels_for_action(detailed_action_name, &accels);
}
2024-10-02 02:14:00 +03:00
// Init components
let browser = Arc::new(Browser::new(
2024-10-04 17:54:25 +03:00
profile_path,
2024-11-05 18:48:00 +02:00
action_about.clone(),
action_debug.clone(),
action_profile.clone(),
action_quit.clone(),
action_update.clone(),
action_page_new.clone(),
action_page_close.clone(),
action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(),
action_page_history_forward.clone(),
action_page_reload.clone(),
action_page_pin.clone(),
));
2024-10-03 20:03:43 +03:00
// Init events
2024-10-06 05:11:52 +03:00
gobject.connect_activate({
2024-11-05 18:48:00 +02:00
let action_update = action_update.clone();
2024-10-04 20:12:52 +03:00
move |_| {
// Make initial update
2024-10-15 08:45:44 +03:00
action_update.activate(Some(&"".to_variant())); // @TODO
2024-10-04 20:12:52 +03:00
}
});
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) => {
// Restore child components
2024-10-06 00:43:35 +03:00
for record in records {
if let Err(e) =
browser.restore(&transaction, &record.id)
{
todo!("{e}")
}
2024-10-06 00:43:35 +03:00
}
// Run initial features
browser.init();
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 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
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
browser::migrate(&tx)?;
// Success
Ok(())
2024-10-02 02:14:00 +03:00
}