/* 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
 *
 * pikamodules.c
 * (C) 1999 Austin Donnelly <austin@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 <gio/gio.h>

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

#include "core-types.h"

#include "config/pikacoreconfig.h"

#include "pika.h"
#include "pika-modules.h"

#include "pika-intl.h"


void
pika_modules_init (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (! pika->no_interface)
    {
      pika->module_db = pika_module_db_new (pika->be_verbose);
      pika->write_modulerc = FALSE;
    }
}

void
pika_modules_exit (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  g_clear_object (&pika->module_db);
}

void
pika_modules_load (Pika *pika)
{
  GFile    *file;
  GScanner *scanner;
  gchar    *module_load_inhibit = NULL;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (pika->no_interface)
    return;

  pika_module_db_set_verbose (pika->module_db, pika->be_verbose);

  file = pika_directory_file ("modulerc", NULL);

  if (pika->be_verbose)
    g_print ("Parsing '%s'\n", pika_file_get_utf8_name (file));

  scanner = pika_scanner_new_file (file, NULL);
  g_object_unref (file);

  if (scanner)
    {
      GTokenType  token;
      GError     *error = NULL;

#define MODULE_LOAD_INHIBIT 1

      g_scanner_scope_add_symbol (scanner, 0, "module-load-inhibit",
                                  GINT_TO_POINTER (MODULE_LOAD_INHIBIT));

      token = G_TOKEN_LEFT_PAREN;

      while (g_scanner_peek_next_token (scanner) == token)
        {
          token = g_scanner_get_next_token (scanner);

          switch (token)
            {
            case G_TOKEN_LEFT_PAREN:
              token = G_TOKEN_SYMBOL;
              break;

            case G_TOKEN_SYMBOL:
              if (scanner->value.v_symbol == GINT_TO_POINTER (MODULE_LOAD_INHIBIT))
                {
                  token = G_TOKEN_STRING;

                  if (! pika_scanner_parse_string_no_validate (scanner,
                                                               &module_load_inhibit))
                    goto error;
                }
              token = G_TOKEN_RIGHT_PAREN;
              break;

            case G_TOKEN_RIGHT_PAREN:
              token = G_TOKEN_LEFT_PAREN;
              break;

            default: /* do nothing */
              break;
            }
        }

#undef MODULE_LOAD_INHIBIT

      if (token != G_TOKEN_LEFT_PAREN)
        {
          g_scanner_get_next_token (scanner);
          g_scanner_unexp_token (scanner, token, NULL, NULL, NULL,
                                 _("fatal parse error"), TRUE);
        }

    error:

      if (error)
        {
          pika_message_literal (pika, NULL, PIKA_MESSAGE_ERROR, error->message);
          g_clear_error (&error);
        }

      pika_scanner_unref (scanner);
    }

  if (module_load_inhibit)
    {
      pika_module_db_set_load_inhibit (pika->module_db, module_load_inhibit);
      g_free (module_load_inhibit);
    }

  pika_module_db_load (pika->module_db, pika->config->module_path);
}

void
pika_modules_unload (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (! pika->no_interface && pika->write_modulerc)
    {
      PikaConfigWriter *writer;
      GString          *str;
      GListModel       *modules = G_LIST_MODEL (pika->module_db);
      guint             i;
      const gchar      *p;
      GFile            *file;
      GError           *error = NULL;

      str = g_string_new (NULL);
      for (i = 0; i < g_list_model_get_n_items (modules); i++)
        {
          PikaModule *module;

          module = g_list_model_get_item (modules, i);
          if (! pika_module_get_auto_load (module))
            {
              GFile *file = pika_module_get_file (module);
              gchar *path = g_file_get_path (file);

              g_string_append_c (str, G_SEARCHPATH_SEPARATOR);
              g_string_append (str, path);

              g_free (path);
            }

          g_clear_object (&module);
        }
      if (str->len > 0)
        p = str->str + 1;
      else
        p = "";

      file = pika_directory_file ("modulerc", NULL);

      if (pika->be_verbose)
        g_print ("Writing '%s'\n", pika_file_get_utf8_name (file));

      writer = pika_config_writer_new_from_file (file,
                                                 TRUE,
                                                 "PIKA modulerc",
                                                 &error);
      g_object_unref (file);

      if (writer)
        {
          pika_config_writer_open (writer, "module-load-inhibit");
          pika_config_writer_string (writer, p);
          pika_config_writer_close (writer);

          pika_config_writer_finish (writer, "end of modulerc", &error);

          pika->write_modulerc = FALSE;
        }

      g_string_free (str, TRUE);

      if (error)
        {
          pika_message_literal (pika, NULL, PIKA_MESSAGE_ERROR, error->message);
          g_clear_error (&error);
        }
    }
}

void
pika_modules_refresh (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (! pika->no_interface)
    {
      pika_module_db_refresh (pika->module_db, pika->config->module_path);
    }
}
