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,
307 if (buf[0] != '\0') {
308 if (fputs(buf, fp) == EOF) {
318 void prefs_set_default(PrefParam *param)
323 g_return_if_fail(param != NULL);
325 for (i = 0; param[i].name != NULL; i++) {
326 if (!param[i].data) continue;
328 switch (param[i].type) {
331 if (param[i].defval != NULL) {
332 if (!strncasecmp(param[i].defval, "ENV_", 4))
333 *((gchar **)param[i].data) =
334 g_strdup(g_getenv(param[i].defval + 4));
335 else if (param[i].defval[0] == '~')
336 *((gchar **)param[i].data) =
337 g_strconcat(get_home_dir(),
340 else if (param[i].defval[0] != '\0')
341 *((gchar **)param[i].data) =
342 g_strdup(param[i].defval);
344 *((gchar **)param[i].data) = NULL;
346 *((gchar **)param[i].data) = NULL;
349 if (param[i].defval != NULL)
350 *((gint *)param[i].data) =
351 (gint)atoi(param[i].defval);
353 *((gint *)param[i].data) = 0;
356 if (param[i].defval != NULL) {
357 if (!strcasecmp(param[i].defval, "TRUE"))
358 *((gboolean *)param[i].data) = TRUE;
360 *((gboolean *)param[i].data) =
361 atoi(param[i].defval) ? TRUE : FALSE;
363 *((gboolean *)param[i].data) = FALSE;
366 if (param[i].defval != NULL)
367 *((DummyEnum*)param[i].data) =
368 (DummyEnum)atoi(param[i].defval);
370 *((DummyEnum *)param[i].data) = 0;
373 if (param[i].defval != NULL)
374 *((gushort *)param[i].data) =
375 (gushort)atoi(param[i].defval);
377 *((gushort *)param[i].data) = 0;
380 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
381 *((gulong *)param[i].data) =
382 RGB_FROM_GDK_COLOR(color);
383 else if (param[i].defval)
384 /* be compatible and accept ints */
385 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
387 *((gulong *)param[i].data) = 0;
395 void prefs_free(PrefParam *param)
399 g_return_if_fail(param != NULL);
401 for (i = 0; param[i].name != NULL; i++) {
402 if (!param[i].data) continue;
404 switch (param[i].type) {
407 g_free(*((gchar **)param[i].data));
415 void prefs_dialog_create(PrefsDialog *dialog)
421 GtkWidget *confirm_area;
423 GtkWidget *cancel_btn;
424 GtkWidget *apply_btn;
426 g_return_if_fail(dialog != NULL);
428 window = gtk_window_new (GTK_WINDOW_DIALOG);
429 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
430 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
431 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
432 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
434 vbox = gtk_vbox_new (FALSE, 6);
435 gtk_widget_show(vbox);
436 gtk_container_add (GTK_CONTAINER (window), vbox);
438 notebook = gtk_notebook_new ();
439 gtk_widget_show(notebook);
440 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
441 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
442 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
443 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
445 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
447 gtkut_button_set_create(&confirm_area,
449 &cancel_btn, _("Cancel"),
450 &apply_btn, _("Apply"));
451 gtk_widget_show(confirm_area);
452 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
453 gtk_widget_grab_default(ok_btn);
455 dialog->window = window;
456 dialog->notebook = notebook;
457 dialog->ok_btn = ok_btn;
458 dialog->cancel_btn = cancel_btn;
459 dialog->apply_btn = apply_btn;
462 void prefs_dialog_destroy(PrefsDialog *dialog)
464 gtk_widget_destroy(dialog->window);
465 dialog->window = NULL;
466 dialog->notebook = NULL;
467 dialog->ok_btn = NULL;
468 dialog->cancel_btn = NULL;
469 dialog->apply_btn = NULL;
472 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
476 is_active = gtk_toggle_button_get_active(toggle_btn);
477 gtk_widget_set_sensitive(widget, is_active);
480 void prefs_set_dialog(PrefParam *param)
484 for (i = 0; param[i].name != NULL; i++) {
485 if (param[i].widget_set_func)
486 param[i].widget_set_func(¶m[i]);
490 void prefs_set_data_from_dialog(PrefParam *param)
494 for (i = 0; param[i].name != NULL; i++) {
495 if (param[i].data_set_func)
496 param[i].data_set_func(¶m[i]);
500 void prefs_set_dialog_to_default(PrefParam *param)
504 gchar *str_data = NULL;
510 for (i = 0; param[i].name != NULL; i++) {
511 if (!param[i].widget_set_func) continue;
515 switch (tmpparam.type) {
518 if (tmpparam.defval) {
519 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
520 str_data = g_strdup(g_getenv(param[i].defval + 4));
521 tmpparam.data = &str_data;
523 } else if (tmpparam.defval[0] == '~') {
525 g_strconcat(get_home_dir(),
528 tmpparam.data = &str_data;
532 tmpparam.data = &tmpparam.defval;
536 int_data = atoi(tmpparam.defval);
539 tmpparam.data = &int_data;
543 ushort_data = atoi(tmpparam.defval);
546 tmpparam.data = &ushort_data;
549 if (tmpparam.defval) {
550 if (!strcasecmp(tmpparam.defval, "TRUE"))
553 bool_data = atoi(tmpparam.defval)
557 tmpparam.data = &bool_data;
561 enum_data = (DummyEnum)atoi(tmpparam.defval);
564 tmpparam.data = &enum_data;
570 tmpparam.widget_set_func(&tmpparam);
576 void prefs_set_data_from_entry(PrefParam *pparam)
578 gchar **str, *entry_str;
580 g_return_if_fail(*pparam->widget != NULL);
582 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
584 switch (pparam->type) {
587 str = (gchar **)pparam->data;
589 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
592 *((gushort *)pparam->data) = atoi(entry_str);
595 *((gint *)pparam->data) = atoi(entry_str);
598 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
603 void prefs_set_entry(PrefParam *pparam)
607 g_return_if_fail(*pparam->widget != NULL);
609 switch (pparam->type) {
612 str = (gchar **)pparam->data;
613 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
617 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
618 itos(*((gint *)pparam->data)));
621 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
622 itos(*((gushort *)pparam->data)));
625 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
630 void prefs_set_data_from_text(PrefParam *pparam)
636 g_return_if_fail(*pparam->widget != NULL);
638 switch (pparam->type) {
641 str = (gchar **)pparam->data;
643 tp = text = gtk_editable_get_chars
644 (GTK_EDITABLE(*pparam->widget), 0, -1);
645 if (text[0] == '\0') {
651 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
652 { *str = NULL; break; });
662 *str = g_strdup(tmp);
666 g_warning("Invalid PrefType for GtkText widget: %d\n",
671 void prefs_set_text(PrefParam *pparam)
673 gchar *buf, *sp, *bufp;
677 g_return_if_fail(*pparam->widget != NULL);
679 switch (pparam->type) {
682 str = (gchar **)pparam->data;
684 bufp = buf = alloca(strlen(*str) + 1);
689 if (*sp == '\\' && *(sp + 1) == 'n') {
700 text = GTK_TEXT(*pparam->widget);
701 gtk_text_set_point(text, 0);
702 gtk_text_forward_delete(text, gtk_text_get_length(text));
703 gtk_text_set_point(text, 0);
704 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
707 g_warning("Invalid PrefType for GtkText widget: %d\n",
712 void prefs_set_data_from_toggle(PrefParam *pparam)
714 g_return_if_fail(pparam->type == P_BOOL);
715 g_return_if_fail(*pparam->widget != NULL);
717 *((gboolean *)pparam->data) =
718 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
721 void prefs_set_toggle(PrefParam *pparam)
723 g_return_if_fail(pparam->type == P_BOOL);
724 g_return_if_fail(*pparam->widget != NULL);
726 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
727 *((gboolean *)pparam->data));
730 void prefs_set_data_from_spinbtn(PrefParam *pparam)
732 g_return_if_fail(*pparam->widget != NULL);
734 switch (pparam->type) {
736 *((gint *)pparam->data) =
737 gtk_spin_button_get_value_as_int
738 (GTK_SPIN_BUTTON(*pparam->widget));
741 *((gushort *)pparam->data) =
742 (gushort)gtk_spin_button_get_value_as_int
743 (GTK_SPIN_BUTTON(*pparam->widget));
746 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
751 void prefs_set_spinbtn(PrefParam *pparam)
753 g_return_if_fail(*pparam->widget != NULL);
755 switch (pparam->type) {
757 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
758 (gfloat)*((gint *)pparam->data));
761 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
762 (gfloat)*((gushort *)pparam->data));
765 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
770 static GSList *prefs_pages = NULL;
772 void prefs_gtk_open()
774 prefswindow_open(prefs_pages, NULL);
777 void prefs_gtk_register_page(PrefsPage *page)
779 prefs_pages = g_slist_append(prefs_pages, page);
782 void prefs_gtk_unregister_page(PrefsPage *page)
784 prefs_pages = g_slist_remove(prefs_pages, page);