/* user-dictionary.c generated by valac 0.56.19, the Vala compiler
 * generated from user-dictionary.vala, do not modify */

/*
 * Copyright (C) 2011-2014 Daiki Ueno <ueno@gnu.org>
 * Copyright (C) 2011-2014 Red Hat, Inc.
 *
 * 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 <http://www.gnu.org/licenses/>.
 */

#include "libkkc/libkkc.h"
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib/gstdio.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif

enum  {
	KKC_USER_DICTIONARY_0_PROPERTY,
	KKC_USER_DICTIONARY_NUM_PROPERTIES
};
static GParamSpec* kkc_user_dictionary_properties[KKC_USER_DICTIONARY_NUM_PROPERTIES];
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))

struct _KkcUserDictionaryPrivate {
	KkcUserSegmentDictionary* segment_dict;
	KkcUserSentenceDictionary* sentence_dict;
};

static gint KkcUserDictionary_private_offset;
static gpointer kkc_user_dictionary_parent_class = NULL;
static KkcDictionaryIface * kkc_user_dictionary_kkc_dictionary_parent_iface = NULL;
static KkcSegmentDictionaryIface * kkc_user_dictionary_kkc_segment_dictionary_parent_iface = NULL;
static KkcSentenceDictionaryIface * kkc_user_dictionary_kkc_sentence_dictionary_parent_iface = NULL;

static void kkc_user_dictionary_real_reload (KkcDictionary* base,
                                      GError** error);
static void kkc_user_dictionary_real_save (KkcDictionary* base,
                                    GError** error);
static gboolean kkc_user_dictionary_real_lookup_candidates (KkcSegmentDictionary* base,
                                                     const gchar* midasi,
                                                     gboolean okuri,
                                                     KkcCandidate*** candidates,
                                                     gint* candidates_length1);
static gchar** kkc_user_dictionary_real_complete (KkcSegmentDictionary* base,
                                           const gchar* midasi,
                                           gint* result_length1);
static gboolean kkc_user_dictionary_real_select_candidate (KkcSegmentDictionary* base,
                                                    KkcCandidate* candidate);
static gboolean kkc_user_dictionary_real_purge_candidate (KkcSegmentDictionary* base,
                                                   KkcCandidate* candidate);
static gboolean kkc_user_dictionary_real_lookup_constraint (KkcSentenceDictionary* base,
                                                     const gchar* input,
                                                     gint** constraint,
                                                     gint* constraint_length1);
static gboolean kkc_user_dictionary_real_lookup_phrase (KkcSentenceDictionary* base,
                                                 gchar** input,
                                                 gint input_length1,
                                                 gchar*** phrase,
                                                 gint* phrase_length1);
static gboolean kkc_user_dictionary_real_select_segments (KkcSentenceDictionary* base,
                                                   KkcSegment** input,
                                                   gint input_length1);
static void kkc_user_dictionary_finalize (GObject * obj);
static GType kkc_user_dictionary_get_type_once (void);
static void _vala_array_destroy (gpointer array,
                          gssize array_length,
                          GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array,
                       gssize array_length,
                       GDestroyNotify destroy_func);
static inline gpointer _vala_memdup2 (gconstpointer mem,
                        gsize byte_size);

static inline gpointer
kkc_user_dictionary_get_instance_private (KkcUserDictionary* self)
{
	return G_STRUCT_MEMBER_P (self, KkcUserDictionary_private_offset);
}

KkcUserDictionary*
kkc_user_dictionary_construct (GType object_type,
                               const gchar* basedir,
                               GError** error)
{
	KkcUserDictionary * self = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	KkcUserSegmentDictionary* _tmp2_ = NULL;
	gchar* _tmp3_;
	gchar* _tmp4_;
	KkcUserSegmentDictionary* _tmp5_;
	KkcUserSegmentDictionary* _tmp6_;
	KkcUserSegmentDictionary* _tmp7_;
	KkcUserSentenceDictionary* _tmp8_ = NULL;
	gchar* _tmp9_;
	gchar* _tmp10_;
	KkcUserSentenceDictionary* _tmp11_;
	KkcUserSentenceDictionary* _tmp12_;
	KkcUserSentenceDictionary* _tmp13_;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (basedir != NULL, NULL);
	self = (KkcUserDictionary*) g_object_new (object_type, NULL);
	_tmp0_ = g_path_get_dirname (basedir);
	_tmp1_ = _tmp0_;
	g_mkdir_with_parents (_tmp1_, 448);
	_g_free0 (_tmp1_);
	_tmp3_ = g_build_filename (basedir, "segment", NULL);
	_tmp4_ = _tmp3_;
	_tmp5_ = kkc_user_segment_dictionary_new (_tmp4_, &_inner_error0_);
	_tmp6_ = _tmp5_;
	_g_free0 (_tmp4_);
	_tmp2_ = _tmp6_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp7_ = _tmp2_;
	_tmp2_ = NULL;
	_g_object_unref0 (self->priv->segment_dict);
	self->priv->segment_dict = _tmp7_;
	_tmp9_ = g_build_filename (basedir, "sentence", NULL);
	_tmp10_ = _tmp9_;
	_tmp11_ = kkc_user_sentence_dictionary_new (_tmp10_, &_inner_error0_);
	_tmp12_ = _tmp11_;
	_g_free0 (_tmp10_);
	_tmp8_ = _tmp12_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (_tmp2_);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp13_ = _tmp8_;
	_tmp8_ = NULL;
	_g_object_unref0 (self->priv->sentence_dict);
	self->priv->sentence_dict = _tmp13_;
	_g_object_unref0 (_tmp8_);
	_g_object_unref0 (_tmp2_);
	return self;
}

KkcUserDictionary*
kkc_user_dictionary_new (const gchar* basedir,
                         GError** error)
{
	return kkc_user_dictionary_construct (KKC_TYPE_USER_DICTIONARY, basedir, error);
}

/**
         * {@inheritDoc}
         */
static void
kkc_user_dictionary_real_reload (KkcDictionary* base,
                                 GError** error)
{
	KkcUserDictionary * self;
	KkcUserSegmentDictionary* _tmp0_;
	KkcUserSentenceDictionary* _tmp1_;
	GError* _inner_error0_ = NULL;
	self = (KkcUserDictionary*) base;
	_tmp0_ = self->priv->segment_dict;
	kkc_dictionary_reload ((KkcDictionary*) _tmp0_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
	_tmp1_ = self->priv->sentence_dict;
	kkc_dictionary_reload ((KkcDictionary*) _tmp1_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
}

/**
         * {@inheritDoc}
         */
static void
kkc_user_dictionary_real_save (KkcDictionary* base,
                               GError** error)
{
	KkcUserDictionary * self;
	KkcUserSegmentDictionary* _tmp0_;
	KkcUserSentenceDictionary* _tmp1_;
	GError* _inner_error0_ = NULL;
	self = (KkcUserDictionary*) base;
	_tmp0_ = self->priv->segment_dict;
	kkc_dictionary_save ((KkcDictionary*) _tmp0_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
	_tmp1_ = self->priv->sentence_dict;
	kkc_dictionary_save ((KkcDictionary*) _tmp1_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		return;
	}
}

/**
         * {@inheritDoc}
         */
static gboolean
kkc_user_dictionary_real_lookup_candidates (KkcSegmentDictionary* base,
                                            const gchar* midasi,
                                            gboolean okuri,
                                            KkcCandidate*** candidates,
                                            gint* candidates_length1)
{
	KkcUserDictionary * self;
	KkcCandidate** _vala_candidates = NULL;
	gint _vala_candidates_length1 = 0;
	KkcUserSegmentDictionary* _tmp0_;
	KkcCandidate** _tmp1_ = NULL;
	gint _tmp2_ = 0;
	gboolean _tmp3_;
	gboolean result;
	self = (KkcUserDictionary*) base;
	g_return_val_if_fail (midasi != NULL, FALSE);
	_tmp0_ = self->priv->segment_dict;
	_tmp3_ = kkc_segment_dictionary_lookup_candidates ((KkcSegmentDictionary*) _tmp0_, midasi, okuri, &_tmp1_, &_tmp2_);
	_vala_candidates = (_vala_array_free (_vala_candidates, _vala_candidates_length1, (GDestroyNotify) g_object_unref), NULL);
	_vala_candidates = _tmp1_;
	_vala_candidates_length1 = _tmp2_;
	result = _tmp3_;
	if (candidates) {
		*candidates = _vala_candidates;
	} else {
		_vala_candidates = (_vala_array_free (_vala_candidates, _vala_candidates_length1, (GDestroyNotify) g_object_unref), NULL);
	}
	if (candidates_length1) {
		*candidates_length1 = _vala_candidates_length1;
	}
	return result;
}

/**
         * {@inheritDoc}
         */
static gchar**
kkc_user_dictionary_real_complete (KkcSegmentDictionary* base,
                                   const gchar* midasi,
                                   gint* result_length1)
{
	KkcUserDictionary * self;
	KkcUserSegmentDictionary* _tmp0_;
	gint _tmp1_ = 0;
	gchar** _tmp2_;
	gchar** _tmp3_;
	gint _tmp3__length1;
	gchar** result;
	self = (KkcUserDictionary*) base;
	g_return_val_if_fail (midasi != NULL, NULL);
	_tmp0_ = self->priv->segment_dict;
	_tmp2_ = kkc_segment_dictionary_complete ((KkcSegmentDictionary*) _tmp0_, midasi, &_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp3__length1 = _tmp1_;
	if (result_length1) {
		*result_length1 = _tmp3__length1;
	}
	result = _tmp3_;
	return result;
}

/**
         * {@inheritDoc}
         */
static gboolean
kkc_user_dictionary_real_select_candidate (KkcSegmentDictionary* base,
                                           KkcCandidate* candidate)
{
	KkcUserDictionary * self;
	KkcUserSegmentDictionary* _tmp0_;
	gboolean result;
	self = (KkcUserDictionary*) base;
	g_return_val_if_fail (candidate != NULL, FALSE);
	_tmp0_ = self->priv->segment_dict;
	result = kkc_segment_dictionary_select_candidate ((KkcSegmentDictionary*) _tmp0_, candidate);
	return result;
}

/**
         * {@inheritDoc}
         */
static gboolean
kkc_user_dictionary_real_purge_candidate (KkcSegmentDictionary* base,
                                          KkcCandidate* candidate)
{
	KkcUserDictionary * self;
	KkcUserSegmentDictionary* _tmp0_;
	gboolean result;
	self = (KkcUserDictionary*) base;
	g_return_val_if_fail (candidate != NULL, FALSE);
	_tmp0_ = self->priv->segment_dict;
	result = kkc_segment_dictionary_purge_candidate ((KkcSegmentDictionary*) _tmp0_, candidate);
	return result;
}

/**
         * {@inheritDoc}
         */
static gboolean
kkc_user_dictionary_real_lookup_constraint (KkcSentenceDictionary* base,
                                            const gchar* input,
                                            gint** constraint,
                                            gint* constraint_length1)
{
	KkcUserDictionary * self;
	gint* _vala_constraint = NULL;
	gint _vala_constraint_length1 = 0;
	KkcUserSentenceDictionary* _tmp0_;
	gint* _tmp1_ = NULL;
	gint _tmp2_ = 0;
	gboolean _tmp3_;
	gboolean result;
	self = (KkcUserDictionary*) base;
	g_return_val_if_fail (input != NULL, FALSE);
	_tmp0_ = self->priv->sentence_dict;
	_tmp3_ = kkc_sentence_dictionary_lookup_constraint ((KkcSentenceDictionary*) _tmp0_, input, &_tmp1_, &_tmp2_);
	_vala_constraint = (g_free (_vala_constraint), NULL);
	_vala_constraint = _tmp1_;
	_vala_constraint_length1 = _tmp2_;
	result = _tmp3_;
	if (constraint) {
		*constraint = _vala_constraint;
	} else {
		_vala_constraint = (g_free (_vala_constraint), NULL);
	}
	if (constraint_length1) {
		*constraint_length1 = _vala_constraint_length1;
	}
	return result;
}

/**
         * {@inheritDoc}
         */
static gboolean
kkc_user_dictionary_real_lookup_phrase (KkcSentenceDictionary* base,
                                        gchar** input,
                                        gint input_length1,
                                        gchar*** phrase,
                                        gint* phrase_length1)
{
	KkcUserDictionary * self;
	gchar** _vala_phrase = NULL;
	gint _vala_phrase_length1 = 0;
	KkcUserSentenceDictionary* _tmp0_;
	gchar** _tmp1_ = NULL;
	gint _tmp2_ = 0;
	gboolean _tmp3_;
	gboolean result;
	self = (KkcUserDictionary*) base;
	_tmp0_ = self->priv->sentence_dict;
	_tmp3_ = kkc_sentence_dictionary_lookup_phrase ((KkcSentenceDictionary*) _tmp0_, input, (gint) input_length1, &_tmp1_, &_tmp2_);
	_vala_phrase = (_vala_array_free (_vala_phrase, _vala_phrase_length1, (GDestroyNotify) g_free), NULL);
	_vala_phrase = _tmp1_;
	_vala_phrase_length1 = _tmp2_;
	result = _tmp3_;
	if (phrase) {
		*phrase = _vala_phrase;
	} else {
		_vala_phrase = (_vala_array_free (_vala_phrase, _vala_phrase_length1, (GDestroyNotify) g_free), NULL);
	}
	if (phrase_length1) {
		*phrase_length1 = _vala_phrase_length1;
	}
	return result;
}

/**
         * {@inheritDoc}
         */
static gboolean
kkc_user_dictionary_real_select_segments (KkcSentenceDictionary* base,
                                          KkcSegment** input,
                                          gint input_length1)
{
	KkcUserDictionary * self;
	KkcUserSentenceDictionary* _tmp0_;
	gboolean result;
	self = (KkcUserDictionary*) base;
	_tmp0_ = self->priv->sentence_dict;
	result = kkc_sentence_dictionary_select_segments ((KkcSentenceDictionary*) _tmp0_, input, (gint) input_length1);
	return result;
}

static void
kkc_user_dictionary_class_init (KkcUserDictionaryClass * klass,
                                gpointer klass_data)
{
	kkc_user_dictionary_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &KkcUserDictionary_private_offset);
	G_OBJECT_CLASS (klass)->finalize = kkc_user_dictionary_finalize;
}

static void
kkc_user_dictionary_kkc_dictionary_interface_init (KkcDictionaryIface * iface,
                                                   gpointer iface_data)
{
	kkc_user_dictionary_kkc_dictionary_parent_iface = g_type_interface_peek_parent (iface);
	iface->reload = (void (*) (KkcDictionary*, GError**)) kkc_user_dictionary_real_reload;
	iface->save = (void (*) (KkcDictionary*, GError**)) kkc_user_dictionary_real_save;
}

static void
kkc_user_dictionary_kkc_segment_dictionary_interface_init (KkcSegmentDictionaryIface * iface,
                                                           gpointer iface_data)
{
	kkc_user_dictionary_kkc_segment_dictionary_parent_iface = g_type_interface_peek_parent (iface);
	iface->lookup_candidates = (gboolean (*) (KkcSegmentDictionary*, const gchar*, gboolean, KkcCandidate***, gint*)) kkc_user_dictionary_real_lookup_candidates;
	iface->complete = (gchar** (*) (KkcSegmentDictionary*, const gchar*, gint*)) kkc_user_dictionary_real_complete;
	iface->select_candidate = (gboolean (*) (KkcSegmentDictionary*, KkcCandidate*)) kkc_user_dictionary_real_select_candidate;
	iface->purge_candidate = (gboolean (*) (KkcSegmentDictionary*, KkcCandidate*)) kkc_user_dictionary_real_purge_candidate;
}

static void
kkc_user_dictionary_kkc_sentence_dictionary_interface_init (KkcSentenceDictionaryIface * iface,
                                                            gpointer iface_data)
{
	kkc_user_dictionary_kkc_sentence_dictionary_parent_iface = g_type_interface_peek_parent (iface);
	iface->lookup_constraint = (gboolean (*) (KkcSentenceDictionary*, const gchar*, gint**, gint*)) kkc_user_dictionary_real_lookup_constraint;
	iface->lookup_phrase = (gboolean (*) (KkcSentenceDictionary*, gchar**, gint, gchar***, gint*)) kkc_user_dictionary_real_lookup_phrase;
	iface->select_segments = (gboolean (*) (KkcSentenceDictionary*, KkcSegment**, gint)) kkc_user_dictionary_real_select_segments;
}

static void
kkc_user_dictionary_instance_init (KkcUserDictionary * self,
                                   gpointer klass)
{
	self->priv = kkc_user_dictionary_get_instance_private (self);
}

static void
kkc_user_dictionary_finalize (GObject * obj)
{
	KkcUserDictionary * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, KKC_TYPE_USER_DICTIONARY, KkcUserDictionary);
	_g_object_unref0 (self->priv->segment_dict);
	_g_object_unref0 (self->priv->sentence_dict);
	G_OBJECT_CLASS (kkc_user_dictionary_parent_class)->finalize (obj);
}

/**
     * Helper class which can be used as a single user dictionary.
     *
     * It implements both SegmentDictionary and SentenceDictionary,
     * with write access.
     */
static GType
kkc_user_dictionary_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (KkcUserDictionaryClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kkc_user_dictionary_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KkcUserDictionary), 0, (GInstanceInitFunc) kkc_user_dictionary_instance_init, NULL };
	static const GInterfaceInfo kkc_dictionary_info = { (GInterfaceInitFunc) kkc_user_dictionary_kkc_dictionary_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	static const GInterfaceInfo kkc_segment_dictionary_info = { (GInterfaceInitFunc) kkc_user_dictionary_kkc_segment_dictionary_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	static const GInterfaceInfo kkc_sentence_dictionary_info = { (GInterfaceInitFunc) kkc_user_dictionary_kkc_sentence_dictionary_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	GType kkc_user_dictionary_type_id;
	kkc_user_dictionary_type_id = g_type_register_static (G_TYPE_OBJECT, "KkcUserDictionary", &g_define_type_info, 0);
	g_type_add_interface_static (kkc_user_dictionary_type_id, KKC_TYPE_DICTIONARY, &kkc_dictionary_info);
	g_type_add_interface_static (kkc_user_dictionary_type_id, KKC_TYPE_SEGMENT_DICTIONARY, &kkc_segment_dictionary_info);
	g_type_add_interface_static (kkc_user_dictionary_type_id, KKC_TYPE_SENTENCE_DICTIONARY, &kkc_sentence_dictionary_info);
	KkcUserDictionary_private_offset = g_type_add_instance_private (kkc_user_dictionary_type_id, sizeof (KkcUserDictionaryPrivate));
	return kkc_user_dictionary_type_id;
}

GType
kkc_user_dictionary_get_type (void)
{
	static volatile gsize kkc_user_dictionary_type_id__once = 0;
	if (g_once_init_enter (&kkc_user_dictionary_type_id__once)) {
		GType kkc_user_dictionary_type_id;
		kkc_user_dictionary_type_id = kkc_user_dictionary_get_type_once ();
		g_once_init_leave (&kkc_user_dictionary_type_id__once, kkc_user_dictionary_type_id);
	}
	return kkc_user_dictionary_type_id__once;
}

static void
_vala_array_destroy (gpointer array,
                     gssize array_length,
                     GDestroyNotify destroy_func)
{
	if ((array != NULL) && (destroy_func != NULL)) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}

static void
_vala_array_free (gpointer array,
                  gssize array_length,
                  GDestroyNotify destroy_func)
{
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}

static inline gpointer
_vala_memdup2 (gconstpointer mem,
               gsize byte_size)
{
	gpointer new_mem;
	if (mem && byte_size != 0) {
		new_mem = g_malloc (byte_size);
		memcpy (new_mem, mem, byte_size);
	} else {
		new_mem = NULL;
	}
	return new_mem;
}

