Factorize to make clearer
[claws.git] / src / textview.c
index c65afdfcef32470ff82743ed63f729b6fdbb68c6..d10485ddb3e9ae52cd7ec88d69173085498c5e12 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2011 Hiroyuki Yamamoto and the Claws Mail team
+ * Copyright (C) 1999-2014 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
@@ -19,6 +19,7 @@
 
 #ifdef HAVE_CONFIG_H
 #  include "config.h"
+#include "claws-features.h"
 #endif
 
 #include "defs.h"
 #if HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
-#if HAVE_LIBCOMPFACE
-#  include <compface.h>
-#endif
-
-#if HAVE_LIBCOMPFACE
-#define XPM_XFACE_HEIGHT       (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
-#endif
 
 #include "main.h"
 #include "summaryview.h"
 #include "html.h"
 #include "enriched.h"
 #include "compose.h"
-#include "addressbook.h"
+#ifndef USE_NEW_ADDRBOOK
+       #include "addressbook.h"
+       #include "addrindex.h"
+#else
+       #include "addressbook-dbus.h"
+       #include "addressadd.h"
+#endif
 #include "displayheader.h"
 #include "account.h"
 #include "mimeview.h"
 #include "inputdialog.h"
 #include "timing.h"
 #include "tags.h"
-#include "addrindex.h"
+#include "manage_window.h"
+#include "folder_item_prefs.h"
+#include "hooks.h"
+#include "avatars.h"
 
 static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
@@ -101,6 +104,27 @@ static GdkColor emphasis_color = {
        (gushort)0
 };
 
+static GdkColor diff_added_color = {
+       (gulong)0,
+       (gushort)0,
+       (gushort)0,
+       (gushort)0
+};
+
+static GdkColor diff_deleted_color = {
+       (gulong)0,
+       (gushort)0,
+       (gushort)0,
+       (gushort)0
+};
+
+static GdkColor diff_hunk_color = {
+       (gulong)0,
+       (gushort)0,
+       (gushort)0,
+       (gushort)0
+};
+
 static GdkCursor *hand_cursor = NULL;
 static GdkCursor *text_cursor = NULL;
 static GdkCursor *watch_cursor= NULL;
@@ -483,6 +507,24 @@ static void textview_create_tags(GtkTextView *text, TextView *textview)
        gtk_text_buffer_create_tag(buffer, "link-hover",
                        "underline", PANGO_UNDERLINE_SINGLE,
                        NULL);
+       gtk_text_buffer_create_tag(buffer, "diff-add",
+                       "foreground-gdk", &diff_added_color,
+                       NULL);
+       gtk_text_buffer_create_tag(buffer, "diff-del",
+                       "foreground-gdk", &diff_deleted_color,
+                       NULL);
+       gtk_text_buffer_create_tag(buffer, "diff-add-file",
+                       "foreground-gdk", &diff_added_color,
+                       "weight", PANGO_WEIGHT_BOLD,
+                       NULL);
+       gtk_text_buffer_create_tag(buffer, "diff-del-file",
+                       "foreground-gdk", &diff_deleted_color,
+                       "weight", PANGO_WEIGHT_BOLD,
+                       NULL);
+       gtk_text_buffer_create_tag(buffer, "diff-hunk",
+                       "foreground-gdk", &diff_hunk_color,
+                       "weight", PANGO_WEIGHT_BOLD,
+                       NULL);
        g_signal_connect(G_OBJECT(qtag), "event",
                          G_CALLBACK(textview_uri_button_pressed), textview);
        g_signal_connect(G_OBJECT(tag), "event",
@@ -522,8 +564,9 @@ static void textview_update_message_colors(TextView *textview)
        GtkTextTag *tag = NULL;
 
        quote_bgcolors[0] = quote_bgcolors[1] = quote_bgcolors[2] = black;
-       quote_colors[0] = quote_colors[1] = quote_colors[2] = 
-               uri_color = emphasis_color = signature_color = black;
+       quote_colors[0] = quote_colors[1] = quote_colors[2] = black;
+       uri_color = emphasis_color = signature_color = diff_added_color =
+               diff_deleted_color = diff_hunk_color = black;
 
        if (prefs_common.enable_color) {
                /* grab the quote colors, converting from an int to a GdkColor */
@@ -539,6 +582,12 @@ static void textview_update_message_colors(TextView *textview)
                                               &signature_color);
                gtkut_convert_int_to_gdk_color(prefs_common.emphasis_col,
                                               &emphasis_color);
+               gtkut_convert_int_to_gdk_color(prefs_common.diff_added_color,
+                                              &diff_added_color);
+               gtkut_convert_int_to_gdk_color(prefs_common.diff_deleted_color,
+                                              &diff_deleted_color);
+               gtkut_convert_int_to_gdk_color(prefs_common.diff_hunk_color,
+                                              &diff_hunk_color);
        }
        if (prefs_common.enable_color && prefs_common.enable_bgcolor) {
                gtkut_convert_int_to_gdk_color(prefs_common.quote_level1_bgcol,
@@ -560,6 +609,11 @@ static void textview_update_message_colors(TextView *textview)
        CHANGE_TAG_COLOR("signature", &signature_color, NULL);
        CHANGE_TAG_COLOR("link", &uri_color, NULL);
        CHANGE_TAG_COLOR("link-hover", &uri_color, NULL);
+       CHANGE_TAG_COLOR("diff-add", &diff_added_color, NULL);
+       CHANGE_TAG_COLOR("diff-del", &diff_deleted_color, NULL);
+       CHANGE_TAG_COLOR("diff-add-file", &diff_added_color, NULL);
+       CHANGE_TAG_COLOR("diff-del-file", &diff_deleted_color, NULL);
+       CHANGE_TAG_COLOR("diff-hunk", &diff_hunk_color, NULL);
 }
 #undef CHANGE_TAG_COLOR
 
@@ -577,34 +631,28 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
        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"))) {
-               textview->loading = TRUE;
-               textview->stop_loading = FALSE;
-               
-               textview_clear(textview);
-               textview_add_parts(textview, mimeinfo);
-
-               textview->loading = FALSE;
-               textview->stop_loading = FALSE;
-               END_TIMING();
-               return;
-       }
        textview->loading = TRUE;
        textview->stop_loading = FALSE;
 
-       if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
-               perror("fseek");
-
        textview_clear(textview);
 
-       if (mimeinfo->type == MIMETYPE_MULTIPART)
+       if ((mimeinfo->type == MIMETYPE_MULTIPART) ||
+           ((mimeinfo->type == MIMETYPE_MESSAGE) && !g_ascii_strcasecmp(mimeinfo->subtype, "rfc822"))) {
                textview_add_parts(textview, mimeinfo);
-       else
-               textview_write_body(textview, mimeinfo);
+       } else {
+               if (fseek(fp, mimeinfo->offset, SEEK_SET) < 0)
+                       perror("fseek");
+
+               if (mimeinfo->type == MIMETYPE_MULTIPART)
+                       textview_add_parts(textview, mimeinfo);
+               else
+                       textview_write_body(textview, mimeinfo);
+       }
 
        textview->loading = FALSE;
        textview->stop_loading = FALSE;
+       textview_set_position(textview, 0);
+
        END_TIMING();
 }
 
@@ -862,16 +910,40 @@ void textview_show_error(TextView *textview)
        textview_show_icon(textview, GTK_STOCK_DIALOG_ERROR);
 }
 
+void textview_show_info(TextView *textview, const gchar *info_str)
+{
+       GtkTextView *text;
+       GtkTextBuffer *buffer;
+       GtkTextIter iter;
+
+       textview_set_font(textview, NULL);
+       textview_clear(textview);
+
+       text = GTK_TEXT_VIEW(textview->text);
+       buffer = gtk_text_view_get_buffer(text);
+       gtk_text_buffer_get_start_iter(buffer, &iter);
+
+       TEXTVIEW_INSERT(info_str);
+       textview_show_icon(textview, GTK_STOCK_DIALOG_INFO);
+       textview_cursor_normal(textview);
+}
+
 void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
 {
        GtkTextView *text;
        GtkTextBuffer *buffer;
        GtkTextIter iter;
        const gchar *name;
-       gchar *content_type;
+       gchar *content_type, *shortcut;
+       GtkUIManager *ui_manager;
 
        if (!partinfo) return;
 
+       if (textview->messageview->window != NULL)
+               ui_manager = textview->messageview->ui_manager;
+       else
+               ui_manager = textview->messageview->mainwin->ui_manager;
+
        textview_set_font(textview, NULL);
        textview_clear(textview);
 
@@ -906,7 +978,11 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        TEXTVIEW_INSERT(_("     - To save, select "));
        TEXTVIEW_INSERT_LINK(_("'Save as...'"), "sc://save_as", NULL);
 #ifndef GENERIC_UMPC
-       TEXTVIEW_INSERT(_(" (Shortcut key: 'y')"));
+       TEXTVIEW_INSERT(_(" (Shortcut key: '"));
+       shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/File/SavePartAs");
+       TEXTVIEW_INSERT(shortcut);
+       g_free(shortcut);
+       TEXTVIEW_INSERT("')");
 #endif
        TEXTVIEW_INSERT("\n");
 
@@ -914,7 +990,11 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        TEXTVIEW_INSERT_LINK(_("'Display as text'"), "sc://display_as_text", NULL);
 
 #ifndef GENERIC_UMPC
-       TEXTVIEW_INSERT(_(" (Shortcut key: 't')"));
+       TEXTVIEW_INSERT(_(" (Shortcut key: '"));
+       shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/View/Part/AsText");
+       TEXTVIEW_INSERT(shortcut);
+       g_free(shortcut);
+       TEXTVIEW_INSERT("')");
 #endif
        TEXTVIEW_INSERT("\n");
 
@@ -922,13 +1002,21 @@ void textview_show_mime_part(TextView *textview, MimeInfo *partinfo)
        TEXTVIEW_INSERT_LINK(_("'Open'"), "sc://open", NULL);
 
 #ifndef GENERIC_UMPC
-       TEXTVIEW_INSERT(_(" (Shortcut key: 'l')\n"));
+       TEXTVIEW_INSERT(_(" (Shortcut key: '"));
+       shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/View/Part/Open");
+       TEXTVIEW_INSERT(shortcut);
+       g_free(shortcut);
+       TEXTVIEW_INSERT("')\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')"));
+       TEXTVIEW_INSERT(_(" (Shortcut key: '"));
+       shortcut = cm_menu_item_get_shortcut(ui_manager, "Menu/View/Part/OpenWith");
+       TEXTVIEW_INSERT(shortcut);
+       g_free(shortcut);
+       TEXTVIEW_INSERT("')");
 #endif
 #endif
        TEXTVIEW_INSERT("\n");
@@ -968,6 +1056,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
        procmime_force_encoding(textview->messageview->forced_encoding);
        
        textview->is_in_signature = FALSE;
+       textview->is_diff = FALSE;
 
        procmime_decode_content(mimeinfo);
 
@@ -1057,6 +1146,10 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
 #endif
        } else {
 textview_default:
+               if (!g_ascii_strcasecmp(mimeinfo->subtype, "x-patch")
+                               || !g_ascii_strcasecmp(mimeinfo->subtype, "x-diff"))
+                       textview->is_diff = TRUE;
+
                if (mimeinfo->content == MIMECONTENT_MEM)
                        tmpfp = str_open_as_stream(mimeinfo->data.mem);
                else
@@ -1257,7 +1350,7 @@ static void textview_make_clickable_parts(TextView *textview,
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
        /* parse for clickable parts, and build a list of begin and end positions  */
-       for (walk = mybuf, n = 0;;) {
+       for (walk = mybuf;;) {
                gint last_index = PARSE_ELEMS;
                gchar *scanpos = NULL;
 
@@ -1377,7 +1470,7 @@ static void textview_make_clickable_parts_later(TextView *textview,
        offset = gtk_text_iter_get_offset(&start_iter);
 
        /* parse for clickable parts, and build a list of begin and end positions  */
-       for (walk = mybuf, n = 0;;) {
+       for (walk = mybuf;;) {
                gint last_index = PARSE_ELEMS;
                gchar *scanpos = NULL;
 
@@ -1498,10 +1591,25 @@ static void textview_write_line(TextView *textview, const gchar *str,
                fg_color = quote_tag_str;
        }
 
-       if (prefs_common.enable_color && 
-           (strcmp(buf,"-- \n") == 0 || strcmp(buf, "- -- \n") == 0 || textview->is_in_signature)) {
-               fg_color = "signature";
-               textview->is_in_signature = TRUE;
+       if (prefs_common.enable_color) {
+               if (textview->is_diff) {
+                       if (strncmp(buf, "+++ ", 4) == 0)
+                               fg_color = "diff-add-file";
+                       else if (buf[0] == '+')
+                               fg_color = "diff-add";
+                       else if (strncmp(buf, "--- ", 4) == 0)
+                               fg_color = "diff-del-file";
+                       else if (buf[0] == '-')
+                               fg_color = "diff-del";
+                       else if (strncmp(buf, "@@ ", 3) == 0 &&
+                                       strcmp(buf+strlen(buf)-4, " @@\n") == 0)
+                               fg_color = "diff-hunk";
+               } else if (strcmp(buf,"-- \n") == 0
+                               || strcmp(buf, "- -- \n") == 0
+                               || textview->is_in_signature) {
+                       fg_color = "signature";
+                       textview->is_in_signature = TRUE;
+               }
        }
 
        if (real_quotelevel > -1 && do_quote_folding) {
@@ -1518,6 +1626,7 @@ do_quote:
                        uri = g_new0(ClickableText, 1);
                        uri->uri = g_strdup("");
                        uri->data = g_strdup(buf);
+                       uri->data_len = strlen(uri->data);
                        uri->start = gtk_text_iter_get_offset(&iter);
                        uri->is_quote = TRUE;
                        uri->quote_level = real_quotelevel;
@@ -1550,11 +1659,12 @@ do_quote:
                                        textview->prev_quote_level = -1;
                                        goto do_quote;
                                }
-                               e_len = lasturi->data ? strlen(lasturi->data):0;
+                               e_len = lasturi->data ? lasturi->data_len: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';
+                               lasturi->data_len += n_len;
                        }
                }
        } else {
@@ -1566,7 +1676,6 @@ do_quote:
 void textview_write_link(TextView *textview, const gchar *str,
                         const gchar *uri, CodeConverter *conv)
 {
-       GdkColor *link_color = NULL;
        GtkTextView *text;
        GtkTextBuffer *buffer;
        GtkTextIter iter;
@@ -1607,9 +1716,6 @@ void textview_write_link(TextView *textview, const gchar *str,
        if (bufp > buf)
                gtk_text_buffer_insert(buffer, &iter, buf, bufp - buf);
 
-       if (prefs_common.enable_color) {
-               link_color = &uri_color;
-       }
        r_uri = g_new0(ClickableText, 1);
        r_uri->uri = g_strdup(uri);
        r_uri->start = gtk_text_iter_get_offset(&iter);
@@ -1645,6 +1751,7 @@ void textview_clear(TextView *textview)
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        textview->image = NULL;
+       textview->avatar_type = 0;
 
        if (textview->messageview->mainwin->cursor_count == 0) {
                textview_set_cursor(window, text_cursor);
@@ -1852,26 +1959,32 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        return sorted_headers;
 }
 
-static void textview_show_face(TextView *textview)
+static void textview_show_avatar(TextView *textview)
 {
        GtkAllocation allocation;
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        MsgInfo *msginfo = textview->messageview->msginfo;
        int x = 0;
+       AvatarRender *avatarr;
        
-       if (prefs_common.display_header_pane
-       ||  !prefs_common.display_xface)
+       if (prefs_common.display_header_pane || !prefs_common.display_xface)
                goto bail;
        
-       if (!msginfo->extradata || !msginfo->extradata->face) {
+       avatarr = avatars_avatarrender_new(msginfo);
+       hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
+
+       if (!avatarr->image) {
+               avatars_avatarrender_free(avatarr);
                goto bail;
        }
 
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        
-       textview->image = face_get_from_header(msginfo->extradata->face);
-       cm_return_if_fail(textview->image != NULL);
+       textview->image = avatarr->image;
+       textview->avatar_type = avatarr->type;
+       avatarr->image = NULL; /* avoid destroying */
+       avatars_avatarrender_free(avatarr);
 
        gtk_widget_show(textview->image);
        
@@ -1882,13 +1995,13 @@ static void textview_show_face(TextView *textview)
                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; 
+       textview->avatar_type = 0;
 }
 
 void textview_show_icon(TextView *textview, const gchar *stock_id)
@@ -1917,65 +2030,18 @@ void textview_show_icon(TextView *textview, const gchar *stock_id)
        return;
 }
 
-#if HAVE_LIBCOMPFACE
-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;
-       
-       if (!msginfo || !msginfo->extradata)
-               goto bail;
-
-       if (msginfo->extradata->face)
-               return;
-       
-       if (!msginfo->extradata->xface || strlen(msginfo->extradata->xface) < 5) {
-               goto bail;
-       }
-
-       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,
-                               window);
-       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;
-       
-}
-#endif
-
 static void textview_save_contact_pic(TextView *textview)
 {
+#ifndef USE_NEW_ADDRBOOK
        MsgInfo *msginfo = textview->messageview->msginfo;
        gchar *filename = NULL;
        GError *error = NULL;
        GdkPixbuf *picture = NULL;
-                               
-       if (!msginfo->extradata || (!msginfo->extradata->face && !msginfo->extradata->xface))
+
+       if (!msginfo->extradata || !msginfo->extradata->avatars)
+               return;
+
+       if (textview->avatar_type > AVATAR_FACE)
                return;
 
        if (textview->image) 
@@ -1993,11 +2059,14 @@ static void textview_save_contact_pic(TextView *textview)
                }
        }
        g_free(filename);
+#else
+       /* new address book */
+#endif
 }
 
 static void textview_show_contact_pic(TextView *textview)
 {
-       GtkAllocation allocation;
+#ifndef USE_NEW_ADDRBOOK
        MsgInfo *msginfo = textview->messageview->msginfo;
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        int x = 0;
@@ -2005,12 +2074,13 @@ static void textview_show_contact_pic(TextView *textview)
        GError *error = NULL;
        GdkPixbuf *picture = NULL;
        gint w, h;
-                               
+       GtkAllocation allocation;
+
        if (prefs_common.display_header_pane
-       ||  !prefs_common.display_xface)
+               || !prefs_common.display_xface)
                goto bail;
        
-       if (msginfo->extradata && (msginfo->extradata->face || msginfo->extradata->xface))
+       if (msginfo->extradata && msginfo->extradata->avatars)
                return;
 
        if (textview->image) 
@@ -2062,7 +2132,10 @@ bail:
        if (textview->image) 
                gtk_widget_destroy(textview->image);
        textview->image = NULL;
-       
+       textview->avatar_type = 0;
+#else
+       /* new address book */
+#endif 
 }
 
 static gint textview_tag_cmp_list(gconstpointer a, gconstpointer b)
@@ -2215,10 +2288,8 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                                                         "header", NULL);
        }
        
-       textview_show_face(textview);
-#if HAVE_LIBCOMPFACE
-       textview_show_xface(textview);
-#endif
+       textview_show_avatar(textview);
+
        textview_save_contact_pic(textview);
        textview_show_contact_pic(textview);
 }
@@ -2324,19 +2395,6 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
                if (summaryview)
                        summary_pass_key_press_event(summaryview, event);
                break;
-       case GDK_KEY_y:
-       case GDK_KEY_t:
-       case GDK_KEY_l:
-       case GDK_KEY_o:
-       case GDK_KEY_c:
-       case GDK_KEY_a:
-               if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
-                       KEY_PRESS_EVENT_STOP();
-                       mimeview_pass_key_press_event(messageview->mimeview,
-                                                     event);
-                       break;
-               }
-               /* possible fall through */
        default:
                window = gtk_widget_get_window(messageview->mainwin->window);
                if (summaryview &&
@@ -2958,6 +3016,8 @@ static void save_file_cb (GtkAction *action, TextView *textview)
        gchar *filepath = NULL;
        gchar *filedir = NULL;
        gchar *tmp_filename = NULL;
+       GtkWidget *window;
+
        if (uri == NULL)
                return;
 
@@ -2982,6 +3042,14 @@ static void save_file_cb (GtkAction *action, TextView *textview)
 
        g_free(filename);
 
+       /* Pick correct window to set the file dialog "transient for" */
+       if (textview->messageview->window != NULL)
+               window = textview->messageview->window;
+       else
+               window = textview->messageview->mainwin->window;
+
+       manage_window_focus_in(window, NULL, NULL);
+
        filename = filesel_select_file_save(_("Save as"), filepath);
        if (!filename) {
                g_free(filepath);
@@ -3043,9 +3111,10 @@ 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;
+       AvatarRender *avatarr = NULL;
        GdkPixbuf *picture = NULL;
        gboolean use_picture = FALSE;
+
        if (uri == NULL)
                return;
 
@@ -3059,26 +3128,25 @@ static void add_uri_to_addrbook_cb (GtkAction *action, TextView *textview)
        fromname = procheader_get_fromname(fromaddress);
        extract_address(fromaddress);
 
-       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);   
+       if (use_picture) {
+               avatarr = avatars_avatarrender_new(textview->messageview->msginfo);
+               hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
+       }
+
+       if (avatarr && avatarr->image) {
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
+       }
+       if (avatarr) {
+               avatars_avatarrender_free(avatarr);
        }
-#endif
-       if (image)
-               picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
 
+#ifndef USE_NEW_ADDRBOOK
        addressbook_add_contact( fromname, fromaddress, NULL, picture);
+#else
+       if (addressadd_selection(fromname, fromaddress, NULL, picture)) {
+               debug_print( "addressbook_add_contact - added\n" );
+       }
+#endif
 
        g_free(fromaddress);
        g_free(fromname);