Yoda/src/app/browser.rs

201 lines
5.2 KiB
Rust
Raw Normal View History

2024-11-03 19:57:44 +02:00
mod about;
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;
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
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::{
gio::{Cancellable, File},
prelude::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 {
action: Rc<Action>,
2024-11-08 05:21:08 +02:00
widget: Rc<Widget>,
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
pub fn new(profile: Rc<Profile>) -> 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());
let window = Rc::new(Window::new(action.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-11 05:11:48 +02:00
window.widget().gobject(),
2024-11-01 03:23:49 +02:00
&[
// Connect action groups (to apply accels)
(
// Browser
action.id(),
action.gobject().clone(),
),
(
// Window
window.action().id(),
window.action().gobject().clone(),
),
2024-11-01 03:23:49 +02:00
],
));
2024-10-03 20:03:43 +03:00
2024-11-10 07:09:55 +02:00
// Connect events
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-11 05:11:48 +02:00
About::new().present(Some(window.widget().gobject()));
2024-11-03 19:57:44 +02:00
}
});
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);
}
});
action.profile().connect_activate({
2024-11-10 07:09:55 +02:00
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 {
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?
}
});
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-01 03:26:18 +02:00
// Return new activated `Self`
2024-10-03 20:03:43 +03:00
Self {
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
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-11-10 07:09:55 +02:00
pub fn update(&self) {
self.window.update(None);
}
2024-09-22 17:34:22 +03:00
// Getters
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
}
}
// 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
}