[seahorse] Adds a DBus interface to create keys



commit c388601bf88412756621e4382c691829bc903ab0
Author: Thorsten Sick <tsick src gnome org>
Date:   Mon Feb 1 14:02:25 2010 +0100

    Adds a DBus interface to create keys
    
    User name, email address and comments can be passed over DBus. The only
    dialog being shown is the password creation dialog.

 daemon/seahorse-service.c     |   72 ++++++++++++++++++++++++++
 daemon/seahorse-service.h     |    3 +
 daemon/seahorse-service.xml   |    7 +++
 pgp/seahorse-gpgme-dialogs.h  |   14 +++++-
 pgp/seahorse-gpgme-generate.c |  111 ++++++++++++++++++++++++++++++----------
 5 files changed, 178 insertions(+), 29 deletions(-)
---
diff --git a/daemon/seahorse-service.c b/daemon/seahorse-service.c
index 9bbd754..cc93155 100644
--- a/daemon/seahorse-service.c
+++ b/daemon/seahorse-service.c
@@ -36,6 +36,10 @@
 #include "seahorse-source.h"
 #include "seahorse-util.h"
 
+#include "../pgp/seahorse-pgp.h"
+#include "../pgp/seahorse-gpgme-source.h"
+#include "../pgp/seahorse-gpgme-key-op.h"
+
 #include <gio/gio.h>
 
 #define KEYSET_PATH "/org/gnome/seahorse/keys/%s"
@@ -162,6 +166,74 @@ seahorse_service_get_keyset (SeahorseService *svc, gchar *ktype,
     return TRUE;
 }
 
+
+/**
+* seahorse_service_generate_credentials:
+* @svc: the seahorse context
+* @ktype: the keytype (example: "openpgp")
+* @values: key-value pairs
+* @error: the error
+*
+* DBus: GenerateCredentials
+*
+* Generates credentials. Will pop up the data input window (name, email, comment)
+* pre-filled with the supplied data. A password dialog will be next. After that
+* the key is created.
+*
+* Returns: True on success
+*/
+gboolean
+seahorse_service_generate_credentials (SeahorseService *svc, gchar *ktype,
+                                       GHashTable *values, GError **error)
+{
+    SeahorseSource *sksrc;
+    GValue  val={0};
+    GValue  *pval=NULL;
+    gchar   *name=NULL;
+    gchar   *email=NULL;
+    gchar   *comment=NULL;
+    SeahorseWidget *swidget;
+
+    sksrc = seahorse_context_find_source (seahorse_context_for_app (),
+                                          SEAHORSE_PGP_TYPE,
+                                          SEAHORSE_LOCATION_LOCAL);
+    g_return_val_if_fail (sksrc != NULL, FALSE);
+
+    pval = &val;
+
+    if (g_strcmp0 (ktype,"openpgp")==0) {
+        pval = (GValue *)g_hash_table_lookup (values,"name");
+        if ((pval) && (G_VALUE_TYPE (pval) == G_TYPE_STRING))
+            name=g_value_dup_string (pval);
+
+        pval = g_hash_table_lookup (values,"email");
+        if ((pval) && (G_VALUE_TYPE (pval) == G_TYPE_STRING))
+            email=g_value_dup_string (pval);
+
+        pval = g_hash_table_lookup (values,"comment");
+        if ((pval) && (G_VALUE_TYPE (pval) == G_TYPE_STRING))
+            comment=g_value_dup_string (pval);
+
+        seahorse_gpgme_generate_key(sksrc, name, email, comment,
+                        DSA_ELGAMAL, 2048,0);
+
+        g_free (name);
+        name = NULL;
+        g_free (email);
+        email = NULL;
+        g_free (comment);
+        comment = NULL;
+    }
+    else {
+        g_set_error (error, SEAHORSE_DBUS_ERROR, SEAHORSE_DBUS_ERROR_INVALID,
+                     _("This keytype is not supported: %s"), ktype);
+        return FALSE;
+    }
+
+    return TRUE;
+}
+
+
 /**
 * seahorse_service_import_keys:
 * @svc: the seahorse context
diff --git a/daemon/seahorse-service.h b/daemon/seahorse-service.h
index 6a519b8..f15a079 100644
--- a/daemon/seahorse-service.h
+++ b/daemon/seahorse-service.h
@@ -68,6 +68,9 @@ gboolean       seahorse_service_get_key_types         (SeahorseService *svc, gch
 gboolean        seahorse_service_get_keyset           (SeahorseService *svc, gchar *ktype, 
                                                        gchar **path, GError **error);
 
+gboolean       seahorse_service_generate_credentials  (SeahorseService *svc, gchar *ktype,
+                              GHashTable *values, GError **error);
+
 gboolean        seahorse_service_import_keys           (SeahorseService *svc, gchar *ktype, 
                                                         gchar *data, gchar ***keys, GError **error);
                               
diff --git a/daemon/seahorse-service.xml b/daemon/seahorse-service.xml
index dce01e0..eda9583 100644
--- a/daemon/seahorse-service.xml
+++ b/daemon/seahorse-service.xml
@@ -53,6 +53,13 @@
             <arg type="as" name="keys" direction="in"/>
         </method>
 -->
+
+        <method name="GenerateCredentials">
+            <annotation name="org.freedesktop.DBus.GLib.CSymbol"
+                value="seahorse_service_generate_credentials"/>
+            <arg type="s" name="keytype" direction="in"/>
+            <arg type="a{sv}" name="values" direction="in"/>
+        </method>
     </interface>
 
 </node>
diff --git a/pgp/seahorse-gpgme-dialogs.h b/pgp/seahorse-gpgme-dialogs.h
index eba74c9..5aa48cc 100644
--- a/pgp/seahorse-gpgme-dialogs.h
+++ b/pgp/seahorse-gpgme-dialogs.h
@@ -34,6 +34,7 @@
 #include "pgp/seahorse-gpgme-subkey.h"
 #include "pgp/seahorse-gpgme-source.h"
 #include "pgp/seahorse-gpgme-uid.h"
+#include "libseahorse/seahorse-widget.h"
 
 void            seahorse_gpgme_sign_prompt         (SeahorseGpgmeKey *key,
                                                     GtkWindow *parent);
@@ -44,7 +45,18 @@ void            seahorse_gpgme_sign_prompt_uid     (SeahorseGpgmeUid *uid,
 void            seahorse_gpgme_generate_register    (void);
 
 void            seahorse_gpgme_generate_show        (SeahorseGpgmeSource *sksrc,
-                                                     GtkWindow *parent);
+                                                     GtkWindow *parent,
+                                                     const char * name,
+                                                     const char *email,
+                                                     const gchar *comment);
+
+void            seahorse_gpgme_generate_key         (SeahorseGpgmeSource *sksrc,
+                                                     const gchar *name,
+                                                     const gchar *email,
+                                                     const gchar *comment,
+                                                     guint type,
+                                                     guint bits,
+                                                     time_t expires);
 
 void            seahorse_gpgme_add_revoker_new      (SeahorseGpgmeKey *pkey,
                                                      GtkWindow *parent);
diff --git a/pgp/seahorse-gpgme-generate.c b/pgp/seahorse-gpgme-generate.c
index 5fbafbe..c8a51e7 100644
--- a/pgp/seahorse-gpgme-generate.c
+++ b/pgp/seahorse-gpgme-generate.c
@@ -72,7 +72,7 @@ on_pgp_generate_key (GtkAction *action, gpointer unused)
 	sksrc = seahorse_context_find_source (seahorse_context_for_app (), SEAHORSE_PGP_TYPE, SEAHORSE_LOCATION_LOCAL);
 	g_return_if_fail (sksrc != NULL);
 	
-	seahorse_gpgme_generate_show (SEAHORSE_GPGME_SOURCE (sksrc), NULL);
+	seahorse_gpgme_generate_show (SEAHORSE_GPGME_SOURCE (sksrc), NULL, NULL, NULL, NULL);
 }
 
 static const GtkActionEntry ACTION_ENTRIES[] = {
@@ -165,6 +165,53 @@ get_expiry_date (SeahorseWidget *swidget)
     return widget;
 }
 
+
+/**
+ * gpgme_generate_key:
+ * @sksrc: the seahorse source
+ * @name: the user's full name
+ * @email: the user's email address
+ * @comment: a comment, added to the key
+ * @type: key type like DSA_ELGAMAL
+ * @bits: the number of bits for the key to generate (2048)
+ * @expires: expiry date can be 0
+ *
+ * Displays a password generation box and creates a key afterwards. For the key
+ * data it uses @name @email and @comment ncryption is chosen by @type and @bits
+ * @expire sets the expiry date
+ *
+ */
+void seahorse_gpgme_generate_key (SeahorseGpgmeSource *sksrc, const gchar *name, const gchar *email,
+                            const gchar *comment, guint type, guint bits, time_t expires)
+{
+    SeahorseOperation *op;
+    const gchar *pass;
+    gpgme_error_t gerr;
+    GtkDialog *dialog;
+
+
+
+    dialog = seahorse_passphrase_prompt_show (_("Passphrase for New PGP Key"),
+                                              _("Enter the passphrase for your new key twice."),
+                                              NULL, NULL, TRUE);
+    if (gtk_dialog_run (dialog) == GTK_RESPONSE_ACCEPT)
+    {
+        pass = seahorse_passphrase_prompt_get (dialog);
+        op = seahorse_gpgme_key_op_generate (sksrc, name, email, comment,
+                                             pass, type, bits, expires, &gerr);
+
+        if (!GPG_IS_OK (gerr)) {
+            seahorse_gpgme_handle_error (gerr, _("Couldn't generate key"));
+        } else {
+            seahorse_progress_show (op, _("Generating key"), TRUE);
+            seahorse_operation_watch (op, (SeahorseDoneFunc)completion_handler, NULL, NULL, NULL);
+            g_object_unref (op);
+        }
+    }
+    gtk_widget_destroy (GTK_WIDGET (dialog));
+}
+
+
 /**
  * on_gpgme_generate_response:
  * @dialog:
@@ -195,12 +242,12 @@ on_gpgme_generate_response (GtkDialog *dialog, guint response, SeahorseWidget *s
         seahorse_widget_show_help (swidget);
         return;
     }
-    
+
     if (response != GTK_RESPONSE_OK) {
         seahorse_widget_destroy (swidget);
         return;
     }
-    
+
     /* The name */
     widget = seahorse_widget_get_widget (swidget, "name-entry");
     g_return_if_fail (widget != NULL);
@@ -210,17 +257,17 @@ on_gpgme_generate_response (GtkDialog *dialog, guint response, SeahorseWidget *s
     /* Make sure it's the right length. Should have been checked earlier */
     name = g_strstrip (name);
     g_return_if_fail (strlen(name) >= 5);
-    
+
     /* The email address */
     widget = seahorse_widget_get_widget (swidget, "email-entry");
     g_return_if_fail (widget != NULL);
     email = gtk_entry_get_text (GTK_ENTRY (widget));
-    
+
     /* The comment */
     widget = seahorse_widget_get_widget (swidget, "comment-entry");
     g_return_if_fail (widget != NULL);
     comment = gtk_entry_get_text (GTK_ENTRY (widget));
-    
+
     /* The algorithm */
     widget = seahorse_widget_get_widget (swidget, "algorithm-choice");
     g_return_if_fail (widget != NULL);
@@ -251,29 +298,13 @@ on_gpgme_generate_response (GtkDialog *dialog, guint response, SeahorseWidget *s
 
     sksrc = SEAHORSE_GPGME_SOURCE (g_object_get_data (G_OBJECT (swidget), "source"));
     g_assert (SEAHORSE_IS_GPGME_SOURCE (sksrc));
-    
+
     /* Less confusing with less on the screen */
     gtk_widget_hide (seahorse_widget_get_toplevel (swidget));
-    
-    dialog = seahorse_passphrase_prompt_show (_("Passphrase for New PGP Key"), 
-                                              _("Enter the passphrase for your new key twice."), 
-                                              NULL, NULL, TRUE);
-    if (gtk_dialog_run (dialog) == GTK_RESPONSE_ACCEPT)
-    {
-        pass = seahorse_passphrase_prompt_get (dialog);
-        op = seahorse_gpgme_key_op_generate (sksrc, name, email, comment,
-                                             pass, type, bits, expires, &gerr);
-    
-        if (!GPG_IS_OK (gerr)) {
-            seahorse_gpgme_handle_error (gerr, _("Couldn't generate key"));
-        } else {
-            seahorse_progress_show (op, _("Generating key"), TRUE);
-            seahorse_operation_watch (op, (SeahorseDoneFunc)completion_handler, NULL, NULL, NULL);
-            g_object_unref (op);
-        }
-    }
-        
-    gtk_widget_destroy (GTK_WIDGET (dialog));
+
+    seahorse_gpgme_generate_key (sksrc, name, email, comment, type, bits, expires);
+
+
     seahorse_widget_destroy (swidget);
     g_free (name);
 }
@@ -357,12 +388,15 @@ on_gpgme_generate_algorithm_changed (GtkComboBox *combo, SeahorseWidget *swidget
  * seahorse_gpgme_generate_show:
  * @sksrc: the gpgme source
  * @parent: the parent window
+ * @name: The user name, can be NULL if not available
+ * @email: The user's email address, can be NULL if not available
+ * @comment: The comment to add to the key. Can be NULL
  *
  * Shows the gpg key generation dialog, sets default entries.
  *
  */
 void
-seahorse_gpgme_generate_show (SeahorseGpgmeSource *sksrc, GtkWindow *parent)
+seahorse_gpgme_generate_show (SeahorseGpgmeSource *sksrc, GtkWindow *parent, const gchar * name, const gchar *email, const gchar *comment)
 {
     SeahorseWidget *swidget;
     GtkWidget *widget, *datetime;
@@ -374,6 +408,27 @@ seahorse_gpgme_generate_show (SeahorseGpgmeSource *sksrc, GtkWindow *parent)
     /* Widget already present */
     if (swidget == NULL)
         return;
+
+    if (name)
+    {
+        widget = seahorse_widget_get_widget (swidget, "name-entry");
+        g_return_if_fail (widget != NULL);
+        gtk_entry_set_text(GTK_ENTRY(widget),name);
+    }
+
+    if (email)
+    {
+        widget = seahorse_widget_get_widget (swidget, "email-entry");
+        g_return_if_fail (widget != NULL);
+        gtk_entry_set_text(GTK_ENTRY(widget),email);
+    }
+
+    if (comment)
+    {
+        widget = seahorse_widget_get_widget (swidget, "comment-entry");
+        g_return_if_fail (widget != NULL);
+        gtk_entry_set_text(GTK_ENTRY(widget),comment);
+    }
     
     widget = seahorse_widget_get_widget (swidget, "pgp-image");
     g_return_if_fail (widget != NULL);



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