/* 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
 *
 * PikaRc, the object for PIKAs user configuration file pikarc.
 * Copyright (C) 2001-2002  Sven Neumann <sven@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 <string.h>

#include <gio/gio.h>
#include <gegl.h>

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

#include "config-types.h"

#include "pikaconfig-file.h"
#include "pikarc.h"
#include "pikarc-deserialize.h"
#include "pikarc-serialize.h"
#include "pikarc-unknown.h"

#include "pika-intl.h"


enum
{
  PROP_0,
  PROP_VERBOSE,
  PROP_SYSTEM_PIKARC,
  PROP_USER_PIKARC
};


static void         pika_rc_config_iface_init (PikaConfigInterface *iface);

static void         pika_rc_dispose           (GObject      *object);
static void         pika_rc_finalize          (GObject      *object);
static void         pika_rc_set_property      (GObject      *object,
                                               guint         property_id,
                                               const GValue *value,
                                               GParamSpec   *pspec);
static void         pika_rc_get_property      (GObject      *object,
                                               guint         property_id,
                                               GValue       *value,
                                               GParamSpec   *pspec);

static PikaConfig * pika_rc_duplicate         (PikaConfig   *object);
static gboolean     pika_rc_idle_save         (PikaRc       *rc);
static void         pika_rc_notify            (PikaRc       *rc,
                                               GParamSpec   *param,
                                               gpointer      data);


G_DEFINE_TYPE_WITH_CODE (PikaRc, pika_rc, PIKA_TYPE_PLUGIN_CONFIG,
                         G_IMPLEMENT_INTERFACE (PIKA_TYPE_CONFIG,
                                                pika_rc_config_iface_init))

#define parent_class pika_rc_parent_class


static void
pika_rc_class_init (PikaRcClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->dispose      = pika_rc_dispose;
  object_class->finalize     = pika_rc_finalize;
  object_class->set_property = pika_rc_set_property;
  object_class->get_property = pika_rc_get_property;

  g_object_class_install_property (object_class, PROP_VERBOSE,
                                   g_param_spec_boolean ("verbose",
                                                         NULL, NULL,
                                                         FALSE,
                                                         G_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_SYSTEM_PIKARC,
                                   g_param_spec_object ("system-pikarc",
                                                        NULL, NULL,
                                                        G_TYPE_FILE,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));

  g_object_class_install_property (object_class, PROP_USER_PIKARC,
                                   g_param_spec_object ("user-pikarc",
                                                        NULL, NULL,
                                                        G_TYPE_FILE,
                                                        G_PARAM_READWRITE |
                                                        G_PARAM_CONSTRUCT));
}

static void
pika_rc_init (PikaRc *rc)
{
  rc->autosave     = FALSE;
  rc->save_idle_id = 0;
}

static void
pika_rc_dispose (GObject *object)
{
  PikaRc *rc = PIKA_RC (object);

  if (rc->save_idle_id)
    pika_rc_idle_save (rc);

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

static void
pika_rc_finalize (GObject *object)
{
  PikaRc *rc = PIKA_RC (object);

  g_clear_object (&rc->system_pikarc);
  g_clear_object (&rc->user_pikarc);

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

static void
pika_rc_set_property (GObject      *object,
                      guint         property_id,
                      const GValue *value,
                      GParamSpec   *pspec)
{
  PikaRc *rc = PIKA_RC (object);

  switch (property_id)
    {
    case PROP_VERBOSE:
      rc->verbose = g_value_get_boolean (value);
      break;

    case PROP_SYSTEM_PIKARC:
      if (rc->system_pikarc)
        g_object_unref (rc->system_pikarc);

      if (g_value_get_object (value))
        rc->system_pikarc = g_value_dup_object (value);
      else
        rc->system_pikarc = pika_sysconf_directory_file ("pikarc", NULL);
      break;

    case PROP_USER_PIKARC:
      if (rc->user_pikarc)
        g_object_unref (rc->user_pikarc);

      if (g_value_get_object (value))
        rc->user_pikarc = g_value_dup_object (value);
      else
        rc->user_pikarc = pika_directory_file ("pikarc", NULL);
      break;

   default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_rc_get_property (GObject    *object,
                      guint       property_id,
                      GValue     *value,
                      GParamSpec *pspec)
{
  PikaRc *rc = PIKA_RC (object);

  switch (property_id)
    {
    case PROP_VERBOSE:
      g_value_set_boolean (value, rc->verbose);
      break;
    case PROP_SYSTEM_PIKARC:
      g_value_set_object (value, rc->system_pikarc);
      break;
    case PROP_USER_PIKARC:
      g_value_set_object (value, rc->user_pikarc);
      break;

   default:
      G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
      break;
    }
}

static void
pika_rc_config_iface_init (PikaConfigInterface *iface)
{
  iface->serialize   = pika_rc_serialize;
  iface->deserialize = pika_rc_deserialize;
  iface->duplicate   = pika_rc_duplicate;
}

static void
pika_rc_duplicate_unknown_token (const gchar *key,
                                 const gchar *value,
                                 gpointer     user_data)
{
  pika_rc_add_unknown_token (PIKA_CONFIG (user_data), key, value);
}

static PikaConfig *
pika_rc_duplicate (PikaConfig *config)
{
  GObject    *pika;
  PikaConfig *dup;

  g_object_get (config, "pika", &pika, NULL);

  dup = g_object_new (PIKA_TYPE_RC,
                      "pika", pika,
                      NULL);

  if (pika)
    g_object_unref (pika);

  pika_config_sync (G_OBJECT (config), G_OBJECT (dup), 0);

  pika_rc_foreach_unknown_token (config,
                                 pika_rc_duplicate_unknown_token, dup);

  return dup;
}

static gboolean
pika_rc_idle_save (PikaRc *rc)
{
  pika_rc_save (rc);

  rc->save_idle_id = 0;

  return FALSE;
}

static void
pika_rc_notify (PikaRc     *rc,
                GParamSpec *param,
                gpointer    data)
{
  if (!rc->autosave)
    return;

  if (!rc->save_idle_id)
    rc->save_idle_id = g_idle_add ((GSourceFunc) pika_rc_idle_save, rc);
}

/**
 * pika_rc_new:
 * @pika:          a #Pika
 * @system_pikarc: the name of the system-wide pikarc file or %NULL to
 *                 use the standard location
 * @user_pikarc:   the name of the user pikarc file or %NULL to use the
 *                 standard location
 * @verbose:       enable console messages about loading and saving
 *
 * Creates a new PikaRc object and loads the system-wide and the user
 * configuration files.
 *
 * Returns: the new #PikaRc.
 */
PikaRc *
pika_rc_new (GObject  *pika,
             GFile    *system_pikarc,
             GFile    *user_pikarc,
             gboolean  verbose)
{
  PikaRc *rc;

  g_return_val_if_fail (G_IS_OBJECT (pika), NULL);
  g_return_val_if_fail (system_pikarc == NULL || G_IS_FILE (system_pikarc),
                        NULL);
  g_return_val_if_fail (user_pikarc == NULL || G_IS_FILE (user_pikarc),
                        NULL);

  rc = g_object_new (PIKA_TYPE_RC,
                     "pika",          pika,
                     "verbose",       verbose,
                     "system-pikarc", system_pikarc,
                     "user-pikarc",   user_pikarc,
                     NULL);

  pika_rc_load_system (rc);
  pika_rc_load_user (rc);

  return rc;
}

void
pika_rc_load_system (PikaRc *rc)
{
  GError *error = NULL;

  g_return_if_fail (PIKA_IS_RC (rc));

  if (rc->verbose)
    g_print ("Parsing '%s'\n",
             pika_file_get_utf8_name (rc->system_pikarc));

  if (! pika_config_deserialize_file (PIKA_CONFIG (rc),
                                      rc->system_pikarc, NULL, &error))
    {
      if (error->code != PIKA_CONFIG_ERROR_OPEN_ENOENT)
        g_message ("%s", error->message);

      g_clear_error (&error);
    }
}

void
pika_rc_load_user (PikaRc *rc)
{
  GError *error = NULL;

  g_return_if_fail (PIKA_IS_RC (rc));

  if (rc->verbose)
    g_print ("Parsing '%s'\n",
             pika_file_get_utf8_name (rc->user_pikarc));

  if (! pika_config_deserialize_file (PIKA_CONFIG (rc),
                                      rc->user_pikarc, NULL, &error))
    {
      if (error->code != PIKA_CONFIG_ERROR_OPEN_ENOENT)
        {
          g_message ("%s", error->message);

          pika_config_file_backup_on_error (rc->user_pikarc, "pikarc", NULL);
        }

      g_clear_error (&error);
    }
}

void
pika_rc_set_autosave (PikaRc   *rc,
                      gboolean  autosave)
{
  g_return_if_fail (PIKA_IS_RC (rc));

  autosave = autosave ? TRUE : FALSE;

  if (rc->autosave == autosave)
    return;

  if (autosave)
    g_signal_connect (rc, "notify",
                      G_CALLBACK (pika_rc_notify),
                      NULL);
  else
    g_signal_handlers_disconnect_by_func (rc, pika_rc_notify, NULL);

  rc->autosave = autosave;
}


/**
 * pika_rc_query:
 * @rc:  a #PikaRc object.
 * @key: a string used as a key for the lookup.
 *
 * This function looks up @key in the object properties of @rc. If
 * there's a matching property, a string representation of its value
 * is returned. If no property is found, the list of unknown tokens
 * attached to the @rc object is searched.
 *
 * Returns: (nullable): a newly allocated string representing the value or %NULL
 *               if the key couldn't be found.
 **/
gchar *
pika_rc_query (PikaRc      *rc,
               const gchar *key)
{
  GObjectClass  *klass;
  GObject       *rc_object;
  GParamSpec   **property_specs;
  GParamSpec    *prop_spec;
  guint          i, n_property_specs;
  gchar         *retval = NULL;

  g_return_val_if_fail (PIKA_IS_RC (rc), NULL);
  g_return_val_if_fail (key != NULL, NULL);

  rc_object = G_OBJECT (rc);
  klass = G_OBJECT_GET_CLASS (rc);

  property_specs = g_object_class_list_properties (klass, &n_property_specs);

  if (!property_specs)
    return NULL;

  for (i = 0, prop_spec = NULL; i < n_property_specs && !prop_spec; i++)
    {
      prop_spec = property_specs[i];

      if (! (prop_spec->flags & PIKA_CONFIG_PARAM_SERIALIZE) ||
          strcmp (prop_spec->name, key))
        {
          prop_spec = NULL;
        }
    }

  if (prop_spec)
    {
      GString *str   = g_string_new (NULL);
      GValue   value = G_VALUE_INIT;

      g_value_init (&value, prop_spec->value_type);
      g_object_get_property (rc_object, prop_spec->name, &value);

      if (pika_config_serialize_value (&value, str, FALSE))
        retval = g_string_free (str, FALSE);
      else
        g_string_free (str, TRUE);

      g_value_unset (&value);
    }
  else
    {
      retval = g_strdup (pika_rc_lookup_unknown_token (PIKA_CONFIG (rc), key));
    }

  g_free (property_specs);

  if (!retval)
    {
      const gchar * const path_tokens[] =
      {
        "pika_dir",
        "pika_data_dir",
        "pika_plug_in_dir",
        "pika_plugin_dir",
        "pika_sysconf_dir"
      };

      for (i = 0; !retval && i < G_N_ELEMENTS (path_tokens); i++)
        if (strcmp (key, path_tokens[i]) == 0)
          retval = g_strdup_printf ("${%s}", path_tokens[i]);
    }

  if (retval)
    {
      gchar *tmp = pika_config_path_expand (retval, FALSE, NULL);

      if (tmp)
        {
          g_free (retval);
          retval = tmp;
        }
    }

  return retval;
}

/**
 * pika_rc_set_unknown_token:
 * @pikarc: a #PikaRc object.
 * @token:
 * @value:
 *
 * Calls pika_rc_add_unknown_token() and triggers an idle-save if
 * autosave is enabled on @pikarc.
 **/
void
pika_rc_set_unknown_token (PikaRc      *rc,
                           const gchar *token,
                           const gchar *value)
{
  g_return_if_fail (PIKA_IS_RC (rc));

  pika_rc_add_unknown_token (PIKA_CONFIG (rc), token, value);

  if (rc->autosave)
    pika_rc_notify (rc, NULL, NULL);
}

/**
 * pika_rc_save:
 * @pikarc: a #PikaRc object.
 *
 * Saves any settings that differ from the system-wide defined
 * defaults to the users personal pikarc file.
 **/
void
pika_rc_save (PikaRc *rc)
{
  GObject *pika;
  PikaRc  *global;
  gchar   *header;
  GError  *error = NULL;

  const gchar *top =
    "PIKA pikarc\n"
    "\n"
    "This is your personal pikarc file.  Any variable defined in this file "
    "takes precedence over the value defined in the system-wide pikarc: ";
  const gchar *bottom =
    "\n"
    "Most values can be set within PIKA by changing some options in "
    "the Preferences dialog.";
  const gchar *footer =
    "end of pikarc";

  g_return_if_fail (PIKA_IS_RC (rc));

  g_object_get (rc, "pika", &pika, NULL);

  global = g_object_new (PIKA_TYPE_RC,
                         "pika", pika,
                         NULL);

  if (pika)
    g_object_unref (pika);

  pika_config_deserialize_file (PIKA_CONFIG (global),
                                rc->system_pikarc, NULL, NULL);

  header = g_strconcat (top, pika_file_get_utf8_name (rc->system_pikarc),
                        bottom, NULL);

  if (rc->verbose)
    g_print ("Writing '%s'\n",
             pika_file_get_utf8_name (rc->user_pikarc));

  if (! pika_config_serialize_to_file (PIKA_CONFIG (rc),
                                       rc->user_pikarc,
                                       header, footer, global,
                                       &error))
    {
      g_message ("%s", error->message);
      g_error_free (error);
    }

  g_free (header);
  g_object_unref (global);
}

/**
 * pika_rc_migrate:
 * @rc: a #PikaRc object.
 *
 * Resets all PikaParamConfigPath properties of the passed rc object
 * to their default values, in order to prevent paths in a migrated
 * pikarc to refer to folders in the old PIKA's user directory.
 **/
void
pika_rc_migrate (PikaRc *rc)
{
  GParamSpec **pspecs;
  guint        n_pspecs;
  gint         i;

  g_return_if_fail (PIKA_IS_RC (rc));

  pspecs = g_object_class_list_properties (G_OBJECT_GET_CLASS (rc), &n_pspecs);

  for (i = 0; i < n_pspecs; i++)
    {
      GParamSpec *pspec = pspecs[i];

      if (PIKA_IS_PARAM_SPEC_CONFIG_PATH (pspec))
        {
          GValue value = G_VALUE_INIT;

          g_value_init (&value, pspec->value_type);

          g_param_value_set_default (pspec, &value);
          g_object_set_property (G_OBJECT (rc), pspec->name, &value);

          g_value_unset (&value);
        }
    }

  g_free (pspecs);
}
