2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Claws Mail team
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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
28 #include <glib/gi18n.h>
38 #include "prefs_gtk.h"
39 #include "prefs_common.h"
42 #include "passcrypt.h"
46 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
47 #define RGB_FROM_GDK_COLOR(c) \
48 ((CL(c.red) << (gulong) 16) | \
49 (CL(c.green) << (gulong) 8) | \
52 #ifdef HAVE_FGETS_UNLOCKED
53 #define SC_FGETS fgets_unlocked
55 #define SC_FGETS fgets
63 static GHashTable
*whole_cache
= NULL
;
65 static gboolean
prefs_read_config_from_cache(PrefParam
*param
, const gchar
*label
,
68 void prefs_read_config(PrefParam
*param
, const gchar
*label
,
69 const gchar
*rcfile
, const gchar
*encoding
)
72 gchar buf
[PREFSBUFSIZE
];
75 g_return_if_fail(param
!= NULL
);
76 g_return_if_fail(label
!= NULL
);
77 g_return_if_fail(rcfile
!= NULL
);
80 g_warning("Encoding is ignored\n");
82 debug_print("Reading configuration...\n");
84 prefs_set_default(param
);
86 if (whole_cache
!= NULL
) {
87 if (prefs_read_config_from_cache(param
, label
, rcfile
) == TRUE
)
91 if ((fp
= g_fopen(rcfile
, "rb")) == NULL
) {
92 if (ENOENT
!= errno
) FILE_OP_ERROR(rcfile
, "fopen");
96 block_label
= g_strdup_printf("[%s]", label
);
98 #ifdef HAVE_FGETS_UNLOCKED
102 /* search aiming block */
103 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
109 conv_str
= conv_codeset_strdup
110 (buf
, encoding
, CS_INTERNAL
);
112 conv_str
= g_strdup(buf
);
114 (conv_str
, block_label
, strlen(block_label
));
117 val
= strncmp(buf
, block_label
, strlen(block_label
));
119 debug_print("Found %s\n", block_label
);
125 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
127 /* reached next block */
128 if (buf
[0] == '[') break;
129 if (buf
[0] == '#') continue;
134 conv_str
= conv_codeset_strdup
135 (buf
, encoding
, CS_INTERNAL
);
137 conv_str
= g_strdup(buf
);
138 prefs_config_parse_one_line(param
, conv_str
);
141 prefs_config_parse_one_line(param
, buf
);
144 debug_print("Finished reading configuration.\n");
145 #ifdef HAVE_FGETS_UNLOCKED
151 void prefs_config_parse_one_line(PrefParam
*param
, const gchar
*buf
)
158 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
159 name_len
= strlen(param
[i
].name
);
160 if (g_ascii_strncasecmp(buf
, param
[i
].name
, name_len
))
162 if (buf
[name_len
] != '=')
164 value
= buf
+ name_len
+ 1;
165 /* debug_print("%s = %s\n", param[i].name, value); */
167 switch (param
[i
].type
) {
173 if (g_utf8_validate(value
, -1, NULL
))
174 tmp
= g_strdup(value
);
176 tmp
= conv_codeset_strdup(value
,
177 conv_get_locale_charset_str_no_utf8(),
184 g_warning("failed to convert character set.");
185 tmp
= g_strdup(value
);
187 g_free(*((gchar
**)param
[i
].data
));
188 *((gchar
**)param
[i
].data
) = tmp
;
192 *((gint
*)param
[i
].data
) =
196 *((gboolean
*)param
[i
].data
) =
197 (*value
== '0' || *value
== '\0')
201 *((DummyEnum
*)param
[i
].data
) =
202 (DummyEnum
)atoi(value
);
205 *((gushort
*)param
[i
].data
) =
206 (gushort
)atoi(value
);
209 if (gdk_color_parse(value
, &color
))
210 *((gulong
*)param
[i
].data
) = RGB_FROM_GDK_COLOR(color
);
212 /* be compatible and accept ints */
213 *((gulong
*)param
[i
].data
) = strtoul(value
, 0, 10);
216 g_free(*((gchar
**)param
[i
].data
));
217 if (value
[0] == '!') {
221 len
= base64_decode(tmp
, &value
[1], strlen(value
) - 1);
222 passcrypt_decrypt(tmp
, len
);
224 *((gchar
**)param
[i
].data
) =
225 *tmp
? g_strdup(tmp
) : NULL
;
227 *((gchar
**)param
[i
].data
) =
228 *value
? g_strdup(value
) : NULL
;
240 g_warning("failed to write configuration to file\n"); \
241 if (orig_fp) fclose(orig_fp); \
242 prefs_file_close_revert(pfile); \
244 g_free(block_label); \
248 void prefs_write_config(PrefParam *param, const gchar *label,
254 gchar buf
[PREFSBUFSIZE
];
255 gchar
*block_label
= NULL
;
256 gboolean block_matched
= FALSE
;
258 g_return_if_fail(param
!= NULL
);
259 g_return_if_fail(label
!= NULL
);
260 g_return_if_fail(rcfile
!= NULL
);
262 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, rcfile
, NULL
);
263 if ((orig_fp
= g_fopen(rcpath
, "rb")) == NULL
) {
264 if (ENOENT
!= errno
) FILE_OP_ERROR(rcpath
, "fopen");
267 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
268 g_warning("failed to write configuration to file\n");
269 if (orig_fp
) fclose(orig_fp
);
274 block_label
= g_strdup_printf("[%s]", label
);
276 /* search aiming block */
278 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
281 val
= strncmp(buf
, block_label
, strlen(block_label
));
283 debug_print("Found %s\n", block_label
);
284 block_matched
= TRUE
;
287 TRY(fputs(buf
, pfile
->fp
) != EOF
);
291 TRY(fprintf(pfile
->fp
, "%s\n", block_label
) > 0);
293 /* write all param data to file */
294 TRY(prefs_write_param(param
, pfile
->fp
) == 0);
297 gboolean in_dup_block
= FALSE
;
298 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
301 TRY(fputc('\n', pfile
->fp
) != EOF
&&
302 fputs(buf
, pfile
->fp
) != EOF
);
306 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
308 if (!strncmp(buf
, block_label
,
309 strlen(block_label
)))
312 in_dup_block
= FALSE
;
315 TRY(fputs(buf
, pfile
->fp
) != EOF
);
322 if (orig_fp
) fclose(orig_fp
);
323 if (prefs_file_close(pfile
) < 0)
324 g_warning("failed to write configuration to file\n");
327 debug_print("Configuration is saved.\n");
330 gint
prefs_write_param(PrefParam
*param
, FILE *fp
)
333 gchar buf
[PREFSBUFSIZE
];
335 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
336 switch (param
[i
].type
) {
341 if (*((gchar
**)param
[i
].data
)) {
342 if (g_utf8_validate(*((gchar
**)param
[i
].data
), -1, NULL
))
343 tmp
= g_strdup(*((gchar
**)param
[i
].data
));
345 tmp
= conv_codeset_strdup(*((gchar
**)param
[i
].data
),
346 conv_get_locale_charset_str_no_utf8(),
349 tmp
= g_strdup(*((gchar
**)param
[i
].data
));
353 g_snprintf(buf
, sizeof(buf
), "%s=%s\n", param
[i
].name
,
360 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
361 *((gint
*)param
[i
].data
));
364 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
365 *((gboolean
*)param
[i
].data
));
368 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
369 *((DummyEnum
*)param
[i
].data
));
372 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
373 *((gushort
*)param
[i
].data
));
376 g_snprintf(buf
, sizeof buf
, "%s=#%6.6lx\n", param
[i
].name
,
377 *((gulong
*) param
[i
].data
));
381 gchar
*tmp
= NULL
, tmp2
[1024] = {0};
383 tmp
= *((gchar
**)param
[i
].data
);
389 passcrypt_encrypt(tmp
, len
);
390 base64_encode(tmp2
, tmp
, len
);
394 g_snprintf(buf
, sizeof(buf
), "%s=!%s\n", param
[i
].name
,
400 /* unrecognized, fail */
401 debug_print("Unrecognized parameter type\n");
405 if (buf
[0] != '\0') {
406 if (fputs(buf
, fp
) == EOF
) {
416 void prefs_set_default(PrefParam
*param
)
421 g_return_if_fail(param
!= NULL
);
423 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
424 if (!param
[i
].data
) continue;
426 switch (param
[i
].type
) {
429 g_free(*((gchar
**)param
[i
].data
));
430 if (param
[i
].defval
!= NULL
) {
431 if (!strncasecmp(param
[i
].defval
, "ENV_", 4)) {
435 envstr
= g_getenv(param
[i
].defval
+ 4);
436 tmp
= envstr
&& *envstr
?
437 conv_codeset_strdup(envstr
,
438 conv_get_locale_charset_str(),
442 g_warning("faild to convert character set.");
443 tmp
= g_strdup(envstr
);
445 *((gchar
**)param
[i
].data
) = tmp
;
446 } else if (param
[i
].defval
[0] == '~')
447 *((gchar
**)param
[i
].data
) =
448 g_strconcat(get_home_dir(),
451 else if (param
[i
].defval
[0] != '\0')
452 *((gchar
**)param
[i
].data
) =
453 g_strdup(param
[i
].defval
);
455 *((gchar
**)param
[i
].data
) = NULL
;
457 *((gchar
**)param
[i
].data
) = NULL
;
460 if (param
[i
].defval
!= NULL
)
461 *((gint
*)param
[i
].data
) =
462 (gint
)atoi(param
[i
].defval
);
464 *((gint
*)param
[i
].data
) = 0;
467 if (param
[i
].defval
!= NULL
) {
468 if (!g_ascii_strcasecmp(param
[i
].defval
, "TRUE"))
469 *((gboolean
*)param
[i
].data
) = TRUE
;
471 *((gboolean
*)param
[i
].data
) =
472 atoi(param
[i
].defval
) ? TRUE
: FALSE
;
474 *((gboolean
*)param
[i
].data
) = FALSE
;
477 if (param
[i
].defval
!= NULL
)
478 *((DummyEnum
*)param
[i
].data
) =
479 (DummyEnum
)atoi(param
[i
].defval
);
481 *((DummyEnum
*)param
[i
].data
) = 0;
484 if (param
[i
].defval
!= NULL
)
485 *((gushort
*)param
[i
].data
) =
486 (gushort
)atoi(param
[i
].defval
);
488 *((gushort
*)param
[i
].data
) = 0;
491 if (param
[i
].defval
!= NULL
&& gdk_color_parse(param
[i
].defval
, &color
))
492 *((gulong
*)param
[i
].data
) =
493 RGB_FROM_GDK_COLOR(color
);
494 else if (param
[i
].defval
)
495 /* be compatible and accept ints */
496 *((gulong
*)param
[i
].data
) = strtoul(param
[i
].defval
, 0, 10);
498 *((gulong
*)param
[i
].data
) = 0;
506 void prefs_free(PrefParam
*param
)
510 g_return_if_fail(param
!= NULL
);
512 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
513 if (!param
[i
].data
) continue;
515 switch (param
[i
].type
) {
518 g_free(*((gchar
**)param
[i
].data
));
526 void prefs_dialog_create(PrefsDialog
*dialog
)
532 GtkWidget
*confirm_area
;
534 GtkWidget
*cancel_btn
;
535 GtkWidget
*apply_btn
;
537 g_return_if_fail(dialog
!= NULL
);
539 window
= gtk_window_new (GTK_WINDOW_TOPLEVEL
);
540 gtk_container_set_border_width (GTK_CONTAINER (window
), 8);
541 gtk_window_set_position (GTK_WINDOW(window
), GTK_WIN_POS_CENTER
);
542 gtk_window_set_modal (GTK_WINDOW (window
), TRUE
);
543 gtk_window_set_resizable(GTK_WINDOW(window
), TRUE
);
545 vbox
= gtk_vbox_new (FALSE
, 6);
546 gtk_widget_show(vbox
);
547 gtk_container_add (GTK_CONTAINER (window
), vbox
);
549 notebook
= gtk_notebook_new ();
550 gtk_widget_show(notebook
);
551 gtk_box_pack_start (GTK_BOX (vbox
), notebook
, TRUE
, TRUE
, 0);
552 gtk_container_set_border_width (GTK_CONTAINER (notebook
), 2);
553 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
554 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook
), TRUE
);
556 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook
));
558 gtkut_stock_button_set_create(&confirm_area
,
559 &ok_btn
, GTK_STOCK_OK
,
560 &cancel_btn
, GTK_STOCK_CANCEL
,
561 &apply_btn
, GTK_STOCK_APPLY
);
562 gtk_widget_show(confirm_area
);
563 gtk_box_pack_end (GTK_BOX(vbox
), confirm_area
, FALSE
, FALSE
, 0);
564 gtk_widget_grab_default(ok_btn
);
566 dialog
->window
= window
;
567 dialog
->notebook
= notebook
;
568 dialog
->ok_btn
= ok_btn
;
569 dialog
->cancel_btn
= cancel_btn
;
570 dialog
->apply_btn
= apply_btn
;
573 void prefs_dialog_destroy(PrefsDialog
*dialog
)
575 gtk_widget_destroy(dialog
->window
);
576 dialog
->window
= NULL
;
577 dialog
->notebook
= NULL
;
578 dialog
->ok_btn
= NULL
;
579 dialog
->cancel_btn
= NULL
;
580 dialog
->apply_btn
= NULL
;
583 void prefs_button_toggled(GtkToggleButton
*toggle_btn
, GtkWidget
*widget
)
587 is_active
= gtk_toggle_button_get_active(toggle_btn
);
588 gtk_widget_set_sensitive(widget
, is_active
);
591 void prefs_button_toggled_reverse(GtkToggleButton
*toggle_btn
, GtkWidget
*widget
)
595 is_active
= gtk_toggle_button_get_active(toggle_btn
);
596 gtk_widget_set_sensitive(widget
, !is_active
);
599 void prefs_set_dialog(PrefParam
*param
)
603 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
604 if (param
[i
].widget_set_func
)
605 param
[i
].widget_set_func(¶m
[i
]);
609 void prefs_set_data_from_dialog(PrefParam
*param
)
613 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
614 if (param
[i
].data_set_func
)
615 param
[i
].data_set_func(¶m
[i
]);
619 void prefs_set_dialog_to_default(PrefParam
*param
)
623 gchar
*str_data
= NULL
;
629 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
630 if (!param
[i
].widget_set_func
) continue;
634 switch (tmpparam
.type
) {
637 if (tmpparam
.defval
) {
638 if (!g_ascii_strncasecmp(tmpparam
.defval
, "ENV_", 4)) {
639 str_data
= g_strdup(g_getenv(param
[i
].defval
+ 4));
640 tmpparam
.data
= &str_data
;
642 } else if (tmpparam
.defval
[0] == '~') {
644 g_strconcat(get_home_dir(),
647 tmpparam
.data
= &str_data
;
651 tmpparam
.data
= &tmpparam
.defval
;
655 int_data
= atoi(tmpparam
.defval
);
658 tmpparam
.data
= &int_data
;
662 ushort_data
= atoi(tmpparam
.defval
);
665 tmpparam
.data
= &ushort_data
;
668 if (tmpparam
.defval
) {
669 if (!g_ascii_strcasecmp(tmpparam
.defval
, "TRUE"))
672 bool_data
= atoi(tmpparam
.defval
)
676 tmpparam
.data
= &bool_data
;
680 enum_data
= (DummyEnum
)atoi(tmpparam
.defval
);
683 tmpparam
.data
= &enum_data
;
689 tmpparam
.widget_set_func(&tmpparam
);
695 void prefs_set_data_from_entry(PrefParam
*pparam
)
698 const gchar
*entry_str
;
700 g_return_if_fail(*pparam
->widget
!= NULL
);
702 entry_str
= gtk_entry_get_text(GTK_ENTRY(*pparam
->widget
));
704 switch (pparam
->type
) {
707 str
= (gchar
**)pparam
->data
;
709 *str
= entry_str
[0] ? g_strdup(entry_str
) : NULL
;
712 *((gushort
*)pparam
->data
) = atoi(entry_str
);
715 *((gint
*)pparam
->data
) = atoi(entry_str
);
718 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
723 void prefs_set_entry(PrefParam
*pparam
)
727 g_return_if_fail(*pparam
->widget
!= NULL
);
729 switch (pparam
->type
) {
732 str
= (gchar
**)pparam
->data
;
733 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
737 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
738 itos(*((gint
*)pparam
->data
)));
741 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
742 itos(*((gushort
*)pparam
->data
)));
745 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
750 void prefs_set_data_from_text(PrefParam
*pparam
)
753 gchar
*text
= NULL
, *tp
= NULL
;
756 g_return_if_fail(*pparam
->widget
!= NULL
);
758 switch (pparam
->type
) {
761 str
= (gchar
**)pparam
->data
;
763 if (GTK_IS_EDITABLE(*pparam
->widget
)) { /* need? */
764 tp
= text
= gtk_editable_get_chars
765 (GTK_EDITABLE(*pparam
->widget
), 0, -1);
766 } else if (GTK_IS_TEXT_VIEW(*pparam
->widget
)) {
767 GtkTextView
*textview
= GTK_TEXT_VIEW(*pparam
->widget
);
768 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(textview
);
769 GtkTextIter start
, end
;
770 gtk_text_buffer_get_start_iter(buffer
, &start
);
771 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, -1);
772 tp
= text
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
775 g_return_if_fail (tp
&& text
);
777 if (text
[0] == '\0') {
783 Xalloca(tmpp
= tmp
, strlen(text
) * 2 + 1,
784 { *str
= NULL
; break; });
794 *str
= g_strdup(tmp
);
798 g_warning("Invalid PrefType for GtkText widget: %d\n",
803 void prefs_set_text(PrefParam
*pparam
)
805 gchar
*buf
, *sp
, *bufp
;
808 GtkTextBuffer
*buffer
;
811 g_return_if_fail(*pparam
->widget
!= NULL
);
813 switch (pparam
->type
) {
816 str
= (gchar
**)pparam
->data
;
818 bufp
= buf
= alloca(strlen(*str
) + 1);
823 if (*sp
== '\\' && *(sp
+ 1) == 'n') {
834 text
= GTK_TEXT_VIEW(*pparam
->widget
);
835 buffer
= gtk_text_view_get_buffer(text
);
836 gtk_text_buffer_set_text(buffer
, "", -1);
837 gtk_text_buffer_get_start_iter(buffer
, &iter
);
838 gtk_text_buffer_insert(buffer
, &iter
, buf
, -1);
841 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
846 void prefs_set_data_from_toggle(PrefParam
*pparam
)
848 g_return_if_fail(pparam
->type
== P_BOOL
);
849 g_return_if_fail(*pparam
->widget
!= NULL
);
851 *((gboolean
*)pparam
->data
) =
852 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam
->widget
));
855 void prefs_set_toggle(PrefParam
*pparam
)
857 g_return_if_fail(pparam
->type
== P_BOOL
);
858 g_return_if_fail(*pparam
->widget
!= NULL
);
860 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam
->widget
),
861 *((gboolean
*)pparam
->data
));
864 void prefs_set_data_from_spinbtn(PrefParam
*pparam
)
866 g_return_if_fail(*pparam
->widget
!= NULL
);
868 switch (pparam
->type
) {
870 *((gint
*)pparam
->data
) =
871 gtk_spin_button_get_value_as_int
872 (GTK_SPIN_BUTTON(*pparam
->widget
));
875 *((gushort
*)pparam
->data
) =
876 (gushort
)gtk_spin_button_get_value_as_int
877 (GTK_SPIN_BUTTON(*pparam
->widget
));
880 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
885 void prefs_set_spinbtn(PrefParam
*pparam
)
887 g_return_if_fail(*pparam
->widget
!= NULL
);
889 switch (pparam
->type
) {
891 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam
->widget
),
892 (gfloat
)*((gint
*)pparam
->data
));
895 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam
->widget
),
896 (gfloat
)*((gushort
*)pparam
->data
));
899 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
904 static GSList
*prefs_pages
= NULL
;
906 void prefs_gtk_open(void)
908 prefswindow_open(_("Preferences"), prefs_pages
, NULL
,
909 &prefs_common
.prefswin_width
, &prefs_common
.prefswin_height
);
912 void prefs_gtk_register_page(PrefsPage
*page
)
914 prefs_pages
= g_slist_append(prefs_pages
, page
);
917 void prefs_gtk_unregister_page(PrefsPage
*page
)
919 prefs_pages
= g_slist_remove(prefs_pages
, page
);
922 static void prefs_destroy_whole_cache(gpointer to_free
)
924 GHashTable
*table
= (GHashTable
*)to_free
;
925 g_hash_table_destroy(table
);
928 static void prefs_destroy_file_cache(gpointer to_free
)
930 GHashTable
*table
= (GHashTable
*)to_free
;
931 g_hash_table_destroy(table
);
934 static int prefs_cache_sections(GHashTable
*file_cache
, const gchar
*rcfile
)
936 FILE *fp
= g_fopen(rcfile
, "rb");
937 gchar buf
[PREFSBUFSIZE
];
938 GHashTable
*section_cache
= NULL
;
941 debug_print("cache: %s: %s", rcfile
, strerror(errno
));
945 #ifdef HAVE_FGETS_UNLOCKED
949 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
954 continue; /* comment */
955 if (buf
[0] == '[') { /* new section */
956 gchar
*blockname
= g_strdup(buf
+1);
958 if (strrchr(blockname
, ']'))
959 *strrchr(blockname
, ']') = '\0';
961 if ((section_cache
= g_hash_table_lookup(file_cache
, blockname
)) == NULL
) {
962 debug_print("new section '%s'\n", blockname
);
963 section_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
965 g_hash_table_insert(file_cache
,
966 blockname
, section_cache
);
968 debug_print("section '%s' already done\n", blockname
);
970 section_cache
= NULL
;
974 if (!section_cache
) {
975 debug_print("skipping stuff %s with no section\n", buf
);
980 if (!strchr(buf
, '=')) {
981 /* plugins do differently */
984 pref
= g_strdup(buf
);
986 //debug_print("new pref '%s'\n", pref);
987 g_hash_table_insert(section_cache
, pref
, GINT_TO_POINTER(1));
991 #ifdef HAVE_FGETS_UNLOCKED
998 static int prefs_cache(const gchar
*rcfile
)
1000 GHashTable
*file_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1001 g_free
, prefs_destroy_file_cache
);
1003 debug_print("new file '%s'\n", rcfile
);
1004 g_hash_table_insert(whole_cache
, g_strdup(rcfile
), file_cache
);
1006 return prefs_cache_sections(file_cache
, rcfile
);
1009 void prefs_prepare_cache(void)
1011 gchar
*clawsrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
1012 gchar
*folderitemrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, FOLDERITEM_RC
, NULL
);
1013 gchar
*accountrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, ACCOUNT_RC
, NULL
);
1015 if (whole_cache
== NULL
) {
1016 whole_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1017 g_free
, prefs_destroy_whole_cache
);
1019 debug_print("already cached\n");
1021 g_free(folderitemrc
);
1025 if (prefs_cache(clawsrc
) < 0 ||
1026 prefs_cache(folderitemrc
) < 0 ||
1027 prefs_cache(accountrc
) < 0)
1028 prefs_destroy_cache();
1031 g_free(folderitemrc
);
1035 void prefs_destroy_cache(void)
1038 debug_print("no cache\n");
1041 debug_print("destroying cache\n");
1042 g_hash_table_destroy(whole_cache
);
1047 static void prefs_parse_cache(gpointer key
, gpointer value
, gpointer user_data
)
1049 gchar
*pref
= (gchar
*)key
;
1051 PrefParam
*param
= (PrefParam
*)user_data
;
1053 prefs_config_parse_one_line(param
, pref
);
1056 static gboolean
prefs_read_config_from_cache(PrefParam
*param
, const gchar
*label
,
1057 const gchar
*rcfile
)
1059 GHashTable
*sections_table
= NULL
;
1060 GHashTable
*values_table
= NULL
;
1061 sections_table
= g_hash_table_lookup(whole_cache
, rcfile
);
1063 if (sections_table
== NULL
) {
1064 g_warning("can't find %s in the whole cache\n", rcfile
);
1067 values_table
= g_hash_table_lookup(sections_table
, label
);
1069 if (values_table
== NULL
) {
1070 debug_print("no '%s' section in '%s' cache\n", label
, rcfile
);
1073 g_hash_table_foreach(values_table
, prefs_parse_cache
, param
);