/* 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-2002 Spencer Kimball, Peter Mattis, and others
 *
 * pikapluginmanager.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 <string.h>

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

#include "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"

#include "plug-in-types.h"

#include "config/pikacoreconfig.h"

#include "core/pika.h"
#include "core/pika-filter-history.h"
#include "core/pika-memsize.h"
#include "core/pikamarshal.h"

#include "pdb/pikapdb.h"

#include "pikaenvirontable.h"
#include "pikainterpreterdb.h"
#include "pikaplugin.h"
#include "pikaplugindebug.h"
#include "pikaplugindef.h"
#include "pikapluginmanager.h"
#include "pikapluginmanager-data.h"
#include "pikapluginmanager-help-domain.h"
#include "pikapluginmanager-menu-branch.h"
#include "pikapluginshm.h"
#include "pikatemporaryprocedure.h"

#include "pika-intl.h"


enum
{
  PLUG_IN_OPENED,
  PLUG_IN_CLOSED,
  MENU_BRANCH_ADDED,
  LAST_SIGNAL
};


static void     pika_plug_in_manager_finalize    (GObject    *object);

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


G_DEFINE_TYPE (PikaPlugInManager, pika_plug_in_manager, PIKA_TYPE_OBJECT)

#define parent_class pika_plug_in_manager_parent_class

static guint manager_signals[LAST_SIGNAL] = { 0, };


static void
pika_plug_in_manager_class_init (PikaPlugInManagerClass *klass)
{
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
  PikaObjectClass *pika_object_class = PIKA_OBJECT_CLASS (klass);

  manager_signals[PLUG_IN_OPENED] =
    g_signal_new ("plug-in-opened",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaPlugInManagerClass,
                                   plug_in_opened),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  PIKA_TYPE_PLUG_IN);

  manager_signals[PLUG_IN_CLOSED] =
    g_signal_new ("plug-in-closed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaPlugInManagerClass,
                                   plug_in_closed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  PIKA_TYPE_PLUG_IN);

  manager_signals[MENU_BRANCH_ADDED] =
    g_signal_new ("menu-branch-added",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaPlugInManagerClass,
                                   menu_branch_added),
                  NULL, NULL,
                  pika_marshal_VOID__OBJECT_STRING_STRING,
                  G_TYPE_NONE, 3,
                  G_TYPE_FILE,
                  G_TYPE_STRING,
                  G_TYPE_STRING);

  object_class->finalize         = pika_plug_in_manager_finalize;

  pika_object_class->get_memsize = pika_plug_in_manager_get_memsize;
}

static void
pika_plug_in_manager_init (PikaPlugInManager *manager)
{
}

static void
pika_plug_in_manager_finalize (GObject *object)
{
  PikaPlugInManager *manager = PIKA_PLUG_IN_MANAGER (object);

  g_clear_pointer (&manager->load_procs,     g_slist_free);
  g_clear_pointer (&manager->save_procs,     g_slist_free);
  g_clear_pointer (&manager->export_procs,   g_slist_free);
  g_clear_pointer (&manager->raw_load_procs, g_slist_free);
  g_clear_pointer (&manager->batch_procs,    g_slist_free);

  g_clear_pointer (&manager->display_load_procs,     g_slist_free);
  g_clear_pointer (&manager->display_save_procs,     g_slist_free);
  g_clear_pointer (&manager->display_export_procs,   g_slist_free);
  g_clear_pointer (&manager->display_raw_load_procs, g_slist_free);

  if (manager->plug_in_procedures)
    {
      g_slist_free_full (manager->plug_in_procedures,
                         (GDestroyNotify) g_object_unref);
      manager->plug_in_procedures = NULL;
    }

  if (manager->plug_in_defs)
    {
      g_slist_free_full (manager->plug_in_defs,
                         (GDestroyNotify) g_object_unref);
      manager->plug_in_defs = NULL;
    }

  g_clear_object (&manager->environ_table);
  g_clear_object (&manager->interpreter_db);

  g_clear_pointer (&manager->debug, pika_plug_in_debug_free);

  pika_plug_in_manager_menu_branch_exit (manager);
  pika_plug_in_manager_help_domain_exit (manager);
  pika_plug_in_manager_data_free (manager);

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

static gint64
pika_plug_in_manager_get_memsize (PikaObject *object,
                                  gint64     *gui_size)
{
  PikaPlugInManager *manager = PIKA_PLUG_IN_MANAGER (object);
  gint64             memsize = 0;

  memsize += pika_g_slist_get_memsize_foreach (manager->plug_in_defs,
                                               (PikaMemsizeFunc)
                                               pika_object_get_memsize,
                                               gui_size);

  memsize += pika_g_slist_get_memsize (manager->plug_in_procedures, 0);
  memsize += pika_g_slist_get_memsize (manager->load_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->save_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->export_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->raw_load_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->batch_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->display_load_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->display_save_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->display_export_procs, 0);
  memsize += pika_g_slist_get_memsize (manager->display_raw_load_procs, 0);

  memsize += pika_g_slist_get_memsize (manager->menu_branches,  0 /* FIXME */);
  memsize += pika_g_slist_get_memsize (manager->help_domains,   0 /* FIXME */);

  memsize += pika_g_slist_get_memsize_foreach (manager->open_plug_ins,
                                               (PikaMemsizeFunc)
                                               pika_object_get_memsize,
                                               gui_size);
  memsize += pika_g_slist_get_memsize (manager->plug_in_stack, 0);

  memsize += 0; /* FIXME manager->shm */
  memsize += /* FIXME */ pika_g_object_get_memsize (G_OBJECT (manager->interpreter_db));
  memsize += /* FIXME */ pika_g_object_get_memsize (G_OBJECT (manager->environ_table));
  memsize += 0; /* FIXME manager->plug_in_debug */
  memsize += pika_g_list_get_memsize (manager->data_list, 0 /* FIXME */);

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

PikaPlugInManager *
pika_plug_in_manager_new (Pika *pika)
{
  PikaPlugInManager *manager;

  manager = g_object_new (PIKA_TYPE_PLUG_IN_MANAGER, NULL);

  manager->pika           = pika;
  manager->interpreter_db = pika_interpreter_db_new (pika->be_verbose);
  manager->environ_table  = pika_environ_table_new (pika->be_verbose);

  return manager;
}

void
pika_plug_in_manager_initialize (PikaPlugInManager  *manager,
                                 PikaInitStatusFunc  status_callback)
{
  PikaCoreConfig *config;
  GList          *path;

  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (status_callback != NULL);

  config = manager->pika->config;

  status_callback (NULL, _("Plug-in Interpreters"), 0.8);

  path = pika_config_path_expand_to_files (config->interpreter_path, NULL);
  pika_interpreter_db_load (manager->interpreter_db, path);
  g_list_free_full (path, (GDestroyNotify) g_object_unref);

  status_callback (NULL, _("Plug-in Environment"), 0.9);

  path = pika_config_path_expand_to_files (config->environ_path, NULL);
  pika_environ_table_load (manager->environ_table, path);
  g_list_free_full (path, (GDestroyNotify) g_object_unref);

  /*  allocate a piece of shared memory for use in transporting tiles
   *  to plug-ins. if we can't allocate a piece of shared memory then
   *  we'll fall back on sending the data over the pipe.
   */
  if (manager->pika->use_shm)
    manager->shm = pika_plug_in_shm_new ();

  manager->debug = pika_plug_in_debug_new ();
}

void
pika_plug_in_manager_exit (PikaPlugInManager *manager)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));

  while (manager->open_plug_ins)
    pika_plug_in_close (manager->open_plug_ins->data, TRUE);

  /*  need to detach from shared memory, we can't rely on exit()
   *  cleaning up behind us (see bug #609026)
   */
  if (manager->shm)
    {
      pika_plug_in_shm_free (manager->shm);
      manager->shm = NULL;
    }
}

void
pika_plug_in_manager_add_procedure (PikaPlugInManager   *manager,
                                    PikaPlugInProcedure *procedure)
{
  GSList *list;

  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_PLUG_IN_PROCEDURE (procedure));

  for (list = manager->plug_in_procedures; list; list = list->next)
    {
      PikaPlugInProcedure *tmp_proc = list->data;

      if (strcmp (pika_object_get_name (procedure),
                  pika_object_get_name (tmp_proc)) == 0)
        {
          GSList *list2;

          list->data = g_object_ref (procedure);

          g_printerr ("Removing duplicate PDB procedure '%s' "
                      "registered by '%s'\n",
                      pika_object_get_name (tmp_proc),
                      pika_file_get_utf8_name (tmp_proc->file));

          /* search the plugin list to see if any plugins had references to
           * the tmp_proc.
           */
          for (list2 = manager->plug_in_defs; list2; list2 = list2->next)
            {
              PikaPlugInDef *plug_in_def = list2->data;

              if (g_slist_find (plug_in_def->procedures, tmp_proc))
                pika_plug_in_def_remove_procedure (plug_in_def, tmp_proc);
            }

          /* also remove it from the lists of load, save and export procs */
          manager->load_procs             = g_slist_remove (manager->load_procs,             tmp_proc);
          manager->save_procs             = g_slist_remove (manager->save_procs,             tmp_proc);
          manager->export_procs           = g_slist_remove (manager->export_procs,           tmp_proc);
          manager->raw_load_procs         = g_slist_remove (manager->raw_load_procs,         tmp_proc);
          manager->batch_procs            = g_slist_remove (manager->batch_procs,            tmp_proc);
          manager->display_load_procs     = g_slist_remove (manager->display_load_procs,     tmp_proc);
          manager->display_save_procs     = g_slist_remove (manager->display_save_procs,     tmp_proc);
          manager->display_export_procs   = g_slist_remove (manager->display_export_procs,   tmp_proc);
          manager->display_raw_load_procs = g_slist_remove (manager->display_raw_load_procs, tmp_proc);

          /* and from the history */
          pika_filter_history_remove (manager->pika, PIKA_PROCEDURE (tmp_proc));

          g_object_unref (tmp_proc);

          return;
        }
    }

  manager->plug_in_procedures = g_slist_prepend (manager->plug_in_procedures,
                                                 g_object_ref (procedure));
}

void
pika_plug_in_manager_add_batch_procedure (PikaPlugInManager   *manager,
                                          PikaPlugInProcedure *proc)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_PLUG_IN_PROCEDURE (proc));

  if (! g_slist_find (manager->batch_procs, proc))
    manager->batch_procs = g_slist_prepend (manager->batch_procs, proc);
}

GSList *
pika_plug_in_manager_get_batch_procedures (PikaPlugInManager *manager)
{
  g_return_val_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager), NULL);

  return manager->batch_procs;
}

void
pika_plug_in_manager_add_temp_proc (PikaPlugInManager      *manager,
                                    PikaTemporaryProcedure *procedure)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_TEMPORARY_PROCEDURE (procedure));

  pika_pdb_register_procedure (manager->pika->pdb, PIKA_PROCEDURE (procedure));

  manager->plug_in_procedures = g_slist_prepend (manager->plug_in_procedures,
                                                 g_object_ref (procedure));
}

void
pika_plug_in_manager_remove_temp_proc (PikaPlugInManager      *manager,
                                       PikaTemporaryProcedure *procedure)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_TEMPORARY_PROCEDURE (procedure));

  manager->plug_in_procedures = g_slist_remove (manager->plug_in_procedures,
                                                procedure);

  pika_filter_history_remove (manager->pika,
                              PIKA_PROCEDURE (procedure));

  pika_pdb_unregister_procedure (manager->pika->pdb,
                                 PIKA_PROCEDURE (procedure));

  g_object_unref (procedure);
}

void
pika_plug_in_manager_add_open_plug_in (PikaPlugInManager *manager,
                                       PikaPlugIn        *plug_in)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_PLUG_IN (plug_in));

  manager->open_plug_ins = g_slist_prepend (manager->open_plug_ins,
                                            g_object_ref (plug_in));

  g_signal_emit (manager, manager_signals[PLUG_IN_OPENED], 0,
                 plug_in);
}

void
pika_plug_in_manager_remove_open_plug_in (PikaPlugInManager *manager,
                                          PikaPlugIn        *plug_in)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_PLUG_IN (plug_in));

  manager->open_plug_ins = g_slist_remove (manager->open_plug_ins, plug_in);

  g_signal_emit (manager, manager_signals[PLUG_IN_CLOSED], 0,
                 plug_in);

  g_object_unref (plug_in);
}

void
pika_plug_in_manager_plug_in_push (PikaPlugInManager *manager,
                                   PikaPlugIn        *plug_in)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));
  g_return_if_fail (PIKA_IS_PLUG_IN (plug_in));

  manager->current_plug_in = plug_in;

  manager->plug_in_stack = g_slist_prepend (manager->plug_in_stack,
                                            manager->current_plug_in);
}

void
pika_plug_in_manager_plug_in_pop (PikaPlugInManager *manager)
{
  g_return_if_fail (PIKA_IS_PLUG_IN_MANAGER (manager));

  if (manager->current_plug_in)
    manager->plug_in_stack = g_slist_remove (manager->plug_in_stack,
                                             manager->plug_in_stack->data);

  if (manager->plug_in_stack)
    manager->current_plug_in = manager->plug_in_stack->data;
  else
    manager->current_plug_in = NULL;
}
