[network-manager-applet/lr/pkcs11: 11/14] XXX add pkcs11 object chooser button
- From: Lubomir Rintel <lkundrak src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [network-manager-applet/lr/pkcs11: 11/14] XXX add pkcs11 object chooser button
- Date: Fri, 24 Feb 2017 19:20:29 +0000 (UTC)
commit c93af5a43e8b7729f73250ae0da69e86463a2c70
Author: Lubomir Rintel <lkundrak v3 sk>
Date: Fri Feb 24 18:39:33 2017 +0100
XXX add pkcs11 object chooser button
Makefile.am | 6 +-
src/libnma/libnma.ver | 3 +
src/libnma/nma-cert-chooser-button.c | 380 ++++++++++++++++++++++++++++++++++
src/libnma/nma-cert-chooser-button.h | 42 ++++
4 files changed, 429 insertions(+), 2 deletions(-)
---
diff --git a/Makefile.am b/Makefile.am
index 3664cb8..3158068 100644
--- a/Makefile.am
+++ b/Makefile.am
@@ -484,7 +484,8 @@ libnma_h_pub = \
src/libnma/nma-cert-chooser.h \
src/libnma/nma-file-cert-chooser.h \
src/libnma/nma-pkcs11-token-login-dialog.h \
- src/libnma/nma-pkcs11-cert-chooser-dialog.h
+ src/libnma/nma-pkcs11-cert-chooser-dialog.h \
+ src/libnma/nma-cert-chooser-button.h
libnma_c_real = \
src/libnma/nma-wifi-dialog.c \
@@ -496,7 +497,8 @@ libnma_c_real = \
src/libnma/nma-cert-chooser.c \
src/libnma/nma-file-cert-chooser.c \
src/libnma/nma-pkcs11-token-login-dialog.c \
- src/libnma/nma-pkcs11-cert-chooser-dialog.c
+ src/libnma/nma-pkcs11-cert-chooser-dialog.c \
+ src/libnma/nma-cert-chooser-button.c
src_libnma_libnmadir = $(includedir)/libnma
diff --git a/src/libnma/libnma.ver b/src/libnma/libnma.ver
index 0dbdbe7..8db1ad2 100644
--- a/src/libnma/libnma.ver
+++ b/src/libnma/libnma.ver
@@ -73,6 +73,9 @@ local:
libnma_1_8_0 {
global:
nma_cert_chooser_add_to_size_group;
+ nma_cert_chooser_button_new;
+ nma_cert_chooser_button_get_type;
+ nma_cert_chooser_button_get_uri;
nma_cert_chooser_get_type;
nma_cert_chooser_set_cert;
nma_cert_chooser_get_cert;
diff --git a/src/libnma/nma-cert-chooser-button.c b/src/libnma/nma-cert-chooser-button.c
new file mode 100644
index 0000000..79700c7
--- /dev/null
+++ b/src/libnma/nma-cert-chooser-button.c
@@ -0,0 +1,380 @@
+/* NetworkManager Applet -- allow user control over networking
+ *
+ * Lubomir Rintel <lkundrak v3 sk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2016,2017 Red Hat, Inc.
+ */
+
+#include <gck/gck.h>
+#include <gtk/gtk.h>
+
+#include "nma-cert-chooser-button.h"
+#include "nma-pkcs11-cert-chooser-dialog.h"
+#include "nm-default.h"
+
+/**
+ * XXX
+ * SECTION:nma-cert-chooser-button
+ * @title: NMACertChooserButton
+ * @short_description: The PKCS11 Tokens Sidebar
+ * @see_also: #GcrObjectChooserDialog
+ *
+ * #NMACertChooserButton is the list of selectable PKCS11 slots present in the
+ * system. The slots that support PIN have a login button.
+ */
+
+#if 0
+enum {
+ TOKEN_ADDED,
+ TOKEN_LOGIN,
+ OPEN_TOKEN,
+ LAST_SIGNAL
+};
+
+static guint signals[LAST_SIGNAL] = { 0 };
+#endif
+
+typedef struct {
+ gchar *uri; // XXX
+// GtkListStore *model;
+#if 0
+ GtkWidget *viewport;
+ GtkWidget *listbox;
+#endif
+} NMACertChooserButtonPrivate;
+
+G_DEFINE_TYPE (NMACertChooserButton, nma_cert_chooser_button, GTK_TYPE_COMBO_BOX);
+
+#define NMA_CERT_CHOOSER_BUTTON_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
+ NMA_TYPE_CERT_CHOOSER_BUTTON, \
+ NMACertChooserButtonPrivate))
+
+static void
+modules_initialized (GObject *object, GAsyncResult *res, gpointer user_data)
+{
+ NMACertChooserButton *self = NMA_CERT_CHOOSER_BUTTON (user_data);
+ GList *slots;
+ GList *list_iter;
+ GError *error = NULL;
+ GList *modules;
+ GtkTreeIter iter;
+ GtkListStore *model;
+ GckTokenInfo *info;
+ gchar *label;
+
+ modules = gck_modules_initialize_registered_finish (res, &error);
+ if (!modules) {
+ /* The Front Fell Off. */
+ g_critical ("Error getting registered modules: %s", error->message);
+ g_error_free (error);
+ }
+
+ model = GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (self)));
+
+ gtk_list_store_insert_with_values (model, &iter, 2, 0, NULL, 1, NULL, -1);
+
+ slots = gck_modules_get_slots (modules, FALSE);
+ for (list_iter = slots; list_iter; list_iter = list_iter->next) {
+ GckSlot *slot = GCK_SLOT (list_iter->data);
+
+ info = gck_slot_get_token_info (slot);
+ g_return_if_fail (info);
+ label = g_strdup_printf ("%s\342\200\246", info->label);
+ gtk_list_store_insert_with_values (model, &iter, 2,
+ 0, label,
+ 1, slot, -1);
+ g_free (label);
+ gck_token_info_free (info);
+ }
+
+ gck_list_unref_free (slots);
+ gck_list_unref_free (modules);
+}
+
+static void
+update_title (NMACertChooserButton *button)
+{
+ NMACertChooserButtonPrivate *priv = NMA_CERT_CHOOSER_BUTTON_GET_PRIVATE (button);
+ GckUriData *data;
+ GtkTreeIter iter;
+ GtkTreeModel *model;
+ gchar *label;
+ GError *error = NULL;
+
+ model = gtk_combo_box_get_model (GTK_COMBO_BOX (button));
+
+ if (!gtk_tree_model_get_iter_first (model, &iter))
+ g_return_if_reached ();
+
+ if (!priv->uri) {
+ label = g_strdup (_("(None)"));
+ } else if (g_str_has_prefix (priv->uri, "pkcs11:")) {
+ data = gck_uri_parse (priv->uri, GCK_URI_FOR_ANY, &error);
+ if (data) {
+ if (!gck_attributes_find_string (data->attributes, CKA_LABEL, &label)) {
+ if (data->token_info)
+ label = g_strdup_printf (_("Certificate in %s"),
data->token_info->label);
+ }
+ gck_uri_data_free (data);
+ } else {
+ g_warning ("Bad URI '%s': %s\n", priv->uri, error->message);
+ g_error_free (error);
+ }
+ } else {
+ label = priv->uri;
+ if (g_str_has_prefix (label, "file://"))
+ label += 7;
+ if (g_strrstr (label, "/"))
+ label = g_strrstr (label, "/") + 1;
+ label = g_strdup (label);
+ }
+
+ if (!label)
+ label = g_strdup (_("(Unknown)"));
+ gtk_list_store_set (GTK_LIST_STORE (model), &iter, 0, label, -1);
+ g_free (label);
+}
+
+static void
+select_from_token (NMACertChooserButton *button, GckSlot *slot)
+{
+ NMACertChooserButtonPrivate *priv = NMA_CERT_CHOOSER_BUTTON_GET_PRIVATE (button);
+ GtkWidget *dialog;
+
+ dialog = nma_pkcs11_cert_chooser_dialog_new (slot, "yolo", NULL,
+ GTK_FILE_CHOOSER_ACTION_OPEN | GTK_DIALOG_USE_HEADER_BAR,
+ _("Select"), GTK_RESPONSE_ACCEPT,
+ _("Cancel"), GTK_RESPONSE_CANCEL,
+ NULL);
+ if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
+ if (priv->uri)
+ g_free (priv->uri);
+ priv->uri = nma_pkcs11_cert_chooser_dialog_get_uri (NMA_PKCS11_CERT_CHOOSER_DIALOG (dialog));
+ update_title (button);
+ // nma_pkcs11_cert_chooser_dialog_get_pin (NMA_PKCS11_CERT_CHOOSER_DIALOG (dialog));
+ // nma_pkcs11_cert_chooser_dialog_get_remember_pin (NMA_PKCS11_CERT_CHOOSER_DIALOG (dialog));
+ }
+ gtk_widget_destroy (dialog);
+}
+
+static void
+select_from_file (NMACertChooserButton *button)
+{
+ NMACertChooserButtonPrivate *priv = NMA_CERT_CHOOSER_BUTTON_GET_PRIVATE (button);
+ GtkWidget *dialog;
+
+ dialog = gtk_file_chooser_dialog_new ("yolo", NULL,
+ GTK_FILE_CHOOSER_ACTION_OPEN | GTK_DIALOG_USE_HEADER_BAR,
+ _("Select"), GTK_RESPONSE_ACCEPT,
+ _("Cancel"), GTK_RESPONSE_CANCEL,
+ NULL);
+ gtk_dialog_set_default_response (GTK_DIALOG (dialog), GTK_RESPONSE_ACCEPT);
+ if (gtk_dialog_run (GTK_DIALOG (dialog)) == GTK_RESPONSE_ACCEPT) {
+ if (priv->uri)
+ g_free (priv->uri);
+ priv->uri = gtk_file_chooser_get_uri (GTK_FILE_CHOOSER (dialog));
+ update_title (button);
+ // nma_pkcs11_cert_chooser_dialog_get_pin (NMA_PKCS11_CERT_CHOOSER_DIALOG (dialog));
+ // nma_pkcs11_cert_chooser_dialog_get_remember_pin (NMA_PKCS11_CERT_CHOOSER_DIALOG (dialog));
+ }
+ gtk_widget_destroy (dialog);
+}
+
+
+static void
+changed (GtkComboBox *combo_box)
+{
+ GtkTreeIter iter;
+ GtkTreeModel *model;
+ gchar *label;
+ GckSlot *slot;
+
+ if (gtk_combo_box_get_active (combo_box) == 0)
+ return;
+
+ gtk_combo_box_get_active_iter (combo_box, &iter);
+
+ model = gtk_combo_box_get_model (combo_box);
+ gtk_tree_model_get (model, &iter, 0, &label, 1, &slot, -1);
+ g_printerr ("Select [%s]\n", label);
+ if (slot)
+ select_from_token (NMA_CERT_CHOOSER_BUTTON (combo_box), slot);
+ else
+ select_from_file (NMA_CERT_CHOOSER_BUTTON (combo_box));
+
+ g_free (label);
+ g_clear_object (&slot);
+ gtk_combo_box_set_active (combo_box, 0);
+}
+
+static void
+nma_cert_chooser_button_class_init (NMACertChooserButtonClass *klass)
+{
+ GObjectClass *object_class = G_OBJECT_CLASS (klass);
+ GtkComboBoxClass *combo_box_class = GTK_COMBO_BOX_CLASS (klass);
+
+ g_type_class_add_private (object_class, sizeof (NMACertChooserButtonPrivate));
+
+ combo_box_class->changed = changed;
+
+#if 0
+ GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);
+
+ gtk_widget_class_set_css_name (widget_class, "placessidebar");
+
+ /**
+ * NMACertChooserButton::token-added:
+ * @slot: The slot that is populated with a token
+ *
+ * Emmitted when a token was found in a slot.
+ * The #NMACertChooserButton initiates a search for tokens when
+ * constructed and emits this signal for each slot.
+ */
+ signals[TOKEN_ADDED] = g_signal_new ("token-added",
+ G_OBJECT_CLASS_TYPE (object_class),
+ G_SIGNAL_RUN_FIRST,
+ 0, NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE,
+ 1, G_TYPE_OBJECT);
+
+ /**
+ * NMACertChooserButton::token-login:
+ * @slot: The slot for which login was requested
+ *
+ * Emmitted when the login button was clocked, requesting
+ * the entry of a PIN.
+ */
+ signals[TOKEN_LOGIN] = g_signal_new ("token-login",
+ G_OBJECT_CLASS_TYPE (object_class),
+ G_SIGNAL_RUN_FIRST,
+ 0, NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE,
+ 1, G_TYPE_OBJECT);
+
+ /**
+ * NMACertChooserButton::open-token:
+ * @slot: The slot that contains the selected token
+ *
+ * Emmitted when the token was selected.
+ */
+ signals[OPEN_TOKEN] = g_signal_new ("open-token",
+ G_OBJECT_CLASS_TYPE (object_class),
+ G_SIGNAL_RUN_FIRST,
+ 0, NULL, NULL,
+ g_cclosure_marshal_VOID__OBJECT,
+ G_TYPE_NONE,
+ 1, G_TYPE_OBJECT);
+#endif
+}
+
+#if 0
+static void
+row_activated (GtkListBox *box, GtkListBoxRow *row, gpointer user_data)
+{
+ NMACertChooserButton *self = user_data;
+
+ g_signal_emit (self, signals[OPEN_TOKEN], 0,
+ nma_cert_chooser_button_row_get_slot (NMA_CERT_CHOOSER_BUTTON_ROW (row)));
+}
+#endif
+
+static void
+nma_cert_chooser_button_init (NMACertChooserButton *self)
+{
+ gck_modules_initialize_registered_async (NULL, modules_initialized, self);
+}
+
+static gboolean
+row_separator (GtkTreeModel *model, GtkTreeIter *iter, gpointer data)
+{
+ gchar *label;
+ GckSlot *slot;
+
+ gtk_tree_model_get (model, iter, 0, &label, 1, &slot, -1);
+ if (label == NULL && slot == NULL)
+ return TRUE;
+ g_free (label);
+ g_clear_object (&slot);
+
+ return FALSE;
+}
+
+gchar *
+nma_cert_chooser_button_get_uri (NMACertChooserButton *button)
+{
+ NMACertChooserButtonPrivate *priv = NMA_CERT_CHOOSER_BUTTON_GET_PRIVATE (button);
+
+ return g_strdup (priv->uri);
+}
+
+void
+nma_cert_chooser_button_set_uri (NMACertChooserButton *button, const gchar *uri)
+{
+ NMACertChooserButtonPrivate *priv = NMA_CERT_CHOOSER_BUTTON_GET_PRIVATE (button);
+
+ if (priv->uri)
+ g_free (priv->uri);
+ priv->uri = g_strdup (uri);
+ update_title (button);
+}
+
+/**
+ * nma_cert_chooser_button_new:
+ *
+ * Creates the new button that can select certificates from
+ * files or PKCS11 tokens.
+ *
+ * Returns: the newly created #NMACertChooserButton
+ */
+GtkWidget *
+nma_cert_chooser_button_new ()
+{
+ GtkWidget *self;
+ GtkListStore *model;
+ GtkTreeIter iter;
+ GtkCellRenderer *cell;
+
+ model = gtk_list_store_new (2, G_TYPE_STRING, GCK_TYPE_SLOT);
+ self = g_object_new (NMA_TYPE_CERT_CHOOSER_BUTTON,
+ "model", model,
+ NULL);
+
+ gtk_combo_box_set_row_separator_func (GTK_COMBO_BOX (self),
+ row_separator,
+ NULL,
+ NULL);
+
+ /* The first entry with current object name. */
+ gtk_list_store_insert_with_values (model, &iter, 0, 0, NULL, 1, NULL, -1);
+ update_title (NMA_CERT_CHOOSER_BUTTON (self));
+
+ /* The separator and the last entry. The tokens will be added in between. */
+ gtk_list_store_insert_with_values (model, &iter, 1, 0, NULL, 1, NULL, -1);
+ gtk_list_store_insert_with_values (model, &iter, 2,
+ 0, _("Select from file\342\200\246"),
+ 1, NULL, -1);
+
+ cell = gtk_cell_renderer_text_new ();
+ gtk_cell_layout_pack_start (GTK_CELL_LAYOUT (self), cell, FALSE);
+ gtk_cell_layout_add_attribute (GTK_CELL_LAYOUT (self), cell, "text", 0);
+ gtk_combo_box_set_active (GTK_COMBO_BOX (self), 0);
+
+ return self;
+}
diff --git a/src/libnma/nma-cert-chooser-button.h b/src/libnma/nma-cert-chooser-button.h
new file mode 100644
index 0000000..6295926
--- /dev/null
+++ b/src/libnma/nma-cert-chooser-button.h
@@ -0,0 +1,42 @@
+/* NetworkManager Applet -- allow user control over networking
+ *
+ * Lubomir Rintel <lkundrak v3 sk>
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the
+ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
+ * Boston, MA 02110-1301 USA.
+ *
+ * Copyright (C) 2016,2017 Red Hat, Inc.
+ */
+
+#ifndef __NMA_CERT_CHOOSER_BUTTON_H__
+#define __NMA_CERT_CHOOSER_BUTTON_H__
+
+#include <gtk/gtk.h>
+
+struct _NMACertChooserButton
+{
+ GtkComboBox parent_instance;
+};
+
+#define NMA_TYPE_CERT_CHOOSER_BUTTON nma_cert_chooser_button_get_type ()
+G_DECLARE_FINAL_TYPE (NMACertChooserButton, nma_cert_chooser_button, NMA, CERT_CHOOSER_BUTTON, GtkComboBox)
+
+GtkWidget *nma_cert_chooser_button_new (void);
+
+gchar *nma_cert_chooser_button_get_uri (NMACertChooserButton *button);
+
+void nma_cert_chooser_button_set_uri (NMACertChooserButton *button, const gchar *uri);
+
+#endif /* __NMA_CERT_CHOOSER_BUTTON_H__ */
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]