Yoda/src/app/browser.rs

287 lines
8.7 KiB
Rust
Raw Normal View History

2024-10-04 21:23:38 +03:00
mod database;
2024-09-20 18:02:10 +03:00
mod header;
2024-10-05 03:27:09 +03:00
mod widget;
2024-10-05 17:10:31 +03:00
mod window;
2024-09-18 20:33:29 +03:00
2024-10-04 21:23:38 +03:00
use database::Database;
2024-09-24 23:08:40 +03:00
use header::Header;
2024-10-05 03:27:09 +03:00
use widget::Widget;
2024-10-05 17:10:31 +03:00
use window::Window;
2024-09-24 23:08:40 +03:00
use gtk::{
2024-10-04 17:54:25 +03:00
gio::{AppInfo, AppLaunchContext, SimpleAction},
2024-10-05 03:27:09 +03:00
prelude::{ActionMapExt, GtkWindowExt},
ApplicationWindow,
2024-09-24 23:08:40 +03:00
};
2024-10-06 01:02:23 +03:00
use sqlite::{Connection, Transaction};
2024-10-06 00:43:35 +03:00
use std::{
path::PathBuf,
sync::{Arc, RwLock},
};
2024-09-23 01:57:16 +03:00
2024-09-22 02:00:54 +03:00
pub struct Browser {
// Extras
2024-10-04 21:23:38 +03:00
database: Arc<Database>,
// Components
2024-10-03 20:03:43 +03:00
// header: Arc<Header>,
2024-10-05 17:10:31 +03:00
// window: Arc<Window>,
2024-10-05 03:27:09 +03:00
widget: Arc<Widget>,
2024-09-22 02:00:54 +03:00
}
2024-09-21 20:48:12 +03:00
2024-09-22 17:34:22 +03:00
impl Browser {
2024-09-23 13:58:56 +03:00
// Construct
2024-09-22 17:34:22 +03:00
pub fn new(
2024-10-02 02:14:00 +03:00
// Extras
2024-10-06 01:02:23 +03:00
profile_database_connection: Arc<RwLock<Connection>>,
2024-10-04 17:54:25 +03:00
profile_path: PathBuf,
2024-09-30 13:43:46 +03:00
// Actions
action_tool_debug: Arc<SimpleAction>,
action_tool_profile_directory: Arc<SimpleAction>,
2024-09-30 13:43:46 +03:00
action_quit: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
action_tab_append: Arc<SimpleAction>,
action_tab_close: Arc<SimpleAction>,
action_tab_close_all: Arc<SimpleAction>,
2024-09-30 15:02:22 +03:00
action_tab_page_navigation_base: Arc<SimpleAction>,
action_tab_page_navigation_history_back: Arc<SimpleAction>,
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
2024-09-30 14:49:37 +03:00
action_tab_page_navigation_reload: Arc<SimpleAction>,
2024-09-30 13:43:46 +03:00
action_tab_pin: Arc<SimpleAction>,
2024-09-23 18:51:48 +03:00
) -> Browser {
2024-09-30 13:43:46 +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,
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-06 00:43:35 +03:00
};
// Init new transaction
let transaction = match connection.transaction() {
Ok(transaction) => transaction,
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-06 00:43:35 +03:00
};
// Init database structure
match Database::init(&transaction) {
Ok(database) => match transaction.commit() {
Ok(_) => Arc::new(database),
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:43:35 +03:00
}
2024-10-04 21:23:38 +03:00
};
2024-09-23 01:57:16 +03:00
// Init components
let header = Arc::new(Header::new(
action_tool_debug.clone(),
action_tool_profile_directory.clone(),
2024-09-28 03:10:07 +03:00
action_quit.clone(),
action_tab_append.clone(),
action_tab_close.clone(),
action_tab_close_all.clone(),
2024-09-30 15:02:22 +03:00
action_tab_page_navigation_base.clone(),
action_tab_page_navigation_history_back.clone(),
action_tab_page_navigation_history_forward.clone(),
2024-09-30 14:49:37 +03:00
action_tab_page_navigation_reload.clone(),
2024-09-28 03:10:07 +03:00
action_tab_pin.clone(),
));
2024-09-28 01:29:05 +03:00
2024-10-06 04:39:00 +03:00
let window = Arc::new(Window::new(
action_tab_page_navigation_base.clone(),
2024-09-30 17:45:44 +03:00
action_tab_page_navigation_history_back.clone(),
action_tab_page_navigation_history_forward.clone(),
2024-09-30 14:49:37 +03:00
action_tab_page_navigation_reload.clone(),
2024-09-28 03:10:07 +03:00
action_update.clone(),
));
2024-09-23 01:57:16 +03:00
2024-09-28 01:29:05 +03:00
// Init widget
2024-10-05 03:27:09 +03:00
let widget = Arc::new(Widget::new(
profile_database_connection.clone(),
2024-10-06 05:23:19 +03:00
header.gobject(),
2024-10-06 05:16:29 +03:00
window.gobject(),
2024-10-05 03:27:09 +03:00
));
2024-09-23 18:51:48 +03:00
2024-10-03 01:30:13 +03:00
// Assign actions
widget.gobject().add_action(action_tool_debug.as_ref());
2024-10-05 03:27:09 +03:00
widget
.gobject()
2024-10-05 03:27:09 +03:00
.add_action(action_tool_profile_directory.as_ref());
widget.gobject().add_action(action_quit.as_ref());
widget.gobject().add_action(action_update.as_ref());
widget.gobject().add_action(action_tab_append.as_ref());
widget.gobject().add_action(action_tab_close.as_ref());
widget.gobject().add_action(action_tab_close_all.as_ref());
2024-10-05 03:27:09 +03:00
widget
.gobject()
2024-10-05 03:27:09 +03:00
.add_action(action_tab_page_navigation_base.as_ref());
widget
.gobject()
2024-10-05 03:27:09 +03:00
.add_action(action_tab_page_navigation_history_back.as_ref());
widget
.gobject()
2024-10-05 03:27:09 +03:00
.add_action(action_tab_page_navigation_history_forward.as_ref());
widget
.gobject()
2024-10-05 03:27:09 +03:00
.add_action(action_tab_page_navigation_reload.as_ref());
widget.gobject().add_action(action_tab_pin.as_ref());
2024-10-03 20:03:43 +03:00
// Init events
action_tool_debug.connect_activate({
2024-10-03 20:03:43 +03:00
let widget = widget.clone();
2024-09-28 01:29:05 +03:00
move |_, _| {
widget.gobject().emit_enable_debugging(true);
2024-09-28 01:29:05 +03:00
}
});
action_tool_profile_directory.connect_activate({
move |_, _| {
2024-10-04 17:54:25 +03:00
// @TODO [4_10] https://docs.gtk.org/gtk4/class.FileLauncher.html
let _ = AppInfo::launch_default_for_uri(
&format!("file://{}", profile_path.to_string_lossy()),
Some(&AppLaunchContext::new()),
);
}
});
2024-10-03 20:03:43 +03:00
action_quit.connect_activate({
let widget = widget.clone();
2024-09-28 01:29:05 +03:00
move |_, _| {
widget.gobject().close();
2024-09-28 01:29:05 +03:00
}
});
2024-10-03 20:03:43 +03:00
action_update.connect_activate({
let header = header.clone();
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-09-28 01:29:05 +03:00
move |_, _| {
2024-10-06 04:39:00 +03:00
window.update();
header.update(window.tab_page_title(), window.tab_page_description());
2024-09-28 01:29:05 +03:00
}
});
2024-10-03 20:03:43 +03:00
action_tab_append.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_append(None);
}
});
2024-10-03 20:03:43 +03:00
action_tab_close.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_close();
}
});
2024-10-03 20:03:43 +03:00
action_tab_close_all.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_close_all();
}
});
2024-10-03 20:03:43 +03:00
action_tab_page_navigation_base.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-09-30 15:02:22 +03:00
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_page_navigation_base();
2024-09-30 15:02:22 +03:00
}
});
2024-10-03 20:03:43 +03:00
action_tab_page_navigation_history_back.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-10-03 20:03:43 +03:00
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_page_navigation_history_back();
2024-10-03 20:03:43 +03:00
}
});
action_tab_page_navigation_history_forward.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-10-03 20:03:43 +03:00
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_page_navigation_history_forward();
2024-10-03 20:03:43 +03:00
}
});
action_tab_page_navigation_reload.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_page_navigation_reload();
}
});
2024-10-03 20:03:43 +03:00
action_tab_pin.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
2024-10-06 04:39:00 +03:00
window.tab_pin();
}
});
2024-10-03 20:03:43 +03:00
// Return new activated Browser struct
Self {
2024-10-04 21:23:38 +03:00
database,
2024-10-03 20:03:43 +03:00
widget,
// header,
2024-10-06 04:39:00 +03:00
// window,
2024-10-03 20:03:43 +03:00
}
2024-09-22 17:34:22 +03:00
}
2024-09-19 18:08:09 +03:00
2024-10-04 20:20:53 +03:00
// Actions
2024-10-06 00:43:35 +03:00
pub fn clean(&self, tx: &Transaction, app_id: &i64) {
match self.database.records(tx, app_id) {
2024-10-04 21:23:38 +03:00
Ok(records) => {
for record in records {
2024-10-06 00:43:35 +03:00
match self.database.delete(tx, &record.id) {
2024-10-04 21:23:38 +03:00
Ok(_) => {
// Delegate clean action to childs
2024-10-05 03:27:09 +03:00
// @TODO
2024-10-04 21:23:38 +03:00
// self.header.clean(record.id);
2024-10-06 04:39:00 +03:00
// self.window.clean(record.id);
2024-10-06 00:43:35 +03:00
self.widget.clean(tx, &record.id);
2024-10-04 21:23:38 +03:00
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-04 21:23:38 +03:00
}
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-04 21:23:38 +03:00
}
}
2024-10-06 00:43:35 +03:00
pub fn restore(&self, tx: &Transaction, app_id: &i64) {
match self.database.records(tx, app_id) {
2024-10-05 15:32:03 +03:00
Ok(records) => {
for record in records {
// Delegate restore action to childs
// @TODO
// self.header.restore(record.id);
2024-10-05 17:10:31 +03:00
// self.window.restore(record.id);
2024-10-06 00:43:35 +03:00
self.widget.restore(tx, &record.id);
2024-10-05 15:32:03 +03:00
}
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-05 15:32:03 +03:00
}
2024-10-04 21:23:38 +03:00
}
2024-10-06 00:43:35 +03:00
pub fn save(&self, tx: &Transaction, app_id: &i64) {
match self.database.add(tx, app_id) {
2024-10-04 21:23:38 +03:00
Ok(_) => {
// Delegate save action to childs
2024-10-06 00:43:35 +03:00
let id = self.database.last_insert_id(tx);
2024-10-05 03:27:09 +03:00
// @TODO
2024-10-04 21:23:38 +03:00
// self.header.save(id);
2024-10-05 17:10:31 +03:00
// self.window.save(id);
2024-10-06 00:43:35 +03:00
self.widget.save(tx, &id);
2024-10-04 21:23:38 +03:00
}
2024-10-06 00:49:43 +03:00
Err(e) => todo!("{e}"),
2024-10-04 21:23:38 +03:00
}
}
2024-10-04 20:20:53 +03:00
2024-09-22 17:34:22 +03:00
// Getters
2024-10-06 05:16:29 +03:00
pub fn gobject(&self) -> &ApplicationWindow {
&self.widget.gobject()
2024-09-22 12:42:34 +03:00
}
2024-09-20 18:02:10 +03:00
}