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);
141 g_warning(_("failed to write configuration to file\n")); \
142 if (orig_fp) fclose(orig_fp); \
143 prefs_write_close_revert(pfile); \
145 g_free(block_label); \
149 void prefs_save_config(PrefParam *param, const gchar *label,
155 gchar buf[PREFSBUFSIZE];
156 gchar *block_label = NULL;
157 gboolean block_matched = FALSE;
159 g_return_if_fail(param != NULL);
160 g_return_if_fail(label != NULL);
161 g_return_if_fail(rcfile != NULL);
163 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
164 if ((orig_fp = fopen(rcpath, "r")) == NULL) {
165 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
168 if ((pfile = prefs_write_open(rcpath)) == NULL) {
169 g_warning(_("failed to write configuration to file\n"));
170 if (orig_fp) fclose(orig_fp);
175 block_label = g_strdup_printf("[%s]", label);
177 /* search aiming block */
179 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
182 val = strncmp(buf, block_label, strlen(block_label));
184 debug_print(_("Found %s\n"), block_label);
185 block_matched = TRUE;
188 TRY(fputs(buf, pfile->fp) != EOF);
192 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
196 /* write all param data to file */
197 TRY(prefs_write_param(param, pfile->fp) == 0);
200 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
203 TRY(fputc('\n', pfile->fp) != EOF &&
204 fputs(buf, pfile->fp) != EOF);
208 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
209 TRY(fputs(buf, pfile->fp) != EOF);
212 if (orig_fp) fclose(orig_fp);
213 if (prefs_write_close(pfile) < 0)
214 g_warning(_("failed to write configuration to file\n"));
217 debug_print(_("Configuration is saved.\n"));
220 gint prefs_write_param(PrefParam *param, FILE *fp)
223 gchar buf[PREFSBUFSIZE];
225 for (i = 0; param[i].name != NULL; i++) {
226 switch (param[i].type) {
228 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
229 *((gchar **)param[i].data) ?
230 *((gchar **)param[i].data) : "");
233 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
234 *((gint *)param[i].data));
237 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
238 *((gboolean *)param[i].data));
241 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
242 *((DummyEnum *)param[i].data));
245 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
246 *((gushort *)param[i].data));
252 if (buf[0] != '\0') {
253 if (fputs(buf, fp) == EOF) {
263 PrefFile *prefs_write_open(const gchar *path)
269 g_return_val_if_fail(path != NULL, NULL);
271 if (prefs_is_readonly(path)) {
272 g_warning(_("no permission - %s\n"), path);
276 tmppath = g_strconcat(path, ".tmp", NULL);
277 if ((fp = fopen(tmppath, "w")) == NULL) {
278 FILE_OP_ERROR(tmppath, "fopen");
283 if (change_file_mode_rw(fp, tmppath) < 0)
284 FILE_OP_ERROR(tmppath, "chmod");
288 pfile = g_new(PrefFile, 1);
290 pfile->path = g_strdup(path);
295 gint prefs_write_close(PrefFile *pfile)
300 gchar *bakpath = NULL;
302 g_return_val_if_fail(pfile != NULL, -1);
308 tmppath = g_strconcat(path, ".tmp", NULL);
309 if (fclose(fp) == EOF) {
310 FILE_OP_ERROR(tmppath, "fclose");
317 if (is_file_exist(path)) {
318 bakpath = g_strconcat(path, ".bak", NULL);
319 if (rename(path, bakpath) < 0) {
320 FILE_OP_ERROR(path, "rename");
329 if (rename(tmppath, path) < 0) {
330 FILE_OP_ERROR(tmppath, "rename");
344 gint prefs_write_close_revert(PrefFile *pfile)
348 g_return_val_if_fail(pfile != NULL, -1);
350 tmppath = g_strconcat(pfile->path, ".tmp", NULL);
352 if (unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
360 void prefs_set_default(PrefParam *param)
364 g_return_if_fail(param != NULL);
366 for (i = 0; param[i].name != NULL; i++) {
367 if (!param[i].data) continue;
369 switch (param[i].type) {
371 if (param[i].defval != NULL) {
372 if (!strncasecmp(param[i].defval, "ENV_", 4))
373 *((gchar **)param[i].data) =
374 g_strdup(g_getenv(param[i].defval + 4));
375 else if (param[i].defval[0] == '~')
376 *((gchar **)param[i].data) =
377 g_strconcat(get_home_dir(),
380 else if (param[i].defval[0] != '\0')
381 *((gchar **)param[i].data) =
382 g_strdup(param[i].defval);
384 *((gchar **)param[i].data) = NULL;
386 *((gchar **)param[i].data) = NULL;
389 if (param[i].defval != NULL)
390 *((gint *)param[i].data) =
391 (gint)atoi(param[i].defval);
393 *((gint *)param[i].data) = 0;
396 if (param[i].defval != NULL) {
397 if (!strcasecmp(param[i].defval, "TRUE"))
398 *((gboolean *)param[i].data) = TRUE;
400 *((gboolean *)param[i].data) =
401 atoi(param[i].defval) ? TRUE : FALSE;
403 *((gboolean *)param[i].data) = FALSE;
406 if (param[i].defval != NULL)
407 *((DummyEnum*)param[i].data) =
408 (DummyEnum)atoi(param[i].defval);
410 *((DummyEnum *)param[i].data) = 0;
413 if (param[i].defval != NULL)
414 *((gushort *)param[i].data) =
415 (gushort)atoi(param[i].defval);
417 *((gushort *)param[i].data) = 0;
425 void prefs_free(PrefParam *param)
429 g_return_if_fail(param != NULL);
431 for (i = 0; param[i].name != NULL; i++) {
432 if (!param[i].data) continue;
434 switch (param[i].type) {
436 g_free(*((gchar **)param[i].data));
444 void prefs_dialog_create(PrefsDialog *dialog)
450 GtkWidget *confirm_area;
452 GtkWidget *cancel_btn;
453 GtkWidget *apply_btn;
455 g_return_if_fail(dialog != NULL);
457 window = gtk_window_new (GTK_WINDOW_DIALOG);
458 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
459 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
460 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
461 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
463 vbox = gtk_vbox_new (FALSE, 6);
464 gtk_widget_show(vbox);
465 gtk_container_add (GTK_CONTAINER (window), vbox);
467 notebook = gtk_notebook_new ();
468 gtk_widget_show(notebook);
469 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
470 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
471 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
472 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
474 gtkut_button_set_create(&confirm_area,
476 &cancel_btn, _("Cancel"),
477 &apply_btn, _("Apply"));
478 gtk_widget_show(confirm_area);
479 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
480 gtk_widget_grab_default(ok_btn);
482 dialog->window = window;
483 dialog->notebook = notebook;
484 dialog->ok_btn = ok_btn;
485 dialog->cancel_btn = cancel_btn;
486 dialog->apply_btn = apply_btn;
489 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
493 is_active = gtk_toggle_button_get_active(toggle_btn);
494 gtk_widget_set_sensitive(widget, is_active);
497 void prefs_set_dialog(PrefParam *param)
501 for (i = 0; param[i].name != NULL; i++) {
502 if (param[i].widget_set_func)
503 param[i].widget_set_func(¶m[i]);
507 void prefs_set_data_from_dialog(PrefParam *param)
511 for (i = 0; param[i].name != NULL; i++) {
512 if (param[i].data_set_func)
513 param[i].data_set_func(¶m[i]);
517 void prefs_set_dialog_to_default(PrefParam *param)
521 gchar *str_data = NULL;
527 for (i = 0; param[i].name != NULL; i++) {
528 if (!param[i].widget_set_func) continue;
532 switch (tmpparam.type) {
534 if (tmpparam.defval) {
535 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
536 str_data = g_strdup(g_getenv(param[i].defval + 4));
537 tmpparam.data = &str_data;
539 } else if (tmpparam.defval[0] == '~') {
541 g_strconcat(get_home_dir(),
544 tmpparam.data = &str_data;
548 tmpparam.data = &tmpparam.defval;
552 int_data = atoi(tmpparam.defval);
555 tmpparam.data = &int_data;
559 ushort_data = atoi(tmpparam.defval);
562 tmpparam.data = &ushort_data;
565 if (tmpparam.defval) {
566 if (!strcasecmp(tmpparam.defval, "TRUE"))
569 bool_data = atoi(tmpparam.defval)
573 tmpparam.data = &bool_data;
577 enum_data = (DummyEnum)atoi(tmpparam.defval);
580 tmpparam.data = &enum_data;
585 tmpparam.widget_set_func(&tmpparam);
591 void prefs_set_data_from_entry(PrefParam *pparam)
593 gchar **str, *entry_str;
595 g_return_if_fail(*pparam->widget != NULL);
597 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
599 switch (pparam->type) {
601 str = (gchar **)pparam->data;
603 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
606 *((gushort *)pparam->data) = atoi(entry_str);
609 *((gint *)pparam->data) = atoi(entry_str);
612 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
617 void prefs_set_entry(PrefParam *pparam)
621 g_return_if_fail(*pparam->widget != NULL);
623 switch (pparam->type) {
625 str = (gchar **)pparam->data;
626 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
630 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
631 itos(*((gint *)pparam->data)));
634 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
635 itos(*((gushort *)pparam->data)));
638 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
643 void prefs_set_data_from_text(PrefParam *pparam)
649 g_return_if_fail(*pparam->widget != NULL);
651 switch (pparam->type) {
653 str = (gchar **)pparam->data;
655 tp = text = gtk_editable_get_chars
656 (GTK_EDITABLE(*pparam->widget), 0, -1);
657 if (text[0] == '\0') {
663 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
664 { *str = NULL; break; });
674 *str = g_strdup(tmp);
678 g_warning("Invalid PrefType for GtkText widget: %d\n",
683 void prefs_set_text(PrefParam *pparam)
685 gchar *buf, *sp, *bufp;
689 g_return_if_fail(*pparam->widget != NULL);
691 switch (pparam->type) {
693 str = (gchar **)pparam->data;
695 bufp = buf = alloca(strlen(*str) + 1);
700 if (*sp == '\\' && *(sp + 1) == 'n') {
711 text = GTK_TEXT(*pparam->widget);
712 gtk_text_set_point(text, 0);
713 gtk_text_forward_delete(text, gtk_text_get_length(text));
714 gtk_text_set_point(text, 0);
715 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
718 g_warning("Invalid PrefType for GtkText widget: %d\n",
723 void prefs_set_data_from_toggle(PrefParam *pparam)
725 g_return_if_fail(pparam->type == P_BOOL);
726 g_return_if_fail(*pparam->widget != NULL);
728 *((gboolean *)pparam->data) =
729 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
732 void prefs_set_toggle(PrefParam *pparam)
734 g_return_if_fail(pparam->type == P_BOOL);
735 g_return_if_fail(*pparam->widget != NULL);
737 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
738 *((gboolean *)pparam->data));
741 void prefs_set_data_from_spinbtn(PrefParam *pparam)
743 g_return_if_fail(*pparam->widget != NULL);
745 switch (pparam->type) {
747 *((gint *)pparam->data) =
748 gtk_spin_button_get_value_as_int
749 (GTK_SPIN_BUTTON(*pparam->widget));
752 *((gushort *)pparam->data) =
753 (gushort)gtk_spin_button_get_value_as_int
754 (GTK_SPIN_BUTTON(*pparam->widget));
757 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
762 void prefs_set_spinbtn(PrefParam *pparam)
764 g_return_if_fail(*pparam->widget != NULL);
766 switch (pparam->type) {
768 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
769 (gfloat)*((gint *)pparam->data));
772 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
773 (gfloat)*((gushort *)pparam->data));
776 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
781 gboolean prefs_is_readonly(const gchar * path)
786 return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
789 gboolean prefs_rc_is_readonly(const gchar * rcfile)
797 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
798 result = prefs_is_readonly(rcpath);