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