diff --git a/src/app/browser/window/tab/item/page/navigation.rs b/src/app/browser/window/tab/item/page/navigation.rs index a63577f5..fb47041b 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -120,7 +120,7 @@ impl Navigation { match Database::delete(transaction, &record.id) { Ok(_) => { // Delegate clean action to the item childs - // nothing yet.. + self.request.clean(transaction, &record.id)?; } Err(e) => return Err(e.to_string()), } @@ -139,9 +139,9 @@ impl Navigation { ) -> Result<(), String> { match Database::records(transaction, app_browser_window_tab_item_page_id) { Ok(records) => { - for _record in records { + for record in records { // Delegate restore action to the item childs - // nothing yet.. + self.request.restore(transaction, &record.id)?; } } Err(e) => return Err(e.to_string()), @@ -157,10 +157,10 @@ impl Navigation { ) -> Result<(), String> { match Database::add(transaction, app_browser_window_tab_item_page_id) { Ok(_) => { - // let id = Database::last_insert_id(transaction); + let id = Database::last_insert_id(transaction); // Delegate save action to childs - // nothing yet.. + self.request.save(transaction, &id)?; } Err(e) => return Err(e.to_string()), } @@ -197,7 +197,7 @@ impl Navigation { } // Delegate migration to childs - // nothing yet.. + Request::migrate(tx)?; // Success Ok(()) diff --git a/src/app/browser/window/tab/item/page/navigation/database.rs b/src/app/browser/window/tab/item/page/navigation/database.rs index 2953cc71..942e5550 100644 --- a/src/app/browser/window/tab/item/page/navigation/database.rs +++ b/src/app/browser/window/tab/item/page/navigation/database.rs @@ -68,8 +68,7 @@ impl Database { ) } - /* not in use pub fn last_insert_id(tx: &Transaction) -> i64 { tx.last_insert_rowid() - } */ + } } diff --git a/src/app/browser/window/tab/item/page/navigation/request.rs b/src/app/browser/window/tab/item/page/navigation/request.rs index 82140579..26211bd2 100644 --- a/src/app/browser/window/tab/item/page/navigation/request.rs +++ b/src/app/browser/window/tab/item/page/navigation/request.rs @@ -1,10 +1,14 @@ +mod database; + +use database::Database; + use gtk::{ gio::SimpleAction, glib::{timeout_add_local, ControlFlow, GString, SourceId, Uri, UriFlags}, prelude::{ActionExt, EditableExt, EntryExt}, Entry, }; - +use sqlite::Transaction; use std::{cell::RefCell, sync::Arc, time::Duration}; // Progressbar animation setup @@ -97,6 +101,78 @@ impl Request { } } + 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 + // 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_page_navigation_id: &i64, + ) -> Result<(), String> { + match Database::records(transaction, app_browser_window_tab_item_page_navigation_id) { + Ok(records) => { + for record in records { + if let Some(text) = record.text { + self.widget.set_text(&text); + } + + // 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_page_navigation_id: &i64, + ) -> Result<(), String> { + let text = self.widget.text(); + + match Database::add( + transaction, + app_browser_window_tab_item_page_navigation_id, + match text.is_empty() { + true => None, + false => Some(text.as_str()), + }, + ) { + Ok(_) => { + // let id = Database::last_insert_id(transaction); + + // Delegate save action to childs + // nothing yet.. + } + Err(e) => return Err(e.to_string()), + } + + Ok(()) + } + // Setters pub fn set_text(&self, value: &GString) { self.widget.set_text(value); @@ -121,4 +197,18 @@ impl Request { _ => 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 + // nothing yet.. + + // Success + Ok(()) + } } diff --git a/src/app/browser/window/tab/item/page/navigation/request/database.rs b/src/app/browser/window/tab/item/page/navigation/request/database.rs new file mode 100644 index 00000000..b66fdb80 --- /dev/null +++ b/src/app/browser/window/tab/item/page/navigation/request/database.rs @@ -0,0 +1,81 @@ +use sqlite::{Error, Transaction}; + +pub struct Table { + pub id: i64, + // pub app_browser_window_tab_item_page_navigation_id: i64, not in use + pub text: Option, // can be stored as NULL +} + +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_page_navigation_request` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `app_browser_window_tab_item_page_navigation_id` INTEGER NOT NULL, + `text` VARCHAR(1024) + )", + [], + ) + } + + pub fn add( + tx: &Transaction, + app_browser_window_tab_item_page_navigation_id: &i64, + text: Option<&str>, + ) -> Result { + tx.execute( + "INSERT INTO `app_browser_window_tab_item_page_navigation_request` ( + `app_browser_window_tab_item_page_navigation_id`, + `text` + ) VALUES (?, ?)", + (app_browser_window_tab_item_page_navigation_id, text), + ) + } + + pub fn records( + tx: &Transaction, + app_browser_window_tab_item_page_navigation_id: &i64, + ) -> Result, Error> { + let mut stmt = tx.prepare( + "SELECT `id`, + `app_browser_window_tab_item_page_navigation_id`, + `text` + FROM `app_browser_window_tab_item_page_navigation_request` + WHERE `app_browser_window_tab_item_page_navigation_id` = ?", + )?; + + let result = stmt.query_map([app_browser_window_tab_item_page_navigation_id], |row| { + Ok(Table { + id: row.get(0)?, + // app_browser_window_tab_item_page_navigation_id: row.get(1)?, not in use + text: 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_page_navigation_request` WHERE `id` = ?", + [id], + ) + } + + /* not in use + pub fn last_insert_id(tx: &Transaction) -> i64 { + tx.last_insert_rowid() + } */ +}