Gimp/pdb/groups/resource.pdb
Jehan d439e9ff5c app, libgimp, pdb: factorize a bunch of similar code into gimp_pdb_get_resource().
Rather than reimplementing the same checks for every possible resource data
type, just do it once and redirect to the correct factory container.

For the libgimp API, we leave per-type functions `gimp_*_get_by_name()` (where *
can be brush|gradient|font|palette|pattern so far), but internally they all use
gimp_pdb_get_resource().

Note that eventually we want these functions to return a list of resources as it
should be possible to have several resources of a given type with the same name
(since they are made by third-party who might have had the same idea of a name).
2023-07-27 15:25:32 +02:00

419 lines
9.4 KiB
Text

# GIMP - The GNU Image Manipulation Program
# Copyright (C) 1995 Spencer Kimball and Peter Mattis
# 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/>.
# "Perlized" from C source by Manish Singh <yosh@gimp.org>
sub resource_get_by_name {
$blurb = "Returns a resource with the given name.";
$help = "Returns a resource with the given name.";
&jehan_pdb_misc('2023', '3.0');
$lib_private = 1;
@inargs = (
{ name => 'type_name', type => 'string', non_empty => 1,
desc => 'The name of the resource type' },
{ name => 'resource_name', type => 'string', non_empty => 1,
desc => 'The name of the resource' }
);
@outargs = (
{ name => 'resource',
type => 'resource',
desc => "The resource" }
);
%invoke = (
code => <<'CODE'
{
resource = gimp_pdb_get_resource (gimp, g_type_from_name (type_name), resource_name,
GIMP_PDB_DATA_ACCESS_READ, error);
if (! resource)
success = FALSE;
}
CODE
);
}
sub resource_id_is_valid {
$blurb = 'Returns TRUE if the resource ID is valid.';
$help = <<'HELP';
This procedure checks if the given resource ID is valid and refers to an
existing resource.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource_id', type => 'int32',
desc => 'The resource ID to check' }
);
@outargs = (
{ name => 'valid', type => 'boolean',
desc => 'Whether the resource ID is valid' }
);
%invoke = (
code => <<'CODE'
{
GimpData *data = gimp_data_get_by_id (resource_id);
valid = GIMP_IS_DATA (data);
}
CODE
);
}
sub resource_id_is_brush {
$blurb = 'Returns whether the resource ID is a brush.';
$help = <<HELP;
This procedure returns TRUE if the specified resource ID is a brush.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource_id', type => 'int32',
desc => 'The resource ID' }
);
@outargs = (
{ name => 'brush', type => 'boolean',
desc => 'TRUE if the resource ID is a brush, FALSE otherwise' }
);
%invoke = (
code => <<'CODE'
{
GimpData *data = gimp_data_get_by_id (resource_id);
brush = GIMP_IS_BRUSH (data);
}
CODE
);
}
sub resource_id_is_pattern {
$blurb = 'Returns whether the resource ID is a pattern.';
$help = <<HELP;
This procedure returns TRUE if the specified resource ID is a pattern.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource_id', type => 'int32',
desc => 'The resource ID' }
);
@outargs = (
{ name => 'pattern', type => 'boolean',
desc => 'TRUE if the resource ID is a pattern, FALSE otherwise' }
);
%invoke = (
code => <<'CODE'
{
GimpData *data = gimp_data_get_by_id (resource_id);
pattern = GIMP_IS_PATTERN (data);
}
CODE
);
}
sub resource_id_is_gradient {
$blurb = 'Returns whether the resource ID is a gradient.';
$help = <<HELP;
This procedure returns TRUE if the specified resource ID is a gradient.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource_id', type => 'int32',
desc => 'The resource ID' }
);
@outargs = (
{ name => 'gradient', type => 'boolean',
desc => 'TRUE if the resource ID is a gradient, FALSE otherwise' }
);
%invoke = (
code => <<'CODE'
{
GimpData *data = gimp_data_get_by_id (resource_id);
gradient = GIMP_IS_GRADIENT (data);
}
CODE
);
}
sub resource_id_is_palette {
$blurb = 'Returns whether the resource ID is a palette.';
$help = <<HELP;
This procedure returns TRUE if the specified resource ID is a palette.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource_id', type => 'int32',
desc => 'The resource ID' }
);
@outargs = (
{ name => 'palette', type => 'boolean',
desc => 'TRUE if the resource ID is a palette, FALSE otherwise' }
);
%invoke = (
code => <<'CODE'
{
GimpData *data = gimp_data_get_by_id (resource_id);
palette = GIMP_IS_PALETTE (data);
}
CODE
);
}
sub resource_id_is_font {
$blurb = 'Returns whether the resource ID is a font.';
$help = <<HELP;
This procedure returns TRUE if the specified resource ID is a font.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource_id', type => 'int32',
desc => 'The resource ID' }
);
@outargs = (
{ name => 'font', type => 'boolean',
desc => 'TRUE if the resource ID is a font, FALSE otherwise' }
);
%invoke = (
code => <<'CODE'
{
GimpData *data = gimp_data_get_by_id (resource_id);
font = GIMP_IS_FONT (data);
}
CODE
);
}
sub resource_get_name {
$blurb = "Returns the resource's name.";
$help = <<HELP;
This procedure returns the resource's name.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource', type => 'resource',
desc => 'The resource' }
);
@outargs = (
{ name => 'name', type => 'string',
desc => "The resource's name" }
);
%invoke = (
code => <<'CODE'
{
name = g_strdup (gimp_object_get_name (GIMP_OBJECT (resource)));
}
CODE
);
}
sub resource_is_editable {
$blurb = "Whether the resource can be edited.";
$help = "Returns TRUE if you have permission to change the resource.";
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource', type => 'resource',
desc => 'The resource' }
);
@outargs = (
{ name => 'editable', type => 'boolean',
desc => 'TRUE if the resource can be edited' }
);
%invoke = (
code => <<'CODE'
{
editable = gimp_data_is_writable (GIMP_DATA (resource));
}
CODE
);
}
sub resource_duplicate {
$blurb = "Duplicates a resource.";
$help = "Returns a copy having a different, unique ID.";
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource', type => 'resource',
desc => 'The resource' }
);
@outargs = (
{ name => 'resource_copy',
type => 'resource',
desc => "A copy of the resource." }
);
%invoke = (
code => <<'CODE'
{
GimpDataFactory *factory;
factory = gimp_pdb_get_data_factory (gimp, G_TYPE_FROM_INSTANCE (resource));
resource_copy = (GimpResource *)
gimp_data_factory_data_duplicate (factory, GIMP_DATA (resource));
if (! resource_copy)
success = FALSE;
}
CODE
);
}
sub resource_rename {
$blurb = "Renames a resource. When the name is in use, renames to a unique name.";
$help = <<'HELP';
Renames a resource. When the proposed name is already used, GIMP
generates a unique name.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource', type => 'resource',
desc => 'The resource' },
{ name => 'new_name', type => 'string', non_empty => 1,
desc => 'The proposed new name of the resource' }
);
%invoke = (
code => <<'CODE'
{
if (gimp_viewable_is_name_editable (GIMP_VIEWABLE (resource)))
{
gimp_object_set_name (GIMP_OBJECT (resource), new_name);
}
else
{
g_set_error (error, GIMP_PDB_ERROR, GIMP_PDB_ERROR_INVALID_ARGUMENT,
_("Resource '%s' is not renamable"),
gimp_object_get_name (GIMP_OBJECT (resource)));
success = FALSE;
}
}
CODE
);
}
sub resource_delete {
$blurb = "Deletes a resource.";
$help = <<'HELP';
Deletes a resource. Returns an error if the resource is not deletable.
Deletes the resource's data. You should not use the resource afterwards.
HELP
&mitch_pdb_misc('2023', '3.0');
@inargs = (
{ name => 'resource', type => 'resource',
desc => 'The resource' }
);
%invoke = (
code => <<'CODE'
{
GimpDataFactory *factory;
factory = gimp_pdb_get_data_factory (gimp, G_TYPE_FROM_INSTANCE (resource));
if (gimp_data_is_deletable (GIMP_DATA (resource)))
success = gimp_data_factory_data_delete (factory, GIMP_DATA (resource),
TRUE, error);
else
success = FALSE;
}
CODE
);
}
@headers = qw("core/gimpbrush.h"
"core/gimpdatafactory.h"
"core/gimpgradient.h"
"core/gimppalette.h"
"core/gimppattern.h"
"text/gimpfont.h"
"gimppdb-utils.h"
"gimppdberror.h"
"gimp-intl.h");
@procs = qw(resource_get_by_name
resource_id_is_valid
resource_id_is_brush
resource_id_is_pattern
resource_id_is_gradient
resource_id_is_palette
resource_id_is_font
resource_get_name
resource_is_editable
resource_duplicate
resource_rename
resource_delete);
%exports = (app => [@procs], lib => [@procs]);
$desc = 'Resource procedures';
$doc_title = 'gimpresource';
$doc_short_desc = 'Functions to manipulate resources.';
$doc_long_desc = 'Functions to manipulate resources.';
1;