2 * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3 * Copyright (C) 1999-2007 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/>.
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) {
428 g_free(*((gchar **)param[i].data));
429 if (param[i].defval != NULL) {
430 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
434 envstr = g_getenv(param[i].defval + 4);
435 tmp = envstr && *envstr ?
436 conv_codeset_strdup(envstr,
437 conv_get_locale_charset_str(),
441 g_warning("Failed to convert character set.");
442 tmp = g_strdup(envstr);
444 *((gchar **)param[i].data) = tmp;
445 } else if (param[i].defval[0] == '~')
446 *((gchar **)param[i].data) =
447 g_strconcat(get_home_dir(),
450 else if (param[i].defval[0] != '\0')
451 *((gchar **)param[i].data) =
452 g_strdup(param[i].defval);
454 *((gchar **)param[i].data) = NULL;
456 *((gchar **)param[i].data) = NULL;
459 g_free(*((gchar **)param[i].data));
460 if (param[i].defval != NULL) {
461 if (param[i].defval[0] != '\0')
462 *((gchar **)param[i].data) =
463 g_strdup(param[i].defval);
465 *((gchar **)param[i].data) = NULL;
467 *((gchar **)param[i].data) = NULL;
470 if (param[i].defval != NULL)
471 *((gint *)param[i].data) =
472 (gint)atoi(param[i].defval);
474 *((gint *)param[i].data) = 0;
477 if (param[i].defval != NULL) {
478 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
479 *((gboolean *)param[i].data) = TRUE;
481 *((gboolean *)param[i].data) =
482 atoi(param[i].defval) ? TRUE : FALSE;
484 *((gboolean *)param[i].data) = FALSE;
487 if (param[i].defval != NULL)
488 *((DummyEnum*)param[i].data) =
489 (DummyEnum)atoi(param[i].defval);
491 *((DummyEnum *)param[i].data) = 0;
494 if (param[i].defval != NULL)
495 *((gushort *)param[i].data) =
496 (gushort)atoi(param[i].defval);
498 *((gushort *)param[i].data) = 0;
501 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
502 *((gulong *)param[i].data) =
503 RGB_FROM_GDK_COLOR(color);
504 else if (param[i].defval)
505 /* be compatible and accept ints */
506 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
508 *((gulong *)param[i].data) = 0;
516 void prefs_free(PrefParam *param)
520 g_return_if_fail(param != NULL);
522 for (i = 0; param[i].name != NULL; i++) {
523 if (!param[i].data) continue;
525 switch (param[i].type) {
528 g_free(*((gchar **)param[i].data));
536 void prefs_dialog_create(PrefsDialog *dialog)
542 GtkWidget *confirm_area;
544 GtkWidget *cancel_btn;
545 GtkWidget *apply_btn;
547 g_return_if_fail(dialog != NULL);
549 window = gtkut_window_new(GTK_WINDOW_TOPLEVEL, "prefs_gtk");
550 gtk_container_set_border_width (GTK_CONTAINER (window), 8);
551 gtk_window_set_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
552 gtk_window_set_modal (GTK_WINDOW (window), TRUE);
553 gtk_window_set_resizable(GTK_WINDOW(window), TRUE);
555 vbox = gtk_vbox_new (FALSE, 6);
556 gtk_widget_show(vbox);
557 gtk_container_add (GTK_CONTAINER (window), vbox);
559 notebook = gtk_notebook_new ();
560 gtk_widget_show(notebook);
561 gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
562 gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
563 /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
564 gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
566 gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
568 gtkut_stock_button_set_create(&confirm_area,
569 &ok_btn, GTK_STOCK_OK,
570 &cancel_btn, GTK_STOCK_CANCEL,
571 &apply_btn, GTK_STOCK_APPLY);
572 gtk_widget_show(confirm_area);
573 gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
574 gtk_widget_grab_default(ok_btn);
576 dialog->window = window;
577 dialog->notebook = notebook;
578 dialog->ok_btn = ok_btn;
579 dialog->cancel_btn = cancel_btn;
580 dialog->apply_btn = apply_btn;
583 void prefs_dialog_destroy(PrefsDialog *dialog)
585 gtk_widget_destroy(dialog->window);
586 dialog->window = NULL;
587 dialog->notebook = NULL;
588 dialog->ok_btn = NULL;
589 dialog->cancel_btn = NULL;
590 dialog->apply_btn = NULL;
593 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
597 is_active = gtk_toggle_button_get_active(toggle_btn);
598 gtk_widget_set_sensitive(widget, is_active);
601 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
605 is_active = gtk_toggle_button_get_active(toggle_btn);
606 gtk_widget_set_sensitive(widget, !is_active);
609 void prefs_set_dialog(PrefParam *param)
613 for (i = 0; param[i].name != NULL; i++) {
614 if (param[i].widget_set_func)
615 param[i].widget_set_func(¶m[i]);
619 void prefs_set_data_from_dialog(PrefParam *param)
623 for (i = 0; param[i].name != NULL; i++) {
624 if (param[i].data_set_func)
625 param[i].data_set_func(¶m[i]);
629 void prefs_set_dialog_to_default(PrefParam *param)
633 gchar *str_data = NULL;
639 for (i = 0; param[i].name != NULL; i++) {
640 if (!param[i].widget_set_func) continue;
644 switch (tmpparam.type) {
646 if (tmpparam.defval) {
647 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
648 str_data = g_strdup(g_getenv(param[i].defval + 4));
649 tmpparam.data = &str_data;
651 } else if (tmpparam.defval[0] == '~') {
653 g_strconcat(get_home_dir(),
656 tmpparam.data = &str_data;
660 tmpparam.data = &tmpparam.defval;
663 tmpparam.data = &tmpparam.defval;
667 int_data = atoi(tmpparam.defval);
670 tmpparam.data = &int_data;
674 ushort_data = atoi(tmpparam.defval);
677 tmpparam.data = &ushort_data;
680 if (tmpparam.defval) {
681 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
684 bool_data = atoi(tmpparam.defval)
688 tmpparam.data = &bool_data;
692 enum_data = (DummyEnum)atoi(tmpparam.defval);
695 tmpparam.data = &enum_data;
701 tmpparam.widget_set_func(&tmpparam);
707 void prefs_set_data_from_entry(PrefParam *pparam)
710 const gchar *entry_str;
712 g_return_if_fail(*pparam->widget != NULL);
714 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
716 switch (pparam->type) {
719 str = (gchar **)pparam->data;
721 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
724 *((gushort *)pparam->data) = atoi(entry_str);
727 *((gint *)pparam->data) = atoi(entry_str);
730 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
735 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
739 g_return_if_fail(*pparam->widget != NULL);
741 switch (pparam->type) {
743 str = (gchar **)pparam->data;
745 *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
748 g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
753 void prefs_set_entry(PrefParam *pparam)
757 g_return_if_fail(*pparam->widget != NULL);
759 switch (pparam->type) {
762 str = (gchar **)pparam->data;
763 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
767 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
768 itos(*((gint *)pparam->data)));
771 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
772 itos(*((gushort *)pparam->data)));
775 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
780 void prefs_set_entry_from_escaped(PrefParam *pparam)
784 g_return_if_fail(*pparam->widget != NULL);
786 switch (pparam->type) {
788 str = (gchar **)pparam->data;
789 pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
793 g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
798 void prefs_set_data_from_text(PrefParam *pparam)
801 gchar *text = NULL, *tp = NULL;
804 g_return_if_fail(*pparam->widget != NULL);
806 switch (pparam->type) {
809 str = (gchar **)pparam->data;
811 if (GTK_IS_EDITABLE(*pparam->widget)) { /* need? */
812 tp = text = gtk_editable_get_chars
813 (GTK_EDITABLE(*pparam->widget), 0, -1);
814 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
815 GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
816 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
817 GtkTextIter start, end;
818 gtk_text_buffer_get_start_iter(buffer, &start);
819 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
820 tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
823 g_return_if_fail (tp && text);
825 if (text[0] == '\0') {
831 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
832 { *str = NULL; break; });
842 *str = g_strdup(tmp);
846 g_warning("Invalid PrefType for GtkText widget: %d\n",
851 void prefs_set_escaped_data_from_text(PrefParam *pparam)
855 g_return_if_fail(*pparam->widget != NULL);
857 switch (pparam->type) {
859 str = (gchar **)pparam->data;
861 *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
864 g_warning("Invalid escaped PrefType for GtkText widget: %d\n",
869 void prefs_set_text(PrefParam *pparam)
871 gchar *buf, *sp, *bufp;
874 GtkTextBuffer *buffer;
877 g_return_if_fail(*pparam->widget != NULL);
879 switch (pparam->type) {
882 str = (gchar **)pparam->data;
884 bufp = buf = alloca(strlen(*str) + 1);
889 if (*sp == '\\' && *(sp + 1) == 'n') {
900 text = GTK_TEXT_VIEW(*pparam->widget);
901 buffer = gtk_text_view_get_buffer(text);
902 gtk_text_buffer_set_text(buffer, "", -1);
903 gtk_text_buffer_get_start_iter(buffer, &iter);
904 gtk_text_buffer_insert(buffer, &iter, buf, -1);
907 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
912 void prefs_set_text_from_escaped(PrefParam *pparam)
916 g_return_if_fail(*pparam->widget != NULL);
918 switch (pparam->type) {
920 str = (gchar **)pparam->data;
921 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
925 g_warning("Invalid escaped PrefType for GtkTextView widget: %d\n",
930 void prefs_set_data_from_toggle(PrefParam *pparam)
932 g_return_if_fail(pparam->type == P_BOOL);
933 g_return_if_fail(*pparam->widget != NULL);
935 *((gboolean *)pparam->data) =
936 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
939 void prefs_set_toggle(PrefParam *pparam)
941 g_return_if_fail(pparam->type == P_BOOL);
942 g_return_if_fail(*pparam->widget != NULL);
944 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
945 *((gboolean *)pparam->data));
948 void prefs_set_data_from_spinbtn(PrefParam *pparam)
950 g_return_if_fail(*pparam->widget != NULL);
952 switch (pparam->type) {
954 *((gint *)pparam->data) =
955 gtk_spin_button_get_value_as_int
956 (GTK_SPIN_BUTTON(*pparam->widget));
959 *((gushort *)pparam->data) =
960 (gushort)gtk_spin_button_get_value_as_int
961 (GTK_SPIN_BUTTON(*pparam->widget));
964 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
969 void prefs_set_spinbtn(PrefParam *pparam)
971 g_return_if_fail(*pparam->widget != NULL);
973 switch (pparam->type) {
975 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
976 (gfloat)*((gint *)pparam->data));
979 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
980 (gfloat)*((gushort *)pparam->data));
983 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
988 static GSList *prefs_pages = NULL;
990 void prefs_gtk_open(void)
992 prefswindow_open(_("Preferences"), prefs_pages, NULL,
993 &prefs_common.prefswin_width, &prefs_common.prefswin_height);
996 void prefs_gtk_register_page(PrefsPage *page)
998 prefs_pages = g_slist_append(prefs_pages, page);
1001 void prefs_gtk_unregister_page(PrefsPage *page)
1003 prefs_pages = g_slist_remove(prefs_pages, page);
1006 static void prefs_destroy_whole_cache(gpointer to_free)
1008 GHashTable *table = (GHashTable *)to_free;
1009 g_hash_table_destroy(table);
1012 static void prefs_destroy_file_cache(gpointer to_free)
1014 GHashTable *table = (GHashTable *)to_free;
1015 g_hash_table_destroy(table);
1018 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
1020 FILE *fp = g_fopen(rcfile, "rb");
1021 gchar buf[PREFSBUFSIZE];
1022 GHashTable *section_cache = NULL;
1025 debug_print("cache: %s: %s", rcfile?rcfile:"(null)", strerror(errno));
1029 #ifdef HAVE_FGETS_UNLOCKED
1033 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
1038 continue; /* comment */
1039 if (buf[0] == '[') { /* new section */
1040 gchar *blockname = g_strdup(buf+1);
1042 if (strrchr(blockname, ']'))
1043 *strrchr(blockname, ']') = '\0';
1045 if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
1046 debug_print("new section '%s'\n", blockname);
1047 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1049 g_hash_table_insert(file_cache,
1050 blockname, section_cache);
1052 debug_print("section '%s' already done\n", blockname);
1054 section_cache = NULL;
1058 if (!section_cache) {
1059 debug_print("skipping stuff %s with no section\n", buf);
1064 if (!strchr(buf, '=')) {
1065 /* plugins do differently */
1068 pref = g_strdup(buf);
1070 //debug_print("new pref '%s'\n", pref);
1071 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
1075 #ifdef HAVE_FGETS_UNLOCKED
1082 static int prefs_cache(const gchar *rcfile)
1084 GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1085 g_free, prefs_destroy_file_cache);
1087 debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
1088 g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1090 return prefs_cache_sections(file_cache, rcfile);
1093 void prefs_prepare_cache(void)
1095 gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1096 gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1097 gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1099 if (whole_cache == NULL) {
1100 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1101 g_free, prefs_destroy_whole_cache);
1103 debug_print("already cached\n");
1105 g_free(folderitemrc);
1109 if (prefs_cache(clawsrc) < 0 ||
1110 prefs_cache(folderitemrc) < 0 ||
1111 prefs_cache(accountrc) < 0)
1112 prefs_destroy_cache();
1115 g_free(folderitemrc);
1119 void prefs_destroy_cache(void)
1122 debug_print("no cache\n");
1125 debug_print("destroying cache\n");
1126 g_hash_table_destroy(whole_cache);
1131 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1133 gchar *pref = (gchar *)key;
1135 PrefParam *param = (PrefParam *)user_data;
1137 prefs_config_parse_one_line(param, pref);
1140 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1141 const gchar *rcfile)
1143 GHashTable *sections_table = NULL;
1144 GHashTable *values_table = NULL;
1145 sections_table = g_hash_table_lookup(whole_cache, rcfile);
1147 if (sections_table == NULL) {
1148 g_warning("Can't find %s in the whole cache\n", rcfile?rcfile:"(null)");
1151 values_table = g_hash_table_lookup(sections_table, label);
1153 if (values_table == NULL) {
1154 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1157 g_hash_table_foreach(values_table, prefs_parse_cache, param);