2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2002 Hiroyuki Yamamoto
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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
34 #include "prefs_gtk.h"
37 #include "passcrypt.h"
40 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
41 #define RGB_FROM_GDK_COLOR(c) \
42 ((CL(c.red) << (gulong) 16) | \
43 (CL(c.green) << (gulong) 8) | \
51 void prefs_read_config(PrefParam *param, const gchar *label,
55 gchar buf[PREFSBUFSIZE];
59 g_return_if_fail(param != NULL);
60 g_return_if_fail(label != NULL);
61 g_return_if_fail(rcfile != NULL);
63 debug_print("Reading configuration...\n");
65 prefs_set_default(param);
67 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
68 if ((fp = fopen(rcpath, "rb")) == NULL) {
69 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
75 block_label = g_strdup_printf("[%s]", label);
77 /* search aiming block */
78 while (fgets(buf, sizeof(buf), fp) != NULL) {
81 val = strncmp(buf, block_label, strlen(block_label));
83 debug_print("Found %s\n", block_label);
89 while (fgets(buf, sizeof(buf), fp) != NULL) {
91 /* reached next block */
92 if (buf[0] == '[') break;
94 prefs_config_parse_one_line(param, buf);
97 debug_print("Finished reading configuration.\n");
101 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
108 for (i = 0; param[i].name != NULL; i++) {
109 name_len = strlen(param[i].name);
110 if (strncasecmp(buf, param[i].name, name_len))
112 if (buf[name_len] != '=')
114 value = buf + name_len + 1;
115 /* debug_print("%s = %s\n", param[i].name, value); */
117 switch (param[i].type) {
119 g_free(*((gchar **)param[i].data));
120 *((gchar **)param[i].data) =
121 *value ? g_strdup(value) : NULL;
124 *((gint *)param[i].data) =
128 *((gboolean *)param[i].data) =
129 (*value == '0' || *value == '\0')
133 *((DummyEnum *)param[i].data) =
134 (DummyEnum)atoi(value);
137 *((gushort *)param[i].data) =
138 (gushort)atoi(value);
141 if (gdk_color_parse(value, &color))
142 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
144 /* be compatible and accept ints */
145 *((gulong *)param[i].data) = strtoul(value, 0, 10);
148 g_free(*((gchar **)param[i].data));
149 if (value[0] == '!') {
153 len = base64_decode(tmp, &value[1], strlen(value) - 1);
154 passcrypt_decrypt(tmp, len);
156 *((gchar **)param[i].data) =
157 *tmp ? g_strdup(tmp) : NULL;
159 *((gchar **)param[i].data) =
160 *value ? g_strdup(value) : NULL;
172 g_warning("failed to write configuration to file\n"); \
173 if (orig_fp) fclose(orig_fp); \
174 prefs_file_close_revert(pfile); \
176 g_free(block_label); \
180 void prefs_save_config(PrefParam *param, const gchar *label,
186 gchar buf[PREFSBUFSIZE];
187 gchar *block_label = NULL;
188 gboolean block_matched = FALSE;
190 g_return_if_fail(param != NULL);
191 g_return_if_fail(label != NULL);
192 g_return_if_fail(rcfile != NULL);
194 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
195 if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
196 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
199 if ((pfile = prefs_write_open(rcpath)) == NULL) {
200 g_warning("failed to write configuration to file\n");
201 if (orig_fp) fclose(orig_fp);
206 block_label = g_strdup_printf("[%s]", label);
208 /* search aiming block */
210 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
213 val = strncmp(buf, block_label, strlen(block_label));
215 debug_print("Found %s\n", block_label);
216 block_matched = TRUE;
219 TRY(fputs(buf, pfile->fp) != EOF);
223 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
227 /* write all param data to file */
228 TRY(prefs_write_param(param, pfile->fp) == 0);
231 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
234 TRY(fputc('\n', pfile->fp) != EOF &&
235 fputs(buf, pfile->fp) != EOF);
239 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
240 TRY(fputs(buf, pfile->fp) != EOF);
243 if (orig_fp) fclose(orig_fp);
244 if (prefs_file_close(pfile) < 0)
245 g_warning("failed to write configuration to file\n");
248 debug_print("Configuration is saved.\n");
251 gint prefs_write_param(PrefParam *param, FILE *fp)
254 gchar buf[PREFSBUFSIZE];
256 for (i = 0; param[i].name != NULL; i++) {
257 switch (param[i].type) {
259 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
260 *((gchar **)param[i].data) ?
261 *((gchar **)param[i].data) : "");
264 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
265 *((gint *)param[i].data));
268 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
269 *((gboolean *)param[i].data));
272 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
273 *((DummyEnum *)param[i].data));
276 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
277 *((gushort *)param[i].data));
280 g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
281 *((gulong *) param[i].data));
285 gchar *tmp = NULL, tmp2[1024] = {0};
287 tmp = *((gchar **)param[i].data);
293 passcrypt_encrypt(tmp, len);
294 base64_encode(tmp2, tmp, len);
298 g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
304 /* unrecognized, fail */
305 debug_print("Unrecognized parameter type\n");
309 if (buf[0] != '\0') {
310 if (fputs(buf, fp) == EOF) {
320 void prefs_set_default(PrefParam *param)
325 g_return_if_fail(param != NULL);
327 for (i = 0; param[i].name != NULL; i++) {
328 if (!param[i].data) continue;
330 switch (param[i].type) {
333 g_free(*((gchar **)param[i].data));
334 if (param[i].defval != NULL) {
335 if (!strncasecmp(param[i].defval, "ENV_", 4))
336 *((gchar **)param[i].data) =
337 g_strdup(g_getenv(param[i].defval + 4));
338 else if (param[i].defval[0] == '~')
339 *((gchar **)param[i].data) =
340 g_strconcat(get_home_dir(),
343 else if (param[i].defval[0] != '\0')
344 *((gchar **)param[i].data) =
345 g_strdup(param[i].defval);
347 *((gchar **)param[i].data) = NULL;
349 *((gchar **)param[i].data) = NULL;
352 if (param[i].defval != NULL)
353 *((gint *)param[i].data) =
354 (gint)atoi(param[i].defval);
356 *((gint *)param[i].data) = 0;
359 if (param[i].defval != NULL) {
360 if (!strcasecmp(param[i].defval, "TRUE"))
361 *((gboolean *)param[i].data) = TRUE;
363 *((gboolean *)param[i].data) =
364 atoi(param[i].defval) ? TRUE : FALSE;
366 *((gboolean *)param[i].data) = FALSE;
369 if (param[i].defval != NULL)
370 *((DummyEnum*)param[i].data) =
371 (DummyEnum)atoi(param[i].defval);
373 *((DummyEnum *)param[i].data) = 0;
376 if (param[i].defval != NULL)
377 *((gushort *)param[i].data) =
378 (gushort)atoi(param[i].defval);
380 *((gushort *)param[i].data) = 0;
383 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
384 *((gulong *)param[i].data) =
385 RGB_FROM_GDK_COLOR(color);
386 else if (param[i].defval)
387 /* be compatible and accept ints */
388 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
390 *((gulong *)param[i].data) = 0;
398 void prefs_free(PrefParam *param)
402 g_return_if_fail(param != NULL);
404 for (i = 0; param[i].name != NULL; i++) {
405 if (!param[i].data) continue;
407 switch (param[i].type) {
410 g_free(*((gchar **)param[i].data));
418 void prefs_dialog_create(PrefsDialog *dialog)
424 GtkWidget *confirm_area;
426 GtkWidget *cancel_btn;
427 GtkWidget *apply_btn;
429 g_return_if_fail(dialog != NULL);
431 window = gtk_window_new (GTK_WINDOW_DIALOG);
432 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
433 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
434 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
435 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
437 vbox = gtk_vbox_new (FALSE, 6);
438 gtk_widget_show(vbox);
439 gtk_container_add (GTK_CONTAINER (window), vbox);
441 notebook = gtk_notebook_new ();
442 gtk_widget_show(notebook);
443 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
444 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
445 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
446 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
448 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
450 gtkut_button_set_create(&confirm_area,
452 &cancel_btn, _("Cancel"),
453 &apply_btn, _("Apply"));
454 gtk_widget_show(confirm_area);
455 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
456 gtk_widget_grab_default(ok_btn);
458 dialog->window = window;
459 dialog->notebook = notebook;
460 dialog->ok_btn = ok_btn;
461 dialog->cancel_btn = cancel_btn;
462 dialog->apply_btn = apply_btn;
465 void prefs_dialog_destroy(PrefsDialog *dialog)
467 gtk_widget_destroy(dialog->window);
468 dialog->window = NULL;
469 dialog->notebook = NULL;
470 dialog->ok_btn = NULL;
471 dialog->cancel_btn = NULL;
472 dialog->apply_btn = NULL;
475 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
479 is_active = gtk_toggle_button_get_active(toggle_btn);
480 gtk_widget_set_sensitive(widget, is_active);
483 void prefs_set_dialog(PrefParam *param)
487 for (i = 0; param[i].name != NULL; i++) {
488 if (param[i].widget_set_func)
489 param[i].widget_set_func(¶m[i]);
493 void prefs_set_data_from_dialog(PrefParam *param)
497 for (i = 0; param[i].name != NULL; i++) {
498 if (param[i].data_set_func)
499 param[i].data_set_func(¶m[i]);
503 void prefs_set_dialog_to_default(PrefParam *param)
507 gchar *str_data = NULL;
513 for (i = 0; param[i].name != NULL; i++) {
514 if (!param[i].widget_set_func) continue;
518 switch (tmpparam.type) {
521 if (tmpparam.defval) {
522 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
523 str_data = g_strdup(g_getenv(param[i].defval + 4));
524 tmpparam.data = &str_data;
526 } else if (tmpparam.defval[0] == '~') {
528 g_strconcat(get_home_dir(),
531 tmpparam.data = &str_data;
535 tmpparam.data = &tmpparam.defval;
539 int_data = atoi(tmpparam.defval);
542 tmpparam.data = &int_data;
546 ushort_data = atoi(tmpparam.defval);
549 tmpparam.data = &ushort_data;
552 if (tmpparam.defval) {
553 if (!strcasecmp(tmpparam.defval, "TRUE"))
556 bool_data = atoi(tmpparam.defval)
560 tmpparam.data = &bool_data;
564 enum_data = (DummyEnum)atoi(tmpparam.defval);
567 tmpparam.data = &enum_data;
573 tmpparam.widget_set_func(&tmpparam);
579 void prefs_set_data_from_entry(PrefParam *pparam)
581 gchar **str, *entry_str;
583 g_return_if_fail(*pparam->widget != NULL);
585 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
587 switch (pparam->type) {
590 str = (gchar **)pparam->data;
592 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
595 *((gushort *)pparam->data) = atoi(entry_str);
598 *((gint *)pparam->data) = atoi(entry_str);
601 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
606 void prefs_set_entry(PrefParam *pparam)
610 g_return_if_fail(*pparam->widget != NULL);
612 switch (pparam->type) {
615 str = (gchar **)pparam->data;
616 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
620 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
621 itos(*((gint *)pparam->data)));
624 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
625 itos(*((gushort *)pparam->data)));
628 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
633 void prefs_set_data_from_text(PrefParam *pparam)
639 g_return_if_fail(*pparam->widget != NULL);
641 switch (pparam->type) {
644 str = (gchar **)pparam->data;
646 tp = text = gtk_editable_get_chars
647 (GTK_EDITABLE(*pparam->widget), 0, -1);
648 if (text[0] == '\0') {
654 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
655 { *str = NULL; break; });
665 *str = g_strdup(tmp);
669 g_warning("Invalid PrefType for GtkText widget: %d\n",
674 void prefs_set_text(PrefParam *pparam)
676 gchar *buf, *sp, *bufp;
680 g_return_if_fail(*pparam->widget != NULL);
682 switch (pparam->type) {
685 str = (gchar **)pparam->data;
687 bufp = buf = alloca(strlen(*str) + 1);
692 if (*sp == '\\' && *(sp + 1) == 'n') {
703 text = GTK_TEXT(*pparam->widget);
704 gtk_text_set_point(text, 0);
705 gtk_text_forward_delete(text, gtk_text_get_length(text));
706 gtk_text_set_point(text, 0);
707 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
710 g_warning("Invalid PrefType for GtkText widget: %d\n",
715 void prefs_set_data_from_toggle(PrefParam *pparam)
717 g_return_if_fail(pparam->type == P_BOOL);
718 g_return_if_fail(*pparam->widget != NULL);
720 *((gboolean *)pparam->data) =
721 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
724 void prefs_set_toggle(PrefParam *pparam)
726 g_return_if_fail(pparam->type == P_BOOL);
727 g_return_if_fail(*pparam->widget != NULL);
729 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
730 *((gboolean *)pparam->data));
733 void prefs_set_data_from_spinbtn(PrefParam *pparam)
735 g_return_if_fail(*pparam->widget != NULL);
737 switch (pparam->type) {
739 *((gint *)pparam->data) =
740 gtk_spin_button_get_value_as_int
741 (GTK_SPIN_BUTTON(*pparam->widget));
744 *((gushort *)pparam->data) =
745 (gushort)gtk_spin_button_get_value_as_int
746 (GTK_SPIN_BUTTON(*pparam->widget));
749 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
754 void prefs_set_spinbtn(PrefParam *pparam)
756 g_return_if_fail(*pparam->widget != NULL);
758 switch (pparam->type) {
760 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
761 (gfloat)*((gint *)pparam->data));
764 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
765 (gfloat)*((gushort *)pparam->data));
768 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
773 static GSList *prefs_pages = NULL;
775 void prefs_gtk_open(void)
777 prefswindow_open(_("Preferences"), prefs_pages, NULL);
780 void prefs_gtk_register_page(PrefsPage *page)
782 prefs_pages = g_slist_append(prefs_pages, page);
785 void prefs_gtk_unregister_page(PrefsPage *page)
787 prefs_pages = g_slist_remove(prefs_pages, page);