2007-04-24 [wwp] 2.9.1cvs19
[claws.git] / src / textview.c
index 63842336a7a1638c2a369be4022de25498686bc7..79772e2653e0681b1e54b1a93b7e951edbcccf84 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws team
+ * Copyright (C) 1999-2007 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
 #include "inputdialog.h"
 #include "timing.h"
 
-struct _ClickableText
-{
-       gchar *uri;
-
-       gchar *filename;
-
-       gpointer data;
-
-       guint start;
-       guint end;
-       
-       gboolean is_quote;
-       gint quote_level;
-       gboolean q_expanded;
-       gchar *fg_color;
-};
-
-gint previousquotelevel = -1;
-
 static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
@@ -151,7 +132,8 @@ static void textview_show_html              (TextView       *textview,
 
 static void textview_write_line                (TextView       *textview,
                                         const gchar    *str,
-                                        CodeConverter  *conv);
+                                        CodeConverter  *conv,
+                                        gboolean        do_quote_folding);
 static void textview_write_link                (TextView       *textview,
                                         const gchar    *str,
                                         const gchar    *uri,
@@ -196,11 +178,10 @@ static gboolean textview_uri_button_pressed       (GtkTextTag     *tag,
                                                 GtkTextIter    *iter,
                                                 TextView       *textview);
 
-static gboolean textview_uri_security_check    (TextView       *textview,
-                                                ClickableText  *uri);
 static void textview_uri_list_remove_all       (GSList         *uri_list);
 
 static void textview_toggle_quote              (TextView       *textview, 
+                                                GSList         *start_list,
                                                 ClickableText  *uri,
                                                 gboolean        expand_only);
 
@@ -225,7 +206,6 @@ static void save_file_cb                    (TextView       *textview,
 static void open_image_cb                      (TextView       *textview,
                                                 guint           action,
                                                 void           *data);
-static void textview_show_icon(TextView *textview, const gchar *stock_id);
 
 static GtkItemFactoryEntry textview_link_popup_entries[] = 
 {
@@ -566,7 +546,7 @@ void textview_show_message(TextView *textview, MimeInfo *mimeinfo,
 
 void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
 {
-       START_TIMING("textview_show_part");
+       START_TIMING("");
        g_return_if_fail(mimeinfo != NULL);
        g_return_if_fail(fp != NULL);
 
@@ -601,27 +581,6 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        END_TIMING();
 }
 
-#define TEXT_INSERT(str) \
-       gtk_text_buffer_insert_with_tags_by_name \
-                               (buffer, &iter, str, -1,\
-                                "header", NULL)
-
-#define TEXT_INSERT_LINK(str, fname, udata) {                          \
-       ClickableText *uri;                                                     \
-       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, str, -1,                \
-                                "link", "header_title", "header",      \
-                                NULL);                                 \
-       uri->end = gtk_text_iter_get_offset(&iter);                     \
-       uri->filename = fname?g_strdup(fname):NULL;                     \
-       uri->data = udata;                                              \
-       textview->uri_list =                                            \
-               g_slist_append(textview->uri_list, uri);                \
-}
-
 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 {
        GtkTextView *text;
@@ -632,14 +591,14 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
        const gchar *name;
        gchar *content_type;
        gint charcount;
-       START_TIMING("textview_add_part");
+       START_TIMING("");
 
        g_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);
        gtk_text_buffer_get_end_iter(buffer, &iter);
-
+       
        if (textview->stop_loading) {
                return;
        }
@@ -648,6 +607,8 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                return;
        }
 
+       textview->prev_quote_level = -1;
+
        if ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822")) {
                FILE *fp;
 
@@ -683,7 +644,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
        || (mimeinfo->disposition == DISPOSITIONTYPE_INLINE && 
            mimeinfo->type != MIMETYPE_TEXT)) {
                gtk_text_buffer_insert(buffer, &iter, "\n", 1);
-               TEXT_INSERT_LINK(buf, "sc://select_attachment", mimeinfo);
+               TEXTVIEW_INSERT_LINK(buf, "sc://select_attachment", mimeinfo);
                gtk_text_buffer_insert(buffer, &iter, " \n", -1);
                if (mimeinfo->type == MIMETYPE_IMAGE  &&
                    prefs_common.inline_img ) {
@@ -692,12 +653,13 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        gchar *filename;
                        ClickableText *uri;
                        gchar *uri_str;
+                       gint err;
                        START_TIMING("inserting image");
 
                        filename = procmime_get_tmp_file_name(mimeinfo);
 
-                       if (procmime_get_part(filename, mimeinfo) < 0) {
-                               g_warning("Can't get the image file.");
+                       if ((err = procmime_get_part(filename, mimeinfo)) < 0) {
+                               g_warning("Can't get the image file.(%s)", strerror(-err));
                                g_free(filename);
                                END_TIMING();
                                return;
@@ -708,7 +670,8 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        } else {
                                gint w, h;
                                gdk_pixbuf_get_file_info(filename, &w, &h);
-                               if (w > textview->scrolledwin->allocation.width - 100)
+                               if (textview->scrolledwin->allocation.width - 100 > 0 &&
+                                   w > textview->scrolledwin->allocation.width - 100)
                                        pixbuf = gdk_pixbuf_new_from_file_at_scale(filename, 
                                                textview->scrolledwin->allocation.width - 100, 
                                                -1, TRUE, &error);
@@ -737,7 +700,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                                uri->end = uri->start + 1;
                                uri->filename = procmime_get_part_file_name(mimeinfo);
                                textview->uri_list =
-                                       g_slist_append(textview->uri_list, uri);
+                                       g_slist_prepend(textview->uri_list, uri);
                                
                                gtk_text_buffer_insert(buffer, &iter, " ", 1);
                                gtk_text_buffer_get_iter_at_offset(buffer, &start_iter, uri->start);    
@@ -766,7 +729,7 @@ static void recursive_add_parts(TextView *textview, GNode *node)
 {
         GNode * iter;
        MimeInfo *mimeinfo;
-        START_TIMING("recursive_add_parts");
+        START_TIMING("");
 
         mimeinfo = (MimeInfo *) node->data;
         
@@ -843,13 +806,13 @@ void textview_show_error(TextView *textview)
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_start_iter(buffer, &iter);
 
-       TEXT_INSERT(_("\n"
+       TEXTVIEW_INSERT(_("\n"
                      "  This message can't be displayed.\n"
                      "  This is probably due to a network error.\n"
                      "\n"
                      "  Use "));
-       TEXT_INSERT_LINK(_("'View Log'"), "sc://view_log", NULL);
-       TEXT_INSERT(_(" in the Tools menu for more information."));
+       TEXTVIEW_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);
 
 }
@@ -869,30 +832,27 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        buffer = gtk_text_view_get_buffer(text);
        gtk_text_buffer_get_start_iter(buffer, &iter);
 
-       TEXT_INSERT("\n");
-       TEXT_INSERT(_("  The following can be performed on this part by\n"));
-       TEXT_INSERT(_("  right-clicking the icon or list item:\n"));
-
-       TEXT_INSERT(_("     - To save, select "));
-       TEXT_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
-       TEXT_INSERT(_(" (Shortcut key: 'y')\n"));
-       TEXT_INSERT(_("     - To display as text, select "));
-       TEXT_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
-       TEXT_INSERT(_(" (Shortcut key: 't')\n"));
-       TEXT_INSERT(_("     - To open with an external program, select "));
-       TEXT_INSERT_LINK(_("'Open'"), "sc://open", NULL);
-       TEXT_INSERT(_(" (Shortcut key: 'l')\n"));
-       TEXT_INSERT(_("       (alternately double-click, or click the middle "));
-       TEXT_INSERT(_("mouse button)\n"));
-       TEXT_INSERT(_("     - Or use "));
-       TEXT_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
-       TEXT_INSERT(_(" (Shortcut key: 'o')\n"));
+       TEXTVIEW_INSERT("\n");
+       TEXTVIEW_INSERT(_("  The following can be performed on this part by\n"));
+       TEXTVIEW_INSERT(_("  right-clicking the icon or list item:\n"));
+
+       TEXTVIEW_INSERT(_("     - To save, select "));
+       TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
+       TEXTVIEW_INSERT(_(" (Shortcut key: 'y')\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"));
+       TEXTVIEW_INSERT(_("     - To open with an external program, select "));
+       TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
+       TEXTVIEW_INSERT(_(" (Shortcut key: 'l')\n"));
+       TEXTVIEW_INSERT(_("       (alternately double-click, or click the middle "));
+       TEXTVIEW_INSERT(_("mouse button)\n"));
+       TEXTVIEW_INSERT(_("     - Or use "));
+       TEXTVIEW_INSERT_LINK(_("'Open with...'"), "sc://open_with", NULL);
+       TEXTVIEW_INSERT(_(" (Shortcut key: 'o')\n"));
        textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
 }
 
-#undef TEXT_INSERT
-#undef TEXT_INSERT_LINK
-
 static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 {
        FILE *tmpfp;
@@ -957,14 +917,14 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                if (pipe(pfd) < 0) {
                        g_snprintf(buf, sizeof(buf),
                                "pipe failed for textview\n\n%s\n", strerror(errno));
-                       textview_write_line(textview, buf, conv);
+                       textview_write_line(textview, buf, conv, TRUE);
                        goto textview_default;
                }
                pid = fork();
                if (pid < 0) {
                        g_snprintf(buf, sizeof(buf),
                                "fork failed for textview\n\n%s\n", strerror(errno));
-                       textview_write_line(textview, buf, conv);
+                       textview_write_line(textview, buf, conv, TRUE);
                        close(pfd[0]);
                        close(pfd[1]);
                        goto textview_default;
@@ -987,7 +947,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                close(pfd[1]);
                tmpfp = fdopen(pfd[0], "rb");
                while (fgets(buf, sizeof(buf), tmpfp)) {
-                       textview_write_line(textview, buf, conv);
+                       textview_write_line(textview, buf, conv, TRUE);
                        
                        lines++;
                        if (lines % 500 == 0)
@@ -1012,7 +972,7 @@ textview_default:
                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)) {
-                       textview_write_line(textview, buf, conv);
+                       textview_write_line(textview, buf, conv, TRUE);
                        lines++;
                        if (lines % 500 == 0)
                                GTK_EVENTS_FLUSH();
@@ -1028,13 +988,14 @@ textview_default:
        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;
                if (!uri->is_quote)
                        continue;
                if (!prefs_common.hide_quotes ||
                    uri->quote_level+1 < prefs_common.hide_quotes) {
-                       textview_toggle_quote(textview, uri, TRUE);
+                       textview_toggle_quote(textview, cur, uri, TRUE);
                        lines++;
                        if (lines % 500 == 0)
                                GTK_EVENTS_FLUSH();
@@ -1059,7 +1020,7 @@ static void textview_show_html(TextView *textview, FILE *fp,
                if (parser->state == SC_HTML_HREF) {
                        /* first time : get and copy the URL */
                        if (parser->href == NULL) {
-                               /* ALF - the sylpheed html parser returns an empty string,
+                               /* ALF - the claws html parser returns an empty string,
                                 * if still inside an <a>, but already parsed past HREF */
                                str = strtok(str, " ");
                                if (str) {
@@ -1074,7 +1035,7 @@ static void textview_show_html(TextView *textview, FILE *fp,
                        if (str != NULL)
                                textview_write_link(textview, str, parser->href, NULL);
                } else
-                       textview_write_line(textview, str, NULL);
+                       textview_write_line(textview, str, NULL, FALSE);
                lines++;
                if (lines % 500 == 0)
                        GTK_EVENTS_FLUSH();
@@ -1082,7 +1043,7 @@ static void textview_show_html(TextView *textview, FILE *fp,
                        return;
                }
        }
-       textview_write_line(textview, "\n", NULL);
+       textview_write_line(textview, "\n", NULL, FALSE);
        sc_html_parser_destroy(parser);
 }
 
@@ -1097,7 +1058,7 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
        g_return_if_fail(parser != NULL);
 
        while ((str = ertf_parse(parser)) != NULL) {
-               textview_write_line(textview, str, NULL);
+               textview_write_line(textview, str, NULL, FALSE);
                lines++;
                if (lines % 500 == 0)
                        GTK_EVENTS_FLUSH();
@@ -1182,6 +1143,7 @@ static void textview_make_clickable_parts(TextView *textview,
        } head = {NULL, NULL, 0,  NULL}, *last = &head;
 
        if (!g_utf8_validate(linebuf, -1, NULL)) {
+               g_free(mybuf);
                mybuf = g_malloc(strlen(linebuf)*2 +1);
                conv_localetodisp(mybuf, strlen(linebuf)*2 +1, linebuf);
        }
@@ -1243,7 +1205,7 @@ static void textview_make_clickable_parts(TextView *textview,
                        uri->end = gtk_text_iter_get_offset(&iter);
                        uri->filename = NULL;
                        textview->uri_list =
-                               g_slist_append(textview->uri_list, uri);
+                               g_slist_prepend(textview->uri_list, uri);
                }
 
                if (*normal_text)
@@ -1372,7 +1334,7 @@ static void textview_make_clickable_parts_later(TextView *textview,
                        uri->end = gtk_text_iter_get_offset(&end_iter);
                        uri->filename = NULL;
                        textview->uri_list =
-                               g_slist_append(textview->uri_list, uri);
+                               g_slist_prepend(textview->uri_list, uri);
                }
        } 
 
@@ -1382,7 +1344,7 @@ static void textview_make_clickable_parts_later(TextView *textview,
 #undef ADD_TXT_POS
 
 static void textview_write_line(TextView *textview, const gchar *str,
-                               CodeConverter *conv)
+                               CodeConverter *conv, gboolean do_quote_folding)
 {
        GtkTextView *text;
        GtkTextBuffer *buffer;
@@ -1435,36 +1397,60 @@ static void textview_write_line(TextView *textview, const gchar *str,
                textview->is_in_signature = TRUE;
        }
 
-       if (real_quotelevel > -1) {
-               if ( previousquotelevel != real_quotelevel ) {
+       if (real_quotelevel > -1 && do_quote_folding) {
+               if (!g_utf8_validate(buf, -1, NULL)) {
+                       gchar *utf8buf = NULL;
+                       utf8buf = g_malloc(BUFFSIZE);
+                       conv_localetodisp(utf8buf, BUFFSIZE, buf);
+                       strncpy2(buf, utf8buf, BUFFSIZE-1);
+                       g_free(utf8buf);
+               }
+do_quote:
+               if ( textview->prev_quote_level != real_quotelevel ) {
                        ClickableText *uri;
                        uri = g_new0(ClickableText, 1);
                        uri->uri = g_strdup("");
                        uri->data = g_strdup(buf);
                        uri->start = gtk_text_iter_get_offset(&iter);
+                       uri->is_quote = TRUE;
+                       uri->quote_level = real_quotelevel;
+                       uri->fg_color = g_strdup(fg_color);
+
                        gtk_text_buffer_insert_with_tags_by_name
-                                               (buffer, &iter, " [...]", -1,
-                                                "qlink", fg_color, NULL);
+                                       (buffer, &iter, " [...]", -1,
+                                        "qlink", fg_color, NULL);
                        uri->end = gtk_text_iter_get_offset(&iter);
+                       gtk_text_buffer_insert(buffer, &iter, "  \n", -1);
+                       
                        uri->filename = NULL;
-                       uri->fg_color = g_strdup(fg_color);
-                       uri->is_quote = TRUE;
-                       uri->quote_level = real_quotelevel;
                        textview->uri_list =
-                               g_slist_append(textview->uri_list, uri);
-                       gtk_text_buffer_insert(buffer, &iter, "  \n", -1);
+                               g_slist_prepend(textview->uri_list, uri);
                
-                       previousquotelevel = real_quotelevel;
+                       textview->prev_quote_level = real_quotelevel;
                } else {
-                       GSList *last = g_slist_last(textview->uri_list);
-                       ClickableText *lasturi = (ClickableText *)last->data;
-                       gchar *tmp = g_strdup_printf("%s%s", (gchar *)lasturi->data, buf);
-                       g_free(lasturi->data);
-                       lasturi->data = tmp;
+                       GSList *last = textview->uri_list;
+                       ClickableText *lasturi = NULL;
+                       gint e_len = 0, n_len = 0;
+                       
+                       if (textview->uri_list) {
+                               lasturi = (ClickableText *)last->data;
+                       } else {
+                               printf("oops (%d %d)\n",
+                                       real_quotelevel, textview->prev_quote_level);
+                       }               
+                       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';
                }
        } else {
                textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
-               previousquotelevel = -1;
+               textview->prev_quote_level = -1;
        }
 }
 
@@ -1522,7 +1508,7 @@ void textview_write_link(TextView *textview, const gchar *str,
                (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);
+       textview->uri_list = g_slist_prepend(textview->uri_list, r_uri);
 }
 
 static void textview_set_cursor(GdkWindow *window, GdkCursor *cursor)
@@ -1544,6 +1530,7 @@ void textview_clear(TextView *textview)
        textview_uri_list_remove_all(textview->uri_list);
        textview->uri_list = NULL;
        textview->uri_hover = NULL;
+       textview->prev_quote_level = -1;
 
        textview->body_pos = 0;
        if (textview->image) 
@@ -1559,8 +1546,16 @@ void textview_clear(TextView *textview)
 
 void textview_destroy(TextView *textview)
 {
+       GtkTextBuffer *buffer;
+       GtkClipboard *clipboard;
+
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+       clipboard = gtk_clipboard_get(GDK_SELECTION_PRIMARY);
+       gtk_text_buffer_remove_selection_clipboard(buffer, clipboard);
+
        textview_uri_list_remove_all(textview->uri_list);
        textview->uri_list = NULL;
+       textview->prev_quote_level = -1;
 
        g_free(textview);
 }
@@ -1591,6 +1586,7 @@ void textview_set_font(TextView *textview, const gchar *codeset)
                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) {
@@ -1661,6 +1657,8 @@ static gboolean header_is_internal(Header *header)
                {"AF:", "NF:", "PS:", "SRH:", "SFN:", "DSR:", "MID:", 
                 "CFG:", "PT:", "S:", "RQ:", "SSV:", "NSV:", "SSH:", 
                 "R:", "MAID:", "SCF:", "RMID:", "FMID:", "NAID:", 
+                "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-Sylpheed-Privacy-System:", "X-Sylpheed-End-Special-Headers:",
                 NULL};
@@ -1782,7 +1780,7 @@ bail:
        textview->image = NULL; 
 }
 
-static void textview_show_icon(TextView *textview, const gchar *stock_id)
+void textview_show_icon(TextView *textview, const gchar *stock_id)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        int x = 0;
@@ -1812,7 +1810,8 @@ static void textview_show_xface(TextView *textview)
        MsgInfo *msginfo = textview->messageview->msginfo;
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        int x = 0;
-
+       GdkWindow *window = NULL;
+       
        if (prefs_common.display_header_pane
        ||  !prefs_common.display_xface)
                goto bail;
@@ -1829,10 +1828,13 @@ static void textview_show_xface(TextView *textview)
 
        if (textview->image) 
                gtk_widget_destroy(textview->image);
-       
+
+       window = mainwindow_get_mainwindow() ?
+                       mainwindow_get_mainwindow()->window->window :
+                       textview->text->window;
        textview->image = xface_get_from_header(msginfo->extradata->xface,
                                &textview->text->style->white,
-                               textview->text->window);
+                               window);
        g_return_if_fail(textview->image != NULL);
 
        gtk_widget_show(textview->image);
@@ -1896,7 +1898,8 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                if ((procheader_headername_equal(header->name, "X-Mailer") ||
                     procheader_headername_equal(header->name,
                                                 "X-Newsreader")) &&
-                   strstr(header->body, "Sylpheed-Claws") != NULL) {
+                   (strstr(header->body, "Claws Mail") != NULL ||
+                    strstr(header->body, "Sylpheed-Claws") != NULL)) {
                        gtk_text_buffer_get_end_iter (buffer, &iter);
                        gtk_text_buffer_insert_with_tags_by_name
                                (buffer, &iter, header->body, -1,
@@ -2008,6 +2011,7 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_t:
        case GDK_l:
        case GDK_c:
+       case GDK_a:
                if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
                        KEY_PRESS_EVENT_STOP();
                        mimeview_pass_key_press_event(messageview->mimeview,
@@ -2243,10 +2247,13 @@ static ClickableText *textview_get_uri(TextView *textview,
        return uri;
 }
 
-static void textview_shift_uris_after(TextView *textview, gint start, gint shift)
+static void textview_shift_uris_after(TextView *textview, GSList *start_list, gint start, gint shift)
 {
        GSList *cur;
-       for (cur = textview->uri_list; cur; cur = cur->next) {
+       if (!start_list)
+               start_list = textview->uri_list;
+
+       for (cur = start_list; cur; cur = cur->next) {
                ClickableText *uri = (ClickableText *)cur->data;
                if (uri->start <= start)
                        continue;
@@ -2278,7 +2285,7 @@ static void textview_remove_uris_in(TextView *textview, gint start, gint end)
        }
 }
 
-static void textview_toggle_quote(TextView *textview, ClickableText *uri, gboolean expand_only)
+static void textview_toggle_quote(TextView *textview, GSList *start_list, ClickableText *uri, gboolean expand_only)
 {
        GtkTextIter start, end;
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
@@ -2308,7 +2315,7 @@ static void textview_toggle_quote(TextView *textview, ClickableText *uri, gboole
        if (!uri->q_expanded) {
                gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
                gtk_text_buffer_get_iter_at_offset(buffer, &end,   uri->end);
-               textview_shift_uris_after(textview, uri->start, 
+               textview_shift_uris_after(textview, start_list, uri->start, 
                        g_utf8_strlen((gchar *)uri->data, -1)-strlen(" [...]\n"));
                gtk_text_buffer_delete(buffer, &start, &end);
                gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
@@ -2324,7 +2331,7 @@ static void textview_toggle_quote(TextView *textview, ClickableText *uri, gboole
                gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
                gtk_text_buffer_get_iter_at_offset(buffer, &end,   uri->end);
                textview_remove_uris_in(textview, uri->start, uri->end);
-               textview_shift_uris_after(textview, uri->start, 
+               textview_shift_uris_after(textview, start_list, uri->start, 
                        strlen(" [...]\n")-g_utf8_strlen((gchar *)uri->data, -1));
                gtk_text_buffer_delete(buffer, &start, &end);
                gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
@@ -2390,7 +2397,7 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                        } 
                        return TRUE;
                } else if (qlink && bevent->button == 1) {
-                       textview_toggle_quote(textview, uri, FALSE);
+                       textview_toggle_quote(textview, NULL, uri, FALSE);
                        return TRUE;
                } else if (!g_ascii_strncasecmp(uri->uri, "mailto:", 7)) {
                        if (bevent->button == 3) {
@@ -2402,16 +2409,16 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
                                               bevent->button, bevent->time);
                        } else {
                                PrefsAccount *account = NULL;
-
+                               FolderItem   *folder_item = NULL;
                                if (textview->messageview && textview->messageview->msginfo &&
                                    textview->messageview->msginfo->folder) {
-                                       FolderItem   *folder_item;
+                                       
 
                                        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_new(account, uri->uri + 7, NULL);
+                               compose_new_with_folderitem(account, folder_item, uri->uri + 7);
                        }
                        return TRUE;
                } else if (g_ascii_strncasecmp(uri->uri, "file:", 5)) {
@@ -2444,6 +2451,20 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
        return FALSE;
 }
 
+gchar *textview_get_visible_uri                (TextView       *textview, 
+                                        ClickableText  *uri)
+{
+       GtkTextBuffer *buffer;
+       GtkTextIter start, end;
+
+       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
+
+       gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
+       gtk_text_buffer_get_iter_at_offset(buffer, &end,   uri->end);
+
+       return gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
+}
+
 /*!
  *\brief    Check to see if a web URL has been disguised as a different
  *          URL (possible with HTML email).
@@ -2455,23 +2476,15 @@ static gboolean textview_uri_button_pressed(GtkTextTag *tag, GObject *obj,
  *\return   gboolean TRUE if the URL is ok, or if the user chose to open
  *          it anyway, otherwise FALSE          
  */
-static gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
+gboolean textview_uri_security_check(TextView *textview, ClickableText *uri)
 {
        gchar *visible_str;
        gboolean retval = TRUE;
-       GtkTextBuffer *buffer;
-       GtkTextIter start, end;
 
        if (is_uri_string(uri->uri) == FALSE)
                return TRUE;
 
-       buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(textview->text));
-
-       gtk_text_buffer_get_iter_at_offset(buffer, &start, uri->start);
-       gtk_text_buffer_get_iter_at_offset(buffer, &end,   uri->end);
-
-       visible_str = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
-
+       visible_str = textview_get_visible_uri(textview, uri);
        if (visible_str == NULL)
                return TRUE;
 
@@ -2665,7 +2678,7 @@ static void save_file_cb (TextView *textview, guint action, void *data)
        filedir = g_path_get_dirname(filename);
        if (filedir && strcmp(filedir, ".")) {
                g_free(prefs_common.attach_save_dir);
-               prefs_common.attach_save_dir = g_strdup(filedir);
+               prefs_common.attach_save_dir = g_filename_to_utf8(filedir, -1, NULL, NULL, NULL);
        }
 
        g_free(filedir);
@@ -2701,7 +2714,7 @@ static void add_uri_to_addrbook_cb (TextView *textview, guint action, void *data
        /* Hiroyuki: please put this function in utils.c! */
        fromname = procheader_get_fromname(fromaddress);
        extract_address(fromaddress);
-       g_message("adding from textview %s <%s>", fromname, fromaddress);
+
        /* Add to address book - Match */
        addressbook_add_contact( fromname, fromaddress, NULL );
 
@@ -2741,3 +2754,20 @@ static void copy_mail_to_uri_cb  (TextView *textview, guint action, void *data)
                          NULL);
 }
 
+void textview_get_selection_offsets(TextView *textview, gint *sel_start, gint *sel_end)
+{
+               GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+               GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
+               GtkTextIter start, end;
+               if (gtk_text_buffer_get_selection_bounds(buffer, &start, &end)) {
+                       if (sel_start)
+                               *sel_start = gtk_text_iter_get_offset(&start);
+                       if (sel_end)
+                               *sel_end = gtk_text_iter_get_offset(&end);
+               } else {
+                       if (sel_start)
+                               *sel_start = -1;
+                       if (sel_end)
+                               *sel_end = -1;
+               }
+}