From 1273f9947d06013b6082c306e7716d3eaff1071c Mon Sep 17 00:00:00 2001 From: yggverse Date: Sat, 7 Dec 2024 22:45:31 +0200 Subject: [PATCH] draft properties update method --- .../item/identity/gemini/widget/form/exit.rs | 11 ++-- .../item/identity/gemini/widget/form/list.rs | 16 +---- .../identity/gemini/widget/form/list/item.rs | 62 +++++++++++++++++-- .../gemini/widget/form/list/item/is_active.rs | 2 +- 4 files changed, 66 insertions(+), 25 deletions(-) diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/exit.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/exit.rs index 11700b68..6355e510 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/exit.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/exit.rs @@ -78,12 +78,15 @@ impl Exit { .auth .remove_ref(profile_identity_gemini_id) { - Ok(_) => match list.update(profile_identity_gemini_id) { - Some(_) => { + Ok(_) => match list.selected().update(&profile, "") { + Ok(_) => { button.set_css_classes(&["success"]); - button.set_label("Identity successfully disconnected"); + button.set_label("Identity successfully disconnected") + } + Err(e) => { + button.set_css_classes(&["error"]); + button.set_label(&e.to_string()) } - None => todo!(), }, Err(e) => { button.set_css_classes(&["error"]); diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs index 42ca629a..facbb690 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list.rs @@ -18,7 +18,6 @@ use gtk::{ pub struct List { pub dropdown: DropDown, list_store: ListStore, - // profile: Rc, } impl List { @@ -140,7 +139,6 @@ impl List { Self { dropdown, list_store, - // profile, } } @@ -154,7 +152,7 @@ impl List { }) } - /// Remove list item by `profile_identity_gemini_id` (stores ID) + /// Remove list item by `profile_identity_gemini_id` /// * return `position` of removed list item pub fn remove(&self, profile_identity_gemini_id: i64) -> Option { match self.find(profile_identity_gemini_id) { @@ -166,18 +164,6 @@ impl List { } } - /// Update list item by `profile_identity_gemini_id` (stores ID) - /// * return `position` of updated list item - pub fn update(&self, profile_identity_gemini_id: i64) -> Option { - match self.find(profile_identity_gemini_id) { - Some(position) => { - // @TODO - Some(position) - } - None => todo!(), - } - } - // Getters /// Get selected `Item` GObject diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs index 4e549926..ba996f51 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item.rs @@ -6,8 +6,6 @@ mod title; mod tooltip; pub mod value; -use std::rc::Rc; - pub use error::Error; pub use value::Value; @@ -16,6 +14,7 @@ use gtk::{ gio::TlsCertificate, glib::{self, Object}, }; +use std::rc::Rc; glib::wrapper! { pub struct Item(ObjectSubclass); @@ -69,7 +68,7 @@ impl Item { // Extract certificate details from PEM string Ok(ref pem) => match TlsCertificate::from_pem(pem) { // Collect certificate scopes for item - Ok(ref certificate) => match scope(profile.clone(), profile_identity_gemini_id) { + Ok(ref certificate) => match scope(&profile, profile_identity_gemini_id) { // Ready to build `Item` GObject Ok(ref scope) => Ok(Object::builder() .property("value", profile_identity_gemini_id) @@ -88,7 +87,7 @@ impl Item { .property( "is_active", is_active::new_for_profile_identity_gemini_id( - profile, + &profile, profile_identity_gemini_id, auth_url, ), @@ -102,6 +101,59 @@ impl Item { } } + // Actions + + /// Update properties for `Self` for given `Profile` and `auth_url` + pub fn update(&self, profile: &Rc, auth_url: &str) -> Result<(), Error> { + // Update item depending on value type + match self.value_enum() { + Value::ProfileIdentityGeminiId(profile_identity_gemini_id) => { + // Get PEM by ID + match profile + .identity + .gemini + .memory + .get(profile_identity_gemini_id) + { + // Extract certificate details from PEM string + Ok(ref pem) => match TlsCertificate::from_pem(pem) { + Ok(ref certificate) => { + // Get current scope + let ref scope = scope(profile, profile_identity_gemini_id)?; + + // Update properties + self.set_title(title::new_for_profile_identity_gemini_id(certificate)); + + self.set_subtitle(subtitle::new_for_profile_identity_gemini_id( + certificate, + scope, + )); + + self.set_tooltip(tooltip::new_for_profile_identity_gemini_id( + certificate, + scope, + )); + + self.set_is_active(is_active::new_for_profile_identity_gemini_id( + profile, + profile_identity_gemini_id, + auth_url, + )); + + // @TODO emit update request + } + Err(e) => return Err(Error::TlsCertificate(e)), + }, + Err(_) => todo!(), + } + } + _ => { + // nothing to update yet.. + } + } + Ok(()) // @TODO + } + // Getters /// Get `Self` C-value as `Value` @@ -118,7 +170,7 @@ impl Item { // Tools /// Collect certificate scope vector from `Profile` database for `profile_identity_gemini_id` -fn scope(profile: Rc, profile_identity_gemini_id: i64) -> Result, Error> { +fn scope(profile: &Rc, profile_identity_gemini_id: i64) -> Result, Error> { match profile.identity.gemini.auth.database.records_scope(None) { Ok(result) => { let mut scope = Vec::new(); diff --git a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/is_active.rs b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/is_active.rs index ef586f98..32964601 100644 --- a/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/is_active.rs +++ b/src/app/browser/window/tab/item/identity/gemini/widget/form/list/item/is_active.rs @@ -2,7 +2,7 @@ use crate::profile::Profile; use std::rc::Rc; pub fn new_for_profile_identity_gemini_id( - profile: Rc, + profile: &Rc, profile_identity_gemini_id: i64, auth_url: &str, ) -> bool {