[network-manager-applet/lr/pkcs11: 19/24] fixup! libnma: add a file certificate chooser



commit 9dfddadb304f909fc3713cf73ebf8a4ce0aaa191
Author: Lubomir Rintel <lkundrak v3 sk>
Date:   Tue Mar 7 12:25:44 2017 +0100

    fixup! libnma: add a file certificate chooser
    
    Restructure to reflect that the NMACertChooser is a class. In
    particular, separate the widget tree construction from setting of its
    properties -- title and flags that affect widget visibility.

 src/libnma/nma-file-cert-chooser.c |  299 ++++++++++++++++++++----------------
 src/libnma/nma-file-cert-chooser.h |    6 +-
 2 files changed, 169 insertions(+), 136 deletions(-)
---
diff --git a/src/libnma/nma-file-cert-chooser.c b/src/libnma/nma-file-cert-chooser.c
index 31114d8..b76f3b4 100644
--- a/src/libnma/nma-file-cert-chooser.c
+++ b/src/libnma/nma-file-cert-chooser.c
@@ -30,8 +30,6 @@
 #endif
 #include "utils.h"
 
-static void nma_cert_chooser_interface_init (NMACertChooserInterface *iface);
-
 typedef struct {
        GtkWidget *key_button_label;
        GtkWidget *key_password_label;
@@ -39,15 +37,14 @@ typedef struct {
        GtkWidget *key_button;
        GtkWidget *key_password;
        GtkWidget *cert_button;
+       GtkWidget *show_password;
 } NMAFileCertChooserPrivate;
 
 #define NMA_FILE_CERT_CHOOSER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
                                               NMA_TYPE_FILE_CERT_CHOOSER, \
                                               NMAFileCertChooserPrivate))
 
-G_DEFINE_TYPE_WITH_CODE (NMAFileCertChooser, nma_file_cert_chooser, GTK_TYPE_GRID,
-                         G_IMPLEMENT_INTERFACE (NMA_TYPE_CERT_CHOOSER,
-                                                nma_cert_chooser_interface_init))
+G_DEFINE_TYPE (NMAFileCertChooser, nma_file_cert_chooser, NMA_TYPE_CERT_CHOOSER)
 
 static void
 set_key_password (NMACertChooser *cert_chooser, const gchar *password)
@@ -87,7 +84,6 @@ get_key (NMACertChooser *cert_chooser, NMSetting8021xCKScheme *scheme)
        NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
 
        g_return_val_if_fail (scheme != NULL, NULL);
-       g_return_val_if_fail (priv->key_button != NULL, NULL);
        *scheme = NM_SETTING_802_1X_CK_SCHEME_PATH;
        return gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->key_button));
 }
@@ -122,10 +118,8 @@ add_to_size_group (NMACertChooser *cert_chooser, GtkSizeGroup *group)
        NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
 
        gtk_size_group_add_widget (group, priv->cert_button_label);
-       if (priv->key_button_label)
-               gtk_size_group_add_widget (group, priv->key_button_label);
-       if (priv->key_password_label)
-               gtk_size_group_add_widget (group, priv->key_password_label);
+       gtk_size_group_add_widget (group, priv->key_button_label);
+       gtk_size_group_add_widget (group, priv->key_password_label);
 }
 
 static gboolean
@@ -133,6 +127,15 @@ validate (NMACertChooser *cert_chooser, GError **error)
 {
        NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
        GError *local = NULL;
+       char *tmp;
+
+       tmp = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->cert_button));
+       if (tmp) {
+               g_free (tmp);
+       } else {
+               g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("No certificate set"));
+               return FALSE;
+       }
 
        g_signal_emit_by_name (cert_chooser, "cert-validate", &local);
        if (local) {
@@ -143,7 +146,15 @@ validate (NMACertChooser *cert_chooser, GError **error)
                widget_unset_error (priv->cert_button);
        }
 
-       if (priv->key_button) {
+       if (gtk_widget_get_visible (priv->key_button)) {
+               tmp = gtk_file_chooser_get_filename (GTK_FILE_CHOOSER (priv->key_button));
+               if (tmp) {
+                       g_free (tmp);
+               } else {
+                       g_set_error_literal (error, NMA_ERROR, NMA_ERROR_GENERIC, _("No key set"));
+                       return FALSE;
+               }
+
                g_signal_emit_by_name (cert_chooser, "key-validate", &local);
                if (local) {
                        widget_set_error (priv->key_button);
@@ -208,24 +219,6 @@ get_key_password_flags (NMACertChooser *cert_chooser)
 }
 
 static void
-nma_cert_chooser_interface_init (NMACertChooserInterface *iface)
-{
-       iface->set_cert = set_cert;
-       iface->get_cert = get_cert;
-       iface->set_key = set_key;
-       iface->get_key = get_key;
-       iface->set_key_password = set_key_password;
-       iface->get_key_password = get_key_password;
-
-       iface->add_to_size_group = add_to_size_group;
-       iface->validate = validate;
-
-       iface->setup_key_password_storage = setup_key_password_storage;
-       iface->update_key_password_storage = update_key_password_storage;
-       iface->get_key_password_flags = get_key_password_flags;
-}
-
-static void
 reset_filter (GtkWidget *widget, GParamSpec *spec, gpointer user_data)
 {
        if (!gtk_file_chooser_get_filter (GTK_FILE_CHOOSER (widget))) {
@@ -240,7 +233,7 @@ cert_changed_cb (GtkFileChooserButton *file_chooser_button, gpointer user_data)
 {
        NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (user_data);
 
-       if (priv->key_button) {
+       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);
        }
@@ -272,100 +265,159 @@ show_toggled_cb (GtkCheckButton *button, gpointer user_data)
                                  gtk_toggle_button_get_active (GTK_TOGGLE_BUTTON (button)));
 }
 
-GtkWidget *
-nma_file_cert_chooser_new (const gchar *title, NMACertChooserFlags flags)
+static void
+set_title (NMACertChooser *cert_chooser, const gchar *title)
 {
-       GtkWidget *self;
-       NMAFileCertChooserPrivate *priv;
-       GtkFileFilter *filter;
+       NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
        gchar *text;
-       GtkWidget *show_password;
 
-       self = g_object_new (NMA_TYPE_FILE_CERT_CHOOSER, NULL);
-       priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (self);
-
-       gtk_grid_insert_column (GTK_GRID (self), 2);
-       gtk_grid_set_row_spacing (GTK_GRID (self), 6);
-       gtk_grid_set_column_spacing (GTK_GRID (self), 12);
-
-       if ((flags & NMA_CERT_CHOOSER_FLAG_CERT) == 0) {
-               /* The key chooser */
-               gtk_grid_insert_row (GTK_GRID (self), 0);
-
-               text = g_strdup_printf (_("Choose a key for %s certificate"), title);
-               priv->key_button = g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
-                                                 "action", GTK_FILE_CHOOSER_ACTION_OPEN,
-                                                 "title", text,
-                                                 "filter", utils_key_filter (),
-                                                 "local-only", TRUE,
-                                                 NULL);
-               g_free (text);
-               gtk_grid_attach (GTK_GRID (self), priv->key_button, 1, 0, 1, 1);
-               gtk_widget_set_hexpand (priv->key_button, TRUE);
-               gtk_widget_set_sensitive (priv->key_button, FALSE);
-               if ((flags & NMA_CERT_CHOOSER_FLAG_PASSWORDS) == 0)
-                       gtk_widget_show (priv->key_button);
-
-               g_signal_connect (priv->key_button, "selection-changed",
-                                 G_CALLBACK (key_changed_cb), self);
-
-               text = g_strdup_printf (_("%s private _key:"), title);
-               priv->key_button_label = gtk_label_new_with_mnemonic (text);
-               g_object_set (priv->key_button_label, "xalign", (gfloat) 0, NULL);
-               gtk_label_set_mnemonic_widget (GTK_LABEL (priv->key_button_label), priv->key_button);
-               gtk_grid_attach (GTK_GRID (self), priv->key_button_label, 0, 0, 1, 1);
-               gtk_widget_set_sensitive (priv->key_button_label, FALSE);
-               if ((flags & NMA_CERT_CHOOSER_FLAG_PASSWORDS) == 0)
-                       gtk_widget_show (priv->key_button_label);
-               g_free (text);
-
-               /* The key password entry */
-               gtk_grid_insert_row (GTK_GRID (self), 1);
-
-               priv->key_password = gtk_entry_new ();
-               gtk_entry_set_visibility (GTK_ENTRY (priv->key_password), FALSE);
-               gtk_grid_attach (GTK_GRID (self), priv->key_password, 1, 1, 1, 1);
-               gtk_widget_set_hexpand (priv->key_password, TRUE);
-               gtk_widget_set_sensitive (priv->key_password, FALSE);
-               gtk_widget_show (priv->key_password);
-
-               g_signal_connect (priv->key_password, "changed",
-                                 G_CALLBACK (key_password_changed_cb), self);
-
-               text = g_strdup_printf (_("%s key _password:"), title);
-               priv->key_password_label = gtk_label_new_with_mnemonic (text);
-               g_object_set (priv->key_password_label, "xalign", (gfloat) 0, NULL);
-               gtk_label_set_mnemonic_widget (GTK_LABEL (priv->key_password_label), priv->key_password);
-               gtk_grid_attach (GTK_GRID (self), priv->key_password_label, 0, 1, 1, 1);
-               gtk_widget_set_sensitive (priv->key_password_label, FALSE);
-               gtk_widget_show (priv->key_password_label);
-               g_free (text);
-
-               /* Show password */
-               gtk_grid_insert_row (GTK_GRID (self), 2);
-               show_password = gtk_check_button_new_with_mnemonic ("Sho_w password");
-               gtk_grid_attach (GTK_GRID (self), show_password, 1, 2, 1, 1);
-               gtk_widget_show (show_password);
-               g_signal_connect (show_password, "toggled",
-                                 G_CALLBACK (show_toggled_cb), self);
+       text = g_strdup_printf (_("Choose a key for %s Certificate"), title);
+       gtk_file_chooser_button_set_title (GTK_FILE_CHOOSER_BUTTON (priv->key_button), text);
+       g_free (text);
+
+       text = g_strdup_printf (_("%s private _key:"), title);
+       gtk_label_set_text_with_mnemonic (GTK_LABEL (priv->key_button_label), text);
+       g_free (text);
+
+       text = g_strdup_printf (_("%s key _password:"), title);
+       gtk_label_set_text_with_mnemonic (GTK_LABEL (priv->key_password_label), text);
+       g_free (text);
+
+       text = g_strdup_printf (_("Choose %s Certificate"), title);
+       gtk_file_chooser_button_set_title (GTK_FILE_CHOOSER_BUTTON (priv->cert_button), text);
+       g_free (text);
+
+       text = g_strdup_printf (_("%s _certificate:"), title);
+       gtk_label_set_text_with_mnemonic (GTK_LABEL (priv->cert_button_label), text);
+       g_free (text);
+}
+
+static void
+set_flags (NMACertChooser *cert_chooser, NMACertChooserFlags flags)
+{
+       NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (cert_chooser);
+
+       if (flags & NMA_CERT_CHOOSER_FLAG_CERT) {
+               gtk_widget_hide (priv->key_button);
+               gtk_widget_hide (priv->key_button_label);
+               gtk_widget_hide (priv->key_password);
+               gtk_widget_hide (priv->key_password_label);
+               gtk_widget_hide (priv->show_password);
        }
 
+       if (flags & NMA_CERT_CHOOSER_FLAG_PASSWORDS) {
+               gtk_widget_hide (priv->cert_button);
+               gtk_widget_hide (priv->cert_button_label);
+               gtk_widget_hide (priv->key_button);
+               gtk_widget_hide (priv->key_button_label);
+       }
+}
+
+static void
+nma_file_cert_chooser_class_init (NMAFileCertChooserClass *klass)
+{
+       GObjectClass *object_class = G_OBJECT_CLASS (klass);
+       NMACertChooserClass *chooser_class = NMA_CERT_CHOOSER_CLASS (klass);
+
+       g_type_class_add_private (object_class, sizeof (NMAFileCertChooserPrivate));
+
+       chooser_class->set_title = set_title;
+       chooser_class->set_flags = set_flags;
+
+       chooser_class->set_cert = set_cert;
+       chooser_class->get_cert = get_cert;
+       chooser_class->set_key = set_key;
+       chooser_class->get_key = get_key;
+       chooser_class->set_key_password = set_key_password;
+       chooser_class->get_key_password = get_key_password;
+
+       chooser_class->add_to_size_group = add_to_size_group;
+       chooser_class->validate = validate;
+
+       chooser_class->setup_key_password_storage = setup_key_password_storage;
+       chooser_class->update_key_password_storage = update_key_password_storage;
+       chooser_class->get_key_password_flags = get_key_password_flags;
+}
+
+static void
+nma_file_cert_chooser_init (NMAFileCertChooser *file_cert_chooser)
+{
+       NMAFileCertChooserPrivate *priv = NMA_FILE_CERT_CHOOSER_GET_PRIVATE (file_cert_chooser);
+       GtkFileFilter *filter;
+
+       gtk_grid_insert_column (GTK_GRID (file_cert_chooser), 2);
+       gtk_grid_set_row_spacing (GTK_GRID (file_cert_chooser), 6);
+       gtk_grid_set_column_spacing (GTK_GRID (file_cert_chooser), 12);
+
+       /* The key chooser */
+       gtk_grid_insert_row (GTK_GRID (file_cert_chooser), 0);
+
+       priv->key_button = g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
+                                         "action", GTK_FILE_CHOOSER_ACTION_OPEN,
+                                         "filter", utils_key_filter (),
+                                         "local-only", TRUE,
+                                         NULL);
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->key_button, 1, 0, 1, 1);
+       gtk_widget_set_hexpand (priv->key_button, TRUE);
+       gtk_widget_set_sensitive (priv->key_button, FALSE);
+       gtk_widget_show (priv->key_button);
+       gtk_widget_set_no_show_all (priv->key_button, TRUE);
+
+        g_signal_connect (priv->key_button, "selection-changed",
+                         G_CALLBACK (key_changed_cb), file_cert_chooser);
+
+       priv->key_button_label = gtk_label_new (NULL);
+       g_object_set (priv->key_button_label, "xalign", (gfloat) 0, NULL);
+       gtk_label_set_mnemonic_widget (GTK_LABEL (priv->key_button_label), priv->key_button);
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->key_button_label, 0, 0, 1, 1);
+       gtk_widget_set_sensitive (priv->key_button_label, FALSE);
+       gtk_widget_show (priv->key_button_label);
+       gtk_widget_set_no_show_all (priv->key_button_label, TRUE);
+
+       /* The key password entry */
+       gtk_grid_insert_row (GTK_GRID (file_cert_chooser), 1);
+
+       priv->key_password = gtk_entry_new ();
+       gtk_entry_set_visibility (GTK_ENTRY (priv->key_password), FALSE);
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->key_password, 1, 1, 1, 1);
+       gtk_widget_set_hexpand (priv->key_password, TRUE);
+       gtk_widget_set_sensitive (priv->key_password, FALSE);
+       gtk_widget_show (priv->key_password);
+       gtk_widget_set_no_show_all (priv->key_password, TRUE);
+
+        g_signal_connect (priv->key_password, "changed",
+                         G_CALLBACK (key_password_changed_cb), file_cert_chooser);
+
+       priv->key_password_label = gtk_label_new (NULL);
+       g_object_set (priv->key_password_label, "xalign", (gfloat) 0, NULL);
+       gtk_label_set_mnemonic_widget (GTK_LABEL (priv->key_password_label), priv->key_password);
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->key_password_label, 0, 1, 1, 1);
+       gtk_widget_set_sensitive (priv->key_password_label, FALSE);
+       gtk_widget_show (priv->key_password_label);
+       gtk_widget_set_no_show_all (priv->key_password_label, TRUE);
+
+       /* Show password */
+       gtk_grid_insert_row (GTK_GRID (file_cert_chooser), 2);
+       priv->show_password = gtk_check_button_new_with_mnemonic ("Sho_w password");
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->show_password, 1, 2, 1, 1);
+       gtk_widget_show (priv->show_password);
+       gtk_widget_set_no_show_all (priv->show_password, TRUE);
+       g_signal_connect (priv->show_password, "toggled",
+                         G_CALLBACK (show_toggled_cb), file_cert_chooser);
+
        /* The certificate chooser */
-       gtk_grid_insert_row (GTK_GRID (self), 0);
+       gtk_grid_insert_row (GTK_GRID (file_cert_chooser), 0);
 
        filter = utils_cert_filter ();
-       text = g_strdup_printf (_("Choose %s certificate"), title);
        priv->cert_button = g_object_new (GTK_TYPE_FILE_CHOOSER_BUTTON,
                                          "action", GTK_FILE_CHOOSER_ACTION_OPEN,
-                                         "title", text,
                                          "filter", filter,
                                          "local-only", TRUE,
                                          NULL);
-       g_free (text);
-       gtk_grid_attach (GTK_GRID (self), priv->cert_button, 1, 0, 1, 1);
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->cert_button, 1, 0, 1, 1);
        gtk_widget_set_hexpand (priv->cert_button, TRUE);
-       if ((flags & NMA_CERT_CHOOSER_FLAG_PASSWORDS) == 0)
-               gtk_widget_show (priv->cert_button);
+       gtk_widget_show (priv->cert_button);
+       gtk_widget_set_no_show_all (priv->cert_button, TRUE);
 
        /* For some reason, GTK+ calls set_current_filter (..., NULL) from
         * gtkfilechooserdefault.c::show_and_select_files_finished_loading() on our
@@ -376,29 +428,12 @@ nma_file_cert_chooser_new (const gchar *title, NMACertChooserFlags flags)
                          G_CALLBACK (reset_filter), filter);
 
        g_signal_connect (priv->cert_button, "selection-changed",
-                         G_CALLBACK (cert_changed_cb), self);
+                         G_CALLBACK (cert_changed_cb), file_cert_chooser);
 
-       text = g_strdup_printf (_("%s _certificate:"), title);
-       priv->cert_button_label = gtk_label_new_with_mnemonic (text);
+       priv->cert_button_label = gtk_label_new (NULL);
        g_object_set (priv->cert_button_label, "xalign", (gfloat) 0, NULL);
        gtk_label_set_mnemonic_widget (GTK_LABEL (priv->cert_button_label), priv->cert_button);
-       gtk_grid_attach (GTK_GRID (self), priv->cert_button_label, 0, 0, 1, 1);
-       if ((flags & NMA_CERT_CHOOSER_FLAG_PASSWORDS) == 0)
-               gtk_widget_show (priv->cert_button_label);
-       g_free (text);
-
-       return self;
-}
-
-static void
-nma_file_cert_chooser_class_init (NMAFileCertChooserClass *klass)
-{
-       GObjectClass *object_class = G_OBJECT_CLASS (klass);
-
-       g_type_class_add_private (object_class, sizeof (NMAFileCertChooserPrivate));
-}
-
-static void
-nma_file_cert_chooser_init (NMAFileCertChooser *file_cert_chooser)
-{
+       gtk_grid_attach (GTK_GRID (file_cert_chooser), priv->cert_button_label, 0, 0, 1, 1);
+       gtk_widget_show (priv->cert_button_label);
+       gtk_widget_set_no_show_all (priv->cert_button_label, TRUE);
 }
diff --git a/src/libnma/nma-file-cert-chooser.h b/src/libnma/nma-file-cert-chooser.h
index 5eafd86..fb95650 100644
--- a/src/libnma/nma-file-cert-chooser.h
+++ b/src/libnma/nma-file-cert-chooser.h
@@ -36,17 +36,15 @@ G_BEGIN_DECLS
 #define NMA_FILE_CERT_CHOOSER_GET_CLASS(obj)         (G_TYPE_INSTANCE_GET_CLASS ((obj), 
NMA_TYPE_FILE_CERT_CHOOSER, NMAFileCertChooserClass))
 
 typedef struct {
-       GtkGrid parent;
+       NMACertChooser parent;
 } NMAFileCertChooser;
 
 typedef struct {
-       GtkGridClass parent_class;
+       NMACertChooserClass parent_class;
 } NMAFileCertChooserClass;
 
 GType nma_file_cert_chooser_get_type (void);
 
-GtkWidget *nma_file_cert_chooser_new (const gchar *title, NMACertChooserFlags flags);
-
 G_END_DECLS
 
 #endif /* NMA_FILE_CERT_CHOOSER_H */


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