2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2012 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"
43 #include "passcrypt.h"
47 #define CL(x) (((gulong) (x) >> (gulong) 8) & 0xFFUL)
48 #define RGB_FROM_GDK_COLOR(c) \
49 ((CL(c.red) << (gulong) 16) | \
50 (CL(c.green) << (gulong) 8) | \
53 #ifdef HAVE_FGETS_UNLOCKED
54 #define SC_FGETS fgets_unlocked
56 #define SC_FGETS fgets
64 static GHashTable
*whole_cache
= NULL
;
66 static gboolean
prefs_read_config_from_cache(PrefParam
*param
, const gchar
*label
,
69 static void prefs_config_parse_one_line(PrefParam
*param
,
72 void prefs_read_config(PrefParam
*param
, const gchar
*label
,
73 const gchar
*rcfile
, const gchar
*encoding
)
76 gchar buf
[PREFSBUFSIZE
];
79 cm_return_if_fail(param
!= NULL
);
80 cm_return_if_fail(label
!= NULL
);
81 cm_return_if_fail(rcfile
!= NULL
);
84 g_warning("Encoding is ignored\n");
86 debug_print("Reading configuration...\n");
88 prefs_set_default(param
);
90 if (whole_cache
!= NULL
) {
91 if (prefs_read_config_from_cache(param
, label
, rcfile
) == TRUE
)
95 if ((fp
= g_fopen(rcfile
, "rb")) == NULL
) {
96 if (ENOENT
!= errno
) FILE_OP_ERROR(rcfile
, "fopen");
100 block_label
= g_strdup_printf("[%s]", label
);
102 #ifdef HAVE_FGETS_UNLOCKED
106 /* search aiming block */
107 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
113 conv_str
= conv_codeset_strdup
114 (buf
, encoding
, CS_INTERNAL
);
116 conv_str
= g_strdup(buf
);
118 (conv_str
, block_label
, strlen(block_label
));
121 val
= strncmp(buf
, block_label
, strlen(block_label
));
123 debug_print("Found %s\n", block_label
);
129 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
131 /* reached next block */
132 if (buf
[0] == '[') break;
133 if (buf
[0] == '#') continue;
138 conv_str
= conv_codeset_strdup
139 (buf
, encoding
, CS_INTERNAL
);
141 conv_str
= g_strdup(buf
);
142 prefs_config_parse_one_line(param
, conv_str
);
145 prefs_config_parse_one_line(param
, buf
);
148 debug_print("Finished reading configuration.\n");
149 #ifdef HAVE_FGETS_UNLOCKED
155 static void prefs_config_parse_one_line(PrefParam
*param
, const gchar
*buf
)
162 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
163 name_len
= strlen(param
[i
].name
);
164 if (g_ascii_strncasecmp(buf
, param
[i
].name
, name_len
))
166 if (buf
[name_len
] != '=')
168 value
= buf
+ name_len
+ 1;
169 /* debug_print("%s = %s\n", param[i].name, value); */
171 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);
221 g_free(*((gchar
**)param
[i
].data
));
222 if (value
[0] == '!') {
226 len
= base64_decode(tmp
, &value
[1], strlen(value
) - 1);
227 passcrypt_decrypt(tmp
, len
);
229 *((gchar
**)param
[i
].data
) =
230 *tmp
? g_strdup(tmp
) : NULL
;
232 *((gchar
**)param
[i
].data
) =
233 *value
? g_strdup(value
) : NULL
;
245 g_warning("Failed to write configuration to file\n"); \
246 if (orig_fp) fclose(orig_fp); \
247 prefs_file_close_revert(pfile); \
249 g_free(block_label); \
253 void prefs_write_config(PrefParam *param, const gchar *label,
259 gchar buf
[PREFSBUFSIZE
];
260 gchar
*block_label
= NULL
;
261 gboolean block_matched
= FALSE
;
263 cm_return_if_fail(param
!= NULL
);
264 cm_return_if_fail(label
!= NULL
);
265 cm_return_if_fail(rcfile
!= NULL
);
267 rcpath
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, rcfile
, NULL
);
268 if ((orig_fp
= g_fopen(rcpath
, "rb")) == NULL
) {
269 if (ENOENT
!= errno
) FILE_OP_ERROR(rcpath
, "fopen");
272 if ((pfile
= prefs_write_open(rcpath
)) == NULL
) {
273 g_warning("Failed to write configuration to file\n");
274 if (orig_fp
) fclose(orig_fp
);
279 block_label
= g_strdup_printf("[%s]", label
);
281 /* search aiming block */
283 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
286 val
= strncmp(buf
, block_label
, strlen(block_label
));
288 debug_print("Found %s\n", block_label
);
289 block_matched
= TRUE
;
292 TRY(fputs(buf
, pfile
->fp
) != EOF
);
296 TRY(fprintf(pfile
->fp
, "%s\n", block_label
) > 0);
298 /* write all param data to file */
299 TRY(prefs_write_param(param
, pfile
->fp
) == 0);
302 gboolean in_dup_block
= FALSE
;
303 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
306 TRY(fputc('\n', pfile
->fp
) != EOF
&&
307 fputs(buf
, pfile
->fp
) != EOF
);
311 while (fgets(buf
, sizeof(buf
), orig_fp
) != NULL
) {
313 if (!strncmp(buf
, block_label
,
314 strlen(block_label
)))
317 in_dup_block
= FALSE
;
320 TRY(fputs(buf
, pfile
->fp
) != EOF
);
327 if (orig_fp
) fclose(orig_fp
);
328 if (prefs_file_close(pfile
) < 0)
329 g_warning("Failed to write configuration to file\n");
332 debug_print("Configuration is saved.\n");
335 gint
prefs_write_param(PrefParam
*param
, FILE *fp
)
338 gchar buf
[PREFSBUFSIZE
];
340 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
341 switch (param
[i
].type
) {
346 if (*((gchar
**)param
[i
].data
)) {
347 if (g_utf8_validate(*((gchar
**)param
[i
].data
), -1, NULL
))
348 tmp
= g_strdup(*((gchar
**)param
[i
].data
));
350 tmp
= conv_codeset_strdup(*((gchar
**)param
[i
].data
),
351 conv_get_locale_charset_str_no_utf8(),
354 tmp
= g_strdup(*((gchar
**)param
[i
].data
));
358 g_snprintf(buf
, sizeof(buf
), "%s=%s\n", param
[i
].name
,
365 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
366 *((gint
*)param
[i
].data
));
369 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
370 *((gboolean
*)param
[i
].data
));
373 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
374 *((DummyEnum
*)param
[i
].data
));
377 g_snprintf(buf
, sizeof(buf
), "%s=%d\n", param
[i
].name
,
378 *((gushort
*)param
[i
].data
));
381 g_snprintf(buf
, sizeof buf
, "%s=#%6.6lx\n", param
[i
].name
,
382 *((gulong
*) param
[i
].data
));
386 gchar
*tmp
= NULL
, tmp2
[1024] = {0};
388 tmp
= *((gchar
**)param
[i
].data
);
394 passcrypt_encrypt(tmp
, len
);
395 base64_encode(tmp2
, tmp
, len
);
399 g_snprintf(buf
, sizeof(buf
), "%s=!%s\n", param
[i
].name
,
405 /* unrecognized, fail */
406 debug_print("Unrecognized parameter type\n");
410 if (buf
[0] != '\0') {
411 if (fputs(buf
, fp
) == EOF
) {
421 void prefs_set_default(PrefParam
*param
)
426 cm_return_if_fail(param
!= NULL
);
428 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
429 if (!param
[i
].data
) continue;
431 switch (param
[i
].type
) {
433 g_free(*((gchar
**)param
[i
].data
));
434 if (param
[i
].defval
!= NULL
) {
435 if (!strncasecmp(param
[i
].defval
, "ENV_", 4)) {
439 envstr
= g_getenv(param
[i
].defval
+ 4);
440 tmp
= envstr
&& *envstr
?
441 conv_codeset_strdup(envstr
,
442 conv_get_locale_charset_str(),
446 g_warning("Failed to convert character set.");
447 tmp
= g_strdup(envstr
);
449 *((gchar
**)param
[i
].data
) = tmp
;
450 } else if (param
[i
].defval
[0] == '~')
451 *((gchar
**)param
[i
].data
) =
452 g_strconcat(get_home_dir(),
456 *((gchar
**)param
[i
].data
) =
457 g_strdup(param
[i
].defval
);
459 *((gchar
**)param
[i
].data
) = NULL
;
462 g_free(*((gchar
**)param
[i
].data
));
463 if (param
[i
].defval
!= NULL
) {
464 if (param
[i
].defval
[0] != '\0')
465 *((gchar
**)param
[i
].data
) =
466 g_strdup(param
[i
].defval
);
468 *((gchar
**)param
[i
].data
) = NULL
;
470 *((gchar
**)param
[i
].data
) = NULL
;
473 if (param
[i
].defval
!= NULL
)
474 *((gint
*)param
[i
].data
) =
475 (gint
)atoi(param
[i
].defval
);
477 *((gint
*)param
[i
].data
) = 0;
480 if (param
[i
].defval
!= NULL
) {
481 if (!g_ascii_strcasecmp(param
[i
].defval
, "TRUE"))
482 *((gboolean
*)param
[i
].data
) = TRUE
;
484 *((gboolean
*)param
[i
].data
) =
485 atoi(param
[i
].defval
) ? TRUE
: FALSE
;
487 *((gboolean
*)param
[i
].data
) = FALSE
;
490 if (param
[i
].defval
!= NULL
)
491 *((DummyEnum
*)param
[i
].data
) =
492 (DummyEnum
)atoi(param
[i
].defval
);
494 *((DummyEnum
*)param
[i
].data
) = 0;
497 if (param
[i
].defval
!= NULL
)
498 *((gushort
*)param
[i
].data
) =
499 (gushort
)atoi(param
[i
].defval
);
501 *((gushort
*)param
[i
].data
) = 0;
504 if (param
[i
].defval
!= NULL
&& gdk_color_parse(param
[i
].defval
, &color
))
505 *((gulong
*)param
[i
].data
) =
506 RGB_FROM_GDK_COLOR(color
);
507 else if (param
[i
].defval
)
508 /* be compatible and accept ints */
509 *((gulong
*)param
[i
].data
) = strtoul(param
[i
].defval
, 0, 10);
511 *((gulong
*)param
[i
].data
) = 0;
519 void prefs_free(PrefParam
*param
)
523 cm_return_if_fail(param
!= NULL
);
525 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
526 if (!param
[i
].data
) continue;
528 switch (param
[i
].type
) {
531 g_free(*((gchar
**)param
[i
].data
));
539 void prefs_button_toggled(GtkToggleButton
*toggle_btn
, GtkWidget
*widget
)
543 is_active
= gtk_toggle_button_get_active(toggle_btn
);
544 gtk_widget_set_sensitive(widget
, is_active
);
547 void prefs_button_toggled_reverse(GtkToggleButton
*toggle_btn
, GtkWidget
*widget
)
551 is_active
= gtk_toggle_button_get_active(toggle_btn
);
552 gtk_widget_set_sensitive(widget
, !is_active
);
555 void prefs_set_dialog(PrefParam
*param
)
559 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
560 if (param
[i
].widget_set_func
)
561 param
[i
].widget_set_func(¶m
[i
]);
565 void prefs_set_data_from_dialog(PrefParam
*param
)
569 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
570 if (param
[i
].data_set_func
)
571 param
[i
].data_set_func(¶m
[i
]);
575 void prefs_set_dialog_to_default(PrefParam
*param
)
579 gchar
*str_data
= NULL
;
585 for (i
= 0; param
[i
].name
!= NULL
; i
++) {
586 if (!param
[i
].widget_set_func
) continue;
590 switch (tmpparam
.type
) {
592 if (tmpparam
.defval
) {
593 if (!g_ascii_strncasecmp(tmpparam
.defval
, "ENV_", 4)) {
594 str_data
= g_strdup(g_getenv(param
[i
].defval
+ 4));
595 tmpparam
.data
= &str_data
;
597 } else if (tmpparam
.defval
[0] == '~') {
599 g_strconcat(get_home_dir(),
602 tmpparam
.data
= &str_data
;
606 tmpparam
.data
= &tmpparam
.defval
;
609 tmpparam
.data
= &tmpparam
.defval
;
613 int_data
= atoi(tmpparam
.defval
);
616 tmpparam
.data
= &int_data
;
620 ushort_data
= atoi(tmpparam
.defval
);
623 tmpparam
.data
= &ushort_data
;
626 if (tmpparam
.defval
) {
627 if (!g_ascii_strcasecmp(tmpparam
.defval
, "TRUE"))
630 bool_data
= atoi(tmpparam
.defval
)
634 tmpparam
.data
= &bool_data
;
638 enum_data
= (DummyEnum
)atoi(tmpparam
.defval
);
641 tmpparam
.data
= &enum_data
;
647 tmpparam
.widget_set_func(&tmpparam
);
653 void prefs_set_data_from_entry(PrefParam
*pparam
)
656 const gchar
*entry_str
;
658 cm_return_if_fail(*pparam
->widget
!= NULL
);
660 entry_str
= gtk_entry_get_text(GTK_ENTRY(*pparam
->widget
));
662 switch (pparam
->type
) {
665 str
= (gchar
**)pparam
->data
;
667 *str
= entry_str
[0] ? g_strdup(entry_str
) : NULL
;
670 *((gushort
*)pparam
->data
) = atoi(entry_str
);
673 *((gint
*)pparam
->data
) = atoi(entry_str
);
676 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
681 void prefs_set_escaped_data_from_entry(PrefParam
*pparam
)
685 cm_return_if_fail(*pparam
->widget
!= NULL
);
687 switch (pparam
->type
) {
689 str
= (gchar
**)pparam
->data
;
691 *str
= pref_get_pref_from_entry(GTK_ENTRY(*pparam
->widget
));
694 g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
699 void prefs_set_entry(PrefParam
*pparam
)
703 cm_return_if_fail(*pparam
->widget
!= NULL
);
705 switch (pparam
->type
) {
708 str
= (gchar
**)pparam
->data
;
709 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
713 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
714 itos(*((gint
*)pparam
->data
)));
717 gtk_entry_set_text(GTK_ENTRY(*pparam
->widget
),
718 itos(*((gushort
*)pparam
->data
)));
721 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
726 void prefs_set_entry_from_escaped(PrefParam
*pparam
)
730 cm_return_if_fail(*pparam
->widget
!= NULL
);
732 switch (pparam
->type
) {
734 str
= (gchar
**)pparam
->data
;
735 pref_set_entry_from_pref(GTK_ENTRY(*pparam
->widget
),
739 g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
744 void prefs_set_data_from_text(PrefParam
*pparam
)
747 gchar
*text
= NULL
, *tp
= NULL
;
750 cm_return_if_fail(*pparam
->widget
!= NULL
);
752 switch (pparam
->type
) {
755 str
= (gchar
**)pparam
->data
;
757 if (GTK_IS_EDITABLE(*pparam
->widget
)) { /* need? */
758 tp
= text
= gtk_editable_get_chars
759 (GTK_EDITABLE(*pparam
->widget
), 0, -1);
760 } else if (GTK_IS_TEXT_VIEW(*pparam
->widget
)) {
761 GtkTextView
*textview
= GTK_TEXT_VIEW(*pparam
->widget
);
762 GtkTextBuffer
*buffer
= gtk_text_view_get_buffer(textview
);
763 GtkTextIter start
, end
;
764 gtk_text_buffer_get_start_iter(buffer
, &start
);
765 gtk_text_buffer_get_iter_at_offset(buffer
, &end
, -1);
766 tp
= text
= gtk_text_buffer_get_text(buffer
, &start
, &end
, FALSE
);
769 cm_return_if_fail (tp
&& text
);
771 if (text
[0] == '\0') {
777 Xalloca(tmpp
= tmp
, strlen(text
) * 2 + 1,
778 { *str
= NULL
; break; });
788 *str
= g_strdup(tmp
);
792 g_warning("Invalid PrefType for GtkText widget: %d\n",
797 void prefs_set_escaped_data_from_text(PrefParam
*pparam
)
801 cm_return_if_fail(*pparam
->widget
!= NULL
);
803 switch (pparam
->type
) {
805 str
= (gchar
**)pparam
->data
;
807 *str
= pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam
->widget
));
810 g_warning("Invalid escaped PrefType for GtkText widget: %d\n",
815 void prefs_set_text(PrefParam
*pparam
)
817 gchar
*buf
, *sp
, *bufp
;
820 GtkTextBuffer
*buffer
;
823 cm_return_if_fail(*pparam
->widget
!= NULL
);
825 switch (pparam
->type
) {
828 str
= (gchar
**)pparam
->data
;
830 bufp
= buf
= alloca(strlen(*str
) + 1);
835 if (*sp
== '\\' && *(sp
+ 1) == 'n') {
846 text
= GTK_TEXT_VIEW(*pparam
->widget
);
847 buffer
= gtk_text_view_get_buffer(text
);
848 gtk_text_buffer_set_text(buffer
, "", -1);
849 gtk_text_buffer_get_start_iter(buffer
, &iter
);
850 gtk_text_buffer_insert(buffer
, &iter
, buf
, -1);
853 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
858 void prefs_set_text_from_escaped(PrefParam
*pparam
)
862 cm_return_if_fail(*pparam
->widget
!= NULL
);
864 switch (pparam
->type
) {
866 str
= (gchar
**)pparam
->data
;
867 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam
->widget
),
871 g_warning("Invalid escaped PrefType for GtkTextView widget: %d\n",
876 void prefs_set_data_from_toggle(PrefParam
*pparam
)
878 cm_return_if_fail(pparam
->type
== P_BOOL
);
879 cm_return_if_fail(*pparam
->widget
!= NULL
);
881 *((gboolean
*)pparam
->data
) =
882 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam
->widget
));
885 void prefs_set_toggle(PrefParam
*pparam
)
887 cm_return_if_fail(pparam
->type
== P_BOOL
);
888 cm_return_if_fail(*pparam
->widget
!= NULL
);
890 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam
->widget
),
891 *((gboolean
*)pparam
->data
));
894 void prefs_set_data_from_spinbtn(PrefParam
*pparam
)
896 cm_return_if_fail(*pparam
->widget
!= NULL
);
898 switch (pparam
->type
) {
900 *((gint
*)pparam
->data
) =
901 gtk_spin_button_get_value_as_int
902 (GTK_SPIN_BUTTON(*pparam
->widget
));
905 *((gushort
*)pparam
->data
) =
906 (gushort
)gtk_spin_button_get_value_as_int
907 (GTK_SPIN_BUTTON(*pparam
->widget
));
910 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
915 void prefs_set_spinbtn(PrefParam
*pparam
)
917 cm_return_if_fail(*pparam
->widget
!= NULL
);
919 switch (pparam
->type
) {
921 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam
->widget
),
922 (gfloat
)*((gint
*)pparam
->data
));
925 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam
->widget
),
926 (gfloat
)*((gushort
*)pparam
->data
));
929 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
934 static GSList
*prefs_pages
= NULL
;
936 void prefs_gtk_open(void)
938 prefswindow_open(_("Preferences"), prefs_pages
, NULL
,
939 &prefs_common
.prefswin_width
, &prefs_common
.prefswin_height
,
943 void prefs_gtk_register_page(PrefsPage
*page
)
945 prefs_pages
= g_slist_append(prefs_pages
, page
);
948 void prefs_gtk_unregister_page(PrefsPage
*page
)
950 prefs_pages
= g_slist_remove(prefs_pages
, page
);
953 static void prefs_destroy_whole_cache(gpointer to_free
)
955 GHashTable
*table
= (GHashTable
*)to_free
;
956 g_hash_table_destroy(table
);
959 static void prefs_destroy_file_cache(gpointer to_free
)
961 GHashTable
*table
= (GHashTable
*)to_free
;
962 g_hash_table_destroy(table
);
965 static int prefs_cache_sections(GHashTable
*file_cache
, const gchar
*rcfile
)
968 gchar buf
[PREFSBUFSIZE
];
969 GHashTable
*section_cache
= NULL
;
972 fp
= g_fopen(rcfile
, "rb");
974 debug_print("cache: %s: %s\n", rcfile
?rcfile
:"(null)", strerror(errno
));
978 #ifdef HAVE_FGETS_UNLOCKED
982 while (SC_FGETS(buf
, sizeof(buf
), fp
) != NULL
) {
987 continue; /* comment */
988 if (buf
[0] == '[') { /* new section */
989 gchar
*blockname
= g_strdup(buf
+1);
991 if (strrchr(blockname
, ']'))
992 *strrchr(blockname
, ']') = '\0';
994 if ((section_cache
= g_hash_table_lookup(file_cache
, blockname
)) == NULL
) {
995 debug_print("new section '%s'\n", blockname
);
996 section_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
998 g_hash_table_insert(file_cache
,
999 blockname
, section_cache
);
1001 debug_print("section '%s' already done\n", blockname
);
1003 section_cache
= NULL
;
1007 if (!section_cache
) {
1008 debug_print("skipping stuff %s with no section\n", buf
);
1013 if (!strchr(buf
, '=')) {
1014 /* plugins do differently */
1017 pref
= g_strdup(buf
);
1019 //debug_print("new pref '%s'\n", pref);
1020 g_hash_table_insert(section_cache
, pref
, GINT_TO_POINTER(1));
1024 #ifdef HAVE_FGETS_UNLOCKED
1031 static int prefs_cache(const gchar
*rcfile
)
1033 GHashTable
*file_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1034 g_free
, prefs_destroy_file_cache
);
1036 debug_print("new file '%s'\n", rcfile
?rcfile
:"(null)");
1037 g_hash_table_insert(whole_cache
, g_strdup(rcfile
), file_cache
);
1039 return prefs_cache_sections(file_cache
, rcfile
);
1042 void prefs_prepare_cache(void)
1044 gchar
*clawsrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, COMMON_RC
, NULL
);
1045 gchar
*folderitemrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, FOLDERITEM_RC
, NULL
);
1046 gchar
*accountrc
= g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S
, ACCOUNT_RC
, NULL
);
1048 if (whole_cache
== NULL
) {
1049 whole_cache
= g_hash_table_new_full(g_str_hash
, g_str_equal
,
1050 g_free
, prefs_destroy_whole_cache
);
1052 debug_print("already cached\n");
1054 g_free(folderitemrc
);
1058 if (prefs_cache(clawsrc
) < 0 ||
1059 prefs_cache(folderitemrc
) < 0 ||
1060 prefs_cache(accountrc
) < 0)
1061 prefs_destroy_cache();
1064 g_free(folderitemrc
);
1068 void prefs_destroy_cache(void)
1071 debug_print("no cache\n");
1074 debug_print("destroying cache\n");
1075 g_hash_table_destroy(whole_cache
);
1080 static void prefs_parse_cache(gpointer key
, gpointer value
, gpointer user_data
)
1082 gchar
*pref
= (gchar
*)key
;
1084 PrefParam
*param
= (PrefParam
*)user_data
;
1086 prefs_config_parse_one_line(param
, pref
);
1089 static gboolean
prefs_read_config_from_cache(PrefParam
*param
, const gchar
*label
,
1090 const gchar
*rcfile
)
1092 GHashTable
*sections_table
= NULL
;
1093 GHashTable
*values_table
= NULL
;
1094 sections_table
= g_hash_table_lookup(whole_cache
, rcfile
);
1096 if (sections_table
== NULL
) {
1097 g_warning("Can't find %s in the whole cache\n", rcfile
?rcfile
:"(null)");
1100 values_table
= g_hash_table_lookup(sections_table
, label
);
1102 if (values_table
== NULL
) {
1103 debug_print("no '%s' section in '%s' cache\n", label
?label
:"(null)", rcfile
?rcfile
:"(null)");
1106 g_hash_table_foreach(values_table
, prefs_parse_cache
, param
);