diff --git a/src/app/browser/window/tab/item/page.rs b/src/app/browser/window/tab/item/page.rs index 4646a493..dc1b9d44 100644 --- a/src/app/browser/window/tab/item/page.rs +++ b/src/app/browser/window/tab/item/page.rs @@ -31,6 +31,7 @@ use std::{cell::RefCell, rc::Rc, time::Duration}; pub struct Page { id: GString, cancellable: RefCell, + profile: Rc, // Actions browser_action: Rc, tab_action: Rc, @@ -54,7 +55,7 @@ impl Page { let content = Rc::new(Content::new((action.1.clone(), action.2.clone()))); let navigation = Rc::new(Navigation::new( - profile, + profile.clone(), (action.0.clone(), action.1.clone(), action.2.clone()), )); @@ -73,6 +74,7 @@ impl Page { Self { cancellable: RefCell::new(Cancellable::new()), id, + profile, // Actions browser_action: action.0, tab_action: action.2, @@ -87,8 +89,11 @@ impl Page { // Actions + /// Toggle bookmark for navigation request in profile database pub fn bookmark(&self) { - // @TODO self.navigation.request().widget().gobject().text() + self.profile + .bookmark + .toggle(self.navigation.request().widget().gobject().text().as_str()) } /// Navigate home URL (parsed from current navigation entry) diff --git a/src/profile/bookmark.rs b/src/profile/bookmark.rs index 0468e17e..ed103624 100644 --- a/src/profile/bookmark.rs +++ b/src/profile/bookmark.rs @@ -4,12 +4,13 @@ mod memory; use database::Database; use memory::Memory; +use gtk::glib::DateTime; use sqlite::{Connection, Transaction}; use std::{rc::Rc, sync::RwLock}; pub struct Bookmark { - database: Rc, - memory: Rc, + database: Rc, // permanent storage + memory: Rc, // fast search index } impl Bookmark { @@ -43,7 +44,19 @@ impl Bookmark { } } - // @TODO add new record with index update + /// Toggle record in bookmarks database, update emory index + pub fn toggle(&self, request: &str) { + let time = DateTime::now_local().unwrap(); + + if self.has_request(request, false) { + // @TODO + } else { + match self.database.add(time.clone(), request.into()) { + Ok(_) => self.memory.set(request.into(), time), + Err(_) => todo!(), + }; + } + } } // Tools diff --git a/src/profile/bookmark/database.rs b/src/profile/bookmark/database.rs index 0d2cda21..ea3cd383 100644 --- a/src/profile/bookmark/database.rs +++ b/src/profile/bookmark/database.rs @@ -31,6 +31,26 @@ impl Database { let tx = readable.unchecked_transaction().unwrap(); select(&tx, self.profile_id, request).unwrap() } + + // Setters + + pub fn add(&self, time: DateTime, request: String) -> Result { + // Begin new transaction + let mut writable = self.connection.write().unwrap(); + let tx = writable.transaction().unwrap(); + + // Create new record + insert(&tx, self.profile_id, time, request).unwrap(); + + // Hold insert ID for result + let id = last_insert_id(&tx); + + // Done + match tx.commit() { + Ok(_) => Ok(id), + Err(_) => Err(()), // @TODO + } + } } // Low-level DB API @@ -102,3 +122,7 @@ pub fn select( pub fn delete(tx: &Transaction, id: i64) -> Result { tx.execute("DELETE FROM `profile_bookmark` WHERE `id` = ?", [id]) } + +pub fn last_insert_id(tx: &Transaction) -> i64 { + tx.last_insert_rowid() +} diff --git a/src/profile/bookmark/memory.rs b/src/profile/bookmark/memory.rs index 95b519ec..5758019b 100644 --- a/src/profile/bookmark/memory.rs +++ b/src/profile/bookmark/memory.rs @@ -9,6 +9,7 @@ pub struct Memory { impl Memory { // Constructors + /// Create new `Self` pub fn new() -> Self { Self { index: RefCell::new(HashMap::new()), @@ -32,6 +33,7 @@ impl Memory { index.insert(request, time); } + /// Check request exist in memory index pub fn is_exist(&self, request: &str) -> bool { self.index.borrow().get(request).is_some() }