2007-01-13 [colin] 2.7.0cvs21
[claws.git] / src / plugins / pgpcore / prefs_gpg.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 2004 Hiroyuki Yamamoto & 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 2 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, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
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 "prefs.h"
27 #include "prefs_gtk.h"
28 #include "prefs_gpg.h"
29 #include "sgpgme.h"
30
31 struct GPGConfig prefs_gpg;
32
33 static PrefParam param[] = {
34         /* Privacy */
35         {"auto_check_signatures", "FALSE",
36          &prefs_gpg.auto_check_signatures, P_BOOL,
37          NULL, NULL, NULL},
38         {"store_passphrase", "FALSE", &prefs_gpg.store_passphrase, P_BOOL,
39          NULL, NULL, NULL},
40         {"store_passphrase_timeout", "0",
41          &prefs_gpg.store_passphrase_timeout, P_INT,
42          NULL, NULL, NULL},
43         {"passphrase_grab", "FALSE", &prefs_gpg.passphrase_grab, P_BOOL,
44          NULL, NULL, NULL},
45         {"gpg_warning", "TRUE", &prefs_gpg.gpg_warning, P_BOOL,
46          NULL, NULL, NULL},
47         {"gpg_ask_create_key", "TRUE", &prefs_gpg.gpg_ask_create_key, P_BOOL,
48          NULL, NULL, NULL},
49
50         {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
51 };
52
53 struct GPGPage
54 {
55         PrefsPage page;
56
57         GtkWidget *checkbtn_auto_check_signatures;
58         GtkWidget *checkbtn_store_passphrase;  
59         GtkWidget *spinbtn_store_passphrase;  
60         GtkWidget *checkbtn_passphrase_grab;  
61         GtkWidget *checkbtn_gpg_warning;
62 };
63
64 static void prefs_gpg_create_widget_func(PrefsPage *_page,
65                                          GtkWindow *window,
66                                          gpointer data)
67 {
68         struct GPGPage *page = (struct GPGPage *) _page;
69         struct GPGConfig *config;
70
71         GtkWidget *checkbtn_passphrase_grab;
72         GtkWidget *checkbtn_store_passphrase;
73         GtkWidget *checkbtn_auto_check_signatures;
74         GtkWidget *checkbtn_gpg_warning;
75         GtkWidget *hbox1;
76         GtkWidget *vbox1, *vbox2;
77         GtkWidget *label_expire1;
78         GtkObject *spinbtn_store_passphrase_adj;
79         GtkWidget *spinbtn_store_passphrase;
80         GtkWidget *label_expire2;
81         GtkWidget *frame_passphrase;
82         GtkTooltips *tooltips;
83
84         tooltips = gtk_tooltips_new();
85
86         vbox1 = gtk_vbox_new (FALSE, VSPACING);
87         gtk_widget_show (vbox1);
88         gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
89
90         vbox2 = gtk_vbox_new (FALSE, 0);
91         gtk_widget_show (vbox2);
92         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
93
94         PACK_CHECK_BUTTON (vbox2, checkbtn_auto_check_signatures,
95                         _("Automatically check signatures"));
96
97         vbox2 = gtkut_get_options_frame(vbox1, &frame_passphrase, _("Passphrase"));
98
99         PACK_CHECK_BUTTON (vbox2, checkbtn_store_passphrase,
100                         _("Store passphrase in memory"));
101
102         hbox1 = gtk_hbox_new (FALSE, 8);
103         gtk_widget_show (hbox1);
104         gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
105
106         label_expire1 = gtk_label_new(_("Expire after"));
107         gtk_widget_show (label_expire1);
108         gtk_box_pack_start (GTK_BOX (hbox1), label_expire1, FALSE, FALSE, 0);
109
110         spinbtn_store_passphrase_adj =
111             gtk_adjustment_new(1, 0, 1440, 1, 10, 10);
112         spinbtn_store_passphrase =
113             gtk_spin_button_new(GTK_ADJUSTMENT
114                                 (spinbtn_store_passphrase_adj), 1, 0);
115         gtk_widget_show(spinbtn_store_passphrase);
116         gtk_box_pack_start(GTK_BOX(hbox1), spinbtn_store_passphrase, FALSE,
117                            FALSE, 0);
118         gtk_widget_set_size_request(spinbtn_store_passphrase, 64, -1);
119         gtk_tooltips_set_tip(tooltips, spinbtn_store_passphrase,
120                              _
121                              ("Setting to '0' will store the passphrase for the whole session"),
122                              NULL);
123         gtk_spin_button_set_numeric(GTK_SPIN_BUTTON
124                                     (spinbtn_store_passphrase), TRUE);
125
126         label_expire2 = gtk_label_new(_("minute(s)"));
127         gtk_widget_show(label_expire2);
128         gtk_box_pack_start(GTK_BOX(hbox1), label_expire2, FALSE, FALSE, 0);
129         gtk_misc_set_alignment(GTK_MISC(label_expire2), 0.0, 0.5);
130
131         SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, label_expire1);
132         SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, spinbtn_store_passphrase);
133         SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, label_expire2);
134
135         PACK_CHECK_BUTTON (vbox2, checkbtn_passphrase_grab,
136                         _("Grab input while entering a passphrase"));
137
138         vbox2 = gtk_vbox_new (FALSE, 0);
139         gtk_widget_show (vbox2);
140         gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
141
142         PACK_CHECK_BUTTON (vbox2, checkbtn_gpg_warning,
143                         _("Display warning on startup if GnuPG doesn't work"));
144
145         config = prefs_gpg_get_config();
146
147         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_auto_check_signatures), config->auto_check_signatures);
148         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_store_passphrase), config->store_passphrase);
149         gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_store_passphrase), (float) config->store_passphrase_timeout);
150         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_passphrase_grab), config->passphrase_grab);
151         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_gpg_warning), config->gpg_warning);
152
153         page->checkbtn_auto_check_signatures = checkbtn_auto_check_signatures;
154         page->checkbtn_store_passphrase = checkbtn_store_passphrase;
155         page->spinbtn_store_passphrase = spinbtn_store_passphrase;
156         page->checkbtn_passphrase_grab = checkbtn_passphrase_grab;
157         page->checkbtn_gpg_warning = checkbtn_gpg_warning;
158
159         page->page.widget = vbox1;
160 }
161
162 static void prefs_gpg_destroy_widget_func(PrefsPage *_page)
163 {
164 }
165
166 static void prefs_gpg_save_func(PrefsPage *_page)
167 {
168         struct GPGPage *page = (struct GPGPage *) _page;
169         GPGConfig *config = prefs_gpg_get_config();
170
171         config->auto_check_signatures =
172                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_auto_check_signatures));
173         config->store_passphrase = 
174                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_store_passphrase));
175         config->store_passphrase_timeout = 
176                 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_store_passphrase));
177         config->passphrase_grab = 
178                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_passphrase_grab));
179         config->gpg_warning = 
180                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_gpg_warning));
181
182         prefs_gpg_save_config();
183 }
184
185 struct GPGAccountPage
186 {
187         PrefsPage page;
188
189         GtkWidget *key_default;
190         GtkWidget *key_by_from;
191         GtkWidget *key_custom;
192         GtkWidget *keyid;
193         GtkWidget *keyid_label;
194         GtkWidget *new_key_label;
195         GtkWidget *new_key_btn;
196         GtkWidget *new_key_box;
197
198         PrefsAccount *account;
199 };
200
201 void key_custom_toggled(GtkToggleButton *togglebutton, gpointer user_data)
202 {
203         struct GPGAccountPage *page = (struct GPGAccountPage *) user_data;
204         gboolean active;
205
206         active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom));
207         gtk_widget_set_sensitive(GTK_WIDGET(page->keyid_label), active);
208         gtk_widget_set_sensitive(GTK_WIDGET(page->keyid), active);
209         if (!active)
210                 gtk_editable_delete_text(GTK_EDITABLE(page->keyid), 0, -1);
211 }
212
213 static void prefs_gpg_update_sens(struct GPGAccountPage *page)
214 {
215         gboolean active;
216         active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom));
217         if (sgpgme_has_secret_key()) {
218                 gtk_widget_hide(page->new_key_box);
219                 gtk_widget_set_sensitive(page->key_default, TRUE);
220                 gtk_widget_set_sensitive(page->key_by_from, TRUE);
221                 gtk_widget_set_sensitive(page->key_custom, TRUE);
222                 gtk_widget_set_sensitive(page->keyid, active);
223                 gtk_widget_set_sensitive(page->keyid_label, active);
224         } else {
225                 gtk_widget_show(page->new_key_box);
226                 gtk_widget_set_sensitive(page->key_default, FALSE);
227                 gtk_widget_set_sensitive(page->key_by_from, FALSE);
228                 gtk_widget_set_sensitive(page->key_custom, FALSE);
229                 gtk_widget_set_sensitive(page->keyid, FALSE);
230                 gtk_widget_set_sensitive(page->keyid_label, FALSE);
231         }
232 }
233
234 static void new_key_clicked(GtkWidget *widget, gpointer user_data)
235 {
236         struct GPGAccountPage *page = (struct GPGAccountPage *) user_data;
237         sgpgme_create_secret_key(page->account, FALSE);
238         prefs_gpg_update_sens(page);
239 }
240
241 static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
242                                                  GtkWindow *window,
243                                                  gpointer data)
244 {
245         struct GPGAccountPage *page = (struct GPGAccountPage *) _page;
246         PrefsAccount *account = (PrefsAccount *) data;
247         GPGAccountConfig *config;
248
249         GtkWidget *vbox;
250         GtkWidget *frame1;
251         GtkWidget *vbox2;
252         GtkWidget *hbox;
253         GSList *key_group = NULL;
254         GtkWidget *key_default;
255         GtkWidget *key_by_from;
256         GtkWidget *key_custom;
257         GtkWidget *keyid_label;
258         GtkWidget *keyid;
259         GtkWidget *image;
260         GtkWidget *new_key_label;
261         GtkWidget *new_key_btn;
262         GtkWidget *new_key_box;
263
264         vbox = gtk_vbox_new(FALSE, VSPACING);
265         gtk_container_set_border_width (GTK_CONTAINER (vbox), VBOX_BORDER);
266         gtk_widget_show(vbox);
267
268         vbox2 = gtkut_get_options_frame(vbox, &frame1, _("Sign key"));
269
270         hbox = gtk_hbox_new (FALSE, 5);
271         gtk_widget_show (hbox);
272         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
273         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
274
275         key_default = gtk_radio_button_new_with_label(key_group,
276                         _("Use default GnuPG key"));
277         key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_default));
278         gtk_widget_show(key_default);
279         gtk_box_pack_start(GTK_BOX(hbox), key_default, FALSE, FALSE, 0);
280
281         hbox = gtk_hbox_new (FALSE, 5);
282         gtk_widget_show (hbox);
283         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
284         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
285
286         key_by_from = gtk_radio_button_new_with_label(key_group,
287                 _("Select key by your email address"));
288         key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_by_from));
289         gtk_widget_show(key_by_from);
290         gtk_box_pack_start(GTK_BOX(hbox), key_by_from, FALSE, FALSE, 0);
291
292         hbox = gtk_hbox_new (FALSE, 5);
293         gtk_widget_show (hbox);
294         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
295         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
296
297         key_custom = gtk_radio_button_new_with_label(key_group,
298                 _("Specify key manually"));
299         key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_custom));
300         gtk_widget_show(key_custom);
301         gtk_box_pack_start(GTK_BOX(hbox), key_custom, FALSE, FALSE, 0);
302
303         hbox = gtk_hbox_new (FALSE, 5);
304         gtk_widget_show (hbox);
305         gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
306         gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
307
308         keyid_label = gtk_label_new(_("User or key ID:"));
309         gtk_widget_show(keyid_label);
310         gtk_label_set_justify(GTK_LABEL(keyid_label), GTK_JUSTIFY_LEFT);
311         gtk_box_pack_start(GTK_BOX(hbox), keyid_label, FALSE, FALSE, 0);
312
313         keyid = gtk_entry_new();
314         gtk_widget_show(keyid);
315         gtk_box_pack_start(GTK_BOX(hbox), keyid, FALSE, FALSE, 0);
316
317         config = prefs_gpg_account_get_config(account);
318         switch (config->sign_key) {
319         case SIGN_KEY_DEFAULT:
320                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_default), TRUE);
321                 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), FALSE);
322                 gtk_widget_set_sensitive(GTK_WIDGET(keyid), FALSE);
323                 break;
324         case SIGN_KEY_BY_FROM:
325                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_by_from), TRUE);
326                 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), FALSE);
327                 gtk_widget_set_sensitive(GTK_WIDGET(keyid), FALSE);
328                 break;
329         case SIGN_KEY_CUSTOM:
330                 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_custom), TRUE);
331                 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), TRUE);
332                 gtk_widget_set_sensitive(GTK_WIDGET(keyid), TRUE);
333                 break;
334         }
335
336         hbox = gtk_hbox_new (FALSE, 5);
337         gtk_widget_show (hbox);
338         gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
339
340         new_key_box = gtk_hbox_new(FALSE, 6);
341         gtk_widget_show(new_key_box);
342         gtk_box_pack_start(GTK_BOX(hbox), new_key_box, FALSE, FALSE, 0);
343
344         image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING,
345                         GTK_ICON_SIZE_SMALL_TOOLBAR);
346
347         gtk_box_pack_start(GTK_BOX(new_key_box), image, FALSE, FALSE, 0);
348         new_key_label = gtk_label_new(
349                         _("No secret key found."));
350         gtk_box_pack_start(GTK_BOX(new_key_box), new_key_label, FALSE, FALSE, 0);
351
352         new_key_btn = gtk_button_new_with_label(_("Generate a new key pair"));
353         gtk_widget_show(new_key_btn);
354         gtk_box_pack_start(GTK_BOX(hbox), new_key_btn, FALSE, FALSE, 0);
355
356         if (config->sign_key_id != NULL)
357                 gtk_entry_set_text(GTK_ENTRY(keyid), config->sign_key_id);
358
359         g_signal_connect(G_OBJECT(key_custom), "toggled", G_CALLBACK(key_custom_toggled), page);
360         g_signal_connect(G_OBJECT(new_key_btn), "clicked", G_CALLBACK(new_key_clicked), page);
361
362         page->key_default = key_default;
363         page->key_by_from = key_by_from;
364         page->key_custom = key_custom;
365         page->keyid = keyid;
366         page->keyid_label = keyid_label;
367         page->new_key_box = new_key_box;
368
369         page->page.widget = vbox;
370         page->account = account;
371         prefs_gpg_update_sens(page);
372 }
373
374 static void prefs_gpg_account_destroy_widget_func(PrefsPage *_page)
375 {
376         /* nothing to do here */
377 }
378
379 static void prefs_gpg_account_save_func(PrefsPage *_page)
380 {
381         struct GPGAccountPage *page = (struct GPGAccountPage *) _page;
382         GPGAccountConfig *config;
383
384         config = prefs_gpg_account_get_config(page->account);
385
386         if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_default)))
387                 config->sign_key = SIGN_KEY_DEFAULT;
388         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_by_from)))
389                 config->sign_key = SIGN_KEY_BY_FROM;
390         else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom))) {
391                 config->sign_key = SIGN_KEY_CUSTOM;
392                 g_free(config->sign_key_id);
393                 config->sign_key_id = gtk_editable_get_chars(GTK_EDITABLE(page->keyid), 0, -1);
394         }
395
396         prefs_gpg_account_set_config(page->account, config);
397         prefs_gpg_account_free_config(config);
398 }
399
400 GPGConfig *prefs_gpg_get_config(void)
401 {
402         return &prefs_gpg;
403 }
404
405 void prefs_gpg_save_config(void)
406 {
407         PrefFile *pfile;
408         gchar *rcpath;
409
410         debug_print("Saving GPG config\n");
411
412         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
413         pfile = prefs_write_open(rcpath);
414         g_free(rcpath);
415         if (!pfile || (prefs_set_block_label(pfile, "GPG") < 0))
416                 return;
417
418         if (prefs_write_param(param, pfile->fp) < 0) {
419                 g_warning("failed to write GPG configuration to file\n");
420                 prefs_file_close_revert(pfile);
421                 return;
422         }
423         fprintf(pfile->fp, "\n");
424
425         prefs_file_close(pfile);
426 }
427
428 struct GPGAccountConfig *prefs_gpg_account_get_config(PrefsAccount *account)
429 {
430         GPGAccountConfig *config;
431         const gchar *confstr;
432         gchar **strv;
433
434         config = g_new0(GPGAccountConfig, 1);
435         config->sign_key = SIGN_KEY_DEFAULT;
436         config->sign_key_id = NULL;
437
438         confstr = prefs_account_get_privacy_prefs(account, "gpg");
439         if (confstr == NULL)
440                 return config;
441
442         strv = g_strsplit(confstr, ";", 0);
443         if (strv[0] != NULL) {
444                 if (!strcmp(strv[0], "DEFAULT"))
445                         config->sign_key = SIGN_KEY_DEFAULT;
446                 if (!strcmp(strv[0], "BY_FROM"))
447                         config->sign_key = SIGN_KEY_BY_FROM;
448                 if (!strcmp(strv[0], "CUSTOM")) {
449                         if (strv[1] != NULL) {
450                                 config->sign_key = SIGN_KEY_CUSTOM;
451                                 config->sign_key_id = g_strdup(strv[1]);
452                         } else
453                                 config->sign_key = SIGN_KEY_DEFAULT;
454                 }
455         }
456         g_strfreev(strv);
457
458         return config;
459 }
460
461 void prefs_gpg_account_set_config(PrefsAccount *account, GPGAccountConfig *config)
462 {
463         gchar *confstr = NULL;
464
465         switch (config->sign_key) {
466         case SIGN_KEY_DEFAULT:
467                 confstr = g_strdup("DEFAULT");
468                 break;
469         case SIGN_KEY_BY_FROM:
470                 confstr = g_strdup("BY_FROM");
471                 break;
472         case SIGN_KEY_CUSTOM:
473                 confstr = g_strdup_printf("CUSTOM;%s", config->sign_key_id);
474                 break;
475         default:
476                 confstr = g_strdup("");
477                 g_warning("prefs_gpg_account_set_config: bad sign_key val\n");
478         }
479
480         prefs_account_set_privacy_prefs(account, "gpg", confstr);
481
482         g_free(confstr);
483 }
484
485 void prefs_gpg_account_free_config(GPGAccountConfig *config)
486 {
487         g_free(config->sign_key_id);
488         g_free(config);
489 }
490
491 static struct GPGPage gpg_page;
492 static struct GPGAccountPage gpg_account_page;
493
494 void prefs_gpg_init()
495 {
496         static gchar *path[3];
497         gchar *rcpath;
498
499         prefs_set_default(param);
500         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
501         prefs_read_config(param, "GPG", rcpath, NULL);
502         g_free(rcpath);
503
504         path[0] = _("Plugins");
505         path[1] = _("GPG");
506         path[2] = NULL;
507
508         gpg_page.page.path = path;
509         gpg_page.page.create_widget = prefs_gpg_create_widget_func;
510         gpg_page.page.destroy_widget = prefs_gpg_destroy_widget_func;
511         gpg_page.page.save_page = prefs_gpg_save_func;
512         gpg_page.page.weight = 30.0;
513
514         prefs_gtk_register_page((PrefsPage *) &gpg_page);
515
516         gpg_account_page.page.path = path;
517         gpg_account_page.page.create_widget = prefs_gpg_account_create_widget_func;
518         gpg_account_page.page.destroy_widget = prefs_gpg_account_destroy_widget_func;
519         gpg_account_page.page.save_page = prefs_gpg_account_save_func;
520         gpg_account_page.page.weight = 30.0;
521
522         prefs_account_register_page((PrefsPage *) &gpg_account_page);
523 }
524
525 void prefs_gpg_done()
526 {
527         prefs_gtk_unregister_page((PrefsPage *) &gpg_page);
528         prefs_account_unregister_page((PrefsPage *) &gpg_account_page);
529 }