2006-08-14 [cleroy] 2.4.0cvs50
[claws.git] / src / plugins / pgpcore / passphrase.c
index a717f1befcd56f9f800e51c9c4704a4b42ed8d8a..12b4d968858dbf0fddac2d195185daff0f31bd0f 100644 (file)
@@ -1,5 +1,5 @@
 /* passphrase.c - GTK+ based passphrase callback
- *      Copyright (C) 2001 Werner Koch (dd9jn)
+ *      Copyright (C) 2001-2006 Werner Koch (dd9jn) and the Sylpheed-Claws team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -13,7 +13,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <gtk/gtksignal.h>
 #include <string.h>
 #include <sys/types.h>
+#ifdef G_OS_WIN32
+#include <windows.h>
+#else
 #include <sys/mman.h>
+#endif
 
 #include "passphrase.h"
 #include "prefs_common.h"
 #include "prefs_gpg.h"
 #include "manage_window.h"
 #include "utils.h"
+#include "mainwindow.h"
+#include "summaryview.h"
 
 static gboolean grab_all = FALSE;
 
@@ -62,11 +68,9 @@ static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
                               gpointer data);
 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                       gpointer data);
-static gchar* passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint,
-                             gint prev_bad);
 
 static GtkWidget *create_description(const gchar *uid_hint,
-                                    const gchar *pass_hint, gint prev_bad);
+                                    const gchar *pass_hint, gint prev_bad, gint new_key);
 
 void
 gpgmegtk_set_passphrase_grab(gint yes)
@@ -74,23 +78,27 @@ gpgmegtk_set_passphrase_grab(gint yes)
     grab_all = yes;
 }
 
-static gchar*
-passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
+gchar*
+passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gint new_key)
 {
     gchar *the_passphrase = NULL;
-    GtkWidget *vbox;
+    GtkWidget *vbox, *hbox;
     GtkWidget *confirm_box;
     GtkWidget *window;
     GtkWidget *pass_entry;
     GtkWidget *ok_button;
     GtkWidget *cancel_button;
 
+    SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
+    
+    gtk_menu_popdown(GTK_MENU(summaryview->popupmenu));
+
     window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
-    gtk_widget_set_size_request(window, 450, -1);
+    gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
     gtk_window_set_position(GTK_WINDOW(window), GTK_WIN_POS_CENTER);
     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
-    gtk_window_set_policy(GTK_WINDOW(window), FALSE, FALSE, FALSE);
+    gtk_window_set_policy(GTK_WINDOW(window), FALSE, TRUE, FALSE);
     g_signal_connect(G_OBJECT(window), "delete_event",
                      G_CALLBACK(passphrase_deleted), NULL);
     g_signal_connect(G_OBJECT(window), "key_press_event",
@@ -103,9 +111,17 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
     gtk_container_set_border_width(GTK_CONTAINER(vbox), 8);
 
     if (uid_hint || pass_hint) {
-        GtkWidget *label;
-        label = create_description (uid_hint, pass_hint, prev_bad);
-        gtk_box_pack_start (GTK_BOX(vbox), label, FALSE, FALSE, 0);
+        GtkWidget *label, *icon;
+        label = create_description (uid_hint, pass_hint, prev_bad, new_key);
+       icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
+                               GTK_ICON_SIZE_DIALOG); 
+
+       hbox = gtk_hbox_new (FALSE, 12);
+       gtk_container_set_border_width (GTK_CONTAINER (hbox), 5);
+       gtk_widget_show (hbox);
+        gtk_box_pack_start (GTK_BOX(hbox), icon, FALSE, FALSE, 0);
+        gtk_box_pack_start (GTK_BOX(hbox), label, FALSE, FALSE, 0);
+        gtk_box_pack_start (GTK_BOX(vbox), hbox, FALSE, FALSE, 0);
     }
 
     pass_entry = gtk_entry_new();
@@ -113,8 +129,9 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
     gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
     gtk_widget_grab_focus(pass_entry);
 
-    gtkut_stock_button_set_create(&confirm_box, &ok_button, GTK_STOCK_OK,
+    gtkut_stock_button_set_create(&confirm_box, 
                                  &cancel_button, GTK_STOCK_CANCEL,
+                                 &ok_button, GTK_STOCK_OK,
                                  NULL, NULL);
     gtk_box_pack_end(GTK_BOX(vbox), confirm_box, FALSE, FALSE, 0);
     gtk_widget_grab_default(ok_button);
@@ -136,23 +153,20 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
         int err = 0, cnt = 0;
         /* make sure that window is viewable */
         gtk_widget_show_now(window);
+       gdk_window_process_updates(window->window, TRUE);
        gdk_flush();
-       while(gtk_events_pending())
+       while(gtk_events_pending()) {
                gtk_main_iteration();
-#ifdef GDK_WINDOWING_X11
-       gdk_x11_display_grab(gdk_display_get_default());
-#endif /* GDK_WINDOWING_X11 */
+       }
 try_again:
         if ((err = gdk_pointer_grab(window->window, TRUE, 0,
                              window->window, NULL, GDK_CURRENT_TIME))) {
            if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
                cnt++;
                g_warning("trying to grab mouse again\n");
+               gtk_main_iteration();
                goto try_again;
             } else {
-#ifdef GDK_WINDOWING_X11
-                gdk_x11_display_ungrab(gdk_display_get_default());
-#endif /* GDK_WINDOWING_X11 */
                 g_warning("OOPS: Could not grab mouse\n");
                 gtk_widget_destroy(window);
                 return NULL;
@@ -161,9 +175,6 @@ try_again:
         if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
             gdk_display_pointer_ungrab(gdk_display_get_default(),
                                       GDK_CURRENT_TIME);
-#ifdef GDK_WINDOWING_X11
-            gdk_x11_display_ungrab(gdk_display_get_default());
-#endif /* GDK_WINDOWING_X11 */
             g_warning("OOPS: Could not grab keyboard\n");
             gtk_widget_destroy(window);
             return NULL;
@@ -176,9 +187,6 @@ try_again:
         gdk_display_keyboard_ungrab(gdk_display_get_default(),
                                    GDK_CURRENT_TIME);
         gdk_display_pointer_ungrab(gdk_display_get_default(), GDK_CURRENT_TIME);
-#ifdef GDK_WINDOWING_X11
-        gdk_x11_display_ungrab(gdk_display_get_default());
-#endif /* GDK_WINDOWING_X11 */
         gdk_flush();
     }
 
@@ -187,7 +195,8 @@ try_again:
     if (pass_ack) {
         const gchar *entry_text;
         entry_text = gtk_entry_get_text(GTK_ENTRY(pass_entry));
-        if (entry_text) /* Hmmm: Do we really need this? */
+       the_passphrase = g_locale_from_utf8(entry_text, -1, NULL, NULL, NULL);
+        if (the_passphrase == NULL) 
             the_passphrase = g_strdup (entry_text);
     }
     gtk_widget_destroy (window);
@@ -239,12 +248,12 @@ linelen (const gchar *s)
 }
 
 static GtkWidget *
-create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
+create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gint new_key)
 {
     const gchar *uid = NULL, *info = NULL;
     gchar *buf;
     GtkWidget *label;
-
+    gchar *my_uid = NULL;
     if (!uid_hint)
         uid = _("[no user id]");
     else
@@ -254,15 +263,34 @@ create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
     else
         info = pass_hint;
 
-    buf = g_strdup_printf (_("%sPlease enter the passphrase for:\n\n"
-                           "  %.*s  \n"
-                           "(%.*s)\n"),
-                           prev_bad ?
-                           _("Bad passphrase! Try again...\n\n") : "",
-                           linelen (uid), uid, linelen (info), info);
+    my_uid = g_strdup(uid);
+    while (strchr(my_uid, '<')) 
+       *(strchr(my_uid, '<')) = '(';
+    while (strchr(my_uid, '>')) 
+       *(strchr(my_uid, '>')) = ')';
 
+    if (new_key == 1) {
+           buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for the new key:</span>\n\n"
+                           "%.*s\n"),
+                           prev_bad ?
+                           _("Passphrases did not match.\n") : "",
+                           linelen (my_uid), my_uid);
+    } else if (new_key == 2) {
+           buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">Please re-enter the passphrase for the new key:</span>\n\n"
+                           "%.*s\n"),
+                           linelen (my_uid), my_uid);
+    } else {
+           buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for:</span>\n\n"
+                           "%.*s\n"),
+                           prev_bad ?
+                           _("Bad passphrase.\n") : "",
+                           linelen (my_uid), my_uid);
+    }
+    g_free(my_uid);
     label = gtk_label_new (buf);
+    gtk_label_set_use_markup(GTK_LABEL (label), TRUE);
     gtk_label_set_justify (GTK_LABEL (label), GTK_JUSTIFY_LEFT);
+    gtk_label_set_line_wrap(GTK_LABEL (label), TRUE);
     g_free (buf);
 
     return label;
@@ -271,10 +299,12 @@ create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
 static int free_passphrase(gpointer _unused)
 {
     if (last_pass != NULL) {
+#ifndef G_PLATFORM_WIN32
         munlock(last_pass, strlen(last_pass));
+#endif
         g_free(last_pass);
         last_pass = NULL;
-        debug_print("%% passphrase removed");
+        debug_print("%% passphrase removed\n");
     }
     
     return FALSE;
@@ -286,41 +316,52 @@ gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
 {
     const char *pass;
 
-    if (prefs_gpg_get_config()->store_passphrase && last_pass != NULL && !prev_bad) {
-        write(fd, last_pass, strlen(last_pass));
-        write(fd, "\n", 1);
-        return GPG_ERR_NO_ERROR;
-    }
+    if (prefs_gpg_get_config()->store_passphrase && last_pass && !prev_bad)
+        pass = last_pass;
+    else {
     gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
-    debug_print ("%% requesting passphrase for `%s': ", uid_hint);
-    pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad);
+    debug_print ("%% requesting passphrase for '%s'\n ", uid_hint);
+    pass = passphrase_mbox (uid_hint, passphrase_hint, prev_bad, FALSE);
     gpgmegtk_free_passphrase();
     if (!pass) {
-        debug_print ("%% cancel passphrase entry");
+        debug_print ("%% cancel passphrase entry\n");
         write(fd, "\n", 1);
         return GPG_ERR_CANCELED;
     }
     else {
         if (prefs_gpg_get_config()->store_passphrase) {
             last_pass = g_strdup(pass);
+#ifndef G_PLATFORM_WIN32
             if (mlock(last_pass, strlen(last_pass)) == -1)
-                debug_print("%% locking passphrase failed");
-
+                debug_print("%% locking passphrase failed\n");
+#endif
             if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
-                gtk_timeout_add(prefs_gpg_get_config()->store_passphrase_timeout*60*1000,
+                    gtk_timeout_add(prefs_gpg_get_config()
+                                    ->store_passphrase_timeout*60*1000,
                                 free_passphrase, NULL);
             }
         }
-        debug_print ("%% sending passphrase");
+        debug_print ("%% sending passphrase\n");
+    }
     }
+
+#ifdef G_OS_WIN32
+    {
+        /* Under Windows FD is actually a System handle. */
+        DWORD nwritten;
+        WriteFile ((HANDLE)fd, pass, strlen (pass), &nwritten, NULL);
+        WriteFile ((HANDLE)fd, "\n", 1, &nwritten, NULL);
+    }
+#else
     write(fd, pass, strlen(pass));
     write(fd, "\n", 1);
+#endif
     return GPG_ERR_NO_ERROR;
 }
 
 void gpgmegtk_free_passphrase()
 {
-    (void)free_passphrase(NULL); // could be inline
+    (void)free_passphrase(NULL); /* could be inline */
 }
 
 #endif /* USE_GPGME */