From d7b781f604465e38f5f8d8080f9c04326ee3cb23 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 11 Oct 2024 23:18:59 +0300 Subject: [PATCH] implement navigation session restore --- src/app/browser/window/tab/item/page.rs | 14 ++-- .../browser/window/tab/item/page/database.rs | 3 +- .../window/tab/item/page/navigation.rs | 76 +++++++++++++++++++ .../tab/item/page/navigation/database.rs | 75 ++++++++++++++++++ 4 files changed, 159 insertions(+), 9 deletions(-) create mode 100644 src/app/browser/window/tab/item/page/navigation/database.rs diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index 9bf49d24..3910231d 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -467,7 +467,7 @@ impl Page { match Database::delete(transaction, &record.id) { Ok(_) => { // Delegate clean action to the item childs - // nothing yet.. + self.navigation.clean(transaction, &record.id)?; } Err(e) => return Err(e.to_string()), } @@ -486,9 +486,9 @@ impl Page { ) -> Result<(), String> { match Database::records(transaction, app_browser_window_tab_item_id) { Ok(records) => { - for _record in records { + for record in records { // Delegate restore action to the item childs - // nothing yet.. + self.navigation.restore(transaction, &record.id)?; } } Err(e) => return Err(e.to_string()), @@ -504,10 +504,10 @@ impl Page { ) -> Result<(), String> { match Database::add(transaction, app_browser_window_tab_item_id) { Ok(_) => { - // let id = Database::last_insert_id(transaction); + let id = Database::last_insert_id(transaction); // Delegate save action to childs - // nothing yet.. + self.navigation.save(transaction, &id)?; } Err(e) => return Err(e.to_string()), } @@ -546,12 +546,12 @@ impl Page { // Tools pub fn migrate(tx: &Transaction) -> Result<(), String> { // Migrate self components - if let Err(e) = Database::init(&tx) { + if let Err(e) = Database::init(tx) { return Err(e.to_string()); } // Delegate migration to childs - // nothing yet.. + Navigation::migrate(tx)?; // Success Ok(()) diff --git a/src/app/browser/window/tab/item/page/database.rs b/src/app/browser/window/tab/item/page/database.rs index 1aa15d31..94a1b41f 100644 --- a/src/app/browser/window/tab/item/page/database.rs +++ b/src/app/browser/window/tab/item/page/database.rs @@ -65,8 +65,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.rs b/src/app/browser/window/tab/item/page/navigation.rs index 561ecf2d..a63577f5 100644 --- a/src/app/browser/window/tab/item/page/navigation.rs +++ b/src/app/browser/window/tab/item/page/navigation.rs @@ -1,11 +1,13 @@ mod base; mod bookmark; +mod database; mod history; mod reload; mod request; use base::Base; use bookmark::Bookmark; +use database::Database; use history::History; use reload::Reload; use request::Request; @@ -16,6 +18,7 @@ use gtk::{ prelude::{BoxExt, WidgetExt}, Box, DirectionType, Orientation, }; +use sqlite::Transaction; use std::sync::Arc; @@ -106,6 +109,65 @@ impl Navigation { self.bookmark.update(); } + pub fn clean( + &self, + transaction: &Transaction, + app_browser_window_tab_item_page_id: &i64, + ) -> Result<(), String> { + match Database::records(transaction, app_browser_window_tab_item_page_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_id: &i64, + ) -> Result<(), String> { + match Database::records(transaction, app_browser_window_tab_item_page_id) { + Ok(records) => { + for _record in records { + // 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_id: &i64, + ) -> Result<(), String> { + match Database::add(transaction, app_browser_window_tab_item_page_id) { + 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_request_text(&self, value: &GString) { // Focus out from content area on activate the link @TODO @@ -126,4 +188,18 @@ impl Navigation { pub fn request_text(&self) -> GString { self.request.text() } + + // 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/database.rs b/src/app/browser/window/tab/item/page/navigation/database.rs new file mode 100644 index 00000000..2953cc71 --- /dev/null +++ b/src/app/browser/window/tab/item/page/navigation/database.rs @@ -0,0 +1,75 @@ +use sqlite::{Error, Transaction}; + +pub struct Table { + pub id: i64, + // pub app_browser_window_tab_item_page_id: i64, not in use +} + +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` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `app_browser_window_tab_item_page_id` INTEGER NOT NULL + )", + [], + ) + } + + pub fn add( + tx: &Transaction, + app_browser_window_tab_item_page_id: &i64, + ) -> Result { + tx.execute( + "INSERT INTO `app_browser_window_tab_item_page_navigation` ( + `app_browser_window_tab_item_page_id` + ) VALUES (?)", + [app_browser_window_tab_item_page_id], + ) + } + + pub fn records( + tx: &Transaction, + app_browser_window_tab_item_page_id: &i64, + ) -> Result, Error> { + let mut stmt = tx.prepare( + "SELECT `id`, + `app_browser_window_tab_item_page_id` + FROM `app_browser_window_tab_item_page_navigation` + WHERE `app_browser_window_tab_item_page_id` = ?", + )?; + + let result = stmt.query_map([app_browser_window_tab_item_page_id], |row| { + Ok(Table { + id: row.get(0)?, + // app_browser_window_tab_item_page_id: row.get(1)?, not in use + }) + })?; + + 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` WHERE `id` = ?", + [id], + ) + } + + /* not in use + pub fn last_insert_id(tx: &Transaction) -> i64 { + tx.last_insert_rowid() + } */ +}