mod database; mod page; mod widget; use database::Database; use page::Page; use widget::Widget; use adw::TabView; use gtk::{ gio::SimpleAction, glib::{uuid_string_random, GString}, Box, }; use sqlite::Transaction; use std::sync::Arc; pub struct Item { // Auto-generated unique item ID // useful as widget name in GTK actions callback id: GString, // Components page: Arc, widget: Arc, } impl Item { // Construct pub fn new_arc( tab_view: &TabView, // Actions action_tab_page_navigation_base: Arc, action_tab_page_navigation_history_back: Arc, action_tab_page_navigation_history_forward: Arc, action_tab_page_navigation_reload: Arc, action_update: Arc, // Options is_selected_page: bool, ) -> Arc { // Generate unique ID for new page components let id = uuid_string_random(); // Init components let page = Page::new_arc( id.clone(), action_tab_page_navigation_base.clone(), action_tab_page_navigation_history_back.clone(), action_tab_page_navigation_history_forward.clone(), action_tab_page_navigation_reload.clone(), action_update.clone(), ); let widget = Widget::new_arc(tab_view, page.gobject(), Some("New page"), is_selected_page); // @TODO // Return struct Arc::new(Self { id, page, widget }) } // Actions pub fn pin(&self) { //self.label.pin(!self.label.is_pinned()) // toggle } pub fn page_navigation_base(&self) { self.page.navigation_base() } pub fn page_navigation_history_back(&self) { self.page.navigation_history_back() } pub fn page_navigation_history_forward(&self) { self.page.navigation_history_forward() } pub fn page_navigation_reload(&self) { self.page.navigation_reload() } pub fn page_navigation_request_grab_focus(&self) { self.page.navigation_request_grab_focus() } pub fn update(&self) { self.page.update(); } pub fn clean( &self, transaction: &Transaction, app_browser_window_tab_id: &i64, ) -> Result<(), String> { match Database::records(transaction, app_browser_window_tab_id) { Ok(records) => { for record in records { match Database::delete(transaction, &record.id) { Ok(_) => { // Delegate clean action to the item childs /* @TODO self.page.clean(transaction, &record.id)?;*/ } Err(e) => return Err(e.to_string()), } } } Err(e) => return Err(e.to_string()), } Ok(()) } // This method does not contain Self context, // because child items creating in the runtime (by parent component) pub fn restore( tab_view: &TabView, transaction: &Transaction, app_browser_window_tab_id: &i64, // Actions action_tab_page_navigation_base: Arc, action_tab_page_navigation_history_back: Arc, action_tab_page_navigation_history_forward: Arc, action_tab_page_navigation_reload: Arc, action_update: Arc, ) -> Result>, String> { let mut items = Vec::new(); match Database::records(transaction, app_browser_window_tab_id) { Ok(records) => { for record in records { // Construct new item object let item = Item::new_arc( tab_view, // Actions action_tab_page_navigation_base.clone(), action_tab_page_navigation_history_back.clone(), action_tab_page_navigation_history_forward.clone(), action_tab_page_navigation_reload.clone(), action_update.clone(), // Options true, ); // Delegate restore action to the item childs /* @TODO self.page.restore(transaction, &id)?; */ // Result items.push(item); } } Err(e) => return Err(e.to_string()), } Ok(items) } pub fn save( &self, transaction: &Transaction, app_browser_window_tab_id: &i64, page_number: &u32, ) -> Result<(), String> { match Database::add(transaction, app_browser_window_tab_id, page_number) { Ok(_) => { let id = Database::last_insert_id(transaction); // Delegate save action to childs /* @TODO self.page.save(transaction, &id)?; */ } Err(e) => return Err(e.to_string()), } Ok(()) } // Getters pub fn id(&self) -> GString { self.id.clone() } pub fn gobject(&self) -> &Box { &self.page.gobject() } pub fn page_title(&self) -> Option { self.page.title() } pub fn page_description(&self) -> Option { self.page.description() } // 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 /* @TODO Page::migrate(&tx)? */ // Success Ok(()) } }