/* 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
 *
 * pika-gegl.c
 * Copyright (C) 2007 Øyvind Kolås <pippin@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 <gegl.h>

#include "libpikaconfig/pikaconfig.h"

#include "pika-gegl-types.h"

#include "config/pikageglconfig.h"

#include "operations/pika-operations.h"

#include "core/pika.h"
#include "core/pika-parallel.h"

#include "pika-babl.h"
#include "pika-gegl.h"

#include <operation/gegl-operation.h>


static void  pika_gegl_notify_temp_path        (PikaGeglConfig *config);
static void  pika_gegl_notify_swap_path        (PikaGeglConfig *config);
static void  pika_gegl_notify_swap_compression (PikaGeglConfig *config);
static void  pika_gegl_notify_tile_cache_size  (PikaGeglConfig *config);
static void  pika_gegl_notify_num_processors   (PikaGeglConfig *config);
static void  pika_gegl_notify_use_opencl       (PikaGeglConfig *config);


/*  public functions  */

void
pika_gegl_init (Pika *pika)
{
  PikaGeglConfig *config;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  config = PIKA_GEGL_CONFIG (pika->config);

  /* make sure temp and swap directories exist */
  pika_gegl_notify_temp_path (config);
  pika_gegl_notify_swap_path (config);

  g_object_set (gegl_config (),
                "swap-compression", config->swap_compression,
                "tile-cache-size",  (guint64) config->tile_cache_size,
                "threads",          config->num_processors,
                "use-opencl",       config->use_opencl,
                NULL);

  pika_parallel_init (pika);

  g_signal_connect (config, "notify::temp-path",
                    G_CALLBACK (pika_gegl_notify_temp_path),
                    NULL);
  g_signal_connect (config, "notify::swap-path",
                    G_CALLBACK (pika_gegl_notify_swap_path),
                    NULL);
  g_signal_connect (config, "notify::swap-compression",
                    G_CALLBACK (pika_gegl_notify_swap_compression),
                    NULL);
  g_signal_connect (config, "notify::num-processors",
                    G_CALLBACK (pika_gegl_notify_num_processors),
                    NULL);
  g_signal_connect (config, "notify::tile-cache-size",
                    G_CALLBACK (pika_gegl_notify_tile_cache_size),
                    NULL);
  g_signal_connect (config, "notify::num-processors",
                    G_CALLBACK (pika_gegl_notify_num_processors),
                    NULL);
  g_signal_connect (config, "notify::use-opencl",
                    G_CALLBACK (pika_gegl_notify_use_opencl),
                    NULL);

  pika_babl_init ();

  pika_operations_init (pika);
}

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

  pika_operations_exit (pika);
  pika_parallel_exit (pika);
}


/*  private functions  */

static void
pika_gegl_notify_temp_path (PikaGeglConfig *config)
{
  GFile *file = pika_file_new_for_config_path (config->temp_path, NULL);

  if (! g_file_query_exists (file, NULL))
    g_file_make_directory_with_parents (file, NULL, NULL);

  g_object_unref (file);
}

static void
pika_gegl_notify_swap_path (PikaGeglConfig *config)
{
  GFile *file = pika_file_new_for_config_path (config->swap_path, NULL);
  gchar *path = g_file_get_path (file);

  if (! g_file_query_exists (file, NULL))
    g_file_make_directory_with_parents (file, NULL, NULL);

  g_object_set (gegl_config (),
                "swap", path,
                NULL);

  g_free (path);
  g_object_unref (file);
}

static void
pika_gegl_notify_swap_compression (PikaGeglConfig *config)
{
  g_object_set (gegl_config (),
                "swap-compression", config->swap_compression,
                NULL);
}

static void
pika_gegl_notify_tile_cache_size (PikaGeglConfig *config)
{
  g_object_set (gegl_config (),
                "tile-cache-size", (guint64) config->tile_cache_size,
                NULL);
}

static void
pika_gegl_notify_num_processors (PikaGeglConfig *config)
{
  g_object_set (gegl_config (),
                "threads", config->num_processors,
                NULL);
}

static void
pika_gegl_notify_use_opencl (PikaGeglConfig *config)
{
  g_object_set (gegl_config (),
                "use-opencl", config->use_opencl,
                NULL);
}
