[network-manager-applet: 5/6] applet: anticipate nma_icon_check_and_load() returning no icon



commit cdb3ff94fb21d95350368e971a44db723c717b1f
Author: Thomas Haller <thaller redhat com>
Date:   Sun Mar 13 10:58:27 2016 +0100

    applet: anticipate nma_icon_check_and_load() returning no icon
    
    nma_icon_check_and_load() uses the fallback_icon when it fails
    to load the icon.
    
    Probably a fallback_icon should be always present. But nma_icons_reload()
    goes through some lengths to cope with an error creating the
    fallback_icon. So, either we assert that the fallback_icon is always
    present, or users of nma_icon_check_and_load() must cope with a NULL
    icon. Do the latter.

 src/ap-menu-item.c   |   34 +++++++++++++++++---------------
 src/applet.c         |   21 ++++++++-----------
 src/mb-menu-item.c   |   14 +++++++-----
 src/mobile-helpers.c |   51 +++++++++++++++++++++++++++----------------------
 4 files changed, 63 insertions(+), 57 deletions(-)
---
diff --git a/src/ap-menu-item.c b/src/ap-menu-item.c
index ee997f7..1a1ba75 100644
--- a/src/ap-menu-item.c
+++ b/src/ap-menu-item.c
@@ -98,7 +98,8 @@ static void
 update_icon (NMNetworkMenuItem *item, NMApplet *applet)
 {
        NMNetworkMenuItemPrivate *priv = NM_NETWORK_MENU_ITEM_GET_PRIVATE (item);
-       GdkPixbuf *icon, *scaled = NULL;
+       gs_unref_object GdkPixbuf *icon_free = NULL, *icon_free2 = NULL;
+       GdkPixbuf *icon;
        const char *icon_name = NULL;
 
        if (priv->is_adhoc)
@@ -106,23 +107,25 @@ update_icon (NMNetworkMenuItem *item, NMApplet *applet)
        else
                icon_name = mobile_helper_get_quality_icon_name (priv->int_strength);
 
-       icon = gdk_pixbuf_copy (nma_icon_check_and_load (icon_name, applet));
+       icon = nma_icon_check_and_load (icon_name, applet);
+       if (icon) {
+               if (priv->is_encrypted) {
+                       GdkPixbuf *encrypted = nma_icon_check_and_load ("nm-secure-lock", applet);
 
-       if (priv->is_encrypted) {
-               GdkPixbuf *encrypted = nma_icon_check_and_load ("nm-secure-lock", applet);
+                       if (encrypted) {
+                               icon = icon_free = gdk_pixbuf_copy (icon);
 
-               gdk_pixbuf_composite (encrypted, icon, 0, 0,
-                                     gdk_pixbuf_get_width (encrypted),
-                                     gdk_pixbuf_get_height (encrypted),
-                                     0, 0, 1.0, 1.0,
-                                     GDK_INTERP_NEAREST, 255);
-       }
+                               gdk_pixbuf_composite (encrypted, icon, 0, 0,
+                                                     gdk_pixbuf_get_width (encrypted),
+                                                     gdk_pixbuf_get_height (encrypted),
+                                                     0, 0, 1.0, 1.0,
+                                                     GDK_INTERP_NEAREST, 255);
+                       }
+               }
 
-       /* Scale to menu size if larger so the menu doesn't look awful */
-       if (gdk_pixbuf_get_height (icon) > 24 || gdk_pixbuf_get_width (icon) > 24) {
-               scaled = gdk_pixbuf_scale_simple (icon, 24, 24, GDK_INTERP_BILINEAR);
-               g_object_unref (icon);
-               icon = scaled;
+               /* Scale to menu size if larger so the menu doesn't look awful */
+               if (gdk_pixbuf_get_height (icon) > 24 || gdk_pixbuf_get_width (icon) > 24)
+                       icon = icon_free2 = gdk_pixbuf_scale_simple (icon, 24, 24, GDK_INTERP_BILINEAR);
        }
 
 #ifdef ENABLE_INDICATOR
@@ -137,7 +140,6 @@ update_icon (NMNetworkMenuItem *item, NMApplet *applet)
 #else
        gtk_image_set_from_pixbuf (GTK_IMAGE (priv->strength), icon);
 #endif
-       g_object_unref (icon);
 }
 
 void
diff --git a/src/applet.c b/src/applet.c
index 24fd213..ad37a00 100644
--- a/src/applet.c
+++ b/src/applet.c
@@ -1482,8 +1482,8 @@ nma_menu_add_vpn_submenu (GtkWidget *menu, NMApplet *applet)
                gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (item), !!active);
 
                g_object_set_data_full (G_OBJECT (item), "connection", 
-                                                   g_object_ref (connection),
-                                                   (GDestroyNotify) g_object_unref);
+                                       g_object_ref (connection),
+                                       (GDestroyNotify) g_object_unref);
 
                g_signal_connect (item, "activate", G_CALLBACK (nma_menu_vpn_item_clicked), applet);
                gtk_menu_shell_append (GTK_MENU_SHELL (vpn_menu), GTK_WIDGET (item));
@@ -1990,7 +1990,7 @@ applet_add_default_connection_item (NMDevice *device,
 {
        AppletMenuItemInfo *info;
        GtkWidget *item;
-       
+
        item = gtk_check_menu_item_new_with_label (label);
        gtk_widget_set_sensitive (GTK_WIDGET (item), sensitive);
        gtk_check_menu_item_set_draw_as_radio (GTK_CHECK_MENU_ITEM (item), TRUE);
@@ -2455,7 +2455,7 @@ applet_common_get_device_icon (NMDeviceState state,
                char *name = g_strdup_printf ("nm-stage%02d-connecting%02d", stage + 1, 
applet->animation_step + 1);
 
                if (out_pixbuf)
-                       *out_pixbuf = g_object_ref (nma_icon_check_and_load (name, applet));
+                       *out_pixbuf = nm_g_object_ref (nma_icon_check_and_load (name, applet));
                if (out_icon_name)
                        *out_icon_name = name;
                else
@@ -2539,7 +2539,7 @@ applet_get_device_icon_for_state (NMApplet *applet,
                dclass->get_icon (device, state, connection, out_pixbuf, &icon_name, out_tip, applet);
 
                if (!*out_pixbuf && icon_name)
-                       *out_pixbuf = g_object_ref (nma_icon_check_and_load (icon_name, applet));
+                       *out_pixbuf = nm_g_object_ref (nma_icon_check_and_load (icon_name, applet));
                *out_icon_name = g_strdup (icon_name);
                if (!*out_tip)
                        *out_tip = get_tip_for_device_state (device, state, connection);
@@ -2952,9 +2952,7 @@ nma_icon_check_and_load (const char *name, NMApplet *applet)
        if (!(icon = gtk_icon_theme_load_icon (applet->icon_theme, name, applet->icon_size, 
GTK_ICON_LOOKUP_FORCE_SIZE, &error))) {
                g_warning ("failed to load icon \"%s\": %s", name, error->message);
                g_clear_error (&error);
-
-               if (applet->fallback_icon)
-                       icon = g_object_ref (applet->fallback_icon);
+               icon = nm_g_object_ref (applet->fallback_icon);
        }
 
        g_hash_table_insert (applet->icon_cache, g_strdup (name), icon);
@@ -2991,13 +2989,12 @@ nma_icons_reload (NMApplet *applet, gpointer user_data)
        if (!gdk_pixbuf_loader_close (loader, &error))
                goto error;
 
-       applet->fallback_icon = gdk_pixbuf_loader_get_pixbuf (loader);
-       g_return_if_fail (applet->fallback_icon);
-       g_object_ref (applet->fallback_icon);
+       applet->fallback_icon = nm_g_object_ref (gdk_pixbuf_loader_get_pixbuf (loader));
+       g_warn_if_fail (applet->fallback_icon);
        return;
 
 error:
-       g_critical ("failed loading default-icon: %s", error->message);
+       g_warning ("failed loading default-icon: %s", error->message);
        g_clear_error (&error);
 }
 
diff --git a/src/mb-menu-item.c b/src/mb-menu-item.c
index e1bcde5..b370d05 100644
--- a/src/mb-menu-item.c
+++ b/src/mb-menu-item.c
@@ -186,20 +186,22 @@ nm_mb_menu_item_new (const char *connection_name,
        /* And the strength icon, if we have strength information at all */
        if (enabled && strength) {
                const char *icon_name = mobile_helper_get_quality_icon_name (strength);
-               GdkPixbuf *pixbuf = nma_icon_check_and_load (icon_name, applet);
 
 #ifdef ENABLE_INDICATOR
+
+               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
 #ifdef DBUSMENU_PIXMAP_SUPPORT
-               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item), gtk_image_new_from_pixbuf 
(pixbuf));
+                                              gtk_image_new_from_pixbuf (nma_icon_check_and_load (icon_name, 
applet))
 #else
-               gtk_image_menu_item_set_image (GTK_IMAGE_MENU_ITEM (item),
-                                              gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU));
-               pixbuf = NULL;
+                                              gtk_image_new_from_icon_name (icon_name, GTK_ICON_SIZE_MENU)
 #endif
+               );
+
                /* For some reason we must always re-set always-show after setting the image */
                gtk_image_menu_item_set_always_show_image (GTK_IMAGE_MENU_ITEM (item), TRUE);
 #else
-               gtk_image_set_from_pixbuf (GTK_IMAGE (priv->strength), pixbuf);
+               gtk_image_set_from_pixbuf (GTK_IMAGE (priv->strength),
+                                          nma_icon_check_and_load (icon_name, applet));
 #endif
        }
 
diff --git a/src/mobile-helpers.c b/src/mobile-helpers.c
index efd954e..406e3e0 100644
--- a/src/mobile-helpers.c
+++ b/src/mobile-helpers.c
@@ -38,9 +38,7 @@ mobile_helper_get_status_pixbuf (guint32 quality,
                                  guint32 access_tech,
                                  NMApplet *applet)
 {
-       GdkPixbuf *pixbuf, *qual_pixbuf, *wwan_pixbuf, *tmp;
-
-       wwan_pixbuf = nma_icon_check_and_load ("nm-wwan-tower", applet);
+       GdkPixbuf *pixbuf, *qual_pixbuf, *tmp;
 
        if (!quality_valid)
                quality = 0;
@@ -48,35 +46,42 @@ mobile_helper_get_status_pixbuf (guint32 quality,
 
        pixbuf = gdk_pixbuf_new (GDK_COLORSPACE_RGB,
                                 TRUE,
-                                gdk_pixbuf_get_bits_per_sample (qual_pixbuf),
-                                gdk_pixbuf_get_width (qual_pixbuf),
-                                gdk_pixbuf_get_height (qual_pixbuf));
+                                qual_pixbuf ? gdk_pixbuf_get_bits_per_sample (qual_pixbuf) : 8,
+                                qual_pixbuf ? gdk_pixbuf_get_width (qual_pixbuf) : 22,
+                                qual_pixbuf ? gdk_pixbuf_get_height (qual_pixbuf) : 22);
        gdk_pixbuf_fill (pixbuf, 0xFFFFFF00);
 
        /* Composite the tower icon into the final icon at the bottom layer */
-       gdk_pixbuf_composite (wwan_pixbuf, pixbuf,
-                             0, 0,
-                             gdk_pixbuf_get_width (wwan_pixbuf),
-                                                 gdk_pixbuf_get_height (wwan_pixbuf),
-                                                 0, 0, 1.0, 1.0,
-                                                 GDK_INTERP_BILINEAR, 255);
+       tmp = nma_icon_check_and_load ("nm-wwan-tower", applet);
+       if (tmp) {
+               gdk_pixbuf_composite (tmp, pixbuf,
+                                     0, 0,
+                                     gdk_pixbuf_get_width (tmp),
+                                     gdk_pixbuf_get_height (tmp),
+                                     0, 0, 1.0, 1.0,
+                                     GDK_INTERP_BILINEAR, 255);
+       }
 
        /* Composite the signal quality onto the icon on top of the WWAN tower */
-       gdk_pixbuf_composite (qual_pixbuf, pixbuf,
-                             0, 0,
-                             gdk_pixbuf_get_width (qual_pixbuf),
-                                                 gdk_pixbuf_get_height (qual_pixbuf),
-                                                 0, 0, 1.0, 1.0,
-                                                 GDK_INTERP_BILINEAR, 255);
+       if (qual_pixbuf) {
+               gdk_pixbuf_composite (qual_pixbuf, pixbuf,
+                                     0, 0,
+                                     gdk_pixbuf_get_width (qual_pixbuf),
+                                     gdk_pixbuf_get_height (qual_pixbuf),
+                                     0, 0, 1.0, 1.0,
+                                     GDK_INTERP_BILINEAR, 255);
+       }
 
        /* And finally the roaming or technology icon */
        if (state == MB_STATE_ROAMING) {
                tmp = nma_icon_check_and_load ("nm-mb-roam", applet);
-               gdk_pixbuf_composite (tmp, pixbuf, 0, 0,
-                                     gdk_pixbuf_get_width (tmp),
-                                                         gdk_pixbuf_get_height (tmp),
-                                                         0, 0, 1.0, 1.0,
-                                                         GDK_INTERP_BILINEAR, 255);
+               if (tmp) {
+                       gdk_pixbuf_composite (tmp, pixbuf, 0, 0,
+                                             gdk_pixbuf_get_width (tmp),
+                                             gdk_pixbuf_get_height (tmp),
+                                              0, 0, 1.0, 1.0,
+                                             GDK_INTERP_BILINEAR, 255);
+               }
        } else {
                const gchar *tech_icon_name;
 


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