/* 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/>.
 */

/* NOTE: This file is auto-generated by pdbgen.pl. */

#include "config.h"

#include "stamp-pdbgen.h"

#include <string.h>

#include <gegl.h>

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

#include "libpikabase/pikabase.h"

#include "pdb-types.h"

#include "core/pika.h"
#include "core/pikaimage.h"
#include "core/pikalayer.h"
#include "core/pikaparamspecs.h"
#include "file/file-open.h"
#include "file/file-save.h"
#include "file/file-utils.h"
#include "plug-in/pikapluginmanager-file.h"

#include "pikapdb.h"
#include "pikaprocedure.h"
#include "internal-procs.h"


static PikaValueArray *
file_load_invoker (PikaProcedure         *procedure,
                   Pika                  *pika,
                   PikaContext           *context,
                   PikaProgress          *progress,
                   const PikaValueArray  *args,
                   GError               **error)
{
  PikaValueArray      *new_args;
  PikaValueArray      *return_vals;
  PikaPlugInProcedure *file_proc;
  PikaProcedure       *proc;
  GFile               *file;
  gint                 i;

  file = g_value_get_object (pika_value_array_index (args, 1));

  if (! file)
    return pika_procedure_get_return_values (procedure, FALSE,
                                             error ? *error : NULL);

  file_proc = pika_plug_in_manager_file_procedure_find (pika->plug_in_manager,
                                                        PIKA_FILE_PROCEDURE_GROUP_OPEN,
                                                        file, error);

  if (! file_proc)
    return pika_procedure_get_return_values (procedure, FALSE,
                                             error ? *error : NULL);

  proc = PIKA_PROCEDURE (file_proc);

  new_args = pika_procedure_get_arguments (proc);

  g_value_transform (pika_value_array_index (args, 0),
                     pika_value_array_index (new_args, 0));
  g_value_transform (pika_value_array_index (args, 1),
                     pika_value_array_index (new_args, 1));

  for (i = 2; i < proc->num_args; i++)
    if (G_IS_PARAM_SPEC_STRING (proc->args[i]))
      g_value_set_static_string (pika_value_array_index (new_args, i), "");

  return_vals =
    pika_pdb_execute_procedure_by_name_args (pika->pdb,
                                             context, progress, error,
                                             pika_object_get_name (proc),
                                             new_args);

  pika_value_array_unref (new_args);

  if (g_value_get_enum (pika_value_array_index (return_vals, 0)) ==
      PIKA_PDB_SUCCESS)
    {
      if (pika_value_array_length (return_vals) > 1 &&
          PIKA_VALUE_HOLDS_IMAGE (pika_value_array_index (return_vals, 1)))
        {
          PikaImage *image =
            g_value_get_object (pika_value_array_index (return_vals, 1));
          pika_image_set_load_proc (image, file_proc);
        }
    }

  return return_vals;
}

static PikaValueArray *
file_load_layer_invoker (PikaProcedure         *procedure,
                         Pika                  *pika,
                         PikaContext           *context,
                         PikaProgress          *progress,
                         const PikaValueArray  *args,
                         GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  gint run_mode;
  PikaImage *image;
  GFile *file;
  PikaLayer *layer = NULL;

  run_mode = g_value_get_enum (pika_value_array_index (args, 0));
  image = g_value_get_object (pika_value_array_index (args, 1));
  file = g_value_get_object (pika_value_array_index (args, 2));

  if (success)
    {
      GList             *layers;
      PikaPDBStatusType  status;

      layers = file_open_layers (pika, context, progress,
                                 image, FALSE,
                                 file, run_mode, NULL, &status, error);

      if (layers)
        {
          layer = layers->data;
          g_list_free (layers);
        }
      else
        success = FALSE;
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    g_value_set_object (pika_value_array_index (return_vals, 1), layer);

  return return_vals;
}

static PikaValueArray *
file_load_layers_invoker (PikaProcedure         *procedure,
                          Pika                  *pika,
                          PikaContext           *context,
                          PikaProgress          *progress,
                          const PikaValueArray  *args,
                          GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  gint run_mode;
  PikaImage *image;
  GFile *file;
  gint num_layers = 0;
  PikaLayer **layers = NULL;

  run_mode = g_value_get_enum (pika_value_array_index (args, 0));
  image = g_value_get_object (pika_value_array_index (args, 1));
  file = g_value_get_object (pika_value_array_index (args, 2));

  if (success)
    {
      GList             *layer_list;
      PikaPDBStatusType  status;

      layer_list = file_open_layers (pika, context, progress,
                                     image, FALSE,
                                     file, run_mode, NULL, &status, error);

      if (layer_list)
        {
          GList *list;
          gint i;

          num_layers = g_list_length (layer_list);

          layers = g_new (PikaLayer *, num_layers);

          for (i = 0, list = layer_list;
               i < num_layers;
               i++, list = g_list_next (list))
            {
              layers[i] = g_object_ref (list->data);
            }

          g_list_free (layer_list);
        }
      else
        success = FALSE;
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    {
      g_value_set_int (pika_value_array_index (return_vals, 1), num_layers);
      pika_value_take_object_array (pika_value_array_index (return_vals, 2), PIKA_TYPE_LAYER, (GObject **) layers, num_layers);
    }

  return return_vals;
}

static PikaValueArray *
file_save_invoker (PikaProcedure         *procedure,
                   Pika                  *pika,
                   PikaContext           *context,
                   PikaProgress          *progress,
                   const PikaValueArray  *args,
                   GError               **error)
{
  PikaValueArray      *new_args;
  PikaValueArray      *return_vals;
  PikaPlugInProcedure *file_proc;
  GFile               *file;
  PikaProcedure       *proc;
  gint                 i;

  file = g_value_get_object (pika_value_array_index (args, 4));

  file_proc = pika_plug_in_manager_file_procedure_find (pika->plug_in_manager,
                                                        PIKA_FILE_PROCEDURE_GROUP_SAVE,
                                                        file, NULL);

  if (! file_proc)
    file_proc = pika_plug_in_manager_file_procedure_find (pika->plug_in_manager,
                                                          PIKA_FILE_PROCEDURE_GROUP_EXPORT,
                                                          file, error);

  if (! file_proc)
    return pika_procedure_get_return_values (procedure, FALSE,
                                             error ? *error : NULL);

  proc = PIKA_PROCEDURE (file_proc);

  new_args = pika_procedure_get_arguments (proc);

  g_value_transform (pika_value_array_index (args, 0),
                     pika_value_array_index (new_args, 0));
  g_value_transform (pika_value_array_index (args, 1),
                     pika_value_array_index (new_args, 1));
  g_value_transform (pika_value_array_index (args, 2),
                     pika_value_array_index (new_args, 2));
  g_value_transform (pika_value_array_index (args, 3),
                     pika_value_array_index (new_args, 3));
  g_value_transform (pika_value_array_index (args, 4),
                     pika_value_array_index (new_args, 4));

  for (i = 5; i < proc->num_args; i++)
    if (G_IS_PARAM_SPEC_STRING (proc->args[i]))
      g_value_set_static_string (pika_value_array_index (new_args, i), "");

  return_vals =
    pika_pdb_execute_procedure_by_name_args (pika->pdb,
                                             context, progress, error,
                                             pika_object_get_name (proc),
                                             new_args);

  pika_value_array_unref (new_args);

  return return_vals;
}

static PikaValueArray *
file_load_thumbnail_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaValueArray *return_vals;
  GFile *file;
  gint width = 0;
  gint height = 0;
  GBytes *thumb_data = NULL;

  file = g_value_get_object (pika_value_array_index (args, 0));

  if (success)
    {
      GdkPixbuf *pixbuf = file_utils_load_thumbnail (file);

      if (pixbuf)
        {
          width            = gdk_pixbuf_get_width (pixbuf);
          height           = gdk_pixbuf_get_height (pixbuf);
          thumb_data       = g_bytes_new (gdk_pixbuf_get_pixels (pixbuf),
                                          3 * width * height);

          g_object_unref (pixbuf);
        }
      else
        success = FALSE;
    }

  return_vals = pika_procedure_get_return_values (procedure, success,
                                                  error ? *error : NULL);

  if (success)
    {
      g_value_set_int (pika_value_array_index (return_vals, 1), width);
      g_value_set_int (pika_value_array_index (return_vals, 2), height);
      g_value_take_boxed (pika_value_array_index (return_vals, 3), thumb_data);
    }

  return return_vals;
}

static PikaValueArray *
file_save_thumbnail_invoker (PikaProcedure         *procedure,
                             Pika                  *pika,
                             PikaContext           *context,
                             PikaProgress          *progress,
                             const PikaValueArray  *args,
                             GError               **error)
{
  gboolean success = TRUE;
  PikaImage *image;
  GFile *file;

  image = g_value_get_object (pika_value_array_index (args, 0));
  file = g_value_get_object (pika_value_array_index (args, 1));

  if (success)
    {
      success = file_utils_save_thumbnail (image, file);
    }

  return pika_procedure_get_return_values (procedure, success,
                                           error ? *error : NULL);
}

void
register_file_procs (PikaPDB *pdb)
{
  PikaProcedure *procedure;

  /*
   * pika-file-load
   */
  procedure = pika_procedure_new (file_load_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-file-load");
  pika_procedure_set_static_help (procedure,
                                  "Loads an image file by invoking the right load handler.",
                                  "This procedure invokes the correct file load handler using magic if possible, and falling back on the file's extension and/or prefix if not.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Josh MacDonald",
                                         "Josh MacDonald",
                                         "1997");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "run mode",
                                                     "The run mode",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_param_spec_enum_exclude_value (PIKA_PARAM_SPEC_ENUM (procedure->args[0]),
                                      PIKA_RUN_WITH_LAST_VALS);
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "file",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_image ("image",
                                                          "image",
                                                          "The output image",
                                                          FALSE,
                                                          PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-file-load-layer
   */
  procedure = pika_procedure_new (file_load_layer_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-file-load-layer");
  pika_procedure_set_static_help (procedure,
                                  "Loads an image file as a layer for an existing image.",
                                  "This procedure behaves like the file-load procedure but opens the specified image as a layer for an existing image. The returned layer needs to be added to the existing image with 'pika-image-insert-layer'.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Sven Neumann <sven@gimp.org>",
                                         "Sven Neumann",
                                         "2005");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "run mode",
                                                     "The run mode",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_param_spec_enum_exclude_value (PIKA_PARAM_SPEC_ENUM (procedure->args[0]),
                                      PIKA_RUN_WITH_LAST_VALS);
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "Destination image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "file",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_layer ("layer",
                                                          "layer",
                                                          "The layer created when loading the image file",
                                                          FALSE,
                                                          PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-file-load-layers
   */
  procedure = pika_procedure_new (file_load_layers_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-file-load-layers");
  pika_procedure_set_static_help (procedure,
                                  "Loads an image file as layers for an existing image.",
                                  "This procedure behaves like the file-load procedure but opens the specified image as layers for an existing image. The returned layers needs to be added to the existing image with 'pika-image-insert-layer'.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Michael Natterer <mitch@gimp.org>",
                                         "Michael Natterer",
                                         "2006");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_enum ("run-mode",
                                                     "run mode",
                                                     "The run mode",
                                                     PIKA_TYPE_RUN_MODE,
                                                     PIKA_RUN_INTERACTIVE,
                                                     PIKA_PARAM_READWRITE));
  pika_param_spec_enum_exclude_value (PIKA_PARAM_SPEC_ENUM (procedure->args[0]),
                                      PIKA_RUN_WITH_LAST_VALS);
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "Destination image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "file",
                                                    "The file to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("num-layers",
                                                     "num layers",
                                                     "The number of loaded layers",
                                                     0, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   pika_param_spec_object_array ("layers",
                                                                 "layers",
                                                                 "The list of loaded layers",
                                                                 PIKA_TYPE_LAYER,
                                                                 PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-file-save
   */
  procedure = pika_procedure_new (file_save_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-file-save");
  pika_procedure_set_static_help (procedure,
                                  "Saves a file by extension.",
                                  "This procedure invokes the correct file save handler according to the file's extension and/or prefix.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Josh MacDonald",
                                         "Josh MacDonald",
                                         "1997");
  pika_procedure_add_argument (procedure,
                               g_param_spec_enum ("run-mode",
                                                  "run mode",
                                                  "The run mode",
                                                  PIKA_TYPE_RUN_MODE,
                                                  PIKA_RUN_INTERACTIVE,
                                                  PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "Input image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_int ("num-drawables",
                                                 "num drawables",
                                                 "The number of drawables to save",
                                                 1, G_MAXINT32, 1,
                                                 PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               pika_param_spec_object_array ("drawables",
                                                             "drawables",
                                                             "Drawables to save",
                                                             PIKA_TYPE_ITEM,
                                                             PIKA_PARAM_READWRITE | PIKA_PARAM_NO_VALIDATE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "file",
                                                    "The file to save the image in",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-file-load-thumbnail
   */
  procedure = pika_procedure_new (file_load_thumbnail_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-file-load-thumbnail");
  pika_procedure_set_static_help (procedure,
                                  "Loads the thumbnail for a file.",
                                  "This procedure tries to load a thumbnail that belongs to the given file. The returned data is an array of colordepth 3 (RGB), regardless of the image type. Width and height of the thumbnail are also returned. Don't use this function if you need a thumbnail of an already opened image, use 'pika-image-thumbnail' instead.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Adam D. Moss, Sven Neumann",
                                         "Adam D. Moss, Sven Neumann",
                                         "1999-2003");
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "file",
                                                    "The file that owns the thumbnail to load",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("width",
                                                     "width",
                                                     "The width of the thumbnail",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_int ("height",
                                                     "height",
                                                     "The height of the thumbnail",
                                                     G_MININT32, G_MAXINT32, 0,
                                                     PIKA_PARAM_READWRITE));
  pika_procedure_add_return_value (procedure,
                                   g_param_spec_boxed ("thumb-data",
                                                       "thumb data",
                                                       "The thumbnail data",
                                                       G_TYPE_BYTES,
                                                       PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);

  /*
   * pika-file-save-thumbnail
   */
  procedure = pika_procedure_new (file_save_thumbnail_invoker);
  pika_object_set_static_name (PIKA_OBJECT (procedure),
                               "pika-file-save-thumbnail");
  pika_procedure_set_static_help (procedure,
                                  "Saves a thumbnail for the given image",
                                  "This procedure saves a thumbnail for the given image according to the Free Desktop Thumbnail Managing Standard. The thumbnail is saved so that it belongs to the given file. This means you have to save the image under this name first, otherwise this procedure will fail. This procedure may become useful if you want to explicitly save a thumbnail with a file.",
                                  NULL);
  pika_procedure_set_static_attribution (procedure,
                                         "Josh MacDonald",
                                         "Josh MacDonald",
                                         "1997");
  pika_procedure_add_argument (procedure,
                               pika_param_spec_image ("image",
                                                      "image",
                                                      "The image",
                                                      FALSE,
                                                      PIKA_PARAM_READWRITE));
  pika_procedure_add_argument (procedure,
                               g_param_spec_object ("file",
                                                    "file",
                                                    "The file the thumbnail belongs to",
                                                    G_TYPE_FILE,
                                                    PIKA_PARAM_READWRITE));
  pika_pdb_register_procedure (pdb, procedure);
  g_object_unref (procedure);
}
