Fix bug #3574: Template addressing
[claws.git] / src / prefs_gtk.c
blobccab0a3ae4250bef532ddff09fb7c93d4a550ab1
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 #define _GNU_SOURCE
26 #include <stdio.h>
28 #include <glib.h>
29 #include <glib/gi18n.h>
30 #include <gtk/gtk.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34 #include <errno.h>
36 #include "defs.h"
37 #include "main.h"
38 #include "prefs.h"
39 #include "prefs_gtk.h"
40 #include "prefs_common.h"
41 #include "utils.h"
42 #include "gtkutils.h"
43 #include "password.h"
44 #include "codeconv.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 #ifdef HAVE_FGETS_UNLOCKED
53 #define SC_FGETS fgets_unlocked
54 #else
55 #define SC_FGETS fgets
56 #endif
58 typedef enum
60 DUMMY_PARAM
61 } DummyEnum;
63 static GHashTable *whole_cache = NULL;
65 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
66 const gchar *rcfile);
68 static void prefs_config_parse_one_line(PrefParam *param,
69 const gchar *buf);
71 void prefs_read_config(PrefParam *param, const gchar *label,
72 const gchar *rcfile, const gchar *encoding)
74 FILE *fp;
75 gchar buf[PREFSBUFSIZE];
76 gchar *block_label;
78 cm_return_if_fail(param != NULL);
79 cm_return_if_fail(label != NULL);
80 cm_return_if_fail(rcfile != NULL);
82 if (encoding != 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)
91 return;
94 if ((fp = g_fopen(rcfile, "rb")) == NULL) {
95 if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
96 return;
99 block_label = g_strdup_printf("[%s]", label);
101 #ifdef HAVE_FGETS_UNLOCKED
102 flockfile(fp);
103 #endif
105 /* search aiming block */
106 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
107 gint val;
109 if (encoding) {
110 gchar *conv_str;
112 conv_str = conv_codeset_strdup
113 (buf, encoding, CS_INTERNAL);
114 if (!conv_str)
115 conv_str = g_strdup(buf);
116 val = strncmp
117 (conv_str, block_label, strlen(block_label));
118 g_free(conv_str);
119 } else
120 val = strncmp(buf, block_label, strlen(block_label));
121 if (val == 0) {
122 debug_print("Found %s\n", block_label);
123 break;
126 g_free(block_label);
128 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
129 strretchomp(buf);
130 /* reached next block */
131 if (buf[0] == '[') break;
132 if (buf[0] == '#') continue;
134 if (encoding) {
135 gchar *conv_str;
137 conv_str = conv_codeset_strdup
138 (buf, encoding, CS_INTERNAL);
139 if (!conv_str)
140 conv_str = g_strdup(buf);
141 prefs_config_parse_one_line(param, conv_str);
142 g_free(conv_str);
143 } else
144 prefs_config_parse_one_line(param, buf);
147 debug_print("Finished reading configuration.\n");
148 #ifdef HAVE_FGETS_UNLOCKED
149 funlockfile(fp);
150 #endif
151 fclose(fp);
154 static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
156 gint i;
157 gint name_len;
158 const gchar *value;
159 GdkColor color;
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))
164 continue;
165 if (buf[name_len] != '=')
166 continue;
167 value = buf + name_len + 1;
168 /* debug_print("%s = %s\n", param[i].name, value); */
170 switch (param[i].type) {
171 case P_STRING:
172 case P_PASSWORD:
174 gchar *tmp = NULL;
176 if (*value) {
177 if (g_utf8_validate(value, -1, NULL))
178 tmp = g_strdup(value);
179 else {
180 tmp = conv_codeset_strdup(value,
181 conv_get_locale_charset_str_no_utf8(),
182 CS_INTERNAL);
184 } else {
185 tmp = g_strdup("");
187 if (!tmp) {
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;
193 break;
195 case P_INT:
196 *((gint *)param[i].data) =
197 (gint)atoi(value);
198 break;
199 case P_BOOL:
200 *((gboolean *)param[i].data) =
201 (*value == '0' || *value == '\0')
202 ? FALSE : TRUE;
203 break;
204 case P_ENUM:
205 *((DummyEnum *)param[i].data) =
206 (DummyEnum)atoi(value);
207 break;
208 case P_USHORT:
209 *((gushort *)param[i].data) =
210 (gushort)atoi(value);
211 break;
212 case P_COLOR:
213 if (gdk_color_parse(value, &color)) {
214 *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color);
216 else
217 /* be compatible and accept ints */
218 *((gulong *)param[i].data) = strtoul(value, 0, 10);
219 break;
220 default:
221 break;
226 #define TRY(func) \
227 if (!(func)) \
229 g_warning("Failed to write configuration to file"); \
230 if (orig_fp) fclose(orig_fp); \
231 prefs_file_close_revert(pfile); \
232 g_free(rcpath); \
233 g_free(block_label); \
234 return; \
237 void prefs_write_config(PrefParam *param, const gchar *label,
238 const gchar *rcfile)
240 FILE *orig_fp;
241 PrefFile *pfile;
242 gchar *rcpath;
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);
259 g_free(rcpath);
260 return;
263 block_label = g_strdup_printf("[%s]", label);
265 /* search aiming block */
266 if (orig_fp) {
267 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
268 gint val;
270 val = strncmp(buf, block_label, strlen(block_label));
271 if (val == 0) {
272 debug_print("Found %s\n", block_label);
273 block_matched = TRUE;
274 break;
275 } else
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);
285 if (block_matched) {
286 gboolean in_dup_block = FALSE;
287 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
288 /* next block */
289 if (buf[0] == '[') {
290 TRY(fputc('\n', pfile->fp) != EOF &&
291 fputs(buf, pfile->fp) != EOF);
292 break;
295 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
296 if (buf[0] == '[') {
297 if (!strncmp(buf, block_label,
298 strlen(block_label)))
299 in_dup_block = TRUE;
300 else
301 in_dup_block = FALSE;
303 if (!in_dup_block)
304 TRY(fputs(buf, pfile->fp) != EOF);
308 g_free(block_label);
309 block_label = NULL;
311 if (orig_fp) fclose(orig_fp);
312 if (prefs_file_close(pfile) < 0)
313 g_warning("Failed to write configuration to file");
314 g_free(rcpath);
316 debug_print("Configuration is saved.\n");
319 gint prefs_write_param(PrefParam *param, FILE *fp)
321 gint i;
322 gchar buf[PREFSBUFSIZE] = "";
324 for (i = 0; param[i].name != NULL; i++) {
325 switch (param[i].type) {
326 case P_STRING:
328 gchar *tmp = NULL;
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));
333 else {
334 tmp = conv_codeset_strdup(*((gchar **)param[i].data),
335 conv_get_locale_charset_str_no_utf8(),
336 CS_INTERNAL);
337 if (!tmp)
338 tmp = g_strdup(*((gchar **)param[i].data));
342 g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
343 tmp ? tmp : "");
345 g_free(tmp);
346 break;
348 case P_PASSWORD:
349 buf[0] = '\0'; /* Passwords are written to password store. */
350 break;
351 case P_INT:
352 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
353 *((gint *)param[i].data));
354 break;
355 case P_BOOL:
356 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
357 *((gboolean *)param[i].data));
358 break;
359 case P_ENUM:
360 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
361 *((DummyEnum *)param[i].data));
362 break;
363 case P_USHORT:
364 g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
365 *((gushort *)param[i].data));
366 break;
367 case P_COLOR:
368 g_snprintf(buf, sizeof buf, "%s=#%6.6lx\n", param[i].name,
369 *((gulong *) param[i].data));
370 break;
371 default:
372 /* unrecognized, fail */
373 debug_print("Unrecognized parameter type\n");
374 return -1;
377 if (buf[0] != '\0') {
378 if (fputs(buf, fp) == EOF) {
379 perror("fputs");
380 return -1;
385 return 0;
388 void prefs_set_default(PrefParam *param)
390 gint i;
391 GdkColor color;
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) {
399 case P_STRING:
400 g_free(*((gchar **)param[i].data));
401 if (param[i].defval != NULL) {
402 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
403 const gchar *envstr;
404 gchar *tmp;
406 envstr = g_getenv(param[i].defval + 4);
407 tmp = envstr && *envstr ?
408 conv_codeset_strdup(envstr,
409 conv_get_locale_charset_str(),
410 CS_INTERNAL)
411 : g_strdup("");
412 if (!tmp) {
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(),
420 param[i].defval + 1,
421 NULL);
422 else
423 *((gchar **)param[i].data) =
424 g_strdup(param[i].defval);
425 } else
426 *((gchar **)param[i].data) = NULL;
427 break;
428 case P_PASSWORD:
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);
434 else
435 *((gchar **)param[i].data) = NULL;
436 } else
437 *((gchar **)param[i].data) = NULL;
438 break;
439 case P_INT:
440 if (param[i].defval != NULL)
441 *((gint *)param[i].data) =
442 (gint)atoi(param[i].defval);
443 else
444 *((gint *)param[i].data) = 0;
445 break;
446 case P_BOOL:
447 if (param[i].defval != NULL) {
448 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
449 *((gboolean *)param[i].data) = TRUE;
450 else
451 *((gboolean *)param[i].data) =
452 atoi(param[i].defval) ? TRUE : FALSE;
453 } else
454 *((gboolean *)param[i].data) = FALSE;
455 break;
456 case P_ENUM:
457 if (param[i].defval != NULL)
458 *((DummyEnum*)param[i].data) =
459 (DummyEnum)atoi(param[i].defval);
460 else
461 *((DummyEnum *)param[i].data) = 0;
462 break;
463 case P_USHORT:
464 if (param[i].defval != NULL)
465 *((gushort *)param[i].data) =
466 (gushort)atoi(param[i].defval);
467 else
468 *((gushort *)param[i].data) = 0;
469 break;
470 case P_COLOR:
471 if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
472 *((gulong *)param[i].data) =
473 RGB_FROM_GDK_COLOR(color);
474 else if (param[i].defval)
475 /* be compatible and accept ints */
476 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10);
477 else
478 *((gulong *)param[i].data) = 0;
479 break;
480 default:
481 break;
486 void prefs_free(PrefParam *param)
488 gint i;
490 cm_return_if_fail(param != NULL);
492 for (i = 0; param[i].name != NULL; i++) {
493 if (!param[i].data) continue;
495 switch (param[i].type) {
496 case P_STRING:
497 case P_PASSWORD:
498 g_free(*((gchar **)param[i].data));
499 break;
500 default:
501 break;
506 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
508 gboolean is_active;
510 is_active = gtk_toggle_button_get_active(toggle_btn);
511 gtk_widget_set_sensitive(widget, is_active);
514 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
516 gboolean is_active;
518 is_active = gtk_toggle_button_get_active(toggle_btn);
519 gtk_widget_set_sensitive(widget, !is_active);
522 void prefs_set_dialog(PrefParam *param)
524 gint i;
526 for (i = 0; param[i].name != NULL; i++) {
527 if (param[i].widget_set_func)
528 param[i].widget_set_func(&param[i]);
532 void prefs_set_data_from_dialog(PrefParam *param)
534 gint i;
536 for (i = 0; param[i].name != NULL; i++) {
537 if (param[i].data_set_func)
538 param[i].data_set_func(&param[i]);
542 void prefs_set_dialog_to_default(PrefParam *param)
544 gint i;
545 PrefParam tmpparam;
546 gchar *str_data = NULL;
547 gint int_data;
548 gushort ushort_data;
549 gboolean bool_data;
550 DummyEnum enum_data;
552 for (i = 0; param[i].name != NULL; i++) {
553 if (!param[i].widget_set_func) continue;
555 tmpparam = param[i];
557 switch (tmpparam.type) {
558 case P_STRING:
559 if (tmpparam.defval) {
560 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
561 str_data = g_strdup(g_getenv(param[i].defval + 4));
562 tmpparam.data = &str_data;
563 break;
564 } else if (tmpparam.defval[0] == '~') {
565 str_data =
566 g_strconcat(get_home_dir(),
567 param[i].defval + 1,
568 NULL);
569 tmpparam.data = &str_data;
570 break;
573 tmpparam.data = &tmpparam.defval;
574 break;
575 case P_PASSWORD:
576 tmpparam.data = &tmpparam.defval;
577 break;
578 case P_INT:
579 if (tmpparam.defval)
580 int_data = atoi(tmpparam.defval);
581 else
582 int_data = 0;
583 tmpparam.data = &int_data;
584 break;
585 case P_USHORT:
586 if (tmpparam.defval)
587 ushort_data = atoi(tmpparam.defval);
588 else
589 ushort_data = 0;
590 tmpparam.data = &ushort_data;
591 break;
592 case P_BOOL:
593 if (tmpparam.defval) {
594 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
595 bool_data = TRUE;
596 else
597 bool_data = atoi(tmpparam.defval)
598 ? TRUE : FALSE;
599 } else
600 bool_data = FALSE;
601 tmpparam.data = &bool_data;
602 break;
603 case P_ENUM:
604 if (tmpparam.defval)
605 enum_data = (DummyEnum)atoi(tmpparam.defval);
606 else
607 enum_data = 0;
608 tmpparam.data = &enum_data;
609 break;
610 case P_OTHER:
611 default:
612 break;
614 tmpparam.widget_set_func(&tmpparam);
615 g_free(str_data);
616 str_data = NULL;
620 void prefs_set_data_from_entry(PrefParam *pparam)
622 gchar **str;
623 const gchar *entry_str;
625 cm_return_if_fail(*pparam->widget != NULL);
627 entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
629 switch (pparam->type) {
630 case P_STRING:
631 str = (gchar **)pparam->data;
632 g_free(*str);
633 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
634 break;
635 case P_PASSWORD:
636 break;
637 case P_USHORT:
638 *((gushort *)pparam->data) = atoi(entry_str);
639 break;
640 case P_INT:
641 *((gint *)pparam->data) = atoi(entry_str);
642 break;
643 default:
644 g_warning("Invalid PrefType for GtkEntry widget: %d",
645 pparam->type);
649 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
651 gchar **str;
653 cm_return_if_fail(*pparam->widget != NULL);
655 switch (pparam->type) {
656 case P_STRING:
657 str = (gchar **)pparam->data;
658 g_free(*str);
659 *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
660 break;
661 default:
662 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
663 pparam->type);
667 void prefs_set_entry(PrefParam *pparam)
669 gchar **str;
670 cm_return_if_fail(*pparam->widget != NULL);
672 switch (pparam->type) {
673 case P_STRING:
674 str = (gchar **)pparam->data;
675 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
676 *str ? *str : "");
677 break;
678 case P_INT:
679 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
680 itos(*((gint *)pparam->data)));
681 break;
682 case P_USHORT:
683 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
684 itos(*((gushort *)pparam->data)));
685 break;
686 default:
687 g_warning("Invalid PrefType for GtkEntry widget: %d",
688 pparam->type);
692 void prefs_set_entry_from_escaped(PrefParam *pparam)
694 gchar **str;
696 cm_return_if_fail(*pparam->widget != NULL);
698 switch (pparam->type) {
699 case P_STRING:
700 str = (gchar **)pparam->data;
701 pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
702 *str ? *str : "");
703 break;
704 default:
705 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
706 pparam->type);
710 void prefs_set_data_from_text(PrefParam *pparam)
712 gchar **str;
713 gchar *text = NULL, *tp = NULL;
714 gchar *tmp, *tmpp;
716 cm_return_if_fail(*pparam->widget != NULL);
718 switch (pparam->type) {
719 case P_STRING:
720 str = (gchar **)pparam->data;
721 g_free(*str);
722 if (GTK_IS_EDITABLE(*pparam->widget)) { /* need? */
723 tp = text = gtk_editable_get_chars
724 (GTK_EDITABLE(*pparam->widget), 0, -1);
725 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
726 GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
727 GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
728 GtkTextIter start, end;
729 gtk_text_buffer_get_start_iter(buffer, &start);
730 gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
731 tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
734 cm_return_if_fail (tp && text);
736 if (text[0] == '\0') {
737 *str = NULL;
738 g_free(text);
739 break;
742 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
743 { *str = NULL; break; });
744 while (*tp) {
745 if (*tp == '\n') {
746 *tmpp++ = '\\';
747 *tmpp++ = 'n';
748 tp++;
749 } else
750 *tmpp++ = *tp++;
752 *tmpp = '\0';
753 *str = g_strdup(tmp);
754 g_free(text);
755 break;
756 default:
757 g_warning("Invalid PrefType for GtkText widget: %d",
758 pparam->type);
762 void prefs_set_escaped_data_from_text(PrefParam *pparam)
764 gchar **str;
766 cm_return_if_fail(*pparam->widget != NULL);
768 switch (pparam->type) {
769 case P_STRING:
770 str = (gchar **)pparam->data;
771 g_free(*str);
772 *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
773 break;
774 default:
775 g_warning("Invalid escaped PrefType for GtkText widget: %d",
776 pparam->type);
780 void prefs_set_text(PrefParam *pparam)
782 gchar *buf, *sp, *bufp;
783 gchar **str;
784 GtkTextView *text;
785 GtkTextBuffer *buffer;
786 GtkTextIter iter;
788 cm_return_if_fail(*pparam->widget != NULL);
790 switch (pparam->type) {
791 case P_STRING:
792 str = (gchar **)pparam->data;
793 if (*str) {
794 bufp = buf = alloca(strlen(*str) + 1);
795 if (!buf) buf = "";
796 else {
797 sp = *str;
798 while (*sp) {
799 if (*sp == '\\' && *(sp + 1) == 'n') {
800 *bufp++ = '\n';
801 sp += 2;
802 } else
803 *bufp++ = *sp++;
805 *bufp = '\0';
807 } else
808 buf = "";
810 text = GTK_TEXT_VIEW(*pparam->widget);
811 buffer = gtk_text_view_get_buffer(text);
812 gtk_text_buffer_set_text(buffer, "", -1);
813 gtk_text_buffer_get_start_iter(buffer, &iter);
814 gtk_text_buffer_insert(buffer, &iter, buf, -1);
815 break;
816 default:
817 g_warning("Invalid PrefType for GtkTextView widget: %d",
818 pparam->type);
822 void prefs_set_text_from_escaped(PrefParam *pparam)
824 gchar **str;
826 cm_return_if_fail(*pparam->widget != NULL);
828 switch (pparam->type) {
829 case P_STRING:
830 str = (gchar **)pparam->data;
831 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
832 *str ? *str : "");
833 break;
834 default:
835 g_warning("Invalid escaped PrefType for GtkTextView widget: %d",
836 pparam->type);
840 void prefs_set_data_from_toggle(PrefParam *pparam)
842 cm_return_if_fail(pparam->type == P_BOOL);
843 cm_return_if_fail(*pparam->widget != NULL);
845 *((gboolean *)pparam->data) =
846 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
849 void prefs_set_toggle(PrefParam *pparam)
851 cm_return_if_fail(pparam->type == P_BOOL);
852 cm_return_if_fail(*pparam->widget != NULL);
854 gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
855 *((gboolean *)pparam->data));
858 void prefs_set_data_from_spinbtn(PrefParam *pparam)
860 cm_return_if_fail(*pparam->widget != NULL);
862 switch (pparam->type) {
863 case P_INT:
864 *((gint *)pparam->data) =
865 gtk_spin_button_get_value_as_int
866 (GTK_SPIN_BUTTON(*pparam->widget));
867 break;
868 case P_USHORT:
869 *((gushort *)pparam->data) =
870 (gushort)gtk_spin_button_get_value_as_int
871 (GTK_SPIN_BUTTON(*pparam->widget));
872 break;
873 default:
874 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
875 pparam->type);
879 void prefs_set_spinbtn(PrefParam *pparam)
881 cm_return_if_fail(*pparam->widget != NULL);
883 switch (pparam->type) {
884 case P_INT:
885 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
886 (gfloat)*((gint *)pparam->data));
887 break;
888 case P_USHORT:
889 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
890 (gfloat)*((gushort *)pparam->data));
891 break;
892 default:
893 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
894 pparam->type);
898 static GSList *prefs_pages = NULL;
900 static void prefs_gtk_window_closed_cb(PrefsWindow *prefswindow)
902 if (prefswindow == NULL)
903 return;
905 if (prefswindow->dialog_response > PREFSWINDOW_RESPONSE_CANCEL)
906 prefs_common_write_config();
909 void prefs_gtk_open(void)
911 prefswindow_open(_("Preferences"), prefs_pages, NULL,
912 &prefs_common.prefswin_width, &prefs_common.prefswin_height,
913 NULL, prefs_gtk_window_closed_cb, prefs_gtk_window_closed_cb);
916 void prefs_gtk_register_page(PrefsPage *page)
918 prefs_pages = g_slist_append(prefs_pages, page);
921 void prefs_gtk_unregister_page(PrefsPage *page)
923 prefs_pages = g_slist_remove(prefs_pages, page);
926 static void prefs_destroy_whole_cache(gpointer to_free)
928 GHashTable *table = (GHashTable *)to_free;
929 g_hash_table_destroy(table);
932 static void prefs_destroy_file_cache(gpointer to_free)
934 GHashTable *table = (GHashTable *)to_free;
935 g_hash_table_destroy(table);
938 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
940 FILE *fp = NULL;
941 gchar buf[PREFSBUFSIZE];
942 GHashTable *section_cache = NULL;
944 if (rcfile)
945 fp = g_fopen(rcfile, "rb");
946 if (!fp) {
947 debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
948 return -1;
951 #ifdef HAVE_FGETS_UNLOCKED
952 flockfile(fp);
953 #endif
955 while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
956 strretchomp(buf);
957 if (buf[0] == '\0')
958 continue;
959 if (buf[0] == '#')
960 continue; /* comment */
961 if (buf[0] == '[') { /* new section */
962 gchar *blockname = g_strdup(buf+1);
964 if (strrchr(blockname, ']'))
965 *strrchr(blockname, ']') = '\0';
967 if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
968 debug_print("new section '%s'\n", blockname);
969 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
970 g_free, NULL);
971 g_hash_table_insert(file_cache,
972 blockname, section_cache);
973 } else {
974 debug_print("section '%s' already done\n", blockname);
975 g_free(blockname);
976 section_cache = NULL;
977 continue;
979 } else {
980 if (!section_cache) {
981 debug_print("skipping stuff %s with no section\n", buf);
982 continue;
983 } else {
984 gchar *pref;
986 if (!strchr(buf, '=')) {
987 /* plugins do differently */
988 continue;
990 pref = g_strdup(buf);
992 //debug_print("new pref '%s'\n", pref);
993 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
997 #ifdef HAVE_FGETS_UNLOCKED
998 funlockfile(fp);
999 #endif
1000 fclose(fp);
1001 return 0;
1004 static int prefs_cache(const gchar *rcfile)
1006 GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1007 g_free, prefs_destroy_file_cache);
1009 debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
1010 g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1012 return prefs_cache_sections(file_cache, rcfile);
1015 void prefs_prepare_cache(void)
1017 gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1018 gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1019 gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1021 if (whole_cache == NULL) {
1022 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1023 g_free, prefs_destroy_whole_cache);
1024 } else {
1025 debug_print("already cached\n");
1026 g_free(clawsrc);
1027 g_free(folderitemrc);
1028 g_free(accountrc);
1029 return;
1031 if (prefs_cache(clawsrc) < 0 ||
1032 prefs_cache(folderitemrc) < 0 ||
1033 prefs_cache(accountrc) < 0)
1034 prefs_destroy_cache();
1036 g_free(clawsrc);
1037 g_free(folderitemrc);
1038 g_free(accountrc);
1041 void prefs_destroy_cache(void)
1043 if (!whole_cache) {
1044 debug_print("no cache\n");
1045 return;
1047 debug_print("destroying cache\n");
1048 g_hash_table_destroy(whole_cache);
1049 whole_cache = NULL;
1050 return;
1053 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1055 gchar *pref = (gchar *)key;
1057 PrefParam *param = (PrefParam *)user_data;
1059 prefs_config_parse_one_line(param, pref);
1062 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1063 const gchar *rcfile)
1065 GHashTable *sections_table = NULL;
1066 GHashTable *values_table = NULL;
1067 sections_table = g_hash_table_lookup(whole_cache, rcfile);
1069 if (sections_table == NULL) {
1070 g_warning("Can't find %s in the whole cache", rcfile?rcfile:"(null)");
1071 return FALSE;
1073 values_table = g_hash_table_lookup(sections_table, label);
1075 if (values_table == NULL) {
1076 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1077 return TRUE;
1079 g_hash_table_foreach(values_table, prefs_parse_cache, param);
1080 return TRUE;