Yoda/src/app.rs

283 lines
9.8 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
use crate::profile::Profile;
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::{uuid_string_random, ExitCode},
2024-10-15 08:45:44 +03:00
prelude::{
ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt, ToVariant,
2024-10-15 08:45:44 +03:00
},
2024-10-02 02:14:00 +03:00
};
use sqlite::Transaction;
use std::rc::Rc;
2024-10-02 02:14:00 +03:00
const APPLICATION_ID: &str = "io.github.yggverse.Yoda";
pub struct App {
profile: Rc<Profile>,
2024-10-06 05:11:52 +03:00
gobject: Application,
2024-10-02 02:14:00 +03:00
}
impl App {
// Construct
2024-11-08 08:43:02 +02:00
pub fn new() -> Self {
// Init profile
let profile = Rc::new(Profile::new());
// @TODO
let default_state = (-1).to_variant();
2024-11-05 03:23:10 +02:00
2024-11-05 18:48:00 +02:00
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);
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 components
2024-11-08 05:21:08 +02:00
let browser = Rc::new(Browser::new(
profile.clone(),
2024-11-05 18:48:00 +02:00
action_page_close.clone(),
action_page_close_all.clone(),
action_page_home.clone(),
action_page_history_back.clone(),
action_page_history_forward.clone(),
));
2024-10-03 20:03:43 +03:00
// Init events
2024-10-06 05:11:52 +03:00
gobject.connect_activate({
2024-11-10 07:09:55 +02:00
let browser = browser.clone();
move |_| browser.update()
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();
let profile = profile.clone();
move |this| {
2024-10-06 00:43:35 +03:00
// Init readable connection
match profile.database().connection().read() {
2024-10-06 00:43:35 +03:00
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({
let browser = browser.clone();
let profile = profile.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() {
2024-10-06 00:43:35 +03:00
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}"),
}
}
});
// Init accels
2024-11-10 07:09:55 +02:00
for (detailed_action_name, &accels) in &[
// Browser actions
(
format!(
2024-11-10 07:09:55 +02:00
"{}.{}",
browser.action().id(),
browser.action().close().id()
),
&["<Primary>Escape"],
),
(
format!(
2024-11-10 07:09:55 +02:00
"{}.{}",
browser.action().id(),
browser.action().debug().id()
),
&["<Primary>i"],
),
(
format!(
2024-11-10 07:09:55 +02:00
"{}.{}",
browser.action().id(),
browser.action().update().id()
),
&["<Primary>u"],
),
2024-11-10 08:07:44 +02:00
// Tab actions
(
format!(
2024-11-10 08:07:44 +02:00
"{}.{}",
browser.window().action().id(),
browser.window().action().append().id()
2024-11-10 08:07:44 +02:00
),
&["<Primary>t"],
),
(
format!(
"{}.{}",
browser.window().action().id(),
browser.window().action().pin().id()
),
&["<Primary>p"],
),
(
format!(
"{}.{}",
browser.window().action().id(),
browser.window().action().reload().id()
),
&["<Primary>r"],
),
// @TODO
(
format!("win.{}", action_page_history_back.name()),
&["<Primary>Left"],
),
(
format!("win.{}", action_page_history_forward.name()),
&["<Primary>Right"],
),
(format!("win.{}", action_page_home.name()), &["<Primary>h"]),
2024-11-10 02:45:02 +02:00
// @TODO page close missed
] {
gobject.set_accels_for_action(detailed_action_name, &accels);
}
2024-10-03 20:03:43 +03:00
// Return activated App struct
Self { profile, gobject }
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
2024-11-08 05:03:02 +02:00
if let Err(e) = Database::init(tx) {
return Err(e.to_string());
}
// Delegate migration to childs
2024-11-08 05:03:02 +02:00
browser::migrate(tx)?;
// Success
Ok(())
2024-10-02 02:14:00 +03:00
}