2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 2004-2007 Hiroyuki Yamamoto & the Claws Mail team
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.
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.
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.
22 #include <glib/gi18n.h>
26 #include "gtk/gtkutils.h"
28 #include "prefs_gtk.h"
29 #include "prefs_gpg.h"
32 struct GPGConfig prefs_gpg;
34 static PrefParam param[] = {
36 {"auto_check_signatures", "FALSE",
37 &prefs_gpg.auto_check_signatures, P_BOOL,
39 {"store_passphrase", "FALSE", &prefs_gpg.store_passphrase, P_BOOL,
41 {"store_passphrase_timeout", "0",
42 &prefs_gpg.store_passphrase_timeout, P_INT,
44 {"passphrase_grab", "FALSE", &prefs_gpg.passphrase_grab, P_BOOL,
46 {"gpg_warning", "TRUE", &prefs_gpg.gpg_warning, P_BOOL,
48 {"gpg_ask_create_key", "TRUE", &prefs_gpg.gpg_ask_create_key, P_BOOL,
51 {NULL, NULL, NULL, P_OTHER, NULL, NULL, NULL}
58 GtkWidget *checkbtn_auto_check_signatures;
59 GtkWidget *checkbtn_store_passphrase;
60 GtkWidget *spinbtn_store_passphrase;
61 GtkWidget *checkbtn_passphrase_grab;
62 GtkWidget *checkbtn_gpg_warning;
65 static void prefs_gpg_create_widget_func(PrefsPage *_page,
69 struct GPGPage *page = (struct GPGPage *) _page;
70 struct GPGConfig *config;
72 GtkWidget *checkbtn_passphrase_grab;
73 GtkWidget *checkbtn_store_passphrase;
74 GtkWidget *checkbtn_auto_check_signatures;
75 GtkWidget *checkbtn_gpg_warning;
77 GtkWidget *vbox1, *vbox2;
78 GtkWidget *label_expire1;
79 GtkObject *spinbtn_store_passphrase_adj;
80 GtkWidget *spinbtn_store_passphrase;
81 GtkWidget *label_expire2;
82 GtkWidget *frame_passphrase;
83 GtkTooltips *tooltips;
85 tooltips = gtk_tooltips_new();
87 vbox1 = gtk_vbox_new (FALSE, VSPACING);
88 gtk_widget_show (vbox1);
89 gtk_container_set_border_width (GTK_CONTAINER (vbox1), VBOX_BORDER);
91 vbox2 = gtk_vbox_new (FALSE, 0);
92 gtk_widget_show (vbox2);
93 gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
95 PACK_CHECK_BUTTON (vbox2, checkbtn_auto_check_signatures,
96 _("Automatically check signatures"));
98 vbox2 = gtkut_get_options_frame(vbox1, &frame_passphrase, _("Passphrase"));
100 PACK_CHECK_BUTTON (vbox2, checkbtn_store_passphrase,
101 _("Store passphrase in memory"));
103 hbox1 = gtk_hbox_new (FALSE, 8);
104 gtk_widget_show (hbox1);
105 gtk_box_pack_start (GTK_BOX (vbox2), hbox1, FALSE, FALSE, 0);
107 label_expire1 = gtk_label_new(_("Expire after"));
108 gtk_widget_show (label_expire1);
109 gtk_box_pack_start (GTK_BOX (hbox1), label_expire1, FALSE, FALSE, 0);
111 spinbtn_store_passphrase_adj =
112 gtk_adjustment_new(1, 0, 1440, 1, 10, 10);
113 spinbtn_store_passphrase =
114 gtk_spin_button_new(GTK_ADJUSTMENT
115 (spinbtn_store_passphrase_adj), 1, 0);
116 gtk_widget_show(spinbtn_store_passphrase);
117 gtk_box_pack_start(GTK_BOX(hbox1), spinbtn_store_passphrase, FALSE,
119 gtk_widget_set_size_request(spinbtn_store_passphrase, 64, -1);
120 gtk_tooltips_set_tip(tooltips, spinbtn_store_passphrase,
122 ("Setting to '0' will store the passphrase for the whole session"),
124 gtk_spin_button_set_numeric(GTK_SPIN_BUTTON
125 (spinbtn_store_passphrase), TRUE);
127 label_expire2 = gtk_label_new(_("minute(s)"));
128 gtk_widget_show(label_expire2);
129 gtk_box_pack_start(GTK_BOX(hbox1), label_expire2, FALSE, FALSE, 0);
130 gtk_misc_set_alignment(GTK_MISC(label_expire2), 0.0, 0.5);
132 SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, label_expire1);
133 SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, spinbtn_store_passphrase);
134 SET_TOGGLE_SENSITIVITY (checkbtn_store_passphrase, label_expire2);
136 PACK_CHECK_BUTTON (vbox2, checkbtn_passphrase_grab,
137 _("Grab input while entering a passphrase"));
139 vbox2 = gtk_vbox_new (FALSE, 0);
140 gtk_widget_show (vbox2);
141 gtk_box_pack_start (GTK_BOX (vbox1), vbox2, FALSE, FALSE, 0);
143 PACK_CHECK_BUTTON (vbox2, checkbtn_gpg_warning,
144 _("Display warning on startup if GnuPG doesn't work"));
146 config = prefs_gpg_get_config();
148 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_auto_check_signatures), config->auto_check_signatures);
149 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_store_passphrase), config->store_passphrase);
150 gtk_spin_button_set_value(GTK_SPIN_BUTTON(spinbtn_store_passphrase), (float) config->store_passphrase_timeout);
151 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_passphrase_grab), config->passphrase_grab);
152 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(checkbtn_gpg_warning), config->gpg_warning);
154 page->checkbtn_auto_check_signatures = checkbtn_auto_check_signatures;
155 page->checkbtn_store_passphrase = checkbtn_store_passphrase;
156 page->spinbtn_store_passphrase = spinbtn_store_passphrase;
157 page->checkbtn_passphrase_grab = checkbtn_passphrase_grab;
158 page->checkbtn_gpg_warning = checkbtn_gpg_warning;
160 page->page.widget = vbox1;
163 static void prefs_gpg_destroy_widget_func(PrefsPage *_page)
167 static void prefs_gpg_save_func(PrefsPage *_page)
169 struct GPGPage *page = (struct GPGPage *) _page;
170 GPGConfig *config = prefs_gpg_get_config();
172 config->auto_check_signatures =
173 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_auto_check_signatures));
174 config->store_passphrase =
175 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_store_passphrase));
176 config->store_passphrase_timeout =
177 gtk_spin_button_get_value_as_int(GTK_SPIN_BUTTON(page->spinbtn_store_passphrase));
178 config->passphrase_grab =
179 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_passphrase_grab));
180 config->gpg_warning =
181 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->checkbtn_gpg_warning));
183 prefs_gpg_save_config();
186 struct GPGAccountPage
190 GtkWidget *key_default;
191 GtkWidget *key_by_from;
192 GtkWidget *key_custom;
194 GtkWidget *keyid_label;
195 GtkWidget *new_key_label;
196 GtkWidget *new_key_btn;
197 GtkWidget *new_key_box;
199 PrefsAccount *account;
202 void key_custom_toggled(GtkToggleButton *togglebutton, gpointer user_data)
204 struct GPGAccountPage *page = (struct GPGAccountPage *) user_data;
207 active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom));
208 gtk_widget_set_sensitive(GTK_WIDGET(page->keyid_label), active);
209 gtk_widget_set_sensitive(GTK_WIDGET(page->keyid), active);
211 gtk_editable_delete_text(GTK_EDITABLE(page->keyid), 0, -1);
214 static void prefs_gpg_update_sens(struct GPGAccountPage *page)
217 active = gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom));
218 if (sgpgme_has_secret_key()) {
219 gtk_widget_hide(page->new_key_box);
220 gtk_widget_set_sensitive(page->key_default, TRUE);
221 gtk_widget_set_sensitive(page->key_by_from, TRUE);
222 gtk_widget_set_sensitive(page->key_custom, TRUE);
223 gtk_widget_set_sensitive(page->keyid, active);
224 gtk_widget_set_sensitive(page->keyid_label, active);
226 gtk_widget_show(page->new_key_box);
227 gtk_widget_set_sensitive(page->key_default, FALSE);
228 gtk_widget_set_sensitive(page->key_by_from, FALSE);
229 gtk_widget_set_sensitive(page->key_custom, FALSE);
230 gtk_widget_set_sensitive(page->keyid, FALSE);
231 gtk_widget_set_sensitive(page->keyid_label, FALSE);
235 static void new_key_clicked(GtkWidget *widget, gpointer user_data)
237 struct GPGAccountPage *page = (struct GPGAccountPage *) user_data;
238 sgpgme_create_secret_key(page->account, FALSE);
239 prefs_gpg_update_sens(page);
242 static void prefs_gpg_account_create_widget_func(PrefsPage *_page,
246 struct GPGAccountPage *page = (struct GPGAccountPage *) _page;
247 PrefsAccount *account = (PrefsAccount *) data;
248 GPGAccountConfig *config;
254 GSList *key_group = NULL;
255 GtkWidget *key_default;
256 GtkWidget *key_by_from;
257 GtkWidget *key_custom;
258 GtkWidget *keyid_label;
261 GtkWidget *new_key_label;
262 GtkWidget *new_key_btn;
263 GtkWidget *new_key_box;
265 vbox = gtk_vbox_new(FALSE, VSPACING);
266 gtk_container_set_border_width (GTK_CONTAINER (vbox), VBOX_BORDER);
267 gtk_widget_show(vbox);
269 vbox2 = gtkut_get_options_frame(vbox, &frame1, _("Sign key"));
271 hbox = gtk_hbox_new (FALSE, 5);
272 gtk_widget_show (hbox);
273 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
274 gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
276 key_default = gtk_radio_button_new_with_label(key_group,
277 _("Use default GnuPG key"));
278 key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_default));
279 gtk_widget_show(key_default);
280 gtk_box_pack_start(GTK_BOX(hbox), key_default, FALSE, FALSE, 0);
282 hbox = gtk_hbox_new (FALSE, 5);
283 gtk_widget_show (hbox);
284 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
285 gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
287 key_by_from = gtk_radio_button_new_with_label(key_group,
288 _("Select key by your email address"));
289 key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_by_from));
290 gtk_widget_show(key_by_from);
291 gtk_box_pack_start(GTK_BOX(hbox), key_by_from, FALSE, FALSE, 0);
293 hbox = gtk_hbox_new (FALSE, 5);
294 gtk_widget_show (hbox);
295 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
296 gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
298 key_custom = gtk_radio_button_new_with_label(key_group,
299 _("Specify key manually"));
300 key_group = gtk_radio_button_get_group(GTK_RADIO_BUTTON(key_custom));
301 gtk_widget_show(key_custom);
302 gtk_box_pack_start(GTK_BOX(hbox), key_custom, FALSE, FALSE, 0);
304 hbox = gtk_hbox_new (FALSE, 5);
305 gtk_widget_show (hbox);
306 gtk_box_pack_start (GTK_BOX (vbox2), hbox, FALSE, FALSE, 0);
307 gtk_container_set_border_width (GTK_CONTAINER (hbox), 0);
309 keyid_label = gtk_label_new(_("User or key ID:"));
310 gtk_widget_show(keyid_label);
311 gtk_label_set_justify(GTK_LABEL(keyid_label), GTK_JUSTIFY_LEFT);
312 gtk_box_pack_start(GTK_BOX(hbox), keyid_label, FALSE, FALSE, 0);
314 keyid = gtk_entry_new();
315 gtk_widget_show(keyid);
316 gtk_box_pack_start(GTK_BOX(hbox), keyid, FALSE, FALSE, 0);
318 config = prefs_gpg_account_get_config(account);
319 switch (config->sign_key) {
320 case SIGN_KEY_DEFAULT:
321 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_default), TRUE);
322 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), FALSE);
323 gtk_widget_set_sensitive(GTK_WIDGET(keyid), FALSE);
325 case SIGN_KEY_BY_FROM:
326 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_by_from), TRUE);
327 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), FALSE);
328 gtk_widget_set_sensitive(GTK_WIDGET(keyid), FALSE);
330 case SIGN_KEY_CUSTOM:
331 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(key_custom), TRUE);
332 gtk_widget_set_sensitive(GTK_WIDGET(keyid_label), TRUE);
333 gtk_widget_set_sensitive(GTK_WIDGET(keyid), TRUE);
337 hbox = gtk_hbox_new (FALSE, 5);
338 gtk_widget_show (hbox);
339 gtk_box_pack_start (GTK_BOX (vbox), hbox, FALSE, FALSE, 0);
341 new_key_box = gtk_hbox_new(FALSE, 6);
342 gtk_widget_show(new_key_box);
343 gtk_box_pack_start(GTK_BOX(hbox), new_key_box, FALSE, FALSE, 0);
345 image = gtk_image_new_from_stock(GTK_STOCK_DIALOG_WARNING,
346 GTK_ICON_SIZE_SMALL_TOOLBAR);
348 gtk_box_pack_start(GTK_BOX(new_key_box), image, FALSE, FALSE, 0);
349 new_key_label = gtk_label_new(
350 _("No secret key found."));
351 gtk_box_pack_start(GTK_BOX(new_key_box), new_key_label, FALSE, FALSE, 0);
353 new_key_btn = gtk_button_new_with_label(_("Generate a new key pair"));
354 gtk_widget_show(new_key_btn);
355 gtk_box_pack_start(GTK_BOX(hbox), new_key_btn, FALSE, FALSE, 0);
357 if (config->sign_key_id != NULL)
358 gtk_entry_set_text(GTK_ENTRY(keyid), config->sign_key_id);
360 g_signal_connect(G_OBJECT(key_custom), "toggled", G_CALLBACK(key_custom_toggled), page);
361 g_signal_connect(G_OBJECT(new_key_btn), "clicked", G_CALLBACK(new_key_clicked), page);
363 page->key_default = key_default;
364 page->key_by_from = key_by_from;
365 page->key_custom = key_custom;
367 page->keyid_label = keyid_label;
368 page->new_key_box = new_key_box;
370 page->page.widget = vbox;
371 page->account = account;
372 prefs_gpg_update_sens(page);
375 static void prefs_gpg_account_destroy_widget_func(PrefsPage *_page)
377 /* nothing to do here */
380 static void prefs_gpg_account_save_func(PrefsPage *_page)
382 struct GPGAccountPage *page = (struct GPGAccountPage *) _page;
383 GPGAccountConfig *config;
385 config = prefs_gpg_account_get_config(page->account);
387 if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_default)))
388 config->sign_key = SIGN_KEY_DEFAULT;
389 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_by_from)))
390 config->sign_key = SIGN_KEY_BY_FROM;
391 else if (gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(page->key_custom))) {
392 config->sign_key = SIGN_KEY_CUSTOM;
393 g_free(config->sign_key_id);
394 config->sign_key_id = gtk_editable_get_chars(GTK_EDITABLE(page->keyid), 0, -1);
397 prefs_gpg_account_set_config(page->account, config);
398 prefs_gpg_account_free_config(config);
401 GPGConfig *prefs_gpg_get_config(void)
406 void prefs_gpg_save_config(void)
411 debug_print("Saving GPG config\n");
413 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
414 pfile = prefs_write_open(rcpath);
416 if (!pfile || (prefs_set_block_label(pfile, "GPG") < 0))
419 if (prefs_write_param(param, pfile->fp) < 0) {
420 g_warning("failed to write GPG configuration to file\n");
421 prefs_file_close_revert(pfile);
424 fprintf(pfile->fp, "\n");
426 prefs_file_close(pfile);
429 struct GPGAccountConfig *prefs_gpg_account_get_config(PrefsAccount *account)
431 GPGAccountConfig *config;
432 const gchar *confstr;
435 config = g_new0(GPGAccountConfig, 1);
436 config->sign_key = SIGN_KEY_DEFAULT;
437 config->sign_key_id = NULL;
439 confstr = prefs_account_get_privacy_prefs(account, "gpg");
443 strv = g_strsplit(confstr, ";", 0);
444 if (strv[0] != NULL) {
445 if (!strcmp(strv[0], "DEFAULT"))
446 config->sign_key = SIGN_KEY_DEFAULT;
447 if (!strcmp(strv[0], "BY_FROM"))
448 config->sign_key = SIGN_KEY_BY_FROM;
449 if (!strcmp(strv[0], "CUSTOM")) {
450 if (strv[1] != NULL) {
451 config->sign_key = SIGN_KEY_CUSTOM;
452 config->sign_key_id = g_strdup(strv[1]);
454 config->sign_key = SIGN_KEY_DEFAULT;
462 void prefs_gpg_account_set_config(PrefsAccount *account, GPGAccountConfig *config)
464 gchar *confstr = NULL;
466 switch (config->sign_key) {
467 case SIGN_KEY_DEFAULT:
468 confstr = g_strdup("DEFAULT");
470 case SIGN_KEY_BY_FROM:
471 confstr = g_strdup("BY_FROM");
473 case SIGN_KEY_CUSTOM:
474 confstr = g_strdup_printf("CUSTOM;%s", config->sign_key_id);
477 confstr = g_strdup("");
478 g_warning("prefs_gpg_account_set_config: bad sign_key val\n");
481 prefs_account_set_privacy_prefs(account, "gpg", confstr);
486 void prefs_gpg_account_free_config(GPGAccountConfig *config)
488 g_free(config->sign_key_id);
492 static struct GPGPage gpg_page;
493 static struct GPGAccountPage gpg_account_page;
495 void prefs_gpg_init()
497 static gchar *path[3];
500 prefs_set_default(param);
501 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
502 prefs_read_config(param, "GPG", rcpath, NULL);
505 path[0] = _("Plugins");
509 gpg_page.page.path = path;
510 gpg_page.page.create_widget = prefs_gpg_create_widget_func;
511 gpg_page.page.destroy_widget = prefs_gpg_destroy_widget_func;
512 gpg_page.page.save_page = prefs_gpg_save_func;
513 gpg_page.page.weight = 30.0;
515 prefs_gtk_register_page((PrefsPage *) &gpg_page);
517 gpg_account_page.page.path = path;
518 gpg_account_page.page.create_widget = prefs_gpg_account_create_widget_func;
519 gpg_account_page.page.destroy_widget = prefs_gpg_account_destroy_widget_func;
520 gpg_account_page.page.save_page = prefs_gpg_account_save_func;
521 gpg_account_page.page.weight = 30.0;
523 prefs_account_register_page((PrefsPage *) &gpg_account_page);
526 void prefs_gpg_done()
528 prefs_gtk_unregister_page((PrefsPage *) &gpg_page);
529 prefs_account_unregister_page((PrefsPage *) &gpg_account_page);