0.9.6claws72
[claws.git] / src / textview.c
index 7e7ec51b914337ef4bddfeeaa03292b9fac7c4e0..3c0d6f57bbda6bd80f38020f6bd23cebfe8f9dcb 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2002 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2003 Hiroyuki Yamamoto
  *
  * 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
 #include "addressbook.h"
 #include "displayheader.h"
 #include "account.h"
-
-#define FONT_LOAD(font, s) \
-{ \
-       gchar *fontstr, *p; \
- \
-       Xstrdup_a(fontstr, s, ); \
-       if ((p = strchr(fontstr, ',')) != NULL) *p = '\0'; \
-       font = gdk_font_load(fontstr); \
-       if (!font) \
-               g_warning("Couldn't load the font '%s'\n", fontstr); \
-}
+#include "mimeview.h"
 
 typedef struct _RemoteURI      RemoteURI;
 
@@ -78,6 +68,13 @@ static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
 };
 
+static GdkColor signature_color = {
+       (gulong)0,
+       (gushort)0x7fff,
+       (gushort)0x7fff,
+       (gushort)0x7fff
+};
+       
 static GdkColor uri_color = {
        (gulong)0,
        (gushort)0,
@@ -101,6 +98,27 @@ static GdkColor error_color = {
 };
 #endif
 
+static GdkColor good_sig_color = {
+       (gulong)0,
+       (gushort)0,
+       (gushort)0xbfff,
+       (gushort)0
+};
+
+static GdkColor nocheck_sig_color = {
+       (gulong)0,
+       (gushort)0,
+       (gushort)0,
+       (gushort)0xcfff
+};
+
+static GdkColor bad_sig_color = {
+       (gulong)0,
+       (gushort)0xefff,
+       (gushort)0,
+       (gushort)0
+};
+
 static GdkFont *text_sb_font;
 static GdkFont *text_mb_font;
 static gint text_sb_font_orig_ascent;
@@ -113,14 +131,11 @@ static void textview_show_ertf            (TextView       *textview,
                                         FILE           *fp,
                                         CodeConverter  *conv);
 static void textview_add_part          (TextView       *textview,
-                                        MimeInfo       *mimeinfo,
-                                        FILE           *fp);
+                                        MimeInfo       *mimeinfo);
 static void textview_add_parts         (TextView       *textview,
-                                        MimeInfo       *mimeinfo,
-                                        FILE           *fp);
+                                        MimeInfo       *mimeinfo);
 static void textview_write_body                (TextView       *textview,
                                         MimeInfo       *mimeinfo,
-                                        FILE           *fp,
                                         const gchar    *charset);
 static void textview_show_html         (TextView       *textview,
                                         FILE           *fp,
@@ -128,9 +143,9 @@ static void textview_show_html              (TextView       *textview,
 static void textview_write_line                (TextView       *textview,
                                         const gchar    *str,
                                         CodeConverter  *conv);
-static void textview_write_link         (TextView      *textview,
-                                         const gchar    *url,
+static void textview_write_link                (TextView       *textview,
                                         const gchar    *str,
+                                        const gchar    *uri,
                                         CodeConverter  *conv);
 static GPtrArray *textview_scan_header (TextView       *textview,
                                         FILE           *fp);
@@ -168,7 +183,7 @@ TextView *textview_create(void)
        GtkWidget *text_sb;
        GtkWidget *text_mb;
 
-       debug_print(_("Creating text view...\n"));
+       debug_print("Creating text view...\n");
        textview = g_new0(TextView, 1);
 
        scrolledwin_sb = gtk_scrolled_window_new(NULL, NULL);
@@ -195,7 +210,7 @@ TextView *textview_create(void)
                GtkStyle *style;
                GdkFont *font;
 
-               FONT_LOAD(font, prefs_common.normalfont);
+               font = gtkut_font_load_from_fontset(prefs_common.normalfont);
                if (font) {
                        style = gtk_style_copy(text_sb->style);
                        gdk_font_unref(style->font);
@@ -288,43 +303,50 @@ void textview_update_message_colors(void)
                                               &quote_colors[2]);
                gtkut_convert_int_to_gdk_color(prefs_common.uri_col,
                                               &uri_color);
+               gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
+                                              &signature_color);
        } else {
                quote_colors[0] = quote_colors[1] = quote_colors[2] = 
-                       uri_color = emphasis_color = black;
+                       uri_color = emphasis_color = signature_color = black;
        }
 }
 
 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
                           const gchar *file)
 {
-       GtkSText *text = GTK_STEXT(textview->text);
+       GtkSText *text;
        FILE *fp;
        const gchar *charset = NULL;
-       GPtrArray *headers = NULL;
 
        if ((fp = fopen(file, "rb")) == NULL) {
                FILE_OP_ERROR(file, "fopen");
                return;
        }
 
-       if (prefs_common.force_charset)
+       if (textview->messageview->forced_charset)
+               charset = textview->messageview->forced_charset;
+       else if (prefs_common.force_charset)
                charset = prefs_common.force_charset;
-       else if (mimeinfo->charset)
-               charset = mimeinfo->charset;
+       else
+               charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
+
        textview_set_font(textview, charset);
        textview_clear(textview);
 
+       text = GTK_STEXT(textview->text);
+
        gtk_stext_freeze(text);
 
-       if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) perror("fseek");
+/*
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0) perror("fseek");
        headers = textview_scan_header(textview, fp);
        if (headers) {
                textview_show_header(textview, headers);
                procheader_header_array_destroy(headers);
                textview->body_pos = gtk_stext_get_length(text);
        }
-
-       textview_add_parts(textview, mimeinfo, fp);
+*/
+       textview_add_parts(textview, mimeinfo);
 
        gtk_stext_thaw(text);
 
@@ -333,82 +355,39 @@ void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
 
 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
 {
-       GtkSText *text = GTK_STEXT(textview->text);
-       gchar buf[BUFFSIZE];
-       const gchar *boundary = NULL;
-       gint boundary_len = 0;
+       GtkSText *text;
        const gchar *charset = NULL;
-       GPtrArray *headers = NULL;
 
        g_return_if_fail(mimeinfo != NULL);
        g_return_if_fail(fp != NULL);
 
-       if (mimeinfo->mime_type == MIME_MULTIPART) {
+       if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
+           ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_strcasecmp(mimeinfo->subtype, "rfc822"))) {
                textview_clear(textview);
-               textview_add_parts(textview, mimeinfo, fp);
+               textview_add_parts(textview, mimeinfo);
                return;
        }
 
-       if (mimeinfo->parent && mimeinfo->parent->boundary) {
-               boundary = mimeinfo->parent->boundary;
-               boundary_len = strlen(boundary);
-       }
-
-       if (!boundary && (mimeinfo->mime_type == MIME_TEXT || 
-                         mimeinfo->mime_type == MIME_TEXT_HTML || 
-                         mimeinfo->mime_type == MIME_TEXT_ENRICHED)) {
-       
-               if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0)
-                       perror("fseek");
-               headers = textview_scan_header(textview, fp);
-       } else {
-               if (mimeinfo->mime_type == MIME_TEXT && mimeinfo->parent) {
-                       glong fpos;
-                       MimeInfo *parent = mimeinfo->parent;
-
-                       while (parent->parent) {
-                               if (parent->main &&
-                                   parent->main->mime_type ==
-                                       MIME_MESSAGE_RFC822)
-                                       break;
-                               parent = parent->parent;
-                       }
-
-                       if ((fpos = ftell(fp)) < 0)
-                               perror("ftell");
-                       else if (fseek(fp, parent->fpos, SEEK_SET) < 0)
-                               perror("fseek");
-                       else {
-                               headers = textview_scan_header(textview, fp);
-                               if (fseek(fp, fpos, SEEK_SET) < 0)
-                                       perror("fseek");
-                       }
-               }
-               /* skip MIME part headers */
-               while (fgets(buf, sizeof(buf), fp) != NULL)
-                       if (buf[0] == '\r' || buf[0] == '\n') break;
-       }
-
-       /* display attached RFC822 single text message */
-       if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
-               if (headers) procheader_header_array_destroy(headers);
-               if (!mimeinfo->sub) {
-                       textview_clear(textview);
-                       return;
-               }
-               headers = textview_scan_header(textview, fp);
-               mimeinfo = mimeinfo->sub;
-       }
-
-       if (prefs_common.force_charset)
+       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
+               perror("fseek");
+/*
+       headers = textview_scan_header(textview, fp);
+*/
+       if (textview->messageview->forced_charset)
+               charset = textview->messageview->forced_charset;
+       else if (prefs_common.force_charset)
                charset = prefs_common.force_charset;
-       else if (mimeinfo->charset)
-               charset = mimeinfo->charset;
+       else
+               charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
+
        textview_set_font(textview, charset);
 
-       textview_clear(textview);
+       text = GTK_STEXT(textview->text);
+
        gtk_stext_freeze(text);
+       textview_clear(textview);
 
+/*
        if (headers) {
                textview_show_header(textview, headers);
                procheader_header_array_destroy(headers);
@@ -416,110 +395,158 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                if (!mimeinfo->main)
                        gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
        }
-
-       if (mimeinfo->mime_type == MIME_MULTIPART || mimeinfo->main)
-               textview_add_parts(textview, mimeinfo, fp);
+*/
+       if (mimeinfo->type == MIMETYPE_MULTIPART)
+               textview_add_parts(textview, mimeinfo);
        else
-               textview_write_body(textview, mimeinfo, fp, charset);
+               textview_write_body(textview, mimeinfo, charset);
 
        gtk_stext_thaw(text);
 }
 
-static void textview_add_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
+static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 {
        GtkSText *text = GTK_STEXT(textview->text);
        gchar buf[BUFFSIZE];
-       const gchar *boundary = NULL;
-       gint boundary_len = 0;
        const gchar *charset = NULL;
        GPtrArray *headers = NULL;
 
        g_return_if_fail(mimeinfo != NULL);
-       g_return_if_fail(fp != NULL);
 
-       if (mimeinfo->mime_type == MIME_MULTIPART) return;
+       if (mimeinfo->type == MIMETYPE_MULTIPART) return;
 
-       if (!mimeinfo->parent &&
-           mimeinfo->mime_type != MIME_TEXT &&
-           mimeinfo->mime_type != MIME_TEXT_HTML &&
-           mimeinfo->mime_type != MIME_TEXT_ENRICHED)
-               return;
-
-       if (fseek(fp, mimeinfo->fpos, SEEK_SET) < 0) {
-               perror("fseek");
-               return;
-       }
-
-       if (mimeinfo->parent && mimeinfo->parent->boundary) {
-               boundary = mimeinfo->parent->boundary;
-               boundary_len = strlen(boundary);
-       }
+       if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_strcasecmp(mimeinfo->subtype, "rfc822")) {
+               FILE *fp;
 
-       while (fgets(buf, sizeof(buf), fp) != NULL)
-               if (buf[0] == '\r' || buf[0] == '\n') break;
-
-       if (mimeinfo->mime_type == MIME_MESSAGE_RFC822) {
+               fp = fopen(mimeinfo->filename, "rb");
+               fseek(fp, mimeinfo->offset, SEEK_SET);
                headers = textview_scan_header(textview, fp);
                if (headers) {
-                       gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
+                       gtk_stext_freeze(text);
+                       if (gtk_stext_get_length(text) > 0)
+                               gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
                        textview_show_header(textview, headers);
                        procheader_header_array_destroy(headers);
+                       gtk_stext_thaw(text);
                }
+               fclose(fp);
                return;
        }
 
        gtk_stext_freeze(text);
 
-       if (mimeinfo->filename || mimeinfo->name)
-               g_snprintf(buf, sizeof(buf), "\n[%s  %s (%d bytes)]\n",
-                          mimeinfo->filename ? mimeinfo->filename :
-                          mimeinfo->name,
-                          mimeinfo->content_type, mimeinfo->size);
+       if (g_hash_table_lookup(mimeinfo->parameters, "name") != NULL)
+               g_snprintf(buf, sizeof(buf), "\n[%s  %s/%s (%d bytes)]\n",
+                          (gchar *) g_hash_table_lookup(mimeinfo->parameters, "name"),
+                          procmime_get_type_str(mimeinfo->type),
+                          mimeinfo->subtype, mimeinfo->length);
        else
-               g_snprintf(buf, sizeof(buf), "\n[%s (%d bytes)]\n",
-                          mimeinfo->content_type, mimeinfo->size);
+               g_snprintf(buf, sizeof(buf), "\n[%s/%s (%d bytes)]\n",
+                          procmime_get_type_str(mimeinfo->type),
+                          mimeinfo->subtype, mimeinfo->length);
 
-       if (mimeinfo->mime_type != MIME_TEXT &&
-           mimeinfo->mime_type != MIME_TEXT_HTML &&
-           mimeinfo->mime_type != MIME_TEXT_ENRICHED) {
+       if (mimeinfo->type != MIMETYPE_TEXT) {
                gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
        } else {
-               if (!mimeinfo->main &&
-                   mimeinfo->parent &&
-                   mimeinfo->parent->children != mimeinfo)
-                       gtk_stext_insert(text, NULL, NULL, NULL, buf, -1);
-               else
+               if (prefs_common.display_header && (gtk_stext_get_length(text) > 0))
                        gtk_stext_insert(text, NULL, NULL, NULL, "\n", 1);
-               if (prefs_common.force_charset)
+               if (textview->messageview->forced_charset)
+                       charset = textview->messageview->forced_charset;
+               else if (prefs_common.force_charset)
                        charset = prefs_common.force_charset;
-               else if (mimeinfo->charset)
-                       charset = mimeinfo->charset;
-               textview_write_body(textview, mimeinfo, fp, charset);
+               else
+                       charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
+
+               textview_write_body(textview, mimeinfo, charset);
        }
 
        gtk_stext_thaw(text);
 }
 
-static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
+#if 0
+static gboolean add_parts_func(GNode *node, gpointer data)
 {
-       gint level;
+       MimeInfo *mimeinfo = (MimeInfo *) node->data;
+       TextView *textview = (TextView *) data;
+
+       g_return_val_if_fail(mimeinfo != NULL, FALSE);
 
+       textview_add_part(textview, mimeinfo);
+
+       return FALSE;
+}
+
+static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
+{
        g_return_if_fail(mimeinfo != NULL);
-       g_return_if_fail(fp != NULL);
 
-       level = mimeinfo->level;
+       g_node_traverse(mimeinfo->node, G_PRE_ORDER, G_TRAVERSE_ALL, -1, add_parts_func, textview);
+}
+#endif
+
+static void recursive_add_parts(TextView *textview, GNode *node)
+{
+        GNode * iter;
+       MimeInfo *mimeinfo;
+        
+        mimeinfo = (MimeInfo *) node->data;
+        
+        textview_add_part(textview, mimeinfo);
+        
+        if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
+            (mimeinfo->type != MIMETYPE_MESSAGE))
+                return;
+        
+        if (strcasecmp(mimeinfo->subtype, "alternative") == 0) {
+                GNode * prefered_body;
+                int prefered_score;
+                
+                /*
+                  text/plain : score 3
+                  text/ *    : score 2
+                  other      : score 1
+                */
+                prefered_body = NULL;
+                prefered_score = 0;
+                
+                for(iter = g_node_first_child(node) ; iter != NULL ;
+                    iter = g_node_next_sibling(iter)) {
+                        int score;
+                        MimeInfo * submime;
+                        
+                        score = 1;
+                        submime = (MimeInfo *) iter->data;
+                        if (submime->type == MIMETYPE_TEXT)
+                                score = 2;
+                        
+                        if (submime->subtype != NULL) {
+                                if (strcasecmp(submime->subtype, "plain") == 0)
+                                        score = 3;
+                        }
+                        
+                        if (score > prefered_score) {
+                                prefered_score = score;
+                                prefered_body = iter;
+                        }
+                }
+                
+                if (prefered_body != NULL) {
+                        recursive_add_parts(textview, prefered_body);
+                }
+        }
+        else {
+                for(iter = g_node_first_child(node) ; iter != NULL ;
+                    iter = g_node_next_sibling(iter)) {
+                        recursive_add_parts(textview, iter);
+                }
+        }
+}
 
-       for (;;) {
-               textview_add_part(textview, mimeinfo, fp);
-               if (mimeinfo->parent && mimeinfo->parent->content_type &&
-                   !strcasecmp(mimeinfo->parent->content_type,
-                               "multipart/alternative"))
-                       mimeinfo = mimeinfo->parent->next;
-               else
-                       mimeinfo = procmime_mimeinfo_next(mimeinfo);
-               if (!mimeinfo || mimeinfo->level <= level)
-                       break;
-       }
+static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
+{
+       g_return_if_fail(mimeinfo != NULL);
+        
+        recursive_add_parts(textview, mimeinfo->node);
 }
 
 #define TEXT_INSERT(str) \
@@ -544,9 +571,6 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        TEXT_INSERT(_("To display this part as a text message, select "));
        TEXT_INSERT(_("`Display as text', or press `t' key.\n\n"));
 
-       TEXT_INSERT(_("To display this part as an image, select "));
-       TEXT_INSERT(_("`Display image', or press `i' key.\n\n"));
-
        TEXT_INSERT(_("To open this part with external program, select "));
        TEXT_INSERT(_("`Open' or `Open with...', "));
        TEXT_INSERT(_("or double-click, or click the center button, "));
@@ -555,35 +579,10 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        gtk_stext_thaw(text);
 }
 
-#if USE_GPGME
-void textview_show_signature_part(TextView *textview, MimeInfo *partinfo)
-{
-       GtkSText *text;
-
-       if (!partinfo) return;
-
-       textview_set_font(textview, NULL);
-       text = GTK_STEXT(textview->text);
-       textview_clear(textview);
-
-       gtk_stext_freeze(text);
-
-       if (partinfo->sigstatus_full == NULL) {
-               TEXT_INSERT(_("This signature has not been checked yet.\n"));
-               TEXT_INSERT(_("To check it, pop up the context menu with\n"));
-               TEXT_INSERT(_("right click and select `Check signature'.\n"));
-       } else {
-               TEXT_INSERT(partinfo->sigstatus_full);
-       }
-
-       gtk_stext_thaw(text);
-}
-#endif /* USE_GPGME */
-
 #undef TEXT_INSERT
 
 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
-                               FILE *fp, const gchar *charset)
+                               const gchar *charset)
 {
        FILE *tmpfp;
        gchar buf[BUFFSIZE];
@@ -591,15 +590,42 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo,
 
        conv = conv_code_converter_new(charset);
 
-       tmpfp = procmime_decode_content(NULL, fp, mimeinfo);
-       if (tmpfp) {
-               if (mimeinfo->mime_type == MIME_TEXT_HTML)
+       textview->is_in_signature = FALSE;
+
+       if(mimeinfo->encoding_type != ENC_BINARY && 
+          mimeinfo->encoding_type != ENC_7BIT && 
+          mimeinfo->encoding_type != ENC_8BIT)
+               procmime_decode_content(mimeinfo);
+
+       if (!g_strcasecmp(mimeinfo->subtype, "html")) {
+               gchar *filename;
+               
+               filename = procmime_get_tmp_file_name(mimeinfo);
+               if (procmime_get_part(filename, mimeinfo) == 0) {
+                       tmpfp = fopen(filename, "rb");
                        textview_show_html(textview, tmpfp, conv);
-               else if (mimeinfo->mime_type == MIME_TEXT_ENRICHED)
+                       fclose(tmpfp);
+                       unlink(filename);
+               }
+               g_free(filename);
+       } else if (!g_strcasecmp(mimeinfo->subtype, "enriched")) {
+               gchar *filename;
+               
+               filename = procmime_get_tmp_file_name(mimeinfo);
+               if (procmime_get_part(filename, mimeinfo) == 0) {
+                       tmpfp = fopen(filename, "rb");
                        textview_show_ertf(textview, tmpfp, conv);
-               else
-                       while (fgets(buf, sizeof(buf), tmpfp) != NULL)
-                               textview_write_line(textview, buf, conv);
+                       fclose(tmpfp);
+                       unlink(filename);
+               }
+               g_free(filename);
+       } else {
+               tmpfp = fopen(mimeinfo->filename, "rb");
+               fseek(tmpfp, mimeinfo->offset, SEEK_SET);
+               debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
+               while ((fgets(buf, sizeof(buf), tmpfp) != NULL) && 
+                      (ftell(tmpfp) <= mimeinfo->offset + mimeinfo->length))
+                       textview_write_line(textview, buf, conv);
                fclose(tmpfp);
        }
 
@@ -611,7 +637,6 @@ static void textview_show_html(TextView *textview, FILE *fp,
 {
        HTMLParser *parser;
        gchar *str;
-       gchar* url = NULL;
 
        parser = html_parser_new(fp, conv);
        g_return_if_fail(parser != NULL);
@@ -619,27 +644,21 @@ static void textview_show_html(TextView *textview, FILE *fp,
        while ((str = html_parse(parser)) != NULL) {
                if (parser->state == HTML_HREF) {
                        /* first time : get and copy the URL */
-                       if (url == NULL) {
+                       if (parser->href == NULL) {
                                /* ALF - the sylpheed html parser returns an empty string,
                                 * if still inside an <a>, but already parsed past HREF */
                                str = strtok(str, " ");
                                if (str) { 
-                                       url = strdup(str);
+                                       parser->href = strdup(str);
                                        /* the URL may (or not) be followed by the
                                         * referenced text */
                                        str = strtok(NULL, "");
                                }       
                        }
-                       if (str != NULL) {
-                               textview_write_link(textview, url, str, NULL);
-                       }
-               } else {
-                       if (url != NULL) {
-                               free(url);
-                               url = NULL;
-                       }
+                       if (str != NULL)
+                               textview_write_link(textview, str, parser->href, NULL);
+               } else
                        textview_write_line(textview, str, NULL);
-               }
        }
        html_parser_destroy(parser);
 }
@@ -649,7 +668,6 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
 {
        ERTFParser *parser;
        gchar *str;
-       gchar* url = NULL;
 
        parser = ertf_parser_new(fp, conv);
        g_return_if_fail(parser != NULL);
@@ -657,6 +675,7 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
        while ((str = ertf_parse(parser)) != NULL) {
                textview_write_line(textview, str, NULL);
        }
+       
        ertf_parser_destroy(parser);
 }
 
@@ -715,6 +734,63 @@ static gchar *make_uri_string(const gchar *bp, const gchar *ep)
 #define IS_ASCII_ALNUM(ch)     (isascii(ch) && isalnum(ch))
 #define IS_QUOTE(ch) ((ch) == '\'' || (ch) == '"')
 
+static GHashTable *create_domain_tab(void)
+{
+       static const gchar *toplvl_domains [] = {
+           "museum", "aero",
+           "arpa", "coop", "info", "name", "biz", "com", "edu", "gov",
+           "int", "mil", "net", "org", "ac", "ad", "ae", "af", "ag",
+           "ai", "al", "am", "an", "ao", "aq", "ar", "as", "at", "au",
+           "aw", "az", "ba", "bb", "bd", "be", "bf", "bg", "bh", "bi",
+           "bj", "bm", "bn", "bo", "br", "bs", "bt", "bv", "bw", "by",
+           "bz", "ca", "cc", "cd", "cf", "cg", "ch", "ci", "ck", "cl",
+           "cm", "cn", "co", "cr", "cu", "cv", "cx", "cy", "cz", "de",
+           "dj", "dk", "dm", "do", "dz", "ec", "ee", "eg", "eh", "er",
+           "es", "et", "fi", "fj", "fk", "fm", "fo", "fr", "ga", "gd",
+           "ge", "gf", "gg", "gh", "gi", "gl", "gm", "gn", "gp", "gq",
+           "gr", "gs", "gt", "gu", "gw", "gy", "hk", "hm", "hn", "hr",
+           "ht", "hu", "id", "ie", "il", "im", "in", "io", "iq", "ir",
+           "is", "it", "je", "jm", "jo", "jp", "ke", "kg", "kh", "ki",
+           "km", "kn", "kp", "kr", "kw", "ky", "kz", "la", "lb", "lc",
+           "li", "lk", "lr", "ls", "lt", "lu", "lv", "ly", "ma", "mc",
+           "md", "mg", "mh", "mk", "ml", "mm", "mn", "mo", "mp", "mq",
+           "mr", "ms", "mt", "mu", "mv", "mw", "mx", "my", "mz", "na",
+           "nc", "ne", "nf", "ng", "ni", "nl", "no", "np", "nr", "nu",
+           "nz", "om", "pa", "pe", "pf", "pg", "ph", "pk", "pl", "pm",
+           "pn", "pr", "ps", "pt", "pw", "py", "qa", "re", "ro", "ru",
+           "rw", "sa", "sb", "sc", "sd", "se", "sg", "sh", "si", "sj",
+           "sk", "sl", "sm", "sn", "so", "sr", "st", "sv", "sy", "sz",
+           "tc", "td", "tf", "tg", "th", "tj", "tk", "tm", "tn", "to",
+           "tp", "tr", "tt", "tv", "tw", "tz", "ua", "ug", "uk", "um",
+           "us", "uy", "uz", "va", "vc", "ve", "vg", "vi", "vn", "vu",
+            "wf", "ws", "ye", "yt", "yu", "za", "zm", "zw" 
+       };
+       gint n;
+       GHashTable *htab = g_hash_table_new(g_stricase_hash, g_stricase_equal);
+       
+       g_return_val_if_fail(htab, NULL);
+       for (n = 0; n < sizeof toplvl_domains / sizeof toplvl_domains[0]; n++) 
+               g_hash_table_insert(htab, (gpointer) toplvl_domains[n], (gpointer) toplvl_domains[n]);
+       return htab;
+}
+
+static gboolean is_toplvl_domain(GHashTable *tab, const gchar *first, const gchar *last)
+{
+       const gint MAX_LVL_DOM_NAME_LEN = 6;
+       gchar buf[MAX_LVL_DOM_NAME_LEN + 1];
+       const gchar *m = buf + MAX_LVL_DOM_NAME_LEN + 1;
+       register gchar *p;
+       
+       if (last - first > MAX_LVL_DOM_NAME_LEN || first > last)
+               return FALSE;
+
+       for (p = buf; p < m &&  first < last; *p++ = *first++)
+               ;
+       *p = 0;
+
+       return g_hash_table_lookup(tab, buf) != NULL;
+}
+
 /* get_email_part() - retrieves an email address. Returns TRUE if succesful */
 static gboolean get_email_part(const gchar *start, const gchar *scanpos,
                               const gchar **bp, const gchar **ep)
@@ -724,6 +800,10 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        gboolean result = FALSE;
        const gchar *bp_ = NULL;
        const gchar *ep_ = NULL;
+       static GHashTable *dom_tab;
+       const gchar *last_dot = NULL;
+       const gchar *prelast_dot = NULL;
+       const gchar *last_tld_char = NULL;
 
        /* the informative part of the email address (describing the name
         * of the email address owner) may contain quoted parts. the
@@ -736,6 +816,10 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        g_return_val_if_fail(bp != NULL, FALSE);
        g_return_val_if_fail(ep != NULL, FALSE);
 
+       if (!dom_tab)
+               dom_tab = create_domain_tab();
+       g_return_val_if_fail(dom_tab, FALSE);   
+
        /* scan start of address */
        for (bp_ = scanpos - 1; bp_ >= start && IS_RFC822_CHAR(*bp_); bp_--)
                ;
@@ -748,18 +832,39 @@ static gboolean get_email_part(const gchar *start, const gchar *scanpos,
        if (bp_ != scanpos) {
                /* scan end of address */
                for (ep_ = scanpos + 1; *ep_ && IS_RFC822_CHAR(*ep_); ep_++)
-                       ;
+                       if (*ep_ == '.') {
+                               prelast_dot = last_dot;
+                               last_dot = ep_;
+                               if (*(last_dot + 1) == '.') {
+                                       if (prelast_dot == NULL)
+                                               return FALSE;
+                                       last_dot = prelast_dot;
+                                       break;
+                               }
+                       }
 
                /* TODO: really should terminate with an alnum? */
                for (; ep_ > scanpos && !IS_ASCII_ALNUM(*ep_); --ep_)
                        ;
                ep_++;
 
-               if (ep_ > scanpos + 1) {
-                       *ep = ep_;
-                       *bp = bp_;
+               if (last_dot == NULL)
+                       return FALSE;
+               if (last_dot >= ep_)
+                       last_dot = prelast_dot;
+               if (last_dot == NULL || (scanpos + 1 >= last_dot))
+                       return FALSE;
+               last_dot++;
+
+               for (last_tld_char = last_dot; last_tld_char < ep_; last_tld_char++)
+                       if (*last_tld_char == '?')
+                               break;
+
+               if (is_toplvl_domain(dom_tab, last_dot, last_tld_char))
                        result = TRUE;
-               }
+
+               *ep = ep_;
+               *bp = bp_;
        }
 
        if (!result) return FALSE;
@@ -888,8 +993,8 @@ static void textview_make_clickable_parts(TextView *textview,
        static struct table parser[] = {
                {"http://",  strcasestr, get_uri_part,   make_uri_string},
                {"https://", strcasestr, get_uri_part,   make_uri_string},
-               {"www.",     strcasestr, get_uri_part,   make_uri_string},
                {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
+               {"www.",     strcasestr, get_uri_part,   make_uri_string},
                {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
                {"@",        strcasestr, get_email_part, make_email_string}
        };
@@ -926,7 +1031,7 @@ static void textview_make_clickable_parts(TextView *textview,
 
                if (scanpos) {
                        /* check if URI can be parsed */
-                       if (parser[last_index].parse(linebuf, scanpos, &bp, &ep)
+                       if (parser[last_index].parse(walk, scanpos, &bp, &ep)
                            && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
                                        ADD_TXT_POS(bp, ep, last_index);
                                        walk = ep;
@@ -971,43 +1076,6 @@ static void textview_make_clickable_parts(TextView *textview,
 
 #undef ADD_TXT_POS
 
-/* This function writes str as a double-clickable link with the given url. */ 
-static void textview_write_link(TextView *textview, const gchar *url,
-                                const gchar *str, CodeConverter *conv)
-{
-    GdkColor *link_color = NULL;
-    RemoteURI* uri;
-    GtkSText *text = GTK_STEXT(textview->text);
-    gchar buf[BUFFSIZE];
-
-    /* this part is taken from textview_write_line. Right now the only place
-     * that calls this function passes NULL for conv, but you never know. */
-#if 0
-    if (!conv)
-           strncpy2(buf, str, sizeof(buf));
-    else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
-                   gtk_stext_insert(text, textview->msgfont,
-                           prefs_common.enable_color
-                           ? &error_color : NULL, NULL,
-                           "*** Warning: code conversion failed ***\n",
-                           -1);
-           return;
-    }
-#endif
-
-    /* this part is based on the code in make_clickable_parts */
-    if (prefs_common.enable_color) {
-       link_color = &uri_color;
-    }
-    uri = g_new(RemoteURI, 1);
-    uri->uri = g_strdup(url);
-    uri->start = gtk_stext_get_point(text);
-    gtk_stext_insert(text, textview->msgfont, link_color, NULL, str,
-                   strlen(str));
-    uri->end = gtk_stext_get_point(text);
-    textview->uri_list = g_slist_append(textview->uri_list, uri);
-}
-
 static void textview_write_line(TextView *textview, const gchar *str,
                                CodeConverter *conv)
 {
@@ -1016,20 +1084,15 @@ static void textview_write_line(TextView *textview, const gchar *str,
        GdkColor *fg_color;
        gint quotelevel = -1;
 
-#if 0
-       if (!conv)
-               strncpy2(buf, str, sizeof(buf));
-       else if (conv_convert(conv, buf, sizeof(buf), str) < 0) {
-               gtk_stext_insert(text, textview->msgfont,
-                               prefs_common.enable_color
-                               ? &error_color : NULL, NULL,
-                               "*** Warning: code conversion failed ***\n",
-                               -1);
-               return;
-       }
-#endif
-       if (!conv || conv_convert(conv, buf, sizeof(buf), str) < 0)
-               strncpy2(buf, str, sizeof(buf));
+       if (!conv) {
+               if (textview->text_is_mb)
+                       conv_localetodisp(buf, sizeof(buf), str);
+               else
+                       strncpy2(buf, str, sizeof(buf));
+       } else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
+               conv_localetodisp(buf, sizeof(buf), str);
+       else if (textview->text_is_mb)
+               conv_unreadable_locale(buf);
 
        strcrchomp(buf);
        if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
@@ -1058,6 +1121,11 @@ static void textview_write_line(TextView *textview, const gchar *str,
        else
                fg_color = &quote_colors[quotelevel];
 
+       if (prefs_common.enable_color && (strcmp(buf,"-- \n") == 0 || textview->is_in_signature)) {
+               fg_color = &signature_color;
+               textview->is_in_signature = TRUE;
+       }
+       
        if (prefs_common.head_space && spacingfont && buf[0] != '\n')
                gtk_stext_insert(text, spacingfont, NULL, NULL, " ", 1);
 
@@ -1069,6 +1137,44 @@ static void textview_write_line(TextView *textview, const gchar *str,
                                              fg_color, NULL, buf);
 }
 
+void textview_write_link(TextView *textview, const gchar *str,
+                        const gchar *uri, CodeConverter *conv)
+{
+       GdkColor *link_color = NULL;
+       GtkSText *text = GTK_STEXT(textview->text);
+       gchar buf[BUFFSIZE];
+       gchar *bufp;
+       RemoteURI *r_uri;
+
+       if (*str == '\0')
+               return;
+
+       if (!conv) {
+               if (textview->text_is_mb)
+                       conv_localetodisp(buf, sizeof(buf), str);
+               else
+                       strncpy2(buf, str, sizeof(buf));
+       } else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
+               conv_localetodisp(buf, sizeof(buf), str);
+       else if (textview->text_is_mb)
+               conv_unreadable_locale(buf);
+
+       strcrchomp(buf);
+
+       for (bufp = buf; isspace(*bufp); bufp++)
+               gtk_stext_insert(text, textview->msgfont, NULL, NULL, bufp, 1);
+
+       if (prefs_common.enable_color) {
+               link_color = &uri_color;
+       }
+       r_uri = g_new(RemoteURI, 1);
+       r_uri->uri = g_strdup(uri);
+       r_uri->start = gtk_stext_get_point(text);
+       gtk_stext_insert(text, textview->msgfont, link_color, NULL, bufp, -1);
+       r_uri->end = gtk_stext_get_point(text);
+       textview->uri_list = g_slist_append(textview->uri_list, r_uri);
+}
+
 void textview_clear(TextView *textview)
 {
        GtkSText *text = GTK_STEXT(textview->text);
@@ -1115,7 +1221,7 @@ void textview_set_font(TextView *textview, const gchar *codeset)
        /* In multi-byte mode, GtkSText can't display 8bit characters
           correctly, so it must be single-byte mode. */
        if (MB_CUR_MAX > 1) {
-               if (codeset) {
+               if (codeset && conv_get_current_charset() != C_UTF_8) {
                        if (!g_strncasecmp(codeset, "ISO-8859-", 9) ||
                            !g_strcasecmp(codeset, "BALTIC"))
                                use_fontset = FALSE;
@@ -1173,11 +1279,11 @@ void textview_set_font(TextView *textview, const gchar *codeset)
                                text_sb_font->ascent = text_sb_font_orig_ascent;
                                text_sb_font->descent = text_sb_font_orig_descent;
                        }
-                       if (MB_CUR_MAX > 1) {
-                               FONT_LOAD(font, "-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
-                       } else {
-                               FONT_LOAD(font, prefs_common.textfont);
-                       }
+                       if (MB_CUR_MAX > 1)
+                               font = gdk_font_load("-*-courier-medium-r-normal--14-*-*-*-*-*-iso8859-1");
+                       else
+                               font = gtkut_font_load_from_fontset
+                                       (prefs_common.textfont);
                        if (font && text_sb_font != font) {
                                if (text_sb_font)
                                        gdk_font_unref(text_sb_font);
@@ -1203,11 +1309,26 @@ void textview_set_font(TextView *textview, const gchar *codeset)
        }
 
        if (!textview->boldfont && prefs_common.boldfont)
-               FONT_LOAD(textview->boldfont, prefs_common.boldfont);
+               textview->boldfont = gtkut_font_load(prefs_common.boldfont);
        if (!spacingfont)
                spacingfont = gdk_font_load("-*-*-medium-r-normal--6-*");
 }
 
+void textview_set_text(TextView *textview, const gchar *text)
+{
+       GtkSText *stext;
+
+       g_return_if_fail(textview != NULL);
+       g_return_if_fail(text != NULL);
+
+       textview_clear(textview);
+
+       stext = GTK_STEXT(textview->text);
+       gtk_stext_freeze(stext);
+       gtk_stext_insert(stext, textview->msgfont, NULL, NULL, text, strlen(text));
+       gtk_stext_thaw(stext);
+}
+
 enum
 {
        H_DATE          = 0,
@@ -1283,9 +1404,10 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
                        header = g_ptr_array_index(headers, i);
                        g_ptr_array_add(sorted_headers, header);
                }
-       }
+               g_ptr_array_free(headers, TRUE);
+       } else
+               procheader_header_array_destroy(headers);
 
-       g_ptr_array_free(headers, FALSE);
 
        return sorted_headers;
 }
@@ -1316,6 +1438,9 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                    procheader_headername_equal(header->name, "Cc"))
                        unfold_line(header->body);
 
+               if (textview->text_is_mb == TRUE)
+                       conv_unreadable_locale(header->body);
+
                if (prefs_common.enable_color &&
                    (procheader_headername_equal(header->name, "X-Mailer") ||
                     procheader_headername_equal(header->name,
@@ -1343,43 +1468,25 @@ gboolean textview_search_string(TextView *textview, const gchar *str,
 {
        GtkSText *text = GTK_STEXT(textview->text);
        gint pos;
-       wchar_t *wcs;
        gint len;
-       gint text_len;
-       gboolean found = FALSE;
 
        g_return_val_if_fail(str != NULL, FALSE);
 
-       wcs = strdup_mbstowcs(str);
-       g_return_val_if_fail(wcs != NULL, FALSE);
-       len = wcslen(wcs);
+       len = get_mbs_len(str);
+       g_return_val_if_fail(len >= 0, FALSE);
+
        pos = textview->cur_pos;
        if (pos < textview->body_pos)
                pos = textview->body_pos;
-       text_len = gtk_stext_get_length(text);
-       if (text_len - pos < len) {
-               g_free(wcs);
-               return FALSE;
-       }
 
-       for (; pos < text_len; pos++) {
-               if (text_len - pos < len) break;
-               if (gtkut_stext_match_string(text, pos, wcs, len, case_sens)
-                   == TRUE) {
-                       gtk_widget_hide(GTK_WIDGET(textview->scrolledwin));
-                       gtk_editable_set_position(GTK_EDITABLE(text),
-                                                 pos + len);
-                       gtk_editable_select_region(GTK_EDITABLE(text),
-                                                  pos, pos + len);
-                       gtk_widget_show(GTK_WIDGET(textview->scrolledwin));
-                       textview_set_position(textview, pos + len);
-                       found = TRUE;
-                       break;
-               }
-               if (text_len - pos == len) break;
+       if ((pos = gtkut_stext_find(text, pos, str, case_sens)) != -1) {
+               gtk_editable_set_position(GTK_EDITABLE(text), pos + len);
+               gtk_editable_select_region(GTK_EDITABLE(text), pos, pos + len);
+               textview_set_position(textview, pos + len);
+               return TRUE;
        }
-       g_free(wcs);
-       return found;
+
+       return FALSE;
 }
 
 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
@@ -1407,7 +1514,7 @@ gboolean textview_search_string_backward(TextView *textview, const gchar *str,
                pos = text_len - len;
 
        for (; pos >= textview->body_pos; pos--) {
-               if (gtkut_stext_match_string(text, pos, wcs, len, case_sens)
+               if (gtk_stext_match_string(text, pos, wcs, len, case_sens)
                    == TRUE) {
                        gtk_editable_set_position(GTK_EDITABLE(text), pos);
                        gtk_editable_select_region(GTK_EDITABLE(text),
@@ -1639,30 +1746,40 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                textview_scroll_one_line(textview,
                                         (event->state & GDK_MOD1_MASK) != 0);
                break;
-       case GDK_n:
-       case GDK_N:
-       case GDK_p:
-       case GDK_P:
+       case GDK_Delete:
+               if (summaryview)
+                       summary_pass_key_press_event(summaryview, event);
+               break;
        case GDK_y:
        case GDK_t:
        case GDK_l:
-               if (messageview->type == MVIEW_MIME) {
+               if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
                        KEY_PRESS_EVENT_STOP();
                        mimeview_pass_key_press_event(messageview->mimeview,
                                                      event);
                        break;
                }
-               /* fall through */
+               /* possible fall through */
        default:
-               if (summaryview)
-                       summary_pass_key_press_event(summaryview, event);
+               if (summaryview &&
+                   event->window != messageview->mainwin->window->window) {
+                       GdkEventKey tmpev = *event;
+
+                       tmpev.window = messageview->mainwin->window->window;
+                       KEY_PRESS_EVENT_STOP();
+                       gtk_widget_event(messageview->mainwin->window,
+                                        (GdkEvent *)&tmpev);
+               }
                break;
        }
 
        return TRUE;
 }
 
-static gint show_url_timeout_cb(TextView    *textview ){
+static gint show_url_timeout_cb(gpointer data)
+{
+       TextView *textview = (TextView *)data;
+       
        if (textview->messageview->mainwin)
                if (textview->show_url_msgid)
                        gtk_statusbar_remove(GTK_STATUSBAR(
@@ -1702,7 +1819,10 @@ static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
                        RemoteURI *uri = (RemoteURI *)cur->data;
 
                        if (textview->cur_pos >= uri->start &&
-                           textview->cur_pos <  uri->end) {
+                           textview->cur_pos <= uri->end) {
+                               gchar *trimmed_uri;
+                               
+                               trimmed_uri = trim_string(uri->uri, 60);
                                /* single click: display url in statusbar */
                                if (event->button == 1 && textview->last_buttonpress != GDK_2BUTTON_PRESS) {
                                        if (textview->messageview->mainwin) {
@@ -1717,15 +1837,14 @@ static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
                                                textview->show_url_msgid = gtk_statusbar_push(
                                                                GTK_STATUSBAR(textview->messageview->mainwin->statusbar),
                                                                textview->messageview->mainwin->folderview_cid,
-                                                               uri->uri);
-                                               textview->show_url_timeout_tag = gtk_timeout_add( 2000, show_url_timeout_cb, textview );
+                                                               trimmed_uri);
+                                               textview->show_url_timeout_tag = gtk_timeout_add( 4000, show_url_timeout_cb, textview );
                                                gtkut_widget_wait_for_draw(textview->messageview->mainwin->hbox_stat);
                                        }
                                } else
                                if (!g_strncasecmp(uri->uri, "mailto:", 7)) {
                                        if (event->button == 3) {
                                                gchar *fromname, *fromaddress;
-                                               GdkEventButton tmpev;   
                                                
                                                /* extract url */
                                                fromaddress = g_strdup(uri->uri + 7);
@@ -1749,13 +1868,13 @@ static gint textview_button_released(GtkWidget *widget, GdkEventButton *event,
                                                        if (folder_item->prefs && folder_item->prefs->enable_default_account)
                                                                account = account_find_from_id(folder_item->prefs->default_account);
                                                }
-                                               compose_new_with_recipient
-                                                       (account, uri->uri + 7);
+                                               compose_new(account, uri->uri + 7, NULL);
                                        }
                                } else {
                                        open_uri(uri->uri,
                                                 prefs_common.uri_cmd);
                                }
+                               g_free(trimmed_uri);
                        }
                }
        }