/* 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
 *
 * pikaplugindef.c
 *
 * 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 <gdk-pixbuf/gdk-pixbuf.h>
#include <gegl.h>

#include "plug-in-types.h"

#include "core/pika-memsize.h"

#include "pikaplugindef.h"
#include "pikapluginprocedure.h"


static void     pika_plug_in_def_finalize    (GObject    *object);

static gint64   pika_plug_in_def_get_memsize (PikaObject *object,
                                              gint64     *gui_size);


G_DEFINE_TYPE (PikaPlugInDef, pika_plug_in_def, PIKA_TYPE_OBJECT)

#define parent_class pika_plug_in_def_parent_class


static void
pika_plug_in_def_class_init (PikaPlugInDefClass *klass)
{
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
  PikaObjectClass *pika_object_class = PIKA_OBJECT_CLASS (klass);

  object_class->finalize         = pika_plug_in_def_finalize;

  pika_object_class->get_memsize = pika_plug_in_def_get_memsize;
}

static void
pika_plug_in_def_init (PikaPlugInDef *def)
{
}

static void
pika_plug_in_def_finalize (GObject *object)
{
  PikaPlugInDef *plug_in_def = PIKA_PLUG_IN_DEF (object);

  g_object_unref (plug_in_def->file);
  g_free (plug_in_def->help_domain_name);
  g_free (plug_in_def->help_domain_uri);

  g_slist_free_full (plug_in_def->procedures, (GDestroyNotify) g_object_unref);

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

static gint64
pika_plug_in_def_get_memsize (PikaObject *object,
                              gint64     *gui_size)
{
  PikaPlugInDef *plug_in_def = PIKA_PLUG_IN_DEF (object);
  gint64         memsize     = 0;

  memsize += pika_g_object_get_memsize (G_OBJECT (plug_in_def->file));
  memsize += pika_string_get_memsize (plug_in_def->help_domain_name);
  memsize += pika_string_get_memsize (plug_in_def->help_domain_uri);

  memsize += pika_g_slist_get_memsize (plug_in_def->procedures, 0);

  return memsize + PIKA_OBJECT_CLASS (parent_class)->get_memsize (object,
                                                                  gui_size);
}


/*  public functions  */

PikaPlugInDef *
pika_plug_in_def_new (GFile *file)
{
  PikaPlugInDef *plug_in_def;

  g_return_val_if_fail (G_IS_FILE (file), NULL);

  plug_in_def = g_object_new (PIKA_TYPE_PLUG_IN_DEF, NULL);

  plug_in_def->file = g_object_ref (file);

  return plug_in_def;
}

void
pika_plug_in_def_add_procedure (PikaPlugInDef       *plug_in_def,
                                PikaPlugInProcedure *proc)
{
  PikaPlugInProcedure *overridden;

  g_return_if_fail (PIKA_IS_PLUG_IN_DEF (plug_in_def));
  g_return_if_fail (PIKA_IS_PLUG_IN_PROCEDURE (proc));

  overridden = pika_plug_in_procedure_find (plug_in_def->procedures,
                                            pika_object_get_name (proc));

  if (overridden)
    pika_plug_in_def_remove_procedure (plug_in_def, overridden);

  proc->mtime = plug_in_def->mtime;

  pika_plug_in_procedure_set_help_domain (proc,
                                          plug_in_def->help_domain_name);

  plug_in_def->procedures = g_slist_append (plug_in_def->procedures,
                                            g_object_ref (proc));
}

void
pika_plug_in_def_remove_procedure (PikaPlugInDef       *plug_in_def,
                                   PikaPlugInProcedure *proc)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_DEF (plug_in_def));
  g_return_if_fail (PIKA_IS_PLUG_IN_PROCEDURE (proc));

  plug_in_def->procedures = g_slist_remove (plug_in_def->procedures, proc);
  g_object_unref (proc);
}

void
pika_plug_in_def_set_help_domain (PikaPlugInDef *plug_in_def,
                                  const gchar   *domain_name,
                                  const gchar   *domain_uri)
{
  GSList *list;

  g_return_if_fail (PIKA_IS_PLUG_IN_DEF (plug_in_def));

  if (plug_in_def->help_domain_name)
    g_free (plug_in_def->help_domain_name);
  plug_in_def->help_domain_name = g_strdup (domain_name);

  if (plug_in_def->help_domain_uri)
    g_free (plug_in_def->help_domain_uri);
  plug_in_def->help_domain_uri = g_strdup (domain_uri);

  for (list = plug_in_def->procedures; list; list = g_slist_next (list))
    {
      PikaPlugInProcedure *procedure = list->data;

      pika_plug_in_procedure_set_help_domain (procedure,
                                              plug_in_def->help_domain_name);
    }
}

void
pika_plug_in_def_set_mtime (PikaPlugInDef *plug_in_def,
                            gint64         mtime)
{
  GSList *list;

  g_return_if_fail (PIKA_IS_PLUG_IN_DEF (plug_in_def));

  plug_in_def->mtime = mtime;

  for (list = plug_in_def->procedures; list; list = g_slist_next (list))
    {
      PikaPlugInProcedure *proc = list->data;

      proc->mtime = plug_in_def->mtime;
    }
}

void
pika_plug_in_def_set_needs_query (PikaPlugInDef *plug_in_def,
                                  gboolean       needs_query)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_DEF (plug_in_def));

  plug_in_def->needs_query = needs_query ? TRUE : FALSE;
}

void
pika_plug_in_def_set_has_init (PikaPlugInDef *plug_in_def,
                               gboolean       has_init)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_DEF (plug_in_def));

  plug_in_def->has_init = has_init ? TRUE : FALSE;
}
