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::{
|
2024-10-02 18:46:08 +03:00
|
|
|
glib::ExitCode,
|
2024-10-02 02:14:00 +03:00
|
|
|
prelude::{ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt},
|
|
|
|
Application,
|
|
|
|
};
|
2024-10-02 18:46:08 +03:00
|
|
|
use sqlite::Connection;
|
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 {
|
2024-10-03 01:30:13 +03:00
|
|
|
// Actions
|
2024-10-03 20:03:43 +03:00
|
|
|
// action_update: Arc<SimpleAction>,
|
2024-10-03 15:38:34 +03:00
|
|
|
// Components
|
2024-10-03 20:03:43 +03:00
|
|
|
// browser: Arc<Browser>,
|
2024-10-03 15:38:34 +03:00
|
|
|
// Extras
|
2024-10-04 02:23:54 +03:00
|
|
|
// database: Arc<Database>,
|
2024-10-03 15:38:34 +03:00
|
|
|
// GTK
|
2024-10-05 15:38:51 +03:00
|
|
|
application: 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-04 21:23:38 +03:00
|
|
|
// Init database
|
2024-10-06 00:43:35 +03:00
|
|
|
let database = {
|
|
|
|
// Init writable database connection
|
|
|
|
let mut connection = match profile_database_connection.write() {
|
|
|
|
Ok(connection) => connection,
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Init new transaction
|
|
|
|
let transaction = match connection.transaction() {
|
|
|
|
Ok(transaction) => transaction,
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Init database structure
|
|
|
|
match Database::init(&transaction) {
|
|
|
|
Ok(database) => match transaction.commit() {
|
|
|
|
Ok(_) => Arc::new(database),
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
},
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
}
|
2024-10-04 02:57:50 +03:00
|
|
|
};
|
2024-10-02 02:14:00 +03:00
|
|
|
|
|
|
|
// Init actions
|
2024-10-04 16:19:26 +03:00
|
|
|
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-05 15:38:51 +03:00
|
|
|
let application = Application::builder()
|
2024-10-02 02:14:00 +03:00
|
|
|
.application_id(APPLICATION_ID)
|
|
|
|
.build();
|
|
|
|
|
|
|
|
// Init accels
|
2024-10-05 15:38:51 +03:00
|
|
|
application.set_accels_for_action(&action_tool_debug.detailed_name(), &["<Primary>i"]);
|
|
|
|
application.set_accels_for_action(&action_update.detailed_name(), &["<Primary>u"]);
|
|
|
|
application.set_accels_for_action(&action_quit.detailed_name(), &["<Primary>Escape"]);
|
|
|
|
application.set_accels_for_action(&action_tab_append.detailed_name(), &["<Primary>t"]);
|
|
|
|
application.set_accels_for_action(&action_tab_pin.detailed_name(), &["<Primary>p"]);
|
|
|
|
application.set_accels_for_action(&action_tab_close.detailed_name(), &["<Primary>q"]);
|
|
|
|
application.set_accels_for_action(
|
2024-10-02 02:14:00 +03:00
|
|
|
&action_tab_page_navigation_base.detailed_name(),
|
|
|
|
&["<Primary>h"],
|
|
|
|
);
|
2024-10-05 15:38:51 +03:00
|
|
|
application.set_accels_for_action(
|
2024-10-02 02:14:00 +03:00
|
|
|
&action_tab_page_navigation_history_back.detailed_name(),
|
|
|
|
&["<Primary>Left"],
|
|
|
|
);
|
2024-10-05 15:38:51 +03:00
|
|
|
application.set_accels_for_action(
|
2024-10-02 02:14:00 +03:00
|
|
|
&action_tab_page_navigation_history_forward.detailed_name(),
|
|
|
|
&["<Primary>Right"],
|
|
|
|
);
|
2024-10-05 15:38:51 +03:00
|
|
|
application.set_accels_for_action(
|
2024-10-02 02:14:00 +03:00
|
|
|
&action_tab_page_navigation_reload.detailed_name(),
|
|
|
|
&["<Primary>r"],
|
|
|
|
);
|
|
|
|
|
2024-10-03 15:38:34 +03:00
|
|
|
// Init components
|
|
|
|
let browser = Arc::new(Browser::new(
|
2024-10-06 00:43:35 +03:00
|
|
|
profile_database_connection.clone(),
|
2024-10-04 17:54:25 +03:00
|
|
|
profile_path,
|
2024-10-04 16:19:26 +03:00
|
|
|
action_tool_debug.simple(),
|
|
|
|
action_tool_profile_directory.simple(),
|
2024-10-03 15:38:34 +03:00
|
|
|
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-05 15:38:51 +03:00
|
|
|
application.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-05 15:38:51 +03:00
|
|
|
application.connect_startup({
|
2024-10-03 20:03:43 +03:00
|
|
|
let browser = browser.clone();
|
2024-10-04 16:19:26 +03:00
|
|
|
let database = database.clone();
|
2024-10-06 00:43:35 +03:00
|
|
|
let profile_database_connection = profile_database_connection.clone();
|
2024-10-03 15:38:34 +03:00
|
|
|
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
|
|
|
|
match database.records(&transaction) {
|
|
|
|
Ok(records) => {
|
|
|
|
for record in records {
|
|
|
|
browser.restore(&transaction, &record.id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => todo!("{error}"),
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-06 00:43:35 +03:00
|
|
|
Err(error) => todo!("{error}"),
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
2024-10-02 02:14:00 +03:00
|
|
|
|
2024-10-04 20:20:53 +03:00
|
|
|
// Assign browser window to this application
|
2024-10-03 20:03:43 +03:00
|
|
|
browser.widget().set_application(Some(this));
|
2024-10-03 01:30:13 +03:00
|
|
|
|
2024-10-02 02:14:00 +03:00
|
|
|
// Show main widget
|
|
|
|
browser.widget().present();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-10-05 15:38:51 +03:00
|
|
|
application.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();
|
2024-10-04 02:23:54 +03:00
|
|
|
let database = database.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) => {
|
|
|
|
match database.records(&transaction) {
|
|
|
|
Ok(records) => {
|
|
|
|
// Cleanup previous session records
|
|
|
|
for record in records {
|
|
|
|
match database.delete(&transaction, &record.id) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Delegate clean action to childs
|
|
|
|
browser.clean(&transaction, &record.id);
|
|
|
|
}
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save current session to DB
|
|
|
|
match database.add(&transaction) {
|
|
|
|
Ok(_) => {
|
|
|
|
// Delegate save action to childs
|
|
|
|
browser.save(
|
|
|
|
&transaction,
|
|
|
|
&database.last_insert_id(&transaction),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
Err(error) => todo!("{error}"),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(error) => todo!("{error}"),
|
2024-10-04 19:21:37 +03:00
|
|
|
}
|
2024-10-04 02:23:54 +03:00
|
|
|
|
2024-10-06 00:43:35 +03:00
|
|
|
// Confirm changes
|
|
|
|
if let Err(error) = transaction.commit() {
|
|
|
|
todo!("{error}")
|
|
|
|
}
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
2024-10-06 00:43:35 +03:00
|
|
|
Err(error) => todo!("{error}"),
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-06 00:43:35 +03:00
|
|
|
Err(error) => todo!("{error}"),
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-04 16:19:26 +03:00
|
|
|
});
|
2024-10-03 15:38:34 +03:00
|
|
|
|
2024-10-03 20:03:43 +03:00
|
|
|
// Return activated App struct
|
|
|
|
Self {
|
|
|
|
// Actions (SimpleAction)
|
|
|
|
// action_update: action_update.simple(),
|
|
|
|
// Components
|
|
|
|
// browser,
|
|
|
|
// Extras
|
2024-10-04 02:23:54 +03:00
|
|
|
// database,
|
2024-10-03 20:03:43 +03:00
|
|
|
// GTK
|
2024-10-05 15:38:51 +03:00
|
|
|
application,
|
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 {
|
2024-10-05 15:38:51 +03:00
|
|
|
self.application.run()
|
2024-10-02 02:14:00 +03:00
|
|
|
}
|
|
|
|
}
|