[gitg/gtk3] Do not show an avatar if it does not exist



commit a8ad9232678a98a39d1ed7b85e43ab062d99dbe0
Author: Garrett Regier <garrettregier gmail com>
Date:   Fri Jul 22 05:15:57 2011 -0700

    Do not show an avatar if it does not exist

 gitg/gitg-avatar-cache.c           |   59 +++++++++++++++++++++++++++++------
 gitg/gitg-revision-details-panel.c |    7 ++--
 2 files changed, 53 insertions(+), 13 deletions(-)
---
diff --git a/gitg/gitg-avatar-cache.c b/gitg/gitg-avatar-cache.c
index 77c7ee3..1a6584d 100644
--- a/gitg/gitg-avatar-cache.c
+++ b/gitg/gitg-avatar-cache.c
@@ -98,6 +98,15 @@ gitg_avatar_cache_init (GitgAvatarCache *cache)
 }
 
 static void
+cached_pixbuf_unref0 (gpointer pixbuf)
+{
+	if (pixbuf != NULL)
+	{
+		g_object_unref (pixbuf);
+	}
+}
+
+static void
 avatar_cache_insert (GitgAvatarCache *cache,
 		     const gchar     *uri,
 		     GdkPixbuf       *pixbuf)
@@ -109,11 +118,15 @@ avatar_cache_insert (GitgAvatarCache *cache,
 		priv->pixbuf_table = g_hash_table_new_full (g_str_hash,
 		                                            g_str_equal,
 		                                            g_free,
-		                                            g_object_unref);
+		                                            cached_pixbuf_unref0);
 	}
 
-	g_hash_table_insert (priv->pixbuf_table, g_strdup (uri),
-	                     g_object_ref (pixbuf));
+	if (pixbuf != NULL)
+	{
+		g_object_ref (pixbuf);
+	}
+
+	g_hash_table_insert (priv->pixbuf_table, g_strdup (uri), pixbuf);
 }
 
 static void
@@ -206,8 +219,19 @@ avatar_cache_open_cb (GObject      *object,
 	}
 	else
 	{
-		if (error && !g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+		/* At the moment G_IO_ERROR_NOT_FOUND is not being returned
+		 * and instead an error code of 404 is. This is HTTP's
+		 * File Not Found error.
+		 */
+		if (g_error_matches (error, G_IO_ERROR, G_IO_ERROR_NOT_FOUND) ||
+		    (error != NULL && error->code == 404))
+		{
+			avatar_cache_insert (loader->cache, loader->uri, NULL);
+		}
+		else if (!g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
+		{
 			g_warning ("%s: %s", G_STRFUNC, error->message);
+		}
 
 		avatar_cache_loader_finish (loader, error);
 	}
@@ -264,6 +288,7 @@ gitg_avatar_cache_load_uri_async (GitgAvatarCache     *cache,
                                   gpointer             user_data)
 {
 	GitgAvatarCachePrivate *priv;
+	gboolean                found = FALSE;
 	GdkPixbuf              *pixbuf = NULL;
 	GSimpleAsyncResult     *result;
 	GitgAvatarCacheLoader  *loader;
@@ -279,13 +304,26 @@ gitg_avatar_cache_load_uri_async (GitgAvatarCache     *cache,
 	                                    gitg_avatar_cache_load_uri_async);
 
 	if (priv->pixbuf_table)
-		pixbuf = g_hash_table_lookup (priv->pixbuf_table, uri);
+	{
+		found = g_hash_table_lookup_extended (priv->pixbuf_table, uri,
+		                                      NULL, (gpointer *) &pixbuf);
+	}
 
-	if (pixbuf)
+	if (found)
 	{
-		g_simple_async_result_set_op_res_gpointer (result,
-		                                           g_object_ref (pixbuf),
-		                                           g_object_unref);
+		if (pixbuf == NULL)
+		{
+			g_simple_async_result_set_error (result, G_IO_ERROR,
+			                                 G_IO_ERROR_NOT_FOUND,
+			                                 "Not Found");
+		}
+		else
+		{
+			g_simple_async_result_set_op_res_gpointer (result,
+			                                           g_object_ref (pixbuf),
+			                                           g_object_unref);
+		}
+
 		g_simple_async_result_complete_in_idle (result);
 	}
 	else
@@ -365,7 +403,8 @@ gitg_avatar_cache_get_gravatar_uri (GitgAvatarCache *cache,
 	g_checksum_update (priv->checksum, (gpointer) gravatar_id,
 	                   strlen (gravatar_id));
 
-	return g_strdup_printf ("http://www.gravatar.com/avatar/%s?s=%d";,
+	/* d=404 will return a File Not Found if the avatar does not exist */
+	return g_strdup_printf ("http://www.gravatar.com/avatar/%s?d=404&s=%d";,
 	                        g_checksum_get_string (priv->checksum),
 	                        AVATAR_SIZE);
 }
diff --git a/gitg/gitg-revision-details-panel.c b/gitg/gitg-revision-details-panel.c
index 5d0f6b6..a1d315d 100644
--- a/gitg/gitg-revision-details-panel.c
+++ b/gitg/gitg-revision-details-panel.c
@@ -750,11 +750,12 @@ avatar_ready (GObject                  *source_object,
 	                                        res,
 	                                        &error);
 
+	gtk_widget_set_visible (GTK_WIDGET (panel->priv->avatar),
+	                        error == NULL);
+
 	if (error == NULL)
 	{
-		gtk_image_set_from_pixbuf (panel->priv->avatar,
-		                           pixbuf);
-		gtk_widget_show (GTK_WIDGET (panel->priv->avatar));
+		gtk_image_set_from_pixbuf (panel->priv->avatar, pixbuf);
 	}
 }
 



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]