5fbeb1fad26d531c04123e1320fda8e187950e55
[claws.git] / src / prefs_gtk.c
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
4  *
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.
9  *
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.
14  *
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/>.
17  *
18  */
19
20 #ifdef HAVE_CONFIG_H
21 #  include "config.h"
22 #include "claws-features.h"
23 #endif
24
25 #define _GNU_SOURCE
26 #include <stdio.h>
27
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>
35
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"
45
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)))
51
52 #ifdef HAVE_FGETS_UNLOCKED
53 #define SC_FGETS fgets_unlocked
54 #else
55 #define SC_FGETS fgets
56 #endif
57
58 typedef enum
59 {
60         DUMMY_PARAM
61 } DummyEnum;
62
63 static GHashTable *whole_cache = NULL;
64
65 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
66                                const gchar *rcfile);
67
68 static void prefs_config_parse_one_line(PrefParam       *param,
69                                          const gchar    *buf);
70
71 void prefs_read_config(PrefParam *param, const gchar *label,
72                        const gchar *rcfile, const gchar *encoding)
73 {
74         FILE *fp;
75         gchar buf[PREFSBUFSIZE];
76         gchar *block_label;
77
78         cm_return_if_fail(param != NULL);
79         cm_return_if_fail(label != NULL);
80         cm_return_if_fail(rcfile != NULL);
81
82         if (encoding != NULL)
83                 g_warning("Encoding is ignored");
84
85         debug_print("Reading configuration...\n");
86
87         prefs_set_default(param);
88
89         if (whole_cache != NULL) {
90                 if (prefs_read_config_from_cache(param, label, rcfile) == TRUE)
91                         return;
92         }
93
94         if ((fp = g_fopen(rcfile, "rb")) == NULL) {
95                 if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
96                 return;
97         }
98
99         block_label = g_strdup_printf("[%s]", label);
100
101 #ifdef HAVE_FGETS_UNLOCKED
102         flockfile(fp);
103 #endif
104
105         /* search aiming block */
106         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
107                 gint val;
108
109                 if (encoding) {
110                         gchar *conv_str;
111
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;
124                 }
125         }
126         g_free(block_label);
127
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;
133
134                 if (encoding) {
135                         gchar *conv_str;
136
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);
145         }
146
147         debug_print("Finished reading configuration.\n");
148 #ifdef HAVE_FGETS_UNLOCKED
149         funlockfile(fp);
150 #endif
151         fclose(fp);
152 }
153
154 static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
155 {
156         gint i;
157         gint name_len;
158         const gchar *value;
159         GdkColor color;
160
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); */
169
170                 switch (param[i].type) {
171                 case P_STRING:
172                 case P_PASSWORD:
173                 {
174                         gchar *tmp = NULL;
175
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);
183                                 }
184                         } else {
185                                 tmp = g_strdup("");
186                         }
187                         if (!tmp) {
188                                 g_warning("Failed to convert character set.");
189                                 tmp = g_strdup(value);
190                         }
191                         g_free(*((gchar **)param[i].data));
192                         *((gchar **)param[i].data) = tmp;
193                         break;
194                 }
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); 
215                         }
216                         else 
217                                 /* be compatible and accept ints */
218                                 *((gulong *)param[i].data) = strtoul(value, 0, 10); 
219                         break;
220                 default:
221                         break;
222                 }
223         }
224 }
225
226 #define TRY(func) \
227 if (!(func)) \
228 { \
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; \
235 } \
236
237 void prefs_write_config(PrefParam *param, const gchar *label,
238                         const gchar *rcfile)
239 {
240         FILE *orig_fp;
241         PrefFile *pfile;
242         gchar *rcpath;
243         gchar buf[PREFSBUFSIZE];
244         gchar *block_label = NULL;
245         gboolean block_matched = FALSE;
246
247         cm_return_if_fail(param != NULL);
248         cm_return_if_fail(label != NULL);
249         cm_return_if_fail(rcfile != NULL);
250
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");
254         }
255
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;
261         }
262
263         block_label = g_strdup_printf("[%s]", label);
264
265         /* search aiming block */
266         if (orig_fp) {
267                 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
268                         gint val;
269
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);
277                 }
278         }
279
280         TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
281
282         /* write all param data to file */
283         TRY(prefs_write_param(param, pfile->fp) == 0);
284
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;
293                         }
294                 }
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;
302                         }
303                         if (!in_dup_block)
304                                 TRY(fputs(buf, pfile->fp) != EOF);
305                 }
306         }
307
308         g_free(block_label);
309         block_label = NULL;
310
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);
315
316         debug_print("Configuration is saved.\n");
317 }
318
319 gint prefs_write_param(PrefParam *param, FILE *fp)
320 {
321         gint i;
322         gchar buf[PREFSBUFSIZE];
323
324         for (i = 0; param[i].name != NULL; i++) {
325                 switch (param[i].type) {
326                 case P_STRING:
327                 {
328                         gchar *tmp = NULL;
329
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));
339                                 }
340                         }
341
342                         g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
343                                    tmp ? tmp : "");
344
345                         g_free(tmp);
346                         break;
347                 }
348                 case P_PASSWORD:
349                         break;
350                 case P_INT:
351                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
352                                    *((gint *)param[i].data));
353                         break;
354                 case P_BOOL:
355                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
356                                    *((gboolean *)param[i].data));
357                         break;
358                 case P_ENUM:
359                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
360                                    *((DummyEnum *)param[i].data));
361                         break;
362                 case P_USHORT:
363                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
364                                    *((gushort *)param[i].data));
365                         break;
366                 case P_COLOR:
367                         g_snprintf(buf, sizeof buf,  "%s=#%6.6lx\n", param[i].name,
368                                    *((gulong *) param[i].data));
369                         break;
370                 default:
371                         /* unrecognized, fail */
372                         debug_print("Unrecognized parameter type\n");
373                         return -1;
374                 }
375
376                 if (buf[0] != '\0') {
377                         if (fputs(buf, fp) == EOF) {
378                                 perror("fputs");
379                                 return -1;
380                         }
381                 }
382         }
383
384         return 0;
385 }
386
387 void prefs_set_default(PrefParam *param)
388 {
389         gint i;
390         GdkColor color;
391
392         cm_return_if_fail(param != NULL);
393
394         for (i = 0; param[i].name != NULL; i++) {
395                 if (!param[i].data) continue;
396
397                 switch (param[i].type) {
398                 case P_STRING:
399                         g_free(*((gchar **)param[i].data));
400                         if (param[i].defval != NULL) {
401                                 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
402                                         const gchar *envstr;
403                                         gchar *tmp;
404
405                                         envstr = g_getenv(param[i].defval + 4);
406                                         tmp = envstr && *envstr ?
407                                                 conv_codeset_strdup(envstr,
408                                                                     conv_get_locale_charset_str(),
409                                                                     CS_INTERNAL)
410                                                 : g_strdup("");
411                                         if (!tmp) {
412                                                 g_warning("Failed to convert character set.");
413                                                 tmp = g_strdup(envstr);
414                                         }
415                                         *((gchar **)param[i].data) = tmp;
416                                 } else if (param[i].defval[0] == '~')
417                                         *((gchar **)param[i].data) =
418                                                 g_strconcat(get_home_dir(),
419                                                             param[i].defval + 1,
420                                                             NULL);
421                                 else 
422                                         *((gchar **)param[i].data) =
423                                                 g_strdup(param[i].defval);
424                         } else
425                                 *((gchar **)param[i].data) = NULL;
426                         break;
427                 case P_PASSWORD:
428                         g_free(*((gchar **)param[i].data));
429                         if (param[i].defval != NULL) {
430                                 if (param[i].defval[0] != '\0')
431                                         *((gchar **)param[i].data) =
432                                                 g_strdup(param[i].defval);
433                                 else
434                                         *((gchar **)param[i].data) = NULL;
435                         } else
436                                 *((gchar **)param[i].data) = NULL;
437                         break;
438                 case P_INT:
439                         if (param[i].defval != NULL)
440                                 *((gint *)param[i].data) =
441                                         (gint)atoi(param[i].defval);
442                         else
443                                 *((gint *)param[i].data) = 0;
444                         break;
445                 case P_BOOL:
446                         if (param[i].defval != NULL) {
447                                 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
448                                         *((gboolean *)param[i].data) = TRUE;
449                                 else
450                                         *((gboolean *)param[i].data) =
451                                                 atoi(param[i].defval) ? TRUE : FALSE;
452                         } else
453                                 *((gboolean *)param[i].data) = FALSE;
454                         break;
455                 case P_ENUM:
456                         if (param[i].defval != NULL)
457                                 *((DummyEnum*)param[i].data) =
458                                         (DummyEnum)atoi(param[i].defval);
459                         else
460                                 *((DummyEnum *)param[i].data) = 0;
461                         break;
462                 case P_USHORT:
463                         if (param[i].defval != NULL)
464                                 *((gushort *)param[i].data) =
465                                         (gushort)atoi(param[i].defval);
466                         else
467                                 *((gushort *)param[i].data) = 0;
468                         break;
469                 case P_COLOR:
470                         if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
471                                 *((gulong *)param[i].data) =
472                                         RGB_FROM_GDK_COLOR(color);
473                         else if (param[i].defval)
474                                 /* be compatible and accept ints */
475                                 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10); 
476                         else
477                                 *((gulong *)param[i].data) = 0; 
478                         break;
479                 default:
480                         break;
481                 }
482         }
483 }
484
485 void prefs_free(PrefParam *param)
486 {
487         gint i;
488
489         cm_return_if_fail(param != NULL);
490
491         for (i = 0; param[i].name != NULL; i++) {
492                 if (!param[i].data) continue;
493
494                 switch (param[i].type) {
495                 case P_STRING:
496                 case P_PASSWORD:
497                         g_free(*((gchar **)param[i].data));
498                         break;
499                 default:
500                         break;
501                 }
502         }
503 }
504
505 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
506 {
507         gboolean is_active;
508
509         is_active = gtk_toggle_button_get_active(toggle_btn);
510         gtk_widget_set_sensitive(widget, is_active);
511 }
512
513 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
514 {
515         gboolean is_active;
516
517         is_active = gtk_toggle_button_get_active(toggle_btn);
518         gtk_widget_set_sensitive(widget, !is_active);
519 }
520
521 void prefs_set_dialog(PrefParam *param)
522 {
523         gint i;
524
525         for (i = 0; param[i].name != NULL; i++) {
526                 if (param[i].widget_set_func)
527                         param[i].widget_set_func(&param[i]);
528         }
529 }
530
531 void prefs_set_data_from_dialog(PrefParam *param)
532 {
533         gint i;
534
535         for (i = 0; param[i].name != NULL; i++) {
536                 if (param[i].data_set_func)
537                         param[i].data_set_func(&param[i]);
538         }
539 }
540
541 void prefs_set_dialog_to_default(PrefParam *param)
542 {
543         gint       i;
544         PrefParam  tmpparam;
545         gchar     *str_data = NULL;
546         gint       int_data;
547         gushort    ushort_data;
548         gboolean   bool_data;
549         DummyEnum  enum_data;
550
551         for (i = 0; param[i].name != NULL; i++) {
552                 if (!param[i].widget_set_func) continue;
553
554                 tmpparam = param[i];
555
556                 switch (tmpparam.type) {
557                 case P_STRING:
558                         if (tmpparam.defval) {
559                                 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
560                                         str_data = g_strdup(g_getenv(param[i].defval + 4));
561                                         tmpparam.data = &str_data;
562                                         break;
563                                 } else if (tmpparam.defval[0] == '~') {
564                                         str_data =
565                                                 g_strconcat(get_home_dir(),
566                                                             param[i].defval + 1,
567                                                             NULL);
568                                         tmpparam.data = &str_data;
569                                         break;
570                                 }
571                         }
572                         tmpparam.data = &tmpparam.defval;
573                         break;
574                 case P_PASSWORD:
575                         tmpparam.data = &tmpparam.defval;
576                         break;
577                 case P_INT:
578                         if (tmpparam.defval)
579                                 int_data = atoi(tmpparam.defval);
580                         else
581                                 int_data = 0;
582                         tmpparam.data = &int_data;
583                         break;
584                 case P_USHORT:
585                         if (tmpparam.defval)
586                                 ushort_data = atoi(tmpparam.defval);
587                         else
588                                 ushort_data = 0;
589                         tmpparam.data = &ushort_data;
590                         break;
591                 case P_BOOL:
592                         if (tmpparam.defval) {
593                                 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
594                                         bool_data = TRUE;
595                                 else
596                                         bool_data = atoi(tmpparam.defval)
597                                                 ? TRUE : FALSE;
598                         } else
599                                 bool_data = FALSE;
600                         tmpparam.data = &bool_data;
601                         break;
602                 case P_ENUM:
603                         if (tmpparam.defval)
604                                 enum_data = (DummyEnum)atoi(tmpparam.defval);
605                         else
606                                 enum_data = 0;
607                         tmpparam.data = &enum_data;
608                         break;
609                 case P_OTHER:
610                 default:
611                         break;
612                 }
613                 tmpparam.widget_set_func(&tmpparam);
614                 g_free(str_data);
615                 str_data = NULL;
616         }
617 }
618
619 void prefs_set_data_from_entry(PrefParam *pparam)
620 {
621         gchar **str;
622         const gchar *entry_str;
623
624         cm_return_if_fail(*pparam->widget != NULL);
625
626         entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
627
628         switch (pparam->type) {
629         case P_STRING:
630                 str = (gchar **)pparam->data;
631                 g_free(*str);
632                 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
633                 break;
634         case P_PASSWORD:
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);
645         }
646 }
647
648 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
649 {
650         gchar **str;
651
652         cm_return_if_fail(*pparam->widget != NULL);
653
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);
663         }
664 }
665
666 void prefs_set_entry(PrefParam *pparam)
667 {
668         gchar **str;
669         cm_return_if_fail(*pparam->widget != NULL);
670
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);
688         }
689 }
690
691 void prefs_set_entry_from_escaped(PrefParam *pparam)
692 {
693         gchar **str;
694
695         cm_return_if_fail(*pparam->widget != NULL);
696
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);
706         }
707 }
708
709 void prefs_set_data_from_text(PrefParam *pparam)
710 {
711         gchar **str;
712         gchar *text = NULL, *tp = NULL;
713         gchar *tmp, *tmpp;
714
715         cm_return_if_fail(*pparam->widget != NULL);
716
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);
731                 }
732
733                 cm_return_if_fail (tp && text);
734
735                 if (text[0] == '\0') {
736                         *str = NULL;
737                         g_free(text);
738                         break;
739                 }
740
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++;
750                 }
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);
758         }
759 }
760
761 void prefs_set_escaped_data_from_text(PrefParam *pparam)
762 {
763         gchar **str;
764
765         cm_return_if_fail(*pparam->widget != NULL);
766
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);
776         }
777 }
778
779 void prefs_set_text(PrefParam *pparam)
780 {
781         gchar *buf, *sp, *bufp;
782         gchar **str;
783         GtkTextView *text;
784         GtkTextBuffer *buffer;
785         GtkTextIter iter;
786
787         cm_return_if_fail(*pparam->widget != NULL);
788
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++;
803                                 }
804                                 *bufp = '\0';
805                         }
806                 } else
807                         buf = "";
808
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);
818         }
819 }
820
821 void prefs_set_text_from_escaped(PrefParam *pparam)
822 {
823         gchar **str;
824
825         cm_return_if_fail(*pparam->widget != NULL);
826
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);
836         }
837 }
838
839 void prefs_set_data_from_toggle(PrefParam *pparam)
840 {
841         cm_return_if_fail(pparam->type == P_BOOL);
842         cm_return_if_fail(*pparam->widget != NULL);
843         
844         *((gboolean *)pparam->data) =
845                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
846 }
847
848 void prefs_set_toggle(PrefParam *pparam)
849 {
850         cm_return_if_fail(pparam->type == P_BOOL);
851         cm_return_if_fail(*pparam->widget != NULL);
852
853         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
854                                      *((gboolean *)pparam->data));
855 }
856
857 void prefs_set_data_from_spinbtn(PrefParam *pparam)
858 {
859         cm_return_if_fail(*pparam->widget != NULL);
860
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);
875         }
876 }
877
878 void prefs_set_spinbtn(PrefParam *pparam)
879 {
880         cm_return_if_fail(*pparam->widget != NULL);
881
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);
894         }
895 }
896
897 static GSList *prefs_pages = NULL;
898
899 static void prefs_gtk_window_closed_cb(PrefsWindow *prefswindow)
900 {
901         if (prefswindow == NULL)
902                 return;
903
904         if (prefswindow->dialog_response > PREFSWINDOW_RESPONSE_CANCEL)
905                 prefs_common_write_config();
906 }
907
908 void prefs_gtk_open(void)
909 {
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);
913 }
914
915 void prefs_gtk_register_page(PrefsPage *page)
916 {
917         prefs_pages = g_slist_append(prefs_pages, page);
918 }
919
920 void prefs_gtk_unregister_page(PrefsPage *page)
921 {
922         prefs_pages = g_slist_remove(prefs_pages, page);
923 }
924
925 static void prefs_destroy_whole_cache(gpointer to_free)
926 {       
927         GHashTable *table = (GHashTable *)to_free;
928         g_hash_table_destroy(table);
929 }
930
931 static void prefs_destroy_file_cache(gpointer to_free)
932 {       
933         GHashTable *table = (GHashTable *)to_free;
934         g_hash_table_destroy(table);
935 }
936
937 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
938 {
939         FILE *fp = NULL;
940         gchar buf[PREFSBUFSIZE];
941         GHashTable *section_cache = NULL;
942
943         if (rcfile)
944                 fp = g_fopen(rcfile, "rb");
945         if (!fp) {
946                 debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
947                 return -1;
948         }
949         
950 #ifdef HAVE_FGETS_UNLOCKED
951         flockfile(fp);
952 #endif
953         
954         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
955                 strretchomp(buf);
956                 if (buf[0] == '\0')
957                         continue;
958                 if (buf[0] == '#')
959                         continue; /* comment */
960                 if (buf[0] == '[') { /* new section */
961                         gchar *blockname = g_strdup(buf+1);
962
963                         if (strrchr(blockname, ']'))
964                                 *strrchr(blockname, ']') = '\0';
965
966                         if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
967                                 debug_print("new section '%s'\n", blockname);
968                                 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
969                                                 g_free, NULL);
970                                 g_hash_table_insert(file_cache, 
971                                         blockname, section_cache);
972                         } else {
973                                 debug_print("section '%s' already done\n", blockname);
974                                 g_free(blockname);
975                                 section_cache = NULL;
976                                 continue;
977                         }
978                 } else {
979                         if (!section_cache) {
980                                 debug_print("skipping stuff %s with no section\n", buf);
981                                 continue;
982                         } else {
983                                 gchar *pref;
984                                 
985                                 if (!strchr(buf, '=')) {
986                                         /* plugins do differently */
987                                         continue;
988                                 }
989                                 pref = g_strdup(buf);
990                                 
991                                 //debug_print("new pref '%s'\n", pref);
992                                 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
993                         }
994                 }
995         }
996 #ifdef HAVE_FGETS_UNLOCKED
997         funlockfile(fp);
998 #endif
999         fclose(fp);
1000         return 0;
1001 }
1002
1003 static int prefs_cache(const gchar *rcfile)
1004 {
1005         GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal, 
1006                                         g_free, prefs_destroy_file_cache);
1007         
1008         debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
1009         g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1010         
1011         return prefs_cache_sections(file_cache, rcfile);
1012 }
1013
1014 void prefs_prepare_cache(void)
1015 {
1016         gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1017         gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1018         gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1019         
1020         if (whole_cache == NULL) {
1021                 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1022                                 g_free, prefs_destroy_whole_cache);
1023         } else {
1024                 debug_print("already cached\n");
1025                 g_free(clawsrc);
1026                 g_free(folderitemrc);
1027                 g_free(accountrc);
1028                 return;
1029         }
1030         if (prefs_cache(clawsrc) < 0 ||
1031             prefs_cache(folderitemrc) < 0 ||
1032             prefs_cache(accountrc) < 0)
1033                 prefs_destroy_cache();
1034
1035         g_free(clawsrc);
1036         g_free(folderitemrc);
1037         g_free(accountrc);
1038 }
1039
1040 void prefs_destroy_cache(void)
1041 {
1042         if (!whole_cache) {
1043                 debug_print("no cache\n");
1044                 return;
1045         }
1046         debug_print("destroying cache\n");
1047         g_hash_table_destroy(whole_cache);
1048         whole_cache = NULL;
1049         return;
1050 }
1051
1052 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1053 {
1054         gchar *pref = (gchar *)key;
1055
1056         PrefParam *param = (PrefParam *)user_data;
1057         
1058         prefs_config_parse_one_line(param, pref);
1059 }
1060
1061 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1062                                const gchar *rcfile) 
1063 {
1064         GHashTable *sections_table = NULL;
1065         GHashTable *values_table = NULL;
1066         sections_table = g_hash_table_lookup(whole_cache, rcfile);
1067         
1068         if (sections_table == NULL) {
1069                 g_warning("Can't find %s in the whole cache", rcfile?rcfile:"(null)");
1070                 return FALSE;
1071         }
1072         values_table = g_hash_table_lookup(sections_table, label);
1073         
1074         if (values_table == NULL) {
1075                 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1076                 return TRUE;
1077         }
1078         g_hash_table_foreach(values_table, prefs_parse_cache, param);
1079         return TRUE;
1080 }