[libnma: 3/7] cert-chooser: improving for handling certificate selection



commit 7822b375753e713dbdf641f6c5008a87606ebac8
Author: Eivind Naess <eivnaes yahoo com>
Date:   Thu Feb 13 15:53:01 2020 +0100

    cert-chooser: improving for handling certificate selection
    
    When a user selects a pkcs12 certificate envelope, nma-file-cert-chooser
    will gray out the key label/button while letting the user type in the
    password. If the certificate changes, it will clear the key input; if the
    key changes, it will clear the password input.
    
    [thaller redhat com: took these parts from original commit and
      (re)wrote commit message]

 src/libnma.ver                 |  5 ++++
 src/nma-cert-chooser-private.h |  2 ++
 src/nma-cert-chooser.c         | 62 ++++++++++++++++++++++++++++++++++----
 src/nma-cert-chooser.h         |  6 ++++
 src/nma-file-cert-chooser.c    | 67 ++++++++++++++++++++++++++++++++++++++----
 5 files changed, 132 insertions(+), 10 deletions(-)
---
diff --git a/src/libnma.ver b/src/libnma.ver
index 9b263d9c..799358db 100644
--- a/src/libnma.ver
+++ b/src/libnma.ver
@@ -112,3 +112,8 @@ libnma_1_8_22 {
        nma_bar_code_widget_get_type;
        nma_bar_code_widget_new;
 } libnma_1_8_12;
+
+libnma_1_8_28 {
+       nma_cert_chooser_clear_cert;
+       nma_cert_chooser_clear_key;
+} libnma_1_8_22;
diff --git a/src/nma-cert-chooser-private.h b/src/nma-cert-chooser-private.h
index dcfc8ccc..44a9091f 100644
--- a/src/nma-cert-chooser-private.h
+++ b/src/nma-cert-chooser-private.h
@@ -91,12 +91,14 @@ struct _NMACertChooserVtable {
        void                 (*set_cert_uri)                 (NMACertChooser *cert_chooser,
                                                              const gchar *uri);
        gchar               *(*get_cert_uri)                 (NMACertChooser *cert_chooser);
+       void                 (*clear_cert)                   (NMACertChooser *cert_chooser);
        void                 (*set_cert_password)            (NMACertChooser *cert_chooser,
                                                              const gchar *password);
        const gchar         *(*get_cert_password)            (NMACertChooser *cert_chooser);
        void                 (*set_key_uri)                  (NMACertChooser *cert_chooser,
                                                              const gchar *uri);
        gchar               *(*get_key_uri)                  (NMACertChooser *cert_chooser);
+       void                 (*clear_key)                    (NMACertChooser *cert_chooser);
        void                 (*set_key_password)             (NMACertChooser *cert_chooser,
                                                              const gchar *password);
        const gchar         *(*get_key_password)             (NMACertChooser *cert_chooser);
diff --git a/src/nma-cert-chooser.c b/src/nma-cert-chooser.c
index 82d0e00b..bd8fd71e 100644
--- a/src/nma-cert-chooser.c
+++ b/src/nma-cert-chooser.c
@@ -120,6 +120,10 @@ nma_cert_chooser_set_cert_uri (NMACertChooser *cert_chooser,
 {
        g_return_if_fail (NMA_IS_CERT_CHOOSER (cert_chooser));
 
+       if (!uri) {
+               nma_cert_chooser_clear_cert (cert_chooser);
+               return;
+       }
        NMA_CERT_CHOOSER_GET_VTABLE (cert_chooser)->set_cert_uri (cert_chooser, uri);
 }
 
@@ -142,9 +146,10 @@ nma_cert_chooser_set_cert (NMACertChooser *cert_chooser,
 
        g_return_if_fail (NMA_IS_CERT_CHOOSER (cert_chooser));
 
-       if (!value)
+       if (!value) {
+               nma_cert_chooser_clear_cert (cert_chooser);
                return;
-
+       }
        uri = value_with_scheme_to_uri (value, scheme);
        nma_cert_chooser_set_cert_uri (cert_chooser, uri);
 }
@@ -236,6 +241,47 @@ nma_cert_chooser_get_cert_password (NMACertChooser *cert_chooser)
        return vtable->get_cert_password (cert_chooser);
 }
 
+/**
+ * nma_cert_chooser_clear_cert:
+ * @cert_chooser: certificate chooser button instance
+ *
+ * Clear the certificate for the chooser button
+ *
+ * Since: 1.8.28
+ */
+void
+nma_cert_chooser_clear_cert (NMACertChooser *cert_chooser)
+{
+       const NMACertChooserVtable *vtable;
+
+       g_return_if_fail (NMA_IS_CERT_CHOOSER (cert_chooser));
+       vtable = NMA_CERT_CHOOSER_GET_VTABLE (cert_chooser);
+       if (vtable->clear_cert)
+               vtable->clear_cert (cert_chooser);
+}
+
+
+/**
+ * nma_cert_chooser_clear_key:
+ * @cert_chooser: certificate chooser button instance
+ *
+ * Clear the key for the chooser button
+ *
+ * Since: 1.8.28
+ */
+void
+nma_cert_chooser_clear_key (NMACertChooser *cert_chooser)
+{
+       const NMACertChooserVtable *vtable;
+
+       g_return_if_fail (NMA_IS_CERT_CHOOSER (cert_chooser));
+
+       vtable = NMA_CERT_CHOOSER_GET_VTABLE (cert_chooser);
+       if (vtable->clear_key)
+               vtable->clear_key (cert_chooser);
+       nma_cert_chooser_set_cert_password (cert_chooser, "");
+}
+
 /**
  * nma_cert_chooser_set_key_uri:
  * @cert_chooser: certificate chooser button instance
@@ -251,7 +297,12 @@ nma_cert_chooser_set_key_uri (NMACertChooser *cert_chooser,
 {
        g_return_if_fail (NMA_IS_CERT_CHOOSER (cert_chooser));
 
-       return NMA_CERT_CHOOSER_GET_VTABLE (cert_chooser)->set_key_uri (cert_chooser, uri);
+       if (!uri) {
+               nma_cert_chooser_clear_key (cert_chooser);
+               return;
+       }
+
+       NMA_CERT_CHOOSER_GET_VTABLE (cert_chooser)->set_key_uri (cert_chooser, uri);
 }
 
 /**
@@ -273,9 +324,10 @@ nma_cert_chooser_set_key (NMACertChooser *cert_chooser,
 
        g_return_if_fail (NMA_IS_CERT_CHOOSER (cert_chooser));
 
-       if (!value)
+       if (!value) {
+               nma_cert_chooser_clear_key (cert_chooser);
                return;
-
+    }
        uri = value_with_scheme_to_uri (value, scheme);
        nma_cert_chooser_set_key_uri (cert_chooser, uri);
 }
diff --git a/src/nma-cert-chooser.h b/src/nma-cert-chooser.h
index c4c0ad54..036a38df 100644
--- a/src/nma-cert-chooser.h
+++ b/src/nma-cert-chooser.h
@@ -99,6 +99,12 @@ void                 nma_cert_chooser_set_key_password             (NMACertChoos
 NMA_AVAILABLE_IN_1_8
 const gchar         *nma_cert_chooser_get_key_password             (NMACertChooser *cert_chooser);
 
+NMA_AVAILABLE_IN_1_8_28
+void                 nma_cert_chooser_clear_cert                   (NMACertChooser *cert_chooser);
+
+NMA_AVAILABLE_IN_1_8_28
+void                 nma_cert_chooser_clear_key                    (NMACertChooser *cert_chooser);
+
 NMA_AVAILABLE_IN_1_8
 GtkWidget           *nma_cert_chooser_new                          (const gchar *title,
                                                                     NMACertChooserFlags flags);
diff --git a/src/nma-file-cert-chooser.c b/src/nma-file-cert-chooser.c
index 8f24c826..89c1792d 100644
--- a/src/nma-file-cert-chooser.c
+++ b/src/nma-file-cert-chooser.c
@@ -60,6 +60,14 @@ get_key_uri (NMACertChooser *cert_chooser)
        return gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (priv->key_button));
 }
 
+static void
+clear_key (NMACertChooser *cert_chooser)
+{
+       NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
+
+       gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->key_button));
+}
+
 static void
 set_cert_uri (NMACertChooser *cert_chooser, const gchar *uri)
 {
@@ -69,6 +77,14 @@ set_cert_uri (NMACertChooser *cert_chooser, const gchar *uri)
                gtk_file_chooser_set_uri (GTK_FILE_CHOOSER (priv->cert_button), uri);
 }
 
+static void
+clear_cert (NMACertChooser *cert_chooser)
+{
+       NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
+
+       gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->cert_button));
+}
+
 static gchar *
 get_cert_uri (NMACertChooser *cert_chooser)
 {
@@ -197,10 +213,30 @@ static void
 cert_changed_cb (GtkFileChooserButton *file_chooser_button, gpointer user_data)
 {
        NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (NMA_CERT_CHOOSER (user_data));
+       gboolean sensitive = FALSE;
+       gboolean is_pkcs12 = FALSE;
+       gs_free char *cert = NULL;
+       gs_free char *key = NULL;
 
        if (gtk_widget_get_visible (priv->key_button)) {
-               gtk_widget_set_sensitive (priv->key_button, TRUE);
-               gtk_widget_set_sensitive (priv->key_button_label, TRUE);
+               cert = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->cert_button));
+               if (cert && *cert) {
+                       is_pkcs12 = nm_utils_file_is_pkcs12 (cert);
+                       if (is_pkcs12) {
+                               key = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->key_button));
+                               if (!key || !nm_streq0(cert, key)) {
+                                       gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (priv->key_button), 
cert);
+                               }
+                       }
+                       else {
+                               gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->key_button));
+                               sensitive = TRUE;
+                       }
+               } else {
+                       gtk_file_chooser_unselect_all (GTK_FILE_CHOOSER (priv->key_button));
+               }
+               gtk_widget_set_sensitive (priv->key_button, sensitive);
+               gtk_widget_set_sensitive (priv->key_button_label, sensitive);
        }
        g_signal_emit_by_name (user_data, "changed");
 }
@@ -209,9 +245,28 @@ static void
 key_changed_cb (GtkFileChooserButton *file_chooser_button, gpointer user_data)
 {
        NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (NMA_CERT_CHOOSER (user_data));
-
-       gtk_widget_set_sensitive (priv->key_password, TRUE);
-       gtk_widget_set_sensitive (priv->key_password_label, TRUE);
+       gboolean sensitive = FALSE;
+       gboolean is_pkcs12 = FALSE;
+       gs_free char *key = NULL;
+       gs_free char *cert = NULL;
+
+       key = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->key_button));
+       if (key && *key) {
+               is_pkcs12 = nm_utils_file_is_pkcs12 (key);
+               if (is_pkcs12) {
+                       cert = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->cert_button));
+                       if (!cert || !nm_streq0(cert, key)) {
+                               gtk_file_chooser_set_filename (GTK_FILE_CHOOSER (priv->cert_button), key);
+                       }
+                       gtk_widget_set_sensitive (priv->key_button, FALSE);
+                       gtk_widget_set_sensitive (priv->key_button_label, FALSE);
+               }
+               sensitive = TRUE;
+       }
+       gtk_entry_set_text (GTK_ENTRY (priv->key_password), "");
+       gtk_widget_set_sensitive (priv->key_password, sensitive);
+       gtk_widget_set_sensitive (priv->key_password_label, sensitive);
+       widget_unset_error(priv->key_password);
        g_signal_emit_by_name (user_data, "changed");
 }
 
@@ -385,8 +440,10 @@ const NMACertChooserVtable nma_cert_chooser_vtable_file = {
 
        .set_cert_uri = set_cert_uri,
        .get_cert_uri = get_cert_uri,
+       .clear_cert   = clear_cert,
        .set_key_uri = set_key_uri,
        .get_key_uri = get_key_uri,
+       .clear_key   = clear_key,
        .set_key_password = set_key_password,
        .get_key_password = get_key_password,
 


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]