Yoda/src/app.rs

279 lines
9.3 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;
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::{
glib::ExitCode,
2024-11-28 01:35:48 +02:00
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt},
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
pub fn new(profile: Rc<Profile>) -> Self {
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
let browser = Rc::new(Browser::new(profile.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-11-13 11:45:33 +02:00
match database::select(&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
}
}
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
// Run initial features, show widget
browser.init(Some(this)).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-11-13 11:45:33 +02:00
match database::select(&transaction) {
2024-10-06 00:43:35 +03:00
Ok(records) => {
// Cleanup previous session records
for record in records {
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-11-13 11:45:33 +02:00
match database::insert(&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,
&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
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.action.id,
browser.action.close.gobject.name()
2024-11-10 07:09:55 +02:00
),
&["<Primary>Escape"],
),
(
format!(
2024-11-10 07:09:55 +02:00
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.action.id,
browser.action.debug.gobject.name()
2024-11-10 07:09:55 +02:00
),
&["<Primary>i"],
),
(
format!(
2024-11-10 07:09:55 +02:00
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.action.id,
browser.action.update.gobject.name()
2024-11-10 07:09:55 +02:00
),
&["<Primary>u"],
),
2024-11-10 08:07:44 +02:00
// Tab actions
(
format!(
2024-11-10 08:07:44 +02:00
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.append.gobject.name()
2024-11-10 08:07:44 +02:00
),
&["<Primary>t"],
),
2024-11-12 18:02:35 +02:00
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.bookmark.gobject.name()
2024-11-12 18:02:35 +02:00
),
&["<Primary>b"],
),
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.pin.gobject.name()
),
&["<Primary>p"],
),
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.reload.gobject.name()
),
&["<Primary>r"],
),
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.home.gobject.name()
),
&["<Primary>h"],
),
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.history_back.gobject.name()
),
&["<Primary>Left"],
),
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.history_forward.gobject.name()
),
&["<Primary>Right"],
),
(
format!(
"{}.{}",
2024-11-28 01:35:48 +02:00
browser.window.action.id,
browser.window.action.close.gobject.name()
),
&["<Primary>q"],
),
] {
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.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
2024-11-08 05:03:02 +02:00
browser::migrate(tx)?;
// Success
Ok(())
2024-10-02 02:14:00 +03:00
}