2008-12-15 [colin] 3.6.1cvs78
[claws.git] / src / plugins / pgpcore / passphrase.c
index 93a3b776cd36395029b560c024ce9ca8f1d99098..5498487074a4b8589c59a1f5abdad11d39f6dcdd 100644 (file)
@@ -1,9 +1,9 @@
 /* passphrase.c - GTK+ based passphrase callback
- *      Copyright (C) 2001-2006 Werner Koch (dd9jn) and the Sylpheed-Claws team
+ *      Copyright (C) 2001-2007 Werner Koch (dd9jn) and the Claws Mail 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -12,8 +12,8 @@
  * GNU General Public License for more details.
  *
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 
 #include <glib.h>
 #include <glib/gi18n.h>
-#include <gdk/gdktypes.h>
+#include <gdk/gdk.h>
 #include <gdk/gdkkeysyms.h>
-#include <gdk/gdkdisplay.h>
 #ifdef GDK_WINDOWING_X11
 #  include <gdk/gdkx.h>
 #endif /* GDK_WINDOWING_X11 */
-#include <gtk/gtkmain.h>
-#include <gtk/gtkwidget.h>
-#include <gtk/gtkwindow.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtktable.h>
-#include <gtk/gtklabel.h>
-#include <gtk/gtkentry.h>
-#include <gtk/gtkhbbox.h>
-#include <gtk/gtkbutton.h>
-#include <gtk/gtkfilesel.h>
-#include <gtk/gtksignal.h>
+#include <gtk/gtk.h> 
 #include <string.h>
 #include <sys/types.h>
 #ifdef G_OS_WIN32
-#include <windows.h>
+#include <w32lib.h>
 #else
 #include <sys/mman.h>
 #endif
@@ -68,11 +57,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)
@@ -80,8 +67,8 @@ 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, *hbox;
@@ -95,12 +82,12 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
     
     gtk_menu_popdown(GTK_MENU(summaryview->popupmenu));
 
-    window = gtk_window_new(GTK_WINDOW_TOPLEVEL);
+    window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "passphrase");
     gtk_window_set_title(GTK_WINDOW(window), _("Passphrase"));
-    gtk_widget_set_size_request(window, 450, -1);
+    gtk_window_set_default_size(GTK_WINDOW(window), 375, 100);
+    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);
     g_signal_connect(G_OBJECT(window), "delete_event",
                      G_CALLBACK(passphrase_deleted), NULL);
     g_signal_connect(G_OBJECT(window), "key_press_event",
@@ -114,7 +101,7 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
 
     if (uid_hint || pass_hint) {
         GtkWidget *label, *icon;
-        label = create_description (uid_hint, pass_hint, prev_bad);
+        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); 
 
@@ -129,12 +116,17 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
     pass_entry = gtk_entry_new();
     gtk_box_pack_start(GTK_BOX(vbox), pass_entry, FALSE, FALSE, 0);
     gtk_entry_set_visibility(GTK_ENTRY(pass_entry), FALSE);
+#ifdef MAEMO
+    hildon_gtk_entry_set_input_mode(GTK_ENTRY(pass_entry), 
+       HILDON_GTK_INPUT_MODE_FULL | HILDON_GTK_INPUT_MODE_INVISIBLE);
+#endif
     gtk_widget_grab_focus(pass_entry);
 
     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);
 
@@ -147,7 +139,7 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
 
     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
     if (grab_all)   
-        gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, TRUE);
+        gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
     
     gtk_widget_show_all(window);
 
@@ -250,7 +242,7 @@ 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;
@@ -271,11 +263,23 @@ create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad)
     while (strchr(my_uid, '>')) 
        *(strchr(my_uid, '>')) = ')';
 
-    buf = g_strdup_printf (_("<span weight=\"bold\" size=\"larger\">%sPlease enter the passphrase for:</span>\n\n"
+    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);
@@ -289,12 +293,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_OS_WIN32
+#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;
@@ -304,35 +308,35 @@ gpgme_error_t
 gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
         const char *passphrase_hint, int prev_bad, int fd)
 {
-    const char *pass;
+    char *pass = NULL;
 
     if (prefs_gpg_get_config()->store_passphrase && last_pass && !prev_bad)
-        pass = last_pass;
+        pass = g_strdup(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);
-    gpgmegtk_free_passphrase();
-    if (!pass) {
-        debug_print ("%% cancel passphrase entry");
-        write(fd, "\n", 1);
-        return GPG_ERR_CANCELED;
-    }
-    else {
-        if (prefs_gpg_get_config()->store_passphrase) {
-            last_pass = g_strdup(pass);
-#ifndef G_OS_WIN32
-            if (mlock(last_pass, strlen(last_pass)) == -1)
-                debug_print("%% locking passphrase failed");
+       gpgmegtk_set_passphrase_grab (prefs_gpg_get_config()->passphrase_grab);
+       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\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\n");
 #endif
-            if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
-                    gtk_timeout_add(prefs_gpg_get_config()
-                                    ->store_passphrase_timeout*60*1000,
-                                free_passphrase, NULL);
+               if (prefs_gpg_get_config()->store_passphrase_timeout > 0) {
+                       g_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
@@ -346,6 +350,8 @@ gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
     write(fd, pass, strlen(pass));
     write(fd, "\n", 1);
 #endif
+    g_free(pass);
+
     return GPG_ERR_NO_ERROR;
 }