diff -Naur --exclude '*.swp' sylpheed-3.0.0beta4/src/gravatar.c sylpheed-3.0.0beta4-gravatar/src/gravatar.c --- sylpheed-3.0.0beta4/src/gravatar.c 1970-01-01 01:00:00.000000000 +0100 +++ sylpheed-3.0.0beta4-gravatar/src/gravatar.c 2010-01-09 18:05:42.241377857 +0100 @@ -0,0 +1,101 @@ +/* + * gravatar.c - a gravatar images downloader in C + * Copyright (C) 2010 Jan Stępień + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifdef HAVE_CONFIG_H +# include "config.h" +#endif + +#if USE_SSL + +#include +#include +#include +#include "gravatar.h" + +static void downloading_cb(GPid pid, gint status, gpointer data) { + void (*cb)(gpointer); + g_spawn_close_pid(pid); + g_assert(data != NULL); + g_assert(((gpointer*) data)[0] != NULL); + cb = ((gpointer*) data)[0]; + cb(((gpointer*) data)[1]); + g_free((gpointer*) data); +} + +static void spawn_downloading_process(const gchar *hash, const gchar *output, + void cb (gpointer), gpointer data) { + /* + * http://www.gravatar.com/avatar/7fa6a31367efc7c27adfbd318b6327cc?s=80&d=404 + * is 74 characters. + */ + gchar url[74 + 1]; + gchar *cmdline[6] = {"curl", "--silent", "--output"}; + GPid pid; + GError *error = NULL; + gpointer *callback_data = g_malloc(2 * sizeof(gpointer)); + callback_data[0] = cb; + callback_data[1] = data; + g_sprintf(url, "http://www.gravatar.com/avatar/%s?s=48&d=404", hash); + cmdline[3] = (gchar*) output; + cmdline[4] = url; + cmdline[5] = NULL; + /* + * TODO: HTTP proxy. + */ + debug_print("Spawning a gravatar downloading process.\n"); + if (g_spawn_async(NULL, cmdline, NULL, + G_SPAWN_DO_NOT_REAP_CHILD | G_SPAWN_SEARCH_PATH, NULL, NULL, + &pid, &error) == FALSE) { + g_warning("Couldn't execute curl"); + if (error) { + g_warning("g_spawn_async: %s", + error->message); + g_error_free(error); + } + return; + } + if (pid == 0) { + g_warning("Couldn't get PID of child process"); + return; + } + g_child_watch_add(pid, downloading_cb, callback_data); +} + +static void md5(const gchar *string, gchar *hash) { + char binary_hash[16]; + int i; + MD5(string, strlen(string), binary_hash); + for (i = 0; i < 16; ++i) + g_sprintf(hash + 2 * i, "%x%x", (binary_hash[i] & 0xF0) >> 4, + binary_hash[i] & 0xF); +} + +int gravatar_fetch_async(gchar* email, gchar* output, void cb (gpointer), + gpointer data) { + /* + * TODO: Caching images, handling error 404. + */ + gchar hash[32 + 1]; + extract_address(email); + md5(email, hash); + spawn_downloading_process(hash, output, cb, data); + return 0; +} + +#endif /* USE_SSL */ diff -Naur --exclude '*.swp' sylpheed-3.0.0beta4/src/gravatar.h sylpheed-3.0.0beta4-gravatar/src/gravatar.h --- sylpheed-3.0.0beta4/src/gravatar.h 1970-01-01 01:00:00.000000000 +0100 +++ sylpheed-3.0.0beta4-gravatar/src/gravatar.h 2010-01-09 18:05:29.994764678 +0100 @@ -0,0 +1,28 @@ +/* + * gravatar.c - a gravatar images downloader in C + * Copyright (C) 2010 Jan Stępień + * + * 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 + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. + */ + +#ifndef __GRAVATAR_H__ +#define __GRAVATAR_H__ + +#include + +int gravatar_fetch_async(gchar* email, gchar* output, void callback (gpointer), + gpointer data); + +#endif /* __GRAVATAR_H__ */ diff -Naur --exclude '*.swp' sylpheed-3.0.0beta4/src/headerview.c sylpheed-3.0.0beta4-gravatar/src/headerview.c --- sylpheed-3.0.0beta4/src/headerview.c 2006-11-13 11:03:21.000000000 +0100 +++ sylpheed-3.0.0beta4-gravatar/src/headerview.c 2010-01-09 18:05:50.398058665 +0100 @@ -47,6 +47,7 @@ #include "codeconv.h" #include "gtkutils.h" #include "utils.h" +#include "gravatar.h" #define TR(str) (prefs_common.trans_hdr ? gettext(str) : str) @@ -204,6 +205,42 @@ #endif } +#if USE_SSL +static void headerview_display_downloaded_gravatar(void *data) +{ + /* + * TODO: DRY. + */ + gchar *filename = "/tmp/sylpheed_gravatar"; + debug_print("Downloaded a gravatar.\n"); + HeaderView *headerview = (HeaderView *) data; + GtkWidget *hbox = headerview->hbox; + if (!headerview->image) { + GtkWidget *image; + + image = gtk_image_new_from_file(filename); + gtk_box_pack_start(GTK_BOX(hbox), image, FALSE, FALSE, 0); + gtk_widget_show(image); + headerview->image = image; + } else { + gtk_image_set_from_file(GTK_IMAGE(headerview->image), filename); + gtk_widget_show(headerview->image); + } + if (g_unlink(filename)) + g_warning("g_unlink: cannot unlink %s", filename); +} + +static void headerview_show_gravatar(HeaderView *headerview, MsgInfo *msginfo) +{ + gchar *filename = "/tmp/sylpheed_gravatar"; + + if (!GTK_WIDGET_VISIBLE(headerview->hbox)) return; + + gravatar_fetch_async(msginfo->from, filename, + &headerview_display_downloaded_gravatar, headerview); +} +#endif + void headerview_show(HeaderView *headerview, MsgInfo *msginfo) { headerview_clear(headerview); @@ -234,6 +271,13 @@ #if HAVE_LIBCOMPFACE headerview_show_xface(headerview, msginfo); +#else +# if USE_SSL + /* + * TODO: Show it only if the user has enabled it in settings. + */ + headerview_show_gravatar(headerview, msginfo); +# endif #endif } diff -Naur --exclude '*.swp' sylpheed-3.0.0beta4/src/Makefile.am sylpheed-3.0.0beta4-gravatar/src/Makefile.am --- sylpheed-3.0.0beta4/src/Makefile.am 2009-11-27 08:21:26.000000000 +0100 +++ sylpheed-3.0.0beta4-gravatar/src/Makefile.am 2010-01-09 17:47:47.613002334 +0100 @@ -93,7 +93,8 @@ update_check.c update_check.h \ quote_fmt_lex.l quote_fmt_lex.h \ quote_fmt_parse.y quote_fmt.h \ - sylpheed-marshal.c sylpheed-marshal.h + sylpheed-marshal.c sylpheed-marshal.h \ + gravatar.c gravatar.h BUILT_SOURCES = \ quote_fmt_lex.c \