Browse Source

draf db features for label mod

master
yggverse 2 months ago
parent
commit
259ec321be
  1. 54
      src/app/browser/window/tab.rs
  2. 3
      src/app/browser/window/tab/database.rs
  3. 92
      src/app/browser/window/tab/label.rs
  4. 88
      src/app/browser/window/tab/label/database.rs

54
src/app/browser/window/tab.rs

@ -23,14 +23,14 @@ use std::{ @@ -23,14 +23,14 @@ use std::{
};
// Common struct for HashMap index
struct TabItem {
pub struct TabItem {
label: Arc<Label>,
page: Arc<Page>,
}
// Main
pub struct Tab {
// Extras
profile_database_connection: Arc<RwLock<Connection>>,
database: Arc<Database>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
@ -39,7 +39,7 @@ pub struct Tab { @@ -39,7 +39,7 @@ pub struct Tab {
action_tab_page_navigation_reload: Arc<SimpleAction>,
action_update: Arc<SimpleAction>,
// Dynamically allocated reference index
index: RefCell<HashMap<GString, TabItem>>,
index: RefCell<HashMap<GString, Arc<TabItem>>>,
// GTK
widget: Arc<Widget>,
}
@ -47,7 +47,6 @@ pub struct Tab { @@ -47,7 +47,6 @@ pub struct Tab {
impl Tab {
// Construct
pub fn new(
// Extras
profile_database_connection: Arc<RwLock<Connection>>,
// Actions
action_tab_page_navigation_base: Arc<SimpleAction>,
@ -88,6 +87,7 @@ impl Tab { @@ -88,6 +87,7 @@ impl Tab {
// Return non activated struct
Self {
profile_database_connection,
database,
// Define action links
action_tab_page_navigation_base,
@ -126,12 +126,21 @@ impl Tab { @@ -126,12 +126,21 @@ impl Tab {
});
}
pub fn append(&self, page_navigation_request_text: Option<GString>, is_current_page: bool) {
pub fn append(
&self,
page_navigation_request_text: Option<GString>,
is_current_page: bool,
) -> Arc<TabItem> {
// Generate unique ID for new page components
let id = uuid_string_random();
// Init new tab components
let label = Arc::new(Label::new(id.clone(), false));
let label = Arc::new(Label::new(
self.profile_database_connection.clone(),
id.clone(),
false,
));
let page = Arc::new(Page::new(
id.clone(),
page_navigation_request_text.clone(),
@ -142,14 +151,14 @@ impl Tab { @@ -142,14 +151,14 @@ impl Tab {
self.action_update.clone(),
));
// Init new tab item
let item = Arc::new(TabItem {
label: label.clone(),
page: page.clone(),
});
// Register dynamically created tab components in the HashMap index
self.index.borrow_mut().insert(
id.clone(),
TabItem {
label: label.clone(),
page: page.clone(),
},
);
self.index.borrow_mut().insert(id.clone(), item.clone());
// Init additional label actions
let controller = GestureClick::new();
@ -173,6 +182,8 @@ impl Tab { @@ -173,6 +182,8 @@ impl Tab {
if page_navigation_request_text.is_none() {
page.navigation_request_grab_focus();
}
item
}
// Close active tab
@ -246,7 +257,10 @@ impl Tab { @@ -246,7 +257,10 @@ impl Tab {
match self.database.delete(tx, &record.id) {
Ok(_) => {
// Delegate clean action to childs
// nothing yet..
for (_, item) in self.index.borrow().iter() {
item.label.clean(tx, &record.id);
// @TODO item.page.clean(tx, &record.id);
}
}
Err(e) => todo!("{e}"),
}
@ -260,9 +274,10 @@ impl Tab { @@ -260,9 +274,10 @@ impl Tab {
match self.database.records(tx, app_browser_window_id) {
Ok(records) => {
for record in records {
self.append(None, record.is_current);
let item = self.append(None, record.is_current);
// Delegate restore action to childs
// nothing yet..
item.label.restore(tx, &record.id);
// item.page.restore(tx, record.id);
}
}
Err(e) => todo!("{e}"),
@ -272,7 +287,7 @@ impl Tab { @@ -272,7 +287,7 @@ impl Tab {
pub fn save(&self, tx: &Transaction, app_browser_window_id: &i64) {
let mut page_number = 0;
for (_, _) in self.index.borrow().iter() {
for (_, item) in self.index.borrow().iter() {
match self.database.add(
tx,
app_browser_window_id,
@ -283,10 +298,11 @@ impl Tab { @@ -283,10 +298,11 @@ impl Tab {
) {
Ok(_) => {
// Delegate save action to childs
// let id = self.database.last_insert_id(tx);
let id = self.database.last_insert_id(tx);
item.label.save(tx, &id);
// @TODO
// item.label.save()
// item.page.save()
}
Err(e) => todo!("{e}"),

3
src/app/browser/window/tab/database.rs

@ -74,8 +74,7 @@ impl Database { @@ -74,8 +74,7 @@ impl Database {
tx.execute("DELETE FROM `app_browser_window_tab` WHERE `id` = ?", [id])
}
/* not in use
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */
}
}

92
src/app/browser/window/tab/label.rs

@ -1,15 +1,19 @@ @@ -1,15 +1,19 @@
mod database;
mod pin;
mod title;
mod widget;
use database::Database;
use pin::Pin;
use sqlite::{Connection, Transaction};
use title::Title;
use widget::Widget;
use gtk::{glib::GString, Box};
use std::sync::Arc;
use std::sync::{Arc, RwLock};
pub struct Label {
database: Arc<Database>,
// Components
pin: Arc<Pin>,
title: Arc<Title>,
@ -19,7 +23,38 @@ pub struct Label { @@ -19,7 +23,38 @@ pub struct Label {
impl Label {
// Construct
pub fn new(name: GString, is_pinned: bool) -> Label {
pub fn new(
profile_database_connection: Arc<RwLock<Connection>>,
name: GString,
is_pinned: bool,
) -> Label {
// Init database
let database = {
/* @TODO init outside
// 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}"),
} */
Arc::new(Database::new())
};
// Components
let pin = Arc::new(Pin::new(is_pinned));
let title = Arc::new(Title::new());
@ -28,10 +63,59 @@ impl Label { @@ -28,10 +63,59 @@ impl Label {
let widget = Arc::new(Widget::new(name, pin.gobject(), title.gobject()));
// Result
Self { pin, title, widget }
Self {
database,
pin,
title,
widget,
}
}
// Actions
pub fn clean(&self, tx: &Transaction, app_browser_window_tab_id: &i64) {
match self.database.records(tx, app_browser_window_tab_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_tab_id: &i64) {
match self.database.records(tx, app_browser_window_tab_id) {
Ok(records) => {
for record in records {
self.pin(record.is_pinned);
// Delegate restore action to childs
// nothing yet..
}
}
Err(e) => todo!("{e}"),
}
}
pub fn save(&self, tx: &Transaction, app_browser_window_tab_id: &i64) {
match self
.database
.add(tx, app_browser_window_tab_id, &self.is_pinned())
{
Ok(_) => {
// Delegate save action to childs
// nothing yet..
}
Err(e) => todo!("{e}"),
}
}
pub fn update(&self, title: Option<&GString>) {
self.title.update(title);
self.widget.update(title);
@ -45,7 +129,7 @@ impl Label { @@ -45,7 +129,7 @@ impl Label {
// Getters
pub fn is_pinned(&self) -> bool {
self.pin.is_pinned()
self.pin.is_pinned() // @TODO
}
pub fn gobject(&self) -> &Box {

88
src/app/browser/window/tab/label/database.rs

@ -0,0 +1,88 @@ @@ -0,0 +1,88 @@
use sqlite::{Error, Transaction};
pub struct Table {
pub id: i64,
// pub app_browser_window_tab_id: i64, not in use
pub is_pinned: bool,
}
pub struct Database {
// nothing yet..
}
impl Database {
pub fn new() -> Self {
Self {}
}
pub fn init(tx: &Transaction) -> Result<Database, Error> {
tx.execute(
"CREATE TABLE IF NOT EXISTS `app_browser_window_tab_label`
(
`id` INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
`app_browser_window_tab_id` INTEGER NOT NULL,
`is_pinned` INTEGER NOT NULL
)",
[],
)?;
Ok(Self {})
}
pub fn add(
&self,
tx: &Transaction,
app_browser_window_tab_id: &i64,
is_pinned: &bool,
) -> Result<usize, Error> {
tx.execute(
"INSERT INTO `app_browser_window_tab_label` (
`app_browser_window_tab_id`,
`is_pinned`
) VALUES (?,?)",
[app_browser_window_tab_id, &(*is_pinned as i64)],
)
}
pub fn records(
&self,
tx: &Transaction,
app_browser_window_tab_id: &i64,
) -> Result<Vec<Table>, Error> {
let mut stmt = tx.prepare(
"SELECT `id`,
`app_browser_window_tab_id`,
`is_pinned` FROM `app_browser_window_tab_label`
WHERE `app_browser_window_tab_id` = ?",
)?;
let result = stmt.query_map([app_browser_window_tab_id], |row| {
Ok(Table {
id: row.get(0)?,
// app_browser_window_tab_id: row.get(1)?, not in use
is_pinned: row.get(2)?,
})
})?;
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_label` WHERE `id` = ?",
[id],
)
}
/* not in use
pub fn last_insert_id(&self, tx: &Transaction) -> i64 {
tx.last_insert_rowid()
} */
}
Loading…
Cancel
Save