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