2005-11-18 [paul] 1.9.100cvs20
[claws.git] / src / textview.c
index 5dfa78cc904db2eb7f9f7336116c3401085080e1..4b4539ed431c5e6cf3aca800262aabbd40663d1d 100644 (file)
@@ -14,7 +14,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
+#if HAVE_LIBCOMPFACE
+#  include <compface.h>
+#endif
+
+#if HAVE_LIBCOMPFACE
+#define XPM_XFACE_HEIGHT       (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
+#endif
 
 #include "main.h"
 #include "summaryview.h"
 #include "mimeview.h"
 #include "alertpanel.h"
 #include "menu.h"
+#include "image_viewer.h"
+#include "filesel.h"
 
 struct _RemoteURI
 {
        gchar *uri;
 
+       gchar *filename;
+
        guint start;
        guint end;
 };
@@ -101,14 +112,14 @@ static GdkColor error_color = {
 static GdkCursor *hand_cursor = NULL;
 static GdkCursor *text_cursor = NULL;
 
-#define TEXTVIEW_STATUSBAR_PUSH(textview, str)                                     \
-{                                                                          \
+#define TEXTVIEW_STATUSBAR_PUSH(textview, str)                             \
+{      if (textview->messageview->statusbar)                               \
        gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
                           textview->messageview->statusbar_cid, str);      \
 }
 
-#define TEXTVIEW_STATUSBAR_POP(textview)                                                  \
-{                                                                         \
+#define TEXTVIEW_STATUSBAR_POP(textview)                                  \
+{      if (textview->messageview->statusbar)                              \
        gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
                          textview->messageview->statusbar_cid);           \
 }
@@ -145,9 +156,9 @@ static gint textview_key_pressed            (GtkWidget      *widget,
 static gboolean textview_motion_notify         (GtkWidget      *widget,
                                                 GdkEventMotion *motion,
                                                 TextView       *textview);
-static gboolean textview_leave_notify          (GtkWidget      *widget,
+static gboolean textview_leave_notify          (GtkWidget        *widget,
                                                 GdkEventCrossing *event,
-                                                TextView       *textview);
+                                                TextView         *textview);
 static gboolean textview_visibility_notify     (GtkWidget      *widget,
                                                 GdkEventVisibility *event,
                                                 TextView       *textview);
@@ -201,20 +212,53 @@ static void mail_to_uri_cb                        (TextView       *textview,
 static void copy_mail_to_uri_cb                        (TextView       *textview,
                                                 guint           action,
                                                 void           *data);
+static void save_file_cb                       (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
+static void open_image_cb                      (TextView       *textview,
+                                                guint           action,
+                                                void           *data);
 
 static GtkItemFactoryEntry textview_link_popup_entries[] = 
 {
-       {N_("/_Open link"),             NULL, open_uri_cb, 0, NULL},
-       {N_("/_Copy link location"),    NULL, copy_uri_cb, 0, NULL},
+       {N_("/_Open with Web browser"), NULL, open_uri_cb, 0, NULL},
+       {N_("/Copy this _link"),        NULL, copy_uri_cb, 0, NULL},
 };
 
 static GtkItemFactoryEntry textview_mail_popup_entries[] = 
 {
-       {N_("/_Add to addressbook"),    NULL, add_uri_to_addrbook_cb, 0, NULL},
-       {N_("/_Email"),                 NULL, mail_to_uri_cb, 0, NULL},
-       {N_("/_Copy"),                  NULL, copy_mail_to_uri_cb, 0, NULL},
+       {N_("/Compose _new message"),   NULL, mail_to_uri_cb, 0, NULL},
+       {N_("/Add to _address book"),   NULL, add_uri_to_addrbook_cb, 0, NULL},
+       {N_("/Copy this add_ress"),     NULL, copy_mail_to_uri_cb, 0, NULL},
 };
 
+static GtkItemFactoryEntry textview_file_popup_entries[] = 
+{
+       {N_("/_Open image"),            NULL, open_image_cb, 0, NULL},
+       {N_("/_Save image..."),         NULL, save_file_cb, 0, NULL},
+};
+
+static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
+{
+#if HAVE_LIBCOMPFACE
+       if (textview->image) {
+               gint x, y, x1;
+               x1 = textview->text->allocation.width - WIDTH - 5;
+               gtk_text_view_buffer_to_window_coords(
+                       GTK_TEXT_VIEW(textview->text),
+                       GTK_TEXT_WINDOW_TEXT, x1, 5, &x, &y);
+               gtk_text_view_move_child(GTK_TEXT_VIEW(textview->text), 
+                       textview->image, x1, y);
+       }
+#endif
+}
+
+static void textview_size_allocate_cb  (GtkWidget      *widget,
+                                        GtkAllocation  *allocation,
+                                        gpointer        data)
+{
+       scrolled_cb(NULL, (TextView *)data);
+}
 
 TextView *textview_create(void)
 {
@@ -224,8 +268,9 @@ TextView *textview_create(void)
        GtkWidget *text;
        GtkTextBuffer *buffer;
        GtkClipboard *clipboard;
-       GtkItemFactory *link_popupfactory, *mail_popupfactory;
-       GtkWidget *link_popupmenu, *mail_popupmenu;
+       GtkItemFactory *link_popupfactory, *mail_popupfactory, *file_popupfactory;
+       GtkWidget *link_popupmenu, *mail_popupmenu, *file_popupmenu;
+       GtkAdjustment *adj;
        gint n_entries;
 
        debug_print("Creating text view...\n");
@@ -260,19 +305,23 @@ TextView *textview_create(void)
 
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
 
-       g_signal_connect(G_OBJECT(text), "key_press_event",
-                        G_CALLBACK(textview_key_pressed),
-                        textview);
-       g_signal_connect(G_OBJECT(text), "motion_notify_event",
-                        G_CALLBACK(textview_motion_notify),
-                        textview);
-       g_signal_connect(G_OBJECT(text), "leave_notify_event",
-                        G_CALLBACK(textview_leave_notify),
-                        textview);
-       g_signal_connect(G_OBJECT(text), "visibility_notify_event",
-                        G_CALLBACK(textview_visibility_notify),
+       g_signal_connect(G_OBJECT(text), "key-press-event",
+                        G_CALLBACK(textview_key_pressed), textview);
+       g_signal_connect(G_OBJECT(text), "motion-notify-event",
+                        G_CALLBACK(textview_motion_notify), textview);
+       g_signal_connect(G_OBJECT(text), "leave-notify-event",
+                        G_CALLBACK(textview_leave_notify), textview);
+       g_signal_connect(G_OBJECT(text), "visibility-notify-event",
+                        G_CALLBACK(textview_visibility_notify), textview);
+       adj = gtk_scrolled_window_get_vadjustment(
+               GTK_SCROLLED_WINDOW(scrolledwin));
+       g_signal_connect(G_OBJECT(adj), "value-changed",
+                        G_CALLBACK(scrolled_cb), textview);
+       g_signal_connect(G_OBJECT(text), "size_allocate",
+                        G_CALLBACK(textview_size_allocate_cb),
                         textview);
 
+
        gtk_widget_show(scrolledwin);
 
        vbox = gtk_vbox_new(FALSE, 0);
@@ -292,6 +341,12 @@ TextView *textview_create(void)
                                      "<UriPopupMenu>", &mail_popupfactory,
                                      textview);
 
+       n_entries = sizeof(textview_file_popup_entries) /
+               sizeof(textview_file_popup_entries[0]);
+       file_popupmenu = menu_create_items(textview_file_popup_entries, n_entries,
+                                     "<FilePopupMenu>", &file_popupfactory,
+                                     textview);
+
        textview->vbox               = vbox;
        textview->scrolledwin        = scrolledwin;
        textview->text               = text;
@@ -303,6 +358,8 @@ TextView *textview_create(void)
        textview->link_popup_factory = link_popupfactory;
        textview->mail_popup_menu    = mail_popupmenu;
        textview->mail_popup_factory = mail_popupfactory;
+       textview->file_popup_menu    = file_popupmenu;
+       textview->file_popup_factory = file_popupfactory;
 
        return textview;
 }
@@ -312,14 +369,14 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
        GtkTextBuffer *buffer;
        GtkTextTag *tag;
        static PangoFontDescription *font_desc, *bold_font_desc;
-
+       
        if (!font_desc)
                font_desc = pango_font_description_from_string
                        (NORMAL_FONT);
 
        if (!bold_font_desc) {
                bold_font_desc = pango_font_description_from_string
-                       (BOLD_FONT);
+                       (NORMAL_FONT);
                pango_font_description_set_weight
                        (bold_font_desc, PANGO_WEIGHT_BOLD);
        }
@@ -360,8 +417,7 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
                                   "foreground-gdk", &uri_color,
                                   "underline", PANGO_UNDERLINE_SINGLE,
                                   NULL);
-
-       g_signal_connect(G_OBJECT(tag), "event",
+       g_signal_connect(G_OBJECT(tag), "event",
                          G_CALLBACK(textview_uri_button_pressed), textview);
  }
 
@@ -372,15 +428,26 @@ void textview_init(TextView *textview)
        if (!text_cursor)
                text_cursor = gdk_cursor_new(GDK_XTERM);
 
-       textview_update_message_colors();
+       textview_reflect_prefs(textview);
        textview_set_all_headers(textview, FALSE);
        textview_set_font(textview, NULL);
        textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
 }
 
-void textview_update_message_colors(void)
+#define CHANGE_TAG_COLOR(tagname, color) { \
+       tag = gtk_text_tag_table_lookup(tags, tagname); \
+       if (tag) \
+               g_object_set(G_OBJECT(tag), "foreground-gdk", color, NULL); \
+}
+
+static void textview_update_message_colors(TextView *textview)
 {
        GdkColor black = {0, 0, 0, 0};
+       GdkColor colored_emphasis = {0, 0, 0, 0xcfff};
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+
+       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+       GtkTextTag *tag = NULL;
 
        if (prefs_common.enable_color) {
                /* grab the quote colors, converting from an int to a GdkColor */
@@ -394,35 +461,42 @@ void textview_update_message_colors(void)
                                               &uri_color);
                gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
                                               &signature_color);
+               emphasis_color = colored_emphasis;
        } else {
                quote_colors[0] = quote_colors[1] = quote_colors[2] = 
                        uri_color = emphasis_color = signature_color = black;
        }
+       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);
+
+}
+#undef CHANGE_TAG_COLOR
+
+void textview_reflect_prefs(TextView *textview)
+{
+       textview_set_font(textview, NULL);
+       textview_update_message_colors(textview);
+       gtk_text_view_set_cursor_visible(GTK_TEXT_VIEW(textview->text),
+                                        prefs_common.textview_cursor_visible);
 }
 
 void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
                           const gchar *file)
 {
-       FILE *fp;
-
-       if ((fp = fopen(file, "rb")) == NULL) {
-               FILE_OP_ERROR(file, "fopen");
-               return;
-       }
-
        textview_clear(textview);
 
        textview_add_parts(textview, mimeinfo);
 
-       fclose(fp);
-
        textview_set_position(textview, 0);
 }
 
 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
 {
-       GtkTextView *text;
-
        g_return_if_fail(mimeinfo != NULL);
        g_return_if_fail(fp != NULL);
 
@@ -442,14 +516,13 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                textview_add_parts(textview, mimeinfo);
        else
                textview_write_body(textview, mimeinfo);
-
 }
 
 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 {
        GtkTextView *text;
        GtkTextBuffer *buffer;
-       GtkTextIter iter;
+       GtkTextIter iter, start_iter;
        gchar buf[BUFFSIZE];
        GPtrArray *headers = NULL;
        const gchar *name;
@@ -467,7 +540,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
        if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
                FILE *fp;
 
-               fp = fopen(mimeinfo->data.filename, "rb");
+               fp = g_fopen(mimeinfo->data.filename, "rb");
                fseek(fp, mimeinfo->offset, SEEK_SET);
                headers = textview_scan_header(textview, fp);
                if (headers) {
@@ -494,9 +567,79 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 
        g_free(content_type);                      
 
-       if (mimeinfo->type != MIMETYPE_TEXT) {
+       if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT) {
                gtk_text_buffer_insert(buffer, &iter, buf, -1);
-       } else if (mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
+               if (mimeinfo->type == MIMETYPE_IMAGE  &&
+                   prefs_common.inline_img ) {
+                       GdkPixbuf *pixbuf;
+                       GError *error = NULL;
+                       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) {
+                               g_warning("Can't get the image file.");
+                               g_free(filename);
+                               return;
+                       }
+
+                       pixbuf = gdk_pixbuf_new_from_file(filename, &error);
+                       if (error != NULL) {
+                               g_warning("%s\n", error->message);
+                               g_error_free(error);
+                       }
+                       if (!pixbuf) {
+                               g_warning("Can't load the image.");
+                               g_free(filename);
+                               return;
+                       }
+
+                       if (prefs_common.resize_img) {
+                               int new_width, new_height;
+                               GdkPixbuf *scaled;
+                               image_viewer_get_resized_size(gdk_pixbuf_get_width(pixbuf),
+                                                gdk_pixbuf_get_height(pixbuf),
+                                                textview->scrolledwin->allocation.width - 100, 
+                                                gdk_pixbuf_get_height(pixbuf),
+                                                &new_width, &new_height);
+                               scaled = gdk_pixbuf_scale_simple
+                                       (pixbuf, new_width, new_height, GDK_INTERP_BILINEAR);
+
+                               g_object_unref(pixbuf);
+                               pixbuf = scaled;
+                       }
+
+                       uri_str = g_filename_to_uri(filename, NULL, NULL);
+                       if (uri_str) {
+                               uri = g_new(RemoteURI, 1);
+                               uri->uri = uri_str;
+                               uri->start = gtk_text_iter_get_offset(&iter);
+                               
+                               gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
+                               
+                               uri->end = uri->start + 1;
+                               uri->filename = procmime_get_part_file_name(mimeinfo);
+                               textview->uri_list =
+                                       g_slist_append(textview->uri_list, uri);
+                               
+                               gtk_text_buffer_insert(buffer, &iter, " ", 1);
+                               gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);    
+                               gtk_text_buffer_apply_tag_by_name(buffer, "link", 
+                                               &start_iter, &iter);
+                       } else {
+                               gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
+                               gtk_text_buffer_insert(buffer, &iter, " ", 1);
+                       }
+
+                       g_object_unref(pixbuf);
+                       g_free(filename);
+               }
+       } else if (mimeinfo->type == MIMETYPE_TEXT) {
                if (prefs_common.display_header && (charcount > 0))
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
 
@@ -642,15 +785,16 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 
        procmime_decode_content(mimeinfo);
 
-       if (!g_ascii_strcasecmp(mimeinfo->subtype, "html")) {
+       if (!g_ascii_strcasecmp(mimeinfo->subtype, "html") &&
+           prefs_common.render_html) {
                gchar *filename;
                
                filename = procmime_get_tmp_file_name(mimeinfo);
                if (procmime_get_part(filename, mimeinfo) == 0) {
-                       tmpfp = fopen(filename, "rb");
+                       tmpfp = g_fopen(filename, "rb");
                        textview_show_html(textview, tmpfp, conv);
                        fclose(tmpfp);
-                       unlink(filename);
+                       g_unlink(filename);
                }
                g_free(filename);
        } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
@@ -658,14 +802,14 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                
                filename = procmime_get_tmp_file_name(mimeinfo);
                if (procmime_get_part(filename, mimeinfo) == 0) {
-                       tmpfp = fopen(filename, "rb");
+                       tmpfp = g_fopen(filename, "rb");
                        textview_show_ertf(textview, tmpfp, conv);
                        fclose(tmpfp);
-                       unlink(filename);
+                       g_unlink(filename);
                }
                g_free(filename);
        } else {
-               tmpfp = fopen(mimeinfo->data.filename, "rb");
+               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);
                while ((fgets(buf, sizeof(buf), tmpfp) != NULL) && 
@@ -706,6 +850,7 @@ static void textview_show_html(TextView *textview, FILE *fp,
                } else
                        textview_write_line(textview, str, NULL);
        }
+       textview_write_line(textview, "\n", NULL);
        html_parser_destroy(parser);
 }
 
@@ -725,324 +870,6 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
        ertf_parser_destroy(parser);
 }
 
-/* get_uri_part() - retrieves a URI starting from scanpos.
-                   Returns TRUE if succesful */
-static gboolean get_uri_part(const gchar *start, const gchar *scanpos,
-                            const gchar **bp, const gchar **ep)
-{
-       const gchar *ep_;
-
-       g_return_val_if_fail(start != NULL, FALSE);
-       g_return_val_if_fail(scanpos != NULL, FALSE);
-       g_return_val_if_fail(bp != NULL, FALSE);
-       g_return_val_if_fail(ep != NULL, FALSE);
-
-       *bp = scanpos;
-
-       /* find end point of URI */
-       for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
-               if (!isgraph(*(const guchar *)ep_) ||
-                   !IS_ASCII(*(const guchar *)ep_) ||
-                   strchr("()<>\"", *ep_))
-                       break;
-       }
-
-       /* no punctuation at end of string */
-
-       /* FIXME: this stripping of trailing punctuations may bite with other URIs.
-        * should pass some URI type to this function and decide on that whether
-        * to perform punctuation stripping */
-
-#define IS_REAL_PUNCT(ch)      (ispunct(ch) && ((ch) != '/')) 
-
-       for (; ep_ - 1 > scanpos + 1 &&
-              IS_REAL_PUNCT(*(const guchar *)(ep_ - 1));
-            ep_--)
-               ;
-
-#undef IS_REAL_PUNCT
-
-       *ep = ep_;
-
-       return TRUE;            
-}
-
-static gchar *make_uri_string(const gchar *bp, const gchar *ep)
-{
-       return g_strndup(bp, ep - bp);
-}
-
-/* valid mail address characters */
-#define IS_RFC822_CHAR(ch) \
-       (IS_ASCII(ch) && \
-        (ch) > 32   && \
-        (ch) != 127 && \
-        !isspace(ch) && \
-        !strchr("(),;<>\"", (ch)))
-
-/* alphabet and number within 7bit ASCII */
-#define IS_ASCII_ALNUM(ch)     (IS_ASCII(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)
-{
-       /* more complex than the uri part because we need to scan back and forward starting from
-        * the scan position. */
-       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
-        * closure stack stores the last encountered quotes. */
-       gchar closure_stack[128];
-       gchar *ptr = closure_stack;
-
-       g_return_val_if_fail(start != NULL, FALSE);
-       g_return_val_if_fail(scanpos != NULL, FALSE);
-       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(*(const guchar *)bp_); bp_--)
-               ;
-
-       /* TODO: should start with an alnum? */
-       bp_++;
-       for (; bp_ < scanpos && !IS_ASCII_ALNUM(*(const guchar *)bp_); bp_++)
-               ;
-
-       if (bp_ != scanpos) {
-               /* scan end of address */
-               for (ep_ = scanpos + 1;
-                    *ep_ && IS_RFC822_CHAR(*(const guchar *)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(*(const guchar *)ep_);
-                    --ep_)
-                       ;
-               ep_++;
-
-               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;
-
-       if (*ep_ && *(bp_ - 1) == '"' && *(ep_) == '"' 
-       && *(ep_ + 1) == ' ' && *(ep_ + 2) == '<'
-       && IS_RFC822_CHAR(*(ep_ + 3))) {
-               /* this informative part with an @ in it is 
-                * followed by the email address */
-               ep_ += 3;
-               
-               /* go to matching '>' (or next non-rfc822 char, like \n) */
-               for (; *ep_ != '>' && *ep != '\0' && IS_RFC822_CHAR(*ep_); ep_++)
-                       ;
-                       
-               /* include the bracket */
-               if (*ep_ == '>') ep_++;
-               
-               /* include the leading quote */         
-               bp_--;
-
-               *ep = ep_;
-               *bp = bp_;
-               return TRUE;
-       }
-
-       /* skip if it's between quotes "'alfons@proteus.demon.nl'" <alfons@proteus.demon.nl> */
-       if (bp_ - 1 > start && IS_QUOTE(*(bp_ - 1)) && IS_QUOTE(*ep_))
-               return FALSE;
-
-       /* see if this is <bracketed>; in this case we also scan for the informative part. */
-       if (bp_ - 1 <= start || *(bp_ - 1) != '<' || *ep_ != '>')
-               return TRUE;
-
-#define FULL_STACK()   ((size_t) (ptr - closure_stack) >= sizeof closure_stack)
-#define IN_STACK()     (ptr > closure_stack)
-/* has underrun check */
-#define POP_STACK()    if(IN_STACK()) --ptr
-/* has overrun check */
-#define PUSH_STACK(c)  if(!FULL_STACK()) *ptr++ = (c); else return TRUE
-/* has underrun check */
-#define PEEK_STACK()   (IN_STACK() ? *(ptr - 1) : 0)
-
-       ep_++;
-
-       /* scan for the informative part. */
-       for (bp_ -= 2; bp_ >= start; bp_--) {
-               /* if closure on the stack keep scanning */
-               if (PEEK_STACK() == *bp_) {
-                       POP_STACK();
-                       continue;
-               }
-               if (*bp_ == '\'' || *bp_ == '"') {
-                       PUSH_STACK(*bp_);
-                       continue;
-               }
-
-               /* if nothing in the closure stack, do the special conditions
-                * the following if..else expression simply checks whether 
-                * a token is acceptable. if not acceptable, the clause
-                * should terminate the loop with a 'break' */
-               if (!PEEK_STACK()) {
-                       if (*bp_ == '-'
-                       && (((bp_ - 1) >= start) && isalnum(*(bp_ - 1)))
-                       && (((bp_ + 1) < ep_)    && isalnum(*(bp_ + 1)))) {
-                               /* hyphens are allowed, but only in
-                                  between alnums */
-                       } else if (!ispunct(*bp_)) {
-                               /* but anything not being a punctiation
-                                  is ok */
-                       } else {
-                               break; /* anything else is rejected */
-                       }
-               }
-       }
-
-       bp_++;
-
-#undef PEEK_STACK
-#undef PUSH_STACK
-#undef POP_STACK
-#undef IN_STACK
-#undef FULL_STACK
-
-       /* scan forward (should start with an alnum) */
-       for (; *bp_ != '<' && isspace(*bp_) && *bp_ != '"'; bp_++)
-               ;
-
-       *ep = ep_;
-       *bp = bp_;
-       
-       return result;
-}
-
-#undef IS_QUOTE
-#undef IS_ASCII_ALNUM
-#undef IS_RFC822_CHAR
-
-static gchar *make_email_string(const gchar *bp, const gchar *ep)
-{
-       /* returns a mailto: URI; mailto: is also used to detect the
-        * uri type later on in the button_pressed signal handler */
-       gchar *tmp;
-       gchar *result;
-
-       tmp = g_strndup(bp, ep - bp);
-       result = g_strconcat("mailto:", tmp, NULL);
-       g_free(tmp);
-
-       return result;
-}
-
-static gchar *make_http_string(const gchar *bp, const gchar *ep)
-{
-       /* returns an http: URI; */
-       gchar *tmp;
-       gchar *result;
-
-       tmp = g_strndup(bp, ep - bp);
-       result = g_strconcat("http://", tmp, NULL);
-       g_free(tmp);
-
-       return result;
-}
-
 #define ADD_TXT_POS(bp_, ep_, pti_) \
        if ((last->next = alloca(sizeof(struct txtpos))) != NULL) { \
                last = last->next; \
@@ -1060,12 +887,14 @@ static gchar *make_http_string(const gchar *bp, const gchar *ep)
 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);
        GtkTextIter iter;
-
+       gchar *mybuf = g_strdup(linebuf);
+       
        /* parse table - in order of priority */
        struct table {
                const gchar *needle; /* token */
@@ -1077,7 +906,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);
@@ -1102,10 +932,15 @@ static void textview_make_clickable_parts(TextView *textview,
                struct txtpos   *next;          /* next */
        } head = {NULL, NULL, 0,  NULL}, *last = &head;
 
+       if (!g_utf8_validate(linebuf, -1, NULL)) {
+               mybuf = g_malloc(strlen(linebuf)*2 +1);
+               conv_localetodisp(mybuf, strlen(linebuf)*2 +1, linebuf);
+       }
+
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
        /* parse for clickable parts, and build a list of begin and end positions  */
-       for (walk = linebuf, n = 0;;) {
+       for (walk = mybuf, n = 0;;) {
                gint last_index = PARSE_ELEMS;
                gchar *scanpos = NULL;
 
@@ -1124,7 +959,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;
@@ -1137,7 +972,7 @@ static void textview_make_clickable_parts(TextView *textview,
 
        /* colorize this line */
        if (head.next) {
-               const gchar *normal_text = linebuf;
+               const gchar *normal_text = mybuf;
 
                /* insert URIs */
                for (last = head.next; last != NULL;
@@ -1157,6 +992,7 @@ static void textview_make_clickable_parts(TextView *textview,
                                (buffer, &iter, last->bp, last->ep - last->bp,
                                 uri_tag, fg_tag, NULL);
                        uri->end = gtk_text_iter_get_offset(&iter);
+                       uri->filename = NULL;
                        textview->uri_list =
                                g_slist_append(textview->uri_list, uri);
                }
@@ -1166,8 +1002,9 @@ static void textview_make_clickable_parts(TextView *textview,
                                (buffer, &iter, normal_text, -1, fg_tag, NULL);
        } else {
                gtk_text_buffer_insert_with_tags_by_name
-                       (buffer, &iter, linebuf, -1, fg_tag, NULL);
+                       (buffer, &iter, mybuf, -1, fg_tag, NULL);
        }
+       g_free(mybuf);
 }
 
 #undef ADD_TXT_POS
@@ -1190,8 +1027,8 @@ static void textview_write_line(TextView *textview, const gchar *str,
        if (!conv)
                strncpy2(buf, str, sizeof(buf));
        else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
-               conv_utf8todisp(buf, sizeof(buf), str);
-
+               conv_localetodisp(buf, sizeof(buf), str);
+               
        strcrchomp(buf);
        //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
        fg_color = NULL;
@@ -1227,10 +1064,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                textview->is_in_signature = TRUE;
        }
 
-       if (prefs_common.enable_color)
-               textview_make_clickable_parts(textview, fg_color, "link", buf);
-       else
-               textview_make_clickable_parts(textview, fg_color, NULL, buf);
+       textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
 }
 
 void textview_write_link(TextView *textview, const gchar *str,
@@ -1244,7 +1078,9 @@ void textview_write_link(TextView *textview, const gchar *str,
        gchar *bufp;
        RemoteURI *r_uri;
 
-       if (*str == '\0')
+       if (!str || *str == '\0')
+               return;
+       if (!uri)
                return;
 
        text = GTK_TEXT_VIEW(textview->text);
@@ -1256,12 +1092,21 @@ void textview_write_link(TextView *textview, const gchar *str,
        else if (conv_convert(conv, buf, sizeof(buf), str) < 0)
                conv_utf8todisp(buf, sizeof(buf), str);
 
+       if (g_utf8_validate(buf, -1, NULL) == FALSE)
+               return;
+
        strcrchomp(buf);
 
        gtk_text_buffer_get_end_iter(buffer, &iter);
+       for (bufp = buf; *bufp != '\0'; bufp = g_utf8_next_char(bufp)) {
+               gunichar ch;
 
-       for (bufp = buf; isspace(*(guchar *)bufp); bufp++)
-               gtk_text_buffer_insert(buffer, &iter, bufp, 1);
+               ch = g_utf8_get_char(bufp);
+               if (!g_unichar_isspace(ch))
+                       break;
+       }
+       if (bufp > buf)
+               gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
 
        if (prefs_common.enable_color) {
                link_color = &uri_color;
@@ -1272,6 +1117,7 @@ void textview_write_link(TextView *textview, const gchar *str,
        gtk_text_buffer_insert_with_tags_by_name
                (buffer, &iter, bufp, -1, "link", NULL);
        r_uri->end = gtk_text_iter_get_offset(&iter);
+       r_uri->filename = NULL;
        textview->uri_list = g_slist_append(textview->uri_list, r_uri);
 }
 
@@ -1288,6 +1134,9 @@ void textview_clear(TextView *textview)
        textview->uri_list = NULL;
 
        textview->body_pos = 0;
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       textview->image = NULL;
 }
 
 void textview_destroy(TextView *textview)
@@ -1303,10 +1152,39 @@ void textview_set_all_headers(TextView *textview, gboolean all_headers)
        textview->show_all_headers = all_headers;
 }
 
+#define CHANGE_TAG_FONT(tagname, font) { \
+       tag = gtk_text_tag_table_lookup(tags, tagname); \
+       if (tag) \
+               g_object_set(G_OBJECT(tag), "font-desc", font, NULL); \
+}
+
 void textview_set_font(TextView *textview, const gchar *codeset)
 {
+       GtkTextTag *tag;
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+       GtkTextTagTable *tags = gtk_text_buffer_get_tag_table(buffer);
+       
+       if (NORMAL_FONT) {
+               PangoFontDescription *font_desc, *bold_font_desc;
+               font_desc = pango_font_description_from_string
+                                               (NORMAL_FONT);
+               bold_font_desc = pango_font_description_from_string
+                                               (NORMAL_FONT);
+               if (font_desc) {
+                       gtk_widget_modify_font(textview->text, font_desc);
+                       CHANGE_TAG_FONT("header", font_desc);
+                       pango_font_description_free(font_desc);
+               }
+               if (bold_font_desc) {
+                       pango_font_description_set_weight
+                               (bold_font_desc, PANGO_WEIGHT_BOLD);
+                       CHANGE_TAG_FONT("header_title", bold_font_desc);
+                       pango_font_description_free(bold_font_desc);
+               }
+       }
+
        if (prefs_common.textfont) {
-               PangoFontDescription *font_desc = NULL;
+               PangoFontDescription *font_desc;
 
                font_desc = pango_font_description_from_string
                                                (prefs_common.textfont);
@@ -1420,6 +1298,73 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        return sorted_headers;
 }
 
+#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;
+       
+       if (!msginfo)
+               goto bail;
+
+       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);
+       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;
+       
+}
+#endif
+
 static void textview_show_header(TextView *textview, GPtrArray *headers)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
@@ -1449,14 +1394,7 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                    procheader_headername_equal(header->name, "Cc"))
                        unfold_line(header->body);
 
-#warning FIXME_GTK2
-#if 0
-               if (textview->text_is_mb == TRUE)
-                       conv_unreadable_locale(header->body);
-#endif
-
-               if (prefs_common.enable_color &&
-                   (procheader_headername_equal(header->name, "X-Mailer") ||
+               if ((procheader_headername_equal(header->name, "X-Mailer") ||
                     procheader_headername_equal(header->name,
                                                 "X-Newsreader")) &&
                    strstr(header->body, "Sylpheed") != NULL) {
@@ -1464,17 +1402,23 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                        gtk_text_buffer_insert_with_tags_by_name
                                (buffer, &iter, header->body, -1,
                                 "header", "emphasis", NULL);
-               } else if (prefs_common.enable_color) {
-                       textview_make_clickable_parts(textview, "header", "link",
-                                                     header->body);
                } else {
-                       textview_make_clickable_parts(textview, "header", NULL,
-                                                     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");
+                       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);
        }
+#if HAVE_LIBCOMPFACE
+       textview_show_xface(textview);
+#endif
 }
 
 gboolean textview_search_string(TextView *textview, const gchar *str,
@@ -1482,9 +1426,8 @@ gboolean textview_search_string(TextView *textview, const gchar *str,
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        GtkTextBuffer *buffer;
-       GtkTextIter iter, end_iter;
+       GtkTextIter iter, match_pos;
        GtkTextMark *mark;
-       gint pos;
        gint len;
 
        g_return_val_if_fail(str != NULL, FALSE);
@@ -1496,12 +1439,14 @@ gboolean textview_search_string(TextView *textview, const gchar *str,
 
        mark = gtk_text_buffer_get_insert(buffer);
        gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
-       pos = gtk_text_iter_get_offset(&iter);
 
-       if ((pos = gtkut_text_buffer_find(buffer, pos, str, case_sens)) != -1) {
-               gtk_text_buffer_get_iter_at_offset(buffer, &end_iter, pos);
-               gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos + len);
-               gtk_text_buffer_select_range(buffer, &iter, &end_iter);
+       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;
        }
@@ -1514,47 +1459,31 @@ gboolean textview_search_string_backward(TextView *textview, const gchar *str,
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        GtkTextBuffer *buffer;
-       GtkTextIter iter, end_iter;
+       GtkTextIter iter, match_pos;
        GtkTextMark *mark;
-       gint pos;
-       gunichar *wcs;
        gint len;
-       glong items_read = 0, items_written = 0;
-       GError *error = NULL;
-       gboolean found = FALSE;
 
        g_return_val_if_fail(str != NULL, FALSE);
 
        buffer = gtk_text_view_get_buffer(text);
 
-       wcs = g_utf8_to_ucs4(str, -1, &items_read, &items_written, &error);
-       if (error != NULL) {
-               g_warning("An error occured while converting a string from UTF-8 to UCS-4: %s\n", error->message);
-               g_error_free(error);
-       }
-       if (!wcs || items_written <= 0) return FALSE;
-       len = (gint)items_written;
+       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);
 
-       while (gtk_text_iter_backward_char(&iter)) {
-               pos = gtk_text_iter_get_offset(&iter);
-               if (gtkut_text_buffer_match_string
-                       (buffer, pos, wcs, len, case_sens) == TRUE) {
-                       gtk_text_buffer_get_iter_at_offset(buffer, &iter, pos);
-                       gtk_text_buffer_get_iter_at_offset
-                               (buffer, &end_iter, pos + len);
-                       gtk_text_buffer_select_range(buffer, &iter, &end_iter);
-                       gtk_text_view_scroll_to_mark
-                               (text, mark, 0.0, FALSE, 0.0, 0.0);
-                       found = TRUE;
-                       break;
-               }
+       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;
        }
 
-       g_free(wcs);
-       return found;
+       return FALSE;
 }
 
 void textview_scroll_one_line(TextView *textview, gboolean up)
@@ -1649,6 +1578,8 @@ static void textview_smooth_scroll_do(TextView *textview,
 
        vadj->value = last_value;
        g_signal_emit_by_name(G_OBJECT(vadj), "value_changed", 0);
+
+       gtk_widget_queue_draw(GTK_WIDGET(text));
 }
 
 static void textview_smooth_scroll_one_line(TextView *textview, gboolean up)
@@ -1776,6 +1707,7 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_y:
        case GDK_t:
        case GDK_l:
+       case GDK_c:
                if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
                        KEY_PRESS_EVENT_STOP();
                        mimeview_pass_key_press_event(messageview->mimeview,
@@ -1921,24 +1853,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,
@@ -2000,8 +1915,6 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
 {
        GdkEventButton *bevent;
        RemoteURI *uri = NULL;
-       GSList *cur;
-       gchar *trimmed_uri;
 
        if (!event)
                return FALSE;
@@ -2041,7 +1954,7 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                                compose_new(account, uri->uri + 7, NULL);
                        }
                        return TRUE;
-               } else {
+               } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
                        if (bevent->button == 1 &&
                            textview_uri_security_check(textview, uri) == TRUE) 
                                        open_uri(uri->uri,
@@ -2055,6 +1968,16 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                                               bevent->button, bevent->time);
                        }
                        return TRUE;
+               } else {
+                       if (bevent->button == 3) {
+                               g_object_set_data(
+                                       G_OBJECT(textview->file_popup_menu),
+                                       "menu_button", uri);
+                               gtk_menu_popup(GTK_MENU(textview->file_popup_menu), 
+                                              NULL, NULL, NULL, NULL, 
+                                              bevent->button, bevent->time);
+                               return TRUE;
+                       }
                }
        }
 
@@ -2108,11 +2031,12 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
 
                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_with_type(_("Warning"), msg,
-                                           GTK_STOCK_YES, GTK_STOCK_NO,
-                                           NULL, NULL, ALERT_WARNING);
+               aval = alertpanel_full(_("Fake URL warning"), msg,
+                                      GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
+                                      NULL, ALERT_WARNING, G_ALERTALTERNATE);
                g_free(msg);
                if (aval == G_ALERTDEFAULT)
                        retval = TRUE;
@@ -2130,6 +2054,7 @@ static void textview_uri_list_remove_all(GSList *uri_list)
        for (cur = uri_list; cur != NULL; cur = cur->next) {
                if (cur->data) {
                        g_free(((RemoteURI *)cur->data)->uri);
+                       g_free(((RemoteURI *)cur->data)->filename);
                        g_free(cur->data);
                }
        }
@@ -2151,6 +2076,131 @@ static void open_uri_cb (TextView *textview, guint action, void *data)
                          NULL);
 }
 
+static void open_image_cb (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
+                                          "menu_button");
+
+       static gchar *default_cmdline = DEFAULT_IMAGE_VIEWER_CMD;
+       gchar buf[1024];
+       const gchar *cmd;
+       const gchar *def_cmd;
+       const gchar *p;
+       gchar *filename = NULL;
+       gchar *tmp_filename = NULL;
+
+       if (uri == NULL)
+               return;
+
+       if (uri->filename == NULL)
+               return;
+       
+       filename = g_strdup(uri->filename);
+       
+       if (!g_utf8_validate(filename, -1, NULL)) {
+               gchar *tmp = conv_filename_to_utf8(filename);
+               g_free(filename);
+               filename = tmp;
+       }
+
+       subst_for_filename(filename);
+
+       tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
+       copy_file(tmp_filename, filename, FALSE);
+       g_free(tmp_filename);
+
+       cmd = prefs_common.mime_image_viewer;
+       def_cmd = default_cmdline;
+       
+       if (cmd && (p = strchr(cmd, '%')) && *(p + 1) == 's' &&
+           !strchr(p + 2, '%'))
+               g_snprintf(buf, sizeof(buf), cmd, filename);
+       else {
+               if (cmd)
+                       g_warning("Image viewer command line is invalid: '%s'", cmd);
+               if (def_cmd)
+                       g_snprintf(buf, sizeof(buf), def_cmd, filename);
+               else
+                       return;
+       }
+
+       execute_command_line(buf, TRUE);
+
+       g_free(filename);
+
+       g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
+                         NULL);
+}
+
+static void save_file_cb (TextView *textview, guint action, void *data)
+{
+       RemoteURI *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
+                                          "menu_button");
+       gchar *filename = NULL;
+       gchar *filepath = NULL;
+       gchar *filedir = NULL;
+       gchar *tmp_filename = NULL;
+       if (uri == NULL)
+               return;
+
+       if (uri->filename == NULL)
+               return;
+       
+       filename = g_strdup(uri->filename);
+       
+       if (!g_utf8_validate(filename, -1, NULL)) {
+               gchar *tmp = conv_filename_to_utf8(filename);
+               g_free(filename);
+               filename = tmp;
+       }
+
+       subst_for_filename(filename);
+       
+       if (prefs_common.attach_save_dir)
+               filepath = g_strconcat(prefs_common.attach_save_dir,
+                                      G_DIR_SEPARATOR_S, filename, NULL);
+       else
+               filepath = g_strdup(filename);
+
+       g_free(filename);
+
+       filename = filesel_select_file_save(_("Save as"), filepath);
+       if (!filename) {
+               g_free(filepath);
+               return;
+       }
+
+       if (is_file_exist(filename)) {
+               AlertValue aval;
+               gchar *res;
+               
+               res = g_strdup_printf(_("Overwrite existing file '%s'?"),
+                                     filename);
+               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_OK, 
+                                 GTK_STOCK_CANCEL, NULL);
+               g_free(res);                                      
+               if (G_ALERTDEFAULT != aval) 
+                       return;
+       }
+
+       tmp_filename = g_filename_from_uri(uri->uri, NULL, NULL);
+       copy_file(tmp_filename, filename, FALSE);
+       g_free(tmp_filename);
+       
+       filedir = g_path_get_dirname(filename);
+       if (filedir && strcmp(filedir, ".")) {
+               if (prefs_common.attach_save_dir)
+                       g_free(prefs_common.attach_save_dir);
+               prefs_common.attach_save_dir = g_strdup(filedir);
+       }
+
+       g_free(filedir);
+       g_free(filepath);
+
+       g_object_set_data(G_OBJECT(textview->file_popup_menu), "menu_button",
+                         NULL);
+}
+
 static void copy_uri_cb        (TextView *textview, guint action, void *data)
 {
        RemoteURI *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
@@ -2158,7 +2208,8 @@ static void copy_uri_cb   (TextView *textview, guint action, void *data)
        if (uri == NULL)
                return;
 
-       gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE), uri->uri, -1);
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri, -1);
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri, -1);
        g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
                          NULL);
 }
@@ -2205,18 +2256,14 @@ static void mail_to_uri_cb (TextView *textview, guint action, void *data)
 
 static void copy_mail_to_uri_cb        (TextView *textview, guint action, void *data)
 {
-       gchar *fromaddress;
        RemoteURI *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
                                           "menu_button");
        if (uri == NULL)
                return;
 
-       fromaddress = g_strdup(uri->uri + 7);
-       
-       gtk_clipboard_set_text(gtk_clipboard_get(GDK_NONE), fromaddress, -1);
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), uri->uri +7, -1);
+       gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), uri->uri +7, -1);
        g_object_set_data(G_OBJECT(textview->mail_popup_menu), "menu_button",
                          NULL);
-
-       g_free(fromaddress);
 }