mirror of
https://github.com/YGGverse/Yoda.git
synced 2025-01-15 17:20:08 +00:00
draft database features
This commit is contained in:
parent
227cfb30a1
commit
3dffcb029a
@ -24,7 +24,7 @@ pub struct Browser {
|
||||
database: Arc<Database>,
|
||||
// Components
|
||||
// header: Arc<Header>,
|
||||
// window: Arc<Window>,
|
||||
window: Arc<Window>,
|
||||
widget: Arc<Widget>,
|
||||
}
|
||||
|
||||
@ -88,6 +88,7 @@ impl Browser {
|
||||
));
|
||||
|
||||
let window = Arc::new(Window::new(
|
||||
profile_database_connection.clone(),
|
||||
action_tab_page_navigation_base.clone(),
|
||||
action_tab_page_navigation_history_back.clone(),
|
||||
action_tab_page_navigation_history_forward.clone(),
|
||||
@ -221,7 +222,7 @@ impl Browser {
|
||||
database,
|
||||
widget,
|
||||
// header,
|
||||
// window,
|
||||
window,
|
||||
}
|
||||
}
|
||||
|
||||
@ -235,8 +236,7 @@ impl Browser {
|
||||
// Delegate clean action to childs
|
||||
// @TODO
|
||||
// self.header.clean(record.id);
|
||||
// self.window.clean(record.id);
|
||||
|
||||
self.window.clean(tx, &record.id);
|
||||
self.widget.clean(tx, &record.id);
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
@ -254,8 +254,7 @@ impl Browser {
|
||||
// Delegate restore action to childs
|
||||
// @TODO
|
||||
// self.header.restore(record.id);
|
||||
// self.window.restore(record.id);
|
||||
|
||||
self.window.restore(tx, &record.id);
|
||||
self.widget.restore(tx, &record.id);
|
||||
}
|
||||
}
|
||||
@ -271,8 +270,7 @@ impl Browser {
|
||||
|
||||
// @TODO
|
||||
// self.header.save(id);
|
||||
// self.window.save(id);
|
||||
|
||||
self.window.save(tx, &id);
|
||||
self.widget.save(tx, &id);
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
|
@ -1,14 +1,18 @@
|
||||
mod database;
|
||||
mod tab;
|
||||
mod widget;
|
||||
|
||||
use database::Database;
|
||||
use sqlite::{Connection, Transaction};
|
||||
use tab::Tab;
|
||||
use widget::Widget;
|
||||
|
||||
use std::sync::Arc;
|
||||
use std::sync::{Arc, RwLock};
|
||||
|
||||
use gtk::{gio::SimpleAction, glib::GString, Box};
|
||||
|
||||
pub struct Window {
|
||||
database: Arc<Database>,
|
||||
tab: Arc<Tab>,
|
||||
widget: Arc<Widget>,
|
||||
}
|
||||
@ -16,14 +20,42 @@ pub struct Window {
|
||||
impl Window {
|
||||
// Construct
|
||||
pub fn new(
|
||||
// Extras
|
||||
profile_database_connection: Arc<RwLock<Connection>>,
|
||||
// Actions
|
||||
action_tab_page_navigation_base: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_history_back: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_reload: Arc<SimpleAction>,
|
||||
action_update: Arc<SimpleAction>,
|
||||
) -> Self {
|
||||
// Init database
|
||||
let database = {
|
||||
// Init writable database connection
|
||||
let mut connection = match profile_database_connection.write() {
|
||||
Ok(connection) => connection,
|
||||
Err(e) => todo!("{e}"),
|
||||
};
|
||||
|
||||
// Init new transaction
|
||||
let transaction = match connection.transaction() {
|
||||
Ok(transaction) => transaction,
|
||||
Err(e) => todo!("{e}"),
|
||||
};
|
||||
|
||||
// Init database structure
|
||||
match Database::init(&transaction) {
|
||||
Ok(database) => match transaction.commit() {
|
||||
Ok(_) => Arc::new(database),
|
||||
Err(e) => todo!("{e}"),
|
||||
},
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
};
|
||||
|
||||
// Init components
|
||||
let tab = Arc::new(Tab::new(
|
||||
profile_database_connection,
|
||||
action_tab_page_navigation_base,
|
||||
action_tab_page_navigation_history_back,
|
||||
action_tab_page_navigation_history_forward,
|
||||
@ -37,7 +69,11 @@ impl Window {
|
||||
let widget = Arc::new(Widget::new(tab.gobject()));
|
||||
|
||||
// Init struct
|
||||
Self { tab, widget }
|
||||
Self {
|
||||
database,
|
||||
tab,
|
||||
widget,
|
||||
}
|
||||
}
|
||||
|
||||
// Actions
|
||||
@ -77,6 +113,58 @@ impl Window {
|
||||
self.tab.update();
|
||||
}
|
||||
|
||||
pub fn clean(&self, tx: &Transaction, app_browser_id: &i64) {
|
||||
match self.database.records(tx, app_browser_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
match self.database.delete(tx, &record.id) {
|
||||
Ok(_) => {
|
||||
// Delegate clean action to childs
|
||||
// @TODO
|
||||
self.tab.clean(tx, &record.id);
|
||||
|
||||
// self.widget.clean(tx, &record.id);
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restore(&self, tx: &Transaction, app_browser_id: &i64) {
|
||||
match self.database.records(tx, app_browser_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
// Delegate restore action to childs
|
||||
// @TODO
|
||||
// self.header.restore(record.id);
|
||||
// self.window.restore(record.id);
|
||||
|
||||
// self.widget.restore(tx, &record.id);
|
||||
}
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, tx: &Transaction, app_browser_id: &i64) {
|
||||
match self.database.add(tx, app_browser_id) {
|
||||
Ok(_) => {
|
||||
// Delegate save action to childs
|
||||
let id = self.database.last_insert_id(tx);
|
||||
|
||||
// @TODO
|
||||
// self.header.save(id);
|
||||
// self.window.save(id);
|
||||
|
||||
// self.widget.save(tx, &id);
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
pub fn tab_page_title(&self) -> Option<GString> {
|
||||
self.tab.page_title()
|
||||
|
64
src/app/browser/window/database.rs
Normal file
64
src/app/browser/window/database.rs
Normal file
@ -0,0 +1,64 @@
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
// pub app_browser_id: i64, not in use
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app_browser_window`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`app_browser_id` INTEGER NOT NULL
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(&self, tx: &Transaction, app_browser_id: &i64) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `app_browser_window` (`app_browser_id`) VALUES (?)",
|
||||
[app_browser_id],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(&self, tx: &Transaction, app_browser_id: &i64) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare(
|
||||
"SELECT `id`,
|
||||
`app_browser_id` FROM `app_browser_window`
|
||||
WHERE `app_browser_id` = ?",
|
||||
)?;
|
||||
|
||||
let result = stmt.query_map([app_browser_id], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
// app_browser_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(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `app_browser_window` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
}
|
@ -1,9 +1,12 @@
|
||||
mod database;
|
||||
mod label;
|
||||
mod page;
|
||||
mod widget;
|
||||
|
||||
use database::Database;
|
||||
use label::Label;
|
||||
use page::Page;
|
||||
use sqlite::{Connection, Transaction};
|
||||
use widget::Widget;
|
||||
|
||||
use gtk::{
|
||||
@ -13,7 +16,11 @@ use gtk::{
|
||||
GestureClick, Notebook,
|
||||
};
|
||||
|
||||
use std::{cell::RefCell, collections::HashMap, sync::Arc};
|
||||
use std::{
|
||||
cell::RefCell,
|
||||
collections::HashMap,
|
||||
sync::{Arc, RwLock},
|
||||
};
|
||||
|
||||
// Common struct for HashMap index
|
||||
struct TabItem {
|
||||
@ -23,7 +30,9 @@ struct TabItem {
|
||||
|
||||
// Main
|
||||
pub struct Tab {
|
||||
// Keep action links in memory to not require them on every tab append
|
||||
// Extras
|
||||
database: Arc<Database>,
|
||||
// Actions
|
||||
action_tab_page_navigation_base: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_history_back: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
|
||||
@ -38,14 +47,42 @@ pub struct Tab {
|
||||
impl Tab {
|
||||
// Construct
|
||||
pub fn new(
|
||||
// Extras
|
||||
profile_database_connection: Arc<RwLock<Connection>>,
|
||||
// Actions
|
||||
action_tab_page_navigation_base: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_history_back: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_history_forward: Arc<SimpleAction>,
|
||||
action_tab_page_navigation_reload: Arc<SimpleAction>,
|
||||
action_update: Arc<SimpleAction>,
|
||||
) -> Self {
|
||||
// Init database
|
||||
let database = {
|
||||
// Init writable database connection
|
||||
let mut connection = match profile_database_connection.write() {
|
||||
Ok(connection) => connection,
|
||||
Err(e) => todo!("{e}"),
|
||||
};
|
||||
|
||||
// Init new transaction
|
||||
let transaction = match connection.transaction() {
|
||||
Ok(transaction) => transaction,
|
||||
Err(e) => todo!("{e}"),
|
||||
};
|
||||
|
||||
// Init database structure
|
||||
match Database::init(&transaction) {
|
||||
Ok(database) => match transaction.commit() {
|
||||
Ok(_) => Arc::new(database),
|
||||
Err(e) => todo!("{e}"),
|
||||
},
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
};
|
||||
|
||||
// Return non activated struct
|
||||
Self {
|
||||
database,
|
||||
// Define action links
|
||||
action_tab_page_navigation_base,
|
||||
action_tab_page_navigation_history_back,
|
||||
@ -196,6 +233,46 @@ impl Tab {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn clean(&self, tx: &Transaction, app_browser_window_id: &i64) {
|
||||
match self.database.records(tx, app_browser_window_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
match self.database.delete(tx, &record.id) {
|
||||
Ok(_) => {
|
||||
// Delegate clean action to childs
|
||||
// nothing yet..
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn restore(&self, tx: &Transaction, app_browser_window_id: &i64) {
|
||||
match self.database.records(tx, app_browser_window_id) {
|
||||
Ok(records) => {
|
||||
for record in records {
|
||||
// Delegate restore action to childs
|
||||
// nothing yet..
|
||||
}
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, tx: &Transaction, app_browser_window_id: &i64) {
|
||||
match self.database.add(tx, app_browser_window_id) {
|
||||
Ok(_) => {
|
||||
// Delegate save action to childs
|
||||
// let id = self.database.last_insert_id(tx);
|
||||
// nothing yet..
|
||||
}
|
||||
Err(e) => todo!("{e}"),
|
||||
}
|
||||
}
|
||||
|
||||
// Getters
|
||||
pub fn page_title(&self) -> Option<GString> {
|
||||
if let Some(id) = self.widget.current_name() {
|
||||
@ -203,7 +280,6 @@ impl Tab {
|
||||
return item.page.title();
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
@ -214,7 +290,6 @@ impl Tab {
|
||||
return item.page.description();
|
||||
}
|
||||
}
|
||||
|
||||
None
|
||||
}
|
||||
|
||||
|
66
src/app/browser/window/tab/database.rs
Normal file
66
src/app/browser/window/tab/database.rs
Normal file
@ -0,0 +1,66 @@
|
||||
use sqlite::{Error, Transaction};
|
||||
|
||||
pub struct Table {
|
||||
pub id: i64,
|
||||
// pub app_browser_window_id: i64, not in use
|
||||
}
|
||||
|
||||
pub struct Database {
|
||||
// nothing yet..
|
||||
}
|
||||
|
||||
impl Database {
|
||||
pub fn init(tx: &Transaction) -> Result<Database, Error> {
|
||||
tx.execute(
|
||||
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab`
|
||||
(
|
||||
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
`app_browser_window_id` INTEGER NOT NULL
|
||||
)",
|
||||
[],
|
||||
)?;
|
||||
|
||||
Ok(Self {})
|
||||
}
|
||||
|
||||
pub fn add(&self, tx: &Transaction, app_browser_window_id: &i64) -> Result<usize, Error> {
|
||||
tx.execute(
|
||||
"INSERT INTO `app_browser_window_tab` (`app_browser_window_id`) VALUES (?)",
|
||||
[app_browser_window_id],
|
||||
)
|
||||
}
|
||||
|
||||
pub fn records(
|
||||
&self,
|
||||
tx: &Transaction,
|
||||
app_browser_window_id: &i64,
|
||||
) -> Result<Vec<Table>, Error> {
|
||||
let mut stmt = tx.prepare("SELECT `id`,
|
||||
`app_browser_window_id` FROM `app_browser_window_tab`
|
||||
WHERE `app_browser_window_id` = ?")?;
|
||||
|
||||
let result = stmt.query_map([app_browser_window_id], |row| {
|
||||
Ok(Table {
|
||||
id: row.get(0)?,
|
||||
// app_browser_window_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(&self, tx: &Transaction, id: &i64) -> Result<usize, Error> {
|
||||
tx.execute("DELETE FROM `app_browser_window_tab` WHERE `id` = ?", [id])
|
||||
}
|
||||
|
||||
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
|
||||
tx.last_insert_rowid()
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user