Switch core from strerror to g_strerror
[claws.git] / src / prefs_gtk.c
index 8dc35f1732a4bdb2ccb04fb7b23543395abb8db2..843f9c95b1656fc5d9f7babdc9b9b86acbfe6e8d 100644 (file)
@@ -1,10 +1,10 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2015 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ *
  */
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
-#include <gtk/gtk.h>
+#define _GNU_SOURCE
 #include <stdio.h>
+
+#include <glib.h>
+#include <glib/gi18n.h>
+#include <gtk/gtk.h>
 #include <stdlib.h>
 #include <string.h>
 #include <unistd.h>
 #include <errno.h>
 
-#include "intl.h"
+#include "defs.h"
 #include "main.h"
 #include "prefs.h"
 #include "prefs_gtk.h"
+#include "prefs_common.h"
 #include "utils.h"
 #include "gtkutils.h"
 #include "passcrypt.h"
-#include "base64.h"
 #include "codeconv.h"
 
 #define CL(x)  (((gulong) (x) >> (gulong) 8) & 0xFFUL)
         (CL(c.green) << (gulong)  8) | \
         (CL(c.blue)))
 
+#ifdef HAVE_FGETS_UNLOCKED
+#define SC_FGETS fgets_unlocked
+#else
+#define SC_FGETS fgets
+#endif
+
 typedef enum
 {
        DUMMY_PARAM
 } DummyEnum;
 
+static GHashTable *whole_cache = NULL;
+
+static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
+                              const gchar *rcfile);
+
+static void prefs_config_parse_one_line(PrefParam      *param,
+                                        const gchar    *buf);
+
 void prefs_read_config(PrefParam *param, const gchar *label,
-                      const gchar *rcfile)
+                      const gchar *rcfile, const gchar *encoding)
 {
        FILE *fp;
        gchar buf[PREFSBUFSIZE];
-       gchar *rcpath;
        gchar *block_label;
 
-       g_return_if_fail(param != NULL);
-       g_return_if_fail(label != NULL);
-       g_return_if_fail(rcfile != NULL);
+       cm_return_if_fail(param != NULL);
+       cm_return_if_fail(label != NULL);
+       cm_return_if_fail(rcfile != NULL);
+
+       if (encoding != NULL)
+               g_warning("Encoding is ignored\n");
 
        debug_print("Reading configuration...\n");
 
        prefs_set_default(param);
 
-       rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
-       if ((fp = fopen(rcpath, "rb")) == NULL) {
-               if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
-               g_free(rcpath);
+       if (whole_cache != NULL) {
+               if (prefs_read_config_from_cache(param, label, rcfile) == TRUE)
+                       return;
+       }
+
+       if ((fp = g_fopen(rcfile, "rb")) == NULL) {
+               if (ENOENT != errno) FILE_OP_ERROR(rcfile, "fopen");
                return;
        }
-       g_free(rcpath);
 
        block_label = g_strdup_printf("[%s]", label);
 
+#ifdef HAVE_FGETS_UNLOCKED
+       flockfile(fp);
+#endif
+
        /* search aiming block */
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
                gint val;
 
-               val = strncmp(buf, block_label, strlen(block_label));
+               if (encoding) {
+                       gchar *conv_str;
+
+                       conv_str = conv_codeset_strdup
+                               (buf, encoding, CS_INTERNAL);
+                       if (!conv_str)
+                               conv_str = g_strdup(buf);
+                       val = strncmp
+                               (conv_str, block_label, strlen(block_label));
+                       g_free(conv_str);
+               } else
+                       val = strncmp(buf, block_label, strlen(block_label));
                if (val == 0) {
                        debug_print("Found %s\n", block_label);
                        break;
@@ -87,19 +125,33 @@ void prefs_read_config(PrefParam *param, const gchar *label,
        }
        g_free(block_label);
 
-       while (fgets(buf, sizeof(buf), fp) != NULL) {
+       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
                strretchomp(buf);
                /* reached next block */
                if (buf[0] == '[') break;
+               if (buf[0] == '#') continue;
+
+               if (encoding) {
+                       gchar *conv_str;
 
-               prefs_config_parse_one_line(param, buf);
+                       conv_str = conv_codeset_strdup
+                               (buf, encoding, CS_INTERNAL);
+                       if (!conv_str)
+                               conv_str = g_strdup(buf);
+                       prefs_config_parse_one_line(param, conv_str);
+                       g_free(conv_str);
+               } else
+                       prefs_config_parse_one_line(param, buf);
        }
 
        debug_print("Finished reading configuration.\n");
+#ifdef HAVE_FGETS_UNLOCKED
+       funlockfile(fp);
+#endif
        fclose(fp);
 }
 
-void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
+static void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
 {
        gint i;
        gint name_len;
@@ -108,7 +160,7 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
 
        for (i = 0; param[i].name != NULL; i++) {
                name_len = strlen(param[i].name);
-               if (strncasecmp(buf, param[i].name, name_len))
+               if (g_ascii_strncasecmp(buf, param[i].name, name_len))
                        continue;
                if (buf[name_len] != '=')
                        continue;
@@ -118,16 +170,21 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
                switch (param[i].type) {
                case P_STRING:
                {
-#warning FIXME_GTK2
-                       gchar *tmp;
-
-                       tmp = *value ?
-                               conv_codeset_strdup(value,
-                                                   conv_get_current_charset_str(),
-                                                   CS_UTF_8)
-                               : g_strdup("");
+                       gchar *tmp = NULL;
+
+                       if (*value) {
+                               if (g_utf8_validate(value, -1, NULL))
+                                       tmp = g_strdup(value);
+                               else {
+                                       tmp = conv_codeset_strdup(value,
+                                                   conv_get_locale_charset_str_no_utf8(),
+                                                   CS_INTERNAL);
+                               }
+                       } else {
+                               tmp = g_strdup("");
+                       }
                        if (!tmp) {
-                               g_warning("faild to convert character set.");
+                               g_warning("Failed to convert character set.");
                                tmp = g_strdup(value);
                        }
                        g_free(*((gchar **)param[i].data));
@@ -152,8 +209,9 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
                                (gushort)atoi(value);
                        break;
                case P_COLOR:
-                       if (gdk_color_parse(value, &color)) 
+                       if (gdk_color_parse(value, &color)) {
                                *((gulong *)param[i].data) = RGB_FROM_GDK_COLOR(color); 
+                       }
                        else 
                                /* be compatible and accept ints */
                                *((gulong *)param[i].data) = strtoul(value, 0, 10); 
@@ -161,14 +219,15 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
                case P_PASSWORD:
                        g_free(*((gchar **)param[i].data));
                        if (value[0] == '!') {
-                               gchar tmp[1024];
-                               gint len;
+                               gchar *tmp;
+                               gsize len;
 
-                               len = base64_decode(tmp, &value[1], strlen(value) - 1);
+                               tmp = g_base64_decode(&value[1], &len);
                                passcrypt_decrypt(tmp, len);
-                               tmp[len] = '\0';
+
                                *((gchar **)param[i].data) =
                                        *tmp ? g_strdup(tmp) : NULL;
+                               g_free(tmp);
                        } else {
                                *((gchar **)param[i].data) =
                                        *value ? g_strdup(value) : NULL;
@@ -183,7 +242,7 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
 #define TRY(func) \
 if (!(func)) \
 { \
-       g_warning("failed to write configuration to file\n"); \
+       g_warning("Failed to write configuration to file\n"); \
        if (orig_fp) fclose(orig_fp); \
        prefs_file_close_revert(pfile); \
        g_free(rcpath); \
@@ -191,8 +250,8 @@ if (!(func)) \
        return; \
 } \
 
-void prefs_save_config(PrefParam *param, const gchar *label,
-                      const gchar *rcfile)
+void prefs_write_config(PrefParam *param, const gchar *label,
+                       const gchar *rcfile)
 {
        FILE *orig_fp;
        PrefFile *pfile;
@@ -201,17 +260,17 @@ void prefs_save_config(PrefParam *param, const gchar *label,
        gchar *block_label = NULL;
        gboolean block_matched = FALSE;
 
-       g_return_if_fail(param != NULL);
-       g_return_if_fail(label != NULL);
-       g_return_if_fail(rcfile != NULL);
+       cm_return_if_fail(param != NULL);
+       cm_return_if_fail(label != NULL);
+       cm_return_if_fail(rcfile != NULL);
 
        rcpath = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, rcfile, NULL);
-       if ((orig_fp = fopen(rcpath, "rb")) == NULL) {
+       if ((orig_fp = g_fopen(rcpath, "rb")) == NULL) {
                if (ENOENT != errno) FILE_OP_ERROR(rcpath, "fopen");
        }
 
        if ((pfile = prefs_write_open(rcpath)) == NULL) {
-               g_warning("failed to write configuration to file\n");
+               g_warning("Failed to write configuration to file\n");
                if (orig_fp) fclose(orig_fp);
                g_free(rcpath);
                return;
@@ -235,13 +294,12 @@ void prefs_save_config(PrefParam *param, const gchar *label,
        }
 
        TRY(fprintf(pfile->fp, "%s\n", block_label) > 0);
-       g_free(block_label);
-       block_label = NULL;
 
        /* write all param data to file */
        TRY(prefs_write_param(param, pfile->fp) == 0);
 
        if (block_matched) {
+               gboolean in_dup_block = FALSE;
                while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
                        /* next block */
                        if (buf[0] == '[') {
@@ -250,13 +308,25 @@ void prefs_save_config(PrefParam *param, const gchar *label,
                                break;
                        }
                }
-               while (fgets(buf, sizeof(buf), orig_fp) != NULL)
-                       TRY(fputs(buf, pfile->fp) != EOF);
+               while (fgets(buf, sizeof(buf), orig_fp) != NULL) {
+                       if (buf[0] == '[') {
+                               if (!strncmp(buf, block_label,
+                                               strlen(block_label)))
+                                       in_dup_block = TRUE;
+                               else
+                                       in_dup_block = FALSE;
+                       }
+                       if (!in_dup_block)
+                               TRY(fputs(buf, pfile->fp) != EOF);
+               }
        }
 
+       g_free(block_label);
+       block_label = NULL;
+
        if (orig_fp) fclose(orig_fp);
        if (prefs_file_close(pfile) < 0)
-               g_warning("failed to write configuration to file\n");
+               g_warning("Failed to write configuration to file\n");
        g_free(rcpath);
 
        debug_print("Configuration is saved.\n");
@@ -271,15 +341,18 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
                switch (param[i].type) {
                case P_STRING:
                {
-#warning FIXME_GTK2
                        gchar *tmp = NULL;
 
                        if (*((gchar **)param[i].data)) {
-                               tmp = conv_codeset_strdup(*((gchar **)param[i].data),
-                                                         CS_UTF_8,
-                                                         conv_get_current_charset_str());
-                               if (!tmp)
+                               if (g_utf8_validate(*((gchar **)param[i].data), -1, NULL))
                                        tmp = g_strdup(*((gchar **)param[i].data));
+                               else {
+                                       tmp = conv_codeset_strdup(*((gchar **)param[i].data),
+                                               conv_get_locale_charset_str_no_utf8(),
+                                               CS_INTERNAL);
+                                       if (!tmp)
+                                               tmp = g_strdup(*((gchar **)param[i].data));
+                               }
                        }
 
                        g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
@@ -310,7 +383,7 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
                        break;
                case P_PASSWORD:
                        {
-                               gchar *tmp = NULL, tmp2[1024] = {0};
+                               gchar *tmp = NULL, *tmp2 = NULL;
 
                                tmp = *((gchar **)param[i].data);
                                if (tmp) {
@@ -319,13 +392,14 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
                                        tmp = g_strdup(tmp);
                                        len = strlen(tmp);
                                        passcrypt_encrypt(tmp, len);
-                                       base64_encode(tmp2, tmp, len);
+                                       tmp2 = g_base64_encode(tmp, len);
                                        g_free(tmp);
                                        tmp = tmp2;
                                }
                                g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
                                           tmp ?
                                           tmp : "");
+                               g_free(tmp);
                        }
                        break;
                default:
@@ -350,15 +424,13 @@ void prefs_set_default(PrefParam *param)
        gint i;
        GdkColor color;
 
-       g_return_if_fail(param != NULL);
+       cm_return_if_fail(param != NULL);
 
        for (i = 0; param[i].name != NULL; i++) {
                if (!param[i].data) continue;
 
                switch (param[i].type) {
-#warning FIXME_GTK2
                case P_STRING:
-               case P_PASSWORD:
                        g_free(*((gchar **)param[i].data));
                        if (param[i].defval != NULL) {
                                if (!strncasecmp(param[i].defval, "ENV_", 4)) {
@@ -368,11 +440,11 @@ void prefs_set_default(PrefParam *param)
                                        envstr = g_getenv(param[i].defval + 4);
                                        tmp = envstr && *envstr ?
                                                conv_codeset_strdup(envstr,
-                                                                   conv_get_current_charset_str(),
-                                                                   CS_UTF_8)
+                                                                   conv_get_locale_charset_str(),
+                                                                   CS_INTERNAL)
                                                : g_strdup("");
                                        if (!tmp) {
-                                               g_warning("faild to convert character set.");
+                                               g_warning("Failed to convert character set.");
                                                tmp = g_strdup(envstr);
                                        }
                                        *((gchar **)param[i].data) = tmp;
@@ -381,7 +453,16 @@ void prefs_set_default(PrefParam *param)
                                                g_strconcat(get_home_dir(),
                                                            param[i].defval + 1,
                                                            NULL);
-                               else if (param[i].defval[0] != '\0')
+                               else 
+                                       *((gchar **)param[i].data) =
+                                               g_strdup(param[i].defval);
+                       } else
+                               *((gchar **)param[i].data) = NULL;
+                       break;
+               case P_PASSWORD:
+                       g_free(*((gchar **)param[i].data));
+                       if (param[i].defval != NULL) {
+                               if (param[i].defval[0] != '\0')
                                        *((gchar **)param[i].data) =
                                                g_strdup(param[i].defval);
                                else
@@ -398,7 +479,7 @@ void prefs_set_default(PrefParam *param)
                        break;
                case P_BOOL:
                        if (param[i].defval != NULL) {
-                               if (!strcasecmp(param[i].defval, "TRUE"))
+                               if (!g_ascii_strcasecmp(param[i].defval, "TRUE"))
                                        *((gboolean *)param[i].data) = TRUE;
                                else
                                        *((gboolean *)param[i].data) =
@@ -440,7 +521,7 @@ void prefs_free(PrefParam *param)
 {
        gint i;
 
-       g_return_if_fail(param != NULL);
+       cm_return_if_fail(param != NULL);
 
        for (i = 0; param[i].name != NULL; i++) {
                if (!param[i].data) continue;
@@ -456,69 +537,20 @@ void prefs_free(PrefParam *param)
        }
 }
 
-void prefs_dialog_create(PrefsDialog *dialog)
+void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
 {
-       GtkWidget *window;
-       GtkWidget *vbox;
-       GtkWidget *notebook;
-
-       GtkWidget *confirm_area;
-       GtkWidget *ok_btn;
-       GtkWidget *cancel_btn;
-       GtkWidget *apply_btn;
-
-       g_return_if_fail(dialog != NULL);
-
-       window = gtk_window_new (GTK_WINDOW_TOPLEVEL);
-       gtk_container_set_border_width (GTK_CONTAINER (window), 8);
-       gtk_window_position (GTK_WINDOW(window), GTK_WIN_POS_CENTER);
-       gtk_window_set_modal (GTK_WINDOW (window), TRUE);
-       gtk_window_set_policy (GTK_WINDOW(window), FALSE, TRUE, FALSE);
-
-       vbox = gtk_vbox_new (FALSE, 6);
-       gtk_widget_show(vbox);
-       gtk_container_add (GTK_CONTAINER (window), vbox);
-
-       notebook = gtk_notebook_new ();
-       gtk_widget_show(notebook);
-       gtk_box_pack_start (GTK_BOX (vbox), notebook, TRUE, TRUE, 0);
-       gtk_container_set_border_width (GTK_CONTAINER (notebook), 2);
-       /* GTK_WIDGET_UNSET_FLAGS (notebook, GTK_CAN_FOCUS); */
-       gtk_notebook_set_scrollable (GTK_NOTEBOOK (notebook), TRUE);
-       
-       gtk_notebook_popup_enable (GTK_NOTEBOOK (notebook));
-       
-       gtkut_button_set_create(&confirm_area,
-                               &ok_btn,        _("OK"),
-                               &cancel_btn,    _("Cancel"),
-                               &apply_btn,     _("Apply"));
-       gtk_widget_show(confirm_area);
-       gtk_box_pack_end (GTK_BOX(vbox), confirm_area, FALSE, FALSE, 0);
-       gtk_widget_grab_default(ok_btn);
-
-       dialog->window     = window;
-       dialog->notebook   = notebook;
-       dialog->ok_btn     = ok_btn;
-       dialog->cancel_btn = cancel_btn;
-       dialog->apply_btn  = apply_btn;
-}
+       gboolean is_active;
 
-void prefs_dialog_destroy(PrefsDialog *dialog)
-{
-       gtk_widget_destroy(dialog->window);
-       dialog->window     = NULL;
-       dialog->notebook   = NULL;
-       dialog->ok_btn     = NULL;
-       dialog->cancel_btn = NULL;
-       dialog->apply_btn  = NULL;
+       is_active = gtk_toggle_button_get_active(toggle_btn);
+       gtk_widget_set_sensitive(widget, is_active);
 }
 
-void prefs_button_toggled(GtkToggleButton *toggle_btn, GtkWidget *widget)
+void prefs_button_toggled_reverse(GtkToggleButton *toggle_btn, GtkWidget *widget)
 {
        gboolean is_active;
 
        is_active = gtk_toggle_button_get_active(toggle_btn);
-       gtk_widget_set_sensitive(widget, is_active);
+       gtk_widget_set_sensitive(widget, !is_active);
 }
 
 void prefs_set_dialog(PrefParam *param)
@@ -558,10 +590,8 @@ void prefs_set_dialog_to_default(PrefParam *param)
 
                switch (tmpparam.type) {
                case P_STRING:
-               case P_PASSWORD:
-#warning FIXME_GTK2
                        if (tmpparam.defval) {
-                               if (!strncasecmp(tmpparam.defval, "ENV_", 4)) {
+                               if (!g_ascii_strncasecmp(tmpparam.defval, "ENV_", 4)) {
                                        str_data = g_strdup(g_getenv(param[i].defval + 4));
                                        tmpparam.data = &str_data;
                                        break;
@@ -576,6 +606,9 @@ void prefs_set_dialog_to_default(PrefParam *param)
                        }
                        tmpparam.data = &tmpparam.defval;
                        break;
+               case P_PASSWORD:
+                       tmpparam.data = &tmpparam.defval;
+                       break;
                case P_INT:
                        if (tmpparam.defval)
                                int_data = atoi(tmpparam.defval);
@@ -592,7 +625,7 @@ void prefs_set_dialog_to_default(PrefParam *param)
                        break;
                case P_BOOL:
                        if (tmpparam.defval) {
-                               if (!strcasecmp(tmpparam.defval, "TRUE"))
+                               if (!g_ascii_strcasecmp(tmpparam.defval, "TRUE"))
                                        bool_data = TRUE;
                                else
                                        bool_data = atoi(tmpparam.defval)
@@ -623,7 +656,7 @@ void prefs_set_data_from_entry(PrefParam *pparam)
        gchar **str;
        const gchar *entry_str;
 
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        entry_str = gtk_entry_get_text(GTK_ENTRY(*pparam->widget));
 
@@ -646,11 +679,29 @@ void prefs_set_data_from_entry(PrefParam *pparam)
        }
 }
 
+void prefs_set_escaped_data_from_entry(PrefParam *pparam)
+{
+       gchar **str;
+
+       cm_return_if_fail(*pparam->widget != NULL);
+
+       switch (pparam->type) {
+       case P_STRING:
+               str = (gchar **)pparam->data;
+               g_free(*str);
+               *str = pref_get_pref_from_entry(GTK_ENTRY(*pparam->widget));
+               break;
+       default:
+               g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
+                         pparam->type);
+       }
+}
+
 void prefs_set_entry(PrefParam *pparam)
 {
        gchar **str;
 
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_STRING:
@@ -673,13 +724,31 @@ void prefs_set_entry(PrefParam *pparam)
        }
 }
 
+void prefs_set_entry_from_escaped(PrefParam *pparam)
+{
+       gchar **str;
+
+       cm_return_if_fail(*pparam->widget != NULL);
+
+       switch (pparam->type) {
+       case P_STRING:
+               str = (gchar **)pparam->data;
+               pref_set_entry_from_pref(GTK_ENTRY(*pparam->widget),
+                                  *str ? *str : "");
+               break;
+       default:
+               g_warning("Invalid escaped PrefType for GtkEntry widget: %d\n",
+                         pparam->type);
+       }
+}
+
 void prefs_set_data_from_text(PrefParam *pparam)
 {
        gchar **str;
        gchar *text = NULL, *tp = NULL;
        gchar *tmp, *tmpp;
 
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_STRING:
@@ -698,7 +767,7 @@ void prefs_set_data_from_text(PrefParam *pparam)
                        tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
                }
 
-               g_return_if_fail (tp && text);
+               cm_return_if_fail (tp && text);
 
                if (text[0] == '\0') {
                        *str = NULL;
@@ -726,6 +795,24 @@ void prefs_set_data_from_text(PrefParam *pparam)
        }
 }
 
+void prefs_set_escaped_data_from_text(PrefParam *pparam)
+{
+       gchar **str;
+
+       cm_return_if_fail(*pparam->widget != NULL);
+
+       switch (pparam->type) {
+       case P_STRING:
+               str = (gchar **)pparam->data;
+               g_free(*str);
+               *str = pref_get_pref_from_textview(GTK_TEXT_VIEW(*pparam->widget));
+               break;
+       default:
+               g_warning("Invalid escaped PrefType for GtkText widget: %d\n",
+                         pparam->type);
+       }
+}
+
 void prefs_set_text(PrefParam *pparam)
 {
        gchar *buf, *sp, *bufp;
@@ -734,7 +821,7 @@ void prefs_set_text(PrefParam *pparam)
        GtkTextBuffer *buffer;
        GtkTextIter iter;
 
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_STRING:
@@ -759,7 +846,7 @@ void prefs_set_text(PrefParam *pparam)
 
                text = GTK_TEXT_VIEW(*pparam->widget);
                buffer = gtk_text_view_get_buffer(text);
-               gtk_text_buffer_set_text(buffer, "\0", -1);
+               gtk_text_buffer_set_text(buffer, "", -1);
                gtk_text_buffer_get_start_iter(buffer, &iter);
                gtk_text_buffer_insert(buffer, &iter, buf, -1);
                break;
@@ -769,10 +856,28 @@ void prefs_set_text(PrefParam *pparam)
        }
 }
 
+void prefs_set_text_from_escaped(PrefParam *pparam)
+{
+       gchar **str;
+
+       cm_return_if_fail(*pparam->widget != NULL);
+
+       switch (pparam->type) {
+       case P_STRING:
+               str = (gchar **)pparam->data;
+               pref_set_textview_from_pref(GTK_TEXT_VIEW(*pparam->widget),
+                                *str ? *str : "");
+               break;
+       default:
+               g_warning("Invalid escaped PrefType for GtkTextView widget: %d\n",
+                         pparam->type);
+       }
+}
+
 void prefs_set_data_from_toggle(PrefParam *pparam)
 {
-       g_return_if_fail(pparam->type == P_BOOL);
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(pparam->type == P_BOOL);
+       cm_return_if_fail(*pparam->widget != NULL);
        
        *((gboolean *)pparam->data) =
                gtk_toggle_button_get_active(GTK_TOGGLE_BUTTON(*pparam->widget));
@@ -780,8 +885,8 @@ void prefs_set_data_from_toggle(PrefParam *pparam)
 
 void prefs_set_toggle(PrefParam *pparam)
 {
-       g_return_if_fail(pparam->type == P_BOOL);
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(pparam->type == P_BOOL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        gtk_toggle_button_set_active(GTK_TOGGLE_BUTTON(*pparam->widget),
                                     *((gboolean *)pparam->data));
@@ -789,7 +894,7 @@ void prefs_set_toggle(PrefParam *pparam)
 
 void prefs_set_data_from_spinbtn(PrefParam *pparam)
 {
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_INT:
@@ -810,7 +915,7 @@ void prefs_set_data_from_spinbtn(PrefParam *pparam)
 
 void prefs_set_spinbtn(PrefParam *pparam)
 {
-       g_return_if_fail(*pparam->widget != NULL);
+       cm_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_INT:
@@ -831,7 +936,9 @@ static GSList *prefs_pages = NULL;
 
 void prefs_gtk_open(void)
 {
-       prefswindow_open(_("Preferences"), prefs_pages, NULL);
+       prefswindow_open(_("Preferences"), prefs_pages, NULL,
+                       &prefs_common.prefswin_width, &prefs_common.prefswin_height,
+                       NULL, NULL);
 }
 
 void prefs_gtk_register_page(PrefsPage *page)
@@ -843,3 +950,160 @@ void prefs_gtk_unregister_page(PrefsPage *page)
 {
        prefs_pages = g_slist_remove(prefs_pages, page);
 }
+
+static void prefs_destroy_whole_cache(gpointer to_free)
+{      
+       GHashTable *table = (GHashTable *)to_free;
+       g_hash_table_destroy(table);
+}
+
+static void prefs_destroy_file_cache(gpointer to_free)
+{      
+       GHashTable *table = (GHashTable *)to_free;
+       g_hash_table_destroy(table);
+}
+
+static int prefs_cache_sections(GHashTable *file_cache, const gchar *rcfile)
+{
+       FILE *fp = NULL;
+       gchar buf[PREFSBUFSIZE];
+       GHashTable *section_cache = NULL;
+
+       if (rcfile)
+               fp = g_fopen(rcfile, "rb");
+       if (!fp) {
+               debug_print("cache: %s: %s\n", rcfile?rcfile:"(null)", g_strerror(errno));
+               return -1;
+       }
+       
+#ifdef HAVE_FGETS_UNLOCKED
+       flockfile(fp);
+#endif
+       
+       while (SC_FGETS(buf, sizeof(buf), fp) != NULL) {
+               strretchomp(buf);
+               if (buf[0] == '\0')
+                       continue;
+               if (buf[0] == '#')
+                       continue; /* comment */
+               if (buf[0] == '[') { /* new section */
+                       gchar *blockname = g_strdup(buf+1);
+
+                       if (strrchr(blockname, ']'))
+                               *strrchr(blockname, ']') = '\0';
+
+                       if ((section_cache = g_hash_table_lookup(file_cache, blockname)) == NULL) {
+                               debug_print("new section '%s'\n", blockname);
+                               section_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+                                               g_free, NULL);
+                               g_hash_table_insert(file_cache, 
+                                       blockname, section_cache);
+                       } else {
+                               debug_print("section '%s' already done\n", blockname);
+                               g_free(blockname);
+                               section_cache = NULL;
+                               continue;
+                       }
+               } else {
+                       if (!section_cache) {
+                               debug_print("skipping stuff %s with no section\n", buf);
+                               continue;
+                       } else {
+                               gchar *pref;
+                               
+                               if (!strchr(buf, '=')) {
+                                       /* plugins do differently */
+                                       continue;
+                               }
+                               pref = g_strdup(buf);
+                               
+                               //debug_print("new pref '%s'\n", pref);
+                               g_hash_table_insert(section_cache, pref, GINT_TO_POINTER(1));
+                       }
+               }
+       }
+#ifdef HAVE_FGETS_UNLOCKED
+       funlockfile(fp);
+#endif
+       fclose(fp);
+       return 0;
+}
+
+static int prefs_cache(const gchar *rcfile)
+{
+       GHashTable *file_cache = g_hash_table_new_full(g_str_hash, g_str_equal, 
+                                       g_free, prefs_destroy_file_cache);
+       
+       debug_print("new file '%s'\n", rcfile?rcfile:"(null)");
+       g_hash_table_insert(whole_cache, g_strdup(rcfile), file_cache);
+       
+       return prefs_cache_sections(file_cache, rcfile);
+}
+
+void prefs_prepare_cache(void)
+{
+       gchar *clawsrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, COMMON_RC, NULL);
+       gchar *folderitemrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, FOLDERITEM_RC, NULL);
+       gchar *accountrc = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S, ACCOUNT_RC, NULL);
+       
+       if (whole_cache == NULL) {
+               whole_cache = g_hash_table_new_full(g_str_hash, g_str_equal,
+                               g_free, prefs_destroy_whole_cache);
+       } else {
+               debug_print("already cached\n");
+               g_free(clawsrc);
+               g_free(folderitemrc);
+               g_free(accountrc);
+               return;
+       }
+       if (prefs_cache(clawsrc) < 0 ||
+           prefs_cache(folderitemrc) < 0 ||
+           prefs_cache(accountrc) < 0)
+               prefs_destroy_cache();
+
+       g_free(clawsrc);
+       g_free(folderitemrc);
+       g_free(accountrc);
+}
+
+void prefs_destroy_cache(void)
+{
+       if (!whole_cache) {
+               debug_print("no cache\n");
+               return;
+       }
+       debug_print("destroying cache\n");
+       g_hash_table_destroy(whole_cache);
+       whole_cache = NULL;
+       return;
+}
+
+static void prefs_parse_cache(gpointer key, gpointer value, gpointer user_data)
+{
+       gchar *pref = (gchar *)key;
+
+       PrefParam *param = (PrefParam *)user_data;
+       
+       prefs_config_parse_one_line(param, pref);
+}
+
+static gboolean prefs_read_config_from_cache(PrefParam *param, const gchar *label,
+                              const gchar *rcfile) 
+{
+       GHashTable *sections_table = NULL;
+       GHashTable *values_table = NULL;
+       sections_table = g_hash_table_lookup(whole_cache, rcfile);
+       
+       if (sections_table == NULL) {
+               g_warning("Can't find %s in the whole cache\n", rcfile?rcfile:"(null)");
+               return FALSE;
+       }
+       values_table = g_hash_table_lookup(sections_table, label);
+       
+       if (values_table == NULL) {
+               debug_print("no '%s' section in '%s' cache\n", label?label:"(null)", rcfile?rcfile:"(null)");
+               return TRUE;
+       }
+       g_hash_table_foreach(values_table, prefs_parse_cache, param);
+       return TRUE;
+}