338206bc44caf40e7e1fe8d70576fc4893d663f7
[claws.git] / src / plugins / pgpcore / prefs_gpg.c
1 /*
2  * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004-2015 the Claws Mail team
4  *
5  * This program is free software; you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation; either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  * 
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #include <gtk/gtk.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28
29 #include <gtk/filesel.h>
30
31 #include "defs.h"
32 #include "gtk/gtkutils.h"
33 #include "utils.h" 
34 #include "prefs.h"
35 #include "prefs_common.h"
36 #include "prefs_gtk.h"
37 #include "prefs_gpg.h"
38 #include "sgpgme.h"
39
40 struct GPGConfig prefs_gpg;
41
42 static PrefParam param[] = {
43         /* Privacy */
44         {"auto_check_signatures", "FALSE",
45          &prefs_gpg.auto_check_signatures, P_BOOL,
46          NULL, NULL, NULL},
47         {"autocompletion", "FALSE",
48          &prefs_gpg.autocompletion, P_BOOL,
49          NULL, NULL, NULL},
50         {"autocompletion_limit", "0",
51          &prefs_gpg.autocompletion_limit, P_INT,
52          NULL, NULL, NULL},
53         {"use_gpg_agent", "TRUE", &prefs_gpg.use_gpg_agent, P_BOOL,
54          NULL, NULL, NULL},
55         {"store_passphrase", "FALSE", &prefs_gpg.store_passphrase, P_BOOL,
56          NULL, NULL, NULL},
57         {"store_passphrase_timeout", "0",
58          &prefs_gpg.store_passphrase_timeout, P_INT,
59          NULL, NULL, NULL},
60         {"passphrase_grab", "FALSE", &prefs_gpg.passphrase_grab, P_BOOL,
61          NULL, NULL, NULL},
62         {"gpg_warning", "TRUE", &prefs_gpg.gpg_warning, P_BOOL,
63          NULL, NULL, NULL},
64         {"gpg_ask_create_key", "TRUE", &prefs_gpg.gpg_ask_create_key, P_BOOL,
65          NULL, NULL, NULL},
66         {"skip_encryption_warning", "", &prefs_gpg.skip_encryption_warning, P_STRING,
67          NULL, NULL, NULL},
68         {"gpg_path", "", &prefs_gpg.gpg_path, P_STRING,
69          NULL, NULL, NULL},
70
71         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
72 };
73
74 static gchar *saved_gpg_agent_info = NULL;
75 static void gpg_path_browse_cb(GtkWidget *widget, gpointer data);
76
77 struct GPGPage
78 {
79         PrefsPage page;
80
81         GtkWidget *checkbtn_auto_check_signatures;
82         GtkWidget *checkbtn_autocompletion;
83         GtkWidget *checkbtn_use_gpg_agent;
84         GtkWidget *checkbtn_store_passphrase;  
85         GtkWidget *spinbtn_store_passphrase;  
86         GtkWidget *checkbtn_passphrase_grab;  
87         GtkWidget *checkbtn_gpg_warning;
88         GtkWidget *gpg_path;
89 };
90
91 static void prefs_gpg_create_widget_func(PrefsPage *_page,
92                                          GtkWindow *window,
93                                          gpointer data)
94 {
95         struct GPGPage *page = (struct GPGPage *) _page;
96         struct GPGConfig *config;
97
98         GtkWidget *checkbtn_use_gpg_agent;
99         GtkWidget *checkbtn_passphrase_grab;
100         GtkWidget *checkbtn_store_passphrase;
101         GtkWidget *checkbtn_auto_check_signatures;
102         GtkWidget *checkbtn_autocompletion;
103         GtkWidget *checkbtn_gpg_warning;
104         GtkWidget *hbox1, *hbox2;
105         GtkWidget *vbox1, *vbox2;
106         GtkWidget *label_gpg_path;
107         GtkWidget *label_expire1;
108         GtkAdjustment *spinbtn_store_passphrase_adj;
109         GtkWidget *spinbtn_store_passphrase;
110         GtkWidget *label_expire2;
111         GtkWidget *frame_passphrase;
112         GtkWidget *gpg_path, *gpg_path_btn;
113
114         vbox1 = gtk_vbox_new (FALSE, VSPACING);
115         gtk_widget_show (vbox1);
116         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
117
118         vbox2 = gtk_vbox_new (FALSE, 0);
119         gtk_widget_show (vbox2);
120         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
121
122         PACK_CHECK_BUTTON (vbox2, checkbtn_auto_check_signatures,
123                         _("Automatically check signatures"));
124
125         PACK_CHECK_BUTTON (vbox2, checkbtn_autocompletion,
126                         _("Use keyring for address autocompletion"));
127
128         vbox2 = gtkut_get_options_frame(vbox1, &frame_passphrase, _("Passphrase"));
129
130         PACK_CHECK_BUTTON (vbox2, checkbtn_use_gpg_agent,
131                         _("Use gpg-agent to manage passwords"));
132         if (saved_gpg_agent_info == NULL)
133                 gtk_widget_set_sensitive(checkbtn_use_gpg_agent, FALSE);
134
135         PACK_CHECK_BUTTON (vbox2, checkbtn_store_passphrase,
136                         _("Store passphrase in memory"));
137
138         SET_TOGGLE_SENSITIVITY_REVERSE(checkbtn_use_gpg_agent, checkbtn_store_passphrase);
139
140         hbox1 = gtk_hbox_new (FALSE, 8);
141         gtk_widget_show (hbox1);
142         gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
143
144         SET_TOGGLE_SENSITIVITY_REVERSE(checkbtn_use_gpg_agent, hbox1);
145
146         label_expire1 = gtk_label_new(_("Expire after"));
147         gtk_widget_show (label_expire1);
148         gtk_box_pack_start (GTK_BOX (hbox1), label_expire1, FALSE, FALSE, 0);
149
150         spinbtn_store_passphrase_adj =
151             GTK_ADJUSTMENT(gtk_adjustment_new(1, 0, 1440, 1, 10, 0));
152         spinbtn_store_passphrase =
153             gtk_spin_button_new(GTK_ADJUSTMENT
154                                 (spinbtn_store_passphrase_adj), 1, 0);
155         gtk_widget_show(spinbtn_store_passphrase);
156         gtk_box_pack_start(GTK_BOX(hbox1), spinbtn_store_passphrase, FALSE,
157                            FALSE, 0);
158         gtk_widget_set_size_request(spinbtn_store_passphrase, 64, -1);
159         CLAWS_SET_TIP(spinbtn_store_passphrase,
160                       _("Setting to '0' will store the passphrase for the whole session"));
161         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON
162                                     (spinbtn_store_passphrase), TRUE);
163
164         label_expire2 = gtk_label_new(_("minute(s)"));
165         gtk_widget_show(label_expire2);
166         gtk_box_pack_start(GTK_BOX(hbox1), label_expire2, FALSE, FALSE, 0);
167         gtk_misc_set_alignment(GTK_MISC(label_expire2), 0.0, 0.5);
168
169         SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, label_expire1);
170         SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, spinbtn_store_passphrase);
171         SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, label_expire2);
172
173         PACK_CHECK_BUTTON (vbox2, checkbtn_passphrase_grab,
174                         _("Grab input while entering a passphrase"));
175
176         vbox2 = gtk_vbox_new (FALSE, 0);
177         gtk_widget_show (vbox2);
178         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
179
180         PACK_CHECK_BUTTON (vbox2, checkbtn_gpg_warning,
181                         _("Display warning on start-up if GnuPG doesn't work"));
182
183         hbox2 = gtk_hbox_new(FALSE, 6);
184         label_gpg_path = gtk_label_new(_("Path to GnuPG executable"));
185         gtk_box_pack_start(GTK_BOX(hbox2), label_gpg_path, FALSE, FALSE, 0);
186         gpg_path = gtk_entry_new();
187         gtk_box_pack_start(GTK_BOX(hbox2), gpg_path, TRUE, TRUE, 0);
188         CLAWS_SET_TIP(gpg_path,
189                       _("If left blank the location of the GnuPG executable will be automatically determined."));
190         gpg_path_btn = gtkut_get_browse_file_btn(_("Bro_wse"));
191         gtk_box_pack_start(GTK_BOX(hbox2), gpg_path_btn, FALSE, FALSE, 0);
192         g_signal_connect(G_OBJECT(gpg_path_btn), "clicked",
193                          G_CALLBACK(gpg_path_browse_cb), gpg_path);
194         pref_set_entry_from_pref(GTK_ENTRY(gpg_path), prefs_gpg.gpg_path);
195
196         gtk_box_pack_start(GTK_BOX(vbox2), hbox2, FALSE, FALSE, 0);
197         gtk_widget_show_all(vbox1);
198
199         config = prefs_gpg_get_config();
200
201         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_auto_check_signatures), config->auto_check_signatures);
202         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_autocompletion), config->autocompletion);
203         if (!getenv("GPG_AGENT_INFO"))
204                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_use_gpg_agent), FALSE);
205         else
206                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_use_gpg_agent), config->use_gpg_agent);
207         if (!gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(checkbtn_use_gpg_agent)))
208                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_store_passphrase), config->store_passphrase);
209         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_store_passphrase), (float) config->store_passphrase_timeout);
210         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_passphrase_grab), config->passphrase_grab);
211         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_gpg_warning), config->gpg_warning);
212         gtk_entry_set_text(GTK_ENTRY(gpg_path), config->gpg_path);
213
214         page->checkbtn_auto_check_signatures = checkbtn_auto_check_signatures;
215         page->checkbtn_autocompletion = checkbtn_autocompletion;
216         page->checkbtn_store_passphrase = checkbtn_store_passphrase;
217         page->spinbtn_store_passphrase = spinbtn_store_passphrase;
218         page->checkbtn_passphrase_grab = checkbtn_passphrase_grab;
219         page->checkbtn_gpg_warning = checkbtn_gpg_warning;
220         page->checkbtn_use_gpg_agent = checkbtn_use_gpg_agent;
221         page->gpg_path = gpg_path;
222         page->page.widget = vbox1;
223 }
224
225 static void gpg_path_browse_cb(GtkWidget* widget, gpointer data)
226 {
227         gchar *filename;
228         GtkEntry *dest = GTK_ENTRY(data);
229
230         filename = filesel_select_file_open(_("Select GnuPG executable"), NULL);
231         if (!filename)
232                 return;
233
234         gtk_entry_set_text(GTK_ENTRY(dest), filename);
235         g_free(filename);
236 }
237
238 static void prefs_gpg_destroy_widget_func(PrefsPage *_page)
239 {
240 }
241
242 static void prefs_gpg_save_func(PrefsPage *_page)
243 {
244         struct GPGPage *page = (struct GPGPage *) _page;
245         GPGConfig *config = prefs_gpg_get_config();
246
247         config->auto_check_signatures =
248                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_auto_check_signatures));
249         config->autocompletion =
250                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_autocompletion));
251         config->use_gpg_agent = 
252                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_use_gpg_agent));
253         config->store_passphrase = 
254                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_store_passphrase));
255         config->store_passphrase_timeout = 
256                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_store_passphrase));
257         config->passphrase_grab = 
258                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_passphrase_grab));
259         config->gpg_warning = 
260                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_gpg_warning));
261         g_free(config->gpg_path);
262         config->gpg_path = g_strdup(gtk_entry_get_text(GTK_ENTRY(page->gpg_path)));
263         if (strcmp(config->gpg_path, "") != 0 && access(config->gpg_path, X_OK) != -1)
264                 gpgme_set_engine_info(GPGME_PROTOCOL_OpenPGP, config->gpg_path, NULL);
265
266         prefs_gpg_enable_agent(config->use_gpg_agent);
267
268         prefs_gpg_save_config();
269 }
270
271 struct GPGAccountPage
272 {
273         PrefsPage page;
274
275         GtkWidget *key_default;
276         GtkWidget *key_by_from;
277         GtkWidget *key_custom;
278         GtkWidget *keyid;
279         GtkWidget *keyid_label;
280         GtkWidget *new_key_label;
281         GtkWidget *new_key_btn;
282         GtkWidget *new_key_box;
283
284         PrefsAccount *account;
285 };
286
287 void key_custom_toggled(GtkToggleButton *togglebutton, gpointer user_data)
288 {
289         struct GPGAccountPage *page = (struct GPGAccountPage *) user_data;
290         gboolean active;
291
292         active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom));
293         gtk_widget_set_sensitive(GTK_WIDGET(page->keyid_label), active);
294         gtk_widget_set_sensitive(GTK_WIDGET(page->keyid), active);
295         if (!active)
296                 gtk_editable_delete_text(GTK_EDITABLE(page->keyid), 0, -1);
297 }
298
299 static void prefs_gpg_update_sens(struct GPGAccountPage *page)
300 {
301         gboolean active;
302         active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom));
303         if (sgpgme_has_secret_key()) {
304                 gtk_widget_hide(page->new_key_box);
305                 gtk_widget_set_sensitive(page->key_default, TRUE);
306                 gtk_widget_set_sensitive(page->key_by_from, TRUE);
307                 gtk_widget_set_sensitive(page->key_custom, TRUE);
308                 gtk_widget_set_sensitive(page->keyid, active);
309                 gtk_widget_set_sensitive(page->keyid_label, active);
310         } else {
311                 gtk_widget_show(page->new_key_box);
312                 gtk_widget_set_sensitive(page->key_default, FALSE);
313                 gtk_widget_set_sensitive(page->key_by_from, FALSE);
314                 gtk_widget_set_sensitive(page->key_custom, FALSE);
315                 gtk_widget_set_sensitive(page->keyid, FALSE);
316                 gtk_widget_set_sensitive(page->keyid_label, FALSE);
317         }
318 }
319
320 static void new_key_clicked(GtkWidget *widget, gpointer user_data)
321 {
322         struct GPGAccountPage *page = (struct GPGAccountPage *) user_data;
323         sgpgme_create_secret_key(page->account, FALSE);
324         prefs_gpg_update_sens(page);
325 }
326
327 static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
328                                                  GtkWindow *window,
329                                                  gpointer data)
330 {
331         struct GPGAccountPage *page = (struct GPGAccountPage *) _page;
332         PrefsAccount *account = (PrefsAccount *) data;
333         GPGAccountConfig *config;
334
335         GtkWidget *vbox;
336         GtkWidget *frame1;
337         GtkWidget *vbox2;
338         GtkWidget *hbox;
339         GSList *key_group = NULL;
340         GtkWidget *key_default;
341         GtkWidget *key_by_from;
342         GtkWidget *key_custom;
343         GtkWidget *keyid_label;
344         GtkWidget *keyid;
345         GtkWidget *image;
346         GtkWidget *new_key_label;
347         GtkWidget *new_key_btn;
348         GtkWidget *new_key_box;
349
350         vbox = gtk_vbox_new(FALSE, VSPACING);
351         gtk_container_set_border_width (GTK_CONTAINER (vbox), VBOX_BORDER);
352         gtk_widget_show(vbox);
353
354         vbox2 = gtkut_get_options_frame(vbox, &frame1, _("Sign key"));
355
356         hbox = gtk_hbox_new (FALSE, 5);
357         gtk_widget_show (hbox);
358         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
359         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
360
361         key_default = gtk_radio_button_new_with_label(key_group,
362                         _("Use default GnuPG key"));
363         key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_default));
364         gtk_widget_show(key_default);
365         gtk_box_pack_start(GTK_BOX(hbox), key_default, FALSE, FALSE, 0);
366
367         hbox = gtk_hbox_new (FALSE, 5);
368         gtk_widget_show (hbox);
369         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
370         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
371
372         key_by_from = gtk_radio_button_new_with_label(key_group,
373                 _("Select key by your email address"));
374         key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_by_from));
375         gtk_widget_show(key_by_from);
376         gtk_box_pack_start(GTK_BOX(hbox), key_by_from, FALSE, FALSE, 0);
377
378         hbox = gtk_hbox_new (FALSE, 5);
379         gtk_widget_show (hbox);
380         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
381         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
382
383         key_custom = gtk_radio_button_new_with_label(key_group,
384                 _("Specify key manually"));
385         key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_custom));
386         gtk_widget_show(key_custom);
387         gtk_box_pack_start(GTK_BOX(hbox), key_custom, FALSE, FALSE, 0);
388
389         hbox = gtk_hbox_new (FALSE, 5);
390         gtk_widget_show (hbox);
391         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
392         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
393
394         keyid_label = gtk_label_new(_("User or key ID:"));
395         gtk_widget_show(keyid_label);
396         gtk_label_set_justify(GTK_LABEL(keyid_label), GTK_JUSTIFY_LEFT);
397         gtk_box_pack_start(GTK_BOX(hbox), keyid_label, FALSE, FALSE, 0);
398
399         keyid = gtk_entry_new();
400         gtk_widget_show(keyid);
401         gtk_box_pack_start(GTK_BOX(hbox), keyid, FALSE, FALSE, 0);
402
403         config = prefs_gpg_account_get_config(account);
404         switch (config->sign_key) {
405         case SIGN_KEY_DEFAULT:
406                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_default), TRUE);
407                 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), FALSE);
408                 gtk_widget_set_sensitive(GTK_WIDGET(keyid), FALSE);
409                 break;
410         case SIGN_KEY_BY_FROM:
411                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_by_from), TRUE);
412                 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), FALSE);
413                 gtk_widget_set_sensitive(GTK_WIDGET(keyid), FALSE);
414                 break;
415         case SIGN_KEY_CUSTOM:
416                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_custom), TRUE);
417                 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), TRUE);
418                 gtk_widget_set_sensitive(GTK_WIDGET(keyid), TRUE);
419                 break;
420         }
421
422         hbox = gtk_hbox_new (FALSE, 5);
423         gtk_widget_show (hbox);
424         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
425
426         new_key_box = gtk_hbox_new(FALSE, 6);
427         gtk_widget_show(new_key_box);
428         gtk_box_pack_start(GTK_BOX(hbox), new_key_box, FALSE, FALSE, 0);
429
430         image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING,
431                         GTK_ICON_SIZE_SMALL_TOOLBAR);
432
433         gtk_box_pack_start(GTK_BOX(new_key_box), image, FALSE, FALSE, 0);
434         new_key_label = gtk_label_new(
435                         _("No secret key found."));
436         gtk_box_pack_start(GTK_BOX(new_key_box), new_key_label, FALSE, FALSE, 0);
437
438         new_key_btn = gtk_button_new_with_label(_("Generate a new key pair"));
439         gtk_widget_show(new_key_btn);
440         gtk_box_pack_start(GTK_BOX(hbox), new_key_btn, FALSE, FALSE, 0);
441
442         if (config->sign_key_id != NULL)
443                 gtk_entry_set_text(GTK_ENTRY(keyid), config->sign_key_id);
444
445         g_signal_connect(G_OBJECT(key_custom), "toggled", G_CALLBACK(key_custom_toggled), page);
446         g_signal_connect(G_OBJECT(new_key_btn), "clicked", G_CALLBACK(new_key_clicked), page);
447
448         page->key_default = key_default;
449         page->key_by_from = key_by_from;
450         page->key_custom = key_custom;
451         page->keyid = keyid;
452         page->keyid_label = keyid_label;
453         page->new_key_box = new_key_box;
454
455         page->page.widget = vbox;
456         page->account = account;
457         prefs_gpg_update_sens(page);
458 }
459
460 static void prefs_gpg_account_destroy_widget_func(PrefsPage *_page)
461 {
462         /* nothing to do here */
463 }
464
465 static void prefs_gpg_account_save_func(PrefsPage *_page)
466 {
467         struct GPGAccountPage *page = (struct GPGAccountPage *) _page;
468         GPGAccountConfig *config;
469
470         config = prefs_gpg_account_get_config(page->account);
471
472         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_default)))
473                 config->sign_key = SIGN_KEY_DEFAULT;
474         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_by_from)))
475                 config->sign_key = SIGN_KEY_BY_FROM;
476         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom))) {
477                 config->sign_key = SIGN_KEY_CUSTOM;
478                 g_free(config->sign_key_id);
479                 config->sign_key_id = gtk_editable_get_chars(GTK_EDITABLE(page->keyid), 0, -1);
480         }
481
482         prefs_gpg_account_set_config(page->account, config);
483         prefs_gpg_account_free_config(config);
484 }
485
486 GPGConfig *prefs_gpg_get_config(void)
487 {
488         return &prefs_gpg;
489 }
490
491 void prefs_gpg_save_config(void)
492 {
493         PrefFile *pfile;
494         gchar *rcpath;
495
496         debug_print("Saving GPG config\n");
497
498         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
499         pfile = prefs_write_open(rcpath);
500         g_free(rcpath);
501         if (!pfile || (prefs_set_block_label(pfile, "GPG") < 0))
502                 return;
503
504         if (prefs_write_param(param, pfile->fp) < 0) {
505                 g_warning("failed to write GPG configuration to file");
506                 prefs_file_close_revert(pfile);
507                 return;
508         }
509         if (fprintf(pfile->fp, "\n") < 0) {
510                 FILE_OP_ERROR(rcpath, "fprintf");
511                 prefs_file_close_revert(pfile);
512         } else
513                 prefs_file_close(pfile);
514 }
515
516 struct GPGAccountConfig *prefs_gpg_account_get_config(PrefsAccount *account)
517 {
518         GPGAccountConfig *config;
519         const gchar *confstr;
520         gchar **strv;
521
522         config = g_new0(GPGAccountConfig, 1);
523         config->sign_key = SIGN_KEY_DEFAULT;
524         config->sign_key_id = NULL;
525
526         confstr = prefs_account_get_privacy_prefs(account, "gpg");
527         if (confstr == NULL)
528                 return config;
529
530         strv = g_strsplit(confstr, ";", 0);
531         if (strv[0] != NULL) {
532                 if (!strcmp(strv[0], "DEFAULT"))
533                         config->sign_key = SIGN_KEY_DEFAULT;
534                 if (!strcmp(strv[0], "BY_FROM"))
535                         config->sign_key = SIGN_KEY_BY_FROM;
536                 if (!strcmp(strv[0], "CUSTOM")) {
537                         if (strv[1] != NULL) {
538                                 config->sign_key = SIGN_KEY_CUSTOM;
539                                 config->sign_key_id = g_strdup(strv[1]);
540                         } else
541                                 config->sign_key = SIGN_KEY_DEFAULT;
542                 }
543         }
544         g_strfreev(strv);
545
546         return config;
547 }
548
549 void prefs_gpg_account_set_config(PrefsAccount *account, GPGAccountConfig *config)
550 {
551         gchar *confstr = NULL;
552
553         switch (config->sign_key) {
554         case SIGN_KEY_DEFAULT:
555                 confstr = g_strdup("DEFAULT");
556                 break;
557         case SIGN_KEY_BY_FROM:
558                 confstr = g_strdup("BY_FROM");
559                 break;
560         case SIGN_KEY_CUSTOM:
561                 confstr = g_strdup_printf("CUSTOM;%s", config->sign_key_id);
562                 break;
563         default:
564                 confstr = g_strdup("");
565                 g_warning("prefs_gpg_account_set_config: bad sign_key val");
566         }
567
568         prefs_account_set_privacy_prefs(account, "gpg", confstr);
569
570         g_free(confstr);
571 }
572
573 void prefs_gpg_account_free_config(GPGAccountConfig *config)
574 {
575         g_free(config->sign_key_id);
576         g_free(config);
577 }
578
579 static struct GPGPage gpg_page;
580 static struct GPGAccountPage gpg_account_page;
581
582 void prefs_gpg_enable_agent(gboolean enable)
583 {
584         if (enable) {
585                 if (saved_gpg_agent_info) {
586                         g_setenv("GPG_AGENT_INFO",
587                                  saved_gpg_agent_info, TRUE);
588                         debug_print("set GPG_AGENT_INFO=%s\n", 
589                                 saved_gpg_agent_info);
590                 } else { 
591                         debug_print("Can't enable gpg agent (no GPG_AGENT_INFO)\n");
592                 }
593         } else {
594                 if (saved_gpg_agent_info) {
595                         g_unsetenv("GPG_AGENT_INFO");
596                         debug_print("unset GPG_AGENT_INFO=%s\n", 
597                                 saved_gpg_agent_info);
598                 } else {
599                         debug_print("Can't disable gpg agent (no GPG_AGENT_INFO)\n");
600                 }
601         }
602 }
603
604 void prefs_gpg_init()
605 {
606         static gchar *path[3];
607         gchar *rcpath;
608         const gchar *tmp = NULL;
609
610         prefs_set_default(param);
611         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
612         prefs_read_config(param, "GPG", rcpath, NULL);
613         g_free(rcpath);
614
615         path[0] = _("Plugins");
616         path[1] = _("GPG");
617         path[2] = NULL;
618
619         gpg_page.page.path = path;
620         gpg_page.page.create_widget = prefs_gpg_create_widget_func;
621         gpg_page.page.destroy_widget = prefs_gpg_destroy_widget_func;
622         gpg_page.page.save_page = prefs_gpg_save_func;
623         gpg_page.page.weight = 30.0;
624
625         prefs_gtk_register_page((PrefsPage *) &gpg_page);
626
627         gpg_account_page.page.path = path;
628         gpg_account_page.page.create_widget = prefs_gpg_account_create_widget_func;
629         gpg_account_page.page.destroy_widget = prefs_gpg_account_destroy_widget_func;
630         gpg_account_page.page.save_page = prefs_gpg_account_save_func;
631         gpg_account_page.page.weight = 30.0;
632
633         prefs_account_register_page((PrefsPage *) &gpg_account_page);
634         
635         tmp = g_getenv("GPG_AGENT_INFO");
636         if (tmp)
637                 saved_gpg_agent_info = g_strdup(tmp);
638
639         prefs_gpg_enable_agent(prefs_gpg_get_config()->use_gpg_agent);
640 }
641
642 void prefs_gpg_done()
643 {
644         prefs_gtk_unregister_page((PrefsPage *) &gpg_page);
645         prefs_account_unregister_page((PrefsPage *) &gpg_account_page);
646         prefs_gpg_enable_agent(TRUE);
647 }
648
649 gboolean prefs_gpg_should_skip_encryption_warning(const gchar *systemid)
650 {
651         gchar **systems = NULL;
652         int i = 0;
653         if (prefs_gpg_get_config()->skip_encryption_warning == NULL)
654                 return FALSE;
655         systems = g_strsplit(prefs_gpg_get_config()->skip_encryption_warning,
656                                 ",", -1);
657         while (systems && systems[i]) {
658                 debug_print(" cmp %s %s\n", systems[i], systemid);
659                 if (!strcmp(systems[i],systemid)) {
660                         g_strfreev(systems);
661                         return TRUE;
662                 }
663                 i++;
664         }
665         g_strfreev(systems);
666         return FALSE;
667 }
668
669 void prefs_gpg_add_skip_encryption_warning(const gchar *systemid)
670 {
671         gchar *tmp = NULL;
672         if (prefs_gpg_get_config()->skip_encryption_warning == NULL)
673                 prefs_gpg_get_config()->skip_encryption_warning =
674                         g_strdup_printf("%s,", systemid);
675         else if (!prefs_gpg_should_skip_encryption_warning(systemid)) {
676                 tmp = g_strdup_printf("%s%s,",
677                         prefs_gpg_get_config()->skip_encryption_warning,
678                         systemid);
679                 g_free(prefs_gpg_get_config()->skip_encryption_warning);
680                 prefs_gpg_get_config()->skip_encryption_warning = tmp;
681         }
682         prefs_gpg_save_config();
683 }
684
685 void prefs_gpg_remove_skip_encryption_warning(const gchar *systemid)
686 {
687         gchar **systems = NULL;
688         int i = 0;
689         if (prefs_gpg_get_config()->skip_encryption_warning == NULL)
690                 return;
691
692         if (prefs_gpg_should_skip_encryption_warning(systemid)) {
693                 systems = g_strsplit(prefs_gpg_get_config()->skip_encryption_warning,
694                                 ",", -1);
695                 g_free(prefs_gpg_get_config()->skip_encryption_warning);
696                 prefs_gpg_get_config()->skip_encryption_warning = NULL;
697
698                 while (systems && systems[i]) {
699                         if (!strcmp(systems[i],systemid)) {
700                                 i++;
701                                 continue;
702                         }
703                         prefs_gpg_add_skip_encryption_warning(systems[i]);
704                         i++;
705                 }
706                 
707                 g_strfreev(systems);
708         }
709         prefs_gpg_save_config();
710 }
711
712 gboolean prefs_gpg_auto_check_signatures(void)
713 {
714         return prefs_gpg_get_config()->auto_check_signatures;
715 }