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