2011-10-07 [colin] 3.7.10cvs21
[claws.git] / src / plugins / pgpcore / prefs_gpg.c
index 29a7d247bc076b1fb63dab8505335ef569c20441..5c149ff00970918b236c21801505e0b60f14b48c 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2004 Hiroyuki Yamamoto & the Sylpheed-Claws team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 2004-2011 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,
@@ -13,8 +13,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/>.
+ * 
  */
 
 #include <gtk/gtk.h>
@@ -22,7 +22,8 @@
 #include <glib/gi18n.h>
 
 #include "defs.h"
-#include "utils.h"
+#include "gtk/gtkutils.h"
+#include "utils.h" 
 #include "prefs.h"
 #include "prefs_gtk.h"
 #include "prefs_gpg.h"
@@ -35,6 +36,8 @@ static PrefParam param[] = {
        {"auto_check_signatures", "FALSE",
         &prefs_gpg.auto_check_signatures, P_BOOL,
         NULL, NULL, NULL},
+       {"use_gpg_agent", "TRUE", &prefs_gpg.use_gpg_agent, P_BOOL,
+        NULL, NULL, NULL},
        {"store_passphrase", "FALSE", &prefs_gpg.store_passphrase, P_BOOL,
         NULL, NULL, NULL},
        {"store_passphrase_timeout", "0",
@@ -46,15 +49,20 @@ static PrefParam param[] = {
         NULL, NULL, NULL},
        {"gpg_ask_create_key", "TRUE", &prefs_gpg.gpg_ask_create_key, P_BOOL,
         NULL, NULL, NULL},
+       {"skip_encryption_warning", "", &prefs_gpg.skip_encryption_warning, P_STRING,
+        NULL, NULL, NULL},
 
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
 };
 
+static gchar *saved_gpg_agent_info = NULL;
+
 struct GPGPage
 {
        PrefsPage page;
 
        GtkWidget *checkbtn_auto_check_signatures;
+       GtkWidget *checkbtn_use_gpg_agent;
         GtkWidget *checkbtn_store_passphrase;  
         GtkWidget *spinbtn_store_passphrase;  
         GtkWidget *checkbtn_passphrase_grab;  
@@ -68,6 +76,7 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        struct GPGPage *page = (struct GPGPage *) _page;
        struct GPGConfig *config;
 
+       GtkWidget *checkbtn_use_gpg_agent;
        GtkWidget *checkbtn_passphrase_grab;
        GtkWidget *checkbtn_store_passphrase;
        GtkWidget *checkbtn_auto_check_signatures;
@@ -75,13 +84,11 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        GtkWidget *hbox1;
        GtkWidget *vbox1, *vbox2;
        GtkWidget *label_expire1;
-       GtkObject *spinbtn_store_passphrase_adj;
+       GtkAdjustment *spinbtn_store_passphrase_adj;
        GtkWidget *spinbtn_store_passphrase;
        GtkWidget *label_expire2;
        GtkWidget *frame_passphrase;
-       GtkTooltips *tooltips;
-
-       tooltips = gtk_tooltips_new();
+       CLAWS_TIP_DECL();
 
        vbox1 = gtk_vbox_new (FALSE, VSPACING);
        gtk_widget_show (vbox1);
@@ -94,26 +101,30 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        PACK_CHECK_BUTTON (vbox2, checkbtn_auto_check_signatures,
                        _("Automatically check signatures"));
 
-       PACK_FRAME(vbox1, frame_passphrase, _("Passphrase"));
+       vbox2 = gtkut_get_options_frame(vbox1, &frame_passphrase, _("Passphrase"));
 
-       vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
-       gtk_widget_show (vbox2);
-       gtk_container_add (GTK_CONTAINER (frame_passphrase), vbox2);
-       gtk_container_set_border_width (GTK_CONTAINER (vbox2), 8);
+       PACK_CHECK_BUTTON (vbox2, checkbtn_use_gpg_agent,
+                       _("Use gpg-agent to manage passwords"));
+       if (saved_gpg_agent_info == NULL)
+               gtk_widget_set_sensitive(checkbtn_use_gpg_agent, FALSE);
 
        PACK_CHECK_BUTTON (vbox2, checkbtn_store_passphrase,
                        _("Store passphrase in memory"));
 
+       SET_TOGGLE_SENSITIVITY_REVERSE(checkbtn_use_gpg_agent, checkbtn_store_passphrase);
+
        hbox1 = gtk_hbox_new (FALSE, 8);
        gtk_widget_show (hbox1);
        gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
 
+       SET_TOGGLE_SENSITIVITY_REVERSE(checkbtn_use_gpg_agent, hbox1);
+
        label_expire1 = gtk_label_new(_("Expire after"));
        gtk_widget_show (label_expire1);
        gtk_box_pack_start (GTK_BOX (hbox1), label_expire1, FALSE, FALSE, 0);
 
        spinbtn_store_passphrase_adj =
-           gtk_adjustment_new(1, 0, 1440, 1, 10, 10);
+           gtk_adjustment_new(1, 0, 1440, 1, 10, 0);
        spinbtn_store_passphrase =
            gtk_spin_button_new(GTK_ADJUSTMENT
                                (spinbtn_store_passphrase_adj), 1, 0);
@@ -121,10 +132,9 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        gtk_box_pack_start(GTK_BOX(hbox1), spinbtn_store_passphrase, FALSE,
                           FALSE, 0);
        gtk_widget_set_size_request(spinbtn_store_passphrase, 64, -1);
-       gtk_tooltips_set_tip(tooltips, spinbtn_store_passphrase,
+       CLAWS_SET_TIP(spinbtn_store_passphrase,
                             _
-                            ("Setting to '0' will store the passphrase for the whole session"),
-                            NULL);
+                            ("Setting to '0' will store the passphrase for the whole session"));
        gtk_spin_button_set_numeric(GTK_SPIN_BUTTON
                                    (spinbtn_store_passphrase), TRUE);
 
@@ -145,12 +155,17 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
 
        PACK_CHECK_BUTTON (vbox2, checkbtn_gpg_warning,
-                       _("Display warning on startup if GnuPG doesn't work"));
+                       _("Display warning on start-up if GnuPG doesn't work"));
 
        config = prefs_gpg_get_config();
 
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_auto_check_signatures), config->auto_check_signatures);
-       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_store_passphrase), config->store_passphrase);
+       if (!getenv("GPG_AGENT_INFO"))
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_use_gpg_agent), FALSE);
+       else
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_use_gpg_agent), config->use_gpg_agent);
+       if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbtn_use_gpg_agent)))
+               gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_store_passphrase), config->store_passphrase);
        gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_store_passphrase), (float) config->store_passphrase_timeout);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_passphrase_grab), config->passphrase_grab);
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_gpg_warning), config->gpg_warning);
@@ -160,7 +175,7 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        page->spinbtn_store_passphrase = spinbtn_store_passphrase;
        page->checkbtn_passphrase_grab = checkbtn_passphrase_grab;
        page->checkbtn_gpg_warning = checkbtn_gpg_warning;
-
+       page->checkbtn_use_gpg_agent = checkbtn_use_gpg_agent;
        page->page.widget = vbox1;
 }
 
@@ -175,6 +190,8 @@ static void prefs_gpg_save_func(PrefsPage *_page)
 
        config->auto_check_signatures =
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_auto_check_signatures));
+       config->use_gpg_agent = 
+               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_use_gpg_agent));
        config->store_passphrase = 
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_store_passphrase));
        config->store_passphrase_timeout = 
@@ -184,6 +201,8 @@ static void prefs_gpg_save_func(PrefsPage *_page)
        config->gpg_warning = 
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_gpg_warning));
 
+       prefs_gpg_enable_agent(config->use_gpg_agent);
+
        prefs_gpg_save_config();
 }
 
@@ -270,12 +289,7 @@ static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
        gtk_container_set_border_width (GTK_CONTAINER (vbox), VBOX_BORDER);
        gtk_widget_show(vbox);
 
-       PACK_FRAME (vbox, frame1, _("Sign key"));
-
-       vbox2 = gtk_vbox_new (FALSE, VSPACING_NARROW);
-       gtk_container_set_border_width (GTK_CONTAINER (vbox2), VBOX_BORDER);
-       gtk_widget_show (vbox2);
-       gtk_container_add (GTK_CONTAINER (frame1), vbox2);
+       vbox2 = gtkut_get_options_frame(vbox, &frame1, _("Sign key"));
 
        hbox = gtk_hbox_new (FALSE, 5);
        gtk_widget_show (hbox);
@@ -284,7 +298,7 @@ static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
 
        key_default = gtk_radio_button_new_with_label(key_group,
                        _("Use default GnuPG key"));
-       key_group = gtk_radio_button_group(GTK_RADIO_BUTTON(key_default));
+       key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_default));
        gtk_widget_show(key_default);
        gtk_box_pack_start(GTK_BOX(hbox), key_default, FALSE, FALSE, 0);
 
@@ -295,7 +309,7 @@ static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
 
        key_by_from = gtk_radio_button_new_with_label(key_group,
                _("Select key by your email address"));
-       key_group = gtk_radio_button_group(GTK_RADIO_BUTTON(key_by_from));
+       key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_by_from));
        gtk_widget_show(key_by_from);
        gtk_box_pack_start(GTK_BOX(hbox), key_by_from, FALSE, FALSE, 0);
 
@@ -306,7 +320,7 @@ static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
 
        key_custom = gtk_radio_button_new_with_label(key_group,
                _("Specify key manually"));
-       key_group = gtk_radio_button_group(GTK_RADIO_BUTTON(key_custom));
+       key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_custom));
        gtk_widget_show(key_custom);
        gtk_box_pack_start(GTK_BOX(hbox), key_custom, FALSE, FALSE, 0);
 
@@ -343,8 +357,14 @@ static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
                break;
        }
 
+       hbox = gtk_hbox_new (FALSE, 5);
+       gtk_widget_show (hbox);
+       gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
+
        new_key_box = gtk_hbox_new(FALSE, 6);
-       
+       gtk_widget_show(new_key_box);
+       gtk_box_pack_start(GTK_BOX(hbox), new_key_box, FALSE, FALSE, 0);
+
        image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING,
                        GTK_ICON_SIZE_SMALL_TOOLBAR);
 
@@ -353,17 +373,9 @@ static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
                        _("No secret key found."));
        gtk_box_pack_start(GTK_BOX(new_key_box), new_key_label, FALSE, FALSE, 0);
 
-       new_key_btn = gtk_button_new_with_label(_("Generate new secret key"));
-
-       gtk_box_pack_start(GTK_BOX(vbox2), new_key_box, FALSE, FALSE, 0);
-
-       hbox = gtk_hbox_new (FALSE, 5);
-       gtk_widget_show (hbox);
-       gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox), new_key_btn, FALSE, FALSE, 0);
-
-       gtk_widget_show_all(new_key_box);
+       new_key_btn = gtk_button_new_with_label(_("Generate a new key pair"));
        gtk_widget_show(new_key_btn);
+       gtk_box_pack_start(GTK_BOX(hbox), new_key_btn, FALSE, FALSE, 0);
 
        if (config->sign_key_id != NULL)
                gtk_entry_set_text(GTK_ENTRY(keyid), config->sign_key_id);
@@ -432,9 +444,11 @@ void prefs_gpg_save_config(void)
                prefs_file_close_revert(pfile);
                return;
        }
-       fprintf(pfile->fp, "\n");
-
-       prefs_file_close(pfile);
+        if (fprintf(pfile->fp, "\n") < 0) {
+               FILE_OP_ERROR(rcpath, "fprintf");
+               prefs_file_close_revert(pfile);
+       } else
+               prefs_file_close(pfile);
 }
 
 struct GPGAccountConfig *prefs_gpg_account_get_config(PrefsAccount *account)
@@ -503,10 +517,33 @@ void prefs_gpg_account_free_config(GPGAccountConfig *config)
 static struct GPGPage gpg_page;
 static struct GPGAccountPage gpg_account_page;
 
+void prefs_gpg_enable_agent(gboolean enable)
+{
+       if (enable) {
+               if (saved_gpg_agent_info) {
+                       g_setenv("GPG_AGENT_INFO",
+                                saved_gpg_agent_info, TRUE);
+                       debug_print("set GPG_AGENT_INFO=%s\n", 
+                               saved_gpg_agent_info);
+               } else { 
+                       debug_print("Can't enable gpg agent (no GPG_AGENT_INFO)\n");
+               }
+       } else {
+               if (saved_gpg_agent_info) {
+                       g_unsetenv("GPG_AGENT_INFO");
+                       debug_print("unset GPG_AGENT_INFO=%s\n", 
+                               saved_gpg_agent_info);
+               } else {
+                       debug_print("Can't disable gpg agent (no GPG_AGENT_INFO)\n");
+               }
+       }
+}
+
 void prefs_gpg_init()
 {
        static gchar *path[3];
        gchar *rcpath;
+       const gchar *tmp = NULL;
 
        prefs_set_default(param);
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
@@ -532,10 +569,80 @@ void prefs_gpg_init()
         gpg_account_page.page.weight = 30.0;
 
         prefs_account_register_page((PrefsPage *) &gpg_account_page);
+       
+       tmp = g_getenv("GPG_AGENT_INFO");
+       if (tmp)
+               saved_gpg_agent_info = g_strdup(tmp);
+
+       prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
 }
 
 void prefs_gpg_done()
 {
        prefs_gtk_unregister_page((PrefsPage *) &gpg_page);
        prefs_account_unregister_page((PrefsPage *) &gpg_account_page);
+       prefs_gpg_enable_agent(TRUE);
+}
+
+gboolean prefs_gpg_should_skip_encryption_warning(const gchar *systemid)
+{
+       gchar **systems = NULL;
+       int i = 0;
+       if (prefs_gpg_get_config()->skip_encryption_warning == NULL)
+               return FALSE;
+       systems = g_strsplit(prefs_gpg_get_config()->skip_encryption_warning,
+                               ",", -1);
+       while (systems && systems[i]) {
+               debug_print(" cmp %s %s\n", systems[i], systemid);
+               if (!strcmp(systems[i],systemid)) {
+                       g_strfreev(systems);
+                       return TRUE;
+               }
+               i++;
+       }
+       g_strfreev(systems);
+       return FALSE;
+}
+
+void prefs_gpg_add_skip_encryption_warning(const gchar *systemid)
+{
+       gchar *tmp = NULL;
+       if (prefs_gpg_get_config()->skip_encryption_warning == NULL)
+               prefs_gpg_get_config()->skip_encryption_warning =
+                       g_strdup_printf("%s,", systemid);
+       else if (!prefs_gpg_should_skip_encryption_warning(systemid)) {
+               tmp = g_strdup_printf("%s%s,",
+                       prefs_gpg_get_config()->skip_encryption_warning,
+                       systemid);
+               g_free(prefs_gpg_get_config()->skip_encryption_warning);
+               prefs_gpg_get_config()->skip_encryption_warning = tmp;
+       }
+       prefs_gpg_save_config();
+}
+
+void prefs_gpg_remove_skip_encryption_warning(const gchar *systemid)
+{
+       gchar **systems = NULL;
+       int i = 0;
+       if (prefs_gpg_get_config()->skip_encryption_warning == NULL)
+               return;
+
+       if (prefs_gpg_should_skip_encryption_warning(systemid)) {
+               systems = g_strsplit(prefs_gpg_get_config()->skip_encryption_warning,
+                               ",", -1);
+               g_free(prefs_gpg_get_config()->skip_encryption_warning);
+               prefs_gpg_get_config()->skip_encryption_warning = NULL;
+
+               while (systems && systems[i]) {
+                       if (!strcmp(systems[i],systemid)) {
+                               i++;
+                               continue;
+                       }
+                       prefs_gpg_add_skip_encryption_warning(systems[i]);
+                       i++;
+               }
+               
+               g_strfreev(systems);
+       }
+       prefs_gpg_save_config();
 }