/* keymap.c generated by valac 0.20.0.2-fd7ed, the Vala compiler
 * generated from keymap.vala, do not modify */

/*
 * Copyright (C) 2011-2012 Daiki Ueno <ueno@unixuser.org>
 * Copyright (C) 2011-2012 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 <glib.h>
#include <glib-object.h>
#include <gee.h>
#include <stdlib.h>
#include <string.h>


#define SKK_TYPE_KEYMAP (skk_keymap_get_type ())
#define SKK_KEYMAP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SKK_TYPE_KEYMAP, SkkKeymap))
#define SKK_KEYMAP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SKK_TYPE_KEYMAP, SkkKeymapClass))
#define SKK_IS_KEYMAP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SKK_TYPE_KEYMAP))
#define SKK_IS_KEYMAP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SKK_TYPE_KEYMAP))
#define SKK_KEYMAP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SKK_TYPE_KEYMAP, SkkKeymapClass))

typedef struct _SkkKeymap SkkKeymap;
typedef struct _SkkKeymapClass SkkKeymapClass;
typedef struct _SkkKeymapPrivate SkkKeymapPrivate;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))

#define SKK_TYPE_KEY_EVENT (skk_key_event_get_type ())
#define SKK_KEY_EVENT(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), SKK_TYPE_KEY_EVENT, SkkKeyEvent))
#define SKK_KEY_EVENT_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), SKK_TYPE_KEY_EVENT, SkkKeyEventClass))
#define SKK_IS_KEY_EVENT(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), SKK_TYPE_KEY_EVENT))
#define SKK_IS_KEY_EVENT_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), SKK_TYPE_KEY_EVENT))
#define SKK_KEY_EVENT_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), SKK_TYPE_KEY_EVENT, SkkKeyEventClass))

typedef struct _SkkKeyEvent SkkKeyEvent;
typedef struct _SkkKeyEventClass SkkKeyEventClass;
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _SkkKeymap {
	GObject parent_instance;
	SkkKeymapPrivate * priv;
};

struct _SkkKeymapClass {
	GObjectClass parent_class;
};

struct _SkkKeymapPrivate {
	GeeMap* entries;
};

typedef enum  {
	SKK_KEY_EVENT_FORMAT_ERROR_PARSE_FAILED,
	SKK_KEY_EVENT_FORMAT_ERROR_KEYSYM_NOT_FOUND
} SkkKeyEventFormatError;
#define SKK_KEY_EVENT_FORMAT_ERROR skk_key_event_format_error_quark ()

static gpointer skk_keymap_parent_class = NULL;

GType skk_keymap_get_type (void) G_GNUC_CONST;
#define SKK_KEYMAP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), SKK_TYPE_KEYMAP, SkkKeymapPrivate))
enum  {
	SKK_KEYMAP_DUMMY_PROPERTY
};
void skk_keymap_set (SkkKeymap* self, const gchar* key, const gchar* command);
GQuark skk_key_event_format_error_quark (void);
SkkKeyEvent* skk_key_event_new_from_string (const gchar* key, GError** error);
SkkKeyEvent* skk_key_event_construct_from_string (GType object_type, const gchar* key, GError** error);
GType skk_key_event_get_type (void) G_GNUC_CONST;
gchar* skk_key_event_to_string (SkkKeyEvent* self);
gchar* skk_keymap_lookup_key (SkkKeymap* self, SkkKeyEvent* key);
SkkKeyEvent* skk_keymap_where_is (SkkKeymap* self, const gchar* command);
SkkKeymap* skk_keymap_new (void);
SkkKeymap* skk_keymap_construct (GType object_type);
static void skk_keymap_finalize (GObject* obj);


void skk_keymap_set (SkkKeymap* self, const gchar* key, const gchar* command) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (key != NULL);
	g_return_if_fail (command != NULL);
	{
		const gchar* _tmp0_;
		SkkKeyEvent* _tmp1_;
		SkkKeyEvent* ev;
		GeeMap* _tmp2_;
		gchar* _tmp3_ = NULL;
		gchar* _tmp4_;
		const gchar* _tmp5_;
		_tmp0_ = key;
		_tmp1_ = skk_key_event_new_from_string (_tmp0_, &_inner_error_);
		ev = _tmp1_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == SKK_KEY_EVENT_FORMAT_ERROR) {
				goto __catch22_skk_key_event_format_error;
			}
			g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
			g_clear_error (&_inner_error_);
			return;
		}
		_tmp2_ = self->priv->entries;
		_tmp3_ = skk_key_event_to_string (ev);
		_tmp4_ = _tmp3_;
		_tmp5_ = command;
		gee_map_set (_tmp2_, _tmp4_, _tmp5_);
		_g_free0 (_tmp4_);
		_g_object_unref0 (ev);
	}
	goto __finally22;
	__catch22_skk_key_event_format_error:
	{
		GError* e = NULL;
		const gchar* _tmp6_;
		GError* _tmp7_;
		const gchar* _tmp8_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp6_ = key;
		_tmp7_ = e;
		_tmp8_ = _tmp7_->message;
		g_warning ("keymap.vala:29: can't get key event from string %s: %s", _tmp6_, _tmp8_);
		_g_error_free0 (e);
	}
	__finally22:
	if (_inner_error_ != NULL) {
		g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
		g_clear_error (&_inner_error_);
		return;
	}
}


gchar* skk_keymap_lookup_key (SkkKeymap* self, SkkKeyEvent* key) {
	gchar* result = NULL;
	GeeMap* _tmp0_;
	SkkKeyEvent* _tmp1_;
	gchar* _tmp2_ = NULL;
	gchar* _tmp3_;
	gpointer _tmp4_ = NULL;
	gchar* _tmp5_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (key != NULL, NULL);
	_tmp0_ = self->priv->entries;
	_tmp1_ = key;
	_tmp2_ = skk_key_event_to_string (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = gee_map_get (_tmp0_, _tmp3_);
	_tmp5_ = (gchar*) _tmp4_;
	_g_free0 (_tmp3_);
	result = _tmp5_;
	return result;
}


SkkKeyEvent* skk_keymap_where_is (SkkKeymap* self, const gchar* command) {
	SkkKeyEvent* result = NULL;
	GeeMap* _tmp0_;
	GeeMapIterator* _tmp1_ = NULL;
	GeeMapIterator* iter;
	GeeMapIterator* _tmp2_;
	gboolean _tmp3_ = FALSE;
	GError * _inner_error_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (command != NULL, NULL);
	_tmp0_ = self->priv->entries;
	_tmp1_ = gee_map_map_iterator (_tmp0_);
	iter = _tmp1_;
	_tmp2_ = iter;
	_tmp3_ = gee_map_iterator_first (_tmp2_);
	if (_tmp3_) {
		{
			gboolean _tmp4_;
			_tmp4_ = TRUE;
			while (TRUE) {
				gboolean _tmp5_;
				GeeMapIterator* _tmp8_;
				gpointer _tmp9_ = NULL;
				gchar* _tmp10_;
				const gchar* _tmp11_;
				gboolean _tmp12_;
				_tmp5_ = _tmp4_;
				if (!_tmp5_) {
					GeeMapIterator* _tmp6_;
					gboolean _tmp7_ = FALSE;
					_tmp6_ = iter;
					_tmp7_ = gee_map_iterator_next (_tmp6_);
					if (!_tmp7_) {
						break;
					}
				}
				_tmp4_ = FALSE;
				_tmp8_ = iter;
				_tmp9_ = gee_map_iterator_get_value (_tmp8_);
				_tmp10_ = (gchar*) _tmp9_;
				_tmp11_ = command;
				_tmp12_ = g_strcmp0 (_tmp10_, _tmp11_) == 0;
				_g_free0 (_tmp10_);
				if (_tmp12_) {
					GeeMapIterator* _tmp13_;
					gpointer _tmp14_ = NULL;
					gchar* key;
					_tmp13_ = iter;
					_tmp14_ = gee_map_iterator_get_key (_tmp13_);
					key = (gchar*) _tmp14_;
					{
						const gchar* _tmp15_;
						SkkKeyEvent* _tmp16_;
						SkkKeyEvent* _tmp17_;
						_tmp15_ = key;
						_tmp16_ = skk_key_event_new_from_string (_tmp15_, &_inner_error_);
						_tmp17_ = _tmp16_;
						if (_inner_error_ != NULL) {
							if (_inner_error_->domain == SKK_KEY_EVENT_FORMAT_ERROR) {
								goto __catch23_skk_key_event_format_error;
							}
							_g_free0 (key);
							_g_object_unref0 (iter);
							g_critical ("file %s: line %d: unexpected error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
							g_clear_error (&_inner_error_);
							return NULL;
						}
						result = _tmp17_;
						_g_free0 (key);
						_g_object_unref0 (iter);
						return result;
					}
					goto __finally23;
					__catch23_skk_key_event_format_error:
					{
						GError* e = NULL;
						const gchar* _tmp18_;
						GError* _tmp19_;
						const gchar* _tmp20_;
						e = _inner_error_;
						_inner_error_ = NULL;
						_tmp18_ = key;
						_tmp19_ = e;
						_tmp20_ = _tmp19_->message;
						g_warning ("keymap.vala:47: can't get key event from string %s: %s", _tmp18_, _tmp20_);
						_g_error_free0 (e);
					}
					__finally23:
					if (_inner_error_ != NULL) {
						_g_free0 (key);
						_g_object_unref0 (iter);
						g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, _inner_error_->message, g_quark_to_string (_inner_error_->domain), _inner_error_->code);
						g_clear_error (&_inner_error_);
						return NULL;
					}
					_g_free0 (key);
				}
			}
		}
	}
	result = NULL;
	_g_object_unref0 (iter);
	return result;
}


SkkKeymap* skk_keymap_construct (GType object_type) {
	SkkKeymap * self = NULL;
	self = (SkkKeymap*) g_object_new (object_type, NULL);
	return self;
}


SkkKeymap* skk_keymap_new (void) {
	return skk_keymap_construct (SKK_TYPE_KEYMAP);
}


static void skk_keymap_class_init (SkkKeymapClass * klass) {
	skk_keymap_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (SkkKeymapPrivate));
	G_OBJECT_CLASS (klass)->finalize = skk_keymap_finalize;
}


static void skk_keymap_instance_init (SkkKeymap * self) {
	GeeHashMap* _tmp0_;
	self->priv = SKK_KEYMAP_GET_PRIVATE (self);
	_tmp0_ = gee_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, g_free, NULL, NULL, NULL);
	self->priv->entries = (GeeMap*) _tmp0_;
}


static void skk_keymap_finalize (GObject* obj) {
	SkkKeymap * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, SKK_TYPE_KEYMAP, SkkKeymap);
	_g_object_unref0 (self->priv->entries);
	G_OBJECT_CLASS (skk_keymap_parent_class)->finalize (obj);
}


GType skk_keymap_get_type (void) {
	static volatile gsize skk_keymap_type_id__volatile = 0;
	if (g_once_init_enter (&skk_keymap_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (SkkKeymapClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) skk_keymap_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (SkkKeymap), 0, (GInstanceInitFunc) skk_keymap_instance_init, NULL };
		GType skk_keymap_type_id;
		skk_keymap_type_id = g_type_register_static (G_TYPE_OBJECT, "SkkKeymap", &g_define_type_info, 0);
		g_once_init_leave (&skk_keymap_type_id__volatile, skk_keymap_type_id);
	}
	return skk_keymap_type_id__volatile;
}



