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