static void gs_editor_refine_app_pixbuf (GsApp *app) { GPtrArray *icons; if (gs_app_get_pixbuf (app) != NULL) return; icons = gs_app_get_icons (app); for (guint i = 0; i < icons->len; i++) { AsIcon *ic = g_ptr_array_index (icons, i); g_autoptr(GError) error = NULL; if (as_icon_get_kind (ic) == AS_ICON_KIND_STOCK) { g_autoptr(GdkPixbuf) pb = NULL; pb = gtk_icon_theme_load_icon (gtk_icon_theme_get_default (), as_icon_get_name (ic), 64, GTK_ICON_LOOKUP_FORCE_SIZE, &error); if (pb == NULL) { g_warning ("failed to load icon: %s", error->message); continue; } gs_app_set_pixbuf (app, pb); } else { if (!as_icon_load (ic, AS_ICON_LOAD_FLAG_SEARCH_SIZE, &error)) { g_warning ("failed to load icon: %s", error->message); continue; } gs_app_set_pixbuf (app, as_icon_get_pixbuf (ic)); } break; } }
/** * gs_refine_item_pixbuf: */ static void gs_refine_item_pixbuf (GsPlugin *plugin, GsApp *app, AsApp *item) { AsIcon *icon; gboolean ret; g_autoptr(GError) error = NULL; g_autofree gchar *fn = NULL; g_autofree gchar *cachedir = NULL; icon = as_app_get_icon_default (item); switch (as_icon_get_kind (icon)) { case AS_ICON_KIND_REMOTE: gs_app_set_icon (app, icon); if (as_icon_get_filename (icon) == NULL) { cachedir = gs_utils_get_cachedir ("icons", NULL); fn = g_build_filename (cachedir, as_icon_get_name (icon), NULL); as_icon_set_filename (icon, fn); as_icon_set_prefix (icon, cachedir); } if (g_file_test (fn, G_FILE_TEST_EXISTS)) { as_icon_set_kind (icon, AS_ICON_KIND_LOCAL); ret = gs_app_load_icon (app, plugin->scale, &error); if (!ret) { g_warning ("failed to load icon %s: %s", as_icon_get_name (icon), error->message); return; } } break; case AS_ICON_KIND_STOCK: case AS_ICON_KIND_LOCAL: gs_app_set_icon (app, icon); /* does not exist, so try to find using the icon theme */ if (as_icon_get_kind (icon) == AS_ICON_KIND_LOCAL && as_icon_get_filename (icon) == NULL) as_icon_set_kind (icon, AS_ICON_KIND_STOCK); /* load */ ret = gs_app_load_icon (app, plugin->scale, &error); if (!ret) { g_warning ("failed to load %s icon %s: %s", as_icon_kind_to_string (as_icon_get_kind (icon)), as_icon_get_name (icon), error->message); return; } break; case AS_ICON_KIND_CACHED: if (plugin->scale == 2) icon = as_app_get_icon_for_size (item, 128, 128); if (icon == NULL) icon = as_app_get_icon_for_size (item, 64, 64); if (icon == NULL) { g_warning ("failed to find cached icon %s", as_icon_get_name (icon)); return; } if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, &error)) { g_warning ("failed to load cached icon %s: %s", as_icon_get_name (icon), error->message); return; } gs_app_set_pixbuf (app, as_icon_get_pixbuf (icon)); break; default: g_warning ("icon kind unknown for %s", as_app_get_id (item)); break; } }
/** * as_icon_convert_to_kind: * @icon: a #AsIcon instance. * @kind: a %AsIconKind, e.g. #AS_ICON_KIND_EMBEDDED * @error: A #GError or %NULL. * * Converts the icon from one kind to another. * * Returns: %TRUE for success * * Since: 0.3.1 **/ gboolean as_icon_convert_to_kind (AsIcon *icon, AsIconKind kind, GError **error) { AsIconPrivate *priv = GET_PRIVATE (icon); /* these can't be converted */ if (priv->kind == AS_ICON_KIND_STOCK || priv->kind == AS_ICON_KIND_REMOTE) return TRUE; /* no change */ if (priv->kind == kind) return TRUE; /* cached -> embedded */ if (priv->kind == AS_ICON_KIND_CACHED && kind == AS_ICON_KIND_EMBEDDED) { gsize data_size; g_autoptr(GBytes) tmp = NULL; g_autofree gchar *data = NULL; /* load the pixbuf and save it to a PNG buffer */ if (priv->pixbuf == NULL) { if (!as_icon_load (icon, AS_ICON_LOAD_FLAG_SEARCH_SIZE, error)) return FALSE; } if (!gdk_pixbuf_save_to_buffer (priv->pixbuf, &data, &data_size, "png", error, NULL)) return FALSE; /* set the PNG buffer to a blob of data */ tmp = g_bytes_new (data, data_size); as_icon_set_data (icon, tmp); as_icon_set_kind (icon, kind); return TRUE; } /* cached -> embedded */ if (priv->kind == AS_ICON_KIND_EMBEDDED && kind == AS_ICON_KIND_CACHED) { g_autofree gchar *size_str = NULL; g_autofree gchar *path = NULL; g_autofree gchar *fn = NULL; /* ensure the parent path exists */ size_str = g_strdup_printf ("%ux%u", priv->width, priv->height); path = g_build_filename (priv->prefix, size_str, NULL); if (g_mkdir_with_parents (path, 0700) != 0) { g_set_error (error, AS_ICON_ERROR, AS_ICON_ERROR_FAILED, "Failed to create: %s", path); return FALSE; } /* save the pixbuf */ fn = g_build_filename (path, priv->name, NULL); if (!gdk_pixbuf_save (priv->pixbuf, fn, "png", error, NULL)) return FALSE; as_icon_set_kind (icon, kind); return TRUE; } /* not supported */ g_set_error (error, AS_ICON_ERROR, AS_ICON_ERROR_FAILED, "converting %s to %s is not supported", as_icon_kind_to_string (priv->kind), as_icon_kind_to_string (kind)); return FALSE; }