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-11-08 09:19:16 +02: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::{gformat, uuid_string_random, ExitCode},
|
2024-10-15 08:45:44 +03:00
|
|
|
prelude::{
|
2024-11-08 06:47:58 +02:00
|
|
|
ActionExt, ApplicationExt, ApplicationExtManual, GtkApplicationExt, GtkWindowExt, ToVariant,
|
2024-10-15 08:45:44 +03:00
|
|
|
},
|
2024-10-02 02:14:00 +03:00
|
|
|
};
|
2024-11-08 07:46:25 +02: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 {
|
2024-11-08 07:46:25 +02:00
|
|
|
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
|
2024-11-05 03:26:55 +02:00
|
|
|
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_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();
|
|
|
|
|
2024-10-03 15:38:34 +03:00
|
|
|
// Init components
|
2024-11-08 05:21:08 +02:00
|
|
|
let browser = Rc::new(Browser::new(
|
2024-11-08 07:46:25 +02:00
|
|
|
profile.clone(),
|
2024-11-05 18:48:00 +02:00
|
|
|
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 15:38:34 +03:00
|
|
|
));
|
|
|
|
|
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();
|
2024-11-08 07:46:25 +02:00
|
|
|
let profile = profile.clone();
|
2024-10-03 15:38:34 +03:00
|
|
|
move |this| {
|
2024-10-06 00:43:35 +03:00
|
|
|
// Init readable connection
|
2024-11-08 07:46:25 +02:00
|
|
|
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) => {
|
2024-10-12 01:08:16 +03:00
|
|
|
// Restore child components
|
2024-10-06 00:43:35 +03:00
|
|
|
for record in records {
|
2024-10-07 21:10:12 +03:00
|
|
|
if let Err(e) =
|
|
|
|
browser.restore(&transaction, &record.id)
|
|
|
|
{
|
|
|
|
todo!("{e}")
|
|
|
|
}
|
2024-10-06 00:43:35 +03:00
|
|
|
}
|
2024-10-12 01:08:16 +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-04 02:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
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-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-07 19:54:28 +03:00
|
|
|
let browser = browser.clone();
|
2024-11-08 07:46:25 +02:00
|
|
|
let profile = profile.clone();
|
2024-10-04 20:12:52 +03:00
|
|
|
move |_| {
|
2024-10-06 00:43:35 +03:00
|
|
|
// Init writable connection
|
2024-11-08 07:46:25 +02:00
|
|
|
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
|
2024-10-07 21:10:12 +03:00
|
|
|
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
|
2024-10-07 21:10:12 +03:00
|
|
|
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),
|
2024-10-07 21:10:12 +03:00
|
|
|
) {
|
|
|
|
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-04 02:23:54 +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-04 02:23:54 +03:00
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
2024-10-04 02:23:54 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-06 00:49:43 +03:00
|
|
|
Err(e) => todo!("{e}"),
|
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-11-08 09:19:16 +02:00
|
|
|
// Init accels
|
2024-11-10 07:09:55 +02:00
|
|
|
for (detailed_action_name, &accels) in &[
|
2024-11-08 09:19:16 +02:00
|
|
|
// Browser actions
|
|
|
|
(
|
2024-11-10 07:09:55 +02:00
|
|
|
gformat!(
|
|
|
|
"{}.{}",
|
|
|
|
browser.action().id(),
|
|
|
|
browser.action().close().id()
|
|
|
|
),
|
|
|
|
&["<Primary>Escape"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
gformat!(
|
|
|
|
"{}.{}",
|
|
|
|
browser.action().id(),
|
|
|
|
browser.action().debug().id()
|
|
|
|
),
|
|
|
|
&["<Primary>i"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
gformat!(
|
|
|
|
"{}.{}",
|
|
|
|
browser.action().id(),
|
|
|
|
browser.action().update().id()
|
|
|
|
),
|
2024-11-08 09:19:16 +02:00
|
|
|
&["<Primary>u"],
|
|
|
|
),
|
2024-11-10 07:09:55 +02:00
|
|
|
// @TODO
|
2024-11-08 09:19:16 +02:00
|
|
|
(
|
|
|
|
gformat!("win.{}", action_page_reload.name()),
|
|
|
|
&["<Primary>r"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
gformat!("win.{}", action_page_history_back.name()),
|
|
|
|
&["<Primary>Left"],
|
|
|
|
),
|
|
|
|
(
|
|
|
|
gformat!("win.{}", action_page_history_forward.name()),
|
|
|
|
&["<Primary>Right"],
|
|
|
|
),
|
|
|
|
(gformat!("win.{}", action_page_home.name()), &["<Primary>h"]),
|
|
|
|
(gformat!("win.{}", action_page_new.name()), &["<Primary>t"]),
|
|
|
|
(gformat!("win.{}", action_page_pin.name()), &["<Primary>p"]),
|
2024-11-10 02:45:02 +02:00
|
|
|
// @TODO page close missed
|
2024-11-09 14:59:58 +02:00
|
|
|
] {
|
2024-11-08 09:19:16 +02:00
|
|
|
gobject.set_accels_for_action(detailed_action_name, &accels);
|
|
|
|
}
|
|
|
|
|
2024-10-03 20:03:43 +03:00
|
|
|
// Return activated App struct
|
2024-11-08 07:46:25 +02:00
|
|
|
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 {
|
2024-10-07 19:54:28 +03:00
|
|
|
// Begin database migration @TODO
|
|
|
|
{
|
|
|
|
// Init writable connection
|
2024-11-08 07:46:25 +02:00
|
|
|
let mut connection = match self.profile.database().connection().try_write() {
|
2024-10-07 19:54:28 +03:00
|
|
|
Ok(connection) => connection,
|
|
|
|
Err(e) => todo!("{e}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Init new transaction
|
|
|
|
let transaction = match connection.transaction() {
|
|
|
|
Ok(transaction) => transaction,
|
|
|
|
Err(e) => todo!("{e}"),
|
|
|
|
};
|
|
|
|
|
|
|
|
// Begin migration
|
2024-11-03 17:05:32 +02:00
|
|
|
match migrate(&transaction) {
|
2024-10-07 19:54:28 +03:00
|
|
|
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
|
|
|
}
|
2024-11-03 17:05:32 +02:00
|
|
|
}
|
2024-10-07 19:54:28 +03:00
|
|
|
|
2024-11-03 17:05:32 +02: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) {
|
2024-11-03 17:05:32 +02:00
|
|
|
return Err(e.to_string());
|
|
|
|
}
|
2024-10-07 19:54:28 +03:00
|
|
|
|
2024-11-03 17:05:32 +02:00
|
|
|
// Delegate migration to childs
|
2024-11-08 05:03:02 +02:00
|
|
|
browser::migrate(tx)?;
|
2024-10-07 19:54:28 +03:00
|
|
|
|
2024-11-03 17:05:32 +02:00
|
|
|
// Success
|
|
|
|
Ok(())
|
2024-10-02 02:14:00 +03:00
|
|
|
}
|