From 91cd9cbae9cbe6c076e456ac5c53f00d3bca260b Mon Sep 17 00:00:00 2001 From: yggverse Date: Wed, 29 Jan 2025 20:37:56 +0200 Subject: [PATCH] implement exit button as trait, set disconnect button status update by total --- .../request/identity/common/form.rs | 27 +++++++++---------- .../request/identity/common/form/exit.rs | 22 +++++++-------- src/profile/identity/auth.rs | 5 ++++ src/profile/identity/auth/memory.rs | 13 +++++++++ 4 files changed, 41 insertions(+), 26 deletions(-) diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs index 39bc358f..cce4a8e1 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form.rs @@ -14,19 +14,22 @@ use save::Save; use super::WidgetAction; use crate::Profile; -use gtk::{glib::Uri, prelude::BoxExt, Box, Orientation}; +use gtk::{ + glib::Uri, + prelude::{BoxExt, WidgetExt}, + Box, Button, Orientation, +}; use std::rc::Rc; pub struct Form { // pub action_widget: Rc, pub drop: Rc, - pub exit: Rc, + pub exit: Button, pub file: Rc, pub list: Rc, pub name: Rc, pub save: Rc, pub g_box: Box, - request: Uri, profile: Rc, } @@ -41,7 +44,7 @@ impl Form { let name = Rc::new(Name::build(widget_action)); let save = Rc::new(Save::build(profile, &list)); let drop = Rc::new(Drop::build(profile, &list)); - let exit = Rc::new(Exit::build(widget_action, profile, &list, request)); + let exit = Button::exit(widget_action, profile, &list, request); // Init main container let g_box = Box::builder().orientation(Orientation::Vertical).build(); @@ -49,7 +52,7 @@ impl Form { g_box.append(&list.dropdown); g_box.append(&name.entry); g_box.append(&file.button); - g_box.append(&exit.button); + g_box.append(&exit); g_box.append(&drop.button); g_box.append(&save.button); @@ -63,7 +66,6 @@ impl Form { name, save, g_box, - request: request.clone(), profile: profile.clone(), } } @@ -90,18 +92,15 @@ impl Form { match value { Value::ProfileIdentityId(profile_identity_id) => { self.drop.update(true); - self.exit.update( - true, - self.profile - .identity - .auth - .is_matches(&self.request.to_string(), profile_identity_id), - ); + self.exit.set_visible(true); + self.exit + .set_sensitive(self.profile.identity.auth.total(profile_identity_id) > 0); self.save.update(true); } _ => { self.drop.update(false); - self.exit.update(false, false); + self.exit.set_visible(false); + self.exit.set_sensitive(false); self.save.update(false); } } diff --git a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs index 8321d69e..2fa1beee 100644 --- a/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs +++ b/src/app/browser/window/tab/item/page/navigation/request/identity/common/form/exit.rs @@ -25,15 +25,20 @@ const BODY: &str = "Stop use selected identity for all scopes?"; const RESPONSE_CANCEL: (&str, &str) = ("cancel", "Cancel"); const RESPONSE_CONFIRM: (&str, &str) = ("confirm", "Confirm"); -pub struct Exit { - pub button: Button, +pub trait Exit { + fn exit( + widget_action: &Rc, + profile: &Rc, + list: &Rc, + request: &Uri, + ) -> Self; } -impl Exit { +impl Exit for Button { // Constructors /// Create new `Self` - pub fn build( + fn exit( widget_action: &Rc, profile: &Rc, list: &Rc, @@ -122,13 +127,6 @@ impl Exit { }); // Return activated `Self` - Self { button } - } - - // Actions - - pub fn update(&self, is_visible: bool, is_sensitive: bool) { - self.button.set_visible(is_visible); - self.button.set_sensitive(is_sensitive); + button } } diff --git a/src/profile/identity/auth.rs b/src/profile/identity/auth.rs index 4d7265cf..3770b8d0 100644 --- a/src/profile/identity/auth.rs +++ b/src/profile/identity/auth.rs @@ -124,6 +124,11 @@ impl Auth { .is_some_and(|auth| auth.profile_identity_id == profile_identity_id) } + /// Check request string matches condition + pub fn total(&self, profile_identity_id: i64) -> usize { + self.memory.total(profile_identity_id) + } + /// Collect certificate scope vector from `Profile` database for `profile_identity_id` pub fn scope(&self, profile_identity_id: i64) -> Vec { let mut scope = Vec::new(); diff --git a/src/profile/identity/auth/memory.rs b/src/profile/identity/auth/memory.rs index 8730ea10..9679bce4 100644 --- a/src/profile/identity/auth/memory.rs +++ b/src/profile/identity/auth/memory.rs @@ -79,4 +79,17 @@ impl Memory { // Get first copy result.first().cloned() } + + /// Get identity exactly match `scope` + /// * [Client certificates specification](https://geminiprotocol.net/docs/protocol-specification.gmi#client-certificates) + /// * see also parent `is_match_request` + pub fn total(&self, profile_identity_id: i64) -> usize { + let mut total = 0; + for (_, _profile_identity_id) in self.index.borrow().iter() { + if *_profile_identity_id == profile_identity_id { + total += 1 + } + } + total + } }