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