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