2010-12-22 [colin] 3.7.8cvs19
[claws.git] / src / textview.c
index 70ba0364b70dc8208e96c64d1940ecf8e00dc47d..7e4c211a330a48f036e7685e85828be32a5aa434 100644 (file)
@@ -1,10 +1,10 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2007 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2009 Hiroyuki Yamamoto and the Claws Mail team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
@@ -27,9 +27,7 @@
 #include <glib/gi18n.h>
 #include <gdk/gdk.h>
 #include <gdk/gdkkeysyms.h>
-#include <gtk/gtkvbox.h>
-#include <gtk/gtkscrolledwindow.h>
-#include <gtk/gtksignal.h>
+#include <gtk/gtk.h>
 #include <stdio.h>
 #include <ctype.h>
 #include <string.h>
@@ -68,6 +66,8 @@
 #include "base64.h"
 #include "inputdialog.h"
 #include "timing.h"
+#include "tags.h"
+#include "addrindex.h"
 
 static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
@@ -98,7 +98,7 @@ static GdkColor emphasis_color = {
        (gulong)0,
        (gushort)0,
        (gushort)0,
-       (gushort)0xcfff
+       (gushort)0
 };
 
 static GdkCursor *hand_cursor = NULL;
@@ -185,45 +185,45 @@ static void textview_toggle_quote         (TextView       *textview,
                                                 ClickableText  *uri,
                                                 gboolean        expand_only);
 
-static void open_uri_cb                                (TextView       *textview,
-                                                guint           action,
-                                                void           *data);
-static void copy_uri_cb                                (TextView       *textview,
-                                                guint           action,
-                                                void           *data);
-static void add_uri_to_addrbook_cb             (TextView       *textview, 
-                                                guint           action, 
-                                                void           *data);
-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);
-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 with Web browser"), NULL, open_uri_cb, 0, NULL},
-       {N_("/Copy this _link"),        NULL, copy_uri_cb, 0, NULL},
+static void open_uri_cb                                (GtkAction      *action,
+                                                TextView       *textview);
+static void copy_uri_cb                                (GtkAction      *action,
+                                                TextView       *textview);
+static void add_uri_to_addrbook_cb             (GtkAction      *action,
+                                                TextView       *textview);
+static void reply_to_uri_cb                    (GtkAction      *action,
+                                                TextView       *textview);
+static void mail_to_uri_cb                     (GtkAction      *action,
+                                                TextView       *textview);
+static void copy_mail_to_uri_cb                        (GtkAction      *action,
+                                                TextView       *textview);
+static void save_file_cb                       (GtkAction      *action,
+                                                TextView       *textview);
+static void open_image_cb                      (GtkAction      *action,
+                                                TextView       *textview);
+static void textview_show_tags(TextView *textview);
+
+static GtkActionEntry textview_link_popup_entries[] = 
+{
+       {"TextviewPopupLink",                   NULL, "TextviewPopupLink" },
+       {"TextviewPopupLink/Open",              NULL, N_("_Open in web browser"), NULL, NULL, G_CALLBACK(open_uri_cb) },
+       {"TextviewPopupLink/Copy",              NULL, N_("Copy this _link"), NULL, NULL, G_CALLBACK(copy_uri_cb) },
 };
 
-static GtkItemFactoryEntry textview_mail_popup_entries[] = 
+static GtkActionEntry textview_mail_popup_entries[] = 
 {
-       {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},
+       {"TextviewPopupMail",                   NULL, "TextviewPopupMail" },
+       {"TextviewPopupMail/Compose",           NULL, N_("Compose _new message"), NULL, NULL, G_CALLBACK(mail_to_uri_cb) },
+       {"TextviewPopupMail/ReplyTo",           NULL, N_("_Reply to this address"), NULL, NULL, G_CALLBACK(reply_to_uri_cb) },
+       {"TextviewPopupMail/AddAB",             NULL, N_("Add to _Address book"), NULL, NULL, G_CALLBACK(add_uri_to_addrbook_cb) },
+       {"TextviewPopupMail/Copy",              NULL, N_("Copy this add_ress"), NULL, NULL, G_CALLBACK(copy_mail_to_uri_cb) },
 };
 
-static GtkItemFactoryEntry textview_file_popup_entries[] = 
+static GtkActionEntry textview_file_popup_entries[] = 
 {
-       {N_("/_Open image"),            NULL, open_image_cb, 0, NULL},
-       {N_("/_Save image..."),         NULL, save_file_cb, 0, NULL},
+       {"TextviewPopupFile",                   NULL, "TextviewPopupFile" },
+       {"TextviewPopupFile/Open",              NULL, N_("_Open image"), NULL, NULL, G_CALLBACK(open_image_cb) },
+       {"TextviewPopupFile/Save",              NULL, N_("_Save image..."), NULL, NULL, G_CALLBACK(save_file_cb) },
 };
 
 static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
@@ -258,10 +258,7 @@ TextView *textview_create(void)
        GtkWidget *text;
        GtkTextBuffer *buffer;
        GtkClipboard *clipboard;
-       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");
        textview = g_new0(TextView, 1);
@@ -291,7 +288,7 @@ TextView *textview_create(void)
 
        gtk_widget_ensure_style(text);
 
-       gtk_widget_ref(scrolledwin);
+       g_object_ref(scrolledwin);
 
        gtk_container_add(GTK_CONTAINER(scrolledwin), text);
 
@@ -319,23 +316,52 @@ TextView *textview_create(void)
 
        gtk_widget_show(vbox);
 
-       n_entries = sizeof(textview_link_popup_entries) /
-               sizeof(textview_link_popup_entries[0]);
-       link_popupmenu = menu_create_items(textview_link_popup_entries, n_entries,
-                                     "<UriPopupMenu>", &link_popupfactory,
-                                     textview);
-
-       n_entries = sizeof(textview_mail_popup_entries) /
-               sizeof(textview_mail_popup_entries[0]);
-       mail_popupmenu = menu_create_items(textview_mail_popup_entries, n_entries,
-                                     "<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->ui_manager = gtk_ui_manager_new();
+       textview->link_action_group = cm_menu_create_action_group_full(textview->ui_manager,
+                       "TextviewPopupLink",
+                       textview_link_popup_entries,
+                       G_N_ELEMENTS(textview_link_popup_entries), (gpointer)textview);
+       textview->mail_action_group = cm_menu_create_action_group_full(textview->ui_manager,
+                       "TextviewPopupMail",
+                       textview_mail_popup_entries,
+                       G_N_ELEMENTS(textview_mail_popup_entries), (gpointer)textview);
+       textview->file_action_group = cm_menu_create_action_group_full(textview->ui_manager,
+                       "TextviewPopupFile",
+                       textview_file_popup_entries,
+                       G_N_ELEMENTS(textview_file_popup_entries), (gpointer)textview);
+
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, "/", "Menus", "Menus", GTK_UI_MANAGER_MENUBAR)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus", "TextviewPopupLink", "TextviewPopupLink", GTK_UI_MANAGER_MENU)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus", "TextviewPopupMail", "TextviewPopupMail", GTK_UI_MANAGER_MENU)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus", "TextviewPopupFile", "TextviewPopupFile", GTK_UI_MANAGER_MENU)
+
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupLink", "Open", "TextviewPopupLink/Open", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupLink", "Copy", "TextviewPopupLink/Copy", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupMail", "Compose", "TextviewPopupMail/Compose", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupMail", "ReplyTo", "TextviewPopupMail/ReplyTo", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupMail", "AddAB", "TextviewPopupMail/AddAB", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupMail", "Copy", "TextviewPopupMail/Copy", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupFile", "Open", "TextviewPopupFile/Open", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(textview->ui_manager, 
+                       "/Menus/TextviewPopupFile", "Save", "TextviewPopupFile/Save", GTK_UI_MANAGER_MENUITEM)
+
+       textview->link_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
+                               gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupLink")) );
+       textview->mail_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
+                               gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupMail")) );
+       textview->file_popup_menu = gtk_menu_item_get_submenu(GTK_MENU_ITEM(
+                               gtk_ui_manager_get_widget(textview->ui_manager, "/Menus/TextviewPopupFile")) );
 
        textview->vbox               = vbox;
        textview->scrolledwin        = scrolledwin;
@@ -344,12 +370,6 @@ TextView *textview_create(void)
        textview->body_pos           = 0;
        textview->show_all_headers   = FALSE;
        textview->last_buttonpress   = GDK_NOTHING;
-       textview->link_popup_menu    = link_popupmenu;
-       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;
        textview->image              = NULL;
        return textview;
 }
@@ -358,17 +378,33 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
 {
        GtkTextBuffer *buffer;
        GtkTextTag *tag, *qtag;
+       static GdkColor yellow, black;
+       static gboolean color_init = FALSE;
        static PangoFontDescription *font_desc, *bold_font_desc;
        
+       if (!color_init) {
+               gdk_color_parse("#f5f6be", &yellow);
+               gdk_color_parse("#000000", &black);
+               color_init = gdk_colormap_alloc_color(
+                       gdk_colormap_get_system(), &yellow, FALSE, TRUE);
+               color_init &= gdk_colormap_alloc_color(
+                       gdk_colormap_get_system(), &black, FALSE, TRUE);
+       }
+
        if (!font_desc)
                font_desc = pango_font_description_from_string
                        (NORMAL_FONT);
 
        if (!bold_font_desc) {
-               bold_font_desc = pango_font_description_from_string
-                       (NORMAL_FONT);
-               pango_font_description_set_weight
-                       (bold_font_desc, PANGO_WEIGHT_BOLD);
+               if (prefs_common.derive_from_normal_font || !BOLD_FONT) {
+                       bold_font_desc = pango_font_description_from_string
+                               (NORMAL_FONT);
+                       pango_font_description_set_weight
+                               (bold_font_desc, PANGO_WEIGHT_BOLD);
+               } else {
+                       bold_font_desc = pango_font_description_from_string
+                               (BOLD_FONT);
+               }
        }
 
        buffer = gtk_text_view_get_buffer(text);
@@ -421,6 +457,10 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
                                "foreground-gdk", &quote_colors[2],
                                NULL);
        }
+       gtk_text_buffer_create_tag(buffer, "tags",
+                       "foreground-gdk", &black,
+                       "paragraph-background-gdk", &yellow,
+                       NULL);
        gtk_text_buffer_create_tag(buffer, "emphasis",
                        "foreground-gdk", &emphasis_color,
                        NULL);
@@ -439,6 +479,10 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
                          G_CALLBACK(textview_uri_button_pressed), textview);
        g_signal_connect(G_OBJECT(tag), "event",
                          G_CALLBACK(textview_uri_button_pressed), textview);
+/*     if (font_desc)
+               pango_font_description_free(font_desc);
+       if (bold_font_desc)
+               pango_font_description_free(bold_font_desc);*/
  }
 
 void textview_init(TextView *textview)
@@ -456,24 +500,15 @@ void textview_init(TextView *textview)
        textview_create_tags(GTK_TEXT_VIEW(textview->text), textview);
 }
 
-#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", 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)
 {
        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);
@@ -495,7 +530,8 @@ static void textview_update_message_colors(TextView *textview)
                                               &uri_color);
                gtkut_convert_int_to_gdk_color(prefs_common.signature_col,
                                               &signature_color);
-               emphasis_color = colored_emphasis;
+               gtkut_convert_int_to_gdk_color(prefs_common.emphasis_col,
+                                              &emphasis_color);
        }
        if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
@@ -528,27 +564,11 @@ void textview_reflect_prefs(TextView *textview)
                                         prefs_common.textview_cursor_visible);
 }
 
-void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
-                          const gchar *file)
-{
-       textview->loading = TRUE;
-       textview->stop_loading = FALSE;
-
-       textview_clear(textview);
-
-       textview_add_parts(textview, mimeinfo);
-
-       textview_set_position(textview, 0);
-
-       textview->loading = FALSE;
-       textview->stop_loading = FALSE;
-}
-
 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
 {
        START_TIMING("");
-       g_return_if_fail(mimeinfo != NULL);
-       g_return_if_fail(fp != NULL);
+       cm_return_if_fail(mimeinfo != NULL);
+       cm_return_if_fail(fp != NULL);
 
        if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
            ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
@@ -593,7 +613,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
        gint charcount;
        START_TIMING("");
 
-       g_return_if_fail(mimeinfo != NULL);
+       cm_return_if_fail(mimeinfo != NULL);
        text = GTK_TEXT_VIEW(textview->text);
        buffer = gtk_text_view_get_buffer(text);
        charcount = gtk_text_buffer_get_char_count(buffer);
@@ -611,13 +631,23 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 
        if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
                FILE *fp;
-
-               fp = g_fopen(mimeinfo->data.filename, "rb");
+               if (mimeinfo->content == MIMECONTENT_MEM)
+                       fp = str_open_as_stream(mimeinfo->data.mem);
+               else
+                       fp = g_fopen(mimeinfo->data.filename, "rb");
+               if (!fp) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+                       END_TIMING();
+                       return;
+               }
                fseek(fp, mimeinfo->offset, SEEK_SET);
                headers = textview_scan_header(textview, fp);
                if (headers) {
                        if (charcount > 0)
                                gtk_text_buffer_insert(buffer, &iter, "\n", 1);
+                       
+                       if (procmime_mimeinfo_parent(mimeinfo) == NULL)
+                               textview_show_tags(textview);
                        textview_show_header(textview, headers);
                        procheader_header_array_destroy(headers);
                }
@@ -649,6 +679,8 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                if (mimeinfo->type == MIMETYPE_IMAGE  &&
                    prefs_common.inline_img ) {
                        GdkPixbuf *pixbuf;
+                       gint avail_width;
+                       gint avail_height;
                        GError *error = NULL;
                        gchar *filename;
                        ClickableText *uri;
@@ -670,14 +702,18 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        } else {
                                gint w, h;
                                gdk_pixbuf_get_file_info(filename, &w, &h);
-                               if (textview->scrolledwin->allocation.width - 100 > 0 &&
-                                   w > textview->scrolledwin->allocation.width - 100)
+                               avail_width = textview->scrolledwin->allocation.width;
+                               avail_height = textview->scrolledwin->allocation.height;
+                               if (avail_width - 100 > 0 &&
+                                   (w > avail_width || h > avail_height))
                                        pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, 
-                                               textview->scrolledwin->allocation.width - 100, 
-                                               -1, TRUE, &error);
+                                               avail_width, avail_height, TRUE, &error);
                                else
                                        pixbuf = gdk_pixbuf_new_from_file(filename, &error);
                        }
+                       if (textview->stop_loading) {
+                               return;
+                       }
                        if (error != NULL) {
                                g_warning("%s\n", error->message);
                                g_error_free(error);
@@ -696,7 +732,10 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                                uri->start = gtk_text_iter_get_offset(&iter);
                                
                                gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
-                               
+                               if (textview->stop_loading) {
+                                       g_free(uri);
+                                       return;
+                               }
                                uri->end = uri->start + 1;
                                uri->filename = procmime_get_part_file_name(mimeinfo);
                                textview->uri_list =
@@ -708,6 +747,9 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                                                &start_iter, &iter);
                        } else {
                                gtk_text_buffer_insert_pixbuf(buffer, &iter, pixbuf);
+                               if (textview->stop_loading) {
+                                       return;
+                               }
                                gtk_text_buffer_insert(buffer, &iter, " ", 1);
                        }
 
@@ -734,7 +776,9 @@ static void recursive_add_parts(TextView *textview, GNode *node)
         mimeinfo = (MimeInfo *) node->data;
         
         textview_add_part(textview, mimeinfo);
-        
+#ifdef GENERIC_UMPC
+       textview_set_position(textview, 0);
+#endif        
         if ((mimeinfo->type != MIMETYPE_MULTIPART) &&
             (mimeinfo->type != MIMETYPE_MESSAGE)) {
                END_TIMING();
@@ -788,7 +832,7 @@ static void recursive_add_parts(TextView *textview, GNode *node)
 
 static void textview_add_parts(TextView *textview, MimeInfo *mimeinfo)
 {
-       g_return_if_fail(mimeinfo != NULL);
+       cm_return_if_fail(mimeinfo != NULL);
         
         recursive_add_parts(textview, mimeinfo->node);
 }
@@ -814,7 +858,6 @@ void textview_show_error(TextView *textview)
        TEXTVIEW_INSERT_LINK(_("'View Log'"), "sc://view_log", NULL);
        TEXTVIEW_INSERT(_(" in the Tools menu for more information."));
        textview_show_icon(textview, GTK_STOCK_DIALOG_ERROR);
-
 }
 
 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
@@ -822,6 +865,8 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        GtkTextView *text;
        GtkTextBuffer *buffer;
        GtkTextIter iter;
+       const gchar *name;
+       gchar *content_type;
 
        if (!partinfo) return;
 
@@ -833,23 +878,59 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        gtk_text_buffer_get_start_iter(buffer, &iter);
 
        TEXTVIEW_INSERT("\n");
-       TEXTVIEW_INSERT(_("  The following can be performed on this part by\n"));
-       TEXTVIEW_INSERT(_("  right-clicking the icon or list item:\n"));
+
+       name = procmime_mimeinfo_get_parameter(partinfo, "filename");
+       if (name == NULL)
+               name = procmime_mimeinfo_get_parameter(partinfo, "name");
+       if (name != NULL) {
+               content_type = procmime_get_content_type_str(partinfo->type,
+                                                    partinfo->subtype);
+               TEXTVIEW_INSERT("  ");
+               TEXTVIEW_INSERT_BOLD(name);
+               TEXTVIEW_INSERT(" (");
+               TEXTVIEW_INSERT(content_type);
+               TEXTVIEW_INSERT(", ");
+               TEXTVIEW_INSERT(to_human_readable((goffset)partinfo->length));
+               TEXTVIEW_INSERT("):\n\n");
+               
+               g_free(content_type);
+       }
+       TEXTVIEW_INSERT(_("  The following can be performed on this part\n"));
+#ifndef GENERIC_UMPC
+       TEXTVIEW_INSERT(_("  by right-clicking the icon or list item:"));
+#endif
+       TEXTVIEW_INSERT("\n");
 
        TEXTVIEW_INSERT(_("     - To save, select "));
        TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
-       TEXTVIEW_INSERT(_(" (Shortcut key: 'y')\n"));
+#ifndef GENERIC_UMPC
+       TEXTVIEW_INSERT(_(" (Shortcut key: 'y')"));
+#endif
+       TEXTVIEW_INSERT("\n");
+
        TEXTVIEW_INSERT(_("     - To display as text, select "));
        TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
-       TEXTVIEW_INSERT(_(" (Shortcut key: 't')\n"));
+
+#ifndef GENERIC_UMPC
+       TEXTVIEW_INSERT(_(" (Shortcut key: 't')"));
+#endif
+       TEXTVIEW_INSERT("\n");
+
        TEXTVIEW_INSERT(_("     - To open with an external program, select "));
        TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
+
+#ifndef GENERIC_UMPC
        TEXTVIEW_INSERT(_(" (Shortcut key: 'l')\n"));
        TEXTVIEW_INSERT(_("       (alternately double-click, or click the middle "));
        TEXTVIEW_INSERT(_("mouse button)\n"));
+#ifndef G_OS_WIN32
        TEXTVIEW_INSERT(_("     - Or use "));
        TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
-       TEXTVIEW_INSERT(_(" (Shortcut key: 'o')\n"));
+       TEXTVIEW_INSERT(_(" (Shortcut key: 'o')"));
+#endif
+#endif
+       TEXTVIEW_INSERT("\n");
+
        textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
 }
 
@@ -860,12 +941,23 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
        CodeConverter *conv;
        const gchar *charset, *p, *cmd;
        GSList *cur;
-       int lines = 0;
-       
+       gboolean continue_write = TRUE;
+       size_t wrote = 0, i = 0;
+
        if (textview->messageview->forced_charset)
                charset = textview->messageview->forced_charset;
-       else
+       else {
+               /* use supersets transparently when possible */
                charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
+               if (charset && !strcasecmp(charset, CS_ISO_8859_1))
+                       charset = CS_WINDOWS_1252;
+               else if (charset && !strcasecmp(charset, CS_X_GBK))
+                       charset = CS_GB18030;
+               else if (charset && !strcasecmp(charset, CS_GBK))
+                       charset = CS_GB18030;
+               else if (charset && !strcasecmp(charset, CS_GB2312))
+                       charset = CS_GB18030;
+       }
 
        textview_set_font(textview, charset);
 
@@ -886,7 +978,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                        tmpfp = g_fopen(filename, "rb");
                        textview_show_html(textview, tmpfp, conv);
                        fclose(tmpfp);
-                       g_unlink(filename);
+                       claws_unlink(filename);
                }
                g_free(filename);
        } else if (!g_ascii_strcasecmp(mimeinfo->subtype, "enriched")) {
@@ -897,7 +989,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                        tmpfp = g_fopen(filename, "rb");
                        textview_show_ertf(textview, tmpfp, conv);
                        fclose(tmpfp);
-                       g_unlink(filename);
+                       claws_unlink(filename);
                }
                g_free(filename);
 #ifndef G_OS_WIN32
@@ -935,10 +1027,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                        argv = strsplit_with_quote(buf, " ", 0);
                        close(1);
                        close(pfd[0]);
-                       dup(pfd[1]);
+                       rc = dup(pfd[1]);
                        rc = execvp(argv[0], argv);
                        close(pfd[1]);
-                       printf (_("The command to view attachment "
+                       g_print(_("The command to view attachment "
                                "as text failed:\n"
                                "    %s\n"
                                "Exit code %d\n"), buf, rc);
@@ -949,37 +1041,44 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                while (fgets(buf, sizeof(buf), tmpfp)) {
                        textview_write_line(textview, buf, conv, TRUE);
                        
-                       lines++;
-                       if (lines % 500 == 0)
-                               GTK_EVENTS_FLUSH();
                        if (textview->stop_loading) {
                                fclose(tmpfp);
                                waitpid(pid, pfd, 0);
-                               unlink(fname);
+                               g_unlink(fname);
                                return;
                        }
                }
 
                fclose(tmpfp);
                waitpid(pid, pfd, 0);
-               unlink(fname);
+               g_unlink(fname);
 #endif
        } else {
 textview_default:
-               lines = 0;
-               tmpfp = g_fopen(mimeinfo->data.filename, "rb");
+               if (mimeinfo->content == MIMECONTENT_MEM)
+                       tmpfp = str_open_as_stream(mimeinfo->data.mem);
+               else
+                       tmpfp = g_fopen(mimeinfo->data.filename, "rb");
+               if (!tmpfp) {
+                       FILE_OP_ERROR(mimeinfo->data.filename, "fopen");
+                       return;
+               }
                fseek(tmpfp, mimeinfo->offset, SEEK_SET);
                debug_print("Viewing text content of type: %s (length: %d)\n", mimeinfo->subtype, mimeinfo->length);
-               while ((ftell(tmpfp) < mimeinfo->offset + mimeinfo->length) &&
-                      (fgets(buf, sizeof(buf), tmpfp) != NULL)) {
+               while (((i = ftell(tmpfp)) < mimeinfo->offset + mimeinfo->length) &&
+                      (fgets(buf, sizeof(buf), tmpfp) != NULL)
+                      && continue_write) {
                        textview_write_line(textview, buf, conv, TRUE);
-                       lines++;
-                       if (lines % 500 == 0)
-                               GTK_EVENTS_FLUSH();
                        if (textview->stop_loading) {
                                fclose(tmpfp);
                                return;
                        }
+                       wrote += ftell(tmpfp)-i;
+                       if (mimeinfo->length > 1024*1024 
+                       &&  wrote > 1024*1024
+                       && !textview->messageview->show_full_text) {
+                               continue_write = FALSE;
+                       }
                }
                fclose(tmpfp);
        }
@@ -987,7 +1086,6 @@ textview_default:
        conv_code_converter_destroy(conv);
        procmime_force_encoding(0);
 
-       lines = 0;
        textview->uri_list = g_slist_reverse(textview->uri_list);
        for (cur = textview->uri_list; cur; cur = cur->next) {
                ClickableText *uri = (ClickableText *)cur->data;
@@ -996,14 +1094,19 @@ textview_default:
                if (!prefs_common.hide_quotes ||
                    uri->quote_level+1 < prefs_common.hide_quotes) {
                        textview_toggle_quote(textview, cur, uri, TRUE);
-                       lines++;
-                       if (lines % 500 == 0)
-                               GTK_EVENTS_FLUSH();
                        if (textview->stop_loading) {
                                return;
                        }
                }
        }
+       
+       if (continue_write == FALSE) {
+               messageview_show_partial_display(
+                       textview->messageview, 
+                       textview->messageview->msginfo,
+                       mimeinfo->length);
+       }
+       GTK_EVENTS_FLUSH();
 }
 
 static void textview_show_html(TextView *textview, FILE *fp,
@@ -1014,7 +1117,7 @@ static void textview_show_html(TextView *textview, FILE *fp,
        gint lines = 0;
 
        parser = sc_html_parser_new(fp, conv);
-       g_return_if_fail(parser != NULL);
+       cm_return_if_fail(parser != NULL);
 
        while ((str = sc_html_parse(parser)) != NULL) {
                if (parser->state == SC_HTML_HREF) {
@@ -1055,7 +1158,7 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
        gint lines = 0;
 
        parser = ertf_parser_new(fp, conv);
-       g_return_if_fail(parser != NULL);
+       cm_return_if_fail(parser != NULL);
 
        while ((str = ertf_parse(parser)) != NULL) {
                textview_write_line(textview, str, NULL, FALSE);
@@ -1127,6 +1230,7 @@ static void textview_make_clickable_parts(TextView *textview,
                {"https://", strcasestr, get_uri_part,   make_uri_string},
                {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
                {"sftp://",  strcasestr, get_uri_part,   make_uri_string},
+               {"gopher://",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}
@@ -1436,18 +1540,20 @@ do_quote:
                        if (textview->uri_list) {
                                lasturi = (ClickableText *)last->data;
                        } else {
-                               printf("oops (%d %d)\n",
+                               g_print("oops (%d %d)\n",
                                        real_quotelevel, textview->prev_quote_level);
-                       }               
-                       if (lasturi->is_quote == FALSE) {
-                               textview->prev_quote_level = -1;
-                               goto do_quote;
+                       }       
+                       if (lasturi) {  
+                               if (lasturi->is_quote == FALSE) {
+                                       textview->prev_quote_level = -1;
+                                       goto do_quote;
+                               }
+                               e_len = lasturi->data ? strlen(lasturi->data):0;
+                               n_len = strlen(buf);
+                               lasturi->data = g_realloc((gchar *)lasturi->data, e_len + n_len + 1);
+                               strcpy((gchar *)lasturi->data + e_len, buf);
+                               *((gchar *)lasturi->data + e_len + n_len) = '\0';
                        }
-                       e_len = lasturi->data ? strlen(lasturi->data):0;
-                       n_len = strlen(buf);
-                       lasturi->data = g_realloc((gchar *)lasturi->data, e_len + n_len + 1);
-                       strcpy((gchar *)lasturi->data + e_len, buf);
-                       *((gchar *)lasturi->data + e_len + n_len) = '\0';
                }
        } else {
                textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
@@ -1577,25 +1683,29 @@ 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);
+       PangoFontDescription *font_desc, *bold_font_desc;
+
+       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);
+               CHANGE_TAG_FONT("hlink", font_desc);
+               pango_font_description_free(font_desc);
+       }
+       if (prefs_common.derive_from_normal_font || !BOLD_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);
-                       CHANGE_TAG_FONT("hlink", font_desc);
-                       pango_font_description_free(font_desc);
-               }
-               if (bold_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);
-               }
+       } else {
+               bold_font_desc = pango_font_description_from_string
+                                               (BOLD_FONT);
+       }
+       if (bold_font_desc) {
+               CHANGE_TAG_FONT("header_title", bold_font_desc);
+               pango_font_description_free(bold_font_desc);
        }
 
        if (prefs_common.textfont) {
@@ -1619,8 +1729,8 @@ void textview_set_text(TextView *textview, const gchar *text)
        GtkTextView *view;
        GtkTextBuffer *buffer;
 
-       g_return_if_fail(textview != NULL);
-       g_return_if_fail(text != NULL);
+       cm_return_if_fail(textview != NULL);
+       cm_return_if_fail(text != NULL);
 
        textview_clear(textview);
 
@@ -1661,6 +1771,7 @@ static gboolean header_is_internal(Header *header)
                 "X-Claws-Account-Id:", "X-Claws-Sign:", "X-Claws-Encrypt:", 
                 "X-Claws-Privacy-System:", "X-Claws-End-Special-Headers:",
                 "X-Sylpheed-Account-Id:", "X-Sylpheed-Sign:", "X-Sylpheed-Encrypt:", 
+                "X-Claws-Auto-Wrapping:", "X-Claws-Auto-Indent:",
                 "X-Sylpheed-Privacy-System:", "X-Sylpheed-End-Special-Headers:",
                 NULL};
        int i;
@@ -1680,7 +1791,7 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        Header *header;
        gint i;
 
-       g_return_val_if_fail(fp != NULL, NULL);
+       cm_return_val_if_fail(fp != NULL, NULL);
 
        if (textview->show_all_headers) {
                headers = procheader_get_header_array_asis(fp);
@@ -1762,7 +1873,7 @@ static void textview_show_face(TextView *textview)
                gtk_widget_destroy(textview->image);
        
        textview->image = face_get_from_header(msginfo->extradata->face);
-       g_return_if_fail(textview->image != NULL);
+       cm_return_if_fail(textview->image != NULL);
 
        gtk_widget_show(textview->image);
        
@@ -1790,7 +1901,7 @@ void textview_show_icon(TextView *textview, const gchar *stock_id)
                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);
+       cm_return_if_fail(textview->image != NULL);
 
        gtk_widget_show(textview->image);
        
@@ -1836,7 +1947,7 @@ static void textview_show_xface(TextView *textview)
        textview->image = xface_get_from_header(msginfo->extradata->xface,
                                &textview->text->style->white,
                                window);
-       g_return_if_fail(textview->image != NULL);
+       cm_return_if_fail(textview->image != NULL);
 
        gtk_widget_show(textview->image);
        
@@ -1856,6 +1967,177 @@ bail:
 }
 #endif
 
+static void textview_save_contact_pic(TextView *textview)
+{
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       gchar *filename = NULL;
+       GError *error = NULL;
+       GdkPixbuf *picture = NULL;
+                               
+       if (!msginfo->extradata || (!msginfo->extradata->face && !msginfo->extradata->xface))
+               return;
+
+       if (textview->image) 
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(textview->image));
+
+       filename = addrindex_get_picture_file(msginfo->from);
+       if (!filename)
+               return;
+       if (!is_file_exist(filename)) {
+               gdk_pixbuf_save(picture, filename, "png", &error, NULL);
+               if (error) {
+                       g_warning(_("Failed to save image: \n%s"),
+                                       error->message);
+                       g_error_free(error);
+               }
+       }
+       g_free(filename);
+}
+
+static void textview_show_contact_pic(TextView *textview)
+{
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       int x = 0;
+       gchar *filename = NULL;
+       GError *error = NULL;
+       GdkPixbuf *picture = NULL;
+       gint w, h;
+                               
+       if (prefs_common.display_header_pane
+       ||  !prefs_common.display_xface)
+               goto bail;
+       
+       if (msginfo->extradata && (msginfo->extradata->face || msginfo->extradata->xface))
+               return;
+
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+
+       filename = addrindex_get_picture_file(msginfo->from);
+       
+       if (!filename)
+               goto bail;
+       if (!is_file_exist(filename)) {
+               g_free(filename);
+               goto bail;
+       }
+
+       gdk_pixbuf_get_file_info(filename, &w, &h);
+       
+       if (w > 48 || h > 48)
+               picture = gdk_pixbuf_new_from_file_at_scale(filename, 
+                                               48, 48, TRUE, &error);
+       else
+               picture = gdk_pixbuf_new_from_file(filename, &error);
+
+       if (error) {
+               debug_print("Failed to import image: \n%s",
+                               error->message);
+               g_error_free(error);
+               goto bail;
+       }
+       g_free(filename);
+
+       if (picture) {
+               textview->image = gtk_image_new_from_pixbuf(picture);
+               g_object_unref(picture);
+       }
+       cm_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 gint textview_tag_cmp_list(gconstpointer a, gconstpointer b)
+{
+       gint id_a = GPOINTER_TO_INT(a);
+       gint id_b = GPOINTER_TO_INT(b);
+       const gchar *tag_a = tags_get_tag(id_a);
+       const gchar *tag_b = tags_get_tag(id_b);
+       
+       if (tag_a == NULL)
+               return tag_b == NULL ? 0:1;
+       
+       if (tag_b == NULL)
+               return tag_a == NULL ? 0:1;
+
+       return g_utf8_collate(tag_a, tag_b);
+}
+
+
+static void textview_show_tags(TextView *textview)
+{
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+       GtkTextIter iter;
+       ClickableText *uri;
+       GSList *cur, *orig;
+       gboolean found_tag = FALSE;
+       
+       if (!msginfo->tags)
+               return;
+       
+       cur = orig = g_slist_sort(g_slist_copy(msginfo->tags), textview_tag_cmp_list);
+
+       for (; cur; cur = cur->next) {
+               if (tags_get_tag(GPOINTER_TO_INT(cur->data)) != NULL) {
+                       found_tag = TRUE;
+                       break;
+               }
+       }
+       if (!found_tag) {
+               g_slist_free(orig);
+               return;
+       }
+
+       gtk_text_buffer_get_end_iter (buffer, &iter);
+       gtk_text_buffer_insert_with_tags_by_name(buffer,
+               &iter, _("Tags: "), -1,
+               "header_title", "header", "tags", NULL);
+
+       for (cur = orig; cur; cur = cur->next) {
+               const gchar *cur_tag = tags_get_tag(GPOINTER_TO_INT(cur->data));
+               if (!cur_tag)
+                       continue;
+               uri = g_new0(ClickableText, 1);
+               uri->uri = g_strdup("");
+               uri->start = gtk_text_iter_get_offset(&iter);
+               gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, 
+                       cur_tag, -1,
+                       "link", "header", "tags", NULL);
+               uri->end = gtk_text_iter_get_offset(&iter);
+               uri->filename = g_strdup_printf("sc://search_tags:%s", cur_tag);
+               uri->data = NULL;
+               textview->uri_list =
+                       g_slist_prepend(textview->uri_list, uri);
+               if (cur->next && tags_get_tag(GPOINTER_TO_INT(cur->next->data)))
+                       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, ", ", 2,
+                               "header", "tags", NULL);
+               else
+                       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, " ", 1,
+                               "header", "tags", NULL);
+       }
+       g_slist_free(orig);
+
+       gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
+               "header", "tags", NULL);
+}
+
 static void textview_show_header(TextView *textview, GPtrArray *headers)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
@@ -1864,11 +2146,11 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
        Header *header;
        gint i;
 
-       g_return_if_fail(headers != NULL);
+       cm_return_if_fail(headers != NULL);
 
        for (i = 0; i < headers->len; i++) {
                header = g_ptr_array_index(headers, i);
-               g_return_if_fail(header->name != NULL);
+               cm_return_if_fail(header->name != NULL);
 
                gtk_text_buffer_get_end_iter (buffer, &iter);
                if(prefs_common.trans_hdr == TRUE) {
@@ -1926,6 +2208,8 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
 #if HAVE_LIBCOMPFACE
        textview_show_xface(textview);
 #endif
+       textview_save_contact_pic(textview);
+       textview_show_contact_pic(textview);
 }
 
 gboolean textview_search_string(TextView *textview, const gchar *str,
@@ -2012,6 +2296,7 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_y:
        case GDK_t:
        case GDK_l:
+       case GDK_o:
        case GDK_c:
        case GDK_a:
                if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
@@ -2168,17 +2453,12 @@ static void textview_uri_update(TextView *textview, gint x, gint y)
                TEXTVIEW_STATUSBAR_POP(textview);
 
                if (uri) {
-                       char *trimmed_uri;
-
                        if (!uri->is_quote)
                                gtk_text_buffer_apply_tag_by_name(buffer,
                                                          "link-hover",
                                                          &start_iter,
                                                          &end_iter);
-
-                       trimmed_uri = trim_string(uri->uri, 60);
-                       TEXTVIEW_STATUSBAR_PUSH(textview, trimmed_uri);
-                       g_free(trimmed_uri);
+                       TEXTVIEW_STATUSBAR_PUSH(textview, uri->uri);
                }
        }
 }
@@ -2399,7 +2679,8 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                        } 
                        return TRUE;
                } else if (qlink && bevent->button == 1) {
-                       textview_toggle_quote(textview, NULL, uri, FALSE);
+                       if (prefs_common.hide_quoted)
+                               textview_toggle_quote(textview, NULL, uri, FALSE);
                        return TRUE;
                } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
                        if (bevent->button == 3) {
@@ -2412,6 +2693,8 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                        } else {
                                PrefsAccount *account = NULL;
                                FolderItem   *folder_item = NULL;
+                               Compose *compose;
+                               
                                if (textview->messageview && textview->messageview->msginfo &&
                                    textview->messageview->msginfo->folder) {
                                        
@@ -2422,14 +2705,16 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                                        if (!account)
                                                account = account_find_from_item(folder_item);
                                }
-                               compose_new_with_folderitem(account, folder_item, uri->uri + 7);
+                               compose = compose_new_with_folderitem(account,
+                                                               folder_item, uri->uri + 7);
+                               compose_check_for_email_account(compose);
                        }
                        return TRUE;
                } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
                        if (bevent->button == 1 &&
                            textview_uri_security_check(textview, uri) == TRUE) 
                                        open_uri(uri->uri,
-                                                prefs_common.uri_cmd);
+                                                prefs_common_get_uri_cmd());
                        else if (bevent->button == 3 && !qlink) {
                                g_object_set_data(
                                        G_OBJECT(textview->link_popup_menu),
@@ -2492,6 +2777,8 @@ gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
        if (visible_str == NULL)
                return TRUE;
 
+       g_strstrip(visible_str);
+
        if (strcmp(visible_str, uri->uri) != 0 && is_uri_string(visible_str)) {
                gchar *uri_path;
                gchar *visible_uri_path;
@@ -2548,21 +2835,28 @@ static void textview_uri_list_remove_all(GSList *uri_list)
        g_slist_free(uri_list);
 }
 
-static void open_uri_cb (TextView *textview, guint action, void *data)
+static void open_uri_cb (GtkAction *action, TextView *textview)
 {
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
                                           "menu_button");
-       if (uri == NULL)
-               return;
-
-       if (textview_uri_security_check(textview, uri) == TRUE) 
-               open_uri(uri->uri,
-                        prefs_common.uri_cmd);
-       g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
-                         NULL);
+       const gchar *raw_url = g_object_get_data(G_OBJECT(textview->link_popup_menu),
+                                          "raw_url");
+
+       if (uri) {
+               if (textview_uri_security_check(textview, uri) == TRUE) 
+                       open_uri(uri->uri,
+                                prefs_common_get_uri_cmd());
+               g_object_set_data(G_OBJECT(textview->link_popup_menu), "menu_button",
+                                 NULL);
+       }
+       if (raw_url) {
+               open_uri(raw_url, prefs_common_get_uri_cmd());
+               g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url",
+                                 NULL);
+       }
 }
 
-static void open_image_cb (TextView *textview, guint action, void *data)
+static void open_image_cb (GtkAction *action, TextView *textview)
 {
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
                                           "menu_button");
@@ -2598,11 +2892,11 @@ static void open_image_cb (TextView *textview, guint action, void *data)
                gboolean remember = FALSE;
                cmd = input_dialog_combo_remember
                        (_("Open with"),
-                        _("Enter the command line to open file:\n"
+                        _("Enter the command-line to open file:\n"
                           "('%s' will be replaced with file name)"),
                         prefs_common.mime_open_cmd,
                         prefs_common.mime_open_cmd_history,
-                        TRUE, &remember);
+                        &remember);
                if (cmd && remember) {
                        mailcap_update_default("image/jpeg", cmd);
                }
@@ -2611,7 +2905,7 @@ static void open_image_cb (TextView *textview, guint action, void *data)
            !strchr(p + 2, '%'))
                g_snprintf(buf, sizeof(buf), cmd, filename);
        else {
-               g_warning("Image viewer command line is invalid: '%s'", cmd);
+               g_warning("Image viewer command-line is invalid: '%s'", cmd);
                return;
        }
 
@@ -2624,7 +2918,7 @@ static void open_image_cb (TextView *textview, guint action, void *data)
                          NULL);
 }
 
-static void save_file_cb (TextView *textview, guint action, void *data)
+static void save_file_cb (GtkAction *action, TextView *textview)
 {
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->file_popup_menu),
                                           "menu_button");
@@ -2648,7 +2942,7 @@ static void save_file_cb (TextView *textview, guint action, void *data)
 
        subst_for_filename(filename);
        
-       if (prefs_common.attach_save_dir)
+       if (prefs_common.attach_save_dir && *prefs_common.attach_save_dir)
                filepath = g_strconcat(prefs_common.attach_save_dir,
                                       G_DIR_SEPARATOR_S, filename, NULL);
        else
@@ -2692,43 +2986,87 @@ static void save_file_cb (TextView *textview, guint action, void *data)
                          NULL);
 }
 
-static void copy_uri_cb        (TextView *textview, guint action, void *data)
+static void copy_uri_cb        (GtkAction *action, TextView *textview)
 {
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->link_popup_menu),
                                           "menu_button");
-       if (uri == NULL)
-               return;
-
-       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",
+       const gchar *raw_url =  g_object_get_data(G_OBJECT(textview->link_popup_menu),
+                                          "raw_url");
+       if (uri) {
+               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);
+       }
+       if (raw_url) {
+               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_PRIMARY), raw_url, -1);
+               gtk_clipboard_set_text(gtk_clipboard_get(GDK_SELECTION_CLIPBOARD), raw_url, -1);
+               g_object_set_data(G_OBJECT(textview->link_popup_menu), "raw_url",
                          NULL);
+       }
 }
 
-static void add_uri_to_addrbook_cb (TextView *textview, guint action, void *data)
+static void add_uri_to_addrbook_cb (GtkAction *action, TextView *textview)
 {
        gchar *fromname, *fromaddress;
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
                                           "menu_button");
+       GtkWidget *image = NULL;
+       GdkPixbuf *picture = NULL;
+       gboolean use_picture = FALSE;
        if (uri == NULL)
                return;
 
        /* extract url */
        fromaddress = g_strdup(uri->uri + 7);
-       /* Hiroyuki: please put this function in utils.c! */
+       
+       if (textview->messageview->msginfo &&
+          !strcmp2(fromaddress, textview->messageview->msginfo->from))
+               use_picture = TRUE;
+
        fromname = procheader_get_fromname(fromaddress);
        extract_address(fromaddress);
 
-       /* Add to address book - Match */
-       addressbook_add_contact( fromname, fromaddress, NULL );
+       if (use_picture && 
+           textview->messageview->msginfo &&
+           textview->messageview->msginfo->extradata &&
+           textview->messageview->msginfo->extradata->face) {
+               image = face_get_from_header(textview->messageview->msginfo->extradata->face);
+       }
+#if HAVE_LIBCOMPFACE 
+       else if (use_picture && 
+                textview->messageview->msginfo &&
+                textview->messageview->msginfo->extradata &&
+                textview->messageview->msginfo->extradata->xface) {
+               image = xface_get_from_header(textview->messageview->msginfo->extradata->xface,
+                               &textview->text->style->white,
+                               mainwindow_get_mainwindow()->window->window);   
+       }
+#endif
+       if (image)
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
+
+       addressbook_add_contact( fromname, fromaddress, NULL, picture);
 
        g_free(fromaddress);
        g_free(fromname);
 }
 
-static void mail_to_uri_cb (TextView *textview, guint action, void *data)
+static void reply_to_uri_cb (GtkAction *action, TextView *textview)
+{
+       ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
+                                          "menu_button");
+       if (!textview->messageview || !uri)
+               return;
+
+       compose_reply_to_address (textview->messageview,
+                                 textview->messageview->msginfo, uri->uri+7);
+}
+
+static void mail_to_uri_cb (GtkAction *action, TextView *textview)
 {
        PrefsAccount *account = NULL;
+       Compose *compose;
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
                                           "menu_button");
        if (uri == NULL)
@@ -2741,11 +3079,15 @@ static void mail_to_uri_cb (TextView *textview, guint action, void *data)
                folder_item = textview->messageview->msginfo->folder;
                if (folder_item->prefs && folder_item->prefs->enable_default_account)
                        account = account_find_from_id(folder_item->prefs->default_account);
+               
+               compose = compose_new_with_folderitem(account, folder_item, uri->uri+7);
+       } else {
+               compose = compose_new(account, uri->uri + 7, NULL);
        }
-       compose_new(account, uri->uri + 7, NULL);
+       compose_check_for_email_account(compose);
 }
 
-static void copy_mail_to_uri_cb        (TextView *textview, guint action, void *data)
+static void copy_mail_to_uri_cb        (GtkAction *action, TextView *textview)
 {
        ClickableText *uri = g_object_get_data(G_OBJECT(textview->mail_popup_menu),
                                           "menu_button");