2006-02-05 [colin] 2.0.0cvs13
[claws.git] / src / headerview.c
index 47eaf3cd995989c1df0f06bbfbe7cbe1a5469690..c1c1cea408fa40e3a0994dd0a57ebec46d3887d2 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999,2000 Hiroyuki Yamamoto
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto
  *
  * 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 "defs.h"
 
 #include <glib.h>
+#include <glib/gi18n.h>
 #include <gtk/gtkwidget.h>
 #include <gtk/gtkstyle.h>
 #include <gtk/gtkscrolledwindow.h>
 #include <gtk/gtkhbox.h>
 #include <gtk/gtkvbox.h>
 #include <gtk/gtklabel.h>
-#include <gtk/gtkpixmap.h>
+#include <gtk/gtkimage.h>
 #include <stdio.h>
 #include <string.h>
 #include <time.h>
 #  include <compface.h>
 #endif
 
-#include "intl.h"
-#include "main.h"
+#include <gdk-pixbuf/gdk-pixbuf.h>
+
 #include "headerview.h"
 #include "prefs_common.h"
+#include "codeconv.h"
 #include "gtkutils.h"
 #include "utils.h"
-
-static GdkFont *boldfont;
+#include "base64.h"
 
 #define TR(str)        (prefs_common.trans_hdr ? gettext(str) : str)
 
@@ -64,10 +65,11 @@ static gchar *xpm_xface[XPM_XFACE_HEIGHT];
 
 static void headerview_show_xface      (HeaderView     *headerview,
                                         MsgInfo        *msginfo);
-static gint create_xpm_from_xface      (gchar          *xpm[],
-                                        const gchar    *xface);
 #endif
 
+static gint headerview_show_face       (HeaderView     *headerview,
+                                        MsgInfo        *msginfo);
+
 HeaderView *headerview_create(void)
 {
        HeaderView *headerview;
@@ -84,12 +86,12 @@ HeaderView *headerview_create(void)
        GtkWidget *subject_header_label;
        GtkWidget *subject_body_label;
 
-       debug_print(_("Creating header view...\n"));
+       debug_print("Creating header view...\n");
        headerview = g_new0(HeaderView, 1);
 
        hbox = gtk_hbox_new(FALSE, 0);
        gtk_container_set_border_width(GTK_CONTAINER(hbox), 2);
-       vbox = gtk_vbox_new(FALSE, 0);
+       vbox = gtk_vbox_new(FALSE, 2);
        gtk_box_pack_start(GTK_BOX(hbox), vbox, TRUE, TRUE, 0);
 
        hbox1 = gtk_hbox_new(FALSE, 4);
@@ -106,15 +108,33 @@ HeaderView *headerview_create(void)
        subject_header_label = gtk_label_new(TR("Subject:"));
        subject_body_label   = gtk_label_new("");
 
+       gtk_label_set_selectable(GTK_LABEL(from_body_label), TRUE);
+       gtk_label_set_selectable(GTK_LABEL(to_body_label), TRUE);
+       gtk_label_set_selectable(GTK_LABEL(ng_body_label), TRUE);
+       gtk_label_set_selectable(GTK_LABEL(subject_body_label), TRUE);
+
+       GTK_WIDGET_UNSET_FLAGS(from_body_label, GTK_CAN_FOCUS);
+       GTK_WIDGET_UNSET_FLAGS(to_body_label, GTK_CAN_FOCUS);
+       GTK_WIDGET_UNSET_FLAGS(ng_body_label, GTK_CAN_FOCUS);
+       GTK_WIDGET_UNSET_FLAGS(subject_body_label, GTK_CAN_FOCUS);
+
        gtk_box_pack_start(GTK_BOX(hbox1), from_header_label, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(hbox1), from_body_label, FALSE, FALSE, 0);
        gtk_box_pack_start(GTK_BOX(hbox1), to_header_label, FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox1), to_body_label, TRUE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox1), ng_header_label, FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, FALSE, FALSE, 0);
+       gtk_box_pack_start(GTK_BOX(hbox1), ng_body_label, TRUE, TRUE, 0);
        gtk_box_pack_start(GTK_BOX(hbox2), subject_header_label, FALSE, FALSE, 0);
-       gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, FALSE, FALSE, 0);
-
+       gtk_box_pack_start(GTK_BOX(hbox2), subject_body_label, TRUE, TRUE, 0);
+
+       gtk_misc_set_alignment(GTK_MISC(to_body_label), 0, 0.5);
+       gtk_misc_set_alignment(GTK_MISC(ng_body_label), 0, 0.5);
+       gtk_misc_set_alignment(GTK_MISC(subject_body_label), 0, 0.5);
+#if GTK_CHECK_VERSION(2, 6, 0)
+       gtk_label_set_ellipsize(GTK_LABEL(to_body_label), PANGO_ELLIPSIZE_END);
+       gtk_label_set_ellipsize(GTK_LABEL(ng_body_label), PANGO_ELLIPSIZE_END);
+       gtk_label_set_ellipsize(GTK_LABEL(subject_body_label), PANGO_ELLIPSIZE_END);
+#endif
        headerview->hbox = hbox;
        headerview->from_header_label    = from_header_label;
        headerview->from_body_label      = from_body_label;
@@ -131,25 +151,35 @@ HeaderView *headerview_create(void)
        return headerview;
 }
 
-void headerview_init(HeaderView *headerview)
+void headerview_set_font(HeaderView *headerview)
 {
-       if (!boldfont)
-               boldfont = gdk_fontset_load(BOLD_FONT);
-
-#define SET_FONT_STYLE(wid) \
-{ \
-       GtkStyle *style; \
- \
-       style = gtk_style_copy(gtk_widget_get_style(headerview->wid)); \
-       style->font = boldfont; \
-       gtk_widget_set_style(headerview->wid, style); \
-}
+       PangoFontDescription *boldfont = NULL;
+       PangoFontDescription *normalfont = NULL;
+       
+       if (!boldfont) {
+               normalfont = pango_font_description_from_string(NORMAL_FONT);
+               boldfont = pango_font_description_from_string(NORMAL_FONT);
+               pango_font_description_set_weight(boldfont, PANGO_WEIGHT_BOLD);
+       }
 
-       SET_FONT_STYLE(from_header_label);
-       SET_FONT_STYLE(to_header_label);
-       SET_FONT_STYLE(ng_header_label);
-       SET_FONT_STYLE(subject_header_label);
+       if (boldfont) {
+               gtk_widget_modify_font(headerview->from_header_label, boldfont);
+               gtk_widget_modify_font(headerview->to_header_label, boldfont);
+               gtk_widget_modify_font(headerview->ng_header_label, boldfont);
+               gtk_widget_modify_font(headerview->subject_header_label, boldfont);
+               pango_font_description_free(boldfont);
+
+               gtk_widget_modify_font(headerview->from_body_label, normalfont);
+               gtk_widget_modify_font(headerview->to_body_label, normalfont);
+               gtk_widget_modify_font(headerview->ng_body_label, normalfont);
+               gtk_widget_modify_font(headerview->subject_body_label, normalfont);
+               pango_font_description_free(normalfont);
+       }
+}
 
+void headerview_init(HeaderView *headerview)
+{
+       headerview_set_font(headerview);
        headerview_clear(headerview);
        headerview_set_visibility(headerview, prefs_common.display_header_pane);
 
@@ -187,6 +217,9 @@ void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
                           msginfo->subject ? msginfo->subject :
                           _("(No Subject)"));
 
+       if (!headerview_show_face(headerview, msginfo))
+               return;
+
 #if HAVE_LIBCOMPFACE
        headerview_show_xface(headerview, msginfo);
 #endif
@@ -227,21 +260,86 @@ static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
        if (!headerview->image) {
                GtkWidget *image;
 
-               image = gtk_pixmap_new(pixmap, mask);
+               image = gtk_image_new_from_pixmap(pixmap, mask);
                gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
                gtk_widget_show(image);
                headerview->image = image;
        } else {
-               gtk_pixmap_set(GTK_PIXMAP(headerview->image), pixmap, mask);
+               gtk_image_set_from_pixmap(GTK_IMAGE(headerview->image), pixmap, mask);
                gtk_widget_show(headerview->image);
        }
 
-       gdk_pixmap_unref(pixmap);
+       g_object_unref(pixmap);
 }
 #endif
 
+static gint headerview_show_face (HeaderView *headerview, MsgInfo *msginfo)
+{
+       gchar face[2048];
+       gchar face_png[2048];
+       gint pngsize;
+       GtkWidget *hbox = headerview->hbox;
+       GdkPixbuf *pixbuf;
+       GError *error = NULL;
+
+       if (!msginfo->face) {
+               if (headerview->image &&
+                   GTK_WIDGET_VISIBLE(headerview->image)) {
+                       gtk_widget_hide(headerview->image);
+                       gtk_widget_queue_resize(hbox);
+               }
+               return -1;
+       }
+       if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return -1;
+
+       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);
+               if (headerview->image)
+                       gtk_widget_hide(headerview->image);
+               return;
+       }
+
+       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);
+               return -1;
+       }
+
+       if (!headerview->image) {
+               GtkWidget *image;
+
+               image = gtk_image_new_from_pixbuf(pixbuf);
+               gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
+               gtk_widget_show(image);
+               headerview->image = image;
+       } else {
+               gtk_image_set_from_pixbuf(GTK_IMAGE(headerview->image), pixbuf);
+               gtk_widget_show(headerview->image);
+       }
+
+       g_object_unref(pixbuf);
+
+       return 0;
+}
+
 void headerview_clear(HeaderView *headerview)
 {
+       if (headerview == NULL)
+               return;
+
        gtk_label_set_text(GTK_LABEL(headerview->from_body_label), "");
        gtk_label_set_text(GTK_LABEL(headerview->to_body_label), "");
        gtk_label_set_text(GTK_LABEL(headerview->ng_body_label), "");
@@ -269,75 +367,3 @@ void headerview_destroy(HeaderView *headerview)
 {
        g_free(headerview);
 }
-
-#if HAVE_LIBCOMPFACE
-static gint create_xpm_from_xface(gchar *xpm[], const gchar *xface)
-{
-       static gchar *bit_pattern[] = {
-               "....",
-               "...#",
-               "..#.",
-               "..##",
-               ".#..",
-               ".#.#",
-               ".##.",
-               ".###",
-               "#...",
-               "#..#",
-               "#.#.",
-               "#.##",
-               "##..",
-               "##.#",
-               "###.",
-               "####"
-       };
-
-       static gchar *xface_header = "48 48 2 1";
-       static gchar *xface_black  = "# c #000000";
-       static gchar *xface_white  = ". c #ffffff";
-
-       gint i, line = 0;
-       const guchar *p;
-       gchar buf[WIDTH * 4 + 1];  /* 4 = strlen("0x0000") */
-
-       p = xface;
-
-       strcpy(xpm[line++], xface_header);
-       strcpy(xpm[line++], xface_black);
-       strcpy(xpm[line++], xface_white);
-
-       for (i = 0; i < HEIGHT; i++) {
-               gint col;
-
-               buf[0] = '\0';
-     
-               for (col = 0; col < 3; col++) {
-                       gint figure;
-
-                       p += 2;  /* skip '0x' */
-
-                       for (figure = 0; figure < 4; figure++) {
-                               gint n = 0;
-
-                               if ('0' <= *p && *p <= '9') {
-                                       n = *p - '0';
-                               } else if ('a' <= *p && *p <= 'f') {
-                                       n = *p - 'a' + 10;
-                               } else if ('A' <= *p && *p <= 'F') {
-                                       n = *p - 'A' + 10;
-                               }
-
-                               strcat(buf, bit_pattern[n]);
-                               p++;  /* skip ',' */
-                       }
-
-                       p++;  /* skip '\n' */
-               }
-
-               strcpy(xpm[line++], buf);
-               p++;
-       }
-
-       return 0;
-}
-#endif