2007-10-10 [colin] 3.0.2cvs53
[claws.git] / src / headerview.c
index 4e06c3aeb46a982db071d4305bf2604ab2228296..dcf9a3b7483610dde78493233bb8f506a328182e 100644 (file)
@@ -4,7 +4,7 @@
  *
  * 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
- * the Free Software Foundation; either version 2 of the License, or
+ * the Free Software Foundation; either version 3 of the License, or
  * (at your option) any later version.
  *
  * This program is distributed in the hope that it will be useful,
@@ -13,8 +13,8 @@
  * GNU General Public License for more details.
  *
  * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
+ * along with this program. If not, see <http://www.gnu.org/licenses/>.
+ * 
  */
 
 #ifdef HAVE_CONFIG_H
 #include "utils.h"
 #include "base64.h"
 #include "headers.h"
+#include "addrindex.h"
 
 #if HAVE_LIBCOMPFACE
 #define XPM_XFACE_HEIGHT       (HEIGHT + 3)  /* 3 = 1 header + 2 colors */
 
 static gchar *xpm_xface[XPM_XFACE_HEIGHT];
 
-static void headerview_show_xface      (HeaderView     *headerview,
+static gint headerview_show_xface      (HeaderView     *headerview,
                                         MsgInfo        *msginfo);
 #endif
 
 static gint headerview_show_face       (HeaderView     *headerview,
                                         MsgInfo        *msginfo);
+static gint headerview_show_contact_pic        (HeaderView     *headerview,
+                                        MsgInfo        *msginfo);
+static void headerview_save_contact_pic        (HeaderView     *headerview,
+                                        MsgInfo        *msginfo);
 
 HeaderView *headerview_create(void)
 {
@@ -237,12 +242,17 @@ void headerview_show(HeaderView *headerview, MsgInfo *msginfo)
                return;
 
 #if HAVE_LIBCOMPFACE
-       headerview_show_xface(headerview, msginfo);
+       if (!headerview_show_xface(headerview, msginfo))
+               return;
 #endif
+
+       if (!headerview_show_contact_pic(headerview, msginfo))
+               return;
+
 }
 
 #if HAVE_LIBCOMPFACE
-static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
+static gint headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
 {
        GtkWidget *hbox = headerview->hbox;
        GtkWidget *image;
@@ -255,9 +265,9 @@ static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
                        gtk_widget_hide(headerview->image);
                        gtk_widget_queue_resize(hbox);
                }
-               return;
+               return -1;
        }
-       if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
+       if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return -1;
 
        if (headerview->image) {
                gtk_widget_destroy(headerview->image);
@@ -274,6 +284,10 @@ static void headerview_show_xface(HeaderView *headerview, MsgInfo *msginfo)
        }
 
        headerview->image = image;
+       if (image) {
+               headerview_save_contact_pic(headerview, msginfo);
+       }
+       return 0;
 }
 #endif
 
@@ -305,6 +319,91 @@ static gint headerview_show_face (HeaderView *headerview, MsgInfo *msginfo)
                gtk_widget_show(image);
        }
 
+       headerview->image = image;
+       if (image == NULL)
+               return -1;
+       else {
+               headerview_save_contact_pic(headerview, msginfo);
+               return 0;
+       }
+}
+
+static void headerview_save_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
+{
+       gchar *filename = NULL;
+       GError *error = NULL;
+       GdkPixbuf *picture = NULL;
+
+       if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return;
+
+       if (headerview->image) {
+               picture = gtk_image_get_pixbuf(GTK_IMAGE(headerview->image));
+       }
+       
+       filename = addrindex_get_picture_file(msginfo->from);
+       if (!filename)
+               return;
+       if (!is_file_exist(filename)) {
+               gdk_pixbuf_save(picture, filename, "png", &error, NULL);
+               if (error) {
+                       g_warning(_("Failed to save image: \n%s"),
+                                       error->message);
+                       g_error_free(error);
+               }
+       }
+       g_free(filename);
+}      
+
+static gint headerview_show_contact_pic (HeaderView *headerview, MsgInfo *msginfo)
+{
+       GtkWidget *hbox = headerview->hbox;
+       GtkWidget *image;
+       gchar *filename = NULL;
+       GError *error = NULL;
+       GdkPixbuf *picture = NULL;
+       gint w, h;
+
+       if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return -1;
+
+       if (headerview->image) {
+               gtk_widget_destroy(headerview->image);
+               headerview->image = NULL;
+       }
+       
+       filename = addrindex_get_picture_file(msginfo->from);
+       
+       if (!filename)
+               return -1;
+       if (!is_file_exist(filename)) {
+               g_free(filename);
+               return -1;
+       }
+       gdk_pixbuf_get_file_info(filename, &w, &h);
+       
+       if (w > 48 || h > 48)
+               picture = gdk_pixbuf_new_from_file_at_scale(filename, 
+                                               48, 48, TRUE, &error);
+       else
+               picture = gdk_pixbuf_new_from_file(filename, &error);
+
+       g_free(filename);
+       if (error) {
+               debug_print("Failed to import image: \n%s",
+                               error->message);
+               g_error_free(error);
+               return -1;
+       }
+       if (picture)
+               image = gtk_image_new_from_pixbuf(picture);
+       else 
+               return -1;
+
+       g_object_unref(picture);
+       if (image) {
+               gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0);
+               gtk_widget_show(image);
+       }
+
        headerview->image = image;
        if (image == NULL)
                return -1;