fix bug 4239, 'Preferences: Text Options Header Display modal is not modal' (sic)
[claws.git] / src / plugins / pgpcore / passphrase.c
index 387994dab41b274e96bd6f14b590019ceaa3656f..781728b1c262f8fb1d70386fae44405dd3af0047 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-2016 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,7 @@
  * 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
@@ -69,8 +57,7 @@ static gint passphrase_deleted(GtkWidget *widget, GdkEventAny *event,
 static gboolean passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event,
                                       gpointer data);
 
-static GtkWidget *create_description(const gchar *uid_hint,
-                                    const gchar *pass_hint, gint prev_bad, gint new_key);
+static GtkWidget *create_description(const gchar *uid_hint, gint prev_bad, gint new_key);
 
 void
 gpgmegtk_set_passphrase_grab(gint yes)
@@ -88,18 +75,19 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gi
     GtkWidget *pass_entry;
     GtkWidget *ok_button;
     GtkWidget *cancel_button;
+    GdkWindow *gdkwin;
 
     SummaryView *summaryview = mainwindow_get_mainwindow()->summaryview;
     
     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_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_type_hint(GTK_WINDOW(window), GDK_WINDOW_TYPE_HINT_DIALOG);
     gtk_window_set_modal(GTK_WINDOW(window), TRUE);
-    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",
@@ -113,7 +101,7 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gi
 
     if (uid_hint || pass_hint) {
         GtkWidget *label, *icon;
-        label = create_description (uid_hint, pass_hint, prev_bad, new_key);
+        label = create_description (uid_hint, prev_bad, new_key);
        icon = gtk_image_new_from_stock(GTK_STOCK_DIALOG_AUTHENTICATION,
                                GTK_ICON_SIZE_DIALOG); 
 
@@ -147,7 +135,7 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gi
 
     gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
     if (grab_all)   
-        gtk_window_set_policy (GTK_WINDOW(window), FALSE, FALSE, FALSE);
+        gtk_window_set_resizable(GTK_WINDOW(window), FALSE);
     
     gtk_widget_show_all(window);
 
@@ -155,29 +143,30 @@ passphrase_mbox(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gi
         int err = 0, cnt = 0;
         /* make sure that window is viewable */
         gtk_widget_show_now(window);
-       gdk_window_process_updates(window->window, TRUE);
+       gdkwin = gtk_widget_get_window(window);
+       gdk_window_process_updates(gdkwin, TRUE);
        gdk_flush();
        while(gtk_events_pending()) {
                gtk_main_iteration();
        }
 try_again:
-        if ((err = gdk_pointer_grab(window->window, TRUE, 0,
-                             window->window, NULL, GDK_CURRENT_TIME))) {
+        if ((err = gdk_pointer_grab(gdkwin, TRUE, 0,
+                             gdkwin, NULL, GDK_CURRENT_TIME))) {
            if (err == GDK_GRAB_NOT_VIEWABLE && cnt < 10) {
                cnt++;
-               g_warning("trying to grab mouse again\n");
+               g_warning("trying to grab mouse again");
                gtk_main_iteration();
                goto try_again;
             } else {
-                g_warning("OOPS: Could not grab mouse\n");
+                g_warning("OOPS: Could not grab mouse");
                 gtk_widget_destroy(window);
                 return NULL;
            }
         }
-        if (gdk_keyboard_grab(window->window, FALSE, GDK_CURRENT_TIME)) {
+        if (gdk_keyboard_grab(gdkwin, FALSE, GDK_CURRENT_TIME)) {
             gdk_display_pointer_ungrab(gdk_display_get_default(),
                                       GDK_CURRENT_TIME);
-            g_warning("OOPS: Could not grab keyboard\n");
+            g_warning("OOPS: Could not grab keyboard");
             gtk_widget_destroy(window);
             return NULL;
         }
@@ -233,7 +222,7 @@ passphrase_deleted(GtkWidget *widget, GdkEventAny *event, gpointer data)
 static gboolean
 passphrase_key_pressed(GtkWidget *widget, GdkEventKey *event, gpointer data)
 {
-    if (event && event->keyval == GDK_Escape)
+    if (event && event->keyval == GDK_KEY_Escape)
         passphrase_cancel_cb(NULL, NULL);
     return FALSE;
 }
@@ -250,9 +239,9 @@ linelen (const gchar *s)
 }
 
 static GtkWidget *
-create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad, gint new_key)
+create_description(const gchar *uid_hint, gint prev_bad, gint new_key)
 {
-    const gchar *uid = NULL, *info = NULL;
+    const gchar *uid = NULL;
     gchar *buf;
     GtkWidget *label;
     gchar *my_uid = NULL;
@@ -260,10 +249,6 @@ create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad,
         uid = _("[no user id]");
     else
         uid = uid_hint;
-    if (!pass_hint)
-        info = "";
-    else
-        info = pass_hint;
 
     my_uid = g_strdup(uid);
     while (strchr(my_uid, '<')) 
@@ -272,18 +257,21 @@ create_description(const gchar *uid_hint, const gchar *pass_hint, gint prev_bad,
        *(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"),
+           buf = g_strdup_printf (g_strconcat("<span weight=\"bold\" size=\"larger\">%s",
+                                       _("Please enter the passphrase for the new key:"),
+                                       "</span>\n\n%.*s\n", NULL),
                            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"),
+           buf = g_strdup_printf (g_strconcat("<span weight=\"bold\" size=\"larger\">",
+                               _("Please re-enter the passphrase for the new key:"),
+                               "</span>\n\n%.*s\n", NULL),
                            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"),
+           buf = g_strdup_printf (g_strconcat("<span weight=\"bold\" size=\"larger\">%s",
+                               _("Please enter the passphrase for:"),
+                               "</span>\n\n%.*s\n", NULL),
                            prev_bad ?
                            _("Bad passphrase.\n") : "",
                            linelen (my_uid), my_uid);
@@ -316,35 +304,37 @@ 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;
-    else {
-    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;
-    }
+        pass = g_strdup(last_pass);
     else {
-        if (prefs_gpg_get_config()->store_passphrase) {
-            last_pass = g_strdup(pass);
+       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");
+            if (write(fd, "\n", 1) != 1)
+                               debug_print("short write\n");
+
+            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");
+               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\n");
-    }
+            debug_print ("%% sending passphrase\n");
+       }
     }
 
 #ifdef G_OS_WIN32
@@ -355,9 +345,14 @@ gpgmegtk_passphrase_cb(void *opaque, const char *uid_hint,
         WriteFile ((HANDLE)fd, "\n", 1, &nwritten, NULL);
     }
 #else
-    write(fd, pass, strlen(pass));
-    write(fd, "\n", 1);
+    if (write(fd, pass, strlen(pass)) != strlen(pass))
+               debug_print("short write\n");
+
+    if (write(fd, "\n", 1) != 1)
+               debug_print("short write\n");
 #endif
+    g_free(pass);
+
     return GPG_ERR_NO_ERROR;
 }