app, pdb, libgimp: new GimpBatchProcedure class.

This new class will be used to register procedures usable for batch
processing.
This commit is contained in:
Jehan 2022-04-16 17:09:21 +02:00
parent d815250043
commit 52b7273294
14 changed files with 562 additions and 1 deletions

View File

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

View File

@ -1081,6 +1081,38 @@ pdb_set_file_proc_thumbnail_loader_invoker (GimpProcedure *procedure,
error ? *error : NULL);
}
static GimpValueArray *
pdb_set_batch_interpreter_invoker (GimpProcedure *procedure,
Gimp *gimp,
GimpContext *context,
GimpProgress *progress,
const GimpValueArray *args,
GError **error)
{
gboolean success = TRUE;
const gchar *procedure_name;
procedure_name = g_value_get_string (gimp_value_array_index (args, 0));
if (success)
{
GimpPlugIn *plug_in = gimp->plug_in_manager->current_plug_in;
if (plug_in &&
gimp_pdb_is_canonical_procedure (procedure_name, error))
{
success = gimp_plug_in_set_batch_interpreter (plug_in,
procedure_name,
error);
}
else
success = FALSE;
}
return gimp_procedure_get_return_values (procedure, success,
error ? *error : NULL);
}
static GimpValueArray *
pdb_get_data_invoker (GimpProcedure *procedure,
Gimp *gimp,
@ -2139,6 +2171,30 @@ register_pdb_procs (GimpPDB *pdb)
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
/*
* gimp-pdb-set-batch-interpreter
*/
procedure = gimp_procedure_new (pdb_set_batch_interpreter_invoker);
gimp_object_set_static_name (GIMP_OBJECT (procedure),
"gimp-pdb-set-batch-interpreter");
gimp_procedure_set_static_help (procedure,
"Registers a batch interpreter procedure.",
"Registers a procedural database procedure to be called with the command line interface options --batch-interpreter and --batch.",
NULL);
gimp_procedure_set_static_attribution (procedure,
"Jehan",
"Jehan",
"2022");
gimp_procedure_add_argument (procedure,
gimp_param_spec_string ("procedure-name",
"procedure name",
"The name of the procedure to be used for running batch commands",
FALSE, FALSE, TRUE,
NULL,
GIMP_PARAM_READWRITE));
gimp_pdb_register_procedure (pdb, procedure);
g_object_unref (procedure);
/*
* gimp-pdb-get-data
*/

View File

@ -598,6 +598,38 @@ gimp_plug_in_set_file_proc_thumb_loader (GimpPlugIn *plug_in,
return TRUE;
}
gboolean
gimp_plug_in_set_batch_interpreter (GimpPlugIn *plug_in,
const gchar *proc_name,
GError **error)
{
GimpPlugInProcedure *proc;
g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), FALSE);
g_return_val_if_fail (proc_name != NULL, FALSE);
proc = gimp_plug_in_proc_find (plug_in, proc_name);
if (! proc)
{
g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_PROCEDURE_NOT_FOUND,
"Plug-in \"%s\"\n(%s)\n"
"attempted to register procedure \"%s\" "
"as a 'batch interpreter'.\n"
"It has however not installed that procedure. "
"This is not allowed.",
gimp_object_get_name (plug_in),
gimp_file_get_utf8_name (plug_in->file),
proc_name);
return FALSE;
}
gimp_plug_in_procedure_set_batch_interpreter (proc);
return TRUE;
}
/* private functions */

View File

@ -85,6 +85,9 @@ gboolean gimp_plug_in_set_file_proc_thumb_loader (GimpPlugIn *plug_in,
const gchar *proc_name,
const gchar *thumb_proc,
GError **error);
gboolean gimp_plug_in_set_batch_interpreter (GimpPlugIn *plug_in,
const gchar *proc_name,
GError **error);
#endif /* __GIMP_PLUG_IN_PROC_H__ */

View File

@ -1274,6 +1274,14 @@ gimp_plug_in_procedure_set_thumb_loader (GimpPlugInProcedure *proc,
proc->thumb_loader = g_strdup (thumb_loader);
}
void
gimp_plug_in_procedure_set_batch_interpreter (GimpPlugInProcedure *proc)
{
g_return_if_fail (GIMP_IS_PLUG_IN_PROCEDURE (proc));
proc->batch_interpreter = TRUE;
}
void
gimp_plug_in_procedure_handle_return_values (GimpPlugInProcedure *proc,
Gimp *gimp,

View File

@ -65,6 +65,7 @@ struct _GimpPlugInProcedure
gchar *mime_types;
gboolean handles_remote;
gboolean handles_raw;
gboolean batch_interpreter;
GSList *extensions_list;
GSList *prefixes_list;
GSList *magics_list;
@ -141,6 +142,7 @@ void gimp_plug_in_procedure_set_handles_remote(GimpPlugInProcedure *pro
void gimp_plug_in_procedure_set_handles_raw (GimpPlugInProcedure *proc);
void gimp_plug_in_procedure_set_thumb_loader (GimpPlugInProcedure *proc,
const gchar *thumbnailer);
void gimp_plug_in_procedure_set_batch_interpreter (GimpPlugInProcedure *proc);
void gimp_plug_in_procedure_handle_return_values (GimpPlugInProcedure *proc,
Gimp *gimp,

View File

@ -109,6 +109,7 @@ libgimp_introspectable_headers = \
../libgimp/gimptypes.h \
../libgimp/gimpenums.h \
${PDB_WRAPPERS_H} \
../libgimp/gimpbatchprocedure.h \
../libgimp/gimpbrushselect.h \
../libgimp/gimpchannel.h \
../libgimp/gimpdisplay.h \
@ -143,6 +144,7 @@ libgimp_introspectable = \
$(libgimp_built_sources) \
../libgimp/gimp.c \
${PDB_WRAPPERS_C} \
../libgimp/gimpbatchprocedure.c \
../libgimp/gimpbrushselect.c \
../libgimp/gimpchannel.c \
../libgimp/gimpdisplay.c \

View File

@ -36,6 +36,7 @@
#include <libgimp/gimpenums.h>
#include <libgimp/gimptypes.h>
#include <libgimp/gimpbatchprocedure.h>
#include <libgimp/gimpbrushselect.h>
#include <libgimp/gimpchannel.h>
#include <libgimp/gimpdisplay.h>

View File

@ -0,0 +1,288 @@
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpbatchprocedure.c
* Copyright (C) 2022 Jehan
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "config.h"
#include "gimp.h"
#include "gimpbatchprocedure.h"
#include "gimppdb_pdb.h"
struct _GimpBatchProcedurePrivate
{
gchar *interpreter_name;
GimpBatchFunc run_func;
gpointer run_data;
GDestroyNotify run_data_destroy;
};
static void gimp_batch_procedure_constructed (GObject *object);
static void gimp_batch_procedure_finalize (GObject *object);
static void gimp_batch_procedure_install (GimpProcedure *procedure);
static GimpValueArray *
gimp_batch_procedure_run (GimpProcedure *procedure,
const GimpValueArray *args);
static GimpProcedureConfig *
gimp_batch_procedure_create_config (GimpProcedure *procedure,
GParamSpec **args,
gint n_args);
G_DEFINE_TYPE_WITH_PRIVATE (GimpBatchProcedure, gimp_batch_procedure,
GIMP_TYPE_PROCEDURE)
#define parent_class gimp_batch_procedure_parent_class
static void
gimp_batch_procedure_class_init (GimpBatchProcedureClass *klass)
{
GObjectClass *object_class = G_OBJECT_CLASS (klass);
GimpProcedureClass *procedure_class = GIMP_PROCEDURE_CLASS (klass);
object_class->constructed = gimp_batch_procedure_constructed;
object_class->finalize = gimp_batch_procedure_finalize;
procedure_class->install = gimp_batch_procedure_install;
procedure_class->run = gimp_batch_procedure_run;
procedure_class->create_config = gimp_batch_procedure_create_config;
}
static void
gimp_batch_procedure_init (GimpBatchProcedure *procedure)
{
procedure->priv = gimp_batch_procedure_get_instance_private (procedure);
}
static void
gimp_batch_procedure_constructed (GObject *object)
{
GimpProcedure *procedure = GIMP_PROCEDURE (object);
G_OBJECT_CLASS (parent_class)->constructed (object);
GIMP_PROC_ARG_ENUM (procedure, "run-mode",
"Run mode",
"The run mode",
GIMP_TYPE_RUN_MODE,
GIMP_RUN_NONINTERACTIVE,
G_PARAM_READWRITE);
GIMP_PROC_ARG_STRING (procedure, "script",
"Batch commands in the target language",
"Batch commands in the target language, which will be run by the interpreter",
"",
G_PARAM_READWRITE);
}
static void
gimp_batch_procedure_finalize (GObject *object)
{
GimpBatchProcedure *procedure = GIMP_BATCH_PROCEDURE (object);
g_clear_pointer (&procedure->priv->interpreter_name, g_free);
G_OBJECT_CLASS (parent_class)->finalize (object);
}
static void
gimp_batch_procedure_install (GimpProcedure *procedure)
{
GIMP_PROCEDURE_CLASS (parent_class)->install (procedure);
_gimp_pdb_set_batch_interpreter (gimp_procedure_get_name (procedure));
}
#define ARG_OFFSET 2
static GimpValueArray *
gimp_batch_procedure_run (GimpProcedure *procedure,
const GimpValueArray *args)
{
GimpBatchProcedure *batch_proc = GIMP_BATCH_PROCEDURE (procedure);
GimpValueArray *remaining;
GimpValueArray *return_values;
GimpRunMode run_mode;
const gchar *cmd;
gint i;
run_mode = GIMP_VALUES_GET_ENUM (args, 0);
cmd = GIMP_VALUES_GET_STRING (args, 1);
remaining = gimp_value_array_new (gimp_value_array_length (args) - ARG_OFFSET);
for (i = ARG_OFFSET; i < gimp_value_array_length (args); i++)
{
GValue *value = gimp_value_array_index (args, i);
gimp_value_array_append (remaining, value);
}
return_values = batch_proc->priv->run_func (procedure,
run_mode,
cmd,
remaining,
batch_proc->priv->run_data);
gimp_value_array_unref (remaining);
return return_values;
}
static GimpProcedureConfig *
gimp_batch_procedure_create_config (GimpProcedure *procedure,
GParamSpec **args,
gint n_args)
{
if (n_args > ARG_OFFSET)
{
args += ARG_OFFSET;
n_args -= ARG_OFFSET;
}
else
{
args = NULL;
n_args = 0;
}
return GIMP_PROCEDURE_CLASS (parent_class)->create_config (procedure,
args,
n_args);
}
/* public functions */
/**
* gimp_batch_procedure_new:
* @plug_in: a #GimpPlugIn.
* @name: the new procedure's name.
* @proc_type: the new procedure's #GimpPDBProcType.
* @run_func: the run function for the new procedure.
* @run_data: user data passed to @run_func.
* @run_data_destroy: (nullable): free function for @run_data, or %NULL.
*
* Creates a new batch interpreter procedure named @name which will call
* @run_func when invoked.
*
* See gimp_procedure_new() for information about @proc_type.
*
* #GimpBatchProcedure is a #GimpProcedure subclass that makes it easier
* to write batch interpreter procedures.
*
* It automatically adds the standard
*
* (#GimpRunMode, #gchar)
*
* arguments of a batch procedure. It is possible to add additional
* arguments.
*
* When invoked via gimp_procedure_run(), it unpacks these standard
* arguments and calls @run_func which is a #GimpBatchFunc. The "args"
* #GimpValueArray of #GimpRunSaveFunc only contains additionally added
* arguments.
*
* Returns: a new #GimpProcedure.
*
* Since: 3.0
**/
GimpProcedure *
gimp_batch_procedure_new (GimpPlugIn *plug_in,
const gchar *name,
GimpPDBProcType proc_type,
GimpBatchFunc run_func,
gpointer run_data,
GDestroyNotify run_data_destroy)
{
GimpBatchProcedure *procedure;
g_return_val_if_fail (GIMP_IS_PLUG_IN (plug_in), NULL);
g_return_val_if_fail (gimp_is_canonical_identifier (name), NULL);
g_return_val_if_fail (proc_type != GIMP_PDB_PROC_TYPE_INTERNAL, NULL);
g_return_val_if_fail (proc_type != GIMP_PDB_PROC_TYPE_EXTENSION, NULL);
g_return_val_if_fail (run_func != NULL, NULL);
procedure = g_object_new (GIMP_TYPE_BATCH_PROCEDURE,
"plug-in", plug_in,
"name", name,
"procedure-type", proc_type,
NULL);
procedure->priv->run_func = run_func;
procedure->priv->run_data = run_data;
procedure->priv->run_data_destroy = run_data_destroy;
return GIMP_PROCEDURE (procedure);
}
/**
* gimp_batch_procedure_set_interpreter_name:
* @procedure: A batch procedure.
* @interpreter_name: A public-facing name for the interpreter, e.g. "Python 3".
*
* Associates an interpreter name with a batch procedure.
*
* This name can be used for any public-facing strings, such as
* graphical interface labels or command line usage. E.g. the command
* line interface could list all available interface, displaying both a
* procedure name and a "pretty printing" title.
*
* Note that since the format name is public-facing, it is recommended
* to localize it at runtime, for instance through gettext, like:
*
* ```c
* gimp_batch_procedure_set_interpreter_name (procedure, _("Python 3"));
* ```
*
* Some language would indeed localize even some technical terms or
* acronyms, even if sometimes just to rewrite them with the local
* writing system.
*
* Since: 3.0
**/
void
gimp_batch_procedure_set_interpreter_name (GimpBatchProcedure *procedure,
const gchar *interpreter_name)
{
g_return_if_fail (GIMP_IS_BATCH_PROCEDURE (procedure));
g_free (procedure->priv->interpreter_name);
procedure->priv->interpreter_name = g_strdup (interpreter_name);
}
/**
* gimp_batch_procedure_get_interpreter_name:
* @procedure: A batch procedure object.
*
* Returns the procedure's interpreter name, as set with
* [method@BatchProcedure.set_interpreter_name].
*
* Returns: The procedure's interpreter name.
*
* Since: 3.0
**/
const gchar *
gimp_batch_procedure_get_interpreter_name (GimpBatchProcedure *procedure)
{
g_return_val_if_fail (GIMP_IS_BATCH_PROCEDURE (procedure), NULL);
return procedure->priv->interpreter_name;
}

View File

@ -0,0 +1,93 @@
/* GIMP - The GNU Image Manipulation Program
* Copyright (C) 1995 Spencer Kimball and Peter Mattis
*
* gimpbatchprocedure.h
* Copyright (C) 2022 Jehan
*
* This library is free software: you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 3 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library. If not, see
* <https://www.gnu.org/licenses/>.
*/
#ifndef __GIMP_BATCH_PROCEDURE_H__
#define __GIMP_BATCH_PROCEDURE_H__
#include <libgimp/gimpprocedure.h>
G_BEGIN_DECLS
/* For information look into the C source or the html documentation */
/**
* GimpBatchFunc:
* @procedure: the #GimpProcedure that runs.
* @run_mode: the #GimpRunMode.
* @args: the @procedure's remaining arguments.
* @run_data: (closure): the run_data given in gimp_batch_procedure_new().
*
* The batch function is run during the lifetime of the GIMP session,
* each time a plug-in batch procedure is called.
*
* Returns: (transfer full): the @procedure's return values.
*
* Since: 3.0
**/
typedef GimpValueArray * (* GimpBatchFunc) (GimpProcedure *procedure,
GimpRunMode run_mode,
const gchar *command,
const GimpValueArray *args,
gpointer run_data);
#define GIMP_TYPE_BATCH_PROCEDURE (gimp_batch_procedure_get_type ())
#define GIMP_BATCH_PROCEDURE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GIMP_TYPE_BATCH_PROCEDURE, GimpBatchProcedure))
#define GIMP_BATCH_PROCEDURE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GIMP_TYPE_BATCH_PROCEDURE, GimpBatchProcedureClass))
#define GIMP_IS_BATCH_PROCEDURE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GIMP_TYPE_BATCH_PROCEDURE))
#define GIMP_IS_BATCH_PROCEDURE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GIMP_TYPE_BATCH_PROCEDURE))
#define GIMP_BATCH_PROCEDURE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GIMP_TYPE_BATCH_PROCEDURE, GimpBatchProcedureClass))
typedef struct _GimpBatchProcedure GimpBatchProcedure;
typedef struct _GimpBatchProcedureClass GimpBatchProcedureClass;
typedef struct _GimpBatchProcedurePrivate GimpBatchProcedurePrivate;
struct _GimpBatchProcedure
{
GimpProcedure parent_instance;
GimpBatchProcedurePrivate *priv;
};
struct _GimpBatchProcedureClass
{
GimpProcedureClass parent_class;
};
GType gimp_batch_procedure_get_type (void) G_GNUC_CONST;
GimpProcedure * gimp_batch_procedure_new (GimpPlugIn *plug_in,
const gchar *name,
GimpPDBProcType proc_type,
GimpBatchFunc run_func,
gpointer run_data,
GDestroyNotify run_data_destroy);
void gimp_batch_procedure_set_interpreter_name (GimpBatchProcedure *procedure,
const gchar *interpreter_name);
const gchar * gimp_batch_procedure_get_interpreter_name (GimpBatchProcedure *procedure);
G_END_DECLS
#endif /* __GIMP_BATCH_PROCEDURE_H__ */

View File

@ -1137,6 +1137,42 @@ _gimp_pdb_set_file_proc_thumbnail_loader (const gchar *load_proc,
return success;
}
/**
* _gimp_pdb_set_batch_interpreter:
* @procedure_name: The name of the procedure to be used for running batch commands.
*
* Registers a batch interpreter procedure.
*
* Registers a procedural database procedure to be called with the
* command line interface options --batch-interpreter and --batch.
*
* Returns: TRUE on success.
*
* Since: 3.0
**/
gboolean
_gimp_pdb_set_batch_interpreter (const gchar *procedure_name)
{
GimpValueArray *args;
GimpValueArray *return_vals;
gboolean success = TRUE;
args = gimp_value_array_new_from_types (NULL,
G_TYPE_STRING, procedure_name,
G_TYPE_NONE);
return_vals = gimp_pdb_run_procedure_array (gimp_get_pdb (),
"gimp-pdb-set-batch-interpreter",
args);
gimp_value_array_unref (args);
success = GIMP_VALUES_GET_ENUM (return_vals, 0) == GIMP_PDB_SUCCESS;
gimp_value_array_unref (return_vals);
return success;
}
/**
* _gimp_pdb_get_data:
* @identifier: The identifier associated with data.

View File

@ -97,6 +97,7 @@ G_GNUC_INTERNAL gboolean _gimp_pdb_set_file_proc_handles_remote (const gcha
G_GNUC_INTERNAL gboolean _gimp_pdb_set_file_proc_handles_raw (const gchar *procedure_name);
G_GNUC_INTERNAL gboolean _gimp_pdb_set_file_proc_thumbnail_loader (const gchar *load_proc,
const gchar *thumb_proc);
G_GNUC_INTERNAL gboolean _gimp_pdb_set_batch_interpreter (const gchar *procedure_name);
G_GNUC_INTERNAL gboolean _gimp_pdb_get_data (const gchar *identifier,
gint *bytes,
guint8 **data);

View File

@ -136,6 +136,7 @@ pdb_wrappers_headers = [
libgimp_sources_introspectable = [
'gimp.c',
'gimpbatchprocedure.c',
'gimpbrushselect.c',
'gimpchannel.c',
'gimpdisplay.c',
@ -191,6 +192,7 @@ libgimp_headers_introspectable = [
'gimpenums.h',
# Other headers
'gimpbatchprocedure.h',
'gimpbrushselect.h',
'gimpchannel.h',
'gimpdisplay.h',

View File

@ -1156,6 +1156,42 @@ CODE
);
}
sub pdb_set_batch_interpreter {
$blurb = 'Registers a batch interpreter procedure.';
$help = <<'HELP';
Registers a procedural database procedure to be called with the command
line interface options --batch-interpreter and --batch.
HELP
&jehan_pdb_misc('2022', '3.0');
$lib_private = 1;
@inargs = (
{ name => 'procedure_name', type => 'string', non_empty => 1,
desc => 'The name of the procedure to be used for running batch commands' },
);
%invoke = (
code => <<'CODE'
{
GimpPlugIn *plug_in = gimp->plug_in_manager->current_plug_in;
if (plug_in &&
gimp_pdb_is_canonical_procedure (procedure_name, error))
{
success = gimp_plug_in_set_batch_interpreter (plug_in,
procedure_name,
error);
}
else
success = FALSE;
}
CODE
);
}
sub pdb_get_data {
$blurb = 'Returns data associated with the specified identifier.';
@ -1359,6 +1395,7 @@ CODE
pdb_set_file_proc_handles_remote
pdb_set_file_proc_handles_raw
pdb_set_file_proc_thumbnail_loader
pdb_set_batch_interpreter
pdb_get_data
pdb_get_data_size
pdb_set_data);