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