2006-02-05 [colin] 2.0.0cvs15
[claws.git] / src / textview.c
index c5a512baa57e3fd3170358e81b469667b6347280..a362def82dd5771f0c5cfae133579027ab6efb53 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto and the Sylpheed-Claws 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,7 @@
  *
  * You should have received a copy of the GNU General Public License
  * along with this program; if not, write to the Free Software
- * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  */
 
 #ifdef HAVE_CONFIG_H
 #include <ctype.h>
 #include <string.h>
 #include <stdlib.h>
+#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"
@@ -54,6 +61,7 @@
 #include "menu.h"
 #include "image_viewer.h"
 #include "filesel.h"
+#include "base64.h"
 
 struct _RemoteURI
 {
@@ -105,14 +113,14 @@ static GdkColor error_color = {
 static GdkCursor *hand_cursor = NULL;
 static GdkCursor *text_cursor = NULL;
 
-#define TEXTVIEW_STATUSBAR_PUSH(textview, str)                                     \
-{                                                                          \
+#define TEXTVIEW_STATUSBAR_PUSH(textview, str)                             \
+{      if (textview->messageview->statusbar)                               \
        gtk_statusbar_push(GTK_STATUSBAR(textview->messageview->statusbar), \
                           textview->messageview->statusbar_cid, str);      \
 }
 
-#define TEXTVIEW_STATUSBAR_POP(textview)                                                  \
-{                                                                         \
+#define TEXTVIEW_STATUSBAR_POP(textview)                                  \
+{      if (textview->messageview->statusbar)                              \
        gtk_statusbar_pop(GTK_STATUSBAR(textview->messageview->statusbar), \
                          textview->messageview->statusbar_cid);           \
 }
@@ -231,6 +239,25 @@ static GtkItemFactoryEntry textview_file_popup_entries[] =
        {N_("/_Save image..."),         NULL, save_file_cb, 0, NULL},
 };
 
+static void scrolled_cb (GtkAdjustment *adj, TextView *textview)
+{
+       if (textview->image) {
+               gint x, y, x1;
+               x1 = textview->text->allocation.width - WIDTH - 5;
+               gtk_text_view_buffer_to_window_coords(
+                       GTK_TEXT_VIEW(textview->text),
+                       GTK_TEXT_WINDOW_TEXT, x1, 5, &x, &y);
+               gtk_text_view_move_child(GTK_TEXT_VIEW(textview->text), 
+                       textview->image, x1, y);
+       }
+}
+
+static void textview_size_allocate_cb  (GtkWidget      *widget,
+                                        GtkAllocation  *allocation,
+                                        gpointer        data)
+{
+       scrolled_cb(NULL, (TextView *)data);
+}
 
 TextView *textview_create(void)
 {
@@ -242,6 +269,7 @@ TextView *textview_create(void)
        GtkClipboard *clipboard;
        GtkItemFactory *link_popupfactory, *mail_popupfactory, *file_popupfactory;
        GtkWidget *link_popupmenu, *mail_popupmenu, *file_popupmenu;
+       GtkAdjustment *adj;
        gint n_entries;
 
        debug_print("Creating text view...\n");
@@ -284,6 +312,14 @@ TextView *textview_create(void)
                         G_CALLBACK(textview_leave_notify), textview);
        g_signal_connect(G_OBJECT(text), "visibility-notify-event",
                         G_CALLBACK(textview_visibility_notify), textview);
+       adj = gtk_scrolled_window_get_vadjustment(
+               GTK_SCROLLED_WINDOW(scrolledwin));
+       g_signal_connect(G_OBJECT(adj), "value-changed",
+                        G_CALLBACK(scrolled_cb), textview);
+       g_signal_connect(G_OBJECT(text), "size_allocate",
+                        G_CALLBACK(textview_size_allocate_cb),
+                        textview);
+
 
        gtk_widget_show(scrolledwin);
 
@@ -323,7 +359,7 @@ TextView *textview_create(void)
        textview->mail_popup_factory = mail_popupfactory;
        textview->file_popup_menu    = file_popupmenu;
        textview->file_popup_factory = file_popupfactory;
-
+       textview->image              = NULL;
        return textview;
 }
 
@@ -479,7 +515,6 @@ void textview_show_part(TextView *textview, MimeInfo *mimeinfo, FILE *fp)
                textview_add_parts(textview, mimeinfo);
        else
                textview_write_body(textview, mimeinfo);
-
 }
 
 static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
@@ -531,7 +566,9 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
 
        g_free(content_type);                      
 
-       if (mimeinfo->type != MIMETYPE_TEXT) {
+       if (mimeinfo->disposition == DISPOSITIONTYPE_ATTACHMENT
+       || (mimeinfo->disposition == DISPOSITIONTYPE_INLINE && 
+           mimeinfo->type != MIMETYPE_TEXT)) {
                gtk_text_buffer_insert(buffer, &iter, buf, -1);
                if (mimeinfo->type == MIMETYPE_IMAGE  &&
                    prefs_common.inline_img ) {
@@ -603,7 +640,7 @@ static void textview_add_part(TextView *textview, MimeInfo *mimeinfo)
                        g_object_unref(pixbuf);
                        g_free(filename);
                }
-       } else if (mimeinfo->disposition != DISPOSITIONTYPE_ATTACHMENT) {
+       } else if (mimeinfo->type == MIMETYPE_TEXT) {
                if (prefs_common.display_header && (charcount > 0))
                        gtk_text_buffer_insert(buffer, &iter, "\n", 1);
 
@@ -738,7 +775,7 @@ static void textview_write_body(TextView *textview, MimeInfo *mimeinfo)
                charset = textview->messageview->forced_charset;
        else
                charset = procmime_mimeinfo_get_parameter(mimeinfo, "charset");
-printf("charset %s\n", charset);
+
        textview_set_font(textview, charset);
 
        conv = conv_code_converter_new(charset);
@@ -851,7 +888,8 @@ static void textview_show_ertf(TextView *textview, FILE *fp,
 static void textview_make_clickable_parts(TextView *textview,
                                          const gchar *fg_tag,
                                          const gchar *uri_tag,
-                                         const gchar *linebuf)
+                                         const gchar *linebuf,
+                                         gboolean hdr)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
        GtkTextBuffer *buffer = gtk_text_view_get_buffer(text);
@@ -869,7 +907,8 @@ static void textview_make_clickable_parts(TextView *textview,
                gboolean  (*parse)      (const gchar *start,
                                         const gchar *scanpos,
                                         const gchar **bp_,
-                                        const gchar **ep_);
+                                        const gchar **ep_,
+                                        gboolean hdr);
                /* part to URI function */
                gchar    *(*build_uri)  (const gchar *bp,
                                         const gchar *ep);
@@ -921,7 +960,7 @@ static void textview_make_clickable_parts(TextView *textview,
 
                if (scanpos) {
                        /* check if URI can be parsed */
-                       if (parser[last_index].parse(walk, scanpos, &bp, &ep)
+                       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;
@@ -992,7 +1031,6 @@ static void textview_write_line(TextView *textview, const gchar *str,
                conv_localetodisp(buf, sizeof(buf), str);
                
        strcrchomp(buf);
-       //if (prefs_common.conv_mb_alnum) conv_mb_alnum(buf);
        fg_color = NULL;
 
        /* change color of quotation
@@ -1026,7 +1064,7 @@ static void textview_write_line(TextView *textview, const gchar *str,
                textview->is_in_signature = TRUE;
        }
 
-       textview_make_clickable_parts(textview, fg_color, "link", buf);
+       textview_make_clickable_parts(textview, fg_color, "link", buf, FALSE);
 }
 
 void textview_write_link(TextView *textview, const gchar *str,
@@ -1096,6 +1134,9 @@ void textview_clear(TextView *textview)
        textview->uri_list = NULL;
 
        textview->body_pos = 0;
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       textview->image = NULL;
 }
 
 void textview_destroy(TextView *textview)
@@ -1257,6 +1298,142 @@ static GPtrArray *textview_scan_header(TextView *textview, FILE *fp)
        return sorted_headers;
 }
 
+static void textview_show_face(TextView *textview)
+{
+       gchar face[2048];
+       gchar face_png[2048];
+       gint pngsize;
+       GdkPixbuf *pixbuf;
+       GError *error = NULL;
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       int x = 0;
+       
+       if (prefs_common.display_header_pane)
+               goto bail;
+
+       if (!msginfo->face) {
+               goto bail;
+       }
+
+       strncpy2(face, msginfo->face, sizeof(face));
+
+       unfold_line(face); /* strip all whitespace and linebreaks */
+       remove_space(face);
+
+       pngsize = base64_decode(face_png, face, strlen(face));
+
+       GdkPixbufLoader *loader = gdk_pixbuf_loader_new ();
+       if (!gdk_pixbuf_loader_write (loader, face_png, pngsize, &error) ||
+           !gdk_pixbuf_loader_close (loader, &error)) {
+               g_warning("loading face failed\n");
+               g_object_unref(loader);
+               goto bail;
+       }
+
+       pixbuf = g_object_ref(gdk_pixbuf_loader_get_pixbuf(loader));
+
+       g_object_unref(loader);
+
+       if ((gdk_pixbuf_get_width(pixbuf) != 48) || (gdk_pixbuf_get_height(pixbuf) != 48)) {
+               g_object_unref(pixbuf);
+               g_warning("wrong_size");
+               goto bail;
+       }
+
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       
+       textview->image = gtk_image_new_from_pixbuf(pixbuf);
+       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);
+
+       g_object_unref(pixbuf);
+       
+       gtk_widget_show_all(textview->text);
+       
+
+       return;
+bail:
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       textview->image = NULL; 
+}
+
+#if HAVE_LIBCOMPFACE
+static void textview_show_xface(TextView *textview)
+{
+       gchar xface[2048];
+       MsgInfo *msginfo = textview->messageview->msginfo;
+       static gchar *xpm_xface[XPM_XFACE_HEIGHT];
+       static gboolean xpm_xface_init = TRUE;
+       GdkPixmap *pixmap;
+       GdkBitmap *mask;
+       GtkTextView *text = GTK_TEXT_VIEW(textview->text);
+       int x = 0;
+       if (prefs_common.display_header_pane
+       ||  !prefs_common.display_xface)
+               goto bail;
+       
+       if (!msginfo)
+               goto bail;
+
+       if (msginfo->face)
+               return;
+       
+       if (!msginfo->xface || strlen(msginfo->xface) < 5) {
+               goto bail;
+       }
+
+       strncpy(xface, msginfo->xface, sizeof(xface));
+
+       if (uncompface(xface) < 0) {
+               g_warning("uncompface failed\n");
+               goto bail;
+       }
+
+       if (xpm_xface_init) {
+               gint i;
+
+               for (i = 0; i < XPM_XFACE_HEIGHT; i++) {
+                       xpm_xface[i] = g_malloc(WIDTH + 1);
+                       *xpm_xface[i] = '\0';
+               }
+               xpm_xface_init = FALSE;
+       }
+
+       create_xpm_from_xface(xpm_xface, xface);
+
+       pixmap = gdk_pixmap_create_from_xpm_d
+               (textview->text->window, &mask, 
+                &textview->text->style->white, xpm_xface);
+       
+       if (textview->image) 
+               gtk_widget_destroy(textview->image);
+       
+       textview->image = gtk_image_new_from_pixmap(pixmap, mask);
+       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_show_header(TextView *textview, GPtrArray *headers)
 {
        GtkTextView *text = GTK_TEXT_VIEW(textview->text);
@@ -1295,13 +1472,24 @@ static void textview_show_header(TextView *textview, GPtrArray *headers)
                                (buffer, &iter, header->body, -1,
                                 "header", "emphasis", NULL);
                } else {
-                       textview_make_clickable_parts(textview, "header", "link",
-                                                     header->body);
+                       gboolean hdr = 
+                         procheader_headername_equal(header->name, "From") ||
+                         procheader_headername_equal(header->name, "To") ||
+                         procheader_headername_equal(header->name, "Cc") ||
+                         procheader_headername_equal(header->name, "Bcc");
+                       textview_make_clickable_parts(textview, "header", 
+                                                     "link", header->body, 
+                                                     hdr);
                }
                gtk_text_buffer_get_end_iter (buffer, &iter);
                gtk_text_buffer_insert_with_tags_by_name(buffer, &iter, "\n", 1,
                                                         "header", NULL);
        }
+       
+       textview_show_face(textview);
+#if HAVE_LIBCOMPFACE
+       textview_show_xface(textview);
+#endif
 }
 
 gboolean textview_search_string(TextView *textview, const gchar *str,
@@ -1590,6 +1778,7 @@ static gint textview_key_pressed(GtkWidget *widget, GdkEventKey *event,
        case GDK_y:
        case GDK_t:
        case GDK_l:
+       case GDK_c:
                if ((event->state & (GDK_MOD1_MASK|GDK_CONTROL_MASK)) == 0) {
                        KEY_PRESS_EVENT_STOP();
                        mimeview_pass_key_press_event(messageview->mimeview,
@@ -1735,24 +1924,7 @@ static gboolean textview_get_uri_range(TextView *textview,
                                       GtkTextIter *start_iter,
                                       GtkTextIter *end_iter)
 {
-       GtkTextIter _start_iter, _end_iter;
-
-       _end_iter = *iter;
-       if (!gtk_text_iter_forward_to_tag_toggle(&_end_iter, tag)) {
-               debug_print("Can't find end");
-               return FALSE;
-       }
-
-       _start_iter = _end_iter;
-       if (!gtk_text_iter_backward_to_tag_toggle(&_start_iter, tag)) {
-               debug_print("Can't find start.");
-               return FALSE;
-       }
-
-       *start_iter = _start_iter;
-       *end_iter = _end_iter;
-
-       return TRUE;
+       return get_tag_range(iter, tag, start_iter, end_iter);
 }
 
 static RemoteURI *textview_get_uri_from_range(TextView *textview,
@@ -1934,10 +2106,10 @@ static gboolean textview_uri_security_check(TextView *textview, RemoteURI *uri)
                                        "Open it anyway?"),
                                      uri->uri, visible_str);
                aval = alertpanel_full(_("Fake URL warning"), msg,
-                                      GTK_STOCK_YES, GTK_STOCK_NO, NULL, FALSE,
-                                      NULL, ALERT_WARNING, G_ALERTALTERNATE);
+                                      GTK_STOCK_CANCEL, _("_Open URL"), NULL, FALSE,
+                                      NULL, ALERT_WARNING, G_ALERTDEFAULT);
                g_free(msg);
-               if (aval == G_ALERTDEFAULT)
+               if (aval == G_ALERTALTERNATE)
                        retval = TRUE;
        }
 
@@ -2075,10 +2247,10 @@ static void save_file_cb (TextView *textview, guint action, void *data)
                
                res = g_strdup_printf(_("Overwrite existing file '%s'?"),
                                      filename);
-               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_OK
-                                 GTK_STOCK_CANCEL, NULL);
+               aval = alertpanel(_("Overwrite"), res, GTK_STOCK_CANCEL
+                                 GTK_STOCK_OK, NULL);
                g_free(res);                                      
-               if (G_ALERTDEFAULT != aval) 
+               if (G_ALERTALTERNATE != aval)
                        return;
        }