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