Fix a leak and g_log() in case of null passwords
[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                 case P_PASSWORD:
328                 {
329                         gchar *tmp = NULL;
330
331                         if (*((gchar **)param[i].data)) {
332                                 if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
333                                         tmp = g_strdup(*((gchar **)param[i].data));
334                                 else {
335                                         tmp = conv_codeset_strdup(*((gchar **)param[i].data),
336                                                 conv_get_locale_charset_str_no_utf8(),
337                                                 CS_INTERNAL);
338                                         if (!tmp)
339                                                 tmp = g_strdup(*((gchar **)param[i].data));
340                                 }
341                         }
342
343                         g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
344                                    tmp ? tmp : "");
345
346                         g_free(tmp);
347                         break;
348                 }
349                 case P_INT:
350                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
351                                    *((gint *)param[i].data));
352                         break;
353                 case P_BOOL:
354                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
355                                    *((gboolean *)param[i].data));
356                         break;
357                 case P_ENUM:
358                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
359                                    *((DummyEnum *)param[i].data));
360                         break;
361                 case P_USHORT:
362                         g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
363                                    *((gushort *)param[i].data));
364                         break;
365                 case P_COLOR:
366                         g_snprintf(buf, sizeof buf,  "%s=#%6.6lx\n", param[i].name,
367                                    *((gulong *) param[i].data));
368                         break;
369                 default:
370                         /* unrecognized, fail */
371                         debug_print("Unrecognized parameter type\n");
372                         return -1;
373                 }
374
375                 if (buf[0] != '\0') {
376                         if (fputs(buf, fp) == EOF) {
377                                 perror("fputs");
378                                 return -1;
379                         }
380                 }
381         }
382
383         return 0;
384 }
385
386 void prefs_set_default(PrefParam *param)
387 {
388         gint i;
389         GdkColor color;
390
391         cm_return_if_fail(param != NULL);
392
393         for (i = 0; param[i].name != NULL; i++) {
394                 if (!param[i].data) continue;
395
396                 switch (param[i].type) {
397                 case P_STRING:
398                         g_free(*((gchar **)param[i].data));
399                         if (param[i].defval != NULL) {
400                                 if (!strncasecmp(param[i].defval, "ENV_", 4)) {
401                                         const gchar *envstr;
402                                         gchar *tmp;
403
404                                         envstr = g_getenv(param[i].defval + 4);
405                                         tmp = envstr && *envstr ?
406                                                 conv_codeset_strdup(envstr,
407                                                                     conv_get_locale_charset_str(),
408                                                                     CS_INTERNAL)
409                                                 : g_strdup("");
410                                         if (!tmp) {
411                                                 g_warning("Failed to convert character set.");
412                                                 tmp = g_strdup(envstr);
413                                         }
414                                         *((gchar **)param[i].data) = tmp;
415                                 } else if (param[i].defval[0] == '~')
416                                         *((gchar **)param[i].data) =
417                                                 g_strconcat(get_home_dir(),
418                                                             param[i].defval + 1,
419                                                             NULL);
420                                 else 
421                                         *((gchar **)param[i].data) =
422                                                 g_strdup(param[i].defval);
423                         } else
424                                 *((gchar **)param[i].data) = NULL;
425                         break;
426                 case P_PASSWORD:
427                         g_free(*((gchar **)param[i].data));
428                         if (param[i].defval != NULL) {
429                                 if (param[i].defval[0] != '\0')
430                                         *((gchar **)param[i].data) =
431                                                 g_strdup(param[i].defval);
432                                 else
433                                         *((gchar **)param[i].data) = NULL;
434                         } else
435                                 *((gchar **)param[i].data) = NULL;
436                         break;
437                 case P_INT:
438                         if (param[i].defval != NULL)
439                                 *((gint *)param[i].data) =
440                                         (gint)atoi(param[i].defval);
441                         else
442                                 *((gint *)param[i].data) = 0;
443                         break;
444                 case P_BOOL:
445                         if (param[i].defval != NULL) {
446                                 if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
447                                         *((gboolean *)param[i].data) = TRUE;
448                                 else
449                                         *((gboolean *)param[i].data) =
450                                                 atoi(param[i].defval) ? TRUE : FALSE;
451                         } else
452                                 *((gboolean *)param[i].data) = FALSE;
453                         break;
454                 case P_ENUM:
455                         if (param[i].defval != NULL)
456                                 *((DummyEnum*)param[i].data) =
457                                         (DummyEnum)atoi(param[i].defval);
458                         else
459                                 *((DummyEnum *)param[i].data) = 0;
460                         break;
461                 case P_USHORT:
462                         if (param[i].defval != NULL)
463                                 *((gushort *)param[i].data) =
464                                         (gushort)atoi(param[i].defval);
465                         else
466                                 *((gushort *)param[i].data) = 0;
467                         break;
468                 case P_COLOR:
469                         if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
470                                 *((gulong *)param[i].data) =
471                                         RGB_FROM_GDK_COLOR(color);
472                         else if (param[i].defval)
473                                 /* be compatible and accept ints */
474                                 *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10); 
475                         else
476                                 *((gulong *)param[i].data) = 0; 
477                         break;
478                 default:
479                         break;
480                 }
481         }
482 }
483
484 void prefs_free(PrefParam *param)
485 {
486         gint i;
487
488         cm_return_if_fail(param != NULL);
489
490         for (i = 0; param[i].name != NULL; i++) {
491                 if (!param[i].data) continue;
492
493                 switch (param[i].type) {
494                 case P_STRING:
495                 case P_PASSWORD:
496                         g_free(*((gchar **)param[i].data));
497                         break;
498                 default:
499                         break;
500                 }
501         }
502 }
503
504 void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
505 {
506         gboolean is_active;
507
508         is_active = gtk_toggle_button_get_active(toggle_btn);
509         gtk_widget_set_sensitive(widget, is_active);
510 }
511
512 void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
513 {
514         gboolean is_active;
515
516         is_active = gtk_toggle_button_get_active(toggle_btn);
517         gtk_widget_set_sensitive(widget, !is_active);
518 }
519
520 void prefs_set_dialog(PrefParam *param)
521 {
522         gint i;
523
524         for (i = 0; param[i].name != NULL; i++) {
525                 if (param[i].widget_set_func)
526                         param[i].widget_set_func(&param[i]);
527         }
528 }
529
530 void prefs_set_data_from_dialog(PrefParam *param)
531 {
532         gint i;
533
534         for (i = 0; param[i].name != NULL; i++) {
535                 if (param[i].data_set_func)
536                         param[i].data_set_func(&param[i]);
537         }
538 }
539
540 void prefs_set_dialog_to_default(PrefParam *param)
541 {
542         gint       i;
543         PrefParam  tmpparam;
544         gchar     *str_data = NULL;
545         gint       int_data;
546         gushort    ushort_data;
547         gboolean   bool_data;
548         DummyEnum  enum_data;
549
550         for (i = 0; param[i].name != NULL; i++) {
551                 if (!param[i].widget_set_func) continue;
552
553                 tmpparam = param[i];
554
555                 switch (tmpparam.type) {
556                 case P_STRING:
557                         if (tmpparam.defval) {
558                                 if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
559                                         str_data = g_strdup(g_getenv(param[i].defval + 4));
560                                         tmpparam.data = &str_data;
561                                         break;
562                                 } else if (tmpparam.defval[0] == '~') {
563                                         str_data =
564                                                 g_strconcat(get_home_dir(),
565                                                             param[i].defval + 1,
566                                                             NULL);
567                                         tmpparam.data = &str_data;
568                                         break;
569                                 }
570                         }
571                         tmpparam.data = &tmpparam.defval;
572                         break;
573                 case P_PASSWORD:
574                         tmpparam.data = &tmpparam.defval;
575                         break;
576                 case P_INT:
577                         if (tmpparam.defval)
578                                 int_data = atoi(tmpparam.defval);
579                         else
580                                 int_data = 0;
581                         tmpparam.data = &int_data;
582                         break;
583                 case P_USHORT:
584                         if (tmpparam.defval)
585                                 ushort_data = atoi(tmpparam.defval);
586                         else
587                                 ushort_data = 0;
588                         tmpparam.data = &ushort_data;
589                         break;
590                 case P_BOOL:
591                         if (tmpparam.defval) {
592                                 if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
593                                         bool_data = TRUE;
594                                 else
595                                         bool_data = atoi(tmpparam.defval)
596                                                 ? TRUE : FALSE;
597                         } else
598                                 bool_data = FALSE;
599                         tmpparam.data = &bool_data;
600                         break;
601                 case P_ENUM:
602                         if (tmpparam.defval)
603                                 enum_data = (DummyEnum)atoi(tmpparam.defval);
604                         else
605                                 enum_data = 0;
606                         tmpparam.data = &enum_data;
607                         break;
608                 case P_OTHER:
609                 default:
610                         break;
611                 }
612                 tmpparam.widget_set_func(&tmpparam);
613                 g_free(str_data);
614                 str_data = NULL;
615         }
616 }
617
618 void prefs_set_data_from_entry(PrefParam *pparam)
619 {
620         gchar **str;
621         const gchar *entry_str;
622
623         cm_return_if_fail(*pparam->widget != NULL);
624
625         entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
626
627         switch (pparam->type) {
628         case P_STRING:
629                 str = (gchar **)pparam->data;
630                 g_free(*str);
631                 *str = entry_str[0] ? g_strdup(entry_str) : NULL;
632                 break;
633         case P_USHORT:
634                 *((gushort *)pparam->data) = atoi(entry_str);
635                 break;
636         case P_INT:
637                 *((gint *)pparam->data) = atoi(entry_str);
638                 break;
639         case P_PASSWORD:
640                 str = (gchar **)pparam->data;
641                 g_free(*str);
642                 *str = password_encrypt(entry_str, NULL);
643                 break;
644         default:
645                 g_warning("Invalid PrefType for GtkEntry widget: %d",
646                           pparam->type);
647         }
648 }
649
650 void prefs_set_escaped_data_from_entry(PrefParam *pparam)
651 {
652         gchar **str;
653
654         cm_return_if_fail(*pparam->widget != NULL);
655
656         switch (pparam->type) {
657         case P_STRING:
658                 str = (gchar **)pparam->data;
659                 g_free(*str);
660                 *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
661                 break;
662         default:
663                 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
664                           pparam->type);
665         }
666 }
667
668 void prefs_set_entry(PrefParam *pparam)
669 {
670         gchar **str;
671     char *decrypted_pass = NULL;
672         cm_return_if_fail(*pparam->widget != NULL);
673
674         switch (pparam->type) {
675         case P_STRING:
676                 str = (gchar **)pparam->data;
677                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
678                                    *str ? *str : "");
679                 break;
680         case P_INT:
681                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
682                                    itos(*((gint *)pparam->data)));
683                 break;
684         case P_USHORT:
685                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
686                                    itos(*((gushort *)pparam->data)));
687                 break;
688         case P_PASSWORD:
689                 str = (gchar **)pparam->data;
690         decrypted_pass = password_decrypt(*str, NULL);
691                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
692                                 (decrypted_pass != NULL ? decrypted_pass : ""));
693         g_free(decrypted_pass);
694                 break;
695         default:
696                 g_warning("Invalid PrefType for GtkEntry widget: %d",
697                           pparam->type);
698         }
699 }
700
701 void prefs_set_entry_from_escaped(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                 str = (gchar **)pparam->data;
710                 pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
711                                    *str ? *str : "");
712                 break;
713         default:
714                 g_warning("Invalid escaped PrefType for GtkEntry widget: %d",
715                           pparam->type);
716         }
717 }
718
719 void prefs_set_data_from_text(PrefParam *pparam)
720 {
721         gchar **str;
722         gchar *text = NULL, *tp = NULL;
723         gchar *tmp, *tmpp;
724
725         cm_return_if_fail(*pparam->widget != NULL);
726
727         switch (pparam->type) {
728         case P_STRING:
729         case P_PASSWORD:
730                 str = (gchar **)pparam->data;
731                 g_free(*str);
732                 if (GTK_IS_EDITABLE(*pparam->widget)) {   /* need? */
733                         tp = text = gtk_editable_get_chars
734                                         (GTK_EDITABLE(*pparam->widget), 0, -1);
735                 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
736                         GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
737                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
738                         GtkTextIter start, end;
739                         gtk_text_buffer_get_start_iter(buffer, &start);
740                         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
741                         tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
742                 }
743
744                 cm_return_if_fail (tp && text);
745
746                 if (text[0] == '\0') {
747                         *str = NULL;
748                         g_free(text);
749                         break;
750                 }
751
752                 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
753                         { *str = NULL; break; });
754                 while (*tp) {
755                         if (*tp == '\n') {
756                                 *tmpp++ = '\\';
757                                 *tmpp++ = 'n';
758                                 tp++;
759                         } else
760                                 *tmpp++ = *tp++;
761                 }
762                 *tmpp = '\0';
763                 *str = g_strdup(tmp);
764                 g_free(text);
765                 break;
766         default:
767                 g_warning("Invalid PrefType for GtkText widget: %d",
768                           pparam->type);
769         }
770 }
771
772 void prefs_set_escaped_data_from_text(PrefParam *pparam)
773 {
774         gchar **str;
775
776         cm_return_if_fail(*pparam->widget != NULL);
777
778         switch (pparam->type) {
779         case P_STRING:
780                 str = (gchar **)pparam->data;
781                 g_free(*str);
782                 *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
783                 break;
784         default:
785                 g_warning("Invalid escaped PrefType for GtkText widget: %d",
786                           pparam->type);
787         }
788 }
789
790 void prefs_set_text(PrefParam *pparam)
791 {
792         gchar *buf, *sp, *bufp;
793         gchar **str;
794         GtkTextView *text;
795         GtkTextBuffer *buffer;
796         GtkTextIter iter;
797
798         cm_return_if_fail(*pparam->widget != NULL);
799
800         switch (pparam->type) {
801         case P_STRING:
802         case P_PASSWORD:
803                 str = (gchar **)pparam->data;
804                 if (*str) {
805                         bufp = buf = alloca(strlen(*str) + 1);
806                         if (!buf) buf = "";
807                         else {
808                                 sp = *str;
809                                 while (*sp) {
810                                         if (*sp == '\\' && *(sp + 1) == 'n') {
811                                                 *bufp++ = '\n';
812                                                 sp += 2;
813                                         } else
814                                                 *bufp++ = *sp++;
815                                 }
816                                 *bufp = '\0';
817                         }
818                 } else
819                         buf = "";
820
821                 text = GTK_TEXT_VIEW(*pparam->widget);
822                 buffer = gtk_text_view_get_buffer(text);
823                 gtk_text_buffer_set_text(buffer, "", -1);
824                 gtk_text_buffer_get_start_iter(buffer, &iter);
825                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
826                 break;
827         default:
828                 g_warning("Invalid PrefType for GtkTextView widget: %d",
829                           pparam->type);
830         }
831 }
832
833 void prefs_set_text_from_escaped(PrefParam *pparam)
834 {
835         gchar **str;
836
837         cm_return_if_fail(*pparam->widget != NULL);
838
839         switch (pparam->type) {
840         case P_STRING:
841                 str = (gchar **)pparam->data;
842                 pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
843                                  *str ? *str : "");
844                 break;
845         default:
846                 g_warning("Invalid escaped PrefType for GtkTextView widget: %d",
847                           pparam->type);
848         }
849 }
850
851 void prefs_set_data_from_toggle(PrefParam *pparam)
852 {
853         cm_return_if_fail(pparam->type == P_BOOL);
854         cm_return_if_fail(*pparam->widget != NULL);
855         
856         *((gboolean *)pparam->data) =
857                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
858 }
859
860 void prefs_set_toggle(PrefParam *pparam)
861 {
862         cm_return_if_fail(pparam->type == P_BOOL);
863         cm_return_if_fail(*pparam->widget != NULL);
864
865         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
866                                      *((gboolean *)pparam->data));
867 }
868
869 void prefs_set_data_from_spinbtn(PrefParam *pparam)
870 {
871         cm_return_if_fail(*pparam->widget != NULL);
872
873         switch (pparam->type) {
874         case P_INT:
875                 *((gint *)pparam->data) =
876                         gtk_spin_button_get_value_as_int
877                         (GTK_SPIN_BUTTON(*pparam->widget));
878                 break;
879         case P_USHORT:
880                 *((gushort *)pparam->data) =
881                         (gushort)gtk_spin_button_get_value_as_int
882                         (GTK_SPIN_BUTTON(*pparam->widget));
883                 break;
884         default:
885                 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
886                           pparam->type);
887         }
888 }
889
890 void prefs_set_spinbtn(PrefParam *pparam)
891 {
892         cm_return_if_fail(*pparam->widget != NULL);
893
894         switch (pparam->type) {
895         case P_INT:
896                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
897                                           (gfloat)*((gint *)pparam->data));
898                 break;
899         case P_USHORT:
900                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
901                                           (gfloat)*((gushort *)pparam->data));
902                 break;
903         default:
904                 g_warning("Invalid PrefType for GtkSpinButton widget: %d",
905                           pparam->type);
906         }
907 }
908
909 static GSList *prefs_pages = NULL;
910
911 static void prefs_gtk_window_closed_cb(PrefsWindow *prefswindow)
912 {
913         if (prefswindow == NULL)
914                 return;
915
916         if (prefswindow->dialog_response > PREFSWINDOW_RESPONSE_CANCEL)
917                 prefs_common_write_config();
918 }
919
920 void prefs_gtk_open(void)
921 {
922         prefswindow_open(_("Preferences"), prefs_pages, NULL,
923                         &prefs_common.prefswin_width, &prefs_common.prefswin_height,
924                         NULL, prefs_gtk_window_closed_cb, prefs_gtk_window_closed_cb);
925 }
926
927 void prefs_gtk_register_page(PrefsPage *page)
928 {
929         prefs_pages = g_slist_append(prefs_pages, page);
930 }
931
932 void prefs_gtk_unregister_page(PrefsPage *page)
933 {
934         prefs_pages = g_slist_remove(prefs_pages, page);
935 }
936
937 static void prefs_destroy_whole_cache(gpointer to_free)
938 {       
939         GHashTable *table = (GHashTable *)to_free;
940         g_hash_table_destroy(table);
941 }
942
943 static void prefs_destroy_file_cache(gpointer to_free)
944 {       
945         GHashTable *table = (GHashTable *)to_free;
946         g_hash_table_destroy(table);
947 }
948
949 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
950 {
951         FILE *fp = NULL;
952         gchar buf[PREFSBUFSIZE];
953         GHashTable *section_cache = NULL;
954
955         if (rcfile)
956                 fp = g_fopen(rcfile, "rb");
957         if (!fp) {
958                 debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
959                 return -1;
960         }
961         
962 #ifdef HAVE_FGETS_UNLOCKED
963         flockfile(fp);
964 #endif
965         
966         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
967                 strretchomp(buf);
968                 if (buf[0] == '\0')
969                         continue;
970                 if (buf[0] == '#')
971                         continue; /* comment */
972                 if (buf[0] == '[') { /* new section */
973                         gchar *blockname = g_strdup(buf+1);
974
975                         if (strrchr(blockname, ']'))
976                                 *strrchr(blockname, ']') = '\0';
977
978                         if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
979                                 debug_print("new section '%s'\n", blockname);
980                                 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
981                                                 g_free, NULL);
982                                 g_hash_table_insert(file_cache, 
983                                         blockname, section_cache);
984                         } else {
985                                 debug_print("section '%s' already done\n", blockname);
986                                 g_free(blockname);
987                                 section_cache = NULL;
988                                 continue;
989                         }
990                 } else {
991                         if (!section_cache) {
992                                 debug_print("skipping stuff %s with no section\n", buf);
993                                 continue;
994                         } else {
995                                 gchar *pref;
996                                 
997                                 if (!strchr(buf, '=')) {
998                                         /* plugins do differently */
999                                         continue;
1000                                 }
1001                                 pref = g_strdup(buf);
1002                                 
1003                                 //debug_print("new pref '%s'\n", pref);
1004                                 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
1005                         }
1006                 }
1007         }
1008 #ifdef HAVE_FGETS_UNLOCKED
1009         funlockfile(fp);
1010 #endif
1011         fclose(fp);
1012         return 0;
1013 }
1014
1015 static int prefs_cache(const gchar *rcfile)
1016 {
1017         GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal, 
1018                                         g_free, prefs_destroy_file_cache);
1019         
1020         debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
1021         g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1022         
1023         return prefs_cache_sections(file_cache, rcfile);
1024 }
1025
1026 void prefs_prepare_cache(void)
1027 {
1028         gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1029         gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1030         gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1031         
1032         if (whole_cache == NULL) {
1033                 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1034                                 g_free, prefs_destroy_whole_cache);
1035         } else {
1036                 debug_print("already cached\n");
1037                 g_free(clawsrc);
1038                 g_free(folderitemrc);
1039                 g_free(accountrc);
1040                 return;
1041         }
1042         if (prefs_cache(clawsrc) < 0 ||
1043             prefs_cache(folderitemrc) < 0 ||
1044             prefs_cache(accountrc) < 0)
1045                 prefs_destroy_cache();
1046
1047         g_free(clawsrc);
1048         g_free(folderitemrc);
1049         g_free(accountrc);
1050 }
1051
1052 void prefs_destroy_cache(void)
1053 {
1054         if (!whole_cache) {
1055                 debug_print("no cache\n");
1056                 return;
1057         }
1058         debug_print("destroying cache\n");
1059         g_hash_table_destroy(whole_cache);
1060         whole_cache = NULL;
1061         return;
1062 }
1063
1064 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1065 {
1066         gchar *pref = (gchar *)key;
1067
1068         PrefParam *param = (PrefParam *)user_data;
1069         
1070         prefs_config_parse_one_line(param, pref);
1071 }
1072
1073 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1074                                const gchar *rcfile) 
1075 {
1076         GHashTable *sections_table = NULL;
1077         GHashTable *values_table = NULL;
1078         sections_table = g_hash_table_lookup(whole_cache, rcfile);
1079         
1080         if (sections_table == NULL) {
1081                 g_warning("Can't find %s in the whole cache", rcfile?rcfile:"(null)");
1082                 return FALSE;
1083         }
1084         values_table = g_hash_table_lookup(sections_table, label);
1085         
1086         if (values_table == NULL) {
1087                 debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
1088                 return TRUE;
1089         }
1090         g_hash_table_foreach(values_table, prefs_parse_cache, param);
1091         return TRUE;
1092 }