2006-07-06 [paul] 2.3.1cvs62
[claws.git] / src / textview.c
index 53809432790ab2bf1e6a37417efd8fd8bba4eb97..9b81b69332f2bc39f43589f6e35cd3bf42796a7e 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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
@@ -34,6 +34,7 @@
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
+#include <sys/wait.h>
 #if HAVE_LIBCOMPFACE
 #  include <compface.h>
 #endif
@@ -61,6 +62,7 @@
 #include "menu.h"
 #include "image_viewer.h"
 #include "filesel.h"
+#include "base64.h"
 
 struct _RemoteURI
 {
@@ -68,6 +70,8 @@ struct _RemoteURI
 
        gchar *filename;
 
+       gpointer data;
+
        guint start;
        guint end;
 };
@@ -78,6 +82,11 @@ static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
 };
 
+static GdkColor quote_bgcolors[3] = {
+       {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
+       {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
+       {(gulong)0, (gushort)0, (gushort)0, (gushort)0}
+};
 static GdkColor signature_color = {
        (gulong)0,
        (gushort)0x7fff,
@@ -99,18 +108,9 @@ static GdkColor emphasis_color = {
        (gushort)0xcfff
 };
 
-#if 0
-static GdkColor error_color = {
-       (gulong)0,
-       (gushort)0xefff,
-       (gushort)0,
-       (gushort)0
-};
-#endif
-
-
 static GdkCursor *hand_cursor = NULL;
 static GdkCursor *text_cursor = NULL;
+static GdkCursor *watch_cursor= NULL;
 
 #define TEXTVIEW_STATUSBAR_PUSH(textview, str)                             \
 {      if (textview->messageview->statusbar)                               \
@@ -218,6 +218,7 @@ static void save_file_cb                    (TextView       *textview,
 static void open_image_cb                      (TextView       *textview,
                                                 guint           action,
                                                 void           *data);
+static void textview_show_icon(TextView *textview, const gchar *stock_id);
 
 static GtkItemFactoryEntry textview_link_popup_entries[] = 
 {
@@ -240,7 +241,10 @@ static GtkItemFactoryEntry textview_file_popup_entries[] =
 
 static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
 {
-#if HAVE_LIBCOMPFACE
+#ifndef WIDTH
+#  define WIDTH 48
+#  define HEIGHT 48
+#endif
        if (textview->image) {
                gint x, y, x1;
                x1 = textview->text->allocation.width - WIDTH - 5;
@@ -250,7 +254,6 @@ static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
                gtk_text_view_move_child(GTK_TEXT_VIEW(textview->text), 
                        textview->image, x1, y);
        }
-#endif
 }
 
 static void textview_size_allocate_cb  (GtkWidget      *widget,
@@ -360,7 +363,7 @@ TextView *textview_create(void)
        textview->mail_popup_factory = mail_popupfactory;
        textview->file_popup_menu    = file_popupmenu;
        textview->file_popup_factory = file_popupfactory;
-
+       textview->image              = NULL;
        return textview;
 }
 
@@ -389,34 +392,49 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
                                   "pixels-below-lines", 0,
                                   "pixels-below-lines-set", TRUE,
                                   "font-desc", font_desc,
-                                  "left-margin", 0,
+                                  "left-margin", 3,
                                   "left-margin-set", TRUE,
                                   NULL);
        gtk_text_buffer_create_tag(buffer, "header_title",
                                   "font-desc", bold_font_desc,
                                   NULL);
-       gtk_text_buffer_create_tag(buffer, "quote0",
-                                  "foreground-gdk", &quote_colors[0],
-                                  NULL);
-       gtk_text_buffer_create_tag(buffer, "quote1",
-                                  "foreground-gdk", &quote_colors[1],
-                                  NULL);
-       gtk_text_buffer_create_tag(buffer, "quote2",
-                                  "foreground-gdk", &quote_colors[2],
-                                  NULL);
+       if (prefs_common.enable_bgcolor) {
+               gtk_text_buffer_create_tag(buffer, "quote0",
+                               "foreground-gdk", &quote_colors[0],
+                               "paragraph-background-gdk", &quote_bgcolors[0],
+                               NULL);
+               gtk_text_buffer_create_tag(buffer, "quote1",
+                               "foreground-gdk", &quote_colors[1],
+                               "paragraph-background-gdk", &quote_bgcolors[1],
+                               NULL);
+               gtk_text_buffer_create_tag(buffer, "quote2",
+                               "foreground-gdk", &quote_colors[2],
+                               "paragraph-background-gdk", &quote_bgcolors[2],
+                               NULL);
+       } else {
+               gtk_text_buffer_create_tag(buffer, "quote0",
+                               "foreground-gdk", &quote_colors[0],
+                               NULL);
+               gtk_text_buffer_create_tag(buffer, "quote1",
+                               "foreground-gdk", &quote_colors[1],
+                               NULL);
+               gtk_text_buffer_create_tag(buffer, "quote2",
+                               "foreground-gdk", &quote_colors[2],
+                               NULL);
+       }
        gtk_text_buffer_create_tag(buffer, "emphasis",
-                                  "foreground-gdk", &emphasis_color,
-                                  NULL);
-       gtk_text_buffer_create_tag(buffer, "signature",
-                                  "foreground-gdk", &signature_color,
-                                  NULL);
-       tag = gtk_text_buffer_create_tag(buffer, "link",
-                                        "foreground-gdk", &uri_color,
-                                        NULL);
+                       "foreground-gdk", &emphasis_color,
+                       NULL);
+       gtk_text_buffer_create_tag(buffer, "signature",
+                       "foreground-gdk", &signature_color,
+                       NULL);
+       tag = gtk_text_buffer_create_tag(buffer, "link",
+                       "foreground-gdk", &uri_color,
+                       NULL);
        gtk_text_buffer_create_tag(buffer, "link-hover",
-                                  "foreground-gdk", &uri_color,
-                                  "underline", PANGO_UNDERLINE_SINGLE,
-                                  NULL);
+                       "foreground-gdk", &uri_color,
+                       "underline", PANGO_UNDERLINE_SINGLE,
+                       NULL);
        g_signal_connect(G_OBJECT(tag), "event",
                          G_CALLBACK(textview_uri_button_pressed), textview);
  }
@@ -427,6 +445,8 @@ void textview_init(TextView *textview)
                hand_cursor = gdk_cursor_new(GDK_HAND2);
        if (!text_cursor)
                text_cursor = gdk_cursor_new(GDK_XTERM);
+       if (!watch_cursor)
+               watch_cursor = gdk_cursor_new(GDK_WATCH);
 
        textview_reflect_prefs(textview);
        textview_set_all_headers(textview, FALSE);
@@ -434,11 +454,19 @@ void textview_init(TextView *textview)
        textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
 }
 
-#define CHANGE_TAG_COLOR(tagname, color) { \
+#if GTK_CHECK_VERSION(2, 8, 0)
+ #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
        tag = gtk_text_tag_table_lookup(tags, tagname); \
        if (tag) \
-               g_object_set(G_OBJECT(tag), "foreground-gdk", color, NULL); \
-}
+               g_object_set(G_OBJECT(tag), "foreground-gdk", colorfg, "paragraph-background-gdk", colorbg, NULL); \
+ }
+#else
+ #define CHANGE_TAG_COLOR(tagname, colorfg, colorbg) { \
+       tag = gtk_text_tag_table_lookup(tags, tagname); \
+       if (tag) \
+               g_object_set(G_OBJECT(tag), "foreground-gdk", colorfg, NULL); \
+ }
+#endif
 
 static void textview_update_message_colors(TextView *textview)
 {
@@ -449,6 +477,10 @@ static void textview_update_message_colors(TextView *textview)
        GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
        GtkTextTag *tag = NULL;
 
+       quote_bgcolors[0] = quote_bgcolors[1] = quote_bgcolors[2] = black;
+       quote_colors[0] = quote_colors[1] = quote_colors[2] = 
+               uri_color = emphasis_color = signature_color = black;
+
        if (prefs_common.enable_color) {
                /* grab the quote colors, converting from an int to a GdkColor */
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_col,
@@ -462,18 +494,27 @@ static void textview_update_message_colors(TextView *textview)
                gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
                                               &signature_color);
                emphasis_color = colored_emphasis;
+       }
+       if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
+               gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
+                                                  &quote_bgcolors[0]);
+               gtkut_convert_int_to_gdk_color(prefs_common.quote_level2_bgcol,
+                                                  &quote_bgcolors[1]);
+               gtkut_convert_int_to_gdk_color(prefs_common.quote_level3_bgcol,
+                                                  &quote_bgcolors[2]);
+               CHANGE_TAG_COLOR("quote0", &quote_colors[0], &quote_bgcolors[0]);
+               CHANGE_TAG_COLOR("quote1", &quote_colors[1], &quote_bgcolors[1]);
+               CHANGE_TAG_COLOR("quote2", &quote_colors[2], &quote_bgcolors[2]);
        } else {
-               quote_colors[0] = quote_colors[1] = quote_colors[2] = 
-                       uri_color = emphasis_color = signature_color = black;
+               CHANGE_TAG_COLOR("quote0", &quote_colors[0], NULL);
+               CHANGE_TAG_COLOR("quote1", &quote_colors[1], NULL);
+               CHANGE_TAG_COLOR("quote2", &quote_colors[2], NULL);
        }
-       CHANGE_TAG_COLOR("quote0", &quote_colors[0]);
-       CHANGE_TAG_COLOR("quote1", &quote_colors[1]);
-       CHANGE_TAG_COLOR("quote2", &quote_colors[2]);
-       CHANGE_TAG_COLOR("emphasis", &emphasis_color);
-       CHANGE_TAG_COLOR("signature", &signature_color);
-       CHANGE_TAG_COLOR("link", &uri_color);
-       CHANGE_TAG_COLOR("link-hover", &uri_color);
 
+       CHANGE_TAG_COLOR("emphasis", &emphasis_color, NULL);
+       CHANGE_TAG_COLOR("signature", &signature_color, NULL);
+       CHANGE_TAG_COLOR("link", &uri_color, NULL);
+       CHANGE_TAG_COLOR("link-hover", &uri_color, NULL);
 }
 #undef CHANGE_TAG_COLOR
 
@@ -518,6 +559,27 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                textview_write_body(textview, mimeinfo);
 }
 
+#define TEXT_INSERT(str) \
+       gtk_text_buffer_insert_with_tags_by_name \
+                               (buffer, &iter, str, -1,\
+                                "header", NULL)
+
+#define TEXT_INSERT_LINK(str, fname, udata) {                          \
+       RemoteURI *uri;                                                 \
+       uri = g_new(RemoteURI, 1);                                      \
+       uri->uri = g_strdup("");                                        \
+       uri->start = gtk_text_iter_get_offset(&iter);                   \
+       gtk_text_buffer_insert_with_tags_by_name                        \
+                               (buffer, &iter, str, -1,                \
+                                "link", "header_title", "header",      \
+                                NULL);                                 \
+       uri->end = gtk_text_iter_get_offset(&iter);                     \
+       uri->filename = fname?g_strdup(fname):NULL;                     \
+       uri->data = udata;                                              \
+       textview->uri_list =                                            \
+               g_slist_append(textview->uri_list, uri);                \
+}
+
 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 {
        GtkTextView *text;
@@ -567,8 +629,10 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 
        g_free(content_type);                      
 
-       if (mimeinfo->type != MIMETYPE_TEXT) {
-               gtk_text_buffer_insert(buffer, &iter, buf, -1);
+       if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
+       || (mimeinfo->disposition == DISPOSITIONTYPE_INLINE && 
+           mimeinfo->type != MIMETYPE_TEXT)) {
+               TEXT_INSERT_LINK(buf, "sc://select_attachment", mimeinfo);
                if (mimeinfo->type == MIMETYPE_IMAGE  &&
                    prefs_common.inline_img ) {
                        GdkPixbuf *pixbuf;
@@ -576,10 +640,6 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        gchar *filename;
                        RemoteURI *uri;
                        gchar *uri_str;
-                       FILE *fp;
-
-                       fp = g_fopen(mimeinfo->data.filename, "rb");
-                       fseek(fp, mimeinfo->offset, SEEK_SET);
 
                        filename = procmime_get_tmp_file_name(mimeinfo);
                        if (procmime_get_part(filename, mimeinfo) < 0) {
@@ -616,6 +676,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 
                        uri_str = g_filename_to_uri(filename, NULL, NULL);
                        if (uri_str) {
+                               gtk_text_buffer_insert(buffer, &iter, " ", -1);
                                uri = g_new(RemoteURI, 1);
                                uri->uri = uri_str;
                                uri->start = gtk_text_iter_get_offset(&iter);
@@ -639,7 +700,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        g_object_unref(pixbuf);
                        g_free(filename);
                }
-       } else if (mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
+       } else if (mimeinfo->type == MIMETYPE_TEXT) {
                if (prefs_common.display_header && (charcount > 0))
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
 
@@ -712,9 +773,6 @@ static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
         recursive_add_parts(textview, mimeinfo->node);
 }
 
-#define TEXT_INSERT(str) \
-       gtk_text_buffer_insert(buffer, &iter, str, -1)
-
 void textview_show_error(TextView *textview)
 {
        GtkTextView *text;
@@ -728,7 +786,14 @@ void textview_show_error(TextView *textview)
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_start_iter(buffer, &iter);
 
-       TEXT_INSERT(_("This message can't be displayed.\n"));
+       TEXT_INSERT(_("\n"
+                     "  This message can't be displayed.\n"
+                     "  This is probably due to a network error.\n"
+                     "\n"
+                     "  Use "));
+       TEXT_INSERT_LINK(_("'View Log'"), "sc://view_log", NULL);
+       TEXT_INSERT(_(" in the Tools menu for more information."));
+       textview_show_icon(textview, GTK_STOCK_DIALOG_ERROR);
 
 }
 
@@ -747,28 +812,36 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_start_iter(buffer, &iter);
 
-       TEXT_INSERT(_("The following can be performed on this part by "));
-       TEXT_INSERT(_("right-clicking the icon or list item:\n"));
-
-       TEXT_INSERT(_("    To save select 'Save as...' (Shortcut key: 'y')\n"));
-       TEXT_INSERT(_("    To display as text select 'Display as text' "));
-       TEXT_INSERT(_("(Shortcut key: 't')\n"));
-       TEXT_INSERT(_("    To open with an external program select 'Open' "));
-       TEXT_INSERT(_("(Shortcut key: 'l'),\n"));
-       TEXT_INSERT(_("    (alternately double-click, or click the middle "));
-       TEXT_INSERT(_("mouse button),\n"));
-       TEXT_INSERT(_("    or 'Open with...' (Shortcut key: 'o')\n"));
-
+       TEXT_INSERT("\n");
+       TEXT_INSERT(_("  The following can be performed on this part by\n"));
+       TEXT_INSERT(_("  right-clicking the icon or list item:\n"));
+
+       TEXT_INSERT(_("     - To save, select "));
+       TEXT_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
+       TEXT_INSERT(_(" (Shortcut key: 'y')\n"));
+       TEXT_INSERT(_("     - To display as text, select "));
+       TEXT_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
+       TEXT_INSERT(_(" (Shortcut key: 't')\n"));
+       TEXT_INSERT(_("     - To open with an external program, select "));
+       TEXT_INSERT_LINK(_("'Open'"), "sc://open", NULL);
+       TEXT_INSERT(_(" (Shortcut key: 'l')\n"));
+       TEXT_INSERT(_("       (alternately double-click, or click the middle "));
+       TEXT_INSERT(_("mouse button)\n"));
+       TEXT_INSERT(_("     - Or use "));
+       TEXT_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
+       TEXT_INSERT(_(" (Shortcut key: 'o')\n"));
+       textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
 }
 
 #undef TEXT_INSERT
+#undef TEXT_INSERT_LINK
 
 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 {
        FILE *tmpfp;
        gchar buf[BUFFSIZE];
        CodeConverter *conv;
-       const gchar *charset;
+       const gchar *charset, *p, *cmd;
        
        if (textview->messageview->forced_charset)
                charset = textview->messageview->forced_charset;
@@ -808,7 +881,53 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                        g_unlink(filename);
                }
                g_free(filename);
+       } else if ( g_ascii_strcasecmp(mimeinfo->subtype, "plain") &&
+                  (cmd = prefs_common.mime_textviewer) && *cmd &&
+                  (p = strchr(cmd, '%')) && *(p + 1) == 's') {
+               int pid, pfd[2];
+               const gchar *fname;
+
+               fname  = procmime_get_tmp_file_name(mimeinfo);
+               if (procmime_get_part(fname, mimeinfo)) goto textview_default;
+
+               g_snprintf(buf, sizeof(buf), cmd, fname);
+               debug_print("Viewing text content of type: %s (length: %d) "
+                       "using %s\n", mimeinfo->subtype, mimeinfo->length, buf);
+
+               if (pipe(pfd) < 0) {
+                       g_snprintf(buf, sizeof(buf),
+                               "pipe failed for textview\n\n%s\n", strerror(errno));
+                       textview_write_line(textview, buf, conv);
+                       goto textview_default;
+               }
+               pid = fork();
+               if (pid < 0) {
+                       g_snprintf(buf, sizeof(buf),
+                               "fork failed for textview\n\n%s\n", strerror(errno));
+                       textview_write_line(textview, buf, conv);
+                       close(pfd[0]);
+                       close(pfd[1]);
+                       goto textview_default;
+               }
+               if (pid == 0) { /* child */
+                       gchar **argv;
+                       argv = strsplit_with_quote(buf, " ", 0);
+                       close(1);
+                       close(pfd[0]);
+                       dup(pfd[1]);
+                       execvp(argv[0], argv);
+                       close(pfd[1]);
+                       exit(255);
+               }
+               close(pfd[1]);
+               tmpfp = fdopen(pfd[0], "rb");
+               while (fgets(buf, sizeof(buf), tmpfp))
+                       textview_write_line(textview, buf, conv);
+               fclose(tmpfp);
+               waitpid(pid, pfd, 0);
+               unlink(fname);
        } else {
+textview_default:
                tmpfp = g_fopen(mimeinfo->data.filename, "rb");
                fseek(tmpfp, mimeinfo->offset, SEEK_SET);
                debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
@@ -825,20 +944,22 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 static void textview_show_html(TextView *textview, FILE *fp,
                               CodeConverter *conv)
 {
-       HTMLParser *parser;
+       SC_HTMLParser *parser;
        gchar *str;
 
-       parser = html_parser_new(fp, conv);
+       parser = sc_html_parser_new(fp, conv);
        g_return_if_fail(parser != NULL);
 
-       while ((str = html_parse(parser)) != NULL) {
-               if (parser->state == HTML_HREF) {
+       while ((str = sc_html_parse(parser)) != NULL) {
+               if (parser->state == SC_HTML_HREF) {
                        /* first time : get and copy the URL */
                        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) { 
+                               if (str) {
+                                       while (str && *str && g_ascii_isspace(*str))
+                                               str++; 
                                        parser->href = g_strdup(str);
                                        /* the URL may (or not) be followed by the
                                         * referenced text */
@@ -851,7 +972,7 @@ static void textview_show_html(TextView *textview, FILE *fp,
                        textview_write_line(textview, str, NULL);
        }
        textview_write_line(textview, "\n", NULL);
-       html_parser_destroy(parser);
+       sc_html_parser_destroy(parser);
 }
 
 static void textview_show_ertf(TextView *textview, FILE *fp,
@@ -887,7 +1008,8 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
 static void textview_make_clickable_parts(TextView *textview,
                                          const gchar *fg_tag,
                                          const gchar *uri_tag,
-                                         const gchar *linebuf)
+                                         const gchar *linebuf,
+                                         gboolean hdr)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
@@ -905,7 +1027,8 @@ static void textview_make_clickable_parts(TextView *textview,
                gboolean  (*parse)      (const gchar *start,
                                         const gchar *scanpos,
                                         const gchar **bp_,
-                                        const gchar **ep_);
+                                        const gchar **ep_,
+                                        gboolean hdr);
                /* part to URI function */
                gchar    *(*build_uri)  (const gchar *bp,
                                         const gchar *ep);
@@ -915,6 +1038,7 @@ static void textview_make_clickable_parts(TextView *textview,
                {"http://",  strcasestr, get_uri_part,   make_uri_string},
                {"https://", strcasestr, get_uri_part,   make_uri_string},
                {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
+               {"sftp://",  strcasestr, get_uri_part,   make_uri_string},
                {"www.",     strcasestr, get_uri_part,   make_http_string},
                {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
                {"@",        strcasestr, get_email_part, make_email_string}
@@ -957,7 +1081,7 @@ static void textview_make_clickable_parts(TextView *textview,
 
                if (scanpos) {
                        /* check if URI can be parsed */
-                       if (parser[last_index].parse(walk, scanpos, &bp, &ep)
+                       if (parser[last_index].parse(walk, scanpos, &bp, &ep, hdr)
                            && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
                                        ADD_TXT_POS(bp, ep, last_index);
                                        walk = ep;
@@ -1028,7 +1152,6 @@ static void textview_write_line(TextView *textview, const gchar *str,
                conv_localetodisp(buf, sizeof(buf), str);
                
        strcrchomp(buf);
-       //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
        fg_color = NULL;
 
        /* change color of quotation
@@ -1062,7 +1185,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                textview->is_in_signature = TRUE;
        }
 
-       textview_make_clickable_parts(textview, fg_color, "link", buf);
+       textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
 }
 
 void textview_write_link(TextView *textview, const gchar *str,
@@ -1081,6 +1204,9 @@ void textview_write_link(TextView *textview, const gchar *str,
        if (!uri)
                return;
 
+       while (uri && *uri && g_ascii_isspace(*uri))
+               uri++;
+               
        text = GTK_TEXT_VIEW(textview->text);
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_end_iter(buffer, &iter);
@@ -1119,10 +1245,17 @@ void textview_write_link(TextView *textview, const gchar *str,
        textview->uri_list = g_slist_append(textview->uri_list, r_uri);
 }
 
+static void textview_set_cursor(GdkWindow *window, GdkCursor *cursor)
+{
+       if (GDK_IS_WINDOW(window))
+               gdk_window_set_cursor(window, cursor);
+}
 void textview_clear(TextView *textview)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        GtkTextBuffer *buffer;
+       GdkWindow *window = gtk_text_view_get_window(text,
+                               GTK_TEXT_WINDOW_TEXT);
 
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_set_text(buffer, "", -1);
@@ -1130,11 +1263,18 @@ void textview_clear(TextView *textview)
        TEXTVIEW_STATUSBAR_POP(textview);
        textview_uri_list_remove_all(textview->uri_list);
        textview->uri_list = NULL;
+       textview->uri_hover = NULL;
 
        textview->body_pos = 0;
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        textview->image = NULL;
+
+       if (textview->messageview->mainwin->cursor_count == 0) {
+               textview_set_cursor(window, text_cursor);
+       } else {
+               textview_set_cursor(window, watch_cursor);
+       }
 }
 
 void textview_destroy(TextView *textview)
@@ -1231,12 +1371,8 @@ enum
 void textview_set_position(TextView *textview, gint pos)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
-       GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
-       GtkTextIter iter;
 
-       gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
-       gtk_text_buffer_place_cursor(buffer, &iter);
-       gtk_text_view_scroll_to_iter(text, &iter, 0.0, FALSE, 0.0, 0.0);
+       gtkut_text_view_set_position(text, pos);
 }
 
 static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
@@ -1296,17 +1432,74 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        return sorted_headers;
 }
 
+static void textview_show_face(TextView *textview)
+{
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       int x = 0;
+       
+       if (prefs_common.display_header_pane
+       ||  !prefs_common.display_xface)
+               goto bail;
+       
+       if (!msginfo->face) {
+               goto bail;
+       }
+
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       
+       textview->image = face_get_from_header(msginfo->face);
+       g_return_if_fail(textview->image != NULL);
+
+       gtk_widget_show(textview->image);
+       
+       x = textview->text->allocation.width - WIDTH -5;
+
+       gtk_text_view_add_child_in_window(text, textview->image, 
+               GTK_TEXT_WINDOW_TEXT, x, 5);
+
+       gtk_widget_show_all(textview->text);
+       
+
+       return;
+bail:
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       textview->image = NULL; 
+}
+
+static void textview_show_icon(TextView *textview, const gchar *stock_id)
+{
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       int x = 0;
+       
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       
+       textview->image = gtk_image_new_from_stock(stock_id, GTK_ICON_SIZE_DIALOG);
+       g_return_if_fail(textview->image != NULL);
+
+       gtk_widget_show(textview->image);
+       
+       x = textview->text->allocation.width - WIDTH -5;
+
+       gtk_text_view_add_child_in_window(text, textview->image, 
+               GTK_TEXT_WINDOW_TEXT, x, 5);
+
+       gtk_widget_show_all(textview->text);
+       
+
+       return;
+}
+
 #if HAVE_LIBCOMPFACE
 static void textview_show_xface(TextView *textview)
 {
-       gchar xface[2048];
        MsgInfo *msginfo = textview->messageview->msginfo;
-       static gchar *xpm_xface[XPM_XFACE_HEIGHT];
-       static gboolean xpm_xface_init = TRUE;
-       GdkPixmap *pixmap;
-       GdkBitmap *mask;
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        int x = 0;
+
        if (prefs_common.display_header_pane
        ||  !prefs_common.display_xface)
                goto bail;
@@ -1314,37 +1507,21 @@ static void textview_show_xface(TextView *textview)
        if (!msginfo)
                goto bail;
 
+       if (msginfo->face)
+               return;
+       
        if (!msginfo->xface || strlen(msginfo->xface) < 5) {
                goto bail;
        }
 
-       strncpy(xface, msginfo->xface, sizeof(xface));
-
-       if (uncompface(xface) < 0) {
-               g_warning("uncompface failed\n");
-               goto bail;
-       }
-
-       if (xpm_xface_init) {
-               gint i;
-
-               for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
-                       xpm_xface[i] = g_malloc(WIDTH + 1);
-                       *xpm_xface[i] = '\0';
-               }
-               xpm_xface_init = FALSE;
-       }
-
-       create_xpm_from_xface(xpm_xface, xface);
-
-       pixmap = gdk_pixmap_create_from_xpm_d
-               (textview->text->window, &mask, 
-                &textview->text->style->white, xpm_xface);
-       
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        
-       textview->image = gtk_image_new_from_pixmap(pixmap, mask);
+       textview->image = xface_get_from_header(msginfo->xface,
+                               &textview->text->style->white,
+                               textview->text->window);
+       g_return_if_fail(textview->image != NULL);
+
        gtk_widget_show(textview->image);
        
        x = textview->text->allocation.width - WIDTH -5;
@@ -1378,9 +1555,20 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                g_return_if_fail(header->name != NULL);
 
                gtk_text_buffer_get_end_iter (buffer, &iter);
-               gtk_text_buffer_insert_with_tags_by_name
-                       (buffer, &iter, header->name, -1,
-                        "header_title", "header", NULL);
+               if(prefs_common.trans_hdr == TRUE) {
+                       gchar *hdr = g_strndup(header->name, strlen(header->name) - 1);
+                       gchar *trans_hdr = gettext(hdr);
+                       gtk_text_buffer_insert_with_tags_by_name(buffer,
+                               &iter, trans_hdr, -1,
+                               "header_title", "header", NULL);
+                       gtk_text_buffer_insert_with_tags_by_name(buffer,
+                               &iter, ":", 1, "header_title", "header", NULL);
+                       g_free(hdr);
+               } else {
+                       gtk_text_buffer_insert_with_tags_by_name(buffer,
+                               &iter, header->name,
+                               -1, "header_title", "header", NULL);
+               }
                if (header->name[strlen(header->name) - 1] != ' ')
                gtk_text_buffer_insert_with_tags_by_name
                                (buffer, &iter, " ", 1,
@@ -1395,19 +1583,29 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                if ((procheader_headername_equal(header->name, "X-Mailer") ||
                     procheader_headername_equal(header->name,
                                                 "X-Newsreader")) &&
-                   strstr(header->body, "Sylpheed") != NULL) {
+                   strstr(header->body, "Sylpheed-Claws") != NULL) {
                        gtk_text_buffer_get_end_iter (buffer, &iter);
                        gtk_text_buffer_insert_with_tags_by_name
                                (buffer, &iter, header->body, -1,
                                 "header", "emphasis", NULL);
                } else {
-                       textview_make_clickable_parts(textview, "header", "link",
-                                                     header->body);
+                       gboolean hdr = 
+                         procheader_headername_equal(header->name, "From") ||
+                         procheader_headername_equal(header->name, "To") ||
+                         procheader_headername_equal(header->name, "Cc") ||
+                         procheader_headername_equal(header->name, "Bcc") ||
+                         procheader_headername_equal(header->name, "Reply-To") ||
+                         procheader_headername_equal(header->name, "Sender");
+                       textview_make_clickable_parts(textview, "header", 
+                                                     "link", header->body, 
+                                                     hdr);
                }
                gtk_text_buffer_get_end_iter (buffer, &iter);
                gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
                                                         "header", NULL);
        }
+       
+       textview_show_face(textview);
 #if HAVE_LIBCOMPFACE
        textview_show_xface(textview);
 #endif
@@ -1417,65 +1615,16 @@ gboolean textview_search_string(TextView *textview, const gchar *str,
                                gboolean case_sens)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
-       GtkTextBuffer *buffer;
-       GtkTextIter iter, match_pos;
-       GtkTextMark *mark;
-       gint len;
 
-       g_return_val_if_fail(str != NULL, FALSE);
-
-       buffer = gtk_text_view_get_buffer(text);
-
-       len = g_utf8_strlen(str, -1);
-       g_return_val_if_fail(len >= 0, FALSE);
-
-       mark = gtk_text_buffer_get_insert(buffer);
-       gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
-
-       if (gtkut_text_buffer_find(buffer, &iter, str, case_sens,
-                                  &match_pos)) {
-               GtkTextIter end = match_pos;
-
-               gtk_text_iter_forward_chars(&end, len);
-               /* place "insert" at the last character */
-               gtk_text_buffer_select_range(buffer, &end, &match_pos);
-               gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
-               return TRUE;
-       }
-
-       return FALSE;
+       return gtkut_text_view_search_string(text, str, case_sens);
 }
 
 gboolean textview_search_string_backward(TextView *textview, const gchar *str,
                                         gboolean case_sens)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
-       GtkTextBuffer *buffer;
-       GtkTextIter iter, match_pos;
-       GtkTextMark *mark;
-       gint len;
 
-       g_return_val_if_fail(str != NULL, FALSE);
-
-       buffer = gtk_text_view_get_buffer(text);
-
-       len = g_utf8_strlen(str, -1);
-       g_return_val_if_fail(len >= 0, FALSE);
-
-       mark = gtk_text_buffer_get_insert(buffer);
-       gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
-
-       if (gtkut_text_buffer_find_backward(buffer, &iter, str, case_sens,
-                                           &match_pos)) {
-               GtkTextIter end = match_pos;
-
-               gtk_text_iter_forward_chars(&end, len);
-               gtk_text_buffer_select_range(buffer, &match_pos, &end);
-               gtk_text_view_scroll_to_mark(text, mark, 0.0, FALSE, 0.0, 0.0);
-               return TRUE;
-       }
-
-       return FALSE;
+       return gtkut_text_view_search_string_backward(text, str, case_sens);
 }
 
 void textview_scroll_one_line(TextView *textview, gboolean up)
@@ -1762,6 +1911,22 @@ static gboolean textview_visibility_notify(GtkWidget *widget,
        return FALSE;
 }
 
+void textview_cursor_wait(TextView *textview)
+{
+       GdkWindow *window = gtk_text_view_get_window(
+                       GTK_TEXT_VIEW(textview->text),
+                       GTK_TEXT_WINDOW_TEXT);
+       textview_set_cursor(window, watch_cursor);
+}
+
+void textview_cursor_normal(TextView *textview)
+{
+       GdkWindow *window = gtk_text_view_get_window(
+                       GTK_TEXT_VIEW(textview->text),
+                       GTK_TEXT_WINDOW_TEXT);
+       textview_set_cursor(window, text_cursor);
+}
+
 static void textview_uri_update(TextView *textview, gint x, gint y)
 {
        GtkTextBuffer *buffer;
@@ -1820,7 +1985,11 @@ static void textview_uri_update(TextView *textview, gint x, gint y)
                
                window = gtk_text_view_get_window(GTK_TEXT_VIEW(textview->text),
                                                  GTK_TEXT_WINDOW_TEXT);
-               gdk_window_set_cursor(window, uri ? hand_cursor : text_cursor);
+               if (textview->messageview->mainwin->cursor_count == 0) {
+                       textview_set_cursor(window, uri ? hand_cursor : text_cursor);
+               } else {
+                       textview_set_cursor(window, watch_cursor);
+               }
 
                TEXTVIEW_STATUSBAR_POP(textview);
 
@@ -1845,24 +2014,7 @@ static gboolean textview_get_uri_range(TextView *textview,
                                       GtkTextIter *start_iter,
                                       GtkTextIter *end_iter)
 {
-       GtkTextIter _start_iter, _end_iter;
-
-       _end_iter = *iter;
-       if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
-               debug_print("Can't find end");
-               return FALSE;
-       }
-
-       _start_iter = _end_iter;
-       if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
-               debug_print("Can't find start.");
-               return FALSE;
-       }
-
-       *start_iter = _start_iter;
-       *end_iter = _end_iter;
-
-       return TRUE;
+       return get_tag_range(iter, tag, start_iter, end_iter);
 }
 
 static RemoteURI *textview_get_uri_from_range(TextView *textview,
@@ -1941,7 +2093,18 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
        /* doubleclick: open compose / add address / browser */
        if ((event->type == GDK_BUTTON_PRESS && bevent->button == 1) ||
                bevent->button == 2 || bevent->button == 3) {
-               if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
+               if (uri->filename && !g_ascii_strncasecmp(uri->filename, "sc://", 5)) {
+                       MimeView *mimeview = 
+                               (textview->messageview)?
+                                       textview->messageview->mimeview:NULL;
+                       if (mimeview && bevent->button == 1) {
+                               mimeview_handle_cmd(mimeview, uri->filename, uri->data);
+                       } else if (mimeview && bevent->button == 2 && 
+                               !g_ascii_strcasecmp(uri->filename, "sc://select_attachment")) {
+                               mimeview_handle_cmd(mimeview, "sc://open_attachment", uri->data);
+                       }
+                       return TRUE;
+               } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
                        if (bevent->button == 3) {
                                g_object_set_data(
                                        G_OBJECT(textview->mail_popup_menu),
@@ -2030,7 +2193,7 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
 
                uri_path = get_uri_path(uri->uri);
                visible_uri_path = get_uri_path(visible_str);
-               if (strcmp(uri_path, visible_uri_path) != 0)
+               if (path_cmp(uri_path, visible_uri_path) != 0)
                        retval = FALSE;
        }
 
@@ -2038,16 +2201,20 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
                gchar *msg;
                AlertValue aval;
 
-               msg = g_strdup_printf(_("The real URL (%s) is different from\n"
-                                       "the apparent URL (%s).\n"
-                                       "\n"
-                                       "Open it anyway?"),
-                                     uri->uri, visible_str);
-               aval = alertpanel_full(_("Fake URL warning"), msg,
-                                      GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
-                                      NULL, ALERT_WARNING, G_ALERTALTERNATE);
+               msg = g_markup_printf_escaped(_("The real URL is different from "
+                                               "the displayed URL.\n"
+                                               "\n"
+                                               "<b>Displayed URL:</b> %s\n"
+                                               "\n"
+                                               "<b>Real URL:</b> %s\n"
+                                               "\n"
+                                               "Open it anyway?"),
+                                              visible_str,uri->uri);
+               aval = alertpanel_full(_("Phishing attempt warning"), msg,
+                                      GTK_STOCK_CANCEL, _("_Open URL"), NULL, FALSE,
+                                      NULL, ALERT_WARNING, G_ALERTDEFAULT);
                g_free(msg);
-               if (aval == G_ALERTDEFAULT)
+               if (aval == G_ALERTALTERNATE)
                        retval = TRUE;
        }
 
@@ -2185,10 +2352,10 @@ static void save_file_cb (TextView *textview, guint action, void *data)
                
                res = g_strdup_printf(_("Overwrite existing file '%s'?"),
                                      filename);
-               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_OK
-                                 GTK_STOCK_CANCEL, NULL);
+               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL
+                                 GTK_STOCK_OK, NULL);
                g_free(res);                                      
-               if (G_ALERTDEFAULT != aval) 
+               if (G_ALERTALTERNATE != aval)
                        return;
        }
 
@@ -2198,8 +2365,7 @@ static void save_file_cb (TextView *textview, guint action, void *data)
        
        filedir = g_path_get_dirname(filename);
        if (filedir && strcmp(filedir, ".")) {
-               if (prefs_common.attach_save_dir)
-                       g_free(prefs_common.attach_save_dir);
+               g_free(prefs_common.attach_save_dir);
                prefs_common.attach_save_dir = g_strdup(filedir);
        }