ba1eb541219fdbc9abbd4fc1941c79d61ec477c6
[claws.git] / src / prefs_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2012 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 "passcrypt.h"
44 #include "base64.h"
45 #include "codeconv.h"
46
47 #define CL(x)   (((gulong) (x) >> (gulong) 8) & 0xFFUL)
48 #define RGB_FROM_GDK_COLOR(c) \
49         ((CL(c.red)   << (gulong) 16) | \
50          (CL(c.green) << (gulong)  8) | \
51          (CL(c.blue)))
52
53 #ifdef HAVE_FGETS_UNLOCKED
54 #define SC_FGETS fgets_unlocked
55 #else
56 #define SC_FGETS fgets
57 #endif
58
59 typedef enum
60 {
61         DUMMY_PARAM
62 } DummyEnum;
63
64 static GHashTable *whole_cache = NULL;
65
66 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
67                                const gchar *rcfile);
68
69 static void prefs_config_parse_one_line(PrefParam       *param,
70                                          const gchar    *buf);
71
72 void prefs_read_config(PrefParam *param, const gchar *label,
73                        const gchar *rcfile, const gchar *encoding)
74 {
75         FILE *fp;
76         gchar buf[PREFSBUFSIZE];
77         gchar *block_label;
78
79         cm_return_if_fail(param != NULL);
80         cm_return_if_fail(label != NULL);
81         cm_return_if_fail(rcfile != NULL);
82
83         if (encoding != NULL)
84                 g_warning("Encoding is ignored\n");
85
86         debug_print("Reading configuration...\n");
87
88         prefs_set_default(param);
89
90         if (whole_cache != NULL) {
91                 if (prefs_read_config_from_cache(param, label, rcfile) == TRUE)
92                         return;
93         }
94
95         if ((fp = g_fopen(rcfile, "rb")) == NULL) {
96                 if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
97                 return;
98         }
99
100         block_label = g_strdup_printf("[%s]", label);
101
102 #ifdef HAVE_FGETS_UNLOCKED
103         flockfile(fp);
104 #endif
105
106         /* search aiming block */
107         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
108                 gint val;
109
110                 if (encoding) {
111                         gchar *conv_str;
112
113                         conv_str = conv_codeset_strdup
114                                 (buf, encoding, CS_INTERNAL);
115                         if (!conv_str)
116                                 conv_str = g_strdup(buf);
117                         val = strncmp
118                                 (conv_str, block_label, strlen(block_label));
119                         g_free(conv_str);
120                 } else
121                         val = strncmp(buf, block_label, strlen(block_label));
122                 if (val == 0) {
123                         debug_print("Found %s\n", block_label);
124                         break;
125                 }
126         }
127         g_free(block_label);
128
129         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
130                 strretchomp(buf);
131                 /* reached next block */
132                 if (buf[0] == '[') break;
133                 if (buf[0] == '#') continue;
134
135                 if (encoding) {
136                         gchar *conv_str;
137
138                         conv_str = conv_codeset_strdup
139                                 (buf, encoding, CS_INTERNAL);
140                         if (!conv_str)
141                                 conv_str = g_strdup(buf);
142                         prefs_config_parse_one_line(param, conv_str);
143                         g_free(conv_str);
144                 } else
145                         prefs_config_parse_one_line(param, buf);
146         }
147
148         debug_print("Finished reading configuration.\n");
149 #ifdef HAVE_FGETS_UNLOCKED
150         funlockfile(fp);
151 #endif
152         fclose(fp);
153 }
154
155 static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
156 {
157         gint i;
158         gint name_len;
159         const gchar *value;
160         GdkColor color;
161
162         for (i = 0; param[i].name != NULL; i++) {
163                 name_len = strlen(param[i].name);
164                 if (g_ascii_strncasecmp(buf, param[i].name, name_len))
165                         continue;
166                 if (buf[name_len] != '=')
167                         continue;
168                 value = buf + name_len + 1;
169                 /* debug_print("%s = %s\n", param[i].name, value); */
170
171                 switch (param[i].type) {
172                 case P_STRING:
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                 case P_PASSWORD:
221                         g_free(*((gchar **)param[i].data));
222                         if (value[0] == '!') {
223                                 gchar tmp[1024];
224                                 gint len;
225
226                                 len = base64_decode(tmp, &value[1], strlen(value) - 1);
227                                 passcrypt_decrypt(tmp, len);
228                                 tmp[len] = '\0';
229                                 *((gchar **)param[i].data) =
230                                         *tmp ? g_strdup(tmp) : NULL;
231                         } else {
232                                 *((gchar **)param[i].data) =
233                                         *value ? g_strdup(value) : NULL;
234                         }
235                         break;
236                 default:
237                         break;
238                 }
239         }
240 }
241
242 #define TRY(func) \
243 if (!(func)) \
244 { \
245         g_warning("Failed to write configuration to file\n"); \
246         if (orig_fp) fclose(orig_fp); \
247         prefs_file_close_revert(pfile); \
248         g_free(rcpath); \
249         g_free(block_label); \
250         return; \
251 } \
252
253 void prefs_write_config(PrefParam *param, const gchar *label,
254                         const gchar *rcfile)
255 {
256         FILE *orig_fp;
257         PrefFile *pfile;
258         gchar *rcpath;
259         gchar buf[PREFSBUFSIZE];
260         gchar *block_label = NULL;
261         gboolean block_matched = FALSE;
262
263         cm_return_if_fail(param != NULL);
264         cm_return_if_fail(label != NULL);
265         cm_return_if_fail(rcfile != NULL);
266
267         rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
268         if ((orig_fp = g_fopen(rcpath, "rb")) == NULL) {
269                 if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
270         }
271
272         if ((pfile = prefs_write_open(rcpath)) == NULL) {
273                 g_warning("Failed to write configuration to file\n");
274                 if (orig_fp) fclose(orig_fp);
275                 g_free(rcpath);
276                 return;
277         }
278
279         block_label = g_strdup_printf("[%s]", label);
280
281         /* search aiming block */
282         if (orig_fp) {
283                 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
284                         gint val;
285
286                         val = strncmp(buf, block_label, strlen(block_label));
287                         if (val == 0) {
288                                 debug_print("Found %s\n", block_label);
289                                 block_matched = TRUE;
290                                 break;
291                         } else
292                                 TRY(fputs(buf, pfile->fp) != EOF);
293                 }
294         }
295
296         TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
297
298         /* write all param data to file */
299         TRY(prefs_write_param(param, pfile->fp) == 0);
300
301         if (block_matched) {
302                 gboolean in_dup_block = FALSE;
303                 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
304                         /* next block */
305                         if (buf[0] == '[') {
306                                 TRY(fputc('\n', pfile->fp) != EOF &&
307                                     fputs(buf, pfile->fp)  != EOF);
308                                 break;
309                         }
310                 }
311                 while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
312                         if (buf[0] == '[') {
313                                 if (!strncmp(buf, block_label,
314                                                 strlen(block_label)))
315                                         in_dup_block = TRUE;
316                                 else
317                                         in_dup_block = FALSE;
318                         }
319                         if (!in_dup_block)
320                                 TRY(fputs(buf, pfile->fp) != EOF);
321                 }
322         }
323
324         g_free(block_label);
325         block_label = NULL;
326
327         if (orig_fp) fclose(orig_fp);
328         if (prefs_file_close(pfile) < 0)
329                 g_warning("Failed to write configuration to file\n");
330         g_free(rcpath);
331
332         debug_print("Configuration is saved.\n");
333 }
334
335 gint prefs_write_param(PrefParam *param, FILE *fp)
336 {
337         gint i;
338         gchar buf[PREFSBUFSIZE];
339
340         for (i = 0; param[i].name != NULL; i++) {
341                 switch (param[i].type) {
342                 case P_STRING:
343                 {
344                         gchar *tmp = NULL;
345
346                         if (*((gchar **)param[i].data)) {
347                                 if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
348                                         tmp = g_strdup(*((gchar **)param[i].data));
349                                 else {
350                                         tmp = conv_codeset_strdup(*((gchar **)param[i].data),
351                                                 conv_get_locale_charset_str_no_utf8(),
352                                                 CS_INTERNAL);
353                                         if (!tmp)
354                                                 tmp = g_strdup(*((gchar **)param[i].data));
355                                 }
356                         }
357
358                         g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
359                                    tmp ? tmp : "");
360
361                         g_free(tmp);
362                         break;
363                 }
364                 case P_INT:
365                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
366                                    *((gint *)param[i].data));
367                         break;
368                 case P_BOOL:
369                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
370                                    *((gboolean *)param[i].data));
371                         break;
372                 case P_ENUM:
373                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
374                                    *((DummyEnum *)param[i].data));
375                         break;
376                 case P_USHORT:
377                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
378                                    *((gushort *)param[i].data));
379                         break;
380                 case P_COLOR:
381                         g_snprintf(buf, sizeof buf,  "%s=#%6.6lx\n", param[i].name,
382                                    *((gulong *) param[i].data));
383                         break;
384                 case P_PASSWORD:
385                         {
386                                 gchar *tmp = NULL, tmp2[1024] = {0};
387
388                                 tmp = *((gchar **)param[i].data);
389                                 if (tmp) {
390                                         gint len;
391
392                                         tmp = g_strdup(tmp);
393                                         len = strlen(tmp);
394                                         passcrypt_encrypt(tmp, len);
395                                         base64_encode(tmp2, tmp, len);
396                                         g_free(tmp);
397                                         tmp = tmp2;
398                                 }
399                                 g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
400                                            tmp ?
401                                            tmp : "");
402                         }
403                         break;
404                 default:
405                         /* unrecognized, fail */
406                         debug_print("Unrecognized parameter type\n");
407                         return -1;
408                 }
409
410                 if (buf[0] != '\0') {
411                         if (fputs(buf, fp) == EOF) {
412                                 perror("fputs");
413                                 return -1;
414                         }
415                 }
416         }
417
418         return 0;
419 }
420
421 void prefs_set_default(PrefParam *param)
422 {
423         gint i;
424         GdkColor color;
425
426         cm_return_if_fail(param != NULL);
427
428         for (i = 0; param[i].name != NULL; i++) {
429                 if (!param[i].data) continue;
430
431                 switch (param[i].type) {
432                 case P_STRING:
433                         g_free(*((gchar **)param[i].data));
434                         if (param[i].defval != NULL) {
435                                 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
436                                         const gchar *envstr;
437                                         gchar *tmp;
438
439                                         envstr = g_getenv(param[i].defval + 4);
440                                         tmp = envstr && *envstr ?
441                                                 conv_codeset_strdup(envstr,
442                                                                     conv_get_locale_charset_str(),
443                                                                     CS_INTERNAL)
444                                                 : g_strdup("");
445                                         if (!tmp) {
446                                                 g_warning("Failed to convert character set.");
447                                                 tmp = g_strdup(envstr);
448                                         }
449                                         *((gchar **)param[i].data) = tmp;
450                                 } else if (param[i].defval[0] == '~')
451                                         *((gchar **)param[i].data) =
452                                                 g_strconcat(get_home_dir(),
453                                                             param[i].defval + 1,
454                                                             NULL);
455                                 else if (param[i].defval[0] != '\0')
456                                         *((gchar **)param[i].data) =
457                                                 g_strdup(param[i].defval);
458                                 else
459                                         *((gchar **)param[i].data) = NULL;
460                         } else
461                                 *((gchar **)param[i].data) = NULL;
462                         break;
463                 case P_PASSWORD:
464                         g_free(*((gchar **)param[i].data));
465                         if (param[i].defval != NULL) {
466                                 if (param[i].defval[0] != '\0')
467                                         *((gchar **)param[i].data) =
468                                                 g_strdup(param[i].defval);
469                                 else
470                                         *((gchar **)param[i].data) = NULL;
471                         } else
472                                 *((gchar **)param[i].data) = NULL;
473                         break;
474                 case P_INT:
475                         if (param[i].defval != NULL)
476                                 *((gint *)param[i].data) =
477                                         (gint)atoi(param[i].defval);
478                         else
479                                 *((gint *)param[i].data) = 0;
480                         break;
481                 case P_BOOL:
482                         if (param[i].defval != NULL) {
483                                 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
484                                         *((gboolean *)param[i].data) = TRUE;
485                                 else
486                                         *((gboolean *)param[i].data) =
487                                                 atoi(param[i].defval) ? TRUE : FALSE;
488                         } else
489                                 *((gboolean *)param[i].data) = FALSE;
490                         break;
491                 case P_ENUM:
492                         if (param[i].defval != NULL)
493                                 *((DummyEnum*)param[i].data) =
494                                         (DummyEnum)atoi(param[i].defval);
495                         else
496                                 *((DummyEnum *)param[i].data) = 0;
497                         break;
498                 case P_USHORT:
499                         if (param[i].defval != NULL)
500                                 *((gushort *)param[i].data) =
501                                         (gushort)atoi(param[i].defval);
502                         else
503                                 *((gushort *)param[i].data) = 0;
504                         break;
505                 case P_COLOR:
506                         if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
507                                 *((gulong *)param[i].data) =
508                                         RGB_FROM_GDK_COLOR(color);
509                         else if (param[i].defval)
510                                 /* be compatible and accept ints */
511                                 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10); 
512                         else
513                                 *((gulong *)param[i].data) = 0; 
514                         break;
515                 default:
516                         break;
517                 }
518         }
519 }
520
521 void prefs_free(PrefParam *param)
522 {
523         gint i;
524
525         cm_return_if_fail(param != NULL);
526
527         for (i = 0; param[i].name != NULL; i++) {
528                 if (!param[i].data) continue;
529
530                 switch (param[i].type) {
531                 case P_STRING:
532                 case P_PASSWORD:
533                         g_free(*((gchar **)param[i].data));
534                         break;
535                 default:
536                         break;
537                 }
538         }
539 }
540
541 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
542 {
543         gboolean is_active;
544
545         is_active = gtk_toggle_button_get_active(toggle_btn);
546         gtk_widget_set_sensitive(widget, is_active);
547 }
548
549 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
550 {
551         gboolean is_active;
552
553         is_active = gtk_toggle_button_get_active(toggle_btn);
554         gtk_widget_set_sensitive(widget, !is_active);
555 }
556
557 void prefs_set_dialog(PrefParam *param)
558 {
559         gint i;
560
561         for (i = 0; param[i].name != NULL; i++) {
562                 if (param[i].widget_set_func)
563                         param[i].widget_set_func(&param[i]);
564         }
565 }
566
567 void prefs_set_data_from_dialog(PrefParam *param)
568 {
569         gint i;
570
571         for (i = 0; param[i].name != NULL; i++) {
572                 if (param[i].data_set_func)
573                         param[i].data_set_func(&param[i]);
574         }
575 }
576
577 void prefs_set_dialog_to_default(PrefParam *param)
578 {
579         gint       i;
580         PrefParam  tmpparam;
581         gchar     *str_data = NULL;
582         gint       int_data;
583         gushort    ushort_data;
584         gboolean   bool_data;
585         DummyEnum  enum_data;
586
587         for (i = 0; param[i].name != NULL; i++) {
588                 if (!param[i].widget_set_func) continue;
589
590                 tmpparam = param[i];
591
592                 switch (tmpparam.type) {
593                 case P_STRING:
594                         if (tmpparam.defval) {
595                                 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
596                                         str_data = g_strdup(g_getenv(param[i].defval + 4));
597                                         tmpparam.data = &str_data;
598                                         break;
599                                 } else if (tmpparam.defval[0] == '~') {
600                                         str_data =
601                                                 g_strconcat(get_home_dir(),
602                                                             param[i].defval + 1,
603                                                             NULL);
604                                         tmpparam.data = &str_data;
605                                         break;
606                                 }
607                         }
608                         tmpparam.data = &tmpparam.defval;
609                         break;
610                 case P_PASSWORD:
611                         tmpparam.data = &tmpparam.defval;
612                         break;
613                 case P_INT:
614                         if (tmpparam.defval)
615                                 int_data = atoi(tmpparam.defval);
616                         else
617                                 int_data = 0;
618                         tmpparam.data = &int_data;
619                         break;
620                 case P_USHORT:
621                         if (tmpparam.defval)
622                                 ushort_data = atoi(tmpparam.defval);
623                         else
624                                 ushort_data = 0;
625                         tmpparam.data = &ushort_data;
626                         break;
627                 case P_BOOL:
628                         if (tmpparam.defval) {
629                                 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
630                                         bool_data = TRUE;
631                                 else
632                                         bool_data = atoi(tmpparam.defval)
633                                                 ? TRUE : FALSE;
634                         } else
635                                 bool_data = FALSE;
636                         tmpparam.data = &bool_data;
637                         break;
638                 case P_ENUM:
639                         if (tmpparam.defval)
640                                 enum_data = (DummyEnum)atoi(tmpparam.defval);
641                         else
642                                 enum_data = 0;
643                         tmpparam.data = &enum_data;
644                         break;
645                 case P_OTHER:
646                 default:
647                         break;
648                 }
649                 tmpparam.widget_set_func(&tmpparam);
650                 g_free(str_data);
651                 str_data = NULL;
652         }
653 }
654
655 void prefs_set_data_from_entry(PrefParam *pparam)
656 {
657         gchar **str;
658         const gchar *entry_str;
659
660         cm_return_if_fail(*pparam->widget != NULL);
661
662         entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
663
664         switch (pparam->type) {
665         case P_STRING:
666         case P_PASSWORD:
667                 str = (gchar **)pparam->data;
668                 g_free(*str);
669                 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
670                 break;
671         case P_USHORT:
672                 *((gushort *)pparam->data) = atoi(entry_str);
673                 break;
674         case P_INT:
675                 *((gint *)pparam->data) = atoi(entry_str);
676                 break;
677         default:
678                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
679                           pparam->type);
680         }
681 }
682
683 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
684 {
685         gchar **str;
686
687         cm_return_if_fail(*pparam->widget != NULL);
688
689         switch (pparam->type) {
690         case P_STRING:
691                 str = (gchar **)pparam->data;
692                 g_free(*str);
693                 *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
694                 break;
695         default:
696                 g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
697                           pparam->type);
698         }
699 }
700
701 void prefs_set_entry(PrefParam *pparam)
702 {
703         gchar **str;
704
705         cm_return_if_fail(*pparam->widget != NULL);
706
707         switch (pparam->type) {
708         case P_STRING:
709         case P_PASSWORD:
710                 str = (gchar **)pparam->data;
711                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
712                                    *str ? *str : "");
713                 break;
714         case P_INT:
715                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
716                                    itos(*((gint *)pparam->data)));
717                 break;
718         case P_USHORT:
719                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
720                                    itos(*((gushort *)pparam->data)));
721                 break;
722         default:
723                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
724                           pparam->type);
725         }
726 }
727
728 void prefs_set_entry_from_escaped(PrefParam *pparam)
729 {
730         gchar **str;
731
732         cm_return_if_fail(*pparam->widget != NULL);
733
734         switch (pparam->type) {
735         case P_STRING:
736                 str = (gchar **)pparam->data;
737                 pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
738                                    *str ? *str : "");
739                 break;
740         default:
741                 g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
742                           pparam->type);
743         }
744 }
745
746 void prefs_set_data_from_text(PrefParam *pparam)
747 {
748         gchar **str;
749         gchar *text = NULL, *tp = NULL;
750         gchar *tmp, *tmpp;
751
752         cm_return_if_fail(*pparam->widget != NULL);
753
754         switch (pparam->type) {
755         case P_STRING:
756         case P_PASSWORD:
757                 str = (gchar **)pparam->data;
758                 g_free(*str);
759                 if (GTK_IS_EDITABLE(*pparam->widget)) {   /* need? */
760                         tp = text = gtk_editable_get_chars
761                                         (GTK_EDITABLE(*pparam->widget), 0, -1);
762                 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
763                         GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
764                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
765                         GtkTextIter start, end;
766                         gtk_text_buffer_get_start_iter(buffer, &start);
767                         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
768                         tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
769                 }
770
771                 cm_return_if_fail (tp && text);
772
773                 if (text[0] == '\0') {
774                         *str = NULL;
775                         g_free(text);
776                         break;
777                 }
778
779                 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
780                         { *str = NULL; break; });
781                 while (*tp) {
782                         if (*tp == '\n') {
783                                 *tmpp++ = '\\';
784                                 *tmpp++ = 'n';
785                                 tp++;
786                         } else
787                                 *tmpp++ = *tp++;
788                 }
789                 *tmpp = '\0';
790                 *str = g_strdup(tmp);
791                 g_free(text);
792                 break;
793         default:
794                 g_warning("Invalid PrefType for GtkText widget: %d\n",
795                           pparam->type);
796         }
797 }
798
799 void prefs_set_escaped_data_from_text(PrefParam *pparam)
800 {
801         gchar **str;
802
803         cm_return_if_fail(*pparam->widget != NULL);
804
805         switch (pparam->type) {
806         case P_STRING:
807                 str = (gchar **)pparam->data;
808                 g_free(*str);
809                 *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
810                 break;
811         default:
812                 g_warning("Invalid escaped PrefType for GtkText widget: %d\n",
813                           pparam->type);
814         }
815 }
816
817 void prefs_set_text(PrefParam *pparam)
818 {
819         gchar *buf, *sp, *bufp;
820         gchar **str;
821         GtkTextView *text;
822         GtkTextBuffer *buffer;
823         GtkTextIter iter;
824
825         cm_return_if_fail(*pparam->widget != NULL);
826
827         switch (pparam->type) {
828         case P_STRING:
829         case P_PASSWORD:
830                 str = (gchar **)pparam->data;
831                 if (*str) {
832                         bufp = buf = alloca(strlen(*str) + 1);
833                         if (!buf) buf = "";
834                         else {
835                                 sp = *str;
836                                 while (*sp) {
837                                         if (*sp == '\\' && *(sp + 1) == 'n') {
838                                                 *bufp++ = '\n';
839                                                 sp += 2;
840                                         } else
841                                                 *bufp++ = *sp++;
842                                 }
843                                 *bufp = '\0';
844                         }
845                 } else
846                         buf = "";
847
848                 text = GTK_TEXT_VIEW(*pparam->widget);
849                 buffer = gtk_text_view_get_buffer(text);
850                 gtk_text_buffer_set_text(buffer, "", -1);
851                 gtk_text_buffer_get_start_iter(buffer, &iter);
852                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
853                 break;
854         default:
855                 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
856                           pparam->type);
857         }
858 }
859
860 void prefs_set_text_from_escaped(PrefParam *pparam)
861 {
862         gchar **str;
863
864         cm_return_if_fail(*pparam->widget != NULL);
865
866         switch (pparam->type) {
867         case P_STRING:
868                 str = (gchar **)pparam->data;
869                 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
870                                  *str ? *str : "");
871                 break;
872         default:
873                 g_warning("Invalid escaped PrefType for GtkTextView widget: %d\n",
874                           pparam->type);
875         }
876 }
877
878 void prefs_set_data_from_toggle(PrefParam *pparam)
879 {
880         cm_return_if_fail(pparam->type == P_BOOL);
881         cm_return_if_fail(*pparam->widget != NULL);
882         
883         *((gboolean *)pparam->data) =
884                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
885 }
886
887 void prefs_set_toggle(PrefParam *pparam)
888 {
889         cm_return_if_fail(pparam->type == P_BOOL);
890         cm_return_if_fail(*pparam->widget != NULL);
891
892         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
893                                      *((gboolean *)pparam->data));
894 }
895
896 void prefs_set_data_from_spinbtn(PrefParam *pparam)
897 {
898         cm_return_if_fail(*pparam->widget != NULL);
899
900         switch (pparam->type) {
901         case P_INT:
902                 *((gint *)pparam->data) =
903                         gtk_spin_button_get_value_as_int
904                         (GTK_SPIN_BUTTON(*pparam->widget));
905                 break;
906         case P_USHORT:
907                 *((gushort *)pparam->data) =
908                         (gushort)gtk_spin_button_get_value_as_int
909                         (GTK_SPIN_BUTTON(*pparam->widget));
910                 break;
911         default:
912                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
913                           pparam->type);
914         }
915 }
916
917 void prefs_set_spinbtn(PrefParam *pparam)
918 {
919         cm_return_if_fail(*pparam->widget != NULL);
920
921         switch (pparam->type) {
922         case P_INT:
923                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
924                                           (gfloat)*((gint *)pparam->data));
925                 break;
926         case P_USHORT:
927                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
928                                           (gfloat)*((gushort *)pparam->data));
929                 break;
930         default:
931                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
932                           pparam->type);
933         }
934 }
935
936 static GSList *prefs_pages = NULL;
937
938 void prefs_gtk_open(void)
939 {
940         prefswindow_open(_("Preferences"), prefs_pages, NULL,
941                         &prefs_common.prefswin_width, &prefs_common.prefswin_height,
942                         NULL, NULL);
943 }
944
945 void prefs_gtk_register_page(PrefsPage *page)
946 {
947         prefs_pages = g_slist_append(prefs_pages, page);
948 }
949
950 void prefs_gtk_unregister_page(PrefsPage *page)
951 {
952         prefs_pages = g_slist_remove(prefs_pages, page);
953 }
954
955 static void prefs_destroy_whole_cache(gpointer to_free)
956 {       
957         GHashTable *table = (GHashTable *)to_free;
958         g_hash_table_destroy(table);
959 }
960
961 static void prefs_destroy_file_cache(gpointer to_free)
962 {       
963         GHashTable *table = (GHashTable *)to_free;
964         g_hash_table_destroy(table);
965 }
966
967 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
968 {
969         FILE *fp = NULL;
970         gchar buf[PREFSBUFSIZE];
971         GHashTable *section_cache = NULL;
972
973         if (rcfile)
974                 fp = g_fopen(rcfile, "rb");
975         if (!fp) {
976                 debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", strerror(errno));
977                 return -1;
978         }
979         
980 #ifdef HAVE_FGETS_UNLOCKED
981         flockfile(fp);
982 #endif
983         
984         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
985                 strretchomp(buf);
986                 if (buf[0] == '\0')
987                         continue;
988                 if (buf[0] == '#')
989                         continue; /* comment */
990                 if (buf[0] == '[') { /* new section */
991                         gchar *blockname = g_strdup(buf+1);
992
993                         if (strrchr(blockname, ']'))
994                                 *strrchr(blockname, ']') = '\0';
995
996                         if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
997                                 debug_print("new section '%s'\n", blockname);
998                                 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
999                                                 g_free, NULL);
1000                                 g_hash_table_insert(file_cache, 
1001                                         blockname, section_cache);
1002                         } else {
1003                                 debug_print("section '%s' already done\n", blockname);
1004                                 g_free(blockname);
1005                                 section_cache = NULL;
1006                                 continue;
1007                         }
1008                 } else {
1009                         if (!section_cache) {
1010                                 debug_print("skipping stuff %s with no section\n", buf);
1011                                 continue;
1012                         } else {
1013                                 gchar *pref;
1014                                 
1015                                 if (!strchr(buf, '=')) {
1016                                         /* plugins do differently */
1017                                         continue;
1018                                 }
1019                                 pref = g_strdup(buf);
1020                                 
1021                                 //debug_print("new pref '%s'\n", pref);
1022                                 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
1023                         }
1024                 }
1025         }
1026 #ifdef HAVE_FGETS_UNLOCKED
1027         funlockfile(fp);
1028 #endif
1029         fclose(fp);
1030         return 0;
1031 }
1032
1033 static int prefs_cache(const gchar *rcfile)
1034 {
1035         GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal, 
1036                                         g_free, prefs_destroy_file_cache);
1037         
1038         debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
1039         g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1040         
1041         return prefs_cache_sections(file_cache, rcfile);
1042 }
1043
1044 void prefs_prepare_cache(void)
1045 {
1046         gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1047         gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1048         gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1049         
1050         if (whole_cache == NULL) {
1051                 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1052                                 g_free, prefs_destroy_whole_cache);
1053         } else {
1054                 debug_print("already cached\n");
1055                 g_free(clawsrc);
1056                 g_free(folderitemrc);
1057                 g_free(accountrc);
1058                 return;
1059         }
1060         if (prefs_cache(clawsrc) < 0 ||
1061             prefs_cache(folderitemrc) < 0 ||
1062             prefs_cache(accountrc) < 0)
1063                 prefs_destroy_cache();
1064
1065         g_free(clawsrc);
1066         g_free(folderitemrc);
1067         g_free(accountrc);
1068 }
1069
1070 void prefs_destroy_cache(void)
1071 {
1072         if (!whole_cache) {
1073                 debug_print("no cache\n");
1074                 return;
1075         }
1076         debug_print("destroying cache\n");
1077         g_hash_table_destroy(whole_cache);
1078         whole_cache = NULL;
1079         return;
1080 }
1081
1082 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1083 {
1084         gchar *pref = (gchar *)key;
1085
1086         PrefParam *param = (PrefParam *)user_data;
1087         
1088         prefs_config_parse_one_line(param, pref);
1089 }
1090
1091 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1092                                const gchar *rcfile) 
1093 {
1094         GHashTable *sections_table = NULL;
1095         GHashTable *values_table = NULL;
1096         sections_table = g_hash_table_lookup(whole_cache, rcfile);
1097         
1098         if (sections_table == NULL) {
1099                 g_warning("Can't find %s in the whole cache\n", rcfile?rcfile:"(null)");
1100                 return FALSE;
1101         }
1102         values_table = g_hash_table_lookup(sections_table, label);
1103         
1104         if (values_table == NULL) {
1105                 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1106                 return TRUE;
1107         }
1108         g_hash_table_foreach(values_table, prefs_parse_cache, param);
1109         return TRUE;
1110 }