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