2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999,2000 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.
42 #define PREFSBUFSIZE 1024
44 void prefs_read_config(PrefParam *param, const gchar *label,
48 gchar buf[PREFSBUFSIZE];
52 g_return_if_fail(param != NULL);
53 g_return_if_fail(label != NULL);
54 g_return_if_fail(rcfile != NULL);
56 debug_print(_("Reading configuration...\n"));
58 prefs_set_default(param);
60 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
61 if ((fp = fopen(rcpath, "r")) == NULL) {
62 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
68 block_label = g_strdup_printf("[%s]", label);
70 /* search aiming block */
71 while (fgets(buf, sizeof(buf), fp) != NULL) {
74 val = strncmp(buf, block_label, strlen(block_label));
76 debug_print(_("Found %s\n"), block_label);
82 while (fgets(buf, sizeof(buf), fp) != NULL) {
84 /* reached next block */
85 if (buf[0] == '[') break;
87 prefs_config_parse_one_line(param, buf);
90 debug_print(_("Finished reading configuration.\n"));
94 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
100 for (i = 0; param[i].name != NULL; i++) {
101 name_len = strlen(param[i].name);
102 if (strncasecmp(buf, param[i].name, name_len))
104 if (buf[name_len] != '=')
106 value = buf + name_len + 1;
107 debug_print("%s = %s\n", param[i].name, value);
109 switch (param[i].type) {
111 g_free(*((gchar **)param[i].data));
112 *((gchar **)param[i].data) =
113 *value ? g_strdup(value) : NULL;
116 *((gint *)param[i].data) =
120 *((gboolean *)param[i].data) =
121 (*value == '0' || *value == '\0')
125 *((DummyEnum *)param[i].data) =
126 (DummyEnum)atoi(value);
129 *((gushort *)param[i].data) =
130 (gushort)atoi(value);
140 g_warning(_("failed to write configuration to file\n")); \
141 if (orig_fp) fclose(orig_fp); \
142 prefs_write_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, "r")) == 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_write_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 PrefFile *prefs_write_open(const gchar *path)
268 g_return_val_if_fail(path != NULL, NULL);
270 tmppath = g_strconcat(path, ".tmp", NULL);
271 if ((fp = fopen(tmppath, "w")) == NULL) {
272 FILE_OP_ERROR(tmppath, "fopen");
277 if (change_file_mode_rw(fp, tmppath) < 0)
278 FILE_OP_ERROR(tmppath, "chmod");
282 pfile = g_new(PrefFile, 1);
284 pfile->path = g_strdup(path);
289 gint prefs_write_close(PrefFile *pfile)
294 gchar *bakpath = NULL;
296 g_return_val_if_fail(pfile != NULL, -1);
302 tmppath = g_strconcat(path, ".tmp", NULL);
303 if (fclose(fp) == EOF) {
304 FILE_OP_ERROR(tmppath, "fclose");
311 if (is_file_exist(path)) {
312 bakpath = g_strconcat(path, ".bak", NULL);
313 if (rename(path, bakpath) < 0) {
314 FILE_OP_ERROR(path, "rename");
323 if (rename(tmppath, path) < 0) {
324 FILE_OP_ERROR(tmppath, "rename");
338 gint prefs_write_close_revert(PrefFile *pfile)
342 g_return_val_if_fail(pfile != NULL, -1);
344 tmppath = g_strconcat(pfile->path, ".tmp", NULL);
346 if (unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
354 void prefs_set_default(PrefParam *param)
358 g_return_if_fail(param != NULL);
360 for (i = 0; param[i].name != NULL; i++) {
361 if (!param[i].data) continue;
363 switch (param[i].type) {
365 if (param[i].defval != NULL) {
366 if (!strncasecmp(param[i].defval, "ENV_", 4))
367 *((gchar **)param[i].data) =
368 g_strdup(g_getenv(param[i].defval + 4));
369 else if (param[i].defval[0] == '~')
370 *((gchar **)param[i].data) =
371 g_strconcat(g_get_home_dir(),
374 else if (param[i].defval[0] != '\0')
375 *((gchar **)param[i].data) =
376 g_strdup(param[i].defval);
378 *((gchar **)param[i].data) = NULL;
380 *((gchar **)param[i].data) = NULL;
383 if (param[i].defval != NULL)
384 *((gint *)param[i].data) =
385 (gint)atoi(param[i].defval);
387 *((gint *)param[i].data) = 0;
390 if (param[i].defval != NULL) {
391 if (!strcasecmp(param[i].defval, "TRUE"))
392 *((gboolean *)param[i].data) = TRUE;
394 *((gboolean *)param[i].data) =
395 atoi(param[i].defval) ? TRUE : FALSE;
397 *((gboolean *)param[i].data) = FALSE;
400 if (param[i].defval != NULL)
401 *((DummyEnum*)param[i].data) =
402 (DummyEnum)atoi(param[i].defval);
404 *((DummyEnum *)param[i].data) = 0;
407 if (param[i].defval != NULL)
408 *((gushort *)param[i].data) =
409 (gushort)atoi(param[i].defval);
411 *((gushort *)param[i].data) = 0;
418 void prefs_free(PrefParam *param)
422 g_return_if_fail(param != NULL);
424 for (i = 0; param[i].name != NULL; i++) {
425 if (!param[i].data) continue;
427 switch (param[i].type) {
429 g_free(*((gchar **)param[i].data));
436 void prefs_dialog_create(PrefsDialog *dialog)
442 GtkWidget *confirm_area;
444 GtkWidget *cancel_btn;
445 GtkWidget *apply_btn;
447 g_return_if_fail(dialog != NULL);
449 window = gtk_window_new (GTK_WINDOW_DIALOG);
450 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
451 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
452 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
453 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
455 vbox = gtk_vbox_new (FALSE, 6);
456 gtk_widget_show(vbox);
457 gtk_container_add (GTK_CONTAINER (window), vbox);
459 notebook = gtk_notebook_new ();
460 gtk_widget_show(notebook);
461 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
462 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
463 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
464 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
466 gtkut_button_set_create(&confirm_area,
468 &cancel_btn, _("Cancel"),
469 &apply_btn, _("Apply"));
470 gtk_widget_show(confirm_area);
471 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
472 gtk_widget_grab_default(ok_btn);
474 dialog->window = window;
475 dialog->notebook = notebook;
476 dialog->ok_btn = ok_btn;
477 dialog->cancel_btn = cancel_btn;
478 dialog->apply_btn = apply_btn;
481 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
485 is_active = gtk_toggle_button_get_active(toggle_btn);
486 gtk_widget_set_sensitive(widget, is_active);
489 void prefs_set_dialog(PrefParam *param)
493 for (i = 0; param[i].name != NULL; i++) {
494 if (param[i].widget_set_func)
495 param[i].widget_set_func(¶m[i]);
499 void prefs_set_data_from_dialog(PrefParam *param)
503 for (i = 0; param[i].name != NULL; i++) {
504 if (param[i].data_set_func)
505 param[i].data_set_func(¶m[i]);
509 void prefs_set_dialog_to_default(PrefParam *param)
513 gchar *str_data = NULL;
519 for (i = 0; param[i].name != NULL; i++) {
520 if (!param[i].widget_set_func) continue;
524 switch (tmpparam.type) {
526 if (tmpparam.defval) {
527 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
528 str_data = g_strdup(g_getenv(param[i].defval + 4));
529 tmpparam.data = &str_data;
531 } else if (tmpparam.defval[0] == '~') {
533 g_strconcat(g_get_home_dir(),
536 tmpparam.data = &str_data;
540 tmpparam.data = &tmpparam.defval;
544 int_data = atoi(tmpparam.defval);
547 tmpparam.data = &int_data;
551 ushort_data = atoi(tmpparam.defval);
554 tmpparam.data = &ushort_data;
557 if (tmpparam.defval) {
558 if (!strcasecmp(tmpparam.defval, "TRUE"))
561 bool_data = atoi(tmpparam.defval)
565 tmpparam.data = &bool_data;
569 enum_data = (DummyEnum)atoi(tmpparam.defval);
572 tmpparam.data = &enum_data;
576 tmpparam.widget_set_func(&tmpparam);
582 void prefs_set_data_from_entry(PrefParam *pparam)
584 gchar **str, *entry_str;
586 g_return_if_fail(*pparam->widget != NULL);
588 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
590 switch (pparam->type) {
592 str = (gchar **)pparam->data;
594 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
597 *((gushort *)pparam->data) = atoi(entry_str);
600 *((gint *)pparam->data) = atoi(entry_str);
603 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
608 void prefs_set_entry(PrefParam *pparam)
612 g_return_if_fail(*pparam->widget != NULL);
614 switch (pparam->type) {
616 str = (gchar **)pparam->data;
617 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
621 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
622 itos(*((gint *)pparam->data)));
625 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
626 itos(*((gushort *)pparam->data)));
629 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
634 void prefs_set_data_from_text(PrefParam *pparam)
640 g_return_if_fail(*pparam->widget != NULL);
642 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) {
684 str = (gchar **)pparam->data;
686 bufp = buf = alloca(strlen(*str) + 1);
691 if (*sp == '\\' && *(sp + 1) == 'n') {
702 text = GTK_TEXT(*pparam->widget);
703 gtk_text_set_point(text, 0);
704 gtk_text_forward_delete(text, gtk_text_get_length(text));
705 gtk_text_set_point(text, 0);
706 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
709 g_warning("Invalid PrefType for GtkText widget: %d\n",
714 void prefs_set_data_from_toggle(PrefParam *pparam)
716 g_return_if_fail(pparam->type == P_BOOL);
717 g_return_if_fail(*pparam->widget != NULL);
719 *((gboolean *)pparam->data) =
720 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
723 void prefs_set_toggle(PrefParam *pparam)
725 g_return_if_fail(pparam->type == P_BOOL);
726 g_return_if_fail(*pparam->widget != NULL);
728 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
729 *((gboolean *)pparam->data));
732 void prefs_set_data_from_spinbtn(PrefParam *pparam)
734 g_return_if_fail(*pparam->widget != NULL);
736 switch (pparam->type) {
738 *((gint *)pparam->data) =
739 gtk_spin_button_get_value_as_int
740 (GTK_SPIN_BUTTON(*pparam->widget));
743 *((gushort *)pparam->data) =
744 (gushort)gtk_spin_button_get_value_as_int
745 (GTK_SPIN_BUTTON(*pparam->widget));
748 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
753 void prefs_set_spinbtn(PrefParam *pparam)
755 g_return_if_fail(*pparam->widget != NULL);
757 switch (pparam->type) {
759 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
760 (gfloat)*((gint *)pparam->data));
763 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
764 (gfloat)*((gushort *)pparam->data));
767 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",