/* PIKA - Photo and Image Kooker Application
 * a rebranding of The GNU Image Manipulation Program (created with heckimp)
 * A derived work which may be trivial. However, any changes may be (C)2023 by Aldercone Studio
 *
 * Original copyright, applying to most contents (license remains unchanged): 
 * Copyright (C) 1995-2003 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/>.
 */

#include "config.h"

#include <string.h>

#include <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pika.h"
#include "core/pikabrush.h"
#include "core/pikabrushgenerated.h"
#include "core/pikachannel.h"
#include "core/pikacontainer.h"
#include "core/pikadatafactory.h"
#include "core/pikadrawable.h"
#include "core/pikadynamics.h"
#include "core/pikagradient.h"
#include "core/pikaimage.h"
#include "core/pikaimage-guides.h"
#include "core/pikaimage-sample-points.h"
#include "core/pikaitem.h"
#include "core/pikamybrush.h"
#include "core/pikapalette.h"
#include "core/pikapattern.h"

#include "text/pikatextlayer.h"
#include "text/pikafont.h"

#include "vectors/pikavectors.h"

#include "pikapdb-utils.h"
#include "pikapdberror.h"

#include "pika-intl.h"


typedef struct
{
  const gchar *name;
  const gchar *collection;
  gboolean     is_internal;
} SearchData;


static PikaResource * pika_pdb_get_data_factory_item    (Pika         *pika,
                                                         GType         data_type,
                                                         const gchar  *name,
                                                         const gchar  *collection,
                                                         gboolean      is_internal);
const gchar *         pika_pdb_get_data_label           (GType         data_type);

static gboolean       pika_pdb_search_in_data_container (PikaData     *data,
                                                         SearchData   *search_data);;


PikaDataFactory *
pika_pdb_get_data_factory (Pika  *pika,
                           GType  data_type)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (g_type_is_a (data_type, PIKA_TYPE_DATA), NULL);

  if (g_type_is_a (data_type, PIKA_TYPE_BRUSH_GENERATED))
    return pika->brush_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_BRUSH))
    return pika->brush_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_PATTERN))
    return pika->pattern_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_GRADIENT))
    return pika->gradient_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_PALETTE))
    return pika->palette_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_FONT))
    return pika->font_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_DYNAMICS))
    return pika->dynamics_factory;
  else if (g_type_is_a (data_type, PIKA_TYPE_MYBRUSH))
    return pika->mybrush_factory;

  /* If we reach this, it means we forgot a data factory in our list! */
  g_return_val_if_reached (NULL);
}

GList *
pika_pdb_get_resources (Pika               *pika,
                        GType               data_type,
                        const gchar        *name,
                        PikaPDBDataAccess   access,
                        GError            **error)
{
  GList           *resources;
  PikaDataFactory *factory;
  PikaContainer   *container;
  const gchar     *label;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  label = pika_pdb_get_data_label (data_type);

  if (! name || ! strlen (name))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: %s is a data label from the
                    * PDB-error-data-label context.
                    */
                   C_("PDB-error-message", "%s name cannot be empty"),
                   g_type_name (data_type));
      return NULL;
    }

  factory = pika_pdb_get_data_factory (pika, data_type);
  g_return_val_if_fail (PIKA_IS_DATA_FACTORY (factory), NULL);

  container = pika_data_factory_get_container (factory);
  resources = pika_container_get_children_by_name (container, name);

  if (! resources && ! strcmp (name, "Standard"))
    {
      PikaData *resource;

      resource = pika_data_factory_data_get_standard (factory, pika_get_user_context (pika));
      g_return_val_if_fail (resource != NULL, NULL);
      resources = g_list_prepend (NULL, resource);
    }

  if (! resources)
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' not found"), label, name);
    }
  else if ((access & PIKA_PDB_DATA_ACCESS_WRITE) ||
           (access & PIKA_PDB_DATA_ACCESS_RENAME))
    {
      for (GList *iter = resources; iter; iter = iter->next)
        {
          if ((access & PIKA_PDB_DATA_ACCESS_WRITE) &&
              ! pika_data_is_writable (PIKA_DATA (iter->data)))
            {
              g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                           /* TRANSLATOR: the first %s is a data label from the
                            * PDB-error-data-label context. The second %s is a data
                            * name.
                            */
                           C_("PDB-error-message", "%s '%s' is not editable"), label, name);
              return NULL;
            }
          else if ((access & PIKA_PDB_DATA_ACCESS_RENAME) &&
                   ! pika_viewable_is_name_editable (PIKA_VIEWABLE (iter->data)))
            {
              g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                           /* TRANSLATOR: the first %s is a data label from the
                            * PDB-error-data-label context. The second %s is a data
                            * name.
                            */
                           C_("PDB-error-message", "%s '%s' is not renamable"), label, name);
              return NULL;
            }
        }
    }

  return resources;
}

PikaResource *
pika_pdb_get_resource (Pika               *pika,
                       GType               data_type,
                       const gchar        *name,
                       PikaPDBDataAccess   access,
                       GError            **error)
{
  PikaResource *resource;
  const gchar  *label;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  label = pika_pdb_get_data_label (data_type);

  if (! name || ! strlen (name))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: %s is a data label from the
                    * PDB-error-data-label context.
                    */
                   C_("PDB-error-message", "%s name cannot be empty"),
                   g_type_name (data_type));
      return NULL;
    }

  resource = pika_pdb_get_data_factory_item (pika, data_type, name, NULL, TRUE);

  if (! resource)
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' not found"), label, name);
    }
  else if ((access & PIKA_PDB_DATA_ACCESS_WRITE) &&
           ! pika_data_is_writable (PIKA_DATA (resource)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' is not editable"), label, name);
      return NULL;
    }
  else if ((access & PIKA_PDB_DATA_ACCESS_RENAME) &&
           ! pika_viewable_is_name_editable (PIKA_VIEWABLE (resource)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' is not renamable"), label, name);
      return NULL;
    }

  return resource;
}

PikaResource *
pika_pdb_get_resource_by_id (Pika               *pika,
                             GType               data_type,
                             const gchar        *name,
                             const gchar        *collection,
                             gboolean            is_internal,
                             PikaPDBDataAccess   access,
                             GError            **error)
{
  PikaResource *resource;
  const gchar  *label;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  label = pika_pdb_get_data_label (data_type);

  if (! name || ! strlen (name))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: %s is a data label from the
                    * PDB-error-data-label context.
                    */
                   C_("PDB-error-message", "%s name cannot be empty"),
                   g_type_name (data_type));
      return NULL;
    }

  resource = pika_pdb_get_data_factory_item (pika, data_type, name, collection, is_internal);

  if (! resource)
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' not found"), label, name);
    }
  else if ((access & PIKA_PDB_DATA_ACCESS_WRITE) &&
           ! pika_data_is_writable (PIKA_DATA (resource)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' is not editable"), label, name);
      return NULL;
    }
  else if ((access & PIKA_PDB_DATA_ACCESS_RENAME) &&
           ! pika_viewable_is_name_editable (PIKA_VIEWABLE (resource)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   /* TRANSLATOR: the first %s is a data label from the
                    * PDB-error-data-label context. The second %s is a data
                    * name.
                    */
                   C_("PDB-error-message", "%s '%s' is not renamable"), label, name);
      return NULL;
    }

  return resource;
}

PikaBrush *
pika_pdb_get_generated_brush (Pika               *pika,
                              const gchar        *name,
                              PikaPDBDataAccess   access,
                              GError            **error)
{
  PikaBrush *brush;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  brush = PIKA_BRUSH (pika_pdb_get_resource (pika, PIKA_TYPE_BRUSH_GENERATED, name, access, error));

  if (brush != NULL && ! PIKA_IS_BRUSH_GENERATED (brush))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Brush '%s' is not a generated brush"), name);
      return NULL;
    }

  return brush;
}

PikaBuffer *
pika_pdb_get_buffer (Pika         *pika,
                     const gchar  *name,
                     GError      **error)
{
  PikaBuffer *buffer;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
      g_set_error_literal (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                           _("Invalid empty buffer name"));
      return NULL;
    }

  buffer = (PikaBuffer *)
    pika_container_get_child_by_name (pika->named_buffers, name);

  if (! buffer)
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Named buffer '%s' not found"), name);
    }

  return buffer;
}

PikaPaintInfo *
pika_pdb_get_paint_info (Pika         *pika,
                         const gchar  *name,
                         GError      **error)
{
  PikaPaintInfo *paint_info;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! name || ! strlen (name))
    {
      g_set_error_literal (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                           _("Invalid empty paint method name"));
      return NULL;
    }

  paint_info = (PikaPaintInfo *)
    pika_container_get_child_by_name (pika->paint_info_list, name);

  if (! paint_info)
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Paint method '%s' does not exist"), name);
    }

  return paint_info;
}

gboolean
pika_pdb_item_is_attached (PikaItem           *item,
                           PikaImage          *image,
                           PikaPDBItemModify   modify,
                           GError            **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (image == NULL || PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! pika_item_is_attached (item))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be used because it has not "
                     "been added to an image"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  if (image && image != pika_item_get_image (item))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be used because it is "
                     "attached to another image"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  return pika_pdb_item_is_modifiable (item, modify, error);
}

gboolean
pika_pdb_item_is_in_tree (PikaItem           *item,
                          PikaImage          *image,
                          PikaPDBItemModify   modify,
                          GError            **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (image == NULL || PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! pika_pdb_item_is_attached (item, image, modify, error))
    return FALSE;

  if (! pika_item_get_tree (item))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be used because it is not "
                     "a direct child of an item tree"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_item_is_in_same_tree (PikaItem   *item,
                               PikaItem   *item2,
                               PikaImage  *image,
                               GError    **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (PIKA_IS_ITEM (item2), FALSE);
  g_return_val_if_fail (image == NULL || PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! pika_pdb_item_is_in_tree (item, image, FALSE, error) ||
      ! pika_pdb_item_is_in_tree (item2, image, FALSE, error))
    return FALSE;

  if (pika_item_get_tree (item) != pika_item_get_tree (item2))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Items '%s' (%d) and '%s' (%d) cannot be used "
                     "because they are not part of the same item tree"),
                   pika_object_get_name (item),
                   pika_item_get_id (item),
                   pika_object_get_name (item2),
                   pika_item_get_id (item2));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_item_is_not_ancestor (PikaItem  *item,
                               PikaItem  *not_descendant,
                               GError   **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (PIKA_IS_ITEM (not_descendant), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (pika_viewable_is_ancestor (PIKA_VIEWABLE (item),
                                 PIKA_VIEWABLE (not_descendant)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) must not be an ancestor of "
                     "'%s' (%d)"),
                   pika_object_get_name (item),
                   pika_item_get_id (item),
                   pika_object_get_name (not_descendant),
                   pika_item_get_id (not_descendant));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_item_is_floating (PikaItem  *item,
                           PikaImage *dest_image,
                           GError   **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (PIKA_IS_IMAGE (dest_image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! g_object_is_floating (item))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) has already been added to an image"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }
  else if (pika_item_get_image (item) != dest_image)
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Trying to add item '%s' (%d) to wrong image"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_item_is_modifiable (PikaItem           *item,
                             PikaPDBItemModify   modify,
                             GError            **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  /*  When a channel is position-locked, it is also implicitly
   *  content-locked because we translate channels by modifying their
   *  pixels.
   */
  if ((modify & PIKA_PDB_ITEM_POSITION) && PIKA_IS_CHANNEL (item))
    modify |= PIKA_PDB_ITEM_CONTENT;

  if ((modify & PIKA_PDB_ITEM_CONTENT) && pika_item_is_content_locked (item, NULL))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be modified because its "
                     "contents are locked"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  if ((modify & PIKA_PDB_ITEM_POSITION) && pika_item_is_position_locked (item, NULL))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be modified because its "
                     "position and size are locked"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_item_is_group (PikaItem  *item,
                        GError   **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! pika_viewable_get_children (PIKA_VIEWABLE (item)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be used because it is "
                     "not a group item"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_item_is_not_group (PikaItem  *item,
                            GError   **error)
{
  g_return_val_if_fail (PIKA_IS_ITEM (item), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (pika_viewable_get_children (PIKA_VIEWABLE (item)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Item '%s' (%d) cannot be modified because it "
                     "is a group item"),
                   pika_object_get_name (item),
                   pika_item_get_id (item));
      return FALSE;
    }

  return TRUE;
}

gboolean
pika_pdb_layer_is_text_layer (PikaLayer          *layer,
                              PikaPDBItemModify   modify,
                              GError            **error)
{
  g_return_val_if_fail (PIKA_IS_LAYER (layer), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! pika_item_is_text_layer (PIKA_ITEM (layer)))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Layer '%s' (%d) cannot be used because it is not "
                     "a text layer"),
                   pika_object_get_name (layer),
                   pika_item_get_id (PIKA_ITEM (layer)));

      return FALSE;
    }

  return pika_pdb_item_is_attached (PIKA_ITEM (layer), NULL, modify, error);
}

static const gchar *
pika_pdb_enum_value_get_nick (GType enum_type,
                              gint  value)
{
  GEnumClass  *enum_class;
  GEnumValue  *enum_value;
  const gchar *nick;

  enum_class = g_type_class_ref (enum_type);
  enum_value = g_enum_get_value (enum_class, value);

  nick = enum_value->value_nick;

  g_type_class_unref (enum_class);

  return nick;
}

gboolean
pika_pdb_image_is_base_type (PikaImage          *image,
                             PikaImageBaseType   type,
                             GError            **error)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (pika_image_get_base_type (image) == type)
    return TRUE;

  g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
               _("Image '%s' (%d) is of type '%s', "
                 "but an image of type '%s' is expected"),
               pika_image_get_display_name (image),
               pika_image_get_id (image),
               pika_pdb_enum_value_get_nick (PIKA_TYPE_IMAGE_BASE_TYPE,
                                             pika_image_get_base_type (image)),
               pika_pdb_enum_value_get_nick (PIKA_TYPE_IMAGE_BASE_TYPE, type));

  return FALSE;
}

gboolean
pika_pdb_image_is_not_base_type (PikaImage          *image,
                                 PikaImageBaseType   type,
                                 GError            **error)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (pika_image_get_base_type (image) != type)
    return TRUE;

  g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
               _("Image '%s' (%d) must not be of type '%s'"),
               pika_image_get_display_name (image),
               pika_image_get_id (image),
               pika_pdb_enum_value_get_nick (PIKA_TYPE_IMAGE_BASE_TYPE, type));

  return FALSE;
}

gboolean
pika_pdb_image_is_precision (PikaImage      *image,
                             PikaPrecision   precision,
                             GError        **error)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (pika_image_get_precision (image) == precision)
    return TRUE;

  g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
               _("Image '%s' (%d) has precision '%s', "
                 "but an image of precision '%s' is expected"),
               pika_image_get_display_name (image),
               pika_image_get_id (image),
               pika_pdb_enum_value_get_nick (PIKA_TYPE_PRECISION,
                                             pika_image_get_precision (image)),
               pika_pdb_enum_value_get_nick (PIKA_TYPE_PRECISION, precision));

  return FALSE;
}

gboolean
pika_pdb_image_is_not_precision (PikaImage      *image,
                                 PikaPrecision   precision,
                                 GError        **error)
{
  g_return_val_if_fail (PIKA_IS_IMAGE (image), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (pika_image_get_precision (image) != precision)
    return TRUE;

  g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
               _("Image '%s' (%d) must not be of precision '%s'"),
               pika_image_get_display_name (image),
               pika_image_get_id (image),
               pika_pdb_enum_value_get_nick (PIKA_TYPE_PRECISION, precision));

  return FALSE;
}

PikaGuide *
pika_pdb_image_get_guide (PikaImage  *image,
                          gint        guide_id,
                          GError    **error)
{
  PikaGuide *guide;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  guide = pika_image_get_guide (image, guide_id);

  if (guide)
    return guide;

  g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
               _("Image '%s' (%d) does not contain guide with ID %d"),
               pika_image_get_display_name (image),
               pika_image_get_id (image),
               guide_id);
  return NULL;
}

PikaSamplePoint *
pika_pdb_image_get_sample_point (PikaImage  *image,
                                 gint        sample_point_id,
                                 GError    **error)
{
  PikaSamplePoint *sample_point;

  g_return_val_if_fail (PIKA_IS_IMAGE (image), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  sample_point = pika_image_get_sample_point (image, sample_point_id);

  if (sample_point)
    return sample_point;

  g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
               _("Image '%s' (%d) does not contain sample point with ID %d"),
               pika_image_get_display_name (image),
               pika_image_get_id (image),
               sample_point_id);
  return NULL;
}

PikaStroke *
pika_pdb_get_vectors_stroke (PikaVectors        *vectors,
                             gint                stroke_id,
                             PikaPDBItemModify   modify,
                             GError            **error)
{
  PikaStroke *stroke = NULL;

  g_return_val_if_fail (PIKA_IS_VECTORS (vectors), NULL);
  g_return_val_if_fail (error == NULL || *error == NULL, NULL);

  if (! pika_pdb_item_is_not_group (PIKA_ITEM (vectors), error))
    return NULL;

  if (! modify || pika_pdb_item_is_modifiable (PIKA_ITEM (vectors),
                                               modify, error))
    {
      stroke = pika_vectors_stroke_get_by_id (vectors, stroke_id);

      if (! stroke)
        g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                     _("Vectors object %d does not contain stroke with ID %d"),
                     pika_item_get_id (PIKA_ITEM (vectors)), stroke_id);
    }

  return stroke;
}

gboolean
pika_pdb_is_canonical_procedure (const gchar  *procedure_name,
                                 GError      **error)
{
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  if (! pika_is_canonical_identifier (procedure_name))
    {
      g_set_error (error, PIKA_PDB_ERROR, PIKA_PDB_ERROR_INVALID_ARGUMENT,
                   _("Procedure name '%s' is not a canonical identifier"),
                   procedure_name);
      return FALSE;
    }

  return TRUE;
}


/* Private functions. */

static PikaResource *
pika_pdb_get_data_factory_item (Pika        *pika,
                                GType        data_type,
                                const gchar *name,
                                const gchar *collection,
                                gboolean     is_internal)
{
  PikaDataFactory *factory;
  PikaContainer   *container;
  PikaObject      *resource;

  factory = pika_pdb_get_data_factory (pika, data_type);
  g_return_val_if_fail (PIKA_IS_DATA_FACTORY (factory), NULL);

  container = pika_data_factory_get_container (factory);

  if (collection == NULL)
    {
      resource = pika_container_get_child_by_name (container, name);
    }
  else
    {
      SearchData *data = g_new (SearchData, 1);

      data->name        = name;
      data->collection  = collection;
      data->is_internal = is_internal;
      resource = pika_container_search (container,
                                        (PikaContainerSearchFunc) pika_pdb_search_in_data_container,
                                        data);
      g_free (data);
    }

  if (! resource)
    resource = pika_container_get_child_by_name (pika_data_factory_get_container_obsolete (factory),
                                                 name);

  if (! resource && ! strcmp (name, "Standard"))
    resource = (PikaObject *) pika_data_factory_data_get_standard (factory,
                                                                   pika_get_user_context (pika));

  return (PikaResource *) resource;
}

const gchar *
pika_pdb_get_data_label (GType data_type)
{
  g_return_val_if_fail (g_type_is_a (data_type, PIKA_TYPE_DATA), NULL);

  if (g_type_is_a (data_type, PIKA_TYPE_BRUSH_GENERATED))
    return C_("PDB-error-data-label", "Generated brush");
  else if (g_type_is_a (data_type, PIKA_TYPE_BRUSH))
    return C_("PDB-error-data-label", "Brush");
  else if (g_type_is_a (data_type, PIKA_TYPE_PATTERN))
    return C_("PDB-error-data-label", "Pattern");
  else if (g_type_is_a (data_type, PIKA_TYPE_GRADIENT))
    return C_("PDB-error-data-label", "Gradient");
  else if (g_type_is_a (data_type, PIKA_TYPE_PALETTE))
    return C_("PDB-error-data-label", "Palette");
  else if (g_type_is_a (data_type, PIKA_TYPE_FONT))
    return C_("PDB-error-data-label", "Font");
  else if (g_type_is_a (data_type, PIKA_TYPE_DYNAMICS))
    return C_("PDB-error-data-label", "Paint dynamics");
  else if (g_type_is_a (data_type, PIKA_TYPE_MYBRUSH))
    return C_("PDB-error-data-label", "MyPaint brush");

  /* If we reach this, it means we forgot a data type in our list! */
  g_return_val_if_reached (NULL);
}

static gboolean
pika_pdb_search_in_data_container (PikaData   *data,
                                   SearchData *search_data)
{
  return pika_data_identify (data, search_data->name, search_data->collection, search_data->is_internal);
}
