/* main.c generated by valac 0.26.2, the Vala compiler
 * generated from main.vala, do not modify */

/* vim:set et sts=4 sw=4:
 *
 * ibus - The Input Bus
 *
 * Copyright (c) 2011-2013 Peng Huang <shawn.p.huang@gmail.com>
 * Copyright (c) 2015 Takao Fujiwara <takao.fujiwara1@gmail.com>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
 * USA
 */

#include <glib.h>
#include <glib-object.h>
#include <ibus.h>
#include <stdlib.h>
#include <string.h>
#include <locale.h>
#include <glib/gstdio.h>


#define TYPE_DUMMY_ENGINE (dummy_engine_get_type ())
#define DUMMY_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_DUMMY_ENGINE, DummyEngine))
#define DUMMY_ENGINE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_DUMMY_ENGINE, DummyEngineClass))
#define IS_DUMMY_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_DUMMY_ENGINE))
#define IS_DUMMY_ENGINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_DUMMY_ENGINE))
#define DUMMY_ENGINE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_DUMMY_ENGINE, DummyEngineClass))

typedef struct _DummyEngine DummyEngine;
typedef struct _DummyEngineClass DummyEngineClass;
typedef struct _DummyEnginePrivate DummyEnginePrivate;
typedef struct _Block1Data Block1Data;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
typedef struct _Block2Data Block2Data;
#define _g_free0(var) (var = (g_free (var), NULL))

struct _DummyEngine {
	IBusEngineSimple parent_instance;
	DummyEnginePrivate * priv;
};

struct _DummyEngineClass {
	IBusEngineSimpleClass parent_class;
};

struct _Block1Data {
	int _ref_count_;
	IBusBus* bus;
	gint id;
};

struct _Block2Data {
	int _ref_count_;
	Block1Data * _data1_;
	IBusEngine* engine;
};


static gpointer dummy_engine_parent_class = NULL;

GType dummy_engine_get_type (void) G_GNUC_CONST;
enum  {
	DUMMY_ENGINE_DUMMY_PROPERTY
};
DummyEngine* dummy_engine_new (void);
DummyEngine* dummy_engine_construct (GType object_type);
gint _vala_main (gchar** args, int args_length1);
static Block1Data* block1_data_ref (Block1Data* _data1_);
static void block1_data_unref (void * _userdata_);
static void __lambda4_ (IBusBus* bus);
static void ___lambda4__ibus_bus_disconnected (IBusBus* _sender, gpointer self);
static IBusEngine* __lambda5_ (Block1Data* _data1_, IBusFactory* factory, const gchar* name);
static Block2Data* block2_data_ref (Block2Data* _data2_);
static void block2_data_unref (void * _userdata_);
static gboolean __lambda6_ (Block2Data* _data2_);
static gboolean ___lambda6__gsource_func (gpointer self);
static IBusEngine* ___lambda5__ibus_factory_create_engine (IBusFactory* _sender, const gchar* engine_name, gpointer self);


DummyEngine* dummy_engine_construct (GType object_type) {
	DummyEngine * self = NULL;
	self = (DummyEngine*) g_object_new (object_type, NULL);
	return self;
}


DummyEngine* dummy_engine_new (void) {
	return dummy_engine_construct (TYPE_DUMMY_ENGINE);
}


static void dummy_engine_class_init (DummyEngineClass * klass) {
	dummy_engine_parent_class = g_type_class_peek_parent (klass);
}


static void dummy_engine_instance_init (DummyEngine * self) {
}


GType dummy_engine_get_type (void) {
	static volatile gsize dummy_engine_type_id__volatile = 0;
	if (g_once_init_enter (&dummy_engine_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (DummyEngineClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) dummy_engine_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (DummyEngine), 0, (GInstanceInitFunc) dummy_engine_instance_init, NULL };
		GType dummy_engine_type_id;
		dummy_engine_type_id = g_type_register_static (ibus_engine_simple_get_type (), "DummyEngine", &g_define_type_info, 0);
		g_once_init_leave (&dummy_engine_type_id__volatile, dummy_engine_type_id);
	}
	return dummy_engine_type_id__volatile;
}


static Block1Data* block1_data_ref (Block1Data* _data1_) {
	g_atomic_int_inc (&_data1_->_ref_count_);
	return _data1_;
}


static void block1_data_unref (void * _userdata_) {
	Block1Data* _data1_;
	_data1_ = (Block1Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data1_->_ref_count_)) {
		_g_object_unref0 (_data1_->bus);
		g_slice_free (Block1Data, _data1_);
	}
}


static void __lambda4_ (IBusBus* bus) {
	g_return_if_fail (bus != NULL);
	g_debug ("main.vala:40: bus disconnected");
	ibus_quit ();
}


static void ___lambda4__ibus_bus_disconnected (IBusBus* _sender, gpointer self) {
	__lambda4_ (_sender);
}


static Block2Data* block2_data_ref (Block2Data* _data2_) {
	g_atomic_int_inc (&_data2_->_ref_count_);
	return _data2_;
}


static void block2_data_unref (void * _userdata_) {
	Block2Data* _data2_;
	_data2_ = (Block2Data*) _userdata_;
	if (g_atomic_int_dec_and_test (&_data2_->_ref_count_)) {
		_g_object_unref0 (_data2_->engine);
		block1_data_unref (_data2_->_data1_);
		_data2_->_data1_ = NULL;
		g_slice_free (Block2Data, _data2_);
	}
}


static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


static gboolean __lambda6_ (Block2Data* _data2_) {
	Block1Data* _data1_;
	gboolean result = FALSE;
	IBusEngineSimple* simple = NULL;
	IBusEngine* _tmp0_ = NULL;
	IBusEngineSimple* _tmp1_ = NULL;
	IBusEngineSimple* _tmp2_ = NULL;
	gchar* user_file = NULL;
	gchar* home = NULL;
	const gchar* _tmp3_ = NULL;
	gchar* _tmp4_ = NULL;
	const gchar* _tmp5_ = NULL;
	const gchar* _tmp12_ = NULL;
	gchar* _tmp13_ = NULL;
	const gchar* _tmp14_ = NULL;
	_data1_ = _data2_->_data1_;
	_tmp0_ = _data2_->engine;
	_tmp1_ = _g_object_ref0 (G_TYPE_CHECK_INSTANCE_CAST (_tmp0_, ibus_engine_simple_get_type (), IBusEngineSimple));
	simple = _tmp1_;
	_tmp2_ = simple;
	ibus_engine_simple_add_table_by_locale (_tmp2_, NULL);
	user_file = NULL;
	_tmp3_ = g_get_home_dir ();
	_tmp4_ = g_strdup (_tmp3_);
	home = _tmp4_;
	_tmp5_ = home;
	if (_tmp5_ != NULL) {
		const gchar* _tmp6_ = NULL;
		gchar* _tmp7_ = NULL;
		const gchar* _tmp8_ = NULL;
		gboolean _tmp9_ = FALSE;
		_tmp6_ = home;
		_tmp7_ = g_strconcat (_tmp6_, "/.XCompose", NULL);
		_g_free0 (user_file);
		user_file = _tmp7_;
		_tmp8_ = user_file;
		_tmp9_ = g_file_test (_tmp8_, G_FILE_TEST_EXISTS);
		if (_tmp9_) {
			IBusEngineSimple* _tmp10_ = NULL;
			const gchar* _tmp11_ = NULL;
			_tmp10_ = simple;
			_tmp11_ = user_file;
			ibus_engine_simple_add_compose_file (_tmp10_, _tmp11_);
		}
	}
	_tmp12_ = g_getenv ("XCOMPOSEFILE");
	_tmp13_ = g_strdup (_tmp12_);
	_g_free0 (user_file);
	user_file = _tmp13_;
	_tmp14_ = user_file;
	if (_tmp14_ != NULL) {
		const gchar* _tmp15_ = NULL;
		gboolean _tmp16_ = FALSE;
		_tmp15_ = user_file;
		_tmp16_ = g_file_test (_tmp15_, G_FILE_TEST_EXISTS);
		if (_tmp16_) {
			IBusEngineSimple* _tmp17_ = NULL;
			const gchar* _tmp18_ = NULL;
			_tmp17_ = simple;
			_tmp18_ = user_file;
			ibus_engine_simple_add_compose_file (_tmp17_, _tmp18_);
		}
	}
	result = FALSE;
	_g_free0 (home);
	_g_free0 (user_file);
	_g_object_unref0 (simple);
	return result;
}


static gboolean ___lambda6__gsource_func (gpointer self) {
	gboolean result;
	result = __lambda6_ (self);
	return result;
}


static IBusEngine* __lambda5_ (Block1Data* _data1_, IBusFactory* factory, const gchar* name) {
	IBusEngine* result = NULL;
	Block2Data* _data2_;
	const gchar* _tmp0_ = NULL;
	gboolean _tmp1_ = FALSE;
	static const char path[] = "/org/freedesktop/IBus/engine/simple/%d";
	const gchar* _tmp2_ = NULL;
	gint _tmp3_ = 0;
	gint _tmp4_ = 0;
	gchar* _tmp5_ = NULL;
	gchar* _tmp6_ = NULL;
	IBusBus* _tmp7_ = NULL;
	GDBusConnection* _tmp8_ = NULL;
	IBusEngine* _tmp9_ = NULL;
	IBusEngine* _tmp10_ = NULL;
	IBusEngine* _tmp11_ = NULL;
	IBusEngine* _tmp12_ = NULL;
	g_return_val_if_fail (factory != NULL, NULL);
	g_return_val_if_fail (name != NULL, NULL);
	_data2_ = g_slice_new0 (Block2Data);
	_data2_->_ref_count_ = 1;
	_data2_->_data1_ = block1_data_ref (_data1_);
	_tmp0_ = name;
	_tmp1_ = g_str_has_prefix (_tmp0_, "xkb:");
	if (!_tmp1_) {
		result = NULL;
		block2_data_unref (_data2_);
		_data2_ = NULL;
		return result;
	}
	_tmp2_ = name;
	_tmp3_ = _data1_->id;
	_data1_->id = _tmp3_ + 1;
	_tmp4_ = _data1_->id;
	_tmp5_ = g_strdup_printf (path, _tmp4_);
	_tmp6_ = _tmp5_;
	_tmp7_ = _data1_->bus;
	_tmp8_ = ibus_bus_get_connection (_tmp7_);
	_tmp9_ = ibus_engine_new_with_type (ibus_engine_simple_get_type (), _tmp2_, _tmp6_, _tmp8_);
	g_object_ref_sink (_tmp9_);
	_tmp10_ = _tmp9_;
	_g_free0 (_tmp6_);
	_data2_->engine = _tmp10_;
	g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, ___lambda6__gsource_func, block2_data_ref (_data2_), block2_data_unref);
	_tmp11_ = _data2_->engine;
	_tmp12_ = _g_object_ref0 (_tmp11_);
	result = _tmp12_;
	block2_data_unref (_data2_);
	_data2_ = NULL;
	return result;
}


static IBusEngine* ___lambda5__ibus_factory_create_engine (IBusFactory* _sender, const gchar* engine_name, gpointer self) {
	IBusEngine* result;
	result = __lambda5_ (self, _sender, engine_name);
	return result;
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	Block1Data* _data1_;
	IBusBus* _tmp0_ = NULL;
	IBusBus* _tmp1_ = NULL;
	gboolean _tmp2_ = FALSE;
	IBusBus* _tmp3_ = NULL;
	IBusFactory* factory = NULL;
	IBusBus* _tmp4_ = NULL;
	GDBusConnection* _tmp5_ = NULL;
	IBusFactory* _tmp6_ = NULL;
	IBusFactory* _tmp7_ = NULL;
	guint flags = 0U;
	guint retval = 0U;
	IBusBus* _tmp8_ = NULL;
	guint _tmp9_ = 0U;
	guint32 _tmp10_ = 0U;
	guint _tmp11_ = 0U;
	_data1_ = g_slice_new0 (Block1Data);
	_data1_->_ref_count_ = 1;
	setlocale (LC_ALL, "");
	ibus_init ();
	_tmp0_ = ibus_bus_new ();
	g_object_ref_sink (_tmp0_);
	_data1_->bus = _tmp0_;
	_tmp1_ = _data1_->bus;
	_tmp2_ = ibus_bus_is_connected (_tmp1_);
	if (!_tmp2_) {
		g_warning ("main.vala:35: ibus-daemon does not exist.");
		result = 1;
		block1_data_unref (_data1_);
		_data1_ = NULL;
		return result;
	}
	_tmp3_ = _data1_->bus;
	g_signal_connect (_tmp3_, "disconnected", (GCallback) ___lambda4__ibus_bus_disconnected, NULL);
	_tmp4_ = _data1_->bus;
	_tmp5_ = ibus_bus_get_connection (_tmp4_);
	_tmp6_ = ibus_factory_new (_tmp5_);
	g_object_ref_sink (_tmp6_);
	factory = _tmp6_;
	_data1_->id = 0;
	_tmp7_ = factory;
	g_signal_connect_data (_tmp7_, "create-engine", (GCallback) ___lambda5__ibus_factory_create_engine, block1_data_ref (_data1_), (GClosureNotify) block1_data_unref, 0);
	flags = (guint) (IBUS_BUS_NAME_FLAG_REPLACE_EXISTING | IBUS_BUS_NAME_FLAG_ALLOW_REPLACEMENT);
	_tmp8_ = _data1_->bus;
	_tmp9_ = flags;
	_tmp10_ = ibus_bus_request_name (_tmp8_, "org.freedesktop.IBus.Simple", (guint32) _tmp9_);
	retval = (guint) _tmp10_;
	_tmp11_ = retval;
	if (_tmp11_ == ((guint) 0)) {
		g_warning ("main.vala:100: Registry bus name org.freedesktop.IBus.Simple failed!");
		result = 1;
		_g_object_unref0 (factory);
		block1_data_unref (_data1_);
		_data1_ = NULL;
		return result;
	}
	ibus_main ();
	result = 0;
	_g_object_unref0 (factory);
	block1_data_unref (_data1_);
	_data1_ = NULL;
	return result;
}


int main (int argc, char ** argv) {
#if !GLIB_CHECK_VERSION (2,35,0)
	g_type_init ();
#endif
	return _vala_main (argv, argc);
}



