/* 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-1999 Spencer Kimball and Peter Mattis
 *
 * pika-contexts.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 "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"

#include "core-types.h"

#include "pika.h"
#include "pikaerror.h"
#include "pika-contexts.h"
#include "pikacontext.h"

#include "config/pikaconfig-file.h"

#include "pika-intl.h"


void
pika_contexts_init (Pika *pika)
{
  PikaContext *context;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  /*  the default context contains the user's saved preferences
   *
   *  TODO: load from disk
   */
  context = pika_context_new (pika, "Default", NULL);
  pika_set_default_context (pika, context);
  g_object_unref (context);

  /*  the initial user_context is a straight copy of the default context
   */
  context = pika_context_new (pika, "User", context);
  pika_set_user_context (pika, context);
  g_object_unref (context);
}

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

  pika_set_user_context (pika, NULL);
  pika_set_default_context (pika, NULL);
}

gboolean
pika_contexts_load (Pika    *pika,
                    GError **error)
{
  GFile    *file;
  GError   *my_error = NULL;
  gboolean  success;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  file = pika_directory_file ("contextrc", NULL);

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

  success = pika_config_deserialize_file (PIKA_CONFIG (pika_get_user_context (pika)),
                                          file,
                                          NULL, &my_error);

  g_object_unref (file);

  if (! success)
    {
      if (my_error->code == PIKA_CONFIG_ERROR_OPEN_ENOENT)
        {
          g_clear_error (&my_error);
          success = TRUE;
        }
      else
        {
          g_propagate_error (error, my_error);
        }
    }

  return success;
}

gboolean
pika_contexts_save (Pika    *pika,
                    GError **error)
{
  GFile    *file;
  gboolean  success;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  file = pika_directory_file ("contextrc", NULL);

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

  success = pika_config_serialize_to_file (PIKA_CONFIG (pika_get_user_context (pika)),
                                           file,
                                           "PIKA user context",
                                           "end of user context",
                                           NULL, error);

  g_object_unref (file);

  return success;
}

gboolean
pika_contexts_clear (Pika    *pika,
                     GError **error)
{
  GFile    *file;
  GError   *my_error = NULL;
  gboolean  success  = TRUE;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);

  file = pika_directory_file ("contextrc", NULL);

  if (! g_file_delete (file, NULL, &my_error) &&
      my_error->code != G_IO_ERROR_NOT_FOUND)
    {
      success = FALSE;

      g_set_error (error, PIKA_ERROR, PIKA_FAILED,
                   _("Deleting \"%s\" failed: %s"),
                   pika_file_get_utf8_name (file), my_error->message);
    }

  g_clear_error (&my_error);
  g_object_unref (file);

  return success;
}
