#include "cafe-utils.h"
#include <string.h>
gboolean
load_image_by_id (CtkImage *image, CtkIconSize size, const gchar *image_id)
{
cairo_surface_t *surface;
gint width;
gint height;
gint scale_factor;
CtkIconTheme *icon_theme;<--- The scope of the variable 'icon_theme' can be reduced. [+]The scope of the variable 'icon_theme' can be reduced. Warning: Be careful when fixing this message, especially when there are inner loops. Here is an example where cppcheck will write that the scope for 'i' can be reduced:
void f(int x)
{
int i = 0;
if (x) {
// it's safe to move 'int i = 0;' here
for (int n = 0; n < 10; ++n) {
// it is possible but not safe to move 'int i = 0;' here
do_something(&i);
}
}
}
When you see this message it is always safe to reduce the variable scope 1 level.
gchar *id;
gboolean icon_exists;
if (!image_id)
return FALSE;
id = g_strdup (image_id);
scale_factor = ctk_widget_get_scale_factor (CTK_WIDGET (image));
ctk_icon_size_lookup (size, &width, &height);
ctk_image_set_pixel_size (image, width);
if (g_path_is_absolute (id))
{
GdkPixbuf *pixbuf;
pixbuf = gdk_pixbuf_new_from_file_at_size (id, width * scale_factor, height * scale_factor, NULL);
icon_exists = (pixbuf != NULL);
if (icon_exists)
{
surface = cdk_cairo_surface_create_from_pixbuf (pixbuf, scale_factor, NULL);
ctk_image_set_from_surface (image, surface);
cairo_surface_destroy (surface);
g_object_unref (pixbuf);
}
else
ctk_image_set_from_icon_name (image, "image-missing", size);
}
else
{
if ( /* file extensions are not copesetic with loading by "name" */
g_str_has_suffix (id, ".png") ||
g_str_has_suffix (id, ".svg") ||
g_str_has_suffix (id, ".xpm")
)
id[strlen (id) - 4] = '\0';
if (ctk_widget_has_screen (CTK_WIDGET (image)))
icon_theme =
ctk_icon_theme_get_for_screen (ctk_widget_get_screen (CTK_WIDGET
(image)));
else
icon_theme = ctk_icon_theme_get_default ();
surface = ctk_icon_theme_load_surface (icon_theme, id,
width, scale_factor,
NULL,
CTK_ICON_LOOKUP_FORCE_SIZE,
NULL);
icon_exists = (surface != NULL);
if (icon_exists) {
ctk_image_set_from_surface (image, surface);
cairo_surface_destroy (surface);
}
else
ctk_image_set_from_icon_name (image, "image-missing", size);
}
g_free (id);
return icon_exists;
}