2 * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2015 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 3 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, see <http://www.gnu.org/licenses/>.
22 #include "claws-features.h"
29 #include <glib/gi18n.h>
39 #include "prefs_gtk.h"
40 #include "prefs_common.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 static void prefs_config_parse_one_line(PrefParam
*param
,
71 void prefs_read_config(PrefParam
*param
, const gchar
*label
,
72 const gchar
*rcfile
, const gchar
*encoding
)
75 gchar buf
[PREFSBUFSIZE
];
78 cm_return_if_fail(param
!= NULL
);
79 cm_return_if_fail(label
!= NULL
);
80 cm_return_if_fail(rcfile
!= NULL
);
83 g_warning("Encoding is ignored");
85 debug_print("Reading configuration...\n");
87 prefs_set_default(param
);
89 if (whole_cache
!= NULL
) {
90 if (prefs_read_config_from_cache(param
, label
, rcfile
) == TRUE
)
94 if ((fp
= g_fopen(rcfile
, "rb")) == NULL
) {
95 if (ENOENT
!= errno
) FILE_OP_ERROR(rcfile
, "fopen");
99 block_label
= g_strdup_printf("[%s]", label
);
101 #ifdef HAVE_FGETS_UNLOCKED
105 /* search aiming block */
106 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
112 conv_str
= conv_codeset_strdup
113 (buf
, encoding
, CS_INTERNAL
);
115 conv_str
= g_strdup(buf
);
117 (conv_str
, block_label
, strlen(block_label
));
120 val
= strncmp(buf
, block_label
, strlen(block_label
));
122 debug_print("Found %s\n", block_label
);
128 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
130 /* reached next block */
131 if (buf
[0] == '[') break;
132 if (buf
[0] == '#') continue;
137 conv_str
= conv_codeset_strdup
138 (buf
, encoding
, CS_INTERNAL
);
140 conv_str
= g_strdup(buf
);
141 prefs_config_parse_one_line(param
, conv_str
);
144 prefs_config_parse_one_line(param
, buf
);
147 debug_print("Finished reading configuration.\n");
148 #ifdef HAVE_FGETS_UNLOCKED
154 static void prefs_config_parse_one_line(PrefParam
*param
, const gchar
*buf
)
161 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
162 name_len
= strlen(param
[i
].name
);
163 if (g_ascii_strncasecmp(buf
, param
[i
].name
, name_len
))
165 if (buf
[name_len
] != '=')
167 value
= buf
+ name_len
+ 1;
168 /* debug_print("%s = %s\n", param[i].name, value); */
170 switch (param
[i
].type
) {
177 if (g_utf8_validate(value
, -1, NULL
))
178 tmp
= g_strdup(value
);
180 tmp
= conv_codeset_strdup(value
,
181 conv_get_locale_charset_str_no_utf8(),
188 g_warning("Failed to convert character set.");
189 tmp
= g_strdup(value
);
191 g_free(*((gchar
**)param
[i
].data
));
192 *((gchar
**)param
[i
].data
) = tmp
;
196 *((gint
*)param
[i
].data
) =
200 *((gboolean
*)param
[i
].data
) =
201 (*value
== '0' || *value
== '\0')
205 *((DummyEnum
*)param
[i
].data
) =
206 (DummyEnum
)atoi(value
);
209 *((gushort
*)param
[i
].data
) =
210 (gushort
)atoi(value
);
213 if (gdk_color_parse(value
, &color
)) {
214 *((gulong
*)param
[i
].data
) = RGB_FROM_GDK_COLOR(color
);
217 /* be compatible and accept ints */
218 *((gulong
*)param
[i
].data
) = strtoul(value
, 0, 10);
229 g_warning("Failed to write configuration to file"); \
230 if (orig_fp) fclose(orig_fp); \
231 prefs_file_close_revert(pfile); \
233 g_free(block_label); \
237 void prefs_write_config(PrefParam *param, const gchar *label,
243 gchar buf
[PREFSBUFSIZE
];
244 gchar
*block_label
= NULL
;
245 gboolean block_matched
= FALSE
;
247 cm_return_if_fail(param
!= NULL
);
248 cm_return_if_fail(label
!= NULL
);
249 cm_return_if_fail(rcfile
!= NULL
);
251 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, rcfile
, NULL
);
252 if ((orig_fp
= g_fopen(rcpath
, "rb")) == NULL
) {
253 if (ENOENT
!= errno
) FILE_OP_ERROR(rcpath
, "fopen");
256 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
257 g_warning("Failed to write configuration to file");
258 if (orig_fp
) fclose(orig_fp
);
263 block_label
= g_strdup_printf("[%s]", label
);
265 /* search aiming block */
267 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
270 val
= strncmp(buf
, block_label
, strlen(block_label
));
272 debug_print("Found %s\n", block_label
);
273 block_matched
= TRUE
;
276 TRY(fputs(buf
, pfile
->fp
) != EOF
);
280 TRY(fprintf(pfile
->fp
, "%s\n", block_label
) > 0);
282 /* write all param data to file */
283 TRY(prefs_write_param(param
, pfile
->fp
) == 0);
286 gboolean in_dup_block
= FALSE
;
287 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
290 TRY(fputc('\n', pfile
->fp
) != EOF
&&
291 fputs(buf
, pfile
->fp
) != EOF
);
295 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
297 if (!strncmp(buf
, block_label
,
298 strlen(block_label
)))
301 in_dup_block
= FALSE
;
304 TRY(fputs(buf
, pfile
->fp
) != EOF
);
311 if (orig_fp
) fclose(orig_fp
);
312 if (prefs_file_close(pfile
) < 0)
313 g_warning("Failed to write configuration to file");
316 debug_print("Configuration is saved.\n");
319 gint
prefs_write_param(PrefParam
*param
, FILE *fp
)
322 gchar buf
[PREFSBUFSIZE
] = "";
324 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
325 switch (param
[i
].type
) {
330 if (*((gchar
**)param
[i
].data
)) {
331 if (g_utf8_validate(*((gchar
**)param
[i
].data
), -1, NULL
))
332 tmp
= g_strdup(*((gchar
**)param
[i
].data
));
334 tmp
= conv_codeset_strdup(*((gchar
**)param
[i
].data
),
335 conv_get_locale_charset_str_no_utf8(),
338 tmp
= g_strdup(*((gchar
**)param
[i
].data
));
342 g_snprintf(buf
, sizeof(buf
), "%s=%s\n", param
[i
].name
,
349 buf
[0] = '\0'; /* Passwords are written to password store. */
352 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
353 *((gint
*)param
[i
].data
));
356 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
357 *((gboolean
*)param
[i
].data
));
360 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
361 *((DummyEnum
*)param
[i
].data
));
364 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
365 *((gushort
*)param
[i
].data
));
368 g_snprintf(buf
, sizeof buf
, "%s=#%6.6lx\n", param
[i
].name
,
369 *((gulong
*) param
[i
].data
));
372 /* unrecognized, fail */
373 debug_print("Unrecognized parameter type\n");
377 if (buf
[0] != '\0') {
378 if (fputs(buf
, fp
) == EOF
) {
388 void prefs_set_default(PrefParam
*param
)
393 cm_return_if_fail(param
!= NULL
);
395 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
396 if (!param
[i
].data
) continue;
398 switch (param
[i
].type
) {
400 g_free(*((gchar
**)param
[i
].data
));
401 if (param
[i
].defval
!= NULL
) {
402 if (!strncasecmp(param
[i
].defval
, "ENV_", 4)) {
406 envstr
= g_getenv(param
[i
].defval
+ 4);
407 tmp
= envstr
&& *envstr
?
408 conv_codeset_strdup(envstr
,
409 conv_get_locale_charset_str(),
413 g_warning("Failed to convert character set.");
414 tmp
= g_strdup(envstr
);
416 *((gchar
**)param
[i
].data
) = tmp
;
417 } else if (param
[i
].defval
[0] == '~')
418 *((gchar
**)param
[i
].data
) =
419 g_strconcat(get_home_dir(),
423 *((gchar
**)param
[i
].data
) =
424 g_strdup(param
[i
].defval
);
426 *((gchar
**)param
[i
].data
) = NULL
;
429 g_free(*((gchar
**)param
[i
].data
));
430 if (param
[i
].defval
!= NULL
) {
431 if (param
[i
].defval
[0] != '\0')
432 *((gchar
**)param
[i
].data
) =
433 g_strdup(param
[i
].defval
);
435 *((gchar
**)param
[i
].data
) = NULL
;
437 *((gchar
**)param
[i
].data
) = NULL
;
440 if (param
[i
].defval
!= NULL
)
441 *((gint
*)param
[i
].data
) =
442 (gint
)atoi(param
[i
].defval
);
444 *((gint
*)param
[i
].data
) = 0;
447 if (param
[i
].defval
!= NULL
) {
448 if (!g_ascii_strcasecmp(param
[i
].defval
, "TRUE"))
449 *((gboolean
*)param
[i
].data
) = TRUE
;
451 *((gboolean
*)param
[i
].data
) = atoi(param
[i
].defval
) ? TRUE
: FALSE
;
453 *((gboolean
*)param
[i
].data
) = FALSE
;
456 if (param
[i
].defval
!= NULL
)
457 *((DummyEnum
*)param
[i
].data
) =
458 (DummyEnum
)atoi(param
[i
].defval
);
460 *((DummyEnum
*)param
[i
].data
) = 0;
463 if (param
[i
].defval
!= NULL
)
464 *((gushort
*)param
[i
].data
) = (gushort
)atoi(param
[i
].defval
);
466 *((gushort
*)param
[i
].data
) = 0;
469 if (param
[i
].defval
!= NULL
&& gdk_color_parse(param
[i
].defval
, &color
))
470 *((gulong
*)param
[i
].data
) = RGB_FROM_GDK_COLOR(color
);
471 else if (param
[i
].defval
)
472 /* be compatible and accept ints */
473 *((gulong
*)param
[i
].data
) = strtoul(param
[i
].defval
, 0, 10);
475 *((gulong
*)param
[i
].data
) = 0;
483 void prefs_free(PrefParam
*param
)
487 cm_return_if_fail(param
!= NULL
);
489 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
490 if (!param
[i
].data
) continue;
492 switch (param
[i
].type
) {
495 g_free(*((gchar
**)param
[i
].data
));
503 void prefs_button_toggled(GtkToggleButton
*toggle_btn
, GtkWidget
*widget
)
507 is_active
= gtk_toggle_button_get_active(toggle_btn
);
508 gtk_widget_set_sensitive(widget
, is_active
);
511 void prefs_button_toggled_reverse(GtkToggleButton
*toggle_btn
, GtkWidget
*widget
)
515 is_active
= gtk_toggle_button_get_active(toggle_btn
);
516 gtk_widget_set_sensitive(widget
, !is_active
);
519 void prefs_set_dialog(PrefParam
*param
)
523 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
524 if (param
[i
].widget_set_func
)
525 param
[i
].widget_set_func(¶m
[i
]);
529 void prefs_set_data_from_dialog(PrefParam
*param
)
533 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
534 if (param
[i
].data_set_func
)
535 param
[i
].data_set_func(¶m
[i
]);
539 void prefs_set_dialog_to_default(PrefParam
*param
)
543 gchar
*str_data
= NULL
;
549 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
550 if (!param
[i
].widget_set_func
) continue;
554 switch (tmpparam
.type
) {
556 if (tmpparam
.defval
) {
557 if (!g_ascii_strncasecmp(tmpparam
.defval
, "ENV_", 4)) {
558 str_data
= g_strdup(g_getenv(param
[i
].defval
+ 4));
559 tmpparam
.data
= &str_data
;
561 } else if (tmpparam
.defval
[0] == '~') {
563 g_strconcat(get_home_dir(),
566 tmpparam
.data
= &str_data
;
570 tmpparam
.data
= &tmpparam
.defval
;
573 tmpparam
.data
= &tmpparam
.defval
;
577 int_data
= atoi(tmpparam
.defval
);
580 tmpparam
.data
= &int_data
;
584 ushort_data
= atoi(tmpparam
.defval
);
587 tmpparam
.data
= &ushort_data
;
590 if (tmpparam
.defval
) {
591 if (!g_ascii_strcasecmp(tmpparam
.defval
, "TRUE"))
594 bool_data
= atoi(tmpparam
.defval
)
598 tmpparam
.data
= &bool_data
;
602 enum_data
= (DummyEnum
)atoi(tmpparam
.defval
);
605 tmpparam
.data
= &enum_data
;
611 tmpparam
.widget_set_func(&tmpparam
);
617 void prefs_set_data_from_entry(PrefParam
*pparam
)
620 const gchar
*entry_str
;
622 cm_return_if_fail(*pparam
->widget
!= NULL
);
624 entry_str
= gtk_entry_get_text(GTK_ENTRY(*pparam
->widget
));
626 switch (pparam
->type
) {
628 str
= (gchar
**)pparam
->data
;
630 *str
= entry_str
[0] ? g_strdup(entry_str
) : NULL
;
635 *((gushort
*)pparam
->data
) = atoi(entry_str
);
638 *((gint
*)pparam
->data
) = atoi(entry_str
);
641 g_warning("Invalid PrefType for GtkEntry widget: %d",
646 void prefs_set_escaped_data_from_entry(PrefParam
*pparam
)
650 cm_return_if_fail(*pparam
->widget
!= NULL
);
652 switch (pparam
->type
) {
654 str
= (gchar
**)pparam
->data
;
656 *str
= pref_get_pref_from_entry(GTK_ENTRY(*pparam
->widget
));
659 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
664 void prefs_set_entry(PrefParam
*pparam
)
667 cm_return_if_fail(*pparam
->widget
!= NULL
);
669 switch (pparam
->type
) {
671 str
= (gchar
**)pparam
->data
;
672 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
676 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
677 itos(*((gint
*)pparam
->data
)));
680 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
681 itos(*((gushort
*)pparam
->data
)));
684 g_warning("Invalid PrefType for GtkEntry widget: %d",
689 void prefs_set_entry_from_escaped(PrefParam
*pparam
)
693 cm_return_if_fail(*pparam
->widget
!= NULL
);
695 switch (pparam
->type
) {
697 str
= (gchar
**)pparam
->data
;
698 pref_set_entry_from_pref(GTK_ENTRY(*pparam
->widget
),
702 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
707 void prefs_set_data_from_text(PrefParam
*pparam
)
710 gchar
*text
= NULL
, *tp
= NULL
;
713 cm_return_if_fail(*pparam
->widget
!= NULL
);
715 switch (pparam
->type
) {
717 str
= (gchar
**)pparam
->data
;
719 if (GTK_IS_EDITABLE(*pparam
->widget
)) { /* need? */
720 tp
= text
= gtk_editable_get_chars
721 (GTK_EDITABLE(*pparam
->widget
), 0, -1);
722 } else if (GTK_IS_TEXT_VIEW(*pparam
->widget
)) {
723 GtkTextView
*textview
= GTK_TEXT_VIEW(*pparam
->widget
);
724 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(textview
);
725 GtkTextIter start
, end
;
726 gtk_text_buffer_get_start_iter(buffer
, &start
);
727 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, -1);
728 tp
= text
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
731 cm_return_if_fail (tp
&& text
);
733 if (text
[0] == '\0') {
739 Xalloca(tmpp
= tmp
, strlen(text
) * 2 + 1,
740 { *str
= NULL
; break; });
750 *str
= g_strdup(tmp
);
754 g_warning("Invalid PrefType for GtkText widget: %d",
759 void prefs_set_escaped_data_from_text(PrefParam
*pparam
)
763 cm_return_if_fail(*pparam
->widget
!= NULL
);
765 switch (pparam
->type
) {
767 str
= (gchar
**)pparam
->data
;
769 *str
= pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam
->widget
));
772 g_warning("Invalid escaped PrefType for GtkText widget: %d",
777 void prefs_set_text(PrefParam
*pparam
)
779 gchar
*buf
, *sp
, *bufp
;
782 GtkTextBuffer
*buffer
;
785 cm_return_if_fail(*pparam
->widget
!= NULL
);
787 switch (pparam
->type
) {
789 str
= (gchar
**)pparam
->data
;
791 bufp
= buf
= alloca(strlen(*str
) + 1);
796 if (*sp
== '\\' && *(sp
+ 1) == 'n') {
807 text
= GTK_TEXT_VIEW(*pparam
->widget
);
808 buffer
= gtk_text_view_get_buffer(text
);
809 gtk_text_buffer_set_text(buffer
, "", -1);
810 gtk_text_buffer_get_start_iter(buffer
, &iter
);
811 gtk_text_buffer_insert(buffer
, &iter
, buf
, -1);
814 g_warning("Invalid PrefType for GtkTextView widget: %d",
819 void prefs_set_text_from_escaped(PrefParam
*pparam
)
823 cm_return_if_fail(*pparam
->widget
!= NULL
);
825 switch (pparam
->type
) {
827 str
= (gchar
**)pparam
->data
;
828 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam
->widget
),
832 g_warning("Invalid escaped PrefType for GtkTextView widget: %d",
837 void prefs_set_data_from_toggle(PrefParam
*pparam
)
839 cm_return_if_fail(pparam
->type
== P_BOOL
);
840 cm_return_if_fail(*pparam
->widget
!= NULL
);
842 *((gboolean
*)pparam
->data
) =
843 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam
->widget
));
846 void prefs_set_toggle(PrefParam
*pparam
)
848 cm_return_if_fail(pparam
->type
== P_BOOL
);
849 cm_return_if_fail(*pparam
->widget
!= NULL
);
851 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam
->widget
),
852 *((gboolean
*)pparam
->data
));
855 void prefs_set_data_from_spinbtn(PrefParam
*pparam
)
857 cm_return_if_fail(*pparam
->widget
!= NULL
);
859 switch (pparam
->type
) {
861 *((gint
*)pparam
->data
) =
862 gtk_spin_button_get_value_as_int
863 (GTK_SPIN_BUTTON(*pparam
->widget
));
866 *((gushort
*)pparam
->data
) =
867 (gushort
)gtk_spin_button_get_value_as_int
868 (GTK_SPIN_BUTTON(*pparam
->widget
));
871 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
876 void prefs_set_spinbtn(PrefParam
*pparam
)
878 cm_return_if_fail(*pparam
->widget
!= NULL
);
880 switch (pparam
->type
) {
882 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam
->widget
),
883 (gfloat
)*((gint
*)pparam
->data
));
886 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam
->widget
),
887 (gfloat
)*((gushort
*)pparam
->data
));
890 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
895 static GSList
*prefs_pages
= NULL
;
897 static void prefs_gtk_window_closed_cb(PrefsWindow
*prefswindow
)
899 if (prefswindow
== NULL
)
902 if (prefswindow
->dialog_response
> PREFSWINDOW_RESPONSE_CANCEL
)
903 prefs_common_write_config();
906 void prefs_gtk_open(void)
908 prefswindow_open(_("Preferences"), prefs_pages
, NULL
,
909 &prefs_common
.prefswin_width
, &prefs_common
.prefswin_height
,
910 NULL
, prefs_gtk_window_closed_cb
, prefs_gtk_window_closed_cb
);
913 void prefs_gtk_register_page(PrefsPage
*page
)
915 prefs_pages
= g_slist_append(prefs_pages
, page
);
918 void prefs_gtk_unregister_page(PrefsPage
*page
)
920 prefs_pages
= g_slist_remove(prefs_pages
, page
);
923 static void prefs_destroy_whole_cache(gpointer to_free
)
925 GHashTable
*table
= (GHashTable
*)to_free
;
926 g_hash_table_destroy(table
);
929 static void prefs_destroy_file_cache(gpointer to_free
)
931 GHashTable
*table
= (GHashTable
*)to_free
;
932 g_hash_table_destroy(table
);
935 static int prefs_cache_sections(GHashTable
*file_cache
, const gchar
*rcfile
)
938 gchar buf
[PREFSBUFSIZE
];
939 GHashTable
*section_cache
= NULL
;
942 fp
= g_fopen(rcfile
, "rb");
944 debug_print("cache: %s: %s\n", rcfile
?rcfile
:"(null)", g_strerror(errno
));
948 #ifdef HAVE_FGETS_UNLOCKED
952 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
957 continue; /* comment */
958 if (buf
[0] == '[') { /* new section */
959 gchar
*blockname
= g_strdup(buf
+1);
961 if (strrchr(blockname
, ']'))
962 *strrchr(blockname
, ']') = '\0';
964 if ((section_cache
= g_hash_table_lookup(file_cache
, blockname
)) == NULL
) {
965 debug_print("new section '%s'\n", blockname
);
966 section_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
968 g_hash_table_insert(file_cache
,
969 blockname
, section_cache
);
971 debug_print("section '%s' already done\n", blockname
);
973 section_cache
= NULL
;
977 if (!section_cache
) {
978 debug_print("skipping stuff %s with no section\n", buf
);
983 if (!strchr(buf
, '=')) {
984 /* plugins do differently */
987 pref
= g_strdup(buf
);
989 //debug_print("new pref '%s'\n", pref);
990 g_hash_table_insert(section_cache
, pref
, GINT_TO_POINTER(1));
994 #ifdef HAVE_FGETS_UNLOCKED
1001 static int prefs_cache(const gchar
*rcfile
)
1003 GHashTable
*file_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1004 g_free
, prefs_destroy_file_cache
);
1006 debug_print("new file '%s'\n", rcfile
?rcfile
:"(null)");
1007 g_hash_table_insert(whole_cache
, g_strdup(rcfile
), file_cache
);
1009 return prefs_cache_sections(file_cache
, rcfile
);
1012 void prefs_prepare_cache(void)
1014 gchar
*clawsrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
1015 gchar
*folderitemrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, FOLDERITEM_RC
, NULL
);
1016 gchar
*accountrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, ACCOUNT_RC
, NULL
);
1018 if (whole_cache
== NULL
) {
1019 whole_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1020 g_free
, prefs_destroy_whole_cache
);
1022 debug_print("already cached\n");
1024 g_free(folderitemrc
);
1028 if (prefs_cache(clawsrc
) < 0 ||
1029 prefs_cache(folderitemrc
) < 0 ||
1030 prefs_cache(accountrc
) < 0)
1031 prefs_destroy_cache();
1034 g_free(folderitemrc
);
1038 void prefs_destroy_cache(void)
1041 debug_print("no cache\n");
1044 debug_print("destroying cache\n");
1045 g_hash_table_destroy(whole_cache
);
1050 static void prefs_parse_cache(gpointer key
, gpointer value
, gpointer user_data
)
1052 gchar
*pref
= (gchar
*)key
;
1054 PrefParam
*param
= (PrefParam
*)user_data
;
1056 prefs_config_parse_one_line(param
, pref
);
1059 static gboolean
prefs_read_config_from_cache(PrefParam
*param
, const gchar
*label
,
1060 const gchar
*rcfile
)
1062 GHashTable
*sections_table
= NULL
;
1063 GHashTable
*values_table
= NULL
;
1064 sections_table
= g_hash_table_lookup(whole_cache
, rcfile
);
1066 if (sections_table
== NULL
) {
1067 g_warning("Can't find %s in the whole cache", rcfile
?rcfile
:"(null)");
1070 values_table
= g_hash_table_lookup(sections_table
, label
);
1072 if (values_table
== NULL
) {
1073 debug_print("no '%s' section in '%s' cache\n", label
?label
:"(null)", rcfile
?rcfile
:"(null)");
1076 g_hash_table_foreach(values_table
, prefs_parse_cache
, param
);