inital gtk2 patch
[claws.git] / src / prefs_gtk.c
index 7ee4238..8797e96 100644 (file)
 #include "prefs_gtk.h"
 #include "utils.h"
 #include "gtkutils.h"
+#include "passcrypt.h"
+#include "base64.h"
+#include "codeconv.h"
+
+#define CL(x)  (((gulong) (x) >> (gulong) 8) & 0xFFUL)
+#define RGB_FROM_GDK_COLOR(c) \
+       ((CL(c.red)   << (gulong) 16) | \
+        (CL(c.green) << (gulong)  8) | \
+        (CL(c.blue)))
 
 typedef enum
 {
@@ -95,6 +104,7 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
        gint i;
        gint name_len;
        const gchar *value;
+       GdkColor color;
 
        for (i = 0; param[i].name != NULL; i++) {
                name_len = strlen(param[i].name);
@@ -107,10 +117,23 @@ 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("");
+                       if (!tmp) {
+                               g_warning("faild to convert character set.");
+                               tmp = g_strdup(value);
+                       }
                        g_free(*((gchar **)param[i].data));
-                       *((gchar **)param[i].data) =
-                               *value ? g_strdup(value) : NULL;
+                       *((gchar **)param[i].data) = tmp;
                        break;
+               }
                case P_INT:
                        *((gint *)param[i].data) =
                                (gint)atoi(value);
@@ -128,6 +151,29 @@ void prefs_config_parse_one_line(PrefParam *param, const gchar *buf)
                        *((gushort *)param[i].data) =
                                (gushort)atoi(value);
                        break;
+               case P_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); 
+                       break;
+               case P_PASSWORD:
+                       g_free(*((gchar **)param[i].data));
+                       if (value[0] == '!') {
+                               gchar tmp[1024];
+                               gint len;
+
+                               len = base64_decode(tmp, &value[1], strlen(value) - 1);
+                               passcrypt_decrypt(tmp, len);
+                               tmp[len] = '\0';
+                               *((gchar **)param[i].data) =
+                                       *tmp ? g_strdup(tmp) : NULL;
+                       } else {
+                               *((gchar **)param[i].data) =
+                                       *value ? g_strdup(value) : NULL;
+                       }
+                       break;
                default:
                        break;
                }
@@ -224,10 +270,24 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
        for (i = 0; param[i].name != NULL; i++) {
                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)
+                                       tmp = g_strdup(*((gchar **)param[i].data));
+                       }
+
                        g_snprintf(buf, sizeof(buf), "%s=%s\n", param[i].name,
-                                  *((gchar **)param[i].data) ?
-                                  *((gchar **)param[i].data) : "");
+                                  tmp ? tmp : "");
+
+                       g_free(tmp);
                        break;
+               }
                case P_INT:
                        g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
                                   *((gint *)param[i].data));
@@ -244,6 +304,30 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
                        g_snprintf(buf, sizeof(buf), "%s=%d\n", param[i].name,
                                   *((gushort *)param[i].data));
                        break;
+               case P_COLOR:
+                       g_snprintf(buf, sizeof buf,  "%s=#%6.6lx\n", param[i].name,
+                                  *((gulong *) param[i].data));
+                       break;
+               case P_PASSWORD:
+                       {
+                               gchar *tmp = NULL, tmp2[1024] = {0};
+
+                               tmp = *((gchar **)param[i].data);
+                               if (tmp) {
+                                       gint len;
+
+                                       tmp = g_strdup(tmp);
+                                       len = strlen(tmp);
+                                       passcrypt_encrypt(tmp, len);
+                                       base64_encode(tmp2, tmp, len);
+                                       g_free(tmp);
+                                       tmp = tmp2;
+                               }
+                               g_snprintf(buf, sizeof(buf), "%s=!%s\n", param[i].name,
+                                          tmp ?
+                                          tmp : "");
+                       }
+                       break;
                default:
                        buf[0] = '\0';
                }
@@ -262,6 +346,7 @@ gint prefs_write_param(PrefParam *param, FILE *fp)
 void prefs_set_default(PrefParam *param)
 {
        gint i;
+       GdkColor color;
 
        g_return_if_fail(param != NULL);
 
@@ -269,12 +354,27 @@ void prefs_set_default(PrefParam *param)
                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))
-                                       *((gchar **)param[i].data) =
-                                               g_strdup(g_getenv(param[i].defval + 4));
-                               else if (param[i].defval[0] == '~')
+                               if (!strncasecmp(param[i].defval, "ENV_", 4)) {
+                                       const gchar *envstr;
+                                       gchar *tmp;
+
+                                       envstr = g_getenv(param[i].defval + 4);
+                                       tmp = envstr && *envstr ?
+                                               conv_codeset_strdup(envstr,
+                                                                   conv_get_current_charset_str(),
+                                                                   CS_UTF_8)
+                                               : g_strdup("");
+                                       if (!tmp) {
+                                               g_warning("faild to convert character set.");
+                                               tmp = g_strdup(envstr);
+                                       }
+                                       *((gchar **)param[i].data) = tmp;
+                               } else if (param[i].defval[0] == '~')
                                        *((gchar **)param[i].data) =
                                                g_strconcat(get_home_dir(),
                                                            param[i].defval + 1,
@@ -318,6 +418,16 @@ void prefs_set_default(PrefParam *param)
                        else
                                *((gushort *)param[i].data) = 0;
                        break;
+               case P_COLOR:
+                       if (param[i].defval != NULL && gdk_color_parse(param[i].defval, &color))
+                               *((gulong *)param[i].data) =
+                                       RGB_FROM_GDK_COLOR(color);
+                       else if (param[i].defval)
+                               /* be compatible and accept ints */
+                               *((gulong *)param[i].data) = strtoul(param[i].defval, 0, 10); 
+                       else
+                               *((gulong *)param[i].data) = 0; 
+                       break;
                default:
                        break;
                }
@@ -335,6 +445,7 @@ void prefs_free(PrefParam *param)
 
                switch (param[i].type) {
                case P_STRING:
+               case P_PASSWORD:
                        g_free(*((gchar **)param[i].data));
                        break;
                default:
@@ -356,7 +467,7 @@ void prefs_dialog_create(PrefsDialog *dialog)
 
        g_return_if_fail(dialog != NULL);
 
-       window = gtk_window_new (GTK_WINDOW_DIALOG);
+       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);
@@ -445,6 +556,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)) {
                                        str_data = g_strdup(g_getenv(param[i].defval + 4));
@@ -494,6 +607,7 @@ void prefs_set_dialog_to_default(PrefParam *param)
                        tmpparam.data = &enum_data;
                        break;
                case P_OTHER:
+               default:
                        break;
                }
                tmpparam.widget_set_func(&tmpparam);
@@ -504,7 +618,8 @@ void prefs_set_dialog_to_default(PrefParam *param)
 
 void prefs_set_data_from_entry(PrefParam *pparam)
 {
-       gchar **str, *entry_str;
+       gchar **str;
+       const gchar *entry_str;
 
        g_return_if_fail(*pparam->widget != NULL);
 
@@ -512,6 +627,7 @@ void prefs_set_data_from_entry(PrefParam *pparam)
 
        switch (pparam->type) {
        case P_STRING:
+       case P_PASSWORD:
                str = (gchar **)pparam->data;
                g_free(*str);
                *str = entry_str[0] ? g_strdup(entry_str) : NULL;
@@ -536,6 +652,7 @@ void prefs_set_entry(PrefParam *pparam)
 
        switch (pparam->type) {
        case P_STRING:
+       case P_PASSWORD:
                str = (gchar **)pparam->data;
                gtk_entry_set_text(GTK_ENTRY(*pparam->widget),
                                   *str ? *str : "");
@@ -557,17 +674,30 @@ void prefs_set_entry(PrefParam *pparam)
 void prefs_set_data_from_text(PrefParam *pparam)
 {
        gchar **str;
-       gchar *text, *tp;
+       gchar *text = NULL, *tp = NULL;
        gchar *tmp, *tmpp;
 
        g_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_STRING:
+       case P_PASSWORD:
                str = (gchar **)pparam->data;
                g_free(*str);
-               tp = text = gtk_editable_get_chars
-                       (GTK_EDITABLE(*pparam->widget), 0, -1);
+               if (GTK_IS_EDITABLE(*pparam->widget)) {   /* need? */
+                       tp = text = gtk_editable_get_chars
+                                       (GTK_EDITABLE(*pparam->widget), 0, -1);
+               } else if (GTK_IS_TEXT_VIEW(*pparam->widget)) {
+                       GtkTextView *textview = GTK_TEXT_VIEW(*pparam->widget);
+                       GtkTextBuffer *buffer = gtk_text_view_get_buffer(textview);
+                       GtkTextIter start, end;
+                       gtk_text_buffer_get_start_iter(buffer, &start);
+                       gtk_text_buffer_get_iter_at_offset(buffer, &end, -1);
+                       tp = text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+               }
+
+               g_return_if_fail (tp && text);
+
                if (text[0] == '\0') {
                        *str = NULL;
                        g_free(text);
@@ -598,12 +728,15 @@ void prefs_set_text(PrefParam *pparam)
 {
        gchar *buf, *sp, *bufp;
        gchar **str;
-       GtkText *text;
+       GtkTextView *text;
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
 
        g_return_if_fail(*pparam->widget != NULL);
 
        switch (pparam->type) {
        case P_STRING:
+       case P_PASSWORD:
                str = (gchar **)pparam->data;
                if (*str) {
                        bufp = buf = alloca(strlen(*str) + 1);
@@ -622,14 +755,14 @@ void prefs_set_text(PrefParam *pparam)
                } else
                        buf = "";
 
-               text = GTK_TEXT(*pparam->widget);
-               gtk_text_set_point(text, 0);
-               gtk_text_forward_delete(text, gtk_text_get_length(text));
-               gtk_text_set_point(text, 0);
-               gtk_text_insert(text, NULL, NULL, NULL, buf, -1);
+               text = GTK_TEXT_VIEW(*pparam->widget);
+               buffer = gtk_text_view_get_buffer(text);
+               gtk_text_buffer_set_text(buffer, "\0", -1);
+               gtk_text_buffer_get_start_iter(buffer, &iter);
+               gtk_text_buffer_insert(buffer, &iter, buf, -1);
                break;
        default:
-               g_warning("Invalid PrefType for GtkText widget: %d\n",
+               g_warning("Invalid PrefType for GtkTextView widget: %d\n",
                          pparam->type);
        }
 }
@@ -691,3 +824,20 @@ void prefs_set_spinbtn(PrefParam *pparam)
                          pparam->type);
        }
 }
+
+static GSList *prefs_pages = NULL;
+
+void prefs_gtk_open(void)
+{
+       prefswindow_open(_("Preferences"), prefs_pages, NULL);
+}
+
+void prefs_gtk_register_page(PrefsPage *page)
+{
+       prefs_pages = g_slist_append(prefs_pages, page);
+}
+
+void prefs_gtk_unregister_page(PrefsPage *page)
+{
+       prefs_pages = g_slist_remove(prefs_pages, page);
+}