2006-11-12 [paul] 2.6.0cvs34
[claws.git] / src / prefs_gtk.c
1 /*
2  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
3  * Copyright (C) 1999-2006 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("faild 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 = gtk_window_new (GTK_WINDOW_TOPLEVEL);
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_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         case P_PASSWORD:
732                 str = (gchar **)pparam->data;
733                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
734                                    *str ? *str : "");
735                 break;
736         case P_INT:
737                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
738                                    itos(*((gint *)pparam->data)));
739                 break;
740         case P_USHORT:
741                 gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
742                                    itos(*((gushort *)pparam->data)));
743                 break;
744         default:
745                 g_warning("Invalid PrefType for GtkEntry widget: %d\n",
746                           pparam->type);
747         }
748 }
749
750 void prefs_set_data_from_text(PrefParam *pparam)
751 {
752         gchar **str;
753         gchar *text = NULL, *tp = NULL;
754         gchar *tmp, *tmpp;
755
756         g_return_if_fail(*pparam->widget != NULL);
757
758         switch (pparam->type) {
759         case P_STRING:
760         case P_PASSWORD:
761                 str = (gchar **)pparam->data;
762                 g_free(*str);
763                 if (GTK_IS_EDITABLE(*pparam->widget)) {   /* need? */
764                         tp = text = gtk_editable_get_chars
765                                         (GTK_EDITABLE(*pparam->widget), 0, -1);
766                 } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
767                         GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
768                         GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
769                         GtkTextIter start, end;
770                         gtk_text_buffer_get_start_iter(buffer, &start);
771                         gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
772                         tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
773                 }
774
775                 g_return_if_fail (tp && text);
776
777                 if (text[0] == '\0') {
778                         *str = NULL;
779                         g_free(text);
780                         break;
781                 }
782
783                 Xalloca(tmpp = tmp, strlen(text) * 2 + 1,
784                         { *str = NULL; break; });
785                 while (*tp) {
786                         if (*tp == '\n') {
787                                 *tmpp++ = '\\';
788                                 *tmpp++ = 'n';
789                                 tp++;
790                         } else
791                                 *tmpp++ = *tp++;
792                 }
793                 *tmpp = '\0';
794                 *str = g_strdup(tmp);
795                 g_free(text);
796                 break;
797         default:
798                 g_warning("Invalid PrefType for GtkText widget: %d\n",
799                           pparam->type);
800         }
801 }
802
803 void prefs_set_text(PrefParam *pparam)
804 {
805         gchar *buf, *sp, *bufp;
806         gchar **str;
807         GtkTextView *text;
808         GtkTextBuffer *buffer;
809         GtkTextIter iter;
810
811         g_return_if_fail(*pparam->widget != NULL);
812
813         switch (pparam->type) {
814         case P_STRING:
815         case P_PASSWORD:
816                 str = (gchar **)pparam->data;
817                 if (*str) {
818                         bufp = buf = alloca(strlen(*str) + 1);
819                         if (!buf) buf = "";
820                         else {
821                                 sp = *str;
822                                 while (*sp) {
823                                         if (*sp == '\\' && *(sp + 1) == 'n') {
824                                                 *bufp++ = '\n';
825                                                 sp += 2;
826                                         } else
827                                                 *bufp++ = *sp++;
828                                 }
829                                 *bufp = '\0';
830                         }
831                 } else
832                         buf = "";
833
834                 text = GTK_TEXT_VIEW(*pparam->widget);
835                 buffer = gtk_text_view_get_buffer(text);
836                 gtk_text_buffer_set_text(buffer, "", -1);
837                 gtk_text_buffer_get_start_iter(buffer, &iter);
838                 gtk_text_buffer_insert(buffer, &iter, buf, -1);
839                 break;
840         default:
841                 g_warning("Invalid PrefType for GtkTextView widget: %d\n",
842                           pparam->type);
843         }
844 }
845
846 void prefs_set_data_from_toggle(PrefParam *pparam)
847 {
848         g_return_if_fail(pparam->type == P_BOOL);
849         g_return_if_fail(*pparam->widget != NULL);
850         
851         *((gboolean *)pparam->data) =
852                 gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
853 }
854
855 void prefs_set_toggle(PrefParam *pparam)
856 {
857         g_return_if_fail(pparam->type == P_BOOL);
858         g_return_if_fail(*pparam->widget != NULL);
859
860         gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
861                                      *((gboolean *)pparam->data));
862 }
863
864 void prefs_set_data_from_spinbtn(PrefParam *pparam)
865 {
866         g_return_if_fail(*pparam->widget != NULL);
867
868         switch (pparam->type) {
869         case P_INT:
870                 *((gint *)pparam->data) =
871                         gtk_spin_button_get_value_as_int
872                         (GTK_SPIN_BUTTON(*pparam->widget));
873                 break;
874         case P_USHORT:
875                 *((gushort *)pparam->data) =
876                         (gushort)gtk_spin_button_get_value_as_int
877                         (GTK_SPIN_BUTTON(*pparam->widget));
878                 break;
879         default:
880                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
881                           pparam->type);
882         }
883 }
884
885 void prefs_set_spinbtn(PrefParam *pparam)
886 {
887         g_return_if_fail(*pparam->widget != NULL);
888
889         switch (pparam->type) {
890         case P_INT:
891                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
892                                           (gfloat)*((gint *)pparam->data));
893                 break;
894         case P_USHORT:
895                 gtk_spin_button_set_value(GTK_SPIN_BUTTON(*pparam->widget),
896                                           (gfloat)*((gushort *)pparam->data));
897                 break;
898         default:
899                 g_warning("Invalid PrefType for GtkSpinButton widget: %d\n",
900                           pparam->type);
901         }
902 }
903
904 static GSList *prefs_pages = NULL;
905
906 void prefs_gtk_open(void)
907 {
908         prefswindow_open(_("Preferences"), prefs_pages, NULL,
909                         &prefs_common.prefswin_width, &prefs_common.prefswin_height);
910 }
911
912 void prefs_gtk_register_page(PrefsPage *page)
913 {
914         prefs_pages = g_slist_append(prefs_pages, page);
915 }
916
917 void prefs_gtk_unregister_page(PrefsPage *page)
918 {
919         prefs_pages = g_slist_remove(prefs_pages, page);
920 }
921
922 static void prefs_destroy_whole_cache(gpointer to_free)
923 {       
924         GHashTable *table = (GHashTable *)to_free;
925         g_hash_table_destroy(table);
926 }
927
928 static void prefs_destroy_file_cache(gpointer to_free)
929 {       
930         GHashTable *table = (GHashTable *)to_free;
931         g_hash_table_destroy(table);
932 }
933
934 static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
935 {
936         FILE *fp = g_fopen(rcfile, "rb");
937         gchar buf[PREFSBUFSIZE];
938         GHashTable *section_cache = NULL;
939
940         if (!fp) {
941                 debug_print("cache: %s: %s", rcfile, strerror(errno));
942                 return -1;
943         }
944         
945 #ifdef HAVE_FGETS_UNLOCKED
946         flockfile(fp);
947 #endif
948         
949         while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
950                 strretchomp(buf);
951                 if (buf[0] == '\0')
952                         continue;
953                 if (buf[0] == '#')
954                         continue; /* comment */
955                 if (buf[0] == '[') { /* new section */
956                         gchar *blockname = g_strdup(buf+1);
957
958                         if (strrchr(blockname, ']'))
959                                 *strrchr(blockname, ']') = '\0';
960
961                         if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
962                                 debug_print("new section '%s'\n", blockname);
963                                 section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
964                                                 g_free, NULL);
965                                 g_hash_table_insert(file_cache, 
966                                         blockname, section_cache);
967                         } else {
968                                 debug_print("section '%s' already done\n", blockname);
969                                 g_free(blockname);
970                                 section_cache = NULL;
971                                 continue;
972                         }
973                 } else {
974                         if (!section_cache) {
975                                 debug_print("skipping stuff %s with no section\n", buf);
976                                 continue;
977                         } else {
978                                 gchar *pref;
979                                 
980                                 if (!strchr(buf, '=')) {
981                                         /* plugins do differently */
982                                         continue;
983                                 }
984                                 pref = g_strdup(buf);
985                                 
986                                 //debug_print("new pref '%s'\n", pref);
987                                 g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
988                         }
989                 }
990         }
991 #ifdef HAVE_FGETS_UNLOCKED
992         funlockfile(fp);
993 #endif
994         fclose(fp);
995         return 0;
996 }
997
998 static int prefs_cache(const gchar *rcfile)
999 {
1000         GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal, 
1001                                         g_free, prefs_destroy_file_cache);
1002         
1003         debug_print("new file '%s'\n", rcfile);
1004         g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
1005         
1006         return prefs_cache_sections(file_cache, rcfile);
1007 }
1008
1009 void prefs_prepare_cache(void)
1010 {
1011         gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
1012         gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
1013         gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
1014         
1015         if (whole_cache == NULL) {
1016                 whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
1017                                 g_free, prefs_destroy_whole_cache);
1018         } else {
1019                 debug_print("already cached\n");
1020                 g_free(clawsrc);
1021                 g_free(folderitemrc);
1022                 g_free(accountrc);
1023                 return;
1024         }
1025         if (prefs_cache(clawsrc) < 0 ||
1026             prefs_cache(folderitemrc) < 0 ||
1027             prefs_cache(accountrc) < 0)
1028                 prefs_destroy_cache();
1029
1030         g_free(clawsrc);
1031         g_free(folderitemrc);
1032         g_free(accountrc);
1033 }
1034
1035 void prefs_destroy_cache(void)
1036 {
1037         if (!whole_cache) {
1038                 debug_print("no cache\n");
1039                 return;
1040         }
1041         debug_print("destroying cache\n");
1042         g_hash_table_destroy(whole_cache);
1043         whole_cache = NULL;
1044         return;
1045 }
1046
1047 static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
1048 {
1049         gchar *pref = (gchar *)key;
1050
1051         PrefParam *param = (PrefParam *)user_data;
1052         
1053         prefs_config_parse_one_line(param, pref);
1054 }
1055
1056 static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
1057                                const gchar *rcfile) 
1058 {
1059         GHashTable *sections_table = NULL;
1060         GHashTable *values_table = NULL;
1061         sections_table = g_hash_table_lookup(whole_cache, rcfile);
1062         
1063         if (sections_table == NULL) {
1064                 g_warning("can't find %s in the whole cache\n", rcfile);
1065                 return FALSE;
1066         }
1067         values_table = g_hash_table_lookup(sections_table, label);
1068         
1069         if (values_table == NULL) {
1070                 debug_print("no '%s' section in '%s' cache\n", label, rcfile);
1071                 return TRUE;
1072         }
1073         g_hash_table_foreach(values_table, prefs_parse_cache, param);
1074         return TRUE;
1075 }