/* 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
 *
 * 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> /* strlen */

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

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

#include "core-types.h"

#include "config/pikarc.h"

#include "gegl/pika-babl.h"

#include "pdb/pikapdb.h"
#include "pdb/pika-pdb-compat.h"
#include "pdb/internal-procs.h"

#include "plug-in/pikapluginmanager.h"
#include "plug-in/pikapluginmanager-restore.h"

#include "paint/pika-paint.h"

#include "xcf/xcf.h"
#include "file-data/file-data.h"

#include "pika.h"
#include "pika-contexts.h"
#include "pika-data-factories.h"
#include "pika-filter-history.h"
#include "pika-memsize.h"
#include "pika-modules.h"
#include "pika-parasites.h"
#include "pika-templates.h"
#include "pika-units.h"
#include "pika-utils.h"
#include "pikabrush.h"
#include "pikabuffer.h"
#include "pikacontext.h"
#include "pikadynamics.h"
#include "pikadocumentlist.h"
#include "pikaextensionmanager.h"
#include "pikagradient.h"
#include "pikaidtable.h"
#include "pikaimage.h"
#include "pikaimagefile.h"
#include "pikalist.h"
#include "pikamarshal.h"
#include "pikamybrush.h"
#include "pikapalette.h"
#include "pikaparasitelist.h"
#include "pikapattern.h"
#include "pikatemplate.h"
#include "pikatoolinfo.h"
#include "pikatreeproxy.h"

#include "pika-intl.h"


/*  we need to register all enum types so they are known to the type
 *  system by name, re-use the files the pdb generated for libpika
 */
void           pika_enums_init           (void);
const gchar ** pika_enums_get_type_names (gint *n_type_names);
#include "libpika/pikaenums.c.tail"


enum
{
  INITIALIZE,
  RESTORE,
  EXIT,
  CLIPBOARD_CHANGED,
  FILTER_HISTORY_CHANGED,
  IMAGE_OPENED,
  LAST_SIGNAL
};

enum
{
  PROP_0,
  PROP_VERBOSE
};


static void      pika_constructed          (GObject           *object);
static void      pika_set_property         (GObject           *object,
                                            guint              property_id,
                                            const GValue      *value,
                                            GParamSpec        *pspec);
static void      pika_get_property         (GObject           *object,
                                            guint              property_id,
                                            GValue            *value,
                                            GParamSpec        *pspec);
static void      pika_dispose              (GObject           *object);
static void      pika_finalize             (GObject           *object);

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

static void      pika_real_initialize      (Pika              *pika,
                                            PikaInitStatusFunc status_callback);
static void      pika_real_restore         (Pika              *pika,
                                            PikaInitStatusFunc status_callback);
static gboolean  pika_real_exit            (Pika              *pika,
                                            gboolean           force);

static void      pika_global_config_notify (GObject           *global_config,
                                            GParamSpec        *param_spec,
                                            GObject           *edit_config);
static void      pika_edit_config_notify   (GObject           *edit_config,
                                            GParamSpec        *param_spec,
                                            GObject           *global_config);


G_DEFINE_TYPE (Pika, pika, PIKA_TYPE_OBJECT)

#define parent_class pika_parent_class

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


static void
pika_class_init (PikaClass *klass)
{
  GObjectClass    *object_class      = G_OBJECT_CLASS (klass);
  PikaObjectClass *pika_object_class = PIKA_OBJECT_CLASS (klass);

  pika_signals[INITIALIZE] =
    g_signal_new ("initialize",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaClass, initialize),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_POINTER);

  pika_signals[RESTORE] =
    g_signal_new ("restore",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaClass, restore),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1,
                  G_TYPE_POINTER);

  pika_signals[EXIT] =
    g_signal_new ("exit",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaClass, exit),
                  g_signal_accumulator_true_handled, NULL,
                  pika_marshal_BOOLEAN__BOOLEAN,
                  G_TYPE_BOOLEAN, 1,
                  G_TYPE_BOOLEAN);

  pika_signals[CLIPBOARD_CHANGED] =
    g_signal_new ("clipboard-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaClass, clipboard_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  pika_signals[FILTER_HISTORY_CHANGED] =
    g_signal_new ("filter-history-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaClass,
                                   filter_history_changed),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 0);

  pika_signals[IMAGE_OPENED] =
    g_signal_new ("image-opened",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_LAST,
                  G_STRUCT_OFFSET (PikaClass, image_opened),
                  NULL, NULL, NULL,
                  G_TYPE_NONE, 1, G_TYPE_FILE);

  object_class->constructed      = pika_constructed;
  object_class->set_property     = pika_set_property;
  object_class->get_property     = pika_get_property;
  object_class->dispose          = pika_dispose;
  object_class->finalize         = pika_finalize;

  pika_object_class->get_memsize = pika_get_memsize;

  klass->initialize              = pika_real_initialize;
  klass->restore                 = pika_real_restore;
  klass->exit                    = pika_real_exit;
  klass->clipboard_changed       = NULL;

  g_object_class_install_property (object_class, PROP_VERBOSE,
                                   g_param_spec_boolean ("verbose", NULL, NULL,
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_init (Pika *pika)
{
  pika->be_verbose       = FALSE;
  pika->no_data          = FALSE;
  pika->no_interface     = FALSE;
  pika->show_gui         = TRUE;
  pika->use_shm          = FALSE;
  pika->use_cpu_accel    = TRUE;
  pika->message_handler  = PIKA_CONSOLE;
  pika->show_playground  = FALSE;
  pika->stack_trace_mode = PIKA_STACK_TRACE_NEVER;
  pika->pdb_compat_mode  = PIKA_PDB_COMPAT_OFF;

  pika_gui_init (pika);

  pika->parasites = pika_parasite_list_new ();

  pika_enums_init ();

  pika_units_init (pika);

  pika->images = pika_list_new_weak (PIKA_TYPE_IMAGE, FALSE);
  pika_object_set_static_name (PIKA_OBJECT (pika->images), "images");

  pika->next_guide_id        = 1;
  pika->next_sample_point_id = 1;
  pika->image_table          = pika_id_table_new ();
  pika->item_table           = pika_id_table_new ();

  pika->displays = g_object_new (PIKA_TYPE_LIST,
                                 "children-type", PIKA_TYPE_OBJECT,
                                 "policy",        PIKA_CONTAINER_POLICY_WEAK,
                                 "append",        TRUE,
                                 NULL);
  pika_object_set_static_name (PIKA_OBJECT (pika->displays), "displays");
  pika->next_display_id = 1;

  pika->named_buffers = pika_list_new (PIKA_TYPE_BUFFER, TRUE);
  pika_object_set_static_name (PIKA_OBJECT (pika->named_buffers),
                               "named buffers");

  pika_data_factories_init (pika);

  pika->tool_info_list = g_object_new (PIKA_TYPE_LIST,
                                       "children-type", PIKA_TYPE_TOOL_INFO,
                                       "append",        TRUE,
                                       NULL);
  pika_object_set_static_name (PIKA_OBJECT (pika->tool_info_list),
                               "tool infos");

  pika->tool_item_list = g_object_new (PIKA_TYPE_LIST,
                                       "children-type", PIKA_TYPE_TOOL_ITEM,
                                       "append",        TRUE,
                                       NULL);
  pika_object_set_static_name (PIKA_OBJECT (pika->tool_item_list),
                               "tool items");

  pika->tool_item_ui_list = pika_tree_proxy_new_for_container (
    pika->tool_item_list);
  pika_object_set_static_name (PIKA_OBJECT (pika->tool_item_ui_list),
                               "ui tool items");

  pika->documents = pika_document_list_new (pika);

  pika->templates = pika_list_new (PIKA_TYPE_TEMPLATE, TRUE);
  pika_object_set_static_name (PIKA_OBJECT (pika->templates), "templates");
}

static void
pika_constructed (GObject *object)
{
  Pika *pika = PIKA (object);

  G_OBJECT_CLASS (parent_class)->constructed (object);

  pika_modules_init (pika);

  pika_paint_init (pika);

  pika->extension_manager = pika_extension_manager_new (pika);
  pika->plug_in_manager   = pika_plug_in_manager_new (pika);
  pika->pdb               = pika_pdb_new (pika);

  xcf_init (pika);
  file_data_init (pika);

  /*  create user and default context  */
  pika_contexts_init (pika);

  /* Initialize the extension manager early as its contents may be used
   * at the very start (e.g. the splash image).
   */
  pika_extension_manager_initialize (pika->extension_manager);
}

static void
pika_set_property (GObject           *object,
                   guint              property_id,
                   const GValue      *value,
                   GParamSpec        *pspec)
{
  Pika *pika = PIKA (object);

  switch (property_id)
    {
    case PROP_VERBOSE:
      pika->be_verbose = g_value_get_boolean (value);
      break;

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

static void
pika_get_property (GObject    *object,
                   guint       property_id,
                   GValue     *value,
                   GParamSpec *pspec)
{
  Pika *pika = PIKA (object);

  switch (property_id)
    {
    case PROP_VERBOSE:
      g_value_set_boolean (value, pika->be_verbose);
      break;

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

static void
pika_dispose (GObject *object)
{
  Pika *pika = PIKA (object);

  if (pika->be_verbose)
    g_print ("EXIT: %s\n", G_STRFUNC);

  pika_data_factories_clear (pika);

  pika_filter_history_clear (pika);

  g_clear_object (&pika->edit_config);
  g_clear_object (&pika->config);

  pika_contexts_exit (pika);

  g_clear_object (&pika->image_new_last_template);

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

static void
pika_finalize (GObject *object)
{
  Pika  *pika      = PIKA (object);
  GList *standards = NULL;

  if (pika->be_verbose)
    g_print ("EXIT: %s\n", G_STRFUNC);

  standards = g_list_prepend (standards,
                              pika_brush_get_standard (pika->user_context));
  standards = g_list_prepend (standards,
                              pika_dynamics_get_standard (pika->user_context));
  standards = g_list_prepend (standards,
                              pika_mybrush_get_standard (pika->user_context));
  standards = g_list_prepend (standards,
                              pika_pattern_get_standard (pika->user_context));
  standards = g_list_prepend (standards,
                              pika_gradient_get_standard (pika->user_context));
  standards = g_list_prepend (standards,
                              pika_palette_get_standard (pika->user_context));

  g_clear_object (&pika->templates);
  g_clear_object (&pika->documents);

  pika_tool_info_set_standard (pika, NULL);

  g_clear_object (&pika->tool_item_list);
  g_clear_object (&pika->tool_item_ui_list);

  if (pika->tool_info_list)
    {
      pika_container_foreach (pika->tool_info_list,
                              (GFunc) g_object_run_dispose, NULL);
      g_clear_object (&pika->tool_info_list);
    }

  file_data_exit (pika);
  xcf_exit (pika);

  g_clear_object (&pika->pdb);

  pika_data_factories_exit (pika);

  g_clear_object (&pika->named_buffers);
  g_clear_object (&pika->clipboard_buffer);
  g_clear_object (&pika->clipboard_image);
  g_clear_object (&pika->displays);
  g_clear_object (&pika->item_table);
  g_clear_object (&pika->image_table);
  g_clear_object (&pika->images);
  g_clear_object (&pika->plug_in_manager);
  g_clear_object (&pika->extension_manager);

  if (pika->module_db)
    pika_modules_exit (pika);

  pika_paint_exit (pika);

  g_clear_object (&pika->parasites);
  g_clear_object (&pika->default_folder);

  g_clear_pointer (&pika->session_name, g_free);

  if (pika->context_list)
    {
      GList *list;

      g_warning ("%s: list of contexts not empty upon exit (%d contexts left)\n",
                 G_STRFUNC, g_list_length (pika->context_list));

      for (list = pika->context_list; list; list = g_list_next (list))
        g_printerr ("stale context: %s\n", pika_object_get_name (list->data));

      g_list_free (pika->context_list);
      pika->context_list = NULL;
    }

  g_list_foreach (standards, (GFunc) g_object_unref, NULL);
  g_list_free (standards);

  pika_units_exit (pika);

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

static gint64
pika_get_memsize (PikaObject *object,
                  gint64     *gui_size)
{
  Pika   *pika    = PIKA (object);
  gint64  memsize = 0;

  memsize += pika_g_list_get_memsize (pika->user_units, 0 /* FIXME */);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->parasites),
                                      gui_size);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->paint_info_list),
                                      gui_size);

  memsize += pika_g_object_get_memsize (G_OBJECT (pika->module_db));
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->plug_in_manager),
                                      gui_size);

  memsize += pika_g_list_get_memsize_foreach (pika->filter_history,
                                              (PikaMemsizeFunc)
                                              pika_object_get_memsize,
                                              gui_size);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->image_table), 0);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->item_table),  0);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->displays), gui_size);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->clipboard_image),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->clipboard_buffer),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->named_buffers),
                                      gui_size);

  memsize += pika_data_factories_get_memsize (pika, gui_size);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->pdb), gui_size);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->tool_info_list),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->standard_tool_info),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->documents),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->templates),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->image_new_last_template),
                                      gui_size);

  memsize += pika_g_list_get_memsize (pika->context_list, 0);

  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->default_context),
                                      gui_size);
  memsize += pika_object_get_memsize (PIKA_OBJECT (pika->user_context),
                                      gui_size);

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

static void
pika_real_initialize (Pika               *pika,
                      PikaInitStatusFunc  status_callback)
{
  if (pika->be_verbose)
    g_print ("INIT: %s\n", G_STRFUNC);

  status_callback (_("Initialization"), NULL, 0.0);

  /*  set the last values used to default values  */
  pika->image_new_last_template =
    pika_config_duplicate (PIKA_CONFIG (pika->config->default_image));

  /*  add data objects that need the user context  */
  pika_data_factories_add_builtin (pika);

  /*  register all internal procedures  */
  status_callback (NULL, _("Internal Procedures"), 0.2);
  internal_procs_init (pika->pdb);
  pika_pdb_compat_procs_register (pika->pdb, pika->pdb_compat_mode);

  pika_plug_in_manager_initialize (pika->plug_in_manager, status_callback);

  status_callback (NULL, "", 1.0);
}

static void
pika_real_restore (Pika               *pika,
                   PikaInitStatusFunc  status_callback)
{
  if (pika->be_verbose)
    g_print ("INIT: %s\n", G_STRFUNC);

  pika_plug_in_manager_restore (pika->plug_in_manager,
                                pika_get_user_context (pika), status_callback);

  /*  initialize babl fishes  */
  status_callback (_("Initialization"), "Babl Fishes", 0.0);
  pika_babl_init_fishes (status_callback);

  pika->restored = TRUE;
}

static gboolean
pika_real_exit (Pika     *pika,
                gboolean  force)
{
  if (pika->be_verbose)
    g_print ("EXIT: %s\n", G_STRFUNC);

  pika_plug_in_manager_exit (pika->plug_in_manager);
  pika_extension_manager_exit (pika->extension_manager);
  pika_modules_unload (pika);

  pika_data_factories_save (pika);

  pika_templates_save (pika);
  pika_parasiterc_save (pika);
  pika_unitrc_save (pika);

  return FALSE; /* continue exiting */
}

Pika *
pika_new (const gchar       *name,
          const gchar       *session_name,
          GFile             *default_folder,
          gboolean           be_verbose,
          gboolean           no_data,
          gboolean           no_fonts,
          gboolean           no_interface,
          gboolean           use_shm,
          gboolean           use_cpu_accel,
          gboolean           console_messages,
          gboolean           show_playground,
          gboolean           show_debug_menu,
          PikaStackTraceMode stack_trace_mode,
          PikaPDBCompatMode  pdb_compat_mode)
{
  Pika *pika;

  g_return_val_if_fail (name != NULL, NULL);

  pika = g_object_new (PIKA_TYPE_PIKA,
                       "name",    name,
                       "verbose", be_verbose ? TRUE : FALSE,
                       NULL);

  if (default_folder)
    pika->default_folder = g_object_ref (default_folder);

  pika->session_name     = g_strdup (session_name);
  pika->no_data          = no_data          ? TRUE : FALSE;
  pika->no_fonts         = no_fonts         ? TRUE : FALSE;
  pika->no_interface     = no_interface     ? TRUE : FALSE;
  pika->use_shm          = use_shm          ? TRUE : FALSE;
  pika->use_cpu_accel    = use_cpu_accel    ? TRUE : FALSE;
  pika->console_messages = console_messages ? TRUE : FALSE;
  pika->show_playground  = show_playground  ? TRUE : FALSE;
  pika->show_debug_menu  = show_debug_menu  ? TRUE : FALSE;
  pika->stack_trace_mode = stack_trace_mode;
  pika->pdb_compat_mode  = pdb_compat_mode;

  return pika;
}

/**
 * pika_set_show_gui:
 * @pika:
 * @show:
 *
 * Test cases that tests the UI typically don't want any windows to be
 * presented during the test run. Allow them to set this.
 **/
void
pika_set_show_gui (Pika     *pika,
                   gboolean  show_gui)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));

  pika->show_gui = show_gui;
}

/**
 * pika_get_show_gui:
 * @pika:
 *
 * Returns: %TRUE if the GUI should be shown, %FALSE otherwise.
 **/
gboolean
pika_get_show_gui (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);

  return pika->show_gui;
}

static void
pika_global_config_notify (GObject    *global_config,
                           GParamSpec *param_spec,
                           GObject    *edit_config)
{
  GValue global_value = G_VALUE_INIT;
  GValue edit_value   = G_VALUE_INIT;

  g_value_init (&global_value, param_spec->value_type);
  g_value_init (&edit_value,   param_spec->value_type);

  g_object_get_property (global_config, param_spec->name, &global_value);
  g_object_get_property (edit_config,   param_spec->name, &edit_value);

  if (g_param_values_cmp (param_spec, &global_value, &edit_value))
    {
      g_signal_handlers_block_by_func (edit_config,
                                       pika_edit_config_notify,
                                       global_config);

      g_object_set_property (edit_config, param_spec->name, &global_value);

      g_signal_handlers_unblock_by_func (edit_config,
                                         pika_edit_config_notify,
                                         global_config);
    }

  g_value_unset (&global_value);
  g_value_unset (&edit_value);
}

static void
pika_edit_config_notify (GObject    *edit_config,
                         GParamSpec *param_spec,
                         GObject    *global_config)
{
  GValue edit_value   = G_VALUE_INIT;
  GValue global_value = G_VALUE_INIT;

  g_value_init (&edit_value,   param_spec->value_type);
  g_value_init (&global_value, param_spec->value_type);

  g_object_get_property (edit_config,   param_spec->name, &edit_value);
  g_object_get_property (global_config, param_spec->name, &global_value);

  if (g_param_values_cmp (param_spec, &edit_value, &global_value))
    {
      if (param_spec->flags & PIKA_CONFIG_PARAM_RESTART)
        {
#ifdef PIKA_CONFIG_DEBUG
          g_print ("NOT Applying edit_config change of '%s' to global_config "
                   "because it needs restart\n",
                   param_spec->name);
#endif
        }
      else
        {
#ifdef PIKA_CONFIG_DEBUG
          g_print ("Applying edit_config change of '%s' to global_config\n",
                   param_spec->name);
#endif
          g_signal_handlers_block_by_func (global_config,
                                           pika_global_config_notify,
                                           edit_config);

          g_object_set_property (global_config, param_spec->name, &edit_value);

          g_signal_handlers_unblock_by_func (global_config,
                                             pika_global_config_notify,
                                             edit_config);
        }
    }

  g_value_unset (&edit_value);
  g_value_unset (&global_value);
}

void
pika_load_config (Pika  *pika,
                  GFile *alternate_system_pikarc,
                  GFile *alternate_pikarc)
{
  PikaRc *pikarc;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (alternate_system_pikarc == NULL ||
                    G_IS_FILE (alternate_system_pikarc));
  g_return_if_fail (alternate_pikarc == NULL ||
                    G_IS_FILE (alternate_pikarc));
  g_return_if_fail (pika->config == NULL);
  g_return_if_fail (pika->edit_config == NULL);

  if (pika->be_verbose)
    g_print ("INIT: %s\n", G_STRFUNC);

  /*  this needs to be done before pikarc loading because pikarc can
   *  use user defined units
   */
  pika_unitrc_load (pika);

  pikarc = pika_rc_new (G_OBJECT (pika),
                        alternate_system_pikarc,
                        alternate_pikarc,
                        pika->be_verbose);

  pika->config = PIKA_CORE_CONFIG (pikarc);

  pika->edit_config = pika_config_duplicate (PIKA_CONFIG (pika->config));

  g_signal_connect_object (pika->config, "notify",
                           G_CALLBACK (pika_global_config_notify),
                           pika->edit_config, 0);
  g_signal_connect_object (pika->edit_config, "notify",
                           G_CALLBACK (pika_edit_config_notify),
                           pika->config, 0);

  if (! pika->show_playground)
    {
      gboolean    use_opencl;
      gboolean    use_npd_tool;
      gboolean    use_seamless_clone_tool;

      /* Playground preferences is shown by default for unstable
       * versions and if the associated CLI option was set. Additionally
       * we want to show it if any of the playground options had been
       * enabled. Otherwise you might end up getting blocked with a
       * playground feature and forget where you can even disable it.
       *
       * Also we check this once at start when loading config, and not
       * inside preferences-dialog.c because we don't want to end up
       * with inconsistent behavior where you open once the Preferences,
       * deactivate features, then back to preferences and the tab is
       * gone.
       */

      g_object_get (pika->edit_config,
                    "use-opencl",                     &use_opencl,
                    "playground-npd-tool",            &use_npd_tool,
                    "playground-seamless-clone-tool", &use_seamless_clone_tool,
                    NULL);
      if (use_opencl || use_npd_tool || use_seamless_clone_tool)
        pika->show_playground = TRUE;
    }
}

void
pika_initialize (Pika               *pika,
                 PikaInitStatusFunc  status_callback)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (status_callback != NULL);
  g_return_if_fail (PIKA_IS_CORE_CONFIG (pika->config));

  if (pika->be_verbose)
    g_print ("INIT: %s\n", G_STRFUNC);

  g_signal_emit (pika, pika_signals[INITIALIZE], 0, status_callback);
}

/**
 * pika_restore:
 * @pika: a #Pika object
 * @error: a #GError for uncessful loading.
 *
 * This function always succeeds. If present, @error may be filled for
 * possible feedback on data which failed to load. It doesn't imply any
 * fatale error.
 **/
void
pika_restore (Pika                *pika,
              PikaInitStatusFunc   status_callback,
              GError             **error)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (status_callback != NULL);

  if (pika->be_verbose)
    g_print ("INIT: %s\n", G_STRFUNC);

  /*  initialize  the global parasite table  */
  status_callback (_("Looking for data files"), _("Parasites"), 0.0);
  pika_parasiterc_load (pika);

  /*  initialize the lists of pika brushes, dynamics, patterns etc.  */
  pika_data_factories_load (pika, status_callback);

  /*  initialize the template list  */
  status_callback (NULL, _("Templates"), 0.8);
  pika_templates_load (pika);

  /*  initialize the module list  */
  status_callback (NULL, _("Modules"), 0.9);
  pika_modules_load (pika);

  g_signal_emit (pika, pika_signals[RESTORE], 0, status_callback);

  /* when done, make sure everything is clean, to clean out dirty
   * states from data objects which reference each other and got
   * dirtied by loading the referenced object
   */
  pika_data_factories_data_clean (pika);
}

/**
 * pika_is_restored:
 * @pika: a #Pika object
 *
 * Returns: %TRUE if PIKA is completely started, %FALSE otherwise.
 **/
gboolean
pika_is_restored (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);

  return pika->initialized && pika->restored;
}

/**
 * pika_exit:
 * @pika: a #Pika object
 * @force: whether to force the application to quit
 *
 * Exit this PIKA session. Unless @force is %TRUE, the user is queried
 * whether unsaved images should be saved and can cancel the operation.
 **/
void
pika_exit (Pika     *pika,
           gboolean  force)
{
  gboolean  handled;
  GList    *image_iter;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  if (pika->be_verbose)
    g_print ("EXIT: %s\n", G_STRFUNC);

  g_signal_emit (pika, pika_signals[EXIT], 0,
                 force ? TRUE : FALSE,
                 &handled);

  if (handled)
    return;

  /* Get rid of images without display. We do this *after* handling the
   * usual exit callbacks, because the things that are torn down there
   * might have references to these images (for instance PikaActions
   * in the UI manager).
   */
  while ((image_iter = pika_get_image_iter (pika)))
    {
      PikaImage *image = image_iter->data;

      g_object_unref (image);
    }
}

GList *
pika_get_image_iter (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return PIKA_LIST (pika->images)->queue->head;
}

GList *
pika_get_display_iter (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return PIKA_LIST (pika->displays)->queue->head;
}

GList *
pika_get_image_windows (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return g_list_copy (pika->image_windows);
}

GList *
pika_get_paint_info_iter (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return PIKA_LIST (pika->paint_info_list)->queue->head;
}

GList *
pika_get_tool_info_iter (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return PIKA_LIST (pika->tool_info_list)->queue->head;
}

GList *
pika_get_tool_item_iter (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return PIKA_LIST (pika->tool_item_list)->queue->head;
}

GList *
pika_get_tool_item_ui_iter (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return PIKA_LIST (pika->tool_item_ui_list)->queue->head;
}

PikaObject *
pika_get_clipboard_object (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (pika->clipboard_image)
    return PIKA_OBJECT (pika->clipboard_image);

  return PIKA_OBJECT (pika->clipboard_buffer);
}

void
pika_set_clipboard_image (Pika      *pika,
                          PikaImage *image)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (image == NULL || PIKA_IS_IMAGE (image));

  g_clear_object (&pika->clipboard_buffer);
  g_set_object (&pika->clipboard_image, image);

  /* we want the signal emission */
  g_signal_emit (pika, pika_signals[CLIPBOARD_CHANGED], 0);
}

PikaImage *
pika_get_clipboard_image (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return pika->clipboard_image;
}

void
pika_set_clipboard_buffer (Pika       *pika,
                           PikaBuffer *buffer)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (buffer == NULL || PIKA_IS_BUFFER (buffer));

  g_clear_object (&pika->clipboard_image);
  g_set_object (&pika->clipboard_buffer, buffer);

  /* we want the signal emission */
  g_signal_emit (pika, pika_signals[CLIPBOARD_CHANGED], 0);
}

PikaBuffer *
pika_get_clipboard_buffer (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return pika->clipboard_buffer;
}

PikaImage *
pika_create_image (Pika              *pika,
                   gint               width,
                   gint               height,
                   PikaImageBaseType  type,
                   PikaPrecision      precision,
                   gboolean           attach_comment)
{
  PikaImage *image;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  image = pika_image_new (pika, width, height, type, precision);

  if (attach_comment)
    {
      const gchar *comment;

      comment = pika_template_get_comment (pika->config->default_image);

      if (comment)
        {
          PikaParasite *parasite = pika_parasite_new ("pika-comment",
                                                      PIKA_PARASITE_PERSISTENT,
                                                      strlen (comment) + 1,
                                                      comment);
          pika_image_parasite_attach (image, parasite, FALSE);
          pika_parasite_free (parasite);
        }
    }

  return image;
}

void
pika_set_default_context (Pika        *pika,
                          PikaContext *context)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (context == NULL || PIKA_IS_CONTEXT (context));

  g_set_object (&pika->default_context, context);
}

PikaContext *
pika_get_default_context (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return pika->default_context;
}

void
pika_set_user_context (Pika        *pika,
                       PikaContext *context)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (context == NULL || PIKA_IS_CONTEXT (context));

  g_set_object (&pika->user_context, context);
}

PikaContext *
pika_get_user_context (Pika *pika)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  return pika->user_context;
}

PikaToolInfo *
pika_get_tool_info (Pika        *pika,
                    const gchar *tool_id)
{
  gpointer info;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (tool_id != NULL, NULL);

  info = pika_container_get_child_by_name (pika->tool_info_list, tool_id);

  return (PikaToolInfo *) info;
}

/**
 * pika_message:
 * @pika:     a pointer to the %Pika object
 * @handler:  either a %PikaProgress or a %GtkWidget pointer
 * @severity: severity of the message
 * @format:   printf-like format string
 * @...:      arguments to use with @format
 *
 * Present a message to the user. How exactly the message is displayed
 * depends on the @severity, the @handler object and user preferences.
 **/
void
pika_message (Pika                *pika,
              GObject             *handler,
              PikaMessageSeverity  severity,
              const gchar         *format,
              ...)
{
  va_list args;

  va_start (args, format);

  pika_message_valist (pika, handler, severity, format, args);

  va_end (args);
}

/**
 * pika_message_valist:
 * @pika:     a pointer to the %Pika object
 * @handler:  either a %PikaProgress or a %GtkWidget pointer
 * @severity: severity of the message
 * @format:   printf-like format string
 * @args:     arguments to use with @format
 *
 * See documentation for pika_message().
 **/
void
pika_message_valist (Pika                *pika,
                     GObject             *handler,
                     PikaMessageSeverity  severity,
                     const gchar         *format,
                     va_list              args)
{
  gchar *message;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (handler == NULL || G_IS_OBJECT (handler));
  g_return_if_fail (format != NULL);

  message = g_strdup_vprintf (format, args);

  pika_show_message (pika, handler, severity, NULL, message);

  g_free (message);
}

void
pika_message_literal (Pika                *pika,
                      GObject             *handler,
                      PikaMessageSeverity  severity,
                      const gchar         *message)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (handler == NULL || G_IS_OBJECT (handler));
  g_return_if_fail (message != NULL);

  pika_show_message (pika, handler, severity, NULL, message);
}

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

  g_signal_emit (pika, pika_signals[FILTER_HISTORY_CHANGED], 0);
}

void
pika_image_opened (Pika  *pika,
                   GFile *file)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (G_IS_FILE (file));

  g_signal_emit (pika, pika_signals[IMAGE_OPENED], 0, file);
}

GFile *
pika_get_temp_file (Pika        *pika,
                    const gchar *extension)
{
  static gint  id = 0;
  static gint  pid;
  gchar       *basename;
  GFile       *dir;
  GFile       *file;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  if (id == 0)
    pid = pika_get_pid ();

  if (extension)
    basename = g_strdup_printf ("pika-temp-%d%d.%s", pid, id++, extension);
  else
    basename = g_strdup_printf ("pika-temp-%d%d", pid, id++);

  dir = pika_file_new_for_config_path (PIKA_GEGL_CONFIG (pika->config)->temp_path,
                                       NULL);
  if (! g_file_query_exists (dir, NULL))
    {
      /* Try to make the temp directory if it doesn't exist.
       * Ignore any error.
       */
      g_file_make_directory_with_parents (dir, NULL, NULL);
    }
  file = g_file_get_child (dir, basename);
  g_free (basename);
  g_object_unref (dir);

  return file;
}
