diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index b879258b..4fdb1b3a 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -103,6 +103,7 @@ impl Item { match Database::delete(transaction, &record.id) { Ok(_) => { // Delegate clean action to the item childs + self.widget.clean(transaction, &record.id)?; /* @TODO self.page.clean(transaction, &record.id)?;*/ @@ -150,6 +151,7 @@ impl Item { ); // Delegate restore action to the item childs + item.widget.restore(transaction, &record.id)?; /* @TODO self.page.restore(transaction, &id)?; */ @@ -180,9 +182,10 @@ impl Item { is_selected, ) { Ok(_) => { - let _id = Database::last_insert_id(transaction); + let id = Database::last_insert_id(transaction); // Delegate save action to childs + self.widget.save(transaction, &id)?; /* @TODO self.page.save(transaction, &id)?; */ @@ -218,6 +221,7 @@ impl Item { } // Delegate migration to childs + Widget::migrate(&tx)?; /* @TODO Page::migrate(&tx)? */ diff --git a/src/app/browser/window/tab/item/widget.rs b/src/app/browser/window/tab/item/widget.rs index e0b603dd..d22c71b2 100644 --- a/src/app/browser/window/tab/item/widget.rs +++ b/src/app/browser/window/tab/item/widget.rs @@ -1,5 +1,10 @@ +mod database; + +use database::Database; + use adw::{TabPage, TabView}; use gtk::Box; +use sqlite::Transaction; use std::sync::Arc; const DEFAULT_TITLE: &str = "New page"; @@ -36,8 +41,92 @@ impl Widget { Arc::new(Self { gobject }) } + // Actions + + pub fn clean( + &self, + transaction: &Transaction, + app_browser_window_tab_item_id: &i64, + ) -> Result<(), String> { + match Database::records(transaction, app_browser_window_tab_item_id) { + Ok(records) => { + for record in records { + match Database::delete(transaction, &record.id) { + Ok(_) => { + // Delegate clean action to the item childs + // nothing yet.. + } + 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_id: &i64, + ) -> Result<(), String> { + match Database::records(transaction, app_browser_window_tab_item_id) { + Ok(records) => { + for record in records { + // Record value can be stored as NULL + if let Some(title) = record.title { + self.gobject.set_title(title.as_str()); + } + + // Delegate restore action to the item childs + // nothing yet.. + } + } + Err(e) => return Err(e.to_string()), + } + + Ok(()) + } + + pub fn save( + &self, + transaction: &Transaction, + app_browser_window_tab_item_id: &i64, + ) -> Result<(), String> { + match Database::add( + transaction, + app_browser_window_tab_item_id, + Some(&self.gobject.title().to_string()), + ) { + Ok(_) => { + // let id = Database::last_insert_id(transaction); + + // Delegate save action to childs + // nothing yet.. + } + Err(e) => return Err(e.to_string()), + } + + Ok(()) + } + // Getters pub fn gobject(&self) -> &TabPage { &self.gobject } + + // 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 + // nothing yet.. + + // Success + Ok(()) + } } diff --git a/src/app/browser/window/tab/item/widget/database.rs b/src/app/browser/window/tab/item/widget/database.rs new file mode 100644 index 00000000..eeb108ce --- /dev/null +++ b/src/app/browser/window/tab/item/widget/database.rs @@ -0,0 +1,81 @@ +use sqlite::{Error, Transaction}; + +pub struct Table { + pub id: i64, + // pub app_browser_window_tab_item_id: i64, not in use + pub title: Option, +} + +pub struct Database { + // nothing yet.. +} + +impl Database { + pub fn init(tx: &Transaction) -> Result { + tx.execute( + "CREATE TABLE IF NOT EXISTS `app_browser_window_tab_item_widget` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `app_browser_window_tab_item_id` INTEGER NOT NULL, + `title` TEXT + )", + [], + ) + } + + pub fn add( + tx: &Transaction, + app_browser_window_tab_item_id: &i64, + title: Option<&String>, + ) -> Result { + tx.execute( + "INSERT INTO `app_browser_window_tab_item_widget` ( + `app_browser_window_tab_item_id`, + `title` + ) VALUES (?, ?)", + (app_browser_window_tab_item_id, title), + ) + } + + pub fn records( + tx: &Transaction, + app_browser_window_tab_item_id: &i64, + ) -> Result, Error> { + let mut stmt = tx.prepare( + "SELECT `id`, + `app_browser_window_tab_item_id`, + `title` + FROM `app_browser_window_tab_item_widget` + WHERE `app_browser_window_tab_item_id` = ?", + )?; + + let result = stmt.query_map([app_browser_window_tab_item_id], |row| { + Ok(Table { + id: row.get(0)?, + // app_browser_window_tab_item_id: row.get(1)?, not in use + title: row.get(2)?, + }) + })?; + + let mut records = Vec::new(); + + for record in result { + let table = record?; + records.push(table); + } + + Ok(records) + } + + pub fn delete(tx: &Transaction, id: &i64) -> Result { + tx.execute( + "DELETE FROM `app_browser_window_tab_item_widget` WHERE `id` = ?", + [id], + ) + } + + /* not in use + pub fn last_insert_id(tx: &Transaction) -> i64 { + tx.last_insert_rowid() + } */ +}