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"
43 void prefs_read_config(PrefParam *param, const gchar *label,
47 gchar buf[PREFSBUFSIZE];
51 g_return_if_fail(param != NULL);
52 g_return_if_fail(label != NULL);
53 g_return_if_fail(rcfile != NULL);
55 debug_print("Reading configuration...\n");
57 prefs_set_default(param);
59 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
60 if ((fp = fopen(rcpath, "rb")) == NULL) {
61 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
67 block_label = g_strdup_printf("[%s]", label);
69 /* search aiming block */
70 while (fgets(buf, sizeof(buf), fp) != NULL) {
73 val = strncmp(buf, block_label, strlen(block_label));
75 debug_print("Found %s\n", block_label);
81 while (fgets(buf, sizeof(buf), fp) != NULL) {
83 /* reached next block */
84 if (buf[0] == '[') break;
86 prefs_config_parse_one_line(param, buf);
89 debug_print("Finished reading configuration.\n");
93 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
99 for (i = 0; param[i].name != NULL; i++) {
100 name_len = strlen(param[i].name);
101 if (strncasecmp(buf, param[i].name, name_len))
103 if (buf[name_len] != '=')
105 value = buf + name_len + 1;
106 /* debug_print("%s = %s\n", param[i].name, value); */
108 switch (param[i].type) {
110 g_free(*((gchar **)param[i].data));
111 *((gchar **)param[i].data) =
112 *value ? g_strdup(value) : NULL;
115 *((gint *)param[i].data) =
119 *((gboolean *)param[i].data) =
120 (*value == '0' || *value == '\0')
124 *((DummyEnum *)param[i].data) =
125 (DummyEnum)atoi(value);
128 *((gushort *)param[i].data) =
129 (gushort)atoi(value);
140 g_warning("failed to write configuration to file\n"); \
141 if (orig_fp) fclose(orig_fp); \
142 prefs_file_close_revert(pfile); \
144 g_free(block_label); \
148 void prefs_save_config(PrefParam *param, const gchar *label,
154 gchar buf[PREFSBUFSIZE];
155 gchar *block_label = NULL;
156 gboolean block_matched = FALSE;
158 g_return_if_fail(param != NULL);
159 g_return_if_fail(label != NULL);
160 g_return_if_fail(rcfile != NULL);
162 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
163 if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
164 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
167 if ((pfile = prefs_write_open(rcpath)) == NULL) {
168 g_warning("failed to write configuration to file\n");
169 if (orig_fp) fclose(orig_fp);
174 block_label = g_strdup_printf("[%s]", label);
176 /* search aiming block */
178 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
181 val = strncmp(buf, block_label, strlen(block_label));
183 debug_print("Found %s\n", block_label);
184 block_matched = TRUE;
187 TRY(fputs(buf, pfile->fp) != EOF);
191 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
195 /* write all param data to file */
196 TRY(prefs_write_param(param, pfile->fp) == 0);
199 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
202 TRY(fputc('\n', pfile->fp) != EOF &&
203 fputs(buf, pfile->fp) != EOF);
207 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
208 TRY(fputs(buf, pfile->fp) != EOF);
211 if (orig_fp) fclose(orig_fp);
212 if (prefs_file_close(pfile) < 0)
213 g_warning("failed to write configuration to file\n");
216 debug_print("Configuration is saved.\n");
219 gint prefs_write_param(PrefParam *param, FILE *fp)
222 gchar buf[PREFSBUFSIZE];
224 for (i = 0; param[i].name != NULL; i++) {
225 switch (param[i].type) {
227 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
228 *((gchar **)param[i].data) ?
229 *((gchar **)param[i].data) : "");
232 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
233 *((gint *)param[i].data));
236 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
237 *((gboolean *)param[i].data));
240 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
241 *((DummyEnum *)param[i].data));
244 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
245 *((gushort *)param[i].data));
251 if (buf[0] != '\0') {
252 if (fputs(buf, fp) == EOF) {
262 void prefs_set_default(PrefParam *param)
266 g_return_if_fail(param != NULL);
268 for (i = 0; param[i].name != NULL; i++) {
269 if (!param[i].data) continue;
271 switch (param[i].type) {
273 if (param[i].defval != NULL) {
274 if (!strncasecmp(param[i].defval, "ENV_", 4))
275 *((gchar **)param[i].data) =
276 g_strdup(g_getenv(param[i].defval + 4));
277 else if (param[i].defval[0] == '~')
278 *((gchar **)param[i].data) =
279 g_strconcat(get_home_dir(),
282 else if (param[i].defval[0] != '\0')
283 *((gchar **)param[i].data) =
284 g_strdup(param[i].defval);
286 *((gchar **)param[i].data) = NULL;
288 *((gchar **)param[i].data) = NULL;
291 if (param[i].defval != NULL)
292 *((gint *)param[i].data) =
293 (gint)atoi(param[i].defval);
295 *((gint *)param[i].data) = 0;
298 if (param[i].defval != NULL) {
299 if (!strcasecmp(param[i].defval, "TRUE"))
300 *((gboolean *)param[i].data) = TRUE;
302 *((gboolean *)param[i].data) =
303 atoi(param[i].defval) ? TRUE : FALSE;
305 *((gboolean *)param[i].data) = FALSE;
308 if (param[i].defval != NULL)
309 *((DummyEnum*)param[i].data) =
310 (DummyEnum)atoi(param[i].defval);
312 *((DummyEnum *)param[i].data) = 0;
315 if (param[i].defval != NULL)
316 *((gushort *)param[i].data) =
317 (gushort)atoi(param[i].defval);
319 *((gushort *)param[i].data) = 0;
327 void prefs_free(PrefParam *param)
331 g_return_if_fail(param != NULL);
333 for (i = 0; param[i].name != NULL; i++) {
334 if (!param[i].data) continue;
336 switch (param[i].type) {
338 g_free(*((gchar **)param[i].data));
346 void prefs_dialog_create(PrefsDialog *dialog)
352 GtkWidget *confirm_area;
354 GtkWidget *cancel_btn;
355 GtkWidget *apply_btn;
357 g_return_if_fail(dialog != NULL);
359 window = gtk_window_new (GTK_WINDOW_DIALOG);
360 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
361 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
362 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
363 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
365 vbox = gtk_vbox_new (FALSE, 6);
366 gtk_widget_show(vbox);
367 gtk_container_add (GTK_CONTAINER (window), vbox);
369 notebook = gtk_notebook_new ();
370 gtk_widget_show(notebook);
371 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
372 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
373 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
374 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
376 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
378 gtkut_button_set_create(&confirm_area,
380 &cancel_btn, _("Cancel"),
381 &apply_btn, _("Apply"));
382 gtk_widget_show(confirm_area);
383 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
384 gtk_widget_grab_default(ok_btn);
386 dialog->window = window;
387 dialog->notebook = notebook;
388 dialog->ok_btn = ok_btn;
389 dialog->cancel_btn = cancel_btn;
390 dialog->apply_btn = apply_btn;
393 void prefs_dialog_destroy(PrefsDialog *dialog)
395 gtk_widget_destroy(dialog->window);
396 dialog->window = NULL;
397 dialog->notebook = NULL;
398 dialog->ok_btn = NULL;
399 dialog->cancel_btn = NULL;
400 dialog->apply_btn = NULL;
403 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
407 is_active = gtk_toggle_button_get_active(toggle_btn);
408 gtk_widget_set_sensitive(widget, is_active);
411 void prefs_set_dialog(PrefParam *param)
415 for (i = 0; param[i].name != NULL; i++) {
416 if (param[i].widget_set_func)
417 param[i].widget_set_func(¶m[i]);
421 void prefs_set_data_from_dialog(PrefParam *param)
425 for (i = 0; param[i].name != NULL; i++) {
426 if (param[i].data_set_func)
427 param[i].data_set_func(¶m[i]);
431 void prefs_set_dialog_to_default(PrefParam *param)
435 gchar *str_data = NULL;
441 for (i = 0; param[i].name != NULL; i++) {
442 if (!param[i].widget_set_func) continue;
446 switch (tmpparam.type) {
448 if (tmpparam.defval) {
449 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
450 str_data = g_strdup(g_getenv(param[i].defval + 4));
451 tmpparam.data = &str_data;
453 } else if (tmpparam.defval[0] == '~') {
455 g_strconcat(get_home_dir(),
458 tmpparam.data = &str_data;
462 tmpparam.data = &tmpparam.defval;
466 int_data = atoi(tmpparam.defval);
469 tmpparam.data = &int_data;
473 ushort_data = atoi(tmpparam.defval);
476 tmpparam.data = &ushort_data;
479 if (tmpparam.defval) {
480 if (!strcasecmp(tmpparam.defval, "TRUE"))
483 bool_data = atoi(tmpparam.defval)
487 tmpparam.data = &bool_data;
491 enum_data = (DummyEnum)atoi(tmpparam.defval);
494 tmpparam.data = &enum_data;
499 tmpparam.widget_set_func(&tmpparam);
505 void prefs_set_data_from_entry(PrefParam *pparam)
507 gchar **str, *entry_str;
509 g_return_if_fail(*pparam->widget != NULL);
511 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
513 switch (pparam->type) {
515 str = (gchar **)pparam->data;
517 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
520 *((gushort *)pparam->data) = atoi(entry_str);
523 *((gint *)pparam->data) = atoi(entry_str);
526 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
531 void prefs_set_entry(PrefParam *pparam)
535 g_return_if_fail(*pparam->widget != NULL);
537 switch (pparam->type) {
539 str = (gchar **)pparam->data;
540 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
544 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
545 itos(*((gint *)pparam->data)));
548 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
549 itos(*((gushort *)pparam->data)));
552 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
557 void prefs_set_data_from_text(PrefParam *pparam)
563 g_return_if_fail(*pparam->widget != NULL);
565 switch (pparam->type) {
567 str = (gchar **)pparam->data;
569 tp = text = gtk_editable_get_chars
570 (GTK_EDITABLE(*pparam->widget), 0, -1);
571 if (text[0] == '\0') {
577 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
578 { *str = NULL; break; });
588 *str = g_strdup(tmp);
592 g_warning("Invalid PrefType for GtkText widget: %d\n",
597 void prefs_set_text(PrefParam *pparam)
599 gchar *buf, *sp, *bufp;
603 g_return_if_fail(*pparam->widget != NULL);
605 switch (pparam->type) {
607 str = (gchar **)pparam->data;
609 bufp = buf = alloca(strlen(*str) + 1);
614 if (*sp == '\\' && *(sp + 1) == 'n') {
625 text = GTK_TEXT(*pparam->widget);
626 gtk_text_set_point(text, 0);
627 gtk_text_forward_delete(text, gtk_text_get_length(text));
628 gtk_text_set_point(text, 0);
629 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
632 g_warning("Invalid PrefType for GtkText widget: %d\n",
637 void prefs_set_data_from_toggle(PrefParam *pparam)
639 g_return_if_fail(pparam->type == P_BOOL);
640 g_return_if_fail(*pparam->widget != NULL);
642 *((gboolean *)pparam->data) =
643 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
646 void prefs_set_toggle(PrefParam *pparam)
648 g_return_if_fail(pparam->type == P_BOOL);
649 g_return_if_fail(*pparam->widget != NULL);
651 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
652 *((gboolean *)pparam->data));
655 void prefs_set_data_from_spinbtn(PrefParam *pparam)
657 g_return_if_fail(*pparam->widget != NULL);
659 switch (pparam->type) {
661 *((gint *)pparam->data) =
662 gtk_spin_button_get_value_as_int
663 (GTK_SPIN_BUTTON(*pparam->widget));
666 *((gushort *)pparam->data) =
667 (gushort)gtk_spin_button_get_value_as_int
668 (GTK_SPIN_BUTTON(*pparam->widget));
671 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
676 void prefs_set_spinbtn(PrefParam *pparam)
678 g_return_if_fail(*pparam->widget != NULL);
680 switch (pparam->type) {
682 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
683 (gfloat)*((gint *)pparam->data));
686 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
687 (gfloat)*((gushort *)pparam->data));
690 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
695 static GSList *prefs_pages = NULL;
697 void prefs_gtk_open()
699 prefswindow_open(prefs_pages, NULL);
702 void prefs_gtk_register_page(PrefsPage *page)
704 prefs_pages = g_slist_append(prefs_pages, page);
707 void prefs_gtk_unregister_page(PrefsPage *page)
709 prefs_pages = g_slist_remove(prefs_pages, page);
712 void prefs_gtk_destroy_all_pages()
716 for (cur = prefs_pages; cur != NULL; cur = g_slist_next(cur)) {
717 PrefsPage *page = (PrefsPage *) cur->data;
719 page->destroy_page(page);