fix implicit declaration warning
[claws.git] / src / textview.c
index 8dd88635a6b6094703acd4c5abab384d0546564c..ef50dd8f0350085b51d8c594bb5b432fecdeb6b7 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 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
 #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"
@@ -76,6 +69,8 @@
 #include "tags.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},
@@ -1311,8 +1306,7 @@ static void textview_make_clickable_parts(TextView *textview,
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
        GtkTextIter iter;
-       gint mybuf_len = strlen(linebuf);
-       gchar *mybuf = g_strndup(linebuf, mybuf_len);
+       gchar *mybuf = g_strdup(linebuf);
        
        /* parse table - in order of priority */
        struct table {
@@ -1320,7 +1314,6 @@ static void textview_make_clickable_parts(TextView *textview,
 
                /* token search function */
                gchar    *(*search)     (const gchar *haystack,
-                                        gint  haystack_len,
                                         const gchar *needle);
                /* part parsing function */
                gboolean  (*parse)      (const gchar *start,
@@ -1334,14 +1327,14 @@ static void textview_make_clickable_parts(TextView *textview,
        };
 
        static struct table parser[] = {
-               {"http://",  strncasestr, get_uri_part,   make_uri_string},
-               {"https://", strncasestr, get_uri_part,   make_uri_string},
-               {"ftp://",   strncasestr, get_uri_part,   make_uri_string},
-               {"sftp://",  strncasestr, get_uri_part,   make_uri_string},
-               {"gopher://",strncasestr, get_uri_part,   make_uri_string},
-               {"www.",     strncasestr, get_uri_part,   make_http_string},
-               {"mailto:",  strncasestr, get_uri_part,   make_uri_string},
-               {"@",        strncasestr, get_email_part, make_email_string}
+               {"http://",  strcasestr, get_uri_part,   make_uri_string},
+               {"https://", strcasestr, get_uri_part,   make_uri_string},
+               {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
+               {"sftp://",  strcasestr, get_uri_part,   make_uri_string},
+               {"gopher://",strcasestr, get_uri_part,   make_uri_string},
+               {"www.",     strcasestr, get_uri_part,   make_http_string},
+               {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
+               {"@",        strcasestr, get_email_part, make_email_string}
        };
        const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
 
@@ -1356,33 +1349,41 @@ static void textview_make_clickable_parts(TextView *textview,
 
        if (!g_utf8_validate(linebuf, -1, NULL)) {
                g_free(mybuf);
-               mybuf = g_malloc(mybuf_len*2 +1);
-               conv_localetodisp(mybuf, mybuf_len*2 +1, linebuf);
-               mybuf_len = strlen(mybuf);
+               mybuf = g_malloc(strlen(linebuf)*2 +1);
+               conv_localetodisp(mybuf, strlen(linebuf)*2 +1, linebuf);
        }
 
        gtk_text_buffer_get_end_iter(buffer, &iter);
 
        /* parse for clickable parts, and build a list of begin and end positions  */
-       for (n = 0; n < PARSE_ELEMS; n++) {
-               gint len = mybuf_len;
-               gint needle_len = strlen(parser[n].needle);
-               for (walk = mybuf;;) {
-                       gchar *scanpos = parser[n].search(walk, len, parser[n].needle);
-                       if (scanpos) {
-                               /* check if URI can be parsed */
-                               if (parser[n].parse(walk, scanpos, &bp, &ep, hdr)
-                                               && (size_t) (ep - bp - 1) > needle_len) {
-                                       ADD_TXT_POS(bp, ep, n);
-                                       len -= ep - walk;
-                                       walk = ep;
-                               } else {
-                                       len -= (scanpos + needle_len) - walk;
-                                       walk = scanpos + needle_len;
+       for (walk = mybuf;;) {
+               gint last_index = PARSE_ELEMS;
+               gchar *scanpos = NULL;
+
+               /* FIXME: this looks phony. scanning for anything in the parse table */
+               for (n = 0; n < PARSE_ELEMS; n++) {
+                       gchar *tmp;
+
+                       tmp = parser[n].search(walk, parser[n].needle);
+                       if (tmp) {
+                               if (scanpos == NULL || tmp < scanpos) {
+                                       scanpos = tmp;
+                                       last_index = n;
                                }
-                       } else
-                               break;
+                       }                                       
                }
+
+               if (scanpos) {
+                       /* check if URI can be parsed */
+                       if (parser[last_index].parse(walk, scanpos, &bp, &ep, hdr)
+                           && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
+                                       ADD_TXT_POS(bp, ep, last_index);
+                                       walk = ep;
+                       } else
+                               walk = scanpos +
+                                       strlen(parser[last_index].needle);
+               } else
+                       break;
        }
 
        /* colorize this line */
@@ -1430,7 +1431,6 @@ static void textview_make_clickable_parts_later(TextView *textview,
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
        GtkTextIter start_iter, end_iter;
        gchar *mybuf;
-       gint mybuf_len;
        gint offset = 0;
        /* parse table - in order of priority */
        struct table {
@@ -1438,7 +1438,6 @@ static void textview_make_clickable_parts_later(TextView *textview,
 
                /* token search function */
                gchar    *(*search)     (const gchar *haystack,
-                                        gint  haystack_len,
                                         const gchar *needle);
                /* part parsing function */
                gboolean  (*parse)      (const gchar *start,
@@ -1452,13 +1451,13 @@ static void textview_make_clickable_parts_later(TextView *textview,
        };
 
        static struct table parser[] = {
-               {"http://",  strncasestr, get_uri_part,   make_uri_string},
-               {"https://", strncasestr, get_uri_part,   make_uri_string},
-               {"ftp://",   strncasestr, get_uri_part,   make_uri_string},
-               {"sftp://",  strncasestr, get_uri_part,   make_uri_string},
-               {"www.",     strncasestr, get_uri_part,   make_http_string},
-               {"mailto:",  strncasestr, get_uri_part,   make_uri_string},
-               {"@",        strncasestr, get_email_part, make_email_string}
+               {"http://",  strcasestr, get_uri_part,   make_uri_string},
+               {"https://", strcasestr, get_uri_part,   make_uri_string},
+               {"ftp://",   strcasestr, get_uri_part,   make_uri_string},
+               {"sftp://",  strcasestr, get_uri_part,   make_uri_string},
+               {"www.",     strcasestr, get_uri_part,   make_http_string},
+               {"mailto:",  strcasestr, get_uri_part,   make_uri_string},
+               {"@",        strcasestr, get_email_part, make_email_string}
        };
        const gint PARSE_ELEMS = sizeof parser / sizeof parser[0];
 
@@ -1477,26 +1476,34 @@ 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  */
-       mybuf_len = strlen(mybuf);
-       for (n = 0; n < PARSE_ELEMS; n++) {
-               gint len = mybuf_len;
-               gint needle_len = strlen(parser[n].needle);
-               for (walk = mybuf;;) {
-                       gchar *scanpos = parser[n].search(walk, len, parser[n].needle);
-                       if (scanpos) {
-                               /* check if URI can be parsed */
-                               if (parser[n].parse(walk, scanpos, &bp, &ep, FALSE)
-                                               && (size_t) (ep - bp - 1) > needle_len) {
-                                       ADD_TXT_POS_LATER(bp, ep, n);
-                                       len -= ep - walk;
-                                       walk = ep;
-                               } else {
-                                       len -= (scanpos + needle_len) - walk;
-                                       walk = scanpos + needle_len;
+       for (walk = mybuf;;) {
+               gint last_index = PARSE_ELEMS;
+               gchar *scanpos = NULL;
+
+               /* FIXME: this looks phony. scanning for anything in the parse table */
+               for (n = 0; n < PARSE_ELEMS; n++) {
+                       gchar *tmp;
+
+                       tmp = parser[n].search(walk, parser[n].needle);
+                       if (tmp) {
+                               if (scanpos == NULL || tmp < scanpos) {
+                                       scanpos = tmp;
+                                       last_index = n;
                                }
-                       } else
-                               break;
+                       }                                       
                }
+
+               if (scanpos) {
+                       /* check if URI can be parsed */
+                       if (parser[last_index].parse(walk, scanpos, &bp, &ep, FALSE)
+                           && (size_t) (ep - bp - 1) > strlen(parser[last_index].needle)) {
+                                       ADD_TXT_POS_LATER(bp, ep, last_index);
+                                       walk = ep;
+                       } else
+                               walk = scanpos +
+                                       strlen(parser[last_index].needle);
+               } else
+                       break;
        }
 
        /* colorize this line */
@@ -1957,26 +1964,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;
+       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;
+       avatarr->image = NULL; /* avoid destroying */
+       avatars_avatarrender_free(avatarr);
 
        gtk_widget_show(textview->image);
        
@@ -1987,7 +1999,6 @@ static void textview_show_face(TextView *textview)
                GTK_TEXT_WINDOW_TEXT, x, 5);
 
        gtk_widget_show_all(textview->text);
-       
 
        return;
 bail:
@@ -2022,59 +2033,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;
-       
-       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);
-       
-       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
@@ -2082,8 +2040,8 @@ static void textview_save_contact_pic(TextView *textview)
        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->image) 
@@ -2116,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) 
@@ -2160,7 +2119,8 @@ static void textview_show_contact_pic(TextView *textview)
 
        gtk_widget_show(textview->image);
        
-       x = textview->text->allocation.width - WIDTH -5;
+       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);
@@ -2327,10 +2287,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);
 }
@@ -3152,7 +3110,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;
 
@@ -3169,24 +3127,17 @@ 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);