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 if (prefs_is_readonly(path)) {
271 g_warning(_("no permission - %s\n"), path);
275 tmppath = g_strconcat(path, ".tmp", NULL);
276 if ((fp = fopen(tmppath, "w")) == NULL) {
277 FILE_OP_ERROR(tmppath, "fopen");
282 if (change_file_mode_rw(fp, tmppath) < 0)
283 FILE_OP_ERROR(tmppath, "chmod");
287 pfile = g_new(PrefFile, 1);
289 pfile->path = g_strdup(path);
294 gint prefs_write_close(PrefFile *pfile)
299 gchar *bakpath = NULL;
301 g_return_val_if_fail(pfile != NULL, -1);
307 tmppath = g_strconcat(path, ".tmp", NULL);
308 if (fclose(fp) == EOF) {
309 FILE_OP_ERROR(tmppath, "fclose");
316 if (is_file_exist(path)) {
317 bakpath = g_strconcat(path, ".bak", NULL);
318 if (rename(path, bakpath) < 0) {
319 FILE_OP_ERROR(path, "rename");
328 if (rename(tmppath, path) < 0) {
329 FILE_OP_ERROR(tmppath, "rename");
343 gint prefs_write_close_revert(PrefFile *pfile)
347 g_return_val_if_fail(pfile != NULL, -1);
349 tmppath = g_strconcat(pfile->path, ".tmp", NULL);
351 if (unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
359 void prefs_set_default(PrefParam *param)
363 g_return_if_fail(param != NULL);
365 for (i = 0; param[i].name != NULL; i++) {
366 if (!param[i].data) continue;
368 switch (param[i].type) {
370 if (param[i].defval != NULL) {
371 if (!strncasecmp(param[i].defval, "ENV_", 4))
372 *((gchar **)param[i].data) =
373 g_strdup(g_getenv(param[i].defval + 4));
374 else if (param[i].defval[0] == '~')
375 *((gchar **)param[i].data) =
376 g_strconcat(get_home_dir(),
379 else if (param[i].defval[0] != '\0')
380 *((gchar **)param[i].data) =
381 g_strdup(param[i].defval);
383 *((gchar **)param[i].data) = NULL;
385 *((gchar **)param[i].data) = NULL;
388 if (param[i].defval != NULL)
389 *((gint *)param[i].data) =
390 (gint)atoi(param[i].defval);
392 *((gint *)param[i].data) = 0;
395 if (param[i].defval != NULL) {
396 if (!strcasecmp(param[i].defval, "TRUE"))
397 *((gboolean *)param[i].data) = TRUE;
399 *((gboolean *)param[i].data) =
400 atoi(param[i].defval) ? TRUE : FALSE;
402 *((gboolean *)param[i].data) = FALSE;
405 if (param[i].defval != NULL)
406 *((DummyEnum*)param[i].data) =
407 (DummyEnum)atoi(param[i].defval);
409 *((DummyEnum *)param[i].data) = 0;
412 if (param[i].defval != NULL)
413 *((gushort *)param[i].data) =
414 (gushort)atoi(param[i].defval);
416 *((gushort *)param[i].data) = 0;
423 void prefs_free(PrefParam *param)
427 g_return_if_fail(param != NULL);
429 for (i = 0; param[i].name != NULL; i++) {
430 if (!param[i].data) continue;
432 switch (param[i].type) {
434 g_free(*((gchar **)param[i].data));
441 void prefs_dialog_create(PrefsDialog *dialog)
447 GtkWidget *confirm_area;
449 GtkWidget *cancel_btn;
450 GtkWidget *apply_btn;
452 g_return_if_fail(dialog != NULL);
454 window = gtk_window_new (GTK_WINDOW_DIALOG);
455 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
456 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
457 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
458 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
460 vbox = gtk_vbox_new (FALSE, 6);
461 gtk_widget_show(vbox);
462 gtk_container_add (GTK_CONTAINER (window), vbox);
464 notebook = gtk_notebook_new ();
465 gtk_widget_show(notebook);
466 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
467 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
468 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
469 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
471 gtkut_button_set_create(&confirm_area,
473 &cancel_btn, _("Cancel"),
474 &apply_btn, _("Apply"));
475 gtk_widget_show(confirm_area);
476 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
477 gtk_widget_grab_default(ok_btn);
479 dialog->window = window;
480 dialog->notebook = notebook;
481 dialog->ok_btn = ok_btn;
482 dialog->cancel_btn = cancel_btn;
483 dialog->apply_btn = apply_btn;
486 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
490 is_active = gtk_toggle_button_get_active(toggle_btn);
491 gtk_widget_set_sensitive(widget, is_active);
494 void prefs_set_dialog(PrefParam *param)
498 for (i = 0; param[i].name != NULL; i++) {
499 if (param[i].widget_set_func)
500 param[i].widget_set_func(¶m[i]);
504 void prefs_set_data_from_dialog(PrefParam *param)
508 for (i = 0; param[i].name != NULL; i++) {
509 if (param[i].data_set_func)
510 param[i].data_set_func(¶m[i]);
514 void prefs_set_dialog_to_default(PrefParam *param)
518 gchar *str_data = NULL;
524 for (i = 0; param[i].name != NULL; i++) {
525 if (!param[i].widget_set_func) continue;
529 switch (tmpparam.type) {
531 if (tmpparam.defval) {
532 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
533 str_data = g_strdup(g_getenv(param[i].defval + 4));
534 tmpparam.data = &str_data;
536 } else if (tmpparam.defval[0] == '~') {
538 g_strconcat(get_home_dir(),
541 tmpparam.data = &str_data;
545 tmpparam.data = &tmpparam.defval;
549 int_data = atoi(tmpparam.defval);
552 tmpparam.data = &int_data;
556 ushort_data = atoi(tmpparam.defval);
559 tmpparam.data = &ushort_data;
562 if (tmpparam.defval) {
563 if (!strcasecmp(tmpparam.defval, "TRUE"))
566 bool_data = atoi(tmpparam.defval)
570 tmpparam.data = &bool_data;
574 enum_data = (DummyEnum)atoi(tmpparam.defval);
577 tmpparam.data = &enum_data;
581 tmpparam.widget_set_func(&tmpparam);
587 void prefs_set_data_from_entry(PrefParam *pparam)
589 gchar **str, *entry_str;
591 g_return_if_fail(*pparam->widget != NULL);
593 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
595 switch (pparam->type) {
597 str = (gchar **)pparam->data;
599 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
602 *((gushort *)pparam->data) = atoi(entry_str);
605 *((gint *)pparam->data) = atoi(entry_str);
608 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
613 void prefs_set_entry(PrefParam *pparam)
617 g_return_if_fail(*pparam->widget != NULL);
619 switch (pparam->type) {
621 str = (gchar **)pparam->data;
622 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
626 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
627 itos(*((gint *)pparam->data)));
630 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
631 itos(*((gushort *)pparam->data)));
634 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
639 void prefs_set_data_from_text(PrefParam *pparam)
645 g_return_if_fail(*pparam->widget != NULL);
647 switch (pparam->type) {
649 str = (gchar **)pparam->data;
651 tp = text = gtk_editable_get_chars
652 (GTK_EDITABLE(*pparam->widget), 0, -1);
653 if (text[0] == '\0') {
659 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
660 { *str = NULL; break; });
670 *str = g_strdup(tmp);
674 g_warning("Invalid PrefType for GtkText widget: %d\n",
679 void prefs_set_text(PrefParam *pparam)
681 gchar *buf, *sp, *bufp;
685 g_return_if_fail(*pparam->widget != NULL);
687 switch (pparam->type) {
689 str = (gchar **)pparam->data;
691 bufp = buf = alloca(strlen(*str) + 1);
696 if (*sp == '\\' && *(sp + 1) == 'n') {
707 text = GTK_TEXT(*pparam->widget);
708 gtk_text_set_point(text, 0);
709 gtk_text_forward_delete(text, gtk_text_get_length(text));
710 gtk_text_set_point(text, 0);
711 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
714 g_warning("Invalid PrefType for GtkText widget: %d\n",
719 void prefs_set_data_from_toggle(PrefParam *pparam)
721 g_return_if_fail(pparam->type == P_BOOL);
722 g_return_if_fail(*pparam->widget != NULL);
724 *((gboolean *)pparam->data) =
725 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
728 void prefs_set_toggle(PrefParam *pparam)
730 g_return_if_fail(pparam->type == P_BOOL);
731 g_return_if_fail(*pparam->widget != NULL);
733 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
734 *((gboolean *)pparam->data));
737 void prefs_set_data_from_spinbtn(PrefParam *pparam)
739 g_return_if_fail(*pparam->widget != NULL);
741 switch (pparam->type) {
743 *((gint *)pparam->data) =
744 gtk_spin_button_get_value_as_int
745 (GTK_SPIN_BUTTON(*pparam->widget));
748 *((gushort *)pparam->data) =
749 (gushort)gtk_spin_button_get_value_as_int
750 (GTK_SPIN_BUTTON(*pparam->widget));
753 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
758 void prefs_set_spinbtn(PrefParam *pparam)
760 g_return_if_fail(*pparam->widget != NULL);
762 switch (pparam->type) {
764 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
765 (gfloat)*((gint *)pparam->data));
768 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
769 (gfloat)*((gushort *)pparam->data));
772 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
777 gboolean prefs_is_readonly(const gchar * path)
782 return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
785 gboolean prefs_rc_is_readonly(const gchar * rcfile)
793 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
794 result = prefs_is_readonly(rcpath);