libgimp: fix getting invalid resource the first call with a NULL arg.

When the core sends a NULL resource, which would be the default for object args,
hence is also what you get for the first call of a plug-in with a resource
parameter, libgimp was creating a GimpResource with NULL id, which is invalid.
It is much better to return NULL (since we made it so that NULL is a valid
value) and let the plug-in handle the NULL value as it sees fit for a given
parameter (they could just set the contextual resource for this type, or keep
NULL to mean "no resource selected").

This fixes failing to run plug-ins the first time (before any "last" values are
set). E.g. I had the issue when testing palette-sort.

Also I'm improving the error message when trying to use a non-installed resource
(it will now also print the resource ID and the error message). And the GError
was leaking in this case, so I properly free it now.
This commit is contained in:
Jehan 2023-01-14 19:06:03 +01:00
parent 24b955bf17
commit e8365e81e2
1 changed files with 20 additions and 11 deletions

View File

@ -530,20 +530,20 @@ get_resource_by_id (gpointer gimp,
GType gtype, GType gtype,
gchar *id) gchar *id)
{ {
GObject *resource = NULL;
GError *error = NULL;
g_return_val_if_fail (id != NULL, NULL);
#ifdef LIBGIMP_COMPILATION #ifdef LIBGIMP_COMPILATION
/* Return a new proxy instance, not any instance already existing. /* Return a new proxy instance, not any instance already existing.
* Primarily for class's new() and duplicate() methods, * Primarily for class's new() and duplicate() methods,
* and for procedure args of a resource type. * and for procedure args of a resource type.
*/ */
GObject *resource = g_object_new (gtype, NULL); resource = g_object_new (gtype, NULL);
g_object_set (resource, "id", id, NULL); g_object_set (resource, "id", id, NULL);
g_debug ("libgimp:get_resource_by_id"); g_debug ("libgimp:get_resource_by_id");
return resource;
#else #else
GError *error = NULL;
GObject *resource = NULL;
if (gtype == GIMP_TYPE_BRUSH) if (gtype == GIMP_TYPE_BRUSH)
resource = (GObject*) gimp_pdb_get_brush (gimp, id, GIMP_PDB_DATA_ACCESS_READ, &error); resource = (GObject*) gimp_pdb_get_brush (gimp, id, GIMP_PDB_DATA_ACCESS_READ, &error);
else if (gtype == GIMP_TYPE_FONT) else if (gtype == GIMP_TYPE_FONT)
@ -558,9 +558,13 @@ get_resource_by_id (gpointer gimp,
g_warning ("%s unsupported type: %s", G_STRFUNC, g_type_name (gtype)); g_warning ("%s unsupported type: %s", G_STRFUNC, g_type_name (gtype));
if (error != NULL) if (error != NULL)
g_warning ("A plugin is trying to use a resource %s that is no longer installed.", g_type_name (gtype) ); g_warning ("A plugin is trying to use resource '%s' (of type %s) that is no longer installed: %s",
return resource; id, g_type_name (gtype), error->message);
#endif #endif
g_clear_error (&error);
return resource;
} }
/* Return a resource's ID. /* Return a resource's ID.
@ -735,8 +739,12 @@ gimp_gp_param_to_value (gpointer gimp,
} }
else if (GIMP_VALUE_HOLDS_RESOURCE (value)) else if (GIMP_VALUE_HOLDS_RESOURCE (value))
{ {
gpointer resource; /* when compiled in app, use generic pointer. */ /* when compiled in app, use generic pointer. */
resource = get_resource_by_id (gimp, G_VALUE_TYPE (value), param->data.d_string); gpointer resource = NULL;
if (param->data.d_string != NULL)
resource = get_resource_by_id (gimp, G_VALUE_TYPE (value), param->data.d_string);
g_value_set_object (value, resource); g_value_set_object (value, resource);
} }
else if (G_VALUE_HOLDS_PARAM (value)) else if (G_VALUE_HOLDS_PARAM (value))
@ -1068,7 +1076,8 @@ gimp_value_to_gp_param (const GValue *value,
if (resource != NULL) if (resource != NULL)
{ {
gchar * resource_id = get_resource_id (resource); gchar *resource_id = get_resource_id (resource);
/* resource_id can be NULL if resource is invalid. */ /* resource_id can be NULL if resource is invalid. */
if (full_copy) if (full_copy)
param->data.d_string = g_strdup (resource_id); param->data.d_string = g_strdup (resource_id);