From 14b1620dc98a255e94bbd738f5963358f7bfea60 Mon Sep 17 00:00:00 2001 From: yggverse Date: Fri, 11 Oct 2024 22:08:11 +0300 Subject: [PATCH] implement page session restore --- src/app/browser/window/tab/item.rs | 16 +--- src/app/browser/window/tab/item/page.rs | 79 ++++++++++++++++++- .../browser/window/tab/item/page/database.rs | 72 +++++++++++++++++ 3 files changed, 154 insertions(+), 13 deletions(-) create mode 100644 src/app/browser/window/tab/item/page/database.rs diff --git a/src/app/browser/window/tab/item.rs b/src/app/browser/window/tab/item.rs index 4fdb1b3a..376e978a 100644 --- a/src/app/browser/window/tab/item.rs +++ b/src/app/browser/window/tab/item.rs @@ -103,10 +103,8 @@ impl Item { match Database::delete(transaction, &record.id) { Ok(_) => { // Delegate clean action to the item childs + self.page.clean(transaction, &record.id)?; self.widget.clean(transaction, &record.id)?; - - /* @TODO - self.page.clean(transaction, &record.id)?;*/ } Err(e) => return Err(e.to_string()), } @@ -151,11 +149,9 @@ impl Item { ); // Delegate restore action to the item childs + item.page.restore(transaction, &record.id)?; item.widget.restore(transaction, &record.id)?; - /* @TODO - self.page.restore(transaction, &id)?; */ - // Result items.push(item); } @@ -185,10 +181,8 @@ impl Item { let id = Database::last_insert_id(transaction); // Delegate save action to childs + self.page.save(transaction, &id)?; self.widget.save(transaction, &id)?; - - /* @TODO - self.page.save(transaction, &id)?; */ } Err(e) => return Err(e.to_string()), } @@ -221,11 +215,9 @@ impl Item { } // Delegate migration to childs + Page::migrate(&tx)?; Widget::migrate(&tx)?; - /* @TODO - Page::migrate(&tx)? */ - // Success Ok(()) } diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index e061bace..cf2a7df1 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -1,13 +1,16 @@ mod content; +mod database; mod meta; mod navigation; mod widget; use content::Content; -use meta::{Meta, Mime, Status}; +use database::Database; use navigation::Navigation; use widget::Widget; +use meta::{Meta, Mime, Status}; + use gtk::{ gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags}, glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags}, @@ -17,6 +20,7 @@ use gtk::{ }, Box, }; +use sqlite::Transaction; use std::{cell::RefCell, path::Path, sync::Arc}; pub struct Page { @@ -452,6 +456,65 @@ impl Page { // @TODO self.content.update(); } + 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 { + // 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) { + 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 progress_fraction(&self) -> Option { // Interpret status to progress fraction @@ -479,4 +542,18 @@ impl Page { pub fn gobject(&self) -> &Box { &self.widget.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/page/database.rs b/src/app/browser/window/tab/item/page/database.rs new file mode 100644 index 00000000..1aa15d31 --- /dev/null +++ b/src/app/browser/window/tab/item/page/database.rs @@ -0,0 +1,72 @@ +use sqlite::{Error, Transaction}; + +pub struct Table { + pub id: i64, + // pub app_browser_window_tab_item_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` + ( + `id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL, + `app_browser_window_tab_item_id` INTEGER NOT NULL + )", + [], + ) + } + + pub fn add(tx: &Transaction, app_browser_window_tab_item_id: &i64) -> Result { + tx.execute( + "INSERT INTO `app_browser_window_tab_item_page` ( + `app_browser_window_tab_item_id` + ) VALUES (?)", + [app_browser_window_tab_item_id], + ) + } + + 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` + FROM `app_browser_window_tab_item_page` + 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 + }) + })?; + + 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` WHERE `id` = ?", + [id], + ) + } + + /* not in use + pub fn last_insert_id(tx: &Transaction) -> i64 { + tx.last_insert_rowid() + } */ +}