update team data
[claws.git] / src / prefs_gtk.c
blobb4fc57fb956f6d180352658a1077997975400eac
1 /*
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/>.
20 #ifdef HAVE_CONFIG_H
21 # include "config.h"
22 #include "claws-features.h"
23 #endif
25 #include <stdio.h>
27 #include <glib.h>
28 #include <glib/gi18n.h>
29 #include <gtk/gtk.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
33 #include <errno.h>
35 #include "defs.h"
36 #include "main.h"
37 #include "prefs.h"
38 #include "prefs_gtk.h"
39 #include "prefs_common.h"
40 #include "utils.h"
41 #include "gtkutils.h"
42 #include "password.h"
43 #include "codeconv.h"
44 #include "file-utils.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) | \
50 (CL(c.blue)))
52 /* Kept for backwards compatibility */
53 #define INTCOLOR_TO_GDKRGBA(intcolor, rgba) \
54 rgba.red = (gdouble)(((intcolor >> 16UL) & 0xFFUL) << 8UL) / 65535; \
55 rgba.green = (gdouble)(((intcolor >> 8UL) & 0xFFUL) << 8UL) / 65535; \
56 rgba.blue = (gdouble)(((intcolor ) & 0xFFUL) << 8UL) / 65535; \
57 rgba.alpha = 1;
60 typedef enum
62 DUMMY_PARAM
63 } DummyEnum;
65 static GHashTable *whole_cache = NULL;
67 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
68 const gchar *rcfile);
70 static void prefs_config_parse_one_line(PrefParam *param,
71 const gchar *buf);
73 void prefs_read_config(PrefParam *param, const gchar *label,
74 const gchar *rcfile, const gchar *encoding)
76 FILE *fp;
77 gchar buf[PREFSBUFSIZE];
78 gchar *block_label;
80 cm_return_if_fail(param != NULL);
81 cm_return_if_fail(label != NULL);
82 cm_return_if_fail(rcfile != NULL);
84 if (encoding != NULL)
85 g_warning("encoding is ignored");
87 debug_print("Reading configuration...\n");
89 prefs_set_default(param);
91 if (whole_cache != NULL) {
92 if (prefs_read_config_from_cache(param, label, rcfile) == TRUE)
93 return;
96 if ((fp = claws_fopen(rcfile, "rb")) == NULL) {
97 if (ENOENT != errno) FILE_OP_ERROR(rcfile, "claws_fopen");
98 return;
101 block_label = g_strdup_printf("[%s]", label);
103 /* search aiming block */
104 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
105 gint val;
107 if (encoding) {
108 gchar *conv_str;
110 conv_str = conv_codeset_strdup
111 (buf, encoding, CS_INTERNAL);
112 if (!conv_str)
113 conv_str = g_strdup(buf);
114 val = strncmp
115 (conv_str, block_label, strlen(block_label));
116 g_free(conv_str);
117 } else
118 val = strncmp(buf, block_label, strlen(block_label));
119 if (val == 0) {
120 debug_print("Found %s\n", block_label);
121 break;
124 g_free(block_label);
126 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
127 strretchomp(buf);
128 /* reached next block */
129 if (buf[0] == '[') break;
130 if (buf[0] == '#') continue;
132 if (encoding) {
133 gchar *conv_str;
135 conv_str = conv_codeset_strdup
136 (buf, encoding, CS_INTERNAL);
137 if (!conv_str)
138 conv_str = g_strdup(buf);
139 prefs_config_parse_one_line(param, conv_str);
140 g_free(conv_str);
141 } else
142 prefs_config_parse_one_line(param, buf);
145 debug_print("Finished reading configuration.\n");
146 claws_fclose(fp);
149 static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
151 gint i;
152 gint name_len;
153 const gchar *value;
154 GdkRGBA color;
156 for (i = 0; param[i].name != NULL; i++) {
157 name_len = strlen(param[i].name);
158 if (g_ascii_strncasecmp(buf, param[i].name, name_len))
159 continue;
160 if (buf[name_len] != '=')
161 continue;
162 value = buf + name_len + 1;
163 /* debug_print("%s = %s\n", param[i].name, value); */
165 switch (param[i].type) {
166 case P_STRING:
167 case P_PASSWORD:
169 gchar *tmp = NULL;
171 if (*value) {
172 if (g_utf8_validate(value, -1, NULL))
173 tmp = g_strdup(value);
174 else {
175 tmp = conv_codeset_strdup(value,
176 conv_get_locale_charset_str_no_utf8(),
177 CS_INTERNAL);
179 } else {
180 tmp = g_strdup("");
182 if (!tmp) {
183 g_warning("failed to convert character set");
184 tmp = g_strdup(value);
186 g_free(*((gchar **)param[i].data));
187 *((gchar **)param[i].data) = tmp;
188 break;
190 case P_INT:
191 *((gint *)param[i].data) =
192 (gint)atoi(value);
193 break;
194 case P_BOOL:
195 *((gboolean *)param[i].data) =
196 (*value == '0' || *value == '\0')
197 ? FALSE : TRUE;
198 break;
199 case P_ENUM:
200 *((DummyEnum *)param[i].data) =
201 (DummyEnum)atoi(value);
202 break;
203 case P_USHORT:
204 *((gushort *)param[i].data) =
205 (gushort)atoi(value);
206 break;
207 case P_COLOR:
208 if (!gdk_rgba_parse(&color, value)) {
209 /* be compatible and accept ints */
210 INTCOLOR_TO_GDKRGBA(strtoul(value, 0, 10), color);
212 *((GdkRGBA *)param[i].data) = color;
213 break;
214 default:
215 break;
220 #define TRY(func) \
221 if (!(func)) \
223 g_warning("failed to write configuration to file"); \
224 if (orig_fp) claws_fclose(orig_fp); \
225 prefs_file_close_revert(pfile); \
226 g_free(rcpath); \
227 g_free(block_label); \
228 return; \
231 void prefs_write_config(PrefParam *param, const gchar *label,
232 const gchar *rcfile)
234 FILE *orig_fp;
235 PrefFile *pfile;
236 gchar *rcpath;
237 gchar buf[PREFSBUFSIZE];
238 gchar *block_label = NULL;
239 gboolean block_matched = FALSE;
241 cm_return_if_fail(param != NULL);
242 cm_return_if_fail(label != NULL);
243 cm_return_if_fail(rcfile != NULL);
245 rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
246 if ((orig_fp = claws_fopen(rcpath, "rb")) == NULL) {
247 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "claws_fopen");
250 if ((pfile = prefs_write_open(rcpath)) == NULL) {
251 g_warning("failed to write configuration to file");
252 if (orig_fp) claws_fclose(orig_fp);
253 g_free(rcpath);
254 return;
257 block_label = g_strdup_printf("[%s]", label);
259 /* search aiming block */
260 if (orig_fp) {
261 while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
262 gint val;
264 val = strncmp(buf, block_label, strlen(block_label));
265 if (val == 0) {
266 debug_print("Found %s\n", block_label);
267 block_matched = TRUE;
268 break;
269 } else
270 TRY(claws_fputs(buf, pfile->fp) != EOF);
274 TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
276 /* write all param data to file */
277 TRY(prefs_write_param(param, pfile->fp) == 0);
279 if (block_matched) {
280 gboolean in_dup_block = FALSE;
281 while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
282 /* next block */
283 if (buf[0] == '[') {
284 TRY(claws_fputc('\n', pfile->fp) != EOF &&
285 claws_fputs(buf, pfile->fp) != EOF);
286 break;
289 while (claws_fgets(buf, sizeof(buf), orig_fp) != NULL) {
290 if (buf[0] == '[') {
291 if (!strncmp(buf, block_label,
292 strlen(block_label)))
293 in_dup_block = TRUE;
294 else
295 in_dup_block = FALSE;
297 if (!in_dup_block)
298 TRY(claws_fputs(buf, pfile->fp) != EOF);
302 g_free(block_label);
303 block_label = NULL;
305 if (orig_fp) claws_fclose(orig_fp);
306 if (prefs_file_close(pfile) < 0)
307 g_warning("failed to write configuration to file");
308 g_free(rcpath);
310 debug_print("Configuration is saved.\n");
313 gint prefs_write_param(PrefParam *param, FILE *fp)
315 gint i;
316 gchar buf[PREFSBUFSIZE] = "";
317 gchar *tmp;
319 for (i = 0; param[i].name != NULL; i++) {
320 switch (param[i].type) {
321 case P_STRING:
323 gchar *tmp = NULL;
325 if (*((gchar **)param[i].data)) {
326 if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
327 tmp = g_strdup(*((gchar **)param[i].data));
328 else {
329 tmp = conv_codeset_strdup(*((gchar **)param[i].data),
330 conv_get_locale_charset_str_no_utf8(),
331 CS_INTERNAL);
332 if (!tmp)
333 tmp = g_strdup(*((gchar **)param[i].data));
337 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
338 tmp ? tmp : "");
340 g_free(tmp);
341 break;
343 case P_PASSWORD:
344 buf[0] = '\0'; /* Passwords are written to password store. */
345 break;
346 case P_INT:
347 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
348 *((gint *)param[i].data));
349 break;
350 case P_BOOL:
351 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
352 *((gboolean *)param[i].data));
353 break;
354 case P_ENUM:
355 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
356 *((DummyEnum *)param[i].data));
357 break;
358 case P_USHORT:
359 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
360 *((gushort *)param[i].data));
361 break;
362 case P_COLOR:
363 tmp = gtkut_gdk_rgba_to_string((GdkRGBA *)param[i].data);
364 g_snprintf(buf, sizeof buf, "%s=%s\n", param[i].name, tmp);
365 g_free(tmp);
366 break;
367 default:
368 /* unrecognized, fail */
369 debug_print("Unrecognized parameter type\n");
370 return -1;
373 if (buf[0] != '\0') {
374 if (claws_fputs(buf, fp) == EOF) {
375 perror("claws_fputs");
376 return -1;
381 return 0;
384 void prefs_set_default(PrefParam *param)
386 gint i;
387 GdkRGBA color;
389 cm_return_if_fail(param != NULL);
391 for (i = 0; param[i].name != NULL; i++) {
392 if (!param[i].data) continue;
394 switch (param[i].type) {
395 case P_STRING:
396 g_free(*((gchar **)param[i].data));
397 if (param[i].defval != NULL) {
398 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
399 const gchar *envstr;
400 gchar *tmp;
402 envstr = g_getenv(param[i].defval + 4);
403 tmp = envstr && *envstr ?
404 conv_codeset_strdup(envstr,
405 conv_get_locale_charset_str(),
406 CS_INTERNAL)
407 : g_strdup("");
408 if (!tmp) {
409 g_warning("failed to convert character set");
410 tmp = g_strdup(envstr);
412 *((gchar **)param[i].data) = tmp;
413 } else if (param[i].defval[0] == '~')
414 *((gchar **)param[i].data) =
415 g_strconcat(get_home_dir(),
416 param[i].defval + 1,
417 NULL);
418 else
419 *((gchar **)param[i].data) =
420 g_strdup(param[i].defval);
421 } else
422 *((gchar **)param[i].data) = NULL;
423 break;
424 case P_PASSWORD:
425 g_free(*((gchar **)param[i].data));
426 if (param[i].defval != NULL) {
427 if (param[i].defval[0] != '\0')
428 *((gchar **)param[i].data) =
429 g_strdup(param[i].defval);
430 else
431 *((gchar **)param[i].data) = NULL;
432 } else
433 *((gchar **)param[i].data) = NULL;
434 break;
435 case P_INT:
436 if (param[i].defval != NULL)
437 *((gint *)param[i].data) =
438 (gint)atoi(param[i].defval);
439 else if (!strcmp(param[i].name, "config_version"))
440 *((gint *)param[i].data) = CLAWS_CONFIG_VERSION;
441 else
442 *((gint *)param[i].data) = 0;
443 break;
444 case P_BOOL:
445 if (param[i].defval != NULL) {
446 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
447 *((gboolean *)param[i].data) = TRUE;
448 else
449 *((gboolean *)param[i].data) = atoi(param[i].defval) ? TRUE : FALSE;
450 } else
451 *((gboolean *)param[i].data) = FALSE;
452 break;
453 case P_ENUM:
454 if (param[i].defval != NULL)
455 *((DummyEnum*)param[i].data) =
456 (DummyEnum)atoi(param[i].defval);
457 else
458 *((DummyEnum *)param[i].data) = 0;
459 break;
460 case P_USHORT:
461 if (param[i].defval != NULL)
462 *((gushort *)param[i].data) = (gushort)atoi(param[i].defval);
463 else
464 *((gushort *)param[i].data) = 0;
465 break;
466 case P_COLOR:
467 if (param[i].defval != NULL && gdk_rgba_parse(&color, param[i].defval)) {
468 color.alpha = 1;
469 *((GdkRGBA *)param[i].data) = color;
470 } else if (param[i].defval) {
471 /* be compatible and accept ints */
472 INTCOLOR_TO_GDKRGBA(strtoul(param[i].defval, 0, 10), color);
473 *((GdkRGBA *)param[i].data) = color;
474 } else {
475 /* set to black as fallback */
476 color.red = color.green = color.blue = 0;
477 color.alpha = 1;
478 *((GdkRGBA *)param[i].data) = color;
480 break;
481 default:
482 break;
487 void prefs_free(PrefParam *param)
489 gint i;
491 cm_return_if_fail(param != NULL);
493 for (i = 0; param[i].name != NULL; i++) {
494 if (!param[i].data) continue;
496 switch (param[i].type) {
497 case P_STRING:
498 case P_PASSWORD:
499 g_free(*((gchar **)param[i].data));
500 break;
501 default:
502 break;
507 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
509 gboolean is_active;
511 is_active = gtk_toggle_button_get_active(toggle_btn);
512 gtk_widget_set_sensitive(widget, is_active);
515 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
517 gboolean is_active;
519 is_active = gtk_toggle_button_get_active(toggle_btn);
520 gtk_widget_set_sensitive(widget, !is_active);
523 void prefs_set_dialog(PrefParam *param)
525 gint i;
527 for (i = 0; param[i].name != NULL; i++) {
528 if (param[i].widget_set_func)
529 param[i].widget_set_func(&param[i]);
533 void prefs_set_data_from_dialog(PrefParam *param)
535 gint i;
537 for (i = 0; param[i].name != NULL; i++) {
538 if (param[i].data_set_func)
539 param[i].data_set_func(&param[i]);
543 void prefs_set_dialog_to_default(PrefParam *param)
545 gint i;
546 PrefParam tmpparam;
547 gchar *str_data = NULL;
548 gint int_data;
549 gushort ushort_data;
550 gboolean bool_data;
551 DummyEnum enum_data;
553 for (i = 0; param[i].name != NULL; i++) {
554 if (!param[i].widget_set_func) continue;
556 tmpparam = param[i];
558 switch (tmpparam.type) {
559 case P_STRING:
560 if (tmpparam.defval) {
561 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
562 str_data = g_strdup(g_getenv(param[i].defval + 4));
563 tmpparam.data = &str_data;
564 break;
565 } else if (tmpparam.defval[0] == '~') {
566 str_data =
567 g_strconcat(get_home_dir(),
568 param[i].defval + 1,
569 NULL);
570 tmpparam.data = &str_data;
571 break;
574 tmpparam.data = &tmpparam.defval;
575 break;
576 case P_PASSWORD:
577 tmpparam.data = &tmpparam.defval;
578 break;
579 case P_INT:
580 if (tmpparam.defval)
581 int_data = atoi(tmpparam.defval);
582 else
583 int_data = 0;
584 tmpparam.data = &int_data;
585 break;
586 case P_USHORT:
587 if (tmpparam.defval)
588 ushort_data = atoi(tmpparam.defval);
589 else
590 ushort_data = 0;
591 tmpparam.data = &ushort_data;
592 break;
593 case P_BOOL:
594 if (tmpparam.defval) {
595 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
596 bool_data = TRUE;
597 else
598 bool_data = atoi(tmpparam.defval)
599 ? TRUE : FALSE;
600 } else
601 bool_data = FALSE;
602 tmpparam.data = &bool_data;
603 break;
604 case P_ENUM:
605 if (tmpparam.defval)
606 enum_data = (DummyEnum)atoi(tmpparam.defval);
607 else
608 enum_data = 0;
609 tmpparam.data = &enum_data;
610 break;
611 case P_OTHER:
612 default:
613 break;
615 tmpparam.widget_set_func(&tmpparam);
616 g_free(str_data);
617 str_data = NULL;
621 void prefs_set_data_from_entry(PrefParam *pparam)
623 gchar **str;
624 const gchar *entry_str;
626 cm_return_if_fail(*pparam->widget != NULL);
628 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
630 switch (pparam->type) {
631 case P_STRING:
632 str = (gchar **)pparam->data;
633 g_free(*str);
634 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
635 break;
636 case P_USHORT:
637 *((gushort *)pparam->data) = atoi(entry_str);
638 break;
639 case P_INT:
640 *((gint *)pparam->data) = atoi(entry_str);
641 break;
642 default:
643 g_warning("invalid PrefType for GtkEntry widget: %d",
644 pparam->type);
648 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
650 gchar **str;
652 cm_return_if_fail(*pparam->widget != NULL);
654 switch (pparam->type) {
655 case P_STRING:
656 str = (gchar **)pparam->data;
657 g_free(*str);
658 *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
659 break;
660 default:
661 g_warning("invalid escaped PrefType for GtkEntry widget: %d",
662 pparam->type);
666 void prefs_set_entry(PrefParam *pparam)
668 gchar **str;
669 cm_return_if_fail(*pparam->widget != NULL);
671 switch (pparam->type) {
672 case P_STRING:
673 str = (gchar **)pparam->data;
674 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
675 *str ? *str : "");
676 break;
677 case P_INT:
678 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
679 itos(*((gint *)pparam->data)));
680 break;
681 case P_USHORT:
682 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
683 itos(*((gushort *)pparam->data)));
684 break;
685 default:
686 g_warning("invalid PrefType for GtkEntry widget: %d",
687 pparam->type);
691 void prefs_set_entry_from_escaped(PrefParam *pparam)
693 gchar **str;
695 cm_return_if_fail(*pparam->widget != NULL);
697 switch (pparam->type) {
698 case P_STRING:
699 str = (gchar **)pparam->data;
700 pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
701 *str ? *str : "");
702 break;
703 default:
704 g_warning("invalid escaped PrefType for GtkEntry widget: %d",
705 pparam->type);
709 void prefs_set_data_from_text(PrefParam *pparam)
711 gchar **str;
712 gchar *text = NULL, *tp = NULL;
713 gchar *tmp, *tmpp;
715 cm_return_if_fail(*pparam->widget != NULL);
717 switch (pparam->type) {
718 case P_STRING:
719 str = (gchar **)pparam->data;
720 g_free(*str);
721 if (GTK_IS_EDITABLE(*pparam->widget)) { /* need? */
722 tp = text = gtk_editable_get_chars
723 (GTK_EDITABLE(*pparam->widget), 0, -1);
724 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
725 GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
726 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
727 GtkTextIter start, end;
728 gtk_text_buffer_get_start_iter(buffer, &start);
729 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
730 tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
733 cm_return_if_fail (tp && text);
735 if (text[0] == '\0') {
736 *str = NULL;
737 g_free(text);
738 break;
741 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
742 { *str = NULL; break; });
743 while (*tp) {
744 if (*tp == '\n') {
745 *tmpp++ = '\\';
746 *tmpp++ = 'n';
747 tp++;
748 } else
749 *tmpp++ = *tp++;
751 *tmpp = '\0';
752 *str = g_strdup(tmp);
753 g_free(text);
754 break;
755 default:
756 g_warning("invalid PrefType for GtkText widget: %d",
757 pparam->type);
761 void prefs_set_escaped_data_from_text(PrefParam *pparam)
763 gchar **str;
765 cm_return_if_fail(*pparam->widget != NULL);
767 switch (pparam->type) {
768 case P_STRING:
769 str = (gchar **)pparam->data;
770 g_free(*str);
771 *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
772 break;
773 default:
774 g_warning("invalid escaped PrefType for GtkText widget: %d",
775 pparam->type);
779 void prefs_set_text(PrefParam *pparam)
781 gchar *buf, *sp, *bufp;
782 gchar **str;
783 GtkTextView *text;
784 GtkTextBuffer *buffer;
785 GtkTextIter iter;
787 cm_return_if_fail(*pparam->widget != NULL);
789 switch (pparam->type) {
790 case P_STRING:
791 str = (gchar **)pparam->data;
792 if (*str) {
793 bufp = buf = alloca(strlen(*str) + 1);
794 if (!buf) buf = "";
795 else {
796 sp = *str;
797 while (*sp) {
798 if (*sp == '\\' && *(sp + 1) == 'n') {
799 *bufp++ = '\n';
800 sp += 2;
801 } else
802 *bufp++ = *sp++;
804 *bufp = '\0';
806 } else
807 buf = "";
809 text = GTK_TEXT_VIEW(*pparam->widget);
810 buffer = gtk_text_view_get_buffer(text);
811 gtk_text_buffer_set_text(buffer, "", -1);
812 gtk_text_buffer_get_start_iter(buffer, &iter);
813 gtk_text_buffer_insert(buffer, &iter, buf, -1);
814 break;
815 default:
816 g_warning("invalid PrefType for GtkTextView widget: %d",
817 pparam->type);
821 void prefs_set_text_from_escaped(PrefParam *pparam)
823 gchar **str;
825 cm_return_if_fail(*pparam->widget != NULL);
827 switch (pparam->type) {
828 case P_STRING:
829 str = (gchar **)pparam->data;
830 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
831 *str ? *str : "");
832 break;
833 default:
834 g_warning("invalid escaped PrefType for GtkTextView widget: %d",
835 pparam->type);
839 void prefs_set_data_from_toggle(PrefParam *pparam)
841 cm_return_if_fail(pparam->type == P_BOOL);
842 cm_return_if_fail(*pparam->widget != NULL);
844 *((gboolean *)pparam->data) =
845 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
848 void prefs_set_toggle(PrefParam *pparam)
850 cm_return_if_fail(pparam->type == P_BOOL);
851 cm_return_if_fail(*pparam->widget != NULL);
853 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
854 *((gboolean *)pparam->data));
857 void prefs_set_data_from_spinbtn(PrefParam *pparam)
859 cm_return_if_fail(*pparam->widget != NULL);
861 switch (pparam->type) {
862 case P_INT:
863 *((gint *)pparam->data) =
864 gtk_spin_button_get_value_as_int
865 (GTK_SPIN_BUTTON(*pparam->widget));
866 break;
867 case P_USHORT:
868 *((gushort *)pparam->data) =
869 (gushort)gtk_spin_button_get_value_as_int
870 (GTK_SPIN_BUTTON(*pparam->widget));
871 break;
872 default:
873 g_warning("invalid PrefType for GtkSpinButton widget: %d",
874 pparam->type);
878 void prefs_set_spinbtn(PrefParam *pparam)
880 cm_return_if_fail(*pparam->widget != NULL);
882 switch (pparam->type) {
883 case P_INT:
884 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
885 (gfloat)*((gint *)pparam->data));
886 break;
887 case P_USHORT:
888 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
889 (gfloat)*((gushort *)pparam->data));
890 break;
891 default:
892 g_warning("invalid PrefType for GtkSpinButton widget: %d",
893 pparam->type);
897 static GSList *prefs_pages = NULL;
899 static void prefs_gtk_window_closed_cb(PrefsWindow *prefswindow)
901 if (prefswindow == NULL)
902 return;
904 if (prefswindow->dialog_response > PREFSWINDOW_RESPONSE_CANCEL)
905 prefs_common_write_config();
908 void prefs_gtk_open(void)
910 prefswindow_open(_("Preferences"), prefs_pages, NULL,
911 &prefs_common.prefswin_width, &prefs_common.prefswin_height,
912 NULL, prefs_gtk_window_closed_cb, prefs_gtk_window_closed_cb);
915 void prefs_gtk_register_page(PrefsPage *page)
917 prefs_pages = g_slist_append(prefs_pages, page);
920 void prefs_gtk_unregister_page(PrefsPage *page)
922 prefs_pages = g_slist_remove(prefs_pages, page);
925 static void prefs_destroy_whole_cache(gpointer to_free)
927 GHashTable *table = (GHashTable *)to_free;
928 g_hash_table_destroy(table);
931 static void prefs_destroy_file_cache(gpointer to_free)
933 GHashTable *table = (GHashTable *)to_free;
934 g_hash_table_destroy(table);
937 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
939 FILE *fp = NULL;
940 gchar buf[PREFSBUFSIZE];
941 GHashTable *section_cache = NULL;
943 if (rcfile)
944 fp = claws_fopen(rcfile, "rb");
945 if (!fp) {
946 debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
947 return -1;
950 while (claws_fgets(buf, sizeof(buf), fp) != NULL) {
951 strretchomp(buf);
952 if (buf[0] == '\0')
953 continue;
954 if (buf[0] == '#')
955 continue; /* comment */
956 if (buf[0] == '[') { /* new section */
957 gchar *blockname = g_strdup(buf+1);
959 if (strrchr(blockname, ']'))
960 *strrchr(blockname, ']') = '\0';
962 if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
963 debug_print("new section '%s'\n", blockname);
964 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
965 g_free, NULL);
966 g_hash_table_insert(file_cache,
967 blockname, section_cache);
968 } else {
969 debug_print("section '%s' already done\n", blockname);
970 g_free(blockname);
971 section_cache = NULL;
972 continue;
974 } else {
975 if (!section_cache) {
976 debug_print("skipping stuff %s with no section\n", buf);
977 continue;
978 } else {
979 gchar *pref;
981 if (!strchr(buf, '=')) {
982 /* plugins do differently */
983 continue;
985 pref = g_strdup(buf);
987 //debug_print("new pref '%s'\n", pref);
988 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
992 claws_fclose(fp);
993 return 0;
996 static int prefs_cache(const gchar *rcfile)
998 GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
999 g_free, prefs_destroy_file_cache);
1001 debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
1002 g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1004 return prefs_cache_sections(file_cache, rcfile);
1007 void prefs_prepare_cache(void)
1009 gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1010 gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1011 gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1013 if (whole_cache == NULL) {
1014 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1015 g_free, prefs_destroy_whole_cache);
1016 } else {
1017 debug_print("already cached\n");
1018 g_free(clawsrc);
1019 g_free(folderitemrc);
1020 g_free(accountrc);
1021 return;
1023 if (prefs_cache(clawsrc) < 0 ||
1024 prefs_cache(folderitemrc) < 0 ||
1025 prefs_cache(accountrc) < 0)
1026 prefs_destroy_cache();
1028 g_free(clawsrc);
1029 g_free(folderitemrc);
1030 g_free(accountrc);
1033 void prefs_destroy_cache(void)
1035 if (!whole_cache) {
1036 debug_print("no cache\n");
1037 return;
1039 debug_print("destroying cache\n");
1040 g_hash_table_destroy(whole_cache);
1041 whole_cache = NULL;
1042 return;
1045 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1047 gchar *pref = (gchar *)key;
1049 PrefParam *param = (PrefParam *)user_data;
1051 prefs_config_parse_one_line(param, pref);
1054 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1055 const gchar *rcfile)
1057 GHashTable *sections_table = NULL;
1058 GHashTable *values_table = NULL;
1059 sections_table = g_hash_table_lookup(whole_cache, rcfile);
1061 if (sections_table == NULL) {
1062 g_warning("can't find %s in the whole cache", rcfile?rcfile:"(null)");
1063 return FALSE;
1065 values_table = g_hash_table_lookup(sections_table, label);
1067 if (values_table == NULL) {
1068 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1069 return TRUE;
1071 g_hash_table_foreach(values_table, prefs_parse_cache, param);
1072 return TRUE;