Yoda/src/app/browser.rs

293 lines
8.5 KiB
Rust
Raw Normal View History

2024-11-03 19:57:44 +02:00
mod about;
2024-10-04 21:23:38 +03:00
mod database;
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-11-03 19:57:44 +02:00
use about::About;
2024-10-04 21:23:38 +03:00
use database::Database;
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
2024-11-08 08:43:02 +02:00
use crate::{action::Browser as BrowserAction, profile::Profile};
2024-10-09 10:50:41 +03:00
use adw::ApplicationWindow;
2024-09-24 23:08:40 +03:00
use gtk::{
2024-10-18 10:16:23 +03:00
gio::{Cancellable, File, SimpleAction},
2024-11-04 19:35:39 +02:00
glib::Variant,
2024-11-04 04:52:33 +02:00
prelude::{ActionExt, GtkWindowExt},
2024-10-18 10:16:23 +03:00
FileLauncher,
2024-09-24 23:08:40 +03:00
};
use sqlite::Transaction;
use std::rc::Rc;
2024-09-23 01:57:16 +03:00
2024-09-22 02:00:54 +03:00
pub struct Browser {
2024-11-08 05:21:08 +02:00
window: Rc<Window>,
widget: Rc<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(
profile: Rc<Profile>,
browser_action: Rc<BrowserAction>,
2024-11-01 03:09:09 +02:00
action_page_new: SimpleAction,
action_page_close: SimpleAction,
action_page_close_all: SimpleAction,
2024-11-01 03:14:11 +02:00
action_page_home: SimpleAction,
2024-11-01 03:09:09 +02:00
action_page_history_back: SimpleAction,
action_page_history_forward: SimpleAction,
action_page_reload: SimpleAction,
action_page_pin: SimpleAction,
2024-09-23 18:51:48 +03:00
) -> Browser {
2024-11-01 03:26:18 +02:00
// Init components
2024-11-08 05:21:08 +02:00
let window = Rc::new(Window::new(
browser_action.clone(),
2024-11-01 03:09:09 +02:00
action_page_new.clone(),
action_page_close.clone(),
action_page_close_all.clone(),
2024-11-01 03:14:11 +02:00
action_page_home.clone(),
2024-11-01 03:09:09 +02:00
action_page_history_back.clone(),
action_page_history_forward.clone(),
action_page_reload.clone(),
action_page_pin.clone(),
));
2024-09-28 01:29:05 +03:00
// Init widget
2024-11-08 05:21:08 +02:00
let widget = Rc::new(Widget::new(
2024-11-01 03:23:49 +02:00
window.gobject(),
&[
browser_action.about().clone(),
browser_action.debug().clone(),
browser_action.profile().clone(),
browser_action.quit().clone(),
browser_action.update().clone(),
2024-11-01 03:23:49 +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 20:03:43 +03:00
// Init events
// Browser actions
browser_action.about().connect_activate({
2024-11-03 19:57:44 +02:00
let window = window.clone();
move |_, _| {
About::new().present(Some(window.gobject()));
}
});
browser_action.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
}
});
browser_action.profile().connect_activate({
move |_, _| {
FileLauncher::new(Some(&File::for_path(profile.config_path()))).launch(
2024-10-18 10:16:23 +03:00
None::<&gtk::Window>,
None::<&Cancellable>,
|result| {
if let Err(error) = result {
// @TODO
println!("Could not delegate launch action: {error}")
}
},
2024-10-04 17:54:25 +03:00
);
}
});
browser_action.quit().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().close();
2024-09-28 01:29:05 +03:00
}
});
browser_action.update().connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 19:35:39 +02:00
move |_, this| window.update(string_from_variant(this).as_str())
2024-09-28 01:29:05 +03:00
});
// Other
2024-11-01 03:09:09 +02:00
action_page_new.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
move |_, _| {
window.tab_append(None);
}
});
2024-11-01 03:09:09 +02:00
action_page_close.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 20:22:27 +02:00
move |this, _| {
window.tab_close(page_position_from_action_state(this));
}
});
2024-11-01 03:09:09 +02:00
action_page_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-11-01 03:14:11 +02:00
action_page_home.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 23:04:57 +02:00
move |this, _| {
2024-11-05 03:19:52 +02:00
window.tab_page_home(page_position_from_action_state(this));
2024-09-30 15:02:22 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_page_history_back.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 23:31:34 +02:00
move |this, _| {
2024-11-05 03:19:52 +02:00
window.tab_page_history_back(page_position_from_action_state(this));
2024-10-03 20:03:43 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_page_history_forward.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 23:31:34 +02:00
move |this, _| {
2024-11-05 03:19:52 +02:00
window.tab_page_history_forward(page_position_from_action_state(this));
2024-10-03 20:03:43 +03:00
}
});
2024-11-01 03:09:09 +02:00
action_page_reload.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-05 03:19:52 +02:00
move |this, _| window.tab_page_reload(page_position_from_action_state(this))
});
2024-11-01 03:09:09 +02:00
action_page_pin.connect_activate({
2024-10-06 04:39:00 +03:00
let window = window.clone();
2024-11-04 23:04:57 +02:00
move |this, _| {
window.tab_pin(page_position_from_action_state(this));
}
});
2024-11-01 03:26:18 +02:00
// Return new activated `Self`
2024-10-03 20:03:43 +03:00
Self {
widget,
// header,
2024-10-06 18:20:36 +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
pub fn clean(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_id) {
2024-10-04 21:23:38 +03:00
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
2024-10-04 21:23:38 +03:00
Ok(_) => {
// Delegate clean action to childs
2024-10-08 03:41:51 +03:00
self.window.clean(transaction, &record.id)?;
self.widget.clean(transaction, &record.id)?;
/* @TODO
2024-10-08 03:41:51 +03:00
self.header.clean(transaction, &record.id)?; */
2024-10-04 21:23:38 +03:00
}
Err(e) => return Err(e.to_string()),
2024-10-04 21:23:38 +03:00
}
}
}
Err(e) => return Err(e.to_string()),
2024-10-04 21:23:38 +03:00
}
Ok(())
2024-10-04 21:23:38 +03:00
}
pub fn restore(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match Database::records(transaction, app_id) {
2024-10-05 15:32:03 +03:00
Ok(records) => {
for record in records {
// Delegate restore action to childs
2024-10-08 03:41:51 +03:00
self.widget.restore(transaction, &record.id)?;
self.window.restore(transaction, &record.id)?;
/* @TODO
2024-10-08 03:41:51 +03:00
self.header.restore(transaction, &record.id)?; */
2024-10-05 15:32:03 +03:00
}
}
Err(e) => return Err(e.to_string()),
2024-10-05 15:32:03 +03:00
}
Ok(())
2024-10-04 21:23:38 +03:00
}
pub fn save(&self, transaction: &Transaction, app_id: &i64) -> Result<(), String> {
match Database::add(transaction, app_id) {
2024-10-04 21:23:38 +03:00
Ok(_) => {
let id = Database::last_insert_id(transaction);
2024-10-04 21:23:38 +03:00
// Delegate save action to childs
2024-10-08 03:41:51 +03:00
self.widget.save(transaction, &id)?;
self.window.save(transaction, &id)?;
/* @TODO
2024-10-08 03:41:51 +03:00
self.header.save(transaction, &id)?; */
2024-10-04 21:23:38 +03:00
}
Err(e) => return Err(e.to_string()),
2024-10-04 21:23:38 +03:00
}
Ok(())
2024-10-04 21:23:38 +03:00
}
2024-10-04 20:20:53 +03:00
pub fn init(&self) {
self.window.init();
}
2024-09-22 17:34:22 +03:00
// Getters
2024-10-06 05:16:29 +03:00
pub fn gobject(&self) -> &ApplicationWindow {
2024-11-08 05:09:19 +02:00
self.widget.gobject()
2024-09-22 12:42:34 +03:00
}
}
// Tools
pub 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
/* @TODO
2024-11-08 05:03:02 +02:00
header::migrate(tx)?; */
window::migrate(tx)?;
widget::migrate(tx)?;
// Success
Ok(())
2024-09-20 18:02:10 +03:00
}
2024-11-04 19:35:39 +02:00
2024-11-04 19:40:13 +02:00
// Private helpers @TODO move outside
2024-11-04 19:43:29 +02:00
/// Extract `Optional` page position from C-based
/// [SimpleAction state](https://docs.gtk.org/gio/property.SimpleAction.state.html)
2024-11-04 19:35:39 +02:00
fn page_position_from_action_state(action: &SimpleAction) -> Option<i32> {
let page_position = action
.state()
2024-11-04 19:37:06 +02:00
.expect("Page position required for this action")
2024-11-04 19:35:39 +02:00
.get::<i32>()
2024-11-04 19:44:19 +02:00
.expect("Parameter type does not match `i32`");
2024-11-04 19:35:39 +02:00
if page_position > -1 {
Some(page_position)
} else {
None
}
2024-11-04 19:40:13 +02:00
}
2024-11-04 19:35:39 +02:00
2024-11-04 19:43:29 +02:00
/// Extract `String` from [Variant](https://docs.gtk.org/glib/struct.Variant.html)
2024-11-04 19:35:39 +02:00
fn string_from_variant(variant: Option<&Variant>) -> String {
variant
.expect("Variant required for this action")
.get::<String>()
2024-11-04 19:44:19 +02:00
.expect("Parameter type does not match `String`")
2024-11-04 19:40:13 +02:00
}