/* 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 Spencer Kimball and Peter Mattis
 *
 * pikafileprocedure.c
 * Copyright (C) 2019 Michael Natterer <mitch@gimp.org>
 *
 * 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 "pika.h"
#include "pikafileprocedure.h"


typedef struct _PikaFileProcedurePrivate
{
  gchar    *format_name;
  gchar    *mime_types;
  gchar    *extensions;
  gchar    *prefixes;
  gchar    *magics;
  gint      priority;
  gboolean  handles_remote;
} PikaFileProcedurePrivate;


static void   pika_file_procedure_constructed (GObject *object);
static void   pika_file_procedure_finalize    (GObject *object);


G_DEFINE_ABSTRACT_TYPE_WITH_PRIVATE (PikaFileProcedure, pika_file_procedure,
                                     PIKA_TYPE_PROCEDURE)

#define parent_class pika_file_procedure_parent_class


static void
pika_file_procedure_class_init (PikaFileProcedureClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_file_procedure_constructed;
  object_class->finalize     = pika_file_procedure_finalize;
}

static void
pika_file_procedure_init (PikaFileProcedure *procedure)
{
}

static void
pika_file_procedure_constructed (GObject *object)
{
  PikaProcedure *procedure = PIKA_PROCEDURE (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  PIKA_PROC_ARG_ENUM (procedure, "run-mode",
                      "Run mode",
                      "The run mode",
                      PIKA_TYPE_RUN_MODE,
                      PIKA_RUN_NONINTERACTIVE,
                      G_PARAM_READWRITE);
}

static void
pika_file_procedure_finalize (GObject *object)
{
  PikaFileProcedure        *procedure;
  PikaFileProcedurePrivate *priv;

  procedure = PIKA_FILE_PROCEDURE (object);
  priv      = pika_file_procedure_get_instance_private (procedure);

  g_clear_pointer (&priv->format_name, g_free);
  g_clear_pointer (&priv->mime_types,  g_free);
  g_clear_pointer (&priv->extensions,  g_free);
  g_clear_pointer (&priv->prefixes,    g_free);
  g_clear_pointer (&priv->magics,      g_free);

  G_OBJECT_CLASS (parent_class)->finalize (object);
}


/*  public functions  */

/**
 * pika_file_procedure_set_format_name:
 * @procedure: A file procedure.
 * @format_name: A public-facing name for the format, e.g. "PNG".
 *
 * Associates a format name with a file handler procedure.
 *
 * This name can be used for any public-facing strings, such as
 * graphical interface labels. An example usage would be
 * %PikaSaveProcedureDialog title looking like "Export Image as %s".
 *
 * Note that since the format name is public-facing, it is recommended
 * to localize it at runtime, for instance through gettext, like:
 *
 * ```c
 * pika_file_procedure_set_format_name (procedure, _("JPEG"));
 * ```
 *
 * 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
pika_file_procedure_set_format_name (PikaFileProcedure *procedure,
                                     const gchar       *format_name)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  g_free (priv->format_name);
  priv->format_name = g_strdup (format_name);
}

/**
 * pika_file_procedure_get_format_name:
 * @procedure: A file procedure object.
 *
 * Returns the procedure's format name, as set with
 * [method@FileProcedure.set_format_name].
 *
 * Returns: The procedure's format name.
 *
 * Since: 3.0
 **/
const gchar *
pika_file_procedure_get_format_name (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), NULL);

  priv = pika_file_procedure_get_instance_private (procedure);

  return priv->format_name;
}

/**
 * pika_file_procedure_set_mime_types:
 * @procedure: A file procedure object.
 * @mime_types: A comma-separated list of MIME types, such as "image/jpeg".
 *
 * Associates MIME types with a file handler procedure.
 *
 * Registers MIME types for a file handler procedure. This allows PIKA
 * to determine the MIME type of the file opened or saved using this
 * procedure. It is recommended that only one MIME type is registered
 * per file procedure; when registering more than one MIME type, PIKA
 * will associate the first one with files opened or saved with this
 * procedure.
 *
 * Since: 3.0
 **/
void
pika_file_procedure_set_mime_types (PikaFileProcedure *procedure,
                                    const gchar       *mime_types)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  g_free (priv->mime_types);
  priv->mime_types = g_strdup (mime_types);
}

/**
 * pika_file_procedure_get_mime_types:
 * @procedure: A file procedure.
 *
 * Returns the procedure's mime-type as set with
 * [method@FileProcedure.set_mime_types].
 *
 * Returns: The procedure's registered mime-types.
 *
 * Since: 3.0
 **/
const gchar *
pika_file_procedure_get_mime_types (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), NULL);

  priv = pika_file_procedure_get_instance_private (procedure);

  return priv->mime_types;
}

/**
 * pika_file_procedure_set_extensions:
 * @procedure:  A file procedure.
 * @extensions: A comma separated list of extensions this procedure can
 *              handle (i.e. "jpg,jpeg").
 *
 * Registers the given list of extensions as something this procedure can
 * handle.
 *
 * Since: 3.0
 **/
void
pika_file_procedure_set_extensions (PikaFileProcedure *procedure,
                                    const gchar       *extensions)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  g_free (priv->extensions);
  priv->extensions = g_strdup (extensions);
}

/**
 * pika_file_procedure_get_extensions:
 * @procedure: A file procedure object.
 *
 * Returns the procedure's extensions as set with
 * [method@FileProcedure.set_extensions].
 *
 * Returns: The procedure's registered extensions.
 *
 * Since: 3.0
 **/
const gchar *
pika_file_procedure_get_extensions (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), NULL);

  priv = pika_file_procedure_get_instance_private (procedure);

  return priv->extensions;
}

/**
 * pika_file_procedure_set_prefixes:
 * @procedure: A file procedure object.
 * @prefixes:  A comma separated list of prefixes this procedure can
 *             handle (i.e. "http:,ftp:").
 *
 * It should almost never be necessary to register prefixes with file
 * procedures, because most sorts of URIs should be handled by GIO.
 *
 * Since: 3.0
 **/
void
pika_file_procedure_set_prefixes (PikaFileProcedure *procedure,
                                  const gchar       *prefixes)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  g_free (priv->prefixes);
  priv->prefixes = g_strdup (prefixes);
}

/**
 * pika_file_procedure_get_prefixes:
 * @procedure: A file procedure object.
 *
 * Returns the procedure's prefixes as set with
 * [method@FileProcedure.set_prefixes].
 *
 * Returns: The procedure's registered prefixes.
 *
 * Since: 3.0
 **/
const gchar *
pika_file_procedure_get_prefixes (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), NULL);

  priv = pika_file_procedure_get_instance_private (procedure);

  return priv->prefixes;
}

/**
 * pika_file_procedure_set_magics:
 * @procedure: A file procedure object.
 * @magics:    A comma-separated list of magic file information (i.e. "0,string,GIF").
 *
 * Registers the list of magic file information this procedure can handle.
 *
 * Since: 3.0
 **/
void
pika_file_procedure_set_magics (PikaFileProcedure *procedure,
                                const gchar       *magics)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  g_free (priv->magics);
  priv->magics = g_strdup (magics);
}

/**
 * pika_file_procedure_get_magics:
 * @procedure: A file procedure object.
 *
 * Returns the procedure's magics as set with [method@FileProcedure.set_magics].
 *
 * Returns: The procedure's registered magics.
 *
 * Since: 3.0
 **/
const gchar *
pika_file_procedure_get_magics (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), NULL);

  priv = pika_file_procedure_get_instance_private (procedure);

  return priv->magics;
}

/**
 * pika_file_procedure_set_priority:
 * @procedure: A file procedure object.
 * @priority: The procedure's priority.
 *
 * Sets the priority of a file handler procedure.
 *
 * When more than one procedure matches a given file, the procedure with the
 * lowest priority is used; if more than one procedure has the lowest priority,
 * it is unspecified which one of them is used. The default priority for file
 * handler procedures is 0.
 *
 * Since: 3.0
 **/
void
pika_file_procedure_set_priority (PikaFileProcedure *procedure,
                                  gint               priority)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  priv->priority = priority;
}

/**
 * pika_file_procedure_get_priority:
 * @procedure: A file procedure object.
 *
 * Returns the procedure's priority as set with
 * [method@FileProcedure.set_priority].
 *
 * Returns: The procedure's registered priority.
 *
 * Since: 3.0
 **/
gint
pika_file_procedure_get_priority (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), 0);

  priv = pika_file_procedure_get_instance_private (procedure);

  return priv->priority;
}

/**
 * pika_file_procedure_set_handles_remote:
 * @procedure:      A #PikaFileProcedure.
 * @handles_remote: The procedure's 'handles remote' flag.
 *
 * Registers a file procedure as capable of handling arbitrary remote
 * URIs via GIO.
 *
 * When @handles_remote is set to %TRUE, the procedure will get a
 * #GFile passed that can point to a remote file.
 *
 * When @handles_remote is set to %FALSE, the procedure will get a
 * local [iface@Gio.File] passed and can use [method@Gio.File.get_path] to get
 * to a filename that can be used with whatever non-GIO means of dealing with
 * the file.
 *
 * Since: 3.0
 **/
void
pika_file_procedure_set_handles_remote (PikaFileProcedure *procedure,
                                        gint               handles_remote)
{
  PikaFileProcedurePrivate *priv;

  g_return_if_fail (PIKA_IS_FILE_PROCEDURE (procedure));

  priv = pika_file_procedure_get_instance_private (procedure);
  priv->handles_remote = handles_remote;
}

/**
 * pika_file_procedure_get_handles_remote:
 * @procedure: A file procedure object.
 *
 * Returns the procedure's 'handles remote' flags as set with
 * [method@FileProcedure.set_handles_remote].
 *
 * Returns: The procedure's 'handles remote' flag
 *
 * Since: 3.0
 **/
gint
pika_file_procedure_get_handles_remote (PikaFileProcedure *procedure)
{
  PikaFileProcedurePrivate *priv;

  g_return_val_if_fail (PIKA_IS_FILE_PROCEDURE (procedure), 0);

  priv = pika_file_procedure_get_instance_private (procedure);
  return priv->handles_remote;
}
