diff --git a/src/app/browser/window/tab/item/identity/gemini.rs b/src/app/browser/window/tab/item/identity/gemini.rs index b457118b..f3bceab4 100644 --- a/src/app/browser/window/tab/item/identity/gemini.rs +++ b/src/app/browser/window/tab/item/identity/gemini.rs @@ -57,9 +57,21 @@ impl Gemini { } // Init events - widget.on_apply(move |response| match response { - // Apply selected identity for `auth_uri` - Some(profile_identity_gemini_id) => { + widget.on_apply({ + let widget = widget.clone(); + move |response| { + let profile_identity_gemini_id = match response { + // Use selected identity + Some(id) => id, + // Create new identity, get last insert ID + None => profile + .identity + .gemini + .create(None, widget.form.name.value().as_deref()) + .unwrap(), // @TODO + }; + + // Apply identity for given `auth_uri` profile .identity .gemini @@ -67,8 +79,6 @@ impl Gemini { .apply(profile_identity_gemini_id, auth_uri.to_string().as_str()) .unwrap(); //@TODO handle errors } - // Create new certificate, then apply it to the new identity for `auth_uri` - None => {} }); // Return activated `Self` diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form.rs index 252f41cd..33264e68 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form.rs @@ -13,7 +13,7 @@ use std::rc::Rc; pub struct Form { pub gobject: Box, pub list: Rc, - // pub name: Rc, + pub name: Rc, } impl Form { @@ -32,13 +32,16 @@ impl Form { gobject.append(&name.gobject); // Connect events - list.on_select(move |key| name.gobject.set_visible(key.is_none())); + list.on_select({ + let name = name.clone(); + move |key| name.gobject.set_visible(key.is_none()) + }); // Return activated `Self` Self { gobject, list, - // name, + name, } } } diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/name.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/name.rs index c05f38a9..d7ba5f77 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/name.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/name.rs @@ -1,4 +1,4 @@ -use gtk::Entry; +use gtk::{glib::GString, prelude::EditableExt, Entry}; const PLACEHOLDER_TEXT: &str = "Identity name (optional)"; const MARGIN: i32 = 8; @@ -20,4 +20,15 @@ impl Name { .build(), } } + + // Getters + + pub fn value(&self) -> Option { + let text = self.gobject.text(); + if text.is_empty() { + None + } else { + Some(text) + } + } } diff --git a/src/profile/identity/gemini.rs b/src/profile/identity/gemini.rs index 0b0da865..21939216 100644 --- a/src/profile/identity/gemini.rs +++ b/src/profile/identity/gemini.rs @@ -1,4 +1,5 @@ mod auth; +mod certificate; mod database; mod error; mod memory; @@ -6,8 +7,10 @@ mod memory; use auth::Auth; use database::Database; use error::Error; + use memory::Memory; +use gtk::glib::DateTime; use sqlite::{Connection, Transaction}; use std::{rc::Rc, sync::RwLock}; @@ -18,7 +21,6 @@ pub struct Gemini { pub auth: Rc, pub database: Rc, pub memory: Rc, - profile_identity_id: Rc, } impl Gemini { @@ -42,7 +44,6 @@ impl Gemini { auth, database, memory, - profile_identity_id, }; // Build initial index @@ -53,6 +54,38 @@ impl Gemini { // Actions + /// Create new record + /// * return new `profile_identity_gemini_id` on success + pub fn create( + &self, + time: Option<(DateTime, DateTime)>, + name: Option<&str>, + ) -> Result { + // Generate new certificate + match certificate::generate( + match time { + Some(value) => value, + None => ( + DateTime::now_local().unwrap(), + DateTime::from_local(9999, 12, 31, 23, 59, 59.9).unwrap(), // max @TODO + ), + }, + match name { + Some(value) => value, // @TODO make sure it's unique + None => "unknown", // @TODO randomize + }, + ) { + Ok(pem) => match self.database.add(&pem, name) { + Ok(profile_identity_gemini_id) => { + self.index()?; + Ok(profile_identity_gemini_id) + } + Err(_) => Err(Error::DatabaseRecordCreate), + }, + Err(reason) => Err(Error::Certificate(reason)), + } + } + /// Create new `Memory` index from `Database` for `Self` pub fn index(&self) -> Result<(), Error> { // Cleanup previous records diff --git a/src/profile/identity/gemini/error.rs b/src/profile/identity/gemini/error.rs index 53086a7f..23129df7 100644 --- a/src/profile/identity/gemini/error.rs +++ b/src/profile/identity/gemini/error.rs @@ -1,6 +1,8 @@ #[derive(Debug)] pub enum Error { AuthInit, - MemoryIndex, DatabaseIndex, + DatabaseRecordCreate, + MemoryIndex, + Certificate(Box), }