/* 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
 *
 * pikatranslationstore.c
 * Copyright (C) 2008, 2009  Sven Neumann <sven@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 <locale.h>
#include <string.h>

#include <gtk/gtk.h>

#include "libpikabase/pikabase.h"

#include "widgets-types.h"

#include "pikahelp.h"
#include "pikalanguagestore-parser.h"
#include "pikatranslationstore.h"

#include "pika-intl.h"

enum
{
  PROP_0,
  PROP_MANUAL_L18N,
  PROP_EMPTY_LABEL
};

struct _PikaTranslationStoreClass
{
  PikaLanguageStoreClass  parent_class;
};

struct _PikaTranslationStore
{
  PikaLanguageStore  parent_instance;

  gboolean           manual_l18n;
  gchar             *empty_label;
};


static void   pika_translation_store_constructed  (GObject           *object);
static void   pika_translation_store_set_property (GObject           *object,
                                                   guint              property_id,
                                                   const GValue      *value,
                                                   GParamSpec        *pspec);
static void   pika_translation_store_get_property (GObject           *object,
                                                   guint              property_id,
                                                   GValue            *value,
                                                   GParamSpec        *pspec);


G_DEFINE_TYPE (PikaTranslationStore, pika_translation_store,
               PIKA_TYPE_LANGUAGE_STORE)

#define parent_class pika_translation_store_parent_class


static void
pika_translation_store_class_init (PikaTranslationStoreClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);

  object_class->constructed  = pika_translation_store_constructed;
  object_class->set_property = pika_translation_store_set_property;
  object_class->get_property = pika_translation_store_get_property;

  g_object_class_install_property (object_class, PROP_MANUAL_L18N,
                                   g_param_spec_boolean ("manual-l18n", NULL, NULL,
                                                         FALSE,
                                                         PIKA_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));
  g_object_class_install_property (object_class, PROP_EMPTY_LABEL,
                                   g_param_spec_string ("empty-label", NULL, NULL,
                                                         NULL,
                                                         PIKA_PARAM_READWRITE |
                                                         G_PARAM_CONSTRUCT_ONLY));
}

static void
pika_translation_store_init (PikaTranslationStore *store)
{
}

static void
pika_translation_store_constructed (GObject *object)
{
  GHashTable     *lang_list;
  GHashTableIter  lang_iter;
  gpointer        code;
  gpointer        name;
  GList          *sublist = NULL;

  lang_list = pika_language_store_parser_get_languages (TRUE);
  g_return_if_fail (lang_list != NULL);

  if (PIKA_TRANSLATION_STORE (object)->manual_l18n)
    sublist = pika_help_get_installed_languages ();

  g_hash_table_iter_init (&lang_iter, lang_list);

  if (PIKA_TRANSLATION_STORE (object)->manual_l18n &&
      PIKA_TRANSLATION_STORE (object)->empty_label)
    {
      PIKA_LANGUAGE_STORE_GET_CLASS (object)->add (PIKA_LANGUAGE_STORE (object),
                                                   PIKA_TRANSLATION_STORE (object)->empty_label,
                                                   "");
    }
  while (g_hash_table_iter_next (&lang_iter, &code, &name))
    {
      if (! PIKA_TRANSLATION_STORE (object)->manual_l18n ||
          g_list_find_custom (sublist, code, (GCompareFunc) g_strcmp0))
        {
          PIKA_LANGUAGE_STORE_GET_CLASS (object)->add (PIKA_LANGUAGE_STORE (object),
                                                       name, code);
        }
    }
  g_list_free_full (sublist, g_free);
}

static void
pika_translation_store_set_property (GObject      *object,
                                     guint         property_id,
                                     const GValue *value,
                                     GParamSpec   *pspec)
{
  PikaTranslationStore *store = PIKA_TRANSLATION_STORE (object);

  switch (property_id)
    {
    case PROP_MANUAL_L18N:
      store->manual_l18n = g_value_get_boolean (value);
      break;
    case PROP_EMPTY_LABEL:
      store->empty_label = g_value_dup_string (value);
      break;

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

static void
pika_translation_store_get_property (GObject    *object,
                                     guint       property_id,
                                     GValue     *value,
                                     GParamSpec *pspec)
{
  PikaTranslationStore *store = PIKA_TRANSLATION_STORE (object);

  switch (property_id)
    {
    case PROP_MANUAL_L18N:
      g_value_set_boolean (value, store->manual_l18n);
      break;
    case PROP_EMPTY_LABEL:
      g_value_set_string (value, store->empty_label);
      break;

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

GtkListStore *
pika_translation_store_new (gboolean     manual_l18n,
                            const gchar *empty_label)
{
  return g_object_new (PIKA_TYPE_TRANSLATION_STORE,
                       "manual-l18n", manual_l18n,
                       "empty-label", empty_label,
                       NULL);
}
