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

/* 
 * Copyright (C) 2013 Daiki Ueno <ueno@gnu.org>
 * Copyright (C) 2013 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 2
 * 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
 * 02110-1301, USA.
 */

#include "libkkc/libkkc.h"
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <glib/gstdio.h>
#include <json-glib/json-glib.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_RULE_0_PROPERTY,
	KKC_USER_RULE_NUM_PROPERTIES
};
static GParamSpec* kkc_user_rule_properties[KKC_USER_RULE_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))
#define __vala_JsonNode_free0(var) ((var == NULL) ? NULL : (var = (_vala_JsonNode_free (var), NULL)))
#define _g_type_class_unref0(var) ((var == NULL) ? NULL : (var = (g_type_class_unref (var), NULL)))

struct _KkcUserRulePrivate {
	KkcRuleMetadata* parent;
	gchar* path;
};

static gint KkcUserRule_private_offset;
static gpointer kkc_user_rule_parent_class = NULL;

static void kkc_user_rule_write_files (KkcRuleMetadata* parent,
                                const gchar* path,
                                const gchar* name,
                                GError** error);
static JsonBuilder* kkc_user_rule_create_metadata (KkcRuleMetadata* parent,
                                            const gchar* name);
static void _vala_JsonNode_free (JsonNode* self);
static GEnumValue* _g_enum_value_dup (GEnumValue* self);
static JsonBuilder* kkc_user_rule_create_keymap (KkcRuleMetadata* parent,
                                          const gchar* name,
                                          KkcKeymap* keymap);
static JsonBuilder* kkc_user_rule_create_rom_kana (KkcRuleMetadata* parent,
                                            const gchar* name);
static void _vala_KkcKeymapEntry_array_free (KkcKeymapEntry * array,
                                      gssize array_length);
static void kkc_user_rule_finalize (GObject * obj);
static GType kkc_user_rule_get_type_once (void);

static inline gpointer
kkc_user_rule_get_instance_private (KkcUserRule* self)
{
	return G_STRUCT_MEMBER_P (self, KkcUserRule_private_offset);
}

/**
         * Create a new UserRule.
         *
         * @param parent metadata of the parent rule
         * @param base_dir base directory where this user rule is saved
         * @param prefix a string prepended to the rule name
         */
static gpointer
_g_object_ref0 (gpointer self)
{
	return self ? g_object_ref (self) : NULL;
}

KkcUserRule*
kkc_user_rule_construct (GType object_type,
                         KkcRuleMetadata* parent,
                         const gchar* base_dir,
                         const gchar* prefix,
                         GError** error)
{
	KkcUserRule * self = NULL;
	gchar* path = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* name = NULL;
	gchar* _tmp3_;
	gchar* _tmp4_;
	const gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	const gchar* _tmp9_;
	KkcRuleMetadata* metadata = NULL;
	const gchar* _tmp12_;
	const gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	KkcRuleMetadata* _tmp16_;
	KkcRuleMetadata* _tmp17_;
	KkcRuleMetadata* _tmp18_;
	const gchar* _tmp19_;
	gchar* _tmp20_;
	KkcRuleMetadata* _tmp21_;
	GError* _inner_error0_ = NULL;
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (base_dir != NULL, NULL);
	g_return_val_if_fail (prefix != NULL, NULL);
	_tmp0_ = kkc_metadata_file_get_name ((KkcMetadataFile*) parent);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_build_filename (base_dir, _tmp1_, NULL);
	path = _tmp2_;
	_tmp3_ = g_strconcat (prefix, ":", NULL);
	_tmp4_ = _tmp3_;
	_tmp5_ = kkc_metadata_file_get_name ((KkcMetadataFile*) parent);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strconcat (_tmp4_, _tmp6_, NULL);
	_tmp8_ = _tmp7_;
	_g_free0 (_tmp4_);
	name = _tmp8_;
	_tmp9_ = path;
	if (!g_file_test (_tmp9_, G_FILE_TEST_IS_DIR)) {
		const gchar* _tmp10_;
		const gchar* _tmp11_;
		_tmp10_ = path;
		_tmp11_ = name;
		kkc_user_rule_write_files (parent, _tmp10_, _tmp11_, &_inner_error0_);
		if (G_UNLIKELY (_inner_error0_ != NULL)) {
			g_propagate_error (error, _inner_error0_);
			_g_free0 (name);
			_g_free0 (path);
			_g_object_unref0 (self);
			return NULL;
		}
	}
	_tmp12_ = name;
	_tmp13_ = path;
	_tmp14_ = g_build_filename (_tmp13_, "metadata.json", NULL);
	_tmp15_ = _tmp14_;
	_tmp16_ = kkc_rule_metadata_new (_tmp12_, _tmp15_, &_inner_error0_);
	_tmp17_ = _tmp16_;
	_g_free0 (_tmp15_);
	metadata = _tmp17_;
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_free0 (name);
		_g_free0 (path);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp18_ = metadata;
	self = (KkcUserRule*) kkc_rule_construct (object_type, _tmp18_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_object_unref0 (metadata);
		_g_free0 (name);
		_g_free0 (path);
		_g_object_unref0 (self);
		return NULL;
	}
	_tmp19_ = path;
	_tmp20_ = g_strdup (_tmp19_);
	_g_free0 (self->priv->path);
	self->priv->path = _tmp20_;
	_tmp21_ = _g_object_ref0 (parent);
	_g_object_unref0 (self->priv->parent);
	self->priv->parent = _tmp21_;
	_g_object_unref0 (metadata);
	_g_free0 (name);
	_g_free0 (path);
	return self;
}

KkcUserRule*
kkc_user_rule_new (KkcRuleMetadata* parent,
                   const gchar* base_dir,
                   const gchar* prefix,
                   GError** error)
{
	return kkc_user_rule_construct (KKC_TYPE_USER_RULE, parent, base_dir, prefix, error);
}

static void
_vala_JsonNode_free (JsonNode* self)
{
	g_boxed_free (json_node_get_type (), self);
}

static GEnumValue*
_g_enum_value_dup (GEnumValue* self)
{
	GEnumValue* dup;
	dup = g_new0 (GEnumValue, 1);
	memcpy (dup, self, sizeof (GEnumValue));
	return dup;
}

static gpointer
__g_enum_value_dup0 (gpointer self)
{
	return self ? _g_enum_value_dup (self) : NULL;
}

static void
kkc_user_rule_write_files (KkcRuleMetadata* parent,
                           const gchar* path,
                           const gchar* name,
                           GError** error)
{
	JsonGenerator* generator = NULL;
	JsonGenerator* _tmp0_;
	JsonGenerator* _tmp1_;
	JsonBuilder* metadata_builder = NULL;
	JsonBuilder* _tmp2_;
	JsonGenerator* _tmp3_;
	JsonBuilder* _tmp4_;
	JsonNode* _tmp5_;
	JsonNode* _tmp6_;
	gchar* metadata_filename = NULL;
	gchar* _tmp7_;
	JsonGenerator* _tmp8_;
	const gchar* _tmp9_;
	gchar* keymap_path = NULL;
	gchar* _tmp10_;
	const gchar* _tmp11_;
	GEnumClass* enum_class = NULL;
	GTypeClass* _tmp12_;
	gchar* rom_kana_path = NULL;
	gchar* _tmp35_;
	const gchar* _tmp36_;
	JsonBuilder* rom_kana_builder = NULL;
	JsonBuilder* _tmp37_;
	JsonGenerator* _tmp38_;
	JsonBuilder* _tmp39_;
	JsonNode* _tmp40_;
	JsonNode* _tmp41_;
	gchar* rom_kana_filename = NULL;
	const gchar* _tmp42_;
	gchar* _tmp43_;
	JsonGenerator* _tmp44_;
	const gchar* _tmp45_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (parent != NULL);
	g_return_if_fail (path != NULL);
	g_return_if_fail (name != NULL);
	_tmp0_ = json_generator_new ();
	generator = _tmp0_;
	_tmp1_ = generator;
	json_generator_set_pretty (_tmp1_, TRUE);
	g_mkdir_with_parents (path, 448);
	_tmp2_ = kkc_user_rule_create_metadata (parent, name);
	metadata_builder = _tmp2_;
	_tmp3_ = generator;
	_tmp4_ = metadata_builder;
	_tmp5_ = json_builder_get_root (_tmp4_);
	_tmp6_ = _tmp5_;
	json_generator_set_root (_tmp3_, _tmp6_);
	__vala_JsonNode_free0 (_tmp6_);
	_tmp7_ = g_build_filename (path, "metadata.json", NULL);
	metadata_filename = _tmp7_;
	_tmp8_ = generator;
	_tmp9_ = metadata_filename;
	json_generator_to_file (_tmp8_, _tmp9_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_free0 (metadata_filename);
		_g_object_unref0 (metadata_builder);
		_g_object_unref0 (generator);
		return;
	}
	_tmp10_ = g_build_filename (path, "keymap", NULL);
	keymap_path = _tmp10_;
	_tmp11_ = keymap_path;
	g_mkdir_with_parents (_tmp11_, 448);
	_tmp12_ = g_type_class_ref (KKC_TYPE_INPUT_MODE);
	enum_class = (GEnumClass*) _tmp12_;
	{
		gint i = 0;
		GEnumClass* _tmp13_;
		_tmp13_ = enum_class;
		i = _tmp13_->minimum;
		{
			gboolean _tmp14_ = FALSE;
			_tmp14_ = TRUE;
			while (TRUE) {
				GEnumClass* _tmp16_;
				GEnumValue* enum_value = NULL;
				GEnumClass* _tmp17_;
				GEnumValue* _tmp18_;
				JsonBuilder* keymap_builder = NULL;
				GEnumValue* _tmp19_;
				const gchar* _tmp20_;
				JsonBuilder* _tmp21_;
				JsonGenerator* _tmp22_;
				JsonBuilder* _tmp23_;
				JsonNode* _tmp24_;
				JsonNode* _tmp25_;
				gchar* keymap_filename = NULL;
				const gchar* _tmp26_;
				GEnumValue* _tmp27_;
				const gchar* _tmp28_;
				gchar* _tmp29_;
				gchar* _tmp30_;
				gchar* _tmp31_;
				gchar* _tmp32_;
				JsonGenerator* _tmp33_;
				const gchar* _tmp34_;
				if (!_tmp14_) {
					gint _tmp15_;
					_tmp15_ = i;
					i = _tmp15_ + 1;
				}
				_tmp14_ = FALSE;
				_tmp16_ = enum_class;
				if (!(i <= _tmp16_->maximum)) {
					break;
				}
				_tmp17_ = enum_class;
				_tmp18_ = __g_enum_value_dup0 (g_enum_get_value (_tmp17_, i));
				enum_value = _tmp18_;
				_tmp19_ = enum_value;
				_tmp20_ = (*_tmp19_).value_nick;
				_tmp21_ = kkc_user_rule_create_keymap (parent, _tmp20_, NULL);
				keymap_builder = _tmp21_;
				_tmp22_ = generator;
				_tmp23_ = keymap_builder;
				_tmp24_ = json_builder_get_root (_tmp23_);
				_tmp25_ = _tmp24_;
				json_generator_set_root (_tmp22_, _tmp25_);
				__vala_JsonNode_free0 (_tmp25_);
				_tmp26_ = keymap_path;
				_tmp27_ = enum_value;
				_tmp28_ = (*_tmp27_).value_nick;
				_tmp29_ = g_strdup_printf ("%s.json", _tmp28_);
				_tmp30_ = _tmp29_;
				_tmp31_ = g_build_filename (_tmp26_, _tmp30_, NULL);
				_tmp32_ = _tmp31_;
				_g_free0 (_tmp30_);
				keymap_filename = _tmp32_;
				_tmp33_ = generator;
				_tmp34_ = keymap_filename;
				json_generator_to_file (_tmp33_, _tmp34_, &_inner_error0_);
				if (G_UNLIKELY (_inner_error0_ != NULL)) {
					g_propagate_error (error, _inner_error0_);
					_g_free0 (keymap_filename);
					_g_object_unref0 (keymap_builder);
					_g_free0 (enum_value);
					_g_type_class_unref0 (enum_class);
					_g_free0 (keymap_path);
					_g_free0 (metadata_filename);
					_g_object_unref0 (metadata_builder);
					_g_object_unref0 (generator);
					return;
				}
				_g_free0 (keymap_filename);
				_g_object_unref0 (keymap_builder);
				_g_free0 (enum_value);
			}
		}
	}
	_tmp35_ = g_build_filename (path, "rom-kana", NULL);
	rom_kana_path = _tmp35_;
	_tmp36_ = rom_kana_path;
	g_mkdir_with_parents (_tmp36_, 448);
	_tmp37_ = kkc_user_rule_create_rom_kana (parent, "default");
	rom_kana_builder = _tmp37_;
	_tmp38_ = generator;
	_tmp39_ = rom_kana_builder;
	_tmp40_ = json_builder_get_root (_tmp39_);
	_tmp41_ = _tmp40_;
	json_generator_set_root (_tmp38_, _tmp41_);
	__vala_JsonNode_free0 (_tmp41_);
	_tmp42_ = rom_kana_path;
	_tmp43_ = g_build_filename (_tmp42_, "default.json", NULL);
	rom_kana_filename = _tmp43_;
	_tmp44_ = generator;
	_tmp45_ = rom_kana_filename;
	json_generator_to_file (_tmp44_, _tmp45_, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_free0 (rom_kana_filename);
		_g_object_unref0 (rom_kana_builder);
		_g_free0 (rom_kana_path);
		_g_type_class_unref0 (enum_class);
		_g_free0 (keymap_path);
		_g_free0 (metadata_filename);
		_g_object_unref0 (metadata_builder);
		_g_object_unref0 (generator);
		return;
	}
	_g_free0 (rom_kana_filename);
	_g_object_unref0 (rom_kana_builder);
	_g_free0 (rom_kana_path);
	_g_type_class_unref0 (enum_class);
	_g_free0 (keymap_path);
	_g_free0 (metadata_filename);
	_g_object_unref0 (metadata_builder);
	_g_object_unref0 (generator);
}

static JsonBuilder*
kkc_user_rule_create_metadata (KkcRuleMetadata* parent,
                               const gchar* name)
{
	JsonBuilder* builder = NULL;
	JsonBuilder* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	const gchar* _tmp3_;
	const gchar* _tmp4_;
	gint _tmp5_;
	gint _tmp6_;
	JsonBuilder* result;
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = json_builder_new ();
	builder = _tmp0_;
	json_builder_begin_object (builder);
	json_builder_set_member_name (builder, "name");
	json_builder_add_string_value (builder, name);
	json_builder_set_member_name (builder, "description");
	_tmp1_ = kkc_metadata_file_get_description ((KkcMetadataFile*) parent);
	_tmp2_ = _tmp1_;
	json_builder_add_string_value (builder, _tmp2_);
	json_builder_set_member_name (builder, "filter");
	_tmp3_ = kkc_rule_metadata_get_filter (parent);
	_tmp4_ = _tmp3_;
	json_builder_add_string_value (builder, _tmp4_);
	json_builder_set_member_name (builder, "priority");
	_tmp5_ = kkc_rule_metadata_get_priority (parent);
	_tmp6_ = _tmp5_;
	json_builder_add_int_value (builder, (gint64) _tmp6_);
	json_builder_end_object (builder);
	result = builder;
	return result;
}

static void
_vala_KkcKeymapEntry_array_free (KkcKeymapEntry * array,
                                 gssize array_length)
{
	if (array != NULL) {
		gssize i;
		for (i = 0; i < array_length; i = i + 1) {
			kkc_keymap_entry_destroy (&array[i]);
		}
	}
	g_free (array);
}

static JsonBuilder*
kkc_user_rule_create_keymap (KkcRuleMetadata* parent,
                             const gchar* name,
                             KkcKeymap* keymap)
{
	JsonBuilder* builder = NULL;
	JsonBuilder* _tmp0_;
	JsonBuilder* _tmp1_;
	JsonBuilder* _tmp2_;
	JsonBuilder* _tmp3_;
	JsonBuilder* _tmp4_;
	const gchar* _tmp5_;
	const gchar* _tmp6_;
	gchar* _tmp7_;
	gchar* _tmp8_;
	gchar* _tmp9_;
	gchar* _tmp10_;
	JsonBuilder* _tmp11_;
	JsonBuilder* _tmp34_;
	JsonBuilder* result;
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = json_builder_new ();
	builder = _tmp0_;
	_tmp1_ = builder;
	json_builder_begin_object (_tmp1_);
	_tmp2_ = builder;
	json_builder_set_member_name (_tmp2_, "include");
	_tmp3_ = builder;
	json_builder_begin_array (_tmp3_);
	_tmp4_ = builder;
	_tmp5_ = kkc_metadata_file_get_name ((KkcMetadataFile*) parent);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_strconcat (_tmp6_, "/", NULL);
	_tmp8_ = _tmp7_;
	_tmp9_ = g_strconcat (_tmp8_, name, NULL);
	_tmp10_ = _tmp9_;
	json_builder_add_string_value (_tmp4_, _tmp10_);
	_g_free0 (_tmp10_);
	_g_free0 (_tmp8_);
	_tmp11_ = builder;
	json_builder_end_array (_tmp11_);
	if (keymap != NULL) {
		JsonBuilder* _tmp12_;
		JsonBuilder* _tmp13_;
		JsonBuilder* _tmp14_;
		JsonBuilder* _tmp15_;
		KkcKeymapEntry* entries = NULL;
		gint _tmp16_ = 0;
		KkcKeymapEntry* _tmp17_;
		gint entries_length1;
		gint _entries_size_;
		KkcKeymapEntry* _tmp18_;
		gint _tmp18__length1;
		JsonBuilder* _tmp32_;
		JsonBuilder* _tmp33_;
		_tmp12_ = builder;
		json_builder_set_member_name (_tmp12_, "define");
		_tmp13_ = builder;
		json_builder_begin_object (_tmp13_);
		_tmp14_ = builder;
		json_builder_set_member_name (_tmp14_, "keymap");
		_tmp15_ = builder;
		json_builder_begin_object (_tmp15_);
		_tmp17_ = kkc_keymap_local_entries (keymap, &_tmp16_);
		entries = _tmp17_;
		entries_length1 = _tmp16_;
		_entries_size_ = entries_length1;
		_tmp18_ = entries;
		_tmp18__length1 = entries_length1;
		{
			KkcKeymapEntry* entry_collection = NULL;
			gint entry_collection_length1 = 0;
			gint _entry_collection_size_ = 0;
			gint entry_it = 0;
			entry_collection = _tmp18_;
			entry_collection_length1 = _tmp18__length1;
			for (entry_it = 0; entry_it < entry_collection_length1; entry_it = entry_it + 1) {
				KkcKeymapEntry _tmp19_;
				KkcKeymapEntry _tmp20_ = {0};
				KkcKeymapEntry entry = {0};
				_tmp19_ = entry_collection[entry_it];
				kkc_keymap_entry_copy (&_tmp19_, &_tmp20_);
				entry = _tmp20_;
				{
					JsonBuilder* _tmp21_;
					KkcKeymapEntry _tmp22_;
					KkcKeyEvent* _tmp23_;
					gchar* _tmp24_;
					gchar* _tmp25_;
					KkcKeymapEntry _tmp26_;
					const gchar* _tmp27_;
					_tmp21_ = builder;
					_tmp22_ = entry;
					_tmp23_ = _tmp22_.key;
					_tmp24_ = kkc_key_event_to_string (_tmp23_);
					_tmp25_ = _tmp24_;
					json_builder_set_member_name (_tmp21_, _tmp25_);
					_g_free0 (_tmp25_);
					_tmp26_ = entry;
					_tmp27_ = _tmp26_.command;
					if (_tmp27_ == NULL) {
						JsonBuilder* _tmp28_;
						_tmp28_ = builder;
						json_builder_add_null_value (_tmp28_);
					} else {
						JsonBuilder* _tmp29_;
						KkcKeymapEntry _tmp30_;
						const gchar* _tmp31_;
						_tmp29_ = builder;
						_tmp30_ = entry;
						_tmp31_ = _tmp30_.command;
						json_builder_add_string_value (_tmp29_, _tmp31_);
					}
					kkc_keymap_entry_destroy (&entry);
				}
			}
		}
		_tmp32_ = builder;
		json_builder_end_object (_tmp32_);
		_tmp33_ = builder;
		json_builder_end_object (_tmp33_);
		entries = (_vala_KkcKeymapEntry_array_free (entries, entries_length1), NULL);
	}
	_tmp34_ = builder;
	json_builder_end_object (_tmp34_);
	result = builder;
	return result;
}

static JsonBuilder*
kkc_user_rule_create_rom_kana (KkcRuleMetadata* parent,
                               const gchar* name)
{
	JsonBuilder* builder = NULL;
	JsonBuilder* _tmp0_;
	const gchar* _tmp1_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	gchar* _tmp4_;
	gchar* _tmp5_;
	gchar* _tmp6_;
	JsonBuilder* result;
	g_return_val_if_fail (parent != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_tmp0_ = json_builder_new ();
	builder = _tmp0_;
	json_builder_begin_object (builder);
	json_builder_set_member_name (builder, "include");
	json_builder_begin_array (builder);
	_tmp1_ = kkc_metadata_file_get_name ((KkcMetadataFile*) parent);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_strconcat (_tmp2_, "/", NULL);
	_tmp4_ = _tmp3_;
	_tmp5_ = g_strconcat (_tmp4_, name, NULL);
	_tmp6_ = _tmp5_;
	json_builder_add_string_value (builder, _tmp6_);
	_g_free0 (_tmp6_);
	_g_free0 (_tmp4_);
	json_builder_end_array (builder);
	json_builder_end_object (builder);
	result = builder;
	return result;
}

/**
         * Save keymap for given input mode.
         *
         * @param input_mode input mode
         */
void
kkc_user_rule_write (KkcUserRule* self,
                     KkcInputMode input_mode,
                     GError** error)
{
	GEnumClass* enum_class = NULL;
	GTypeClass* _tmp0_;
	gchar* keymap_name = NULL;
	const gchar* _tmp1_;
	gchar* _tmp2_;
	gchar* keymap_path = NULL;
	const gchar* _tmp3_;
	gchar* _tmp4_;
	JsonGenerator* generator = NULL;
	JsonGenerator* _tmp5_;
	JsonBuilder* builder = NULL;
	KkcRuleMetadata* _tmp6_;
	KkcKeymap* _tmp7_;
	KkcKeymap* _tmp8_;
	JsonBuilder* _tmp9_;
	JsonBuilder* _tmp10_;
	JsonNode* _tmp11_;
	JsonNode* _tmp12_;
	gchar* filename = NULL;
	gchar* _tmp13_;
	gchar* _tmp14_;
	gchar* _tmp15_;
	gchar* _tmp16_;
	GError* _inner_error0_ = NULL;
	g_return_if_fail (self != NULL);
	_tmp0_ = g_type_class_ref (KKC_TYPE_INPUT_MODE);
	enum_class = (GEnumClass*) _tmp0_;
	_tmp1_ = (*g_enum_get_value (enum_class, (gint) input_mode)).value_nick;
	_tmp2_ = g_strdup (_tmp1_);
	keymap_name = _tmp2_;
	_tmp3_ = self->priv->path;
	_tmp4_ = g_build_filename (_tmp3_, "keymap", NULL);
	keymap_path = _tmp4_;
	g_mkdir_with_parents (keymap_path, 448);
	_tmp5_ = json_generator_new ();
	generator = _tmp5_;
	json_generator_set_pretty (generator, TRUE);
	_tmp6_ = self->priv->parent;
	_tmp7_ = kkc_rule_get_keymap ((KkcRule*) self, input_mode);
	_tmp8_ = _tmp7_;
	_tmp9_ = kkc_user_rule_create_keymap (_tmp6_, keymap_name, _tmp8_);
	_tmp10_ = _tmp9_;
	_g_object_unref0 (_tmp8_);
	builder = _tmp10_;
	_tmp11_ = json_builder_get_root (builder);
	_tmp12_ = _tmp11_;
	json_generator_set_root (generator, _tmp12_);
	__vala_JsonNode_free0 (_tmp12_);
	_tmp13_ = g_strdup_printf ("%s.json", keymap_name);
	_tmp14_ = _tmp13_;
	_tmp15_ = g_build_filename (keymap_path, _tmp14_, NULL);
	_tmp16_ = _tmp15_;
	_g_free0 (_tmp14_);
	filename = _tmp16_;
	json_generator_to_file (generator, filename, &_inner_error0_);
	if (G_UNLIKELY (_inner_error0_ != NULL)) {
		g_propagate_error (error, _inner_error0_);
		_g_free0 (filename);
		_g_object_unref0 (builder);
		_g_object_unref0 (generator);
		_g_free0 (keymap_path);
		_g_free0 (keymap_name);
		_g_type_class_unref0 (enum_class);
		return;
	}
	_g_free0 (filename);
	_g_object_unref0 (builder);
	_g_object_unref0 (generator);
	_g_free0 (keymap_path);
	_g_free0 (keymap_name);
	_g_type_class_unref0 (enum_class);
}

static void
kkc_user_rule_class_init (KkcUserRuleClass * klass,
                          gpointer klass_data)
{
	kkc_user_rule_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &KkcUserRule_private_offset);
	G_OBJECT_CLASS (klass)->finalize = kkc_user_rule_finalize;
}

static void
kkc_user_rule_instance_init (KkcUserRule * self,
                             gpointer klass)
{
	self->priv = kkc_user_rule_get_instance_private (self);
}

static void
kkc_user_rule_finalize (GObject * obj)
{
	KkcUserRule * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, KKC_TYPE_USER_RULE, KkcUserRule);
	_g_object_unref0 (self->priv->parent);
	_g_free0 (self->priv->path);
	G_OBJECT_CLASS (kkc_user_rule_parent_class)->finalize (obj);
}

/**
     * Object representing a writable typing rule.
     */
static GType
kkc_user_rule_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (KkcUserRuleClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) kkc_user_rule_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (KkcUserRule), 0, (GInstanceInitFunc) kkc_user_rule_instance_init, NULL };
	GType kkc_user_rule_type_id;
	kkc_user_rule_type_id = g_type_register_static (KKC_TYPE_RULE, "KkcUserRule", &g_define_type_info, 0);
	KkcUserRule_private_offset = g_type_add_instance_private (kkc_user_rule_type_id, sizeof (KkcUserRulePrivate));
	return kkc_user_rule_type_id;
}

GType
kkc_user_rule_get_type (void)
{
	static volatile gsize kkc_user_rule_type_id__once = 0;
	if (g_once_init_enter (&kkc_user_rule_type_id__once)) {
		GType kkc_user_rule_type_id;
		kkc_user_rule_type_id = kkc_user_rule_get_type_once ();
		g_once_init_leave (&kkc_user_rule_type_id__once, kkc_user_rule_type_id);
	}
	return kkc_user_rule_type_id__once;
}

