From b441a681f7a235fe7b01509dfe45f4d206a3765d Mon Sep 17 00:00:00 2001 From: yggverse Date: Thu, 14 Nov 2024 06:00:41 +0200 Subject: [PATCH] deactivate welcome dialog, auto-generate profile on first launch, remove extra references, draft bookmarks model --- src/app/browser.rs | 7 +++++-- src/app/browser/welcome.rs | 3 +++ src/profile.rs | 19 ++++++++++++++++-- src/profile/bookmark.rs | 18 ++++++++++++++++- src/profile/bookmark/database.rs | 33 ++++++++++++++++++++++---------- src/profile/database.rs | 4 ++-- src/profile/history/database.rs | 16 ++++++++-------- 7 files changed, 75 insertions(+), 25 deletions(-) diff --git a/src/app/browser.rs b/src/app/browser.rs index 723dd412..c9e6840f 100644 --- a/src/app/browser.rs +++ b/src/app/browser.rs @@ -176,9 +176,12 @@ impl Browser { self.widget.gobject().present(); // Show welcome dialog on profile not selected yet (e.g. first launch) - if self.profile.database.selected().is_none() { + /* @TODO + currently this feature inactive, because profile auto-generated on first application launch + see also: src/app/browser/welcome.rs + if self.profile.database.active().is_none() { Welcome::new(self.profile.clone(), self.widget.gobject().clone()).present(); - } + } */ self } diff --git a/src/app/browser/welcome.rs b/src/app/browser/welcome.rs index 2e7f7f46..b85af542 100644 --- a/src/app/browser/welcome.rs +++ b/src/app/browser/welcome.rs @@ -1,3 +1,6 @@ +// @TODO prototype +// do not use it as currently profile get auto-generating on application startup! + mod widget; use gtk::glib::DateTime; use widget::Widget; diff --git a/src/profile.rs b/src/profile.rs index e1e5c4e9..f6f6ae63 100644 --- a/src/profile.rs +++ b/src/profile.rs @@ -3,9 +3,10 @@ mod database; mod history; mod identity; +use bookmark::Bookmark; use database::Database; -use gtk::glib::user_config_dir; +use gtk::glib::{user_config_dir, DateTime}; use sqlite::{Connection, Transaction}; use std::{fs::create_dir_all, path::PathBuf, rc::Rc, sync::RwLock}; @@ -16,6 +17,7 @@ const BRANCH: &str = "master"; const DB_NAME: &str = "database.sqlite3"; pub struct Profile { + pub bookmark: Rc, pub database: Rc, pub config_path: PathBuf, } @@ -77,9 +79,22 @@ impl Profile { } } // unlock database + // Init model + let database = Rc::new(Database::new(connection.clone())); + + // Get active profile or create new one + let profile_id = match database.active() { + Some(profile) => profile.id, + None => match database.add(true, DateTime::now_local().unwrap(), None) { + Ok(id) => id, + Err(_) => todo!(), + }, + }; + // Result Self { - database: Rc::new(Database::new(connection)), + bookmark: Rc::new(Bookmark::new(connection, profile_id)), + database, config_path, } } diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index ac00d38d..47a874fb 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -1,6 +1,22 @@ mod database; +use database::Database; -use sqlite::Transaction; +use sqlite::{Connection, Transaction}; +use std::{rc::Rc, sync::RwLock}; + +pub struct Bookmark { + pub database: Rc, +} + +impl Bookmark { + // Constructors + + pub fn new(connection: Rc>, profile_id: i64) -> Self { + Self { + database: Rc::new(Database::new(connection, profile_id)), + } + } +} // Tools diff --git a/src/profile/bookmark/database.rs b/src/profile/bookmark/database.rs index 55b72e35..cf1dcdcb 100644 --- a/src/profile/bookmark/database.rs +++ b/src/profile/bookmark/database.rs @@ -1,5 +1,6 @@ use gtk::glib::DateTime; -use sqlite::{Error, Transaction}; +use sqlite::{Connection, Error, Transaction}; +use std::{rc::Rc, sync::RwLock}; pub struct Table { pub id: i64, @@ -8,8 +9,20 @@ pub struct Table { pub request: String, } -pub struct Bookmark { - // nothing yet.. +pub struct Database { + pub connection: Rc>, + profile_id: i64, // @TODO multi-profile implementation +} + +impl Database { + // Constructors + + pub fn new(connection: Rc>, profile_id: i64) -> Self { + Self { + connection, + profile_id, + } + } } // Low-level DB API @@ -29,9 +42,9 @@ pub fn init(tx: &Transaction) -> Result { pub fn insert( tx: &Transaction, - profile_id: &i64, - time: &DateTime, - request: &str, + profile_id: i64, + time: DateTime, + request: String, ) -> Result { tx.execute( "INSERT INTO `profile_bookmark` ( @@ -45,8 +58,8 @@ pub fn insert( pub fn select( tx: &Transaction, - profile_id: &i64, - request: Option<&str>, + profile_id: i64, + request: Option, ) -> Result, Error> { let mut stmt = tx.prepare( "SELECT `id`, `profile_id`, `time`, `request` @@ -56,7 +69,7 @@ pub fn select( let filter = match request { Some(value) => value, - None => "%", + None => format!("%"), }; let result = stmt.query_map((profile_id, filter), |row| { @@ -78,6 +91,6 @@ pub fn select( Ok(records) } -pub fn delete(tx: &Transaction, id: &i64) -> Result { +pub fn delete(tx: &Transaction, id: i64) -> Result { tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id]) } diff --git a/src/profile/database.rs b/src/profile/database.rs index 83d8b7e4..b21491ac 100644 --- a/src/profile/database.rs +++ b/src/profile/database.rs @@ -30,8 +30,8 @@ impl Database { select(&tx).unwrap() } - /// Get selected profile record if exist - pub fn selected(&self) -> Option { + /// Get active profile record if exist + pub fn active(&self) -> Option
{ for record in self.records() { if record.is_active { return Some(record); diff --git a/src/profile/history/database.rs b/src/profile/history/database.rs index 7f161dd8..1645f49f 100644 --- a/src/profile/history/database.rs +++ b/src/profile/history/database.rs @@ -8,7 +8,7 @@ pub struct Table { pub request: String, } -pub struct History { +pub struct Database { // nothing yet.. } @@ -29,9 +29,9 @@ pub fn init(tx: &Transaction) -> Result { pub fn insert( tx: &Transaction, - profile_id: &i64, - time: &DateTime, - request: &str, + profile_id: i64, + time: DateTime, + request: String, ) -> Result { tx.execute( "INSERT INTO `history` ( @@ -45,8 +45,8 @@ pub fn insert( pub fn select( tx: &Transaction, - profile_id: &i64, - request: Option<&str>, + profile_id: i64, + request: Option, ) -> Result, Error> { let mut stmt = tx.prepare( "SELECT `id`, `profile_id`, `time`, `request` @@ -56,7 +56,7 @@ pub fn select( let filter = match request { Some(value) => value, - None => "%", + None => format!("%"), }; let result = stmt.query_map((profile_id, filter), |row| { @@ -78,6 +78,6 @@ pub fn select( Ok(records) } -pub fn delete(tx: &Transaction, id: &i64) -> Result { +pub fn delete(tx: &Transaction, id: i64) -> Result { tx.execute("DELETE FROM `profile_history` WHERE `id` = ?", [id]) }