Cosmetic: use always g_getenv instead of getenv
[claws.git] / src / plugins / pgpcore / prefs_gpg.c
index ae6e7df2f5fdaf3d9b9b6996edb961d1ffcd4c25..2aaebf28f335308ec05e48958515d968655c1bd0 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 2004-2011 the Claws Mail team
+ * Copyright (C) 2004-2015 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
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
+#ifdef HAVE_CONFIG_H
+#include "config.h"
+#include "claws-features.h"
+#endif
+
 #include <gtk/gtk.h>
 #include <glib.h>
 #include <glib/gi18n.h>
 
+#include <gtk/filesel.h>
+
 #include "defs.h"
 #include "gtk/gtkutils.h"
 #include "utils.h" 
 #include "prefs.h"
+#include "prefs_common.h"
 #include "prefs_gtk.h"
 #include "prefs_gpg.h"
 #include "sgpgme.h"
@@ -36,6 +43,12 @@ static PrefParam param[] = {
        {"auto_check_signatures", "FALSE",
         &prefs_gpg.auto_check_signatures, P_BOOL,
         NULL, NULL, NULL},
+       {"autocompletion", "FALSE",
+        &prefs_gpg.autocompletion, P_BOOL,
+        NULL, NULL, NULL},
+       {"autocompletion_limit", "0",
+        &prefs_gpg.autocompletion_limit, P_INT,
+        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,
@@ -51,22 +64,27 @@ static PrefParam param[] = {
         NULL, NULL, NULL},
        {"skip_encryption_warning", "", &prefs_gpg.skip_encryption_warning, P_STRING,
         NULL, NULL, NULL},
+       {"gpg_path", "", &prefs_gpg.gpg_path, P_STRING,
+        NULL, NULL, NULL},
 
        {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
 };
 
 static gchar *saved_gpg_agent_info = NULL;
+static void gpg_path_browse_cb(GtkWidget *widget, gpointer data);
 
 struct GPGPage
 {
        PrefsPage page;
 
        GtkWidget *checkbtn_auto_check_signatures;
+       GtkWidget *checkbtn_autocompletion;
        GtkWidget *checkbtn_use_gpg_agent;
         GtkWidget *checkbtn_store_passphrase;  
         GtkWidget *spinbtn_store_passphrase;  
         GtkWidget *checkbtn_passphrase_grab;  
         GtkWidget *checkbtn_gpg_warning;
+       GtkWidget *gpg_path;
 };
 
 static void prefs_gpg_create_widget_func(PrefsPage *_page,
@@ -80,15 +98,17 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        GtkWidget *checkbtn_passphrase_grab;
        GtkWidget *checkbtn_store_passphrase;
        GtkWidget *checkbtn_auto_check_signatures;
+       GtkWidget *checkbtn_autocompletion;
        GtkWidget *checkbtn_gpg_warning;
-       GtkWidget *hbox1;
+       GtkWidget *hbox1, *hbox2;
        GtkWidget *vbox1, *vbox2;
+       GtkWidget *label_gpg_path;
        GtkWidget *label_expire1;
-       GtkObject *spinbtn_store_passphrase_adj;
+       GtkAdjustment *spinbtn_store_passphrase_adj;
        GtkWidget *spinbtn_store_passphrase;
        GtkWidget *label_expire2;
        GtkWidget *frame_passphrase;
-       CLAWS_TIP_DECL();
+       GtkWidget *gpg_path, *gpg_path_btn;
 
        vbox1 = gtk_vbox_new (FALSE, VSPACING);
        gtk_widget_show (vbox1);
@@ -101,6 +121,9 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        PACK_CHECK_BUTTON (vbox2, checkbtn_auto_check_signatures,
                        _("Automatically check signatures"));
 
+       PACK_CHECK_BUTTON (vbox2, checkbtn_autocompletion,
+                       _("Use keyring for address autocompletion"));
+
        vbox2 = gtkut_get_options_frame(vbox1, &frame_passphrase, _("Passphrase"));
 
        PACK_CHECK_BUTTON (vbox2, checkbtn_use_gpg_agent,
@@ -124,7 +147,7 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        gtk_box_pack_start (GTK_BOX (hbox1), label_expire1, FALSE, FALSE, 0);
 
        spinbtn_store_passphrase_adj =
-           gtk_adjustment_new(1, 0, 1440, 1, 10, 0);
+           GTK_ADJUSTMENT(gtk_adjustment_new(1, 0, 1440, 1, 10, 0));
        spinbtn_store_passphrase =
            gtk_spin_button_new(GTK_ADJUSTMENT
                                (spinbtn_store_passphrase_adj), 1, 0);
@@ -133,8 +156,7 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
                           FALSE, 0);
        gtk_widget_set_size_request(spinbtn_store_passphrase, 64, -1);
        CLAWS_SET_TIP(spinbtn_store_passphrase,
-                            _
-                            ("Setting to '0' will store the passphrase for the whole session"));
+                     _("Setting to '0' will store the passphrase for the whole session"));
        gtk_spin_button_set_numeric(GTK_SPIN_BUTTON
                                    (spinbtn_store_passphrase), TRUE);
 
@@ -157,10 +179,27 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        PACK_CHECK_BUTTON (vbox2, checkbtn_gpg_warning,
                        _("Display warning on start-up if GnuPG doesn't work"));
 
+       hbox2 = gtk_hbox_new(FALSE, 6);
+       label_gpg_path = gtk_label_new(_("Path to GnuPG executable"));
+       gtk_box_pack_start(GTK_BOX(hbox2), label_gpg_path, FALSE, FALSE, 0);
+       gpg_path = gtk_entry_new();
+       gtk_box_pack_start(GTK_BOX(hbox2), gpg_path, TRUE, TRUE, 0);
+       CLAWS_SET_TIP(gpg_path,
+                     _("If left blank the location of the GnuPG executable will be automatically determined."));
+       gpg_path_btn = gtkut_get_browse_file_btn(_("Bro_wse"));
+       gtk_box_pack_start(GTK_BOX(hbox2), gpg_path_btn, FALSE, FALSE, 0);
+       g_signal_connect(G_OBJECT(gpg_path_btn), "clicked",
+                        G_CALLBACK(gpg_path_browse_cb), gpg_path);
+       pref_set_entry_from_pref(GTK_ENTRY(gpg_path), prefs_gpg.gpg_path);
+
+       gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
+       gtk_widget_show_all(vbox1);
+
        config = prefs_gpg_get_config();
 
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_auto_check_signatures), config->auto_check_signatures);
-       if (!getenv("GPG_AGENT_INFO"))
+       gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_autocompletion), config->autocompletion);
+       if (!g_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);
@@ -169,16 +208,32 @@ static void prefs_gpg_create_widget_func(PrefsPage *_page,
        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);
+       gtk_entry_set_text(GTK_ENTRY(gpg_path), config->gpg_path);
 
        page->checkbtn_auto_check_signatures = checkbtn_auto_check_signatures;
+       page->checkbtn_autocompletion = checkbtn_autocompletion;
        page->checkbtn_store_passphrase = checkbtn_store_passphrase;
        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->gpg_path = gpg_path;
        page->page.widget = vbox1;
 }
 
+static void gpg_path_browse_cb(GtkWidget* widget, gpointer data)
+{
+       gchar *filename;
+       GtkEntry *dest = GTK_ENTRY(data);
+
+       filename = filesel_select_file_open(_("Select GnuPG executable"), NULL);
+       if (!filename)
+               return;
+
+       gtk_entry_set_text(GTK_ENTRY(dest), filename);
+       g_free(filename);
+}
+
 static void prefs_gpg_destroy_widget_func(PrefsPage *_page)
 {
 }
@@ -190,6 +245,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->autocompletion =
+               gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autocompletion));
        config->use_gpg_agent = 
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_use_gpg_agent));
        config->store_passphrase = 
@@ -200,6 +257,10 @@ static void prefs_gpg_save_func(PrefsPage *_page)
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_passphrase_grab));
        config->gpg_warning = 
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_gpg_warning));
+       g_free(config->gpg_path);
+       config->gpg_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(page->gpg_path)));
+       if (strcmp(config->gpg_path, "") != 0 && access(config->gpg_path, X_OK) != -1)
+               gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, config->gpg_path, NULL);
 
        prefs_gpg_enable_agent(config->use_gpg_agent);
 
@@ -440,7 +501,7 @@ void prefs_gpg_save_config(void)
                return;
 
        if (prefs_write_param(param, pfile->fp) < 0) {
-               g_warning("failed to write GPG configuration to file\n");
+               g_warning("failed to write GPG configuration to file");
                prefs_file_close_revert(pfile);
                return;
        }
@@ -500,7 +561,7 @@ void prefs_gpg_account_set_config(PrefsAccount *account, GPGAccountConfig *confi
                break;
        default:
                confstr = g_strdup("");
-               g_warning("prefs_gpg_account_set_config: bad sign_key val\n");
+               g_warning("prefs_gpg_account_set_config: bad sign_key val");
        }
 
        prefs_account_set_privacy_prefs(account, "gpg", confstr);
@@ -646,3 +707,8 @@ void prefs_gpg_remove_skip_encryption_warning(const gchar *systemid)
        }
        prefs_gpg_save_config();
 }
+
+gboolean prefs_gpg_auto_check_signatures(void)
+{
+       return prefs_gpg_get_config()->auto_check_signatures;
+}