implement session save/restore for page meta data

This commit is contained in:
yggverse 2024-11-03 16:49:57 +02:00
parent 9928e9c5b3
commit 51a38c98b2
4 changed files with 183 additions and 1 deletions

View file

@ -1,7 +1,11 @@
mod database;
mod redirect;
use database::Database;
use redirect::Redirect;
use gtk::glib::GString;
use sqlite::Transaction;
use std::{cell::RefCell, sync::Arc};
#[derive(Debug, Clone)]
@ -98,4 +102,94 @@ impl Meta {
pub fn take_redirect(&self) -> Option<Redirect> {
self.redirect.take()
}
// Actions
pub fn clean(
&self,
transaction: &Transaction,
app_browser_window_tab_page_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_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_page_id: &i64,
) -> Result<(), String> {
match Database::records(transaction, app_browser_window_tab_page_id) {
Ok(records) => {
for record in records {
// Record value can be stored as NULL
if let Some(title) = record.title {
self.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_page_id: &i64,
) -> Result<(), String> {
// Keep value in memory until operation complete
let title = self.title();
match Database::add(
transaction,
app_browser_window_tab_page_id,
match title.is_empty() {
true => None,
false => Some(title.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(())
}
// 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(())
}
}