/* gimpparasite.c: Copyright 1998 Jay Cox <jaycox@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 "libpikabase/pikabase.h"
#include "libpikaconfig/pikaconfig.h"

#include "core-types.h"

#include "pika.h"
#include "pika-parasites.h"
#include "pikaparasitelist.h"


gboolean
pika_parasite_validate (Pika                *pika,
                        const PikaParasite  *parasite,
                        GError             **error)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), FALSE);
  g_return_val_if_fail (parasite != NULL, FALSE);
  g_return_val_if_fail (error == NULL || *error == NULL, FALSE);

  return TRUE;
}

void
pika_parasite_attach (Pika               *pika,
                      const PikaParasite *parasite)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (parasite != NULL);

  pika_parasite_list_add (pika->parasites, parasite);
}

void
pika_parasite_detach (Pika        *pika,
                      const gchar *name)
{
  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (name != NULL);

  pika_parasite_list_remove (pika->parasites, name);
}

const PikaParasite *
pika_parasite_find (Pika        *pika,
                    const gchar *name)
{
  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);
  g_return_val_if_fail (name != NULL, NULL);

  return pika_parasite_list_find (pika->parasites, name);
}

static void
list_func (const gchar    *key,
           PikaParasite   *parasite,
           gchar        ***current)
{
  *(*current)++ = g_strdup (key);
}

gchar **
pika_parasite_list (Pika *pika)
{
  gint    count;
  gchar **list;
  gchar **current;

  g_return_val_if_fail (PIKA_IS_PIKA (pika), NULL);

  count = pika_parasite_list_length (pika->parasites);

  list = current = g_new0 (gchar *, count + 1);

  pika_parasite_list_foreach (pika->parasites, (GHFunc) list_func, &current);

  return list;
}


/*  FIXME: this doesn't belong here  */

void
pika_parasite_shift_parent (PikaParasite *parasite)
{
  g_return_if_fail (parasite != NULL);

  parasite->flags = (parasite->flags >> 8);
}


/*  parasiterc functions  */

void
pika_parasiterc_load (Pika *pika)
{
  GFile  *file;
  GError *error = NULL;

  g_return_if_fail (PIKA_IS_PIKA (pika));

  file = pika_directory_file ("parasiterc", NULL);

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

  if (! pika_config_deserialize_file (PIKA_CONFIG (pika->parasites),
                                      file, NULL, &error))
    {
      if (error->code != PIKA_CONFIG_ERROR_OPEN_ENOENT)
        pika_message_literal (pika, NULL, PIKA_MESSAGE_ERROR, error->message);

      g_error_free (error);
    }

  g_object_unref (file);
}

void
pika_parasiterc_save (Pika *pika)
{
  const gchar *header =
    "PIKA parasiterc\n"
    "\n"
    "This file will be entirely rewritten each time you exit.";
  const gchar *footer =
    "end of parasiterc";

  GFile  *file;
  GError *error = NULL;

  g_return_if_fail (PIKA_IS_PIKA (pika));
  g_return_if_fail (PIKA_IS_PARASITE_LIST (pika->parasites));

  file = pika_directory_file ("parasiterc", NULL);

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

  if (! pika_config_serialize_to_file (PIKA_CONFIG (pika->parasites),
                                       file,
                                       header, footer, NULL,
                                       &error))
    {
      pika_message_literal (pika, NULL, PIKA_MESSAGE_ERROR, error->message);
      g_error_free (error);
    }

  g_object_unref (file);
}
