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"
38 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
39 #define RGB_FROM_GDK_COLOR(c) \
40 ((CL(c.red) << (gulong) 16) | \
41 (CL(c.green) << (gulong) 8) | \
49 void prefs_read_config(PrefParam *param, const gchar *label,
53 gchar buf[PREFSBUFSIZE];
57 g_return_if_fail(param != NULL);
58 g_return_if_fail(label != NULL);
59 g_return_if_fail(rcfile != NULL);
61 debug_print("Reading configuration...\n");
63 prefs_set_default(param);
65 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
66 if ((fp = fopen(rcpath, "rb")) == NULL) {
67 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
73 block_label = g_strdup_printf("[%s]", label);
75 /* search aiming block */
76 while (fgets(buf, sizeof(buf), fp) != NULL) {
79 val = strncmp(buf, block_label, strlen(block_label));
81 debug_print("Found %s\n", block_label);
87 while (fgets(buf, sizeof(buf), fp) != NULL) {
89 /* reached next block */
90 if (buf[0] == '[') break;
92 prefs_config_parse_one_line(param, buf);
95 debug_print("Finished reading configuration.\n");
99 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
106 for (i = 0; param[i].name != NULL; i++) {
107 name_len = strlen(param[i].name);
108 if (strncasecmp(buf, param[i].name, name_len))
110 if (buf[name_len] != '=')
112 value = buf + name_len + 1;
113 /* debug_print("%s = %s\n", param[i].name, value); */
115 switch (param[i].type) {
117 g_free(*((gchar **)param[i].data));
118 *((gchar **)param[i].data) =
119 *value ? g_strdup(value) : NULL;
122 *((gint *)param[i].data) =
126 *((gboolean *)param[i].data) =
127 (*value == '0' || *value == '\0')
131 *((DummyEnum *)param[i].data) =
132 (DummyEnum)atoi(value);
135 *((gushort *)param[i].data) =
136 (gushort)atoi(value);
139 if (gdk_color_parse(value, &color))
140 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
142 /* be compatible and accept ints */
143 *((gulong *)param[i].data) = strtoul(value, 0, 10);
154 g_warning("failed to write configuration to file\n"); \
155 if (orig_fp) fclose(orig_fp); \
156 prefs_file_close_revert(pfile); \
158 g_free(block_label); \
162 void prefs_save_config(PrefParam *param, const gchar *label,
168 gchar buf[PREFSBUFSIZE];
169 gchar *block_label = NULL;
170 gboolean block_matched = FALSE;
172 g_return_if_fail(param != NULL);
173 g_return_if_fail(label != NULL);
174 g_return_if_fail(rcfile != NULL);
176 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
177 if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
178 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
181 if ((pfile = prefs_write_open(rcpath)) == NULL) {
182 g_warning("failed to write configuration to file\n");
183 if (orig_fp) fclose(orig_fp);
188 block_label = g_strdup_printf("[%s]", label);
190 /* search aiming block */
192 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
195 val = strncmp(buf, block_label, strlen(block_label));
197 debug_print("Found %s\n", block_label);
198 block_matched = TRUE;
201 TRY(fputs(buf, pfile->fp) != EOF);
205 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
209 /* write all param data to file */
210 TRY(prefs_write_param(param, pfile->fp) == 0);
213 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
216 TRY(fputc('\n', pfile->fp) != EOF &&
217 fputs(buf, pfile->fp) != EOF);
221 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
222 TRY(fputs(buf, pfile->fp) != EOF);
225 if (orig_fp) fclose(orig_fp);
226 if (prefs_file_close(pfile) < 0)
227 g_warning("failed to write configuration to file\n");
230 debug_print("Configuration is saved.\n");
233 gint prefs_write_param(PrefParam *param, FILE *fp)
236 gchar buf[PREFSBUFSIZE];
238 for (i = 0; param[i].name != NULL; i++) {
239 switch (param[i].type) {
241 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
242 *((gchar **)param[i].data) ?
243 *((gchar **)param[i].data) : "");
246 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
247 *((gint *)param[i].data));
250 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
251 *((gboolean *)param[i].data));
254 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
255 *((DummyEnum *)param[i].data));
258 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
259 *((gushort *)param[i].data));
262 g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
263 *((gulong *) param[i].data));
269 if (buf[0] != '\0') {
270 if (fputs(buf, fp) == EOF) {
280 void prefs_set_default(PrefParam *param)
285 g_return_if_fail(param != NULL);
287 for (i = 0; param[i].name != NULL; i++) {
288 if (!param[i].data) continue;
290 switch (param[i].type) {
292 if (param[i].defval != NULL) {
293 if (!strncasecmp(param[i].defval, "ENV_", 4))
294 *((gchar **)param[i].data) =
295 g_strdup(g_getenv(param[i].defval + 4));
296 else if (param[i].defval[0] == '~')
297 *((gchar **)param[i].data) =
298 g_strconcat(get_home_dir(),
301 else if (param[i].defval[0] != '\0')
302 *((gchar **)param[i].data) =
303 g_strdup(param[i].defval);
305 *((gchar **)param[i].data) = NULL;
307 *((gchar **)param[i].data) = NULL;
310 if (param[i].defval != NULL)
311 *((gint *)param[i].data) =
312 (gint)atoi(param[i].defval);
314 *((gint *)param[i].data) = 0;
317 if (param[i].defval != NULL) {
318 if (!strcasecmp(param[i].defval, "TRUE"))
319 *((gboolean *)param[i].data) = TRUE;
321 *((gboolean *)param[i].data) =
322 atoi(param[i].defval) ? TRUE : FALSE;
324 *((gboolean *)param[i].data) = FALSE;
327 if (param[i].defval != NULL)
328 *((DummyEnum*)param[i].data) =
329 (DummyEnum)atoi(param[i].defval);
331 *((DummyEnum *)param[i].data) = 0;
334 if (param[i].defval != NULL)
335 *((gushort *)param[i].data) =
336 (gushort)atoi(param[i].defval);
338 *((gushort *)param[i].data) = 0;
341 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
342 *((gulong *)param[i].data) =
343 RGB_FROM_GDK_COLOR(color);
344 else if (param[i].defval)
345 /* be compatible and accept ints */
346 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
348 *((gulong *)param[i].data) = 0;
356 void prefs_free(PrefParam *param)
360 g_return_if_fail(param != NULL);
362 for (i = 0; param[i].name != NULL; i++) {
363 if (!param[i].data) continue;
365 switch (param[i].type) {
367 g_free(*((gchar **)param[i].data));
375 void prefs_dialog_create(PrefsDialog *dialog)
381 GtkWidget *confirm_area;
383 GtkWidget *cancel_btn;
384 GtkWidget *apply_btn;
386 g_return_if_fail(dialog != NULL);
388 window = gtk_window_new (GTK_WINDOW_DIALOG);
389 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
390 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
391 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
392 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
394 vbox = gtk_vbox_new (FALSE, 6);
395 gtk_widget_show(vbox);
396 gtk_container_add (GTK_CONTAINER (window), vbox);
398 notebook = gtk_notebook_new ();
399 gtk_widget_show(notebook);
400 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
401 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
402 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
403 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
405 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
407 gtkut_button_set_create(&confirm_area,
409 &cancel_btn, _("Cancel"),
410 &apply_btn, _("Apply"));
411 gtk_widget_show(confirm_area);
412 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
413 gtk_widget_grab_default(ok_btn);
415 dialog->window = window;
416 dialog->notebook = notebook;
417 dialog->ok_btn = ok_btn;
418 dialog->cancel_btn = cancel_btn;
419 dialog->apply_btn = apply_btn;
422 void prefs_dialog_destroy(PrefsDialog *dialog)
424 gtk_widget_destroy(dialog->window);
425 dialog->window = NULL;
426 dialog->notebook = NULL;
427 dialog->ok_btn = NULL;
428 dialog->cancel_btn = NULL;
429 dialog->apply_btn = NULL;
432 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
436 is_active = gtk_toggle_button_get_active(toggle_btn);
437 gtk_widget_set_sensitive(widget, is_active);
440 void prefs_set_dialog(PrefParam *param)
444 for (i = 0; param[i].name != NULL; i++) {
445 if (param[i].widget_set_func)
446 param[i].widget_set_func(¶m[i]);
450 void prefs_set_data_from_dialog(PrefParam *param)
454 for (i = 0; param[i].name != NULL; i++) {
455 if (param[i].data_set_func)
456 param[i].data_set_func(¶m[i]);
460 void prefs_set_dialog_to_default(PrefParam *param)
464 gchar *str_data = NULL;
470 for (i = 0; param[i].name != NULL; i++) {
471 if (!param[i].widget_set_func) continue;
475 switch (tmpparam.type) {
477 if (tmpparam.defval) {
478 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
479 str_data = g_strdup(g_getenv(param[i].defval + 4));
480 tmpparam.data = &str_data;
482 } else if (tmpparam.defval[0] == '~') {
484 g_strconcat(get_home_dir(),
487 tmpparam.data = &str_data;
491 tmpparam.data = &tmpparam.defval;
495 int_data = atoi(tmpparam.defval);
498 tmpparam.data = &int_data;
502 ushort_data = atoi(tmpparam.defval);
505 tmpparam.data = &ushort_data;
508 if (tmpparam.defval) {
509 if (!strcasecmp(tmpparam.defval, "TRUE"))
512 bool_data = atoi(tmpparam.defval)
516 tmpparam.data = &bool_data;
520 enum_data = (DummyEnum)atoi(tmpparam.defval);
523 tmpparam.data = &enum_data;
529 tmpparam.widget_set_func(&tmpparam);
535 void prefs_set_data_from_entry(PrefParam *pparam)
537 gchar **str, *entry_str;
539 g_return_if_fail(*pparam->widget != NULL);
541 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
543 switch (pparam->type) {
545 str = (gchar **)pparam->data;
547 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
550 *((gushort *)pparam->data) = atoi(entry_str);
553 *((gint *)pparam->data) = atoi(entry_str);
556 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
561 void prefs_set_entry(PrefParam *pparam)
565 g_return_if_fail(*pparam->widget != NULL);
567 switch (pparam->type) {
569 str = (gchar **)pparam->data;
570 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
574 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
575 itos(*((gint *)pparam->data)));
578 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
579 itos(*((gushort *)pparam->data)));
582 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
587 void prefs_set_data_from_text(PrefParam *pparam)
593 g_return_if_fail(*pparam->widget != NULL);
595 switch (pparam->type) {
597 str = (gchar **)pparam->data;
599 tp = text = gtk_editable_get_chars
600 (GTK_EDITABLE(*pparam->widget), 0, -1);
601 if (text[0] == '\0') {
607 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
608 { *str = NULL; break; });
618 *str = g_strdup(tmp);
622 g_warning("Invalid PrefType for GtkText widget: %d\n",
627 void prefs_set_text(PrefParam *pparam)
629 gchar *buf, *sp, *bufp;
633 g_return_if_fail(*pparam->widget != NULL);
635 switch (pparam->type) {
637 str = (gchar **)pparam->data;
639 bufp = buf = alloca(strlen(*str) + 1);
644 if (*sp == '\\' && *(sp + 1) == 'n') {
655 text = GTK_TEXT(*pparam->widget);
656 gtk_text_set_point(text, 0);
657 gtk_text_forward_delete(text, gtk_text_get_length(text));
658 gtk_text_set_point(text, 0);
659 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
662 g_warning("Invalid PrefType for GtkText widget: %d\n",
667 void prefs_set_data_from_toggle(PrefParam *pparam)
669 g_return_if_fail(pparam->type == P_BOOL);
670 g_return_if_fail(*pparam->widget != NULL);
672 *((gboolean *)pparam->data) =
673 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
676 void prefs_set_toggle(PrefParam *pparam)
678 g_return_if_fail(pparam->type == P_BOOL);
679 g_return_if_fail(*pparam->widget != NULL);
681 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
682 *((gboolean *)pparam->data));
685 void prefs_set_data_from_spinbtn(PrefParam *pparam)
687 g_return_if_fail(*pparam->widget != NULL);
689 switch (pparam->type) {
691 *((gint *)pparam->data) =
692 gtk_spin_button_get_value_as_int
693 (GTK_SPIN_BUTTON(*pparam->widget));
696 *((gushort *)pparam->data) =
697 (gushort)gtk_spin_button_get_value_as_int
698 (GTK_SPIN_BUTTON(*pparam->widget));
701 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
706 void prefs_set_spinbtn(PrefParam *pparam)
708 g_return_if_fail(*pparam->widget != NULL);
710 switch (pparam->type) {
712 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
713 (gfloat)*((gint *)pparam->data));
716 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
717 (gfloat)*((gushort *)pparam->data));
720 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
725 static GSList *prefs_pages = NULL;
727 void prefs_gtk_open()
729 prefswindow_open(prefs_pages, NULL);
732 void prefs_gtk_register_page(PrefsPage *page)
734 prefs_pages = g_slist_append(prefs_pages, page);
737 void prefs_gtk_unregister_page(PrefsPage *page)
739 prefs_pages = g_slist_remove(prefs_pages, page);