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