2024-11-11 01:14:09 +02:00

124 lines
3.1 KiB
Rust

mod database;
mod widget;
use database::Database;
use widget::Widget;
use crate::app::browser::action::Action as BrowserAction;
use gtk::{
gio::SimpleAction,
glib::{Uri, UriFlags},
prelude::EditableExt,
};
use sqlite::Transaction;
use std::rc::Rc;
// Main
pub struct Request {
widget: Rc<Widget>,
}
impl Request {
// Construct
pub fn new(
// Actions
browser_action: Rc<BrowserAction>,
action_page_reload: SimpleAction, // @TODO local `action_page_open`?
) -> Self {
Self {
widget: Rc::new(Widget::new(browser_action, action_page_reload)),
}
}
// Actions
pub fn update(&self, progress_fraction: Option<f64>) {
self.widget.update(progress_fraction);
}
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_item_page_navigation_id) {
Ok(records) => {
for record in records {
match Database::delete(transaction, &record.id) {
Ok(_) => {
// Delegate clean action to the item childs
self.widget.clean(transaction, &record.id)?;
}
Err(e) => return Err(e.to_string()),
}
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn restore(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_item_page_navigation_id) {
Ok(records) => {
for record in records {
// Delegate restore action to the item childs
self.widget.restore(transaction, &record.id)?;
}
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
pub fn save(
&self,
transaction: &Transaction,
app_browser_window_tab_item_page_navigation_id: &i64,
) -> Result<(), String> {
match Database::add(transaction, app_browser_window_tab_item_page_navigation_id) {
Ok(_) => {
let id = Database::last_insert_id(transaction);
// Delegate save action to childs
self.widget.save(transaction, &id)?;
}
Err(e) => return Err(e.to_string()),
}
Ok(())
}
// Getters
pub fn widget(&self) -> &Rc<Widget> {
&self.widget
}
pub fn uri(&self) -> Option<Uri> {
match Uri::parse(&self.widget.gobject().text(), UriFlags::NONE) {
Ok(uri) => Some(uri),
_ => None,
}
}
}
// Tools
pub fn migrate(tx: &Transaction) -> Result<(), String> {
// Migrate self components
if let Err(e) = Database::init(tx) {
return Err(e.to_string());
}
// Delegate migration to childs
widget::migrate(tx)?;
// Success
Ok(())
}