/* 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
 *
 * modifiers.c
 * Copyright (C) 2022 Jehan
 *
 * 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 <gegl.h>
#include <gtk/gtk.h>

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

#include "gui-types.h"

#include "config/pikaconfig-file.h"
#include "config/pikaguiconfig.h"

#include "core/pika.h"
#include "core/pikaerror.h"

#include "display/pikamodifiersmanager.h"

#include "widgets/pikawidgets-utils.h"

#include "dialogs/dialogs.h"

#include "modifiers.h"
#include "pika-log.h"

#include "pika-intl.h"


enum
{
  MODIFIERS_INFO = 1,
  HIDE_DOCKS,
  SINGLE_WINDOW_MODE,
  SHOW_TABS,
  TABS_POSITION,
  LAST_TIP_SHOWN
};


static GFile * modifiers_file (Pika *pika);


/*  private variables  */

static gboolean   modifiersrc_deleted = FALSE;


/*  public functions  */

void
modifiers_init (Pika *pika)
{
  PikaDisplayConfig    *display_config;
  GFile                *file;
  PikaModifiersManager *manager = NULL;
  GError               *error   = NULL;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  display_config = PIKA_DISPLAY_CONFIG (pika->config);
  if (display_config->modifiers_manager != NULL)
    return;

  manager = pika_modifiers_manager_new ();
  g_object_set (display_config, "modifiers-manager", manager, NULL);
  g_object_unref (manager);

  file = modifiers_file (pika);

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

  pika_config_deserialize_file (PIKA_CONFIG (manager), file, NULL, &error);

  if (error)
    {
      /* File not existing is considered a normal event, not an error.
       * It can happen for instance the first time you run PIKA. When
       * this happens, we ignore the error. The PikaModifiersManager
       * object will simply use default modifiers.
       */
      if (error->domain != PIKA_CONFIG_ERROR ||
          error->code != PIKA_CONFIG_ERROR_OPEN_ENOENT)
        {
          pika_message_literal (pika, NULL, PIKA_MESSAGE_ERROR, error->message);
          pika_config_file_backup_on_error (file, "modifiersrc", NULL);
        }

      g_clear_error (&error);
    }

  g_object_unref (file);
}

void
modifiers_exit (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
}

void
modifiers_restore (Pika *pika)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
}

void
modifiers_save (Pika     *pika,
                gboolean  always_save)
{
  PikaDisplayConfig    *display_config;
  GFile                *file;
  PikaModifiersManager *manager = NULL;
  GError               *error   = NULL;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (modifiersrc_deleted && ! always_save)
    return;

  display_config = PIKA_DISPLAY_CONFIG (pika->config);
  g_return_if_fail (PIKA_IS_DISPLAY_CONFIG (display_config));

  manager = PIKA_MODIFIERS_MANAGER (display_config->modifiers_manager);
  g_return_if_fail (manager != NULL);
  g_return_if_fail (PIKA_IS_MODIFIERS_MANAGER (manager));
  file = modifiers_file (pika);

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

  pika_config_serialize_to_file (PIKA_CONFIG (manager), file,
                                 "PIKA modifiersrc\n\n"
                                 "This file stores modifiers configuration. "
                                 "You are not supposed to edit it manually, "
                                 "but of course you can do. The modifiersrc "
                                 "will be entirely rewritten every time you "
                                 "quit PIKA. If this file isn't found, "
                                 "defaults are used.",
                                 NULL, NULL, &error);
  if (error != NULL)
    {
      pika_message_literal (pika, NULL, PIKA_MESSAGE_ERROR, error->message);
      g_clear_error (&error);
    }

  g_object_unref (file);

  modifiersrc_deleted = FALSE;
}

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

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

  file = modifiers_file (pika);

  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);
    }
  else
    {
      modifiersrc_deleted = TRUE;
    }

  g_clear_error (&my_error);
  g_object_unref (file);

  return success;
}

static GFile *
modifiers_file (Pika *pika)
{
  const gchar *basename;
  GFile       *file;

  basename = g_getenv ("PIKA_TESTING_MODIFIERSRC_NAME");
  if (! basename)
    basename = "modifiersrc";

  file = pika_directory_file (basename, NULL);

  return file;
}
