deactivate welcome dialog, auto-generate profile on first launch, remove extra references, draft bookmarks model

This commit is contained in:
yggverse 2024-11-14 06:00:41 +02:00
parent f81e496fa4
commit b441a681f7
7 changed files with 75 additions and 25 deletions

View File

@ -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
}

View File

@ -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;

View File

@ -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<Bookmark>,
pub database: Rc<Database>,
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,
}
}

View File

@ -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<Database>,
}
impl Bookmark {
// Constructors
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: i64) -> Self {
Self {
database: Rc::new(Database::new(connection, profile_id)),
}
}
}
// Tools

View File

@ -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<RwLock<Connection>>,
profile_id: i64, // @TODO multi-profile implementation
}
impl Database {
// Constructors
pub fn new(connection: Rc<RwLock<Connection>>, profile_id: i64) -> Self {
Self {
connection,
profile_id,
}
}
}
// Low-level DB API
@ -29,9 +42,9 @@ pub fn init(tx: &Transaction) -> Result<usize, Error> {
pub fn insert(
tx: &Transaction,
profile_id: &i64,
time: &DateTime,
request: &str,
profile_id: i64,
time: DateTime,
request: String,
) -> Result<usize, Error> {
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<String>,
) -> Result<Vec<Table>, 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<usize, Error> {
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id])
}

View File

@ -30,8 +30,8 @@ impl Database {
select(&tx).unwrap()
}
/// Get selected profile record if exist
pub fn selected(&self) -> Option<Table> {
/// Get active profile record if exist
pub fn active(&self) -> Option<Table> {
for record in self.records() {
if record.is_active {
return Some(record);

View File

@ -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<usize, Error> {
pub fn insert(
tx: &Transaction,
profile_id: &i64,
time: &DateTime,
request: &str,
profile_id: i64,
time: DateTime,
request: String,
) -> Result<usize, Error> {
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<String>,
) -> Result<Vec<Table>, 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<usize, Error> {
pub fn delete(tx: &Transaction, id: i64) -> Result<usize, Error> {
tx.execute("DELETE FROM `profile_history` WHERE `id` = ?", [id])
}