From dc577baadcb57da9dbb6cfd3397ba499ce40a5eb Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Sat, 26 Oct 2013 15:03:51 -0400 Subject: IdoUserMenuItem: only allow file icons as avatars In practice, all avatars are file icons. This restriction allowed for quite some cleanup, as we don't need to worry about symbolic icons and themes aymore. Note: this patch removes the warning when the user avatar image was not found. This is a common enough case that the warning just clutters up logs. --- src/idousermenuitem.c | 158 +++++++++++++++++--------------------------------- 1 file changed, 53 insertions(+), 105 deletions(-) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index c92a890..ffb9412 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -61,100 +61,6 @@ static gboolean ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * image, cairo_t * cr, gpointer gself); -/*** -**** Avatar -***/ - -static GdkPixbuf * -load_gicon (GtkWidget * self, GIcon * icon, GError ** error) -{ - GtkSettings * settings; - gint width; - gint height; - GdkScreen * screen; - GtkIconTheme * icon_theme; - GtkIconInfo * info; - GtkStyleContext * style_context; - GdkPixbuf * pixbuf; - - settings = gtk_widget_get_settings (self); - if (settings == NULL) /* not attached to a toplevel yet */ - settings = gtk_settings_get_default (); - - if (!gtk_icon_size_lookup_for_settings (settings, - GTK_ICON_SIZE_MENU, - &width, - &height)) - { - /* arbitrary default size in case _size_lookup fails */ - width = 12; - height = 12; - } - - if ((screen = gtk_widget_get_screen (self))) - icon_theme = gtk_icon_theme_get_for_screen (screen); - else - icon_theme = gtk_icon_theme_get_default (); - - - info = gtk_icon_theme_lookup_by_gicon (icon_theme, - icon, - MIN (width, height), - GTK_ICON_LOOKUP_USE_BUILTIN | - GTK_ICON_LOOKUP_GENERIC_FALLBACK | - GTK_ICON_LOOKUP_FORCE_SIZE); - - style_context = gtk_widget_get_style_context (self); - - pixbuf = gtk_icon_info_load_symbolic_for_context (info, - style_context, - NULL, - error); - - g_clear_object (&info); - return pixbuf; -} - -static void -update_avatar (IdoUserMenuItem * self) -{ - IdoUserMenuItemPrivate * p = self->priv; - GdkPixbuf * pixbuf; - GtkImage * image; - - if (p->icon == NULL) - { - pixbuf = NULL; - } - else - { - GError * error = NULL; - - pixbuf = load_gicon (GTK_WIDGET(self), p->icon, &error); - - if (error != NULL) - { - g_warning ("Can't load user avatar icon: %s", error->message); - g_error_free (error); - } - } - - image = GTK_IMAGE (p->user_image); - - if (pixbuf != NULL) - { - gtk_image_set_from_pixbuf (image, pixbuf); - g_object_unref (G_OBJECT (pixbuf)); - } - else - { - GIcon * icon; - icon = g_themed_icon_new_with_default_fallbacks (FALLBACK_ICON_NAME); - gtk_image_set_from_gicon (image, icon, GTK_ICON_SIZE_MENU); - g_object_unref (icon); - } -} - /*** **** GObject virtual functions ***/ @@ -301,6 +207,9 @@ ido_user_menu_item_init (IdoUserMenuItem *self) // Create the UI elements. priv->user_image = gtk_image_new (); + gtk_image_set_from_icon_name (GTK_IMAGE (priv->user_image), + FALLBACK_ICON_NAME, + GTK_ICON_SIZE_MENU); priv->user_name = gtk_label_new (NULL); @@ -337,11 +246,6 @@ ido_user_menu_item_init (IdoUserMenuItem *self) g_signal_connect_after (GTK_WIDGET(self), "draw", G_CALLBACK(ido_user_menu_item_primitive_draw_cb_gtk_3), GTK_WIDGET(self)); - - /* load_gicon()'s behavior depends on the current screen/toplevel/etc, - so reload the avatar whenever the widget is realized */ - g_signal_connect_swapped (self, "realize", - G_CALLBACK(update_avatar), self); } @@ -386,6 +290,39 @@ ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * widget, return FALSE; } +/*** +**** Avatar +***/ +gboolean +ido_user_menu_item_set_icon_from_file_icon (IdoUserMenuItem *self, + GFileIcon *icon) +{ + GFile *file; + gchar *path; + gint width = 12; + gint height = 12; + GdkPixbuf *pixbuf; + + file = g_file_icon_get_file (G_FILE_ICON (icon)); + path = g_file_get_path (file); + + gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (GTK_WIDGET (self)), + GTK_ICON_SIZE_MENU, + &width, &height); + + pixbuf = gdk_pixbuf_new_from_file_at_scale (path, width, height, TRUE, NULL); + g_free (path); + + if (pixbuf) + { + gtk_image_set_from_pixbuf (GTK_IMAGE (self->priv->user_image), pixbuf); + g_object_unref (pixbuf); + return TRUE; + } + + return FALSE; +} + /*** **** PUBLIC API ***/ @@ -395,14 +332,25 @@ ido_user_menu_item_set_icon (IdoUserMenuItem * self, GIcon * icon) { IdoUserMenuItemPrivate * p = self->priv; - if (p->icon != icon) - { - g_clear_object (&p->icon); + if (p->icon == icon) + return; + + g_clear_object (&p->icon); - if (icon != NULL) - p->icon = g_object_ref (icon); + if (icon) + p->icon = g_object_ref (icon); - update_avatar (self); + /* Avatars are always loaded from disk. Show the fallback when no icon + * is set, the icon is not a file icon, or the file could not be + * found. + */ + if (icon == NULL || + !G_IS_FILE_ICON (icon) || + !ido_user_menu_item_set_icon_from_file_icon (self, G_FILE_ICON (icon))) + { + gtk_image_set_from_icon_name (GTK_IMAGE (p->user_image), + FALLBACK_ICON_NAME, + GTK_ICON_SIZE_MENU); } } -- cgit v1.2.3 From 0e079e89a79dcceb2960e8c37ba958910b520c9e Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Sun, 27 Oct 2013 07:24:09 -0700 Subject: Don't export ido_user_menu_item_set_icon_from_file_icon() --- src/idousermenuitem.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index ffb9412..d6df01e 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -293,7 +293,7 @@ ido_user_menu_item_primitive_draw_cb_gtk_3 (GtkWidget * widget, /*** **** Avatar ***/ -gboolean +static gboolean ido_user_menu_item_set_icon_from_file_icon (IdoUserMenuItem *self, GFileIcon *icon) { -- cgit v1.2.3 From a2e6b66a3ff0a3001c4ba307a2489afea5a09a1d Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Mon, 28 Oct 2013 10:12:52 -0700 Subject: ido_user_menu_item_set_icon_from_file_icon: free file --- src/idousermenuitem.c | 1 + 1 file changed, 1 insertion(+) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index d6df01e..4a67e8d 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -305,6 +305,7 @@ ido_user_menu_item_set_icon_from_file_icon (IdoUserMenuItem *self, file = g_file_icon_get_file (G_FILE_ICON (icon)); path = g_file_get_path (file); + g_object_unref (file); gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (GTK_WIDGET (self)), GTK_ICON_SIZE_MENU, -- cgit v1.2.3 From 6485128ded273d7d6bbb9f764e16c7b5cfc08919 Mon Sep 17 00:00:00 2001 From: Lars Uebernickel Date: Mon, 28 Oct 2013 10:16:39 -0700 Subject: ido_user_menu_item_set_icon_from_file_icon: don't initialize width and height --- src/idousermenuitem.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) (limited to 'src') diff --git a/src/idousermenuitem.c b/src/idousermenuitem.c index 4a67e8d..387c851 100644 --- a/src/idousermenuitem.c +++ b/src/idousermenuitem.c @@ -299,14 +299,15 @@ ido_user_menu_item_set_icon_from_file_icon (IdoUserMenuItem *self, { GFile *file; gchar *path; - gint width = 12; - gint height = 12; + gint width; + gint height; GdkPixbuf *pixbuf; file = g_file_icon_get_file (G_FILE_ICON (icon)); path = g_file_get_path (file); g_object_unref (file); + /* width and height will always be set by this function */ gtk_icon_size_lookup_for_settings (gtk_widget_get_settings (GTK_WIDGET (self)), GTK_ICON_SIZE_MENU, &width, &height); -- cgit v1.2.3