implement page session restore

This commit is contained in:
yggverse 2024-10-11 22:08:11 +03:00
parent 6e9ea25ffd
commit 14b1620dc9
3 changed files with 154 additions and 13 deletions

View File

@ -103,10 +103,8 @@ impl Item {
match Database::delete(transaction, &record.id) { match Database::delete(transaction, &record.id) {
Ok(_) => { Ok(_) => {
// Delegate clean action to the item childs // Delegate clean action to the item childs
self.page.clean(transaction, &record.id)?;
self.widget.clean(transaction, &record.id)?; self.widget.clean(transaction, &record.id)?;
/* @TODO
self.page.clean(transaction, &record.id)?;*/
} }
Err(e) => return Err(e.to_string()), Err(e) => return Err(e.to_string()),
} }
@ -151,11 +149,9 @@ impl Item {
); );
// Delegate restore action to the item childs // Delegate restore action to the item childs
item.page.restore(transaction, &record.id)?;
item.widget.restore(transaction, &record.id)?; item.widget.restore(transaction, &record.id)?;
/* @TODO
self.page.restore(transaction, &id)?; */
// Result // Result
items.push(item); items.push(item);
} }
@ -185,10 +181,8 @@ impl Item {
let id = Database::last_insert_id(transaction); let id = Database::last_insert_id(transaction);
// Delegate save action to childs // Delegate save action to childs
self.page.save(transaction, &id)?;
self.widget.save(transaction, &id)?; self.widget.save(transaction, &id)?;
/* @TODO
self.page.save(transaction, &id)?; */
} }
Err(e) => return Err(e.to_string()), Err(e) => return Err(e.to_string()),
} }
@ -221,11 +215,9 @@ impl Item {
} }
// Delegate migration to childs // Delegate migration to childs
Page::migrate(&tx)?;
Widget::migrate(&tx)?; Widget::migrate(&tx)?;
/* @TODO
Page::migrate(&tx)? */
// Success // Success
Ok(()) Ok(())
} }

View File

@ -1,13 +1,16 @@
mod content; mod content;
mod database;
mod meta; mod meta;
mod navigation; mod navigation;
mod widget; mod widget;
use content::Content; use content::Content;
use meta::{Meta, Mime, Status}; use database::Database;
use navigation::Navigation; use navigation::Navigation;
use widget::Widget; use widget::Widget;
use meta::{Meta, Mime, Status};
use gtk::{ use gtk::{
gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags}, gio::{Cancellable, SimpleAction, SocketClient, SocketProtocol, TlsCertificateFlags},
glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags}, glib::{gformat, GString, Priority, Regex, RegexCompileFlags, RegexMatchFlags, Uri, UriFlags},
@ -17,6 +20,7 @@ use gtk::{
}, },
Box, Box,
}; };
use sqlite::Transaction;
use std::{cell::RefCell, path::Path, sync::Arc}; use std::{cell::RefCell, path::Path, sync::Arc};
pub struct Page { pub struct Page {
@ -452,6 +456,65 @@ impl Page {
// @TODO self.content.update(); // @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 // Getters
pub fn progress_fraction(&self) -> Option<f64> { pub fn progress_fraction(&self) -> Option<f64> {
// Interpret status to progress fraction // Interpret status to progress fraction
@ -479,4 +542,18 @@ impl Page {
pub fn gobject(&self) -> &Box { pub fn gobject(&self) -> &Box {
&self.widget.gobject() &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(())
}
} }

View File

@ -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<usize, Error> {
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<usize, Error> {
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<Vec<Table>, 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<usize, Error> {
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()
} */
}