2024-11-03 19:57:44 +02:00
|
|
|
mod about;
|
2024-11-08 09:19:16 +02:00
|
|
|
mod action;
|
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-11-08 09:19:16 +02:00
|
|
|
use action::Action;
|
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 09:19:16 +02:00
|
|
|
use crate::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-10 07:09:55 +02:00
|
|
|
prelude::{ActionExt, GtkWindowExt, WidgetExt},
|
2024-10-18 10:16:23 +03:00
|
|
|
FileLauncher,
|
2024-09-24 23:08:40 +03:00
|
|
|
};
|
2024-10-07 19:54:28 +03:00
|
|
|
use sqlite::Transaction;
|
2024-11-08 07:46:25 +02:00
|
|
|
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 09:19:16 +02:00
|
|
|
action: Rc<Action>,
|
2024-11-08 05:21:08 +02:00
|
|
|
widget: Rc<Widget>,
|
2024-11-08 09:19:16 +02:00
|
|
|
window: Rc<Window>,
|
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-11-08 07:46:25 +02:00
|
|
|
profile: Rc<Profile>,
|
2024-11-01 03:09:09 +02:00
|
|
|
action_page_close: SimpleAction,
|
|
|
|
action_page_close_all: SimpleAction,
|
|
|
|
action_page_history_back: SimpleAction,
|
|
|
|
action_page_history_forward: SimpleAction,
|
2024-09-23 18:51:48 +03:00
|
|
|
) -> Browser {
|
2024-11-01 03:26:18 +02:00
|
|
|
// Init components
|
2024-11-10 07:09:55 +02:00
|
|
|
let action = Rc::new(Action::new());
|
2024-11-08 05:21:08 +02:00
|
|
|
let window = Rc::new(Window::new(
|
2024-11-08 09:19:16 +02:00
|
|
|
action.clone(),
|
2024-11-01 03:09:09 +02:00
|
|
|
action_page_close.clone(),
|
|
|
|
action_page_close_all.clone(),
|
|
|
|
action_page_history_back.clone(),
|
|
|
|
action_page_history_forward.clone(),
|
2024-09-28 01:53:16 +03:00
|
|
|
));
|
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(),
|
|
|
|
&[
|
|
|
|
action_page_close.clone(),
|
|
|
|
action_page_close_all.clone(),
|
|
|
|
action_page_history_back.clone(),
|
|
|
|
action_page_history_forward.clone(),
|
|
|
|
],
|
|
|
|
));
|
2024-10-03 20:03:43 +03:00
|
|
|
|
2024-11-10 09:19:38 +02:00
|
|
|
// Connect actions (window required for accels)
|
2024-11-10 07:09:55 +02:00
|
|
|
widget
|
|
|
|
.gobject()
|
|
|
|
.insert_action_group(action.id(), Some(action.gobject()));
|
2024-11-08 06:47:58 +02:00
|
|
|
|
2024-11-10 08:53:20 +02:00
|
|
|
widget
|
|
|
|
.gobject()
|
|
|
|
.insert_action_group(window.action().id(), Some(window.action().gobject()));
|
|
|
|
|
2024-11-10 08:51:08 +02:00
|
|
|
widget.gobject().insert_action_group(
|
|
|
|
window.tab().action().id(),
|
|
|
|
Some(window.tab().action().gobject()),
|
2024-11-10 09:19:38 +02:00
|
|
|
);
|
2024-11-10 08:51:08 +02:00
|
|
|
|
2024-11-10 07:09:55 +02:00
|
|
|
// Connect events
|
2024-11-08 09:19:16 +02:00
|
|
|
action.about().connect_activate({
|
2024-11-03 19:57:44 +02:00
|
|
|
let window = window.clone();
|
2024-11-10 07:09:55 +02:00
|
|
|
move || {
|
2024-11-03 19:57:44 +02:00
|
|
|
About::new().present(Some(window.gobject()));
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-10 07:09:55 +02:00
|
|
|
action.close().connect_activate({
|
|
|
|
let widget = widget.clone();
|
|
|
|
move || widget.gobject().close()
|
|
|
|
});
|
|
|
|
|
|
|
|
action.debug().connect_activate({
|
|
|
|
let widget = widget.clone();
|
|
|
|
move || {
|
|
|
|
widget.gobject().emit_enable_debugging(true);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-08 09:19:16 +02:00
|
|
|
action.profile().connect_activate({
|
2024-11-10 07:09:55 +02:00
|
|
|
move || {
|
2024-11-08 07:46:25 +02:00
|
|
|
FileLauncher::new(Some(&File::for_path(profile.config_path()))).launch(
|
2024-10-18 10:16:23 +03:00
|
|
|
None::<>k::Window>,
|
|
|
|
None::<&Cancellable>,
|
|
|
|
|result| {
|
|
|
|
if let Err(error) = result {
|
2024-11-09 08:24:35 +02:00
|
|
|
println!("{error}")
|
2024-10-18 10:16:23 +03:00
|
|
|
}
|
|
|
|
},
|
2024-11-10 07:09:55 +02:00
|
|
|
); // @TODO move out?
|
2024-10-04 16:19:26 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2024-11-08 09:19:16 +02:00
|
|
|
action.update().connect_activate({
|
2024-10-06 04:39:00 +03:00
|
|
|
let window = window.clone();
|
2024-11-10 07:09:55 +02:00
|
|
|
move |tab_item_id| window.update(tab_item_id)
|
2024-09-28 01:29:05 +03:00
|
|
|
});
|
|
|
|
|
2024-11-10 07:09:55 +02:00
|
|
|
// @TODO
|
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-09-28 01:53:16 +03:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
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();
|
2024-09-28 01:53:16 +03:00
|
|
|
move |_, _| {
|
2024-10-06 04:39:00 +03:00
|
|
|
window.tab_close_all();
|
2024-09-28 01:53:16 +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:26:18 +02:00
|
|
|
// Return new activated `Self`
|
2024-10-03 20:03:43 +03:00
|
|
|
Self {
|
2024-11-08 09:19:16 +02:00
|
|
|
action,
|
2024-10-03 20:03:43 +03:00
|
|
|
widget,
|
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
|
2024-11-08 09:19:16 +02:00
|
|
|
|
2024-10-07 21:10:12 +03:00
|
|
|
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 {
|
2024-10-07 21:10:12 +03:00
|
|
|
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)?;
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
/* @TODO
|
2024-10-08 03:41:51 +03:00
|
|
|
self.header.clean(transaction, &record.id)?; */
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 21:10:12 +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)?;
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
/* @TODO
|
2024-10-08 03:41:51 +03:00
|
|
|
self.header.restore(transaction, &record.id)?; */
|
2024-10-05 15:32:03 +03:00
|
|
|
}
|
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-05 15:32:03 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
|
|
|
|
2024-10-07 21:10:12 +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(_) => {
|
2024-10-07 21:10:12 +03:00
|
|
|
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)?;
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
/* @TODO
|
2024-10-08 03:41:51 +03:00
|
|
|
self.header.save(transaction, &id)?; */
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
Err(e) => return Err(e.to_string()),
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
2024-10-07 21:10:12 +03:00
|
|
|
|
|
|
|
Ok(())
|
2024-10-04 21:23:38 +03:00
|
|
|
}
|
2024-10-04 20:20:53 +03:00
|
|
|
|
2024-10-12 01:08:16 +03:00
|
|
|
pub fn init(&self) {
|
|
|
|
self.window.init();
|
|
|
|
}
|
|
|
|
|
2024-11-10 07:09:55 +02:00
|
|
|
pub fn update(&self) {
|
|
|
|
self.window.update(None);
|
|
|
|
}
|
|
|
|
|
2024-09-22 17:34:22 +03:00
|
|
|
// Getters
|
2024-11-08 09:19:16 +02:00
|
|
|
|
|
|
|
pub fn action(&self) -> &Rc<Action> {
|
|
|
|
&self.action
|
|
|
|
}
|
|
|
|
|
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
|
|
|
}
|
2024-11-10 08:07:44 +02:00
|
|
|
|
|
|
|
pub fn window(&self) -> &Rc<Window> {
|
|
|
|
&self.window
|
|
|
|
}
|
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
|
|
|
|
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) {
|
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
|
|
|
|
/* @TODO
|
2024-11-08 05:03:02 +02:00
|
|
|
header::migrate(tx)?; */
|
|
|
|
window::migrate(tx)?;
|
|
|
|
widget::migrate(tx)?;
|
2024-10-07 19:54:28 +03:00
|
|
|
|
2024-11-03 17:05:32 +02:00
|
|
|
// 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
|
|
|
}
|