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.
42 void prefs_read_config(PrefParam *param, const gchar *label,
46 gchar buf[PREFSBUFSIZE];
50 g_return_if_fail(param != NULL);
51 g_return_if_fail(label != NULL);
52 g_return_if_fail(rcfile != NULL);
54 debug_print("Reading configuration...\n");
56 prefs_set_default(param);
58 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
59 if ((fp = fopen(rcpath, "rb")) == NULL) {
60 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
66 block_label = g_strdup_printf("[%s]", label);
68 /* search aiming block */
69 while (fgets(buf, sizeof(buf), fp) != NULL) {
72 val = strncmp(buf, block_label, strlen(block_label));
74 debug_print("Found %s\n", block_label);
80 while (fgets(buf, sizeof(buf), fp) != NULL) {
82 /* reached next block */
83 if (buf[0] == '[') break;
85 prefs_config_parse_one_line(param, buf);
88 debug_print("Finished reading configuration.\n");
92 void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
98 for (i = 0; param[i].name != NULL; i++) {
99 name_len = strlen(param[i].name);
100 if (strncasecmp(buf, param[i].name, name_len))
102 if (buf[name_len] != '=')
104 value = buf + name_len + 1;
105 /* debug_print("%s = %s\n", param[i].name, value); */
107 switch (param[i].type) {
109 g_free(*((gchar **)param[i].data));
110 *((gchar **)param[i].data) =
111 *value ? g_strdup(value) : NULL;
114 *((gint *)param[i].data) =
118 *((gboolean *)param[i].data) =
119 (*value == '0' || *value == '\0')
123 *((DummyEnum *)param[i].data) =
124 (DummyEnum)atoi(value);
127 *((gushort *)param[i].data) =
128 (gushort)atoi(value);
139 g_warning("failed to write configuration to file\n"); \
140 if (orig_fp) fclose(orig_fp); \
141 prefs_write_close_revert(pfile); \
143 g_free(block_label); \
147 void prefs_save_config(PrefParam *param, const gchar *label,
153 gchar buf[PREFSBUFSIZE];
154 gchar *block_label = NULL;
155 gboolean block_matched = FALSE;
157 g_return_if_fail(param != NULL);
158 g_return_if_fail(label != NULL);
159 g_return_if_fail(rcfile != NULL);
161 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
162 if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
163 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
166 if ((pfile = prefs_write_open(rcpath)) == NULL) {
167 g_warning("failed to write configuration to file\n");
168 if (orig_fp) fclose(orig_fp);
173 block_label = g_strdup_printf("[%s]", label);
175 /* search aiming block */
177 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
180 val = strncmp(buf, block_label, strlen(block_label));
182 debug_print("Found %s\n", block_label);
183 block_matched = TRUE;
186 TRY(fputs(buf, pfile->fp) != EOF);
190 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
194 /* write all param data to file */
195 TRY(prefs_write_param(param, pfile->fp) == 0);
198 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
201 TRY(fputc('\n', pfile->fp) != EOF &&
202 fputs(buf, pfile->fp) != EOF);
206 while (fgets(buf, sizeof(buf), orig_fp) != NULL)
207 TRY(fputs(buf, pfile->fp) != EOF);
210 if (orig_fp) fclose(orig_fp);
211 if (prefs_write_close(pfile) < 0)
212 g_warning("failed to write configuration to file\n");
215 debug_print("Configuration is saved.\n");
218 gint prefs_write_param(PrefParam *param, FILE *fp)
221 gchar buf[PREFSBUFSIZE];
223 for (i = 0; param[i].name != NULL; i++) {
224 switch (param[i].type) {
226 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
227 *((gchar **)param[i].data) ?
228 *((gchar **)param[i].data) : "");
231 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
232 *((gint *)param[i].data));
235 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
236 *((gboolean *)param[i].data));
239 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
240 *((DummyEnum *)param[i].data));
243 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
244 *((gushort *)param[i].data));
250 if (buf[0] != '\0') {
251 if (fputs(buf, fp) == EOF) {
261 PrefFile *prefs_write_open(const gchar *path)
267 g_return_val_if_fail(path != NULL, NULL);
269 if (prefs_is_readonly(path)) {
270 g_warning("no permission - %s\n", path);
274 tmppath = g_strconcat(path, ".tmp", NULL);
275 if ((fp = fopen(tmppath, "wb")) == NULL) {
276 FILE_OP_ERROR(tmppath, "fopen");
281 if (change_file_mode_rw(fp, tmppath) < 0)
282 FILE_OP_ERROR(tmppath, "chmod");
286 pfile = g_new(PrefFile, 1);
288 pfile->path = g_strdup(path);
293 gint prefs_write_close(PrefFile *pfile)
298 gchar *bakpath = NULL;
300 g_return_val_if_fail(pfile != NULL, -1);
306 tmppath = g_strconcat(path, ".tmp", NULL);
307 if (fclose(fp) == EOF) {
308 FILE_OP_ERROR(tmppath, "fclose");
315 if (is_file_exist(path)) {
316 bakpath = g_strconcat(path, ".bak", NULL);
317 if (rename(path, bakpath) < 0) {
318 FILE_OP_ERROR(path, "rename");
327 if (rename(tmppath, path) < 0) {
328 FILE_OP_ERROR(tmppath, "rename");
342 gint prefs_write_close_revert(PrefFile *pfile)
346 g_return_val_if_fail(pfile != NULL, -1);
348 tmppath = g_strconcat(pfile->path, ".tmp", NULL);
350 if (unlink(tmppath) < 0) FILE_OP_ERROR(tmppath, "unlink");
358 void prefs_set_default(PrefParam *param)
362 g_return_if_fail(param != NULL);
364 for (i = 0; param[i].name != NULL; i++) {
365 if (!param[i].data) continue;
367 switch (param[i].type) {
369 if (param[i].defval != NULL) {
370 if (!strncasecmp(param[i].defval, "ENV_", 4))
371 *((gchar **)param[i].data) =
372 g_strdup(g_getenv(param[i].defval + 4));
373 else if (param[i].defval[0] == '~')
374 *((gchar **)param[i].data) =
375 g_strconcat(get_home_dir(),
378 else if (param[i].defval[0] != '\0')
379 *((gchar **)param[i].data) =
380 g_strdup(param[i].defval);
382 *((gchar **)param[i].data) = NULL;
384 *((gchar **)param[i].data) = NULL;
387 if (param[i].defval != NULL)
388 *((gint *)param[i].data) =
389 (gint)atoi(param[i].defval);
391 *((gint *)param[i].data) = 0;
394 if (param[i].defval != NULL) {
395 if (!strcasecmp(param[i].defval, "TRUE"))
396 *((gboolean *)param[i].data) = TRUE;
398 *((gboolean *)param[i].data) =
399 atoi(param[i].defval) ? TRUE : FALSE;
401 *((gboolean *)param[i].data) = FALSE;
404 if (param[i].defval != NULL)
405 *((DummyEnum*)param[i].data) =
406 (DummyEnum)atoi(param[i].defval);
408 *((DummyEnum *)param[i].data) = 0;
411 if (param[i].defval != NULL)
412 *((gushort *)param[i].data) =
413 (gushort)atoi(param[i].defval);
415 *((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));
442 void prefs_dialog_create(PrefsDialog *dialog)
448 GtkWidget *confirm_area;
450 GtkWidget *cancel_btn;
451 GtkWidget *apply_btn;
453 g_return_if_fail(dialog != NULL);
455 window = gtk_window_new (GTK_WINDOW_DIALOG);
456 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
457 gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
458 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
459 gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
461 vbox = gtk_vbox_new (FALSE, 6);
462 gtk_widget_show(vbox);
463 gtk_container_add (GTK_CONTAINER (window), vbox);
465 notebook = gtk_notebook_new ();
466 gtk_widget_show(notebook);
467 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
468 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
469 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
470 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
472 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
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_dialog_destroy(PrefsDialog *dialog)
491 gtk_widget_destroy(dialog->window);
492 dialog->window = NULL;
493 dialog->notebook = NULL;
494 dialog->ok_btn = NULL;
495 dialog->cancel_btn = NULL;
496 dialog->apply_btn = NULL;
499 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
503 is_active = gtk_toggle_button_get_active(toggle_btn);
504 gtk_widget_set_sensitive(widget, is_active);
507 void prefs_set_dialog(PrefParam *param)
511 for (i = 0; param[i].name != NULL; i++) {
512 if (param[i].widget_set_func)
513 param[i].widget_set_func(¶m[i]);
517 void prefs_set_data_from_dialog(PrefParam *param)
521 for (i = 0; param[i].name != NULL; i++) {
522 if (param[i].data_set_func)
523 param[i].data_set_func(¶m[i]);
527 void prefs_set_dialog_to_default(PrefParam *param)
531 gchar *str_data = NULL;
537 for (i = 0; param[i].name != NULL; i++) {
538 if (!param[i].widget_set_func) continue;
542 switch (tmpparam.type) {
544 if (tmpparam.defval) {
545 if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
546 str_data = g_strdup(g_getenv(param[i].defval + 4));
547 tmpparam.data = &str_data;
549 } else if (tmpparam.defval[0] == '~') {
551 g_strconcat(get_home_dir(),
554 tmpparam.data = &str_data;
558 tmpparam.data = &tmpparam.defval;
562 int_data = atoi(tmpparam.defval);
565 tmpparam.data = &int_data;
569 ushort_data = atoi(tmpparam.defval);
572 tmpparam.data = &ushort_data;
575 if (tmpparam.defval) {
576 if (!strcasecmp(tmpparam.defval, "TRUE"))
579 bool_data = atoi(tmpparam.defval)
583 tmpparam.data = &bool_data;
587 enum_data = (DummyEnum)atoi(tmpparam.defval);
590 tmpparam.data = &enum_data;
595 tmpparam.widget_set_func(&tmpparam);
601 void prefs_set_data_from_entry(PrefParam *pparam)
603 gchar **str, *entry_str;
605 g_return_if_fail(*pparam->widget != NULL);
607 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
609 switch (pparam->type) {
611 str = (gchar **)pparam->data;
613 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
616 *((gushort *)pparam->data) = atoi(entry_str);
619 *((gint *)pparam->data) = atoi(entry_str);
622 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
627 void prefs_set_entry(PrefParam *pparam)
631 g_return_if_fail(*pparam->widget != NULL);
633 switch (pparam->type) {
635 str = (gchar **)pparam->data;
636 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
640 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
641 itos(*((gint *)pparam->data)));
644 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
645 itos(*((gushort *)pparam->data)));
648 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
653 void prefs_set_data_from_text(PrefParam *pparam)
659 g_return_if_fail(*pparam->widget != NULL);
661 switch (pparam->type) {
663 str = (gchar **)pparam->data;
665 tp = text = gtk_editable_get_chars
666 (GTK_EDITABLE(*pparam->widget), 0, -1);
667 if (text[0] == '\0') {
673 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
674 { *str = NULL; break; });
684 *str = g_strdup(tmp);
688 g_warning("Invalid PrefType for GtkText widget: %d\n",
693 void prefs_set_text(PrefParam *pparam)
695 gchar *buf, *sp, *bufp;
699 g_return_if_fail(*pparam->widget != NULL);
701 switch (pparam->type) {
703 str = (gchar **)pparam->data;
705 bufp = buf = alloca(strlen(*str) + 1);
710 if (*sp == '\\' && *(sp + 1) == 'n') {
721 text = GTK_TEXT(*pparam->widget);
722 gtk_text_set_point(text, 0);
723 gtk_text_forward_delete(text, gtk_text_get_length(text));
724 gtk_text_set_point(text, 0);
725 gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
728 g_warning("Invalid PrefType for GtkText widget: %d\n",
733 void prefs_set_data_from_toggle(PrefParam *pparam)
735 g_return_if_fail(pparam->type == P_BOOL);
736 g_return_if_fail(*pparam->widget != NULL);
738 *((gboolean *)pparam->data) =
739 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
742 void prefs_set_toggle(PrefParam *pparam)
744 g_return_if_fail(pparam->type == P_BOOL);
745 g_return_if_fail(*pparam->widget != NULL);
747 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
748 *((gboolean *)pparam->data));
751 void prefs_set_data_from_spinbtn(PrefParam *pparam)
753 g_return_if_fail(*pparam->widget != NULL);
755 switch (pparam->type) {
757 *((gint *)pparam->data) =
758 gtk_spin_button_get_value_as_int
759 (GTK_SPIN_BUTTON(*pparam->widget));
762 *((gushort *)pparam->data) =
763 (gushort)gtk_spin_button_get_value_as_int
764 (GTK_SPIN_BUTTON(*pparam->widget));
767 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
772 void prefs_set_spinbtn(PrefParam *pparam)
774 g_return_if_fail(*pparam->widget != NULL);
776 switch (pparam->type) {
778 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
779 (gfloat)*((gint *)pparam->data));
782 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
783 (gfloat)*((gushort *)pparam->data));
786 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
791 gboolean prefs_is_readonly(const gchar * path)
796 return (access(path, W_OK) != 0 && access(path, F_OK) == 0);
799 gboolean prefs_rc_is_readonly(const gchar * rcfile)
807 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
808 result = prefs_is_readonly(rcpath);