Make Message-ID string generation less confusing.
[claws.git] / src / messageview.c
index 1f1f0940e080ee2114455fee194a38fd5aa88afc..d2373161c1c3992a405dd49fd096cc468deb4e58 100644 (file)
@@ -1,6 +1,6 @@
 /*
- * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2013 Hiroyuki Yamamoto and the Claws Mail team
+ * Claws Mail -- a GTK+ based, lightweight, and fast e-mail client
+ * Copyright (C) 1999-2016 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
@@ -14,7 +14,6 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program. If not, see <http://www.gnu.org/licenses/>.
- * 
  */
 
 #include "defs.h"
@@ -67,7 +66,8 @@
 #include "version.h"
 #include "statusbar.h"
 #include "folder_item_prefs.h"
-#ifndef USE_NEW_ADDRBOOK
+#include "avatars.h"
+#ifndef USE_ALT_ADDRBOOK
        #include "addressbook.h"
 #else
        #include "addressadd.h"
@@ -250,7 +250,7 @@ static GtkActionEntry msgview_entries[] =
        {"View/Goto/ParentMessage",     NULL, N_("Parent message"), "<control>Up", NULL, G_CALLBACK(parent_cb) },
        /* {"View/Goto/---",            NULL, "---", NULL, NULL, NULL }, */
        {"View/Goto/NextUnreadFolder",  NULL, N_("Next unread _folder"), "<shift>G", NULL, G_CALLBACK(goto_unread_folder_cb) },
-       {"View/Goto/OtherFolder",       NULL, N_("_Other folder..."), "G", NULL, G_CALLBACK(goto_folder_cb) },
+       {"View/Goto/Folder",            NULL, N_("F_older..."), "G", NULL, G_CALLBACK(goto_folder_cb) },
        /* {"View/Goto/---",            NULL, "---", NULL, NULL, NULL }, */
        {"View/Goto/NextPart",          NULL, N_("Next part"), "A", NULL, G_CALLBACK(goto_next_part_cb) },
        {"View/Goto/PrevPart",          NULL, N_("Previous part"), "Z", NULL, G_CALLBACK(goto_prev_part_cb) },
@@ -365,6 +365,7 @@ static GtkRadioActionEntry msgview_radio_enc_entries[] =
        ENC_ACTION(CS_ISO_8859_9, C_ISO_8859_9, N_("Turkish (ISO-8859-_9)")), /* RADIO set_charset_cb */
        ENC_ACTION("Cyrillic/"CS_ISO_8859_5, C_ISO_8859_5, "ISO-8859-_5"), /* RADIO set_charset_cb */
        ENC_ACTION("Cyrillic/"CS_KOI8_R, C_KOI8_R, "KOI8-_R"), /* RADIO set_charset_cb */
+       ENC_ACTION("Cyrillic/"CS_MACCYR, C_MACCYR, "MAC_CYR"), /* RADIO set_charset_cb */
        ENC_ACTION("Cyrillic/"CS_KOI8_U, C_KOI8_U, "KOI8-_U"), /* RADIO set_charset_cb */
        ENC_ACTION("Cyrillic/"CS_WINDOWS_1251, C_WINDOWS_1251, "Windows-1251"), /* RADIO set_charset_cb */
        ENC_ACTION("Japanese/"CS_ISO_2022_JP, C_ISO_2022_JP, "ISO-2022-_JP"), /* RADIO set_charset_cb */
@@ -441,7 +442,7 @@ MessageView *messageview_create(MainWindow *mainwin)
        return messageview;
 }
 
-GList *messageview_get_msgview_list(void)
+const GList *messageview_get_msgview_list(void)
 {
        return msgview_list;
 }
@@ -525,7 +526,7 @@ static void messageview_add_toolbar(MessageView *msgview, GtkWidget *window)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "ParentMessage", "View/Goto/ParentMessage", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator7", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextUnreadFolder", "View/Goto/NextUnreadFolder", GTK_UI_MANAGER_MENUITEM)
-       MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "OtherFolder", "View/Goto/OtherFolder", GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Folder", "View/Goto/Folder", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "Separator8", "View/Goto/---", GTK_UI_MANAGER_SEPARATOR)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "NextPart", "View/Goto/NextPart", GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Goto", "PrevPart", "View/Goto/PrevPart", GTK_UI_MANAGER_MENUITEM)
@@ -571,6 +572,7 @@ static void messageview_add_toolbar(MessageView *msgview, GtkWidget *window)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding", "Cyrillic", "View/Encoding/Cyrillic", GTK_UI_MANAGER_MENU)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_ISO_8859_5, "View/Encoding/Cyrillic/"CS_ISO_8859_5, GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_KOI8_R, "View/Encoding/Cyrillic/"CS_KOI8_R, GTK_UI_MANAGER_MENUITEM)
+       MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_MACCYR, "View/Encoding/Cyrillic/"CS_MACCYR, GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_KOI8_U, "View/Encoding/Cyrillic/"CS_KOI8_U, GTK_UI_MANAGER_MENUITEM)
        MENUITEM_ADDUI_MANAGER(msgview->ui_manager, "/Menu/View/Encoding/Cyrillic", CS_WINDOWS_1251, "View/Encoding/Cyrillic/"CS_WINDOWS_1251, GTK_UI_MANAGER_MENUITEM)
 
@@ -835,7 +837,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
                ok = strcasecmp(to_addr, buf);
                g_free(to_addr);
        } else {
-               strncpy(buf, _("<No Return-Path found>"), 
+               g_strlcpy(buf, _("<No Return-Path found>"), 
                                sizeof(buf));
        }
        
@@ -901,7 +903,7 @@ static gint disposition_notification_send(MsgInfo *msginfo)
        /* chmod for security */
        if (change_file_mode_rw(fp, tmp) < 0) {
                FILE_OP_ERROR(tmp, "chmod");
-               g_warning("can't change file mode\n");
+               g_warning("can't change file mode");
        }
        
        addr = g_strdup(to);
@@ -974,26 +976,13 @@ static gint disposition_notification_send(MsgInfo *msginfo)
                goto FILE_ERROR;
 
        /* Message ID */
-       if (account->set_domain && account->domain) {
-               g_snprintf(buf, sizeof(buf), "%s", account->domain); 
-       } else if (!strncmp(get_domain_name(), "localhost", strlen("localhost"))) {
-               g_snprintf(buf, sizeof(buf), "%s", 
-                       strchr(account->address, '@') ?
-                               strchr(account->address, '@')+1 :
-                               account->address);
-       } else {
-               g_snprintf(buf, sizeof(buf), "%s", "");
-       }
-       
        if (account->gen_msgid) {
-               gchar *addr = NULL;
-               if (account->msgid_with_addr) {
-                       addr = account->address;
-               }
-               generate_msgid(buf, sizeof(buf), addr);
-
-               if (fprintf(fp, "Message-ID: <%s>\n", buf) < 0)
+               gchar *addr = prefs_account_generate_msgid(account);
+               if (fprintf(fp, "Message-ID: <%s>\n", addr) < 0) {
+                       g_free(addr);
                        goto FILE_ERROR;
+               }
+               g_free(addr);
        }
 
        boundary = generate_mime_boundary("DN");
@@ -1078,13 +1067,13 @@ static gint disposition_notification_send(MsgInfo *msginfo)
        queue = account_get_special_folder(account, F_QUEUE);
        if (!queue) queue = folder_get_default_queue();
        if (!queue) {
-               g_warning("can't find queue folder\n");
+               g_warning("can't find queue folder");
                claws_unlink(tmp);
                return -1;
        }
        folder_item_scan(queue);
        if ((num = folder_item_add_msg(queue, tmp, NULL, TRUE)) < 0) {
-               g_warning("can't queue the message\n");
+               g_warning("can't queue the message");
                claws_unlink(tmp);
                return -1;
        }
@@ -1207,11 +1196,12 @@ static void messageview_register_nav(MessageView *messageview)
 }
 
 gboolean messageview_nav_has_prev(MessageView *messageview) {
-       return messageview->trail != NULL &&  messageview->trail_pos > 0;
+       return messageview != NULL && messageview->trail != NULL
+               && messageview->trail_pos > 0;
 }
 
 gboolean messageview_nav_has_next(MessageView *messageview) {
-       if (!messageview->trail)
+       if (!messageview || !messageview->trail)
                return FALSE;
        
        return sc_g_list_bigger(messageview->trail, messageview->trail_pos + 1);
@@ -1269,12 +1259,66 @@ MsgInfo *messageview_nav_get_next(MessageView *messageview) {
        return info;
 }
 
+static gboolean messageview_try_select_mimeinfo(MessageView *messageview, MsgInfo *msginfo, MimeInfo *mimeinfo)
+{
+       if (mimeinfo->type == MIMETYPE_TEXT) {
+               if (!strcasecmp(mimeinfo->subtype, "calendar")
+                               && mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
+                       mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
+                       return TRUE;
+               } else if (!strcasecmp(mimeinfo->subtype, "html")
+                               && mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT
+                               && (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS
+                                       || (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT
+                                               && prefs_common.promote_html_part))) {
+                       mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
+                       return TRUE;
+               }
+       }
+       return FALSE;
+}
+
+static void messageview_find_part_depth_first(MimeInfoSearch *context, MimeMediaType type, const gchar *subtype)
+{
+       MimeInfo * mimeinfo = context->current;
+
+       if (!mimeinfo)
+               return;
+
+       debug_print("found part %d/%s\n", mimeinfo->type, mimeinfo->subtype);
+
+       if (mimeinfo->type == type
+                       && !strcasecmp(mimeinfo->subtype, subtype)) {
+               context->found = mimeinfo;
+       } else if (mimeinfo->type == MIMETYPE_MULTIPART) {
+               if (!strcasecmp(mimeinfo->subtype, "alternative")
+                               || !strcasecmp(mimeinfo->subtype, "related")) {
+                       context->found = procmime_mimeinfo_next(mimeinfo);
+                       while (context->found && context->found != context->parent) {
+                               if (context->found->type == type
+                                       && !strcasecmp(context->found->subtype, subtype))
+                                               break;
+                               context->found = procmime_mimeinfo_next(context->found);
+                       }
+                       if (context->found == context->parent)
+                               context->found = NULL;
+               }
+               if (!context->found
+                       && (!strcasecmp(mimeinfo->subtype, "related")
+                               || !strcasecmp(mimeinfo->subtype, "mixed"))) {
+                       context->parent = mimeinfo;
+                       context->current = procmime_mimeinfo_next(mimeinfo);
+                       messageview_find_part_depth_first(context, type, subtype);
+               }
+       }
+}
+
 gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                      gboolean all_headers)
 {
        gchar *text = NULL;
        gchar *file;
-       MimeInfo *mimeinfo, *encinfo, *brokeninfo, *root;
+       MimeInfo *mimeinfo, *encinfo, *root;
        gchar *subject = NULL;
        cm_return_val_if_fail(msginfo != NULL, -1);
 
@@ -1325,7 +1369,7 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                statuswindow_pop_all();
 
        if (!file) {
-               g_warning("can't get message file path.\n");
+               g_warning("can't get message file path.");
                textview_show_error(messageview->mimeview->textview);
                return -1;
        }
@@ -1366,7 +1410,7 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
        }
                        
        if (messageview->msginfo != msginfo) {
-               procmsg_msginfo_free(messageview->msginfo);
+               procmsg_msginfo_free(&(messageview->msginfo));
                messageview->msginfo = NULL;
                messageview_set_menu_sensitive(messageview);
                messageview->msginfo = 
@@ -1378,7 +1422,8 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                messageview_set_menu_sensitive(messageview);
                messageview->msginfo = msginfo;
        }
-       headerview_show(messageview->headerview, messageview->msginfo);
+       if (prefs_common.display_header_pane)
+               headerview_show(messageview->headerview, messageview->msginfo);
 
        messageview_register_nav(messageview);
        messageview_set_position(messageview, 0);
@@ -1428,7 +1473,7 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                return_receipt_show(messageview->noticeview, 
                                    messageview->msginfo);
 
-       if ((brokeninfo = find_broken_part(mimeinfo)) != NULL) {
+       if (find_broken_part(mimeinfo) != NULL) {
                noticeview_set_icon(messageview->noticeview,
                                    STOCK_PIXMAP_NOTICE_WARN);
                if (!noticeview_is_visible(messageview->noticeview)) {
@@ -1450,82 +1495,36 @@ gint messageview_show(MessageView *messageview, MsgInfo *msginfo,
                        
        root = mimeinfo;
        mimeinfo = procmime_mimeinfo_next(mimeinfo);
-       if (!all_headers && mimeinfo 
-                       && (mimeinfo->type != MIMETYPE_TEXT || 
-           strcasecmp(mimeinfo->subtype, "plain")) 
-                       && (mimeinfo->type != MIMETYPE_MULTIPART || 
-           strcasecmp(mimeinfo->subtype, "signed"))) {
-               if (strcasecmp(mimeinfo->subtype, "html")) {
-                       MimeInfo *saved_mimeinfo = mimeinfo;
-                       MimeInfo *alt_parent = mimeinfo;
-
-                       /* if multipart/{related,mixed} part, look inside for a multipart/alternative child */
-                       if (mimeinfo->type == MIMETYPE_MULTIPART &&
-                           (!strcasecmp(mimeinfo->subtype, "related") ||
-                            !strcasecmp(mimeinfo->subtype, "mixed"))) {
-                               for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
-                                       if (mimeinfo->node->parent != saved_mimeinfo->node) {
-                                               /* only consider children of the 
-                                                * multipart/{related,mixed} part */
-                                               continue;
-                                       }
-                                       if (mimeinfo->type == MIMETYPE_MULTIPART && 
-                                           !strcasecmp(mimeinfo->subtype, "alternative")) {
-                                               /* we got an alternative part */
-                                               alt_parent = mimeinfo;
-                                               break;
-                                       }
-                                       if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "calendar") &&
-                                           mimeview_has_viewer_for_content_type(messageview->mimeview,
-                                                                                "text/calendar")) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
-                                               goto done;
-                                       } else if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "html") &&
-                                           mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT &&
-                                                       (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS ||
-                                                        (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT &&
-                                                               prefs_common.promote_html_part))) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
+       if (!all_headers && mimeinfo
+                       && (mimeinfo->type != MIMETYPE_TEXT
+                               || strcasecmp(mimeinfo->subtype, "plain"))
+                       && (mimeinfo->type != MIMETYPE_MULTIPART
+                               || strcasecmp(mimeinfo->subtype, "signed"))) {
+               if (strcasecmp(mimeinfo->subtype, "html")) {
+                       MimeInfoSearch context = {
+                               .parent = root,
+                               .current = mimeinfo,
+                               .found = NULL
+                       };
+                       if (mimeview_has_viewer_for_content_type(messageview->mimeview, "text/calendar")) {
+                               MimeInfoSearch cal_context = context;
+                               messageview_find_part_depth_first(&cal_context, MIMETYPE_TEXT, "calendar");
+                               if (cal_context.found) { /* calendar found */
+                                       mimeinfo = cal_context.found;
+                                       if (messageview_try_select_mimeinfo(messageview, msginfo, mimeinfo))
                                                goto done;
-                                       }
                                }
                        }
-
-                       /* if we now have a multipart/alternative part (possibly inside a
-                        * multipart/{related,mixed} part, look for an HTML part inside */
-                       if (mimeinfo && mimeinfo->type == MIMETYPE_MULTIPART &&
-                           !strcasecmp(mimeinfo->subtype, "alternative")) {
-                               for (; mimeinfo; mimeinfo = procmime_mimeinfo_next(mimeinfo)) {
-                                       if (mimeinfo->node->parent != alt_parent->node) {
-                                               /* only consider children of the 
-                                                * multipart/alternative part, so as
-                                                * not to show html attachments */
-                                               continue;
-                                       }
-                                       
-                                       if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "calendar") &&
-                                           mimeview_has_viewer_for_content_type(messageview->mimeview,
-                                                                                "text/calendar")) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
-                                               goto done;
-                                       } else if (mimeinfo->type == MIMETYPE_TEXT && 
-                                           !strcasecmp(mimeinfo->subtype, "html") &&
-                                           mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT &&
-                                                       (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS ||
-                                                        (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT &&
-                                                               prefs_common.promote_html_part))) {
-                                               mimeview_select_mimepart_icon(messageview->mimeview, mimeinfo);
-                                               goto done;
-                                       }
-                               }
-                       }
-                       
-                       /* if we didn't find anything, go back to start */
-                       if (!mimeinfo) 
-                               mimeinfo = saved_mimeinfo;
+                       messageview_find_part_depth_first(&context, MIMETYPE_TEXT, "html");
+                       if (context.found &&
+                           (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_ALWAYS ||
+                            (msginfo->folder->prefs->promote_html_part == HTML_PROMOTE_DEFAULT &&
+                             prefs_common.promote_html_part))) { /* html found */
+                               mimeinfo = context.found;
+                               if (messageview_try_select_mimeinfo(messageview, msginfo, mimeinfo))
+                                       goto done;
+                       } else
+                               mimeinfo = root; /* nothing found */
 
                        if (!mimeview_show_part(messageview->mimeview, mimeinfo))
                                mimeview_select_mimepart_icon(messageview->mimeview, root);
@@ -1576,7 +1575,7 @@ void messageview_clear(MessageView *messageview)
 {
        if (!messageview)
                return;
-       procmsg_msginfo_free(messageview->msginfo);
+       procmsg_msginfo_free(&(messageview->msginfo));
        messageview->msginfo = NULL;
        messageview->filtered = FALSE;
 
@@ -1628,7 +1627,7 @@ void messageview_destroy(MessageView *messageview)
        mimeview_destroy(messageview->mimeview);
        noticeview_destroy(messageview->noticeview);
 
-       procmsg_msginfo_free(messageview->msginfo);
+       procmsg_msginfo_free(&(messageview->msginfo));
        toolbar_clear_list(TOOLBAR_MSGVIEW);
        if (messageview->toolbar) {
                toolbar_destroy(messageview->toolbar);
@@ -1935,7 +1934,7 @@ static void return_receipt_show(NoticeView *noticeview, MsgInfo *msginfo)
        if (from_me) {
                noticeview_set_icon(noticeview, STOCK_PIXMAP_NOTICE_WARN);
                if (MSG_IS_RETRCPT_GOT(msginfo->flags)) {
-                       noticeview_set_text(noticeview, _("You got a return receipt for this message : "
+                       noticeview_set_text(noticeview, _("You got a return receipt for this message: "
                                                          "it has been displayed by the recipient."));
                } else {
                        noticeview_set_text(noticeview, _("You asked for a return receipt in this message."));
@@ -1960,7 +1959,7 @@ static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo
 
        file = procmsg_get_message_file_path(msginfo);
        if (!file) {
-               g_warning("can't get message file path.\n");
+               g_warning("can't get message file path.");
                return;
        }
 
@@ -1973,7 +1972,7 @@ static void return_receipt_send_clicked(NoticeView *noticeview, MsgInfo *msginfo
                noticeview_hide(noticeview);
        }               
 
-       procmsg_msginfo_free(tmpmsginfo);
+       procmsg_msginfo_free(&tmpmsginfo);
        g_free(file);
 }
 
@@ -2114,6 +2113,8 @@ gchar *messageview_get_selection(MessageView *msgview)
        GtkTextView *edit = NULL;
        GtkTextBuffer *textbuf;
        gint body_pos = 0;
+       GtkTextIter start_iter, end_iter;
+       GtkTextMark *body_start, *body_end;
        
        cm_return_val_if_fail(msgview != NULL, NULL);
 
@@ -2135,15 +2136,35 @@ gchar *messageview_get_selection(MessageView *msgview)
 
        textbuf = gtk_text_view_get_buffer(edit);
 
-       if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL))
+       if (gtk_text_buffer_get_selection_bounds(textbuf, NULL, NULL)) {
                return gtkut_text_view_get_selection(edit);
-       else if (msgview->filtered) {
-               GtkTextIter start_iter, end_iter;
-               gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
-               gtk_text_buffer_get_end_iter(textbuf, &end_iter);
-               gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
-       } else
-               text = NULL;
+       } else {
+               if (msgview->filtered) {
+                       gtk_text_buffer_get_iter_at_offset(textbuf, &start_iter, body_pos);
+                       gtk_text_buffer_get_end_iter(textbuf, &end_iter);
+               } else {
+                       body_start = gtk_text_buffer_get_mark(textbuf, "body_start");
+
+                       /* If there is no body_start mark, an attachment is likely
+                        * selected, and we're looking at instructions on what to do
+                        * with it. No point in quoting that, so we'll just return NULL,
+                        * so that original message body is quoted instead down the line.
+                        */
+                       if (body_start == NULL) {
+                               return NULL;
+                       }
+
+                       gtk_text_buffer_get_iter_at_mark(textbuf, &start_iter, body_start);
+
+                       body_end = gtk_text_buffer_get_mark(textbuf, "body_end");
+                       if (body_end != NULL) /* Just in case */
+                               gtk_text_buffer_get_iter_at_mark(textbuf, &end_iter, body_end);
+                       else
+                               gtk_text_buffer_get_end_iter(textbuf, &end_iter);
+               }
+
+               return gtk_text_buffer_get_text(textbuf, &start_iter, &end_iter, FALSE);
+       }
 
        return text;
 }
@@ -2187,7 +2208,10 @@ static void print_mimeview(MimeView *mimeview, gint sel_start, gint sel_end, gin
                mainwin = mainwindow_get_mainwindow();
                printing_print(GTK_TEXT_VIEW(mimeview->textview->text),
                               mainwin ? GTK_WINDOW(mainwin->window) : NULL,
-                               sel_start, sel_end);
+                               sel_start, sel_end,
+                               (mimeview->textview->image 
+                                       ? GTK_IMAGE(mimeview->textview->image)
+                                       : NULL));
        }
 }
 
@@ -2524,7 +2548,7 @@ static void prev_history_cb(GtkAction *action, gpointer data)
                messageview_show(messageview, info, 
                                         messageview->all_headers);
                messageview->updating = FALSE;
-               procmsg_msginfo_free(info);
+               procmsg_msginfo_free(&info);
                if (messageview->deferred_destroy) {
                        debug_print("messageview got away!\n");
                        messageview_destroy(messageview);
@@ -2542,7 +2566,7 @@ static void next_history_cb(GtkAction *action, gpointer data)
                messageview_show(messageview, info, 
                                         messageview->all_headers);
                messageview->updating = FALSE;
-               procmsg_msginfo_free(info);
+               procmsg_msginfo_free(&info);
                if (messageview->deferred_destroy) {
                        debug_print("messageview got away!\n");
                        messageview_destroy(messageview);
@@ -2579,8 +2603,9 @@ static void parent_cb(GtkAction *action, gpointer data)
 static void goto_unread_folder_cb(GtkAction *action, gpointer data)
 {
        MessageView *messageview = (MessageView *)data;
+
        messageview->updating = TRUE;
-       folderview_select_next_unread(messageview->mainwin->folderview, FALSE);
+       folderview_select_next_with_flag(messageview->mainwin->folderview, MSG_UNREAD, FALSE);
        messageview->updating = FALSE;
 
        if (messageview->deferred_destroy) {
@@ -2604,9 +2629,7 @@ static void goto_unread_folder_cb(GtkAction *action, gpointer data)
 static void goto_folder_cb(GtkAction *action, gpointer data)
 {
        MessageView *messageview = (MessageView *)data;
-       messageview->updating = TRUE;
        FolderItem *to_folder;
-       messageview->updating = FALSE;
 
        to_folder = foldersel_folder_sel(NULL, FOLDER_SEL_ALL, NULL, FALSE);
 
@@ -2717,7 +2740,7 @@ static void show_all_header_cb(GtkToggleAction *action, gpointer data)
        if (!msginfo) return;
        messageview->msginfo = NULL;
        messageview_show(messageview, msginfo, messageview->all_headers);
-       procmsg_msginfo_free(msginfo);
+       procmsg_msginfo_free(&msginfo);
        main_window_set_menu_sensitive(messageview->mainwin);
        summary_redisplay_msg(messageview->mainwin->summaryview);
 }
@@ -2749,7 +2772,7 @@ static void msg_hide_quotes_cb(GtkToggleAction *action, gpointer data)
        messageview->msginfo = NULL;
        messageview_show(messageview, msginfo,
                         messageview->all_headers);
-       procmsg_msginfo_free(msginfo);
+       procmsg_msginfo_free(&msginfo);
        
        /* update main window */
        main_window_set_menu_sensitive(messageview->mainwin);
@@ -2804,15 +2827,14 @@ static void reply_cb(GtkAction *gaction, gpointer data)
 
 static void addressbook_open_cb(GtkAction *action, gpointer data)
 {
-#ifndef USE_NEW_ADDRBOOK
+#ifndef USE_ALT_ADDRBOOK
        addressbook_open(NULL);
 #else
        GError* error = NULL;
        
        addressbook_dbus_open(FALSE, &error);
        if (error) {
-               g_warning("Failed to open address book");
-               g_warning("%s", error->message);
+               g_warning("Failed to open address book: %s", error->message);
                g_error_free(error);
        }
 #endif
@@ -2823,9 +2845,8 @@ static void add_address_cb(GtkAction *action, gpointer data)
        MessageView *messageview = (MessageView *)data;
        MsgInfo *msginfo, *full_msginfo;
        gchar *from;
-       GtkWidget *image = NULL;
        GdkPixbuf *picture = NULL;
-       gchar *face;
+       AvatarRender *avatarr;
 
        if (!messageview->msginfo || !messageview->msginfo->from) 
                return;
@@ -2836,31 +2857,23 @@ static void add_address_cb(GtkAction *action, gpointer data)
        extract_address(from);
        
        full_msginfo = procmsg_msginfo_get_full_info(msginfo);
-       face = procmsg_msginfo_get_avatar(full_msginfo, AVATAR_FACE);
-       if (face) {
-               image = face_get_from_header(face);
-       }
-#if HAVE_LIBCOMPFACE
-       else {
-               gchar *xface = procmsg_msginfo_get_avatar(full_msginfo, AVATAR_XFACE);
-               if (xface) {
-                       image = xface_get_from_header(xface);
-               }
-       }
-#endif
-       procmsg_msginfo_free(full_msginfo);
-       if (image)
-               picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
 
-#ifndef USE_NEW_ADDRBOOK
+       avatarr = avatars_avatarrender_new(full_msginfo);
+       hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
+
+       procmsg_msginfo_free(&full_msginfo);
+
+       if (avatarr->image != NULL)
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
+
+#ifndef USE_ALT_ADDRBOOK
        addressbook_add_contact(msginfo->fromname, from, NULL, picture);
 #else
        if (addressadd_selection(msginfo->fromname, from, NULL, picture)) {
                debug_print( "addressbook_add_contact - added\n" );
        }
 #endif
-       if (image)
-               gtk_widget_destroy(image);
+       avatars_avatarrender_free(avatarr);
 }
 
 static void create_filter_cb(GtkAction *gaction, gpointer data)