2008-12-10 [colin] 3.6.1cvs68
[claws.git] / src / plugins / pgpcore / pgp_viewer.c
index 6473e58857b8860a3c3de0626520779047c23503..9563022df4498e742f37362cfe1049d3a1a2b174 100644 (file)
@@ -1,11 +1,11 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2007 the Claws Mail team
  * This file Copyright (C) 2006 Colin Leroy <colin@colino.net>
  *
  * 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,
@@ -14,8 +14,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 <sys/types.h>
-#include <sys/wait.h>
+#ifndef G_OS_WIN32
+#  include <sys/wait.h>
+#endif
+#if (defined(__DragonFly__) || defined(SOLARIS) || defined (__NetBSD__) || defined (__FreeBSD__) || defined (__OpenBSD__))
+#  include <sys/signal.h>
+#endif
 
 #include "version.h"
-#include "common/sylpheed.h"
+#include "common/claws.h"
 #include "mainwindow.h"
 #include "mimeview.h"
 #include "textview.h"
 #include "sgpgme.h"
 #include "prefs_common.h"
 #include "prefs_gpg.h"
+#include "alertpanel.h"
 #include "plugin.h"
 
 typedef struct _PgpViewer PgpViewer;
@@ -107,33 +113,49 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
                return;
        }
        gpgme_get_key(ctx, sig->fpr, &key, 0);
-       TEXTVIEW_INSERT(_("\n  Importing key ID "));
-       TEXTVIEW_INSERT(sig->fpr);
-       TEXTVIEW_INSERT(":\n\n");
        if (!key) {
-               if (prefs_common.work_offline) {
-                       gchar *cmd = g_strdup_printf("gpg --recv-keys %s", sig->fpr);
+               gchar *cmd = g_strdup_printf("gpg --no-tty --recv-keys %s", sig->fpr);
+               AlertValue val = G_ALERTDEFAULT;
+               if (!prefs_common_get_prefs()->work_offline) {
+                       val = alertpanel(_("Key import"),
+                               _("This key is not in your keyring. Do you want "
+                                 "Claws Mail to try and import it from a "
+                                 "keyserver?"),
+                                 GTK_STOCK_NO, "+" GTK_STOCK_YES, NULL);
+                       GTK_EVENTS_FLUSH();
+               }
+               if (val == G_ALERTDEFAULT) {
+                       TEXTVIEW_INSERT(_("\n  Key ID "));
+                       TEXTVIEW_INSERT(sig->fpr);
+                       TEXTVIEW_INSERT(":\n\n");
                        TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
-                       TEXTVIEW_INSERT(_("   It should be possible to import it when working online,\n"));
-                       TEXTVIEW_INSERT(_("   or with the following command: \n\n     "));
+                       TEXTVIEW_INSERT(_("   It should be possible to import it "));
+                       if (prefs_common_get_prefs()->work_offline)
+                               TEXTVIEW_INSERT(_("when working online,\n   or "));
+                       TEXTVIEW_INSERT(_("with the following command: \n\n     "));
                        TEXTVIEW_INSERT(cmd);
-                       g_free(cmd);
                } else {
 #ifndef G_OS_WIN32
-                       gchar *cmd = g_strdup_printf("gpg --recv-keys %s", sig->fpr);
                        int res = 0;
                        pid_t pid = 0;
+       
+                       TEXTVIEW_INSERT(_("\n  Importing key ID "));
+                       TEXTVIEW_INSERT(sig->fpr);
+                       TEXTVIEW_INSERT(":\n\n");
 
                        main_window_cursor_wait(mainwindow_get_mainwindow());
+                       textview_cursor_wait(textview);
                        GTK_EVENTS_FLUSH();
-                       
+
                        pid = fork();
                        if (pid == -1) {
                                res = -1;
                        } else if (pid == 0) {
                                /* son */
-                               res = system(cmd);
-                               _exit(res);
+                               gchar **argv;
+                               argv = strsplit_with_quote(cmd, " ", 0);
+                               res = execvp(argv[0], argv);
+                               exit(255);
                        } else {
                                int status = 0;
                                time_t start_wait = time(NULL);
@@ -145,35 +167,40 @@ static void pgpview_show_mime_part(TextView *textview, MimeInfo *partinfo)
                                                res = WEXITSTATUS(status);
                                                break;
                                        }
-                                       if (time(NULL) - start_wait > 5) {
-                                               debug_print("SIGTERM'ing gpg\n");
+                                       if (time(NULL) - start_wait > 9) {
+                                               debug_print("SIGTERM'ing gpg %d\n", pid);
                                                kill(pid, SIGTERM);
                                        }
-                                       if (time(NULL) - start_wait > 6) {
-                                               debug_print("SIGKILL'ing gpg\n");
+                                       if (time(NULL) - start_wait > 10) {
+                                               debug_print("SIGKILL'ing gpg %d\n", pid);
                                                kill(pid, SIGKILL);
                                                break;
                                        }
                                } while(1);
                        }
                        main_window_cursor_normal(mainwindow_get_mainwindow());
+                       textview_cursor_normal(textview);
+                       debug_print("res %d\n", res);
                        if (res == 0) {
                                TEXTVIEW_INSERT(_("   This key has been imported to your keyring.\n"));
                        } else {
                                TEXTVIEW_INSERT(_("   This key couldn't be imported to your keyring.\n"));
+                               TEXTVIEW_INSERT(_("   Key servers are sometimes slow.\n"));
                                TEXTVIEW_INSERT(_("   You can try to import it manually with the command:\n\n     "));
                                TEXTVIEW_INSERT(cmd);
                        }
-                       g_free(cmd);
 #else
                        TEXTVIEW_INSERT(_("   This key is not in your keyring.\n"));
                        TEXTVIEW_INSERT(_("   Key import isn't implemented in Windows.\n"));
 #endif
                }
+               g_free(cmd);
                return;
        } else {
-               TEXTVIEW_INSERT(_("   This key is already in your keyring.\n"));
-
+               TEXTVIEW_INSERT(_("\n  Key ID "));
+               TEXTVIEW_INSERT(sig->fpr);
+               TEXTVIEW_INSERT(":\n\n");
+               TEXTVIEW_INSERT(_("   This key is in your keyring.\n"));
        }
        gpgme_data_release(sigdata);
        gpgme_release(ctx);