app, libgimp, pdb: new function gimp_link_layer_get_mime_type().

As discussed with NikcDC, this will be useful, for instance for the SVG
export to know the mime type of the linked file. If not PNG, JPG or SVG,
we may want to output a small warning that some viewers may not be able
to read such files, since the SVG spec makes the support of these 3
formats only as mandatory.
This commit is contained in:
Jehan 2025-09-24 16:08:47 +02:00
parent c13bf1af19
commit 28fa2e5fc7
8 changed files with 171 additions and 11 deletions

View file

@ -76,6 +76,7 @@ struct _GimpLinkPrivate
GimpImageBaseType base_type;
GimpPrecision precision;
GimpPlugInProcedure *load_proc;
const gchar *mime_type;
};
static void gimp_link_finalize (GObject *object);
@ -298,8 +299,8 @@ gimp_link_update_buffer (GimpLink *link,
{
GimpImage *image;
GimpPDBStatusType status;
const gchar *mime_type = NULL;
link->p->mime_type = NULL;
image = file_open_image (link->p->gimp,
gimp_get_user_context (link->p->gimp),
progress,
@ -313,7 +314,8 @@ gimp_link_update_buffer (GimpLink *link,
*/
GIMP_RUN_NONINTERACTIVE,
&link->p->is_vector,
&status, &mime_type, &real_error);
&status, &link->p->mime_type,
&real_error);
if (image && status == GIMP_PDB_SUCCESS)
{
@ -635,6 +637,14 @@ gimp_link_set_absolute_path (GimpLink *link,
link->p->absolute_path = absolute_path;
}
const gchar *
gimp_link_get_mime_type (GimpLink *link)
{
g_return_val_if_fail (GIMP_IS_LINK (link), NULL);
return link->p->mime_type;
}
void
gimp_link_freeze (GimpLink *link)
{

View file

@ -73,6 +73,9 @@ void gimp_link_set_file (GimpLink *layer,
gboolean gimp_link_get_absolute_path (GimpLink *link);
void gimp_link_set_absolute_path (GimpLink *link,
gboolean absolute_path);
const gchar * gimp_link_get_mime_type (GimpLink *link);
void gimp_link_freeze (GimpLink *link);
void gimp_link_thaw (GimpLink *link);
gboolean gimp_link_is_monitored (GimpLink *link);

View file

@ -30,7 +30,7 @@
#include "internal-procs.h"
/* 758 procedures registered total */
/* 759 procedures registered total */
void
internal_procs_init (GimpPDB *pdb)

View file

@ -213,6 +213,35 @@ link_layer_set_file_invoker (GimpProcedure *procedure,
error ? *error : NULL);
}
static GimpValueArray *
link_layer_get_mime_type_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
GimpValueArray *return_vals;
GimpLinkLayer *layer;
gchar *mimetype = NULL;
layer = g_value_get_object (gimp_value_array_index (args, 0));
if (success)
{
mimetype = g_strdup (gimp_link_get_mime_type (gimp_link_layer_get_link (layer)));
}
return_vals = gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
if (success)
g_value_take_string (gimp_value_array_index (return_vals, 1), mimetype);
return return_vals;
}
void
register_link_layer_procs (GimpPDB *pdb)
{
@ -361,4 +390,38 @@ register_link_layer_procs (GimpPDB *pdb)
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
/*
* gimp-link-layer-get-mime-type
*/
procedure = gimp_procedure_new (link_layer_get_mime_type_invoker, FALSE);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-link-layer-get-mime-type");
gimp_procedure_set_static_help (procedure,
"Get the mime type of the monitored file.",
"This procedure returns the mime type of the file which is being monitored by @layer.\n"
"\n"
"Note that this will be the real mime type, corresponding to our format support, as returned by the [class@Gimp.LoadProcedure] which actually performs the external image file import.\n"
"\n"
"This function may also return %NULL in case of error (for instance if the external file doesn't exist anymore).",
NULL);
gimp_procedure_set_static_attribution (procedure,
"Jehan",
"Jehan",
"2025");
gimp_procedure_add_argument (procedure,
gimp_param_spec_link_layer ("layer",
"layer",
"The link layer",
FALSE,
GIMP_PARAM_READWRITE));
gimp_procedure_add_return_value (procedure,
gimp_param_spec_string ("mimetype",
"mimetype",
"The mime type of the monitored file",
FALSE, FALSE, FALSE,
NULL,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
}

View file

@ -684,6 +684,7 @@ EXPORTS
gimp_link_layer_discard
gimp_link_layer_get_by_id
gimp_link_layer_get_file
gimp_link_layer_get_mime_type
gimp_link_layer_get_type
gimp_link_layer_monitor
gimp_link_layer_new

View file

@ -232,3 +232,48 @@ gimp_link_layer_set_file (GimpLinkLayer *layer,
return success;
}
/**
* gimp_link_layer_get_mime_type:
* @layer: The link layer.
*
* Get the mime type of the monitored file.
*
* This procedure returns the mime type of the file which is being
* monitored by @layer.
*
* Note that this will be the real mime type, corresponding to our
* format support, as returned by the [class@Gimp.LoadProcedure] which
* actually performs the external image file import.
*
* This function may also return %NULL in case of error (for instance
* if the external file doesn't exist anymore).
*
* Returns: (transfer full): The mime type of the monitored file.
* The returned value must be freed with g_free().
*
* Since: 3.2
**/
gchar *
gimp_link_layer_get_mime_type (GimpLinkLayer *layer)
{
GimpValueArray *args;
GimpValueArray *return_vals;
gchar *mimetype = NULL;
args = gimp_value_array_new_from_types (NULL,
GIMP_TYPE_LINK_LAYER, layer,
G_TYPE_NONE);
return_vals = _gimp_pdb_run_procedure_array (gimp_get_pdb (),
"gimp-link-layer-get-mime-type",
args);
gimp_value_array_unref (args);
if (GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS)
mimetype = GIMP_VALUES_DUP_STRING (return_vals, 1);
gimp_value_array_unref (return_vals);
return mimetype;
}

View file

@ -32,13 +32,14 @@ G_BEGIN_DECLS
/* For information look into the C source or the html documentation */
GimpLinkLayer* gimp_link_layer_new (GimpImage *image,
GFile *file);
gboolean gimp_link_layer_discard (GimpLinkLayer *layer);
gboolean gimp_link_layer_monitor (GimpLinkLayer *layer);
GFile* gimp_link_layer_get_file (GimpLinkLayer *layer);
gboolean gimp_link_layer_set_file (GimpLinkLayer *layer,
GFile *file);
GimpLinkLayer* gimp_link_layer_new (GimpImage *image,
GFile *file);
gboolean gimp_link_layer_discard (GimpLinkLayer *layer);
gboolean gimp_link_layer_monitor (GimpLinkLayer *layer);
GFile* gimp_link_layer_get_file (GimpLinkLayer *layer);
gboolean gimp_link_layer_set_file (GimpLinkLayer *layer,
GFile *file);
gchar* gimp_link_layer_get_mime_type (GimpLinkLayer *layer);
G_END_DECLS

View file

@ -204,6 +204,42 @@ CODE
);
}
sub link_layer_get_mime_type {
$blurb = 'Get the mime type of the monitored file.';
$help = <<'HELP';
This procedure returns the mime type of the file which is being monitored by @layer.
Note that this will be the real mime type, corresponding to our format support,
as returned by the [class@Gimp.LoadProcedure] which actually performs the external
image file import.
This function may also return %NULL in case of error (for instance if the external
file doesn't exist anymore).
HELP
&jehan_pdb_misc('2025', '3.2');
@inargs = (
{ name => 'layer', type => 'link_layer',
desc => 'The link layer' }
);
@outargs = (
{ name => 'mimetype', type => 'string',
desc => 'The mime type of the monitored file' }
);
%invoke = (
code => <<'CODE'
{
mimetype = g_strdup (gimp_link_get_mime_type (gimp_link_layer_get_link (layer)));
}
CODE
);
}
@headers = qw("core/gimplink.h"
"core/gimplinklayer.h"
"gimppdberror.h"
@ -213,7 +249,8 @@ CODE
link_layer_discard
link_layer_monitor
link_layer_get_file
link_layer_set_file);
link_layer_set_file
link_layer_get_mime_type);
%exports = (app => [@procs], lib => [@procs]);