Use new internal plugin for rendering avatars
authorRicardo Mones <ricardo@mones.org>
Sun, 16 Feb 2014 18:21:54 +0000 (19:21 +0100)
committerRicardo Mones <ricardo@mones.org>
Fri, 21 Feb 2014 18:58:30 +0000 (19:58 +0100)
Updates and simplifies rendering logic in header pane,
message view and text view and when adding pictures to
address book.

src/headerview.c
src/messageview.c
src/summaryview.c
src/textview.c

index 2d7074b..7880d25 100644 (file)
 #include <string.h>
 #include <time.h>
 
-#if HAVE_LIBCOMPFACE
-#  include <compface.h>
-#endif
-
 #include <gdk-pixbuf/gdk-pixbuf.h>
 
 #include "headerview.h"
 #include "base64.h"
 #include "headers.h"
 #include "addrindex.h"
+#include "hooks.h"
+#include "avatars.h"
 
-#if HAVE_LIBCOMPFACE
-static gint headerview_show_xface      (HeaderView     *headerview,
-                                        MsgInfo        *msginfo);
-#endif
-
-static gint headerview_show_face       (HeaderView     *headerview,
+static gint headerview_show_avatar     (HeaderView     *headerview,
                                         MsgInfo        *msginfo);
 static gint headerview_show_contact_pic        (HeaderView     *headerview,
                                         MsgInfo        *msginfo);
@@ -221,91 +214,50 @@ void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
                gtk_widget_show(headerview->tags_body_label);
                g_free(tags);
        }
-       if (!headerview_show_face(headerview, msginfo))
-               return;
-
-#if HAVE_LIBCOMPFACE
-       if (!headerview_show_xface(headerview, msginfo))
+       if (!headerview_show_avatar(headerview, msginfo))
                return;
-#endif
 
        if (!headerview_show_contact_pic(headerview, msginfo))
                return;
 
 }
 
-#if HAVE_LIBCOMPFACE
-static gint headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
+static gint headerview_show_avatar (HeaderView *headerview, MsgInfo *msginfo)
 {
+       AvatarRender *avatarr = avatars_avatarrender_new(msginfo);
        GtkWidget *hbox = headerview->hbox;
        GtkWidget *image;
-       gchar *xface = procmsg_msginfo_get_avatar(msginfo, AVATAR_XFACE);
 
-       if (!msginfo->extradata || !xface || strlen(xface) < 5) {
-               if (headerview->image &&
-                   gtk_widget_get_visible(headerview->image)) {
+       hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
+
+       if (!avatarr->image) {
+               if (headerview->image
+                               && gtk_widget_get_visible(headerview->image)) {
                        gtk_widget_hide(headerview->image);
                        gtk_widget_queue_resize(hbox);
                }
+               avatars_avatarrender_free(avatarr);
                return -1;
        }
-       if (!gtk_widget_get_visible(headerview->hbox)) return -1;
-
-       if (headerview->image) {
-               gtk_widget_destroy(headerview->image);
-               headerview->image = NULL;
-       }
-
-       image = xface_get_from_header(xface);
-
-       if (image) {
-               gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
-               gtk_widget_show(image);
-       }
-
-       headerview->image = image;
-       if (image) {
-               headerview_save_contact_pic(headerview, msginfo);
-       }
-       return 0;
-}
-#endif
-
-static gint headerview_show_face (HeaderView *headerview, MsgInfo *msginfo)
-{
-       GtkWidget *hbox = headerview->hbox;
-       GtkWidget *image;
-       gchar *face = procmsg_msginfo_get_avatar(msginfo, AVATAR_FACE);
-
-       if (!msginfo->extradata || !face) {
-               if (headerview->image &&
-                   gtk_widget_get_visible(headerview->image)) {
-                       gtk_widget_hide(headerview->image);
-                       gtk_widget_queue_resize(hbox);
-               }
+       if (!gtk_widget_get_visible(hbox)) {
+               avatars_avatarrender_free(avatarr);
                return -1;
        }
-       if (!gtk_widget_get_visible(headerview->hbox)) return -1;
 
        if (headerview->image) {
                gtk_widget_destroy(headerview->image);
                headerview->image = NULL;
        }
 
-       image = face_get_from_header(face);
+       gtk_box_pack_start(GTK_BOX(hbox), avatarr->image, FALSE, FALSE, 0);
+       gtk_widget_show(avatarr->image);
 
-       if (image) {
-               gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
-               gtk_widget_show(image);
-       }
+       headerview->image = avatarr->image;
+       avatarr->image = NULL; /* avoid destroying */
+       avatars_avatarrender_free(avatarr);
 
-       headerview->image = image;
-       if (image == NULL)
-               return -1;
-       else {
-               headerview_save_contact_pic(headerview, msginfo);
-               return 0;
-       }
+       headerview_save_contact_pic(headerview, msginfo);
+       return 0;
 }
 
 static void headerview_save_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
index 1f1f094..bfab482 100644 (file)
@@ -67,6 +67,7 @@
 #include "version.h"
 #include "statusbar.h"
 #include "folder_item_prefs.h"
+#include "avatars.h"
 #ifndef USE_NEW_ADDRBOOK
        #include "addressbook.h"
 #else
@@ -2823,9 +2824,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,21 +2836,14 @@ 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
+
+       avatarr = avatars_avatarrender_new(full_msginfo);
+       hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
+
        procmsg_msginfo_free(full_msginfo);
-       if (image)
-               picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
+
+       if (avatarr->image != NULL)
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
 
 #ifndef USE_NEW_ADDRBOOK
        addressbook_add_contact(msginfo->fromname, from, NULL, picture);
@@ -2859,8 +2852,7 @@ static void add_address_cb(GtkAction *action, gpointer data)
                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)
index 3bd2ac0..8da3900 100644 (file)
@@ -81,6 +81,7 @@
 #include "edittags.h"
 #include "manual.h"
 #include "manage_window.h"
+#include "avatars.h"
 
 #define SUMMARY_COL_MARK_WIDTH         10
 #define SUMMARY_COL_STATUS_WIDTH       13
@@ -4682,9 +4683,8 @@ void summary_add_address(SummaryView *summaryview)
 {
        MsgInfo *msginfo, *full_msginfo;
        gchar *from;
-       GtkWidget *image = NULL;
        GdkPixbuf *picture = NULL;
-       gchar *face;
+       AvatarRender *avatarr;
 
        msginfo = gtk_cmctree_node_get_row_data(GTK_CMCTREE(summaryview->ctree),
                                              summaryview->selected);
@@ -4696,21 +4696,14 @@ void summary_add_address(SummaryView *summaryview)
        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
+
+       avatarr = avatars_avatarrender_new(full_msginfo);
+       hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
+
        procmsg_msginfo_free(full_msginfo);
-       if (image)
-               picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
+
+       if (avatarr->image)
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
 
 #ifndef USE_NEW_ADDRBOOK
        addressbook_add_contact(msginfo->fromname, from, NULL, picture);
@@ -4719,8 +4712,7 @@ void summary_add_address(SummaryView *summaryview)
                debug_print( "addressbook_add_contact - added\n" );
        }
 #endif
-       if (image)
-               gtk_widget_destroy(image);
+       avatars_avatarrender_free(avatarr);
 }
 
 void summary_select_all(SummaryView *summaryview)
index 5575bfd..5106d45 100644 (file)
@@ -37,9 +37,6 @@
 #if HAVE_SYS_WAIT_H
 #include <sys/wait.h>
 #endif
-#if HAVE_LIBCOMPFACE
-#  include <compface.h>
-#endif
 
 #include "main.h"
 #include "summaryview.h"
@@ -72,6 +69,7 @@
 #include "tags.h"
 #include "manage_window.h"
 #include "folder_item_prefs.h"
+#include "avatars.h"
 
 static GdkColor quote_colors[3] = {
        {(gulong)0, (gushort)0, (gushort)0, (gushort)0},
@@ -1965,26 +1963,31 @@ 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;
-       gchar *face;
+       AvatarRender *avatarr;
        
        if (prefs_common.display_header_pane || !prefs_common.display_xface)
                goto bail;
        
-       face = procmsg_msginfo_get_avatar(msginfo, AVATAR_FACE);
-       if (!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(face);
-       cm_return_if_fail(textview->image != NULL);
+       textview->image = avatarr->image;
+       avatarr->image = NULL; /* avoid destroying */
+       avatars_avatarrender_free(avatarr);
 
        gtk_widget_show(textview->image);
        
@@ -1995,7 +1998,6 @@ static void textview_show_face(TextView *textview)
                GTK_TEXT_WINDOW_TEXT, x, 5);
 
        gtk_widget_show_all(textview->text);
-       
 
        return;
 bail:
@@ -2030,58 +2032,6 @@ void textview_show_icon(TextView *textview, const gchar *stock_id)
        return;
 }
 
-#if HAVE_LIBCOMPFACE
-static void textview_show_xface(TextView *textview)
-{
-       GtkAllocation allocation;
-       MsgInfo *msginfo = textview->messageview->msginfo;
-       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
-       int x = 0;
-       GdkWindow *window = NULL;
-       gchar *face, *xface;
-       
-       if (prefs_common.display_header_pane || !prefs_common.display_xface)
-               goto bail;
-       
-       if (!msginfo || !msginfo->extradata || !msginfo->extradata->avatars)
-               goto bail;
-
-       face = procmsg_msginfo_get_avatar(msginfo, AVATAR_FACE);
-       if (face)
-               return;
-       
-       xface = procmsg_msginfo_get_avatar(msginfo, AVATAR_XFACE);
-       if (!xface || strlen(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(xface);
-       cm_return_if_fail(textview->image != NULL);
-
-       gtk_widget_show(textview->image);
-       
-       gtk_widget_get_allocation(textview->text, &allocation);
-       x = 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
@@ -2089,14 +2039,8 @@ static void textview_save_contact_pic(TextView *textview)
        gchar *filename = NULL;
        GError *error = NULL;
        GdkPixbuf *picture = NULL;
-       gchar *face, *xface;
-                               
-       if (!msginfo->extradata || !msginfo->extradata->avatars)
-               return;
 
-       face = procmsg_msginfo_get_avatar(msginfo, AVATAR_FACE);
-        xface = procmsg_msginfo_get_avatar(msginfo, AVATAR_XFACE);
-       if (!face && !xface)
+       if (!msginfo->extradata || !msginfo->extradata->avatars)
                return;
 
        if (textview->image) 
@@ -2130,15 +2074,12 @@ static void textview_show_contact_pic(TextView *textview)
        GdkPixbuf *picture = NULL;
        gint w, h;
        GtkAllocation allocation;
-       gchar *face, *xface;
-                               
+
        if (prefs_common.display_header_pane
                || !prefs_common.display_xface)
                goto bail;
        
-       face = procmsg_msginfo_get_avatar(msginfo, AVATAR_FACE);
-       xface = procmsg_msginfo_get_avatar(msginfo, AVATAR_XFACE);
-       if (msginfo->extradata && (face || xface)) /* FIXME extradata not needed */
+       if (msginfo->extradata && msginfo->extradata->avatars)
                return;
 
        if (textview->image) 
@@ -2345,10 +2286,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);
 }
@@ -3170,7 +3109,7 @@ 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;
 
@@ -3188,26 +3127,16 @@ static void add_uri_to_addrbook_cb (GtkAction *action, TextView *textview)
        extract_address(fromaddress);
 
        if (use_picture) {
-               gchar *face = procmsg_msginfo_get_avatar(
-                                       textview->messageview->msginfo,
-                                       AVATAR_FACE);
-               if (face) {
-                       image = face_get_from_header(face);
-               }
-#if HAVE_LIBCOMPFACE 
-               else {
-                       gchar *xface = procmsg_msginfo_get_avatar(
-                                               textview->messageview->msginfo,
-                                               AVATAR_XFACE);
-                       if (xface) {
-                               image = xface_get_from_header(xface);
-                       }
-               }
-#endif
+               avatarr = avatars_avatarrender_new(textview->messageview->msginfo);
+               hooks_invoke(AVATAR_IMAGE_RENDER_HOOKLIST, avatarr);
        }
 
-       if (image)
-               picture = gtk_image_get_pixbuf(GTK_IMAGE(image));
+       if (avatarr && avatarr->image) {
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(avatarr->image));
+       }
+       if (avatarr) {
+               avatars_avatarrender_free(avatarr);
+       }
 
 #ifndef USE_NEW_ADDRBOOK
        addressbook_add_contact( fromname, fromaddress, NULL, picture);