/* shared-query-test.c generated by valac 0.9.5.4-19a5, the Vala compiler
 * generated from shared-query-test.vala, do not modify */


#include <glib.h>
#include <glib-object.h>
#include <tracker-sparql.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>


#define TYPE_TEST_APP (test_app_get_type ())
#define TEST_APP(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_TEST_APP, TestApp))
#define TEST_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_TEST_APP, TestAppClass))
#define IS_TEST_APP(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_TEST_APP))
#define IS_TEST_APP_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_TEST_APP))
#define TEST_APP_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_TEST_APP, TestAppClass))

typedef struct _TestApp TestApp;
typedef struct _TestAppClass TestAppClass;
typedef struct _TestAppPrivate TestAppPrivate;
#define _g_main_loop_unref0(var) ((var == NULL) ? NULL : (var = (g_main_loop_unref (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
typedef struct _TestAppTestQueryAsyncData TestAppTestQueryAsyncData;
typedef struct _TestAppDoAsyncTestsData TestAppDoAsyncTestsData;

struct _TestApp {
	GObject parent_instance;
	TestAppPrivate * priv;
};

struct _TestAppClass {
	GObjectClass parent_class;
};

struct _TestAppPrivate {
	GMainLoop* loop;
	TrackerSparqlConnection* con;
	gint res;
};

struct _TestAppTestQueryAsyncData {
	int _state_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	TestApp* self;
	TrackerSparqlCursor* cursor;
	TrackerSparqlCursor* _tmp0_;
	TrackerSparqlCursor* _tmp1_;
	GError * e;
	GError * _inner_error_;
};

struct _TestAppDoAsyncTestsData {
	int _state_;
	GAsyncResult* _res_;
	GSimpleAsyncResult* _async_result;
	TestApp* self;
};


static gpointer test_app_parent_class = NULL;

GType test_app_get_type (void) G_GNUC_CONST;
#define TEST_APP_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), TYPE_TEST_APP, TestAppPrivate))
enum  {
	TEST_APP_DUMMY_PROPERTY
};
TestApp* test_app_new (TrackerSparqlConnection* connection);
TestApp* test_app_construct (GType object_type, TrackerSparqlConnection* connection);
static char* test_app_type_to_string (TestApp* self, TrackerSparqlValueType type);
static gint test_app_iter_cursor (TestApp* self, TrackerSparqlCursor* cursor);
static void test_app_test_query (TestApp* self);
static void test_app_test_query_async_data_free (gpointer _data);
static void test_app_test_query_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static void test_app_test_query_async (TestApp* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void test_app_test_query_finish (TestApp* self, GAsyncResult* _res_);
static gboolean test_app_test_query_async_co (TestAppTestQueryAsyncData* data);
static void test_app_do_sync_tests (TestApp* self);
static void test_app_do_async_tests_data_free (gpointer _data);
static void test_app_do_async_tests_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_);
static void test_app_do_async_tests (TestApp* self, GAsyncReadyCallback _callback_, gpointer _user_data_);
static void test_app_do_async_tests_finish (TestApp* self, GAsyncResult* _res_);
static gboolean test_app_do_async_tests_co (TestAppDoAsyncTestsData* data);
static gboolean test_app_in_mainloop (TestApp* self);
static gboolean _test_app_in_mainloop_gsource_func (gpointer self);
gint test_app_run (TestApp* self);
static void test_app_finalize (GObject* obj);



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


TestApp* test_app_construct (GType object_type, TrackerSparqlConnection* connection) {
	TestApp * self;
	TrackerSparqlConnection* _tmp0_;
	g_return_val_if_fail (connection != NULL, NULL);
	self = (TestApp*) g_object_new (object_type, NULL);
	self->priv->con = (_tmp0_ = _g_object_ref0 (connection), _g_object_unref0 (self->priv->con), _tmp0_);
	return self;
}


TestApp* test_app_new (TrackerSparqlConnection* connection) {
	return test_app_construct (TYPE_TEST_APP, connection);
}


static char* test_app_type_to_string (TestApp* self, TrackerSparqlValueType type) {
	char* result = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	switch (type) {
		case TRACKER_SPARQL_VALUE_TYPE_UNBOUND:
		{
			result = g_strdup ("unbound");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_URI:
		{
			result = g_strdup ("uri");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_STRING:
		{
			result = g_strdup ("string");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_INTEGER:
		{
			result = g_strdup ("integer");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_DOUBLE:
		{
			result = g_strdup ("double");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_DATETIME:
		{
			result = g_strdup ("datetime");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_BLANK_NODE:
		{
			result = g_strdup ("blank-node");
			return result;
		}
		case TRACKER_SPARQL_VALUE_TYPE_BOOLEAN:
		{
			result = g_strdup ("boolean");
			return result;
		}
		default:
		{
			break;
		}
	}
	result = g_strdup ("unknown");
	return result;
}


static gint test_app_iter_cursor (TestApp* self, TrackerSparqlCursor* cursor) {
	gint result = 0;
	GError * _inner_error_;
	gint i = 0;
	g_return_val_if_fail (self != NULL, 0);
	g_return_val_if_fail (cursor != NULL, 0);
	_inner_error_ = NULL;
	{
		{
			gboolean _tmp0_;
			i = 0;
			_tmp0_ = TRUE;
			while (TRUE) {
				if (!_tmp0_) {
					i++;
				}
				_tmp0_ = FALSE;
				if (!(i < tracker_sparql_cursor_get_n_columns (cursor))) {
					break;
				}
				g_print ("| %s ", tracker_sparql_cursor_get_variable_name (cursor, i));
			}
		}
		g_print ("| -> %d columns\n", tracker_sparql_cursor_get_n_columns (cursor));
		while (TRUE) {
			gboolean _tmp1_;
			_tmp1_ = tracker_sparql_cursor_next (cursor, NULL, &_inner_error_);
			if (_inner_error_ != NULL) {
				goto __catch0_g_error;
			}
			if (!_tmp1_) {
				break;
			}
			{
				gboolean _tmp2_;
				i = 0;
				_tmp2_ = TRUE;
				while (TRUE) {
					const char* _tmp3_;
					char* _tmp4_;
					if (!_tmp2_) {
						i++;
					}
					_tmp2_ = FALSE;
					if (!(i < tracker_sparql_cursor_get_n_columns (cursor))) {
						break;
					}
					_tmp3_ = NULL;
					if (i != 0) {
						_tmp3_ = ",";
					} else {
						_tmp3_ = "";
					}
					g_print ("%s%s a %s", _tmp3_, tracker_sparql_cursor_get_string (cursor, i, NULL), _tmp4_ = test_app_type_to_string (self, tracker_sparql_cursor_get_value_type (cursor, i)));
					_g_free0 (_tmp4_);
				}
			}
			g_print ("\n");
		}
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_warning ("shared-query-test.vala:56: Couldn't iterate query results: %s", e->message);
			result = -1;
			_g_error_free0 (e);
			return result;
		}
	}
	__finally0:
	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 0;
	}
	result = 0;
	return result;
}


static void test_app_test_query (TestApp* self) {
	GError * _inner_error_;
	TrackerSparqlCursor* cursor;
	g_return_if_fail (self != NULL);
	_inner_error_ = NULL;
	cursor = NULL;
	g_print ("Sync test\n");
	{
		TrackerSparqlCursor* _tmp0_;
		TrackerSparqlCursor* _tmp1_;
		_tmp0_ = tracker_sparql_connection_query (self->priv->con, "SELECT ?u WHERE { ?u a rdfs:Class }", NULL, &_inner_error_);
		if (_inner_error_ != NULL) {
			goto __catch1_g_error;
		}
		cursor = (_tmp1_ = _tmp0_, _g_object_unref0 (cursor), _tmp1_);
	}
	goto __finally1;
	__catch1_g_error:
	{
		GError * e;
		e = _inner_error_;
		_inner_error_ = NULL;
		{
			g_warning ("shared-query-test.vala:70: Couldn't perform query: %s", e->message);
			self->priv->res = -1;
			_g_error_free0 (e);
			_g_object_unref0 (cursor);
			return;
		}
	}
	__finally1:
	if (_inner_error_ != NULL) {
		_g_object_unref0 (cursor);
		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;
	}
	self->priv->res = test_app_iter_cursor (self, cursor);
	if (self->priv->res == (-1)) {
		_g_object_unref0 (cursor);
		return;
	}
	g_print ("\nRewinding\n");
	tracker_sparql_cursor_rewind (cursor);
	g_print ("\nSecond run\n");
	self->priv->res = test_app_iter_cursor (self, cursor);
	_g_object_unref0 (cursor);
}


static void test_app_test_query_async_data_free (gpointer _data) {
	TestAppTestQueryAsyncData* data;
	data = _data;
	g_object_unref (data->self);
	g_slice_free (TestAppTestQueryAsyncData, data);
}


static void test_app_test_query_async (TestApp* self, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	TestAppTestQueryAsyncData* _data_;
	_data_ = g_slice_new0 (TestAppTestQueryAsyncData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, test_app_test_query_async);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, test_app_test_query_async_data_free);
	_data_->self = g_object_ref (self);
	test_app_test_query_async_co (_data_);
}


static void test_app_test_query_finish (TestApp* self, GAsyncResult* _res_) {
	TestAppTestQueryAsyncData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void test_app_test_query_async_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	TestAppTestQueryAsyncData* data;
	data = _user_data_;
	data->_res_ = _res_;
	test_app_test_query_async_co (data);
}


static gboolean test_app_test_query_async_co (TestAppTestQueryAsyncData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		case 1:
		goto _state_1;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		g_print ("Async test\n");
		{
			data->_state_ = 1;
			tracker_sparql_connection_query_async (data->self->priv->con, "SELECT ?u WHERE { ?u a rdfs:Class }", NULL, test_app_test_query_async_ready, data);
			return FALSE;
			_state_1:
			data->_tmp0_ = tracker_sparql_connection_query_finish (data->self->priv->con, data->_res_, &data->_inner_error_);
			if (data->_inner_error_ != NULL) {
				goto __catch2_g_error;
			}
			data->cursor = (data->_tmp1_ = data->_tmp0_, _g_object_unref0 (data->cursor), data->_tmp1_);
		}
		goto __finally2;
		__catch2_g_error:
		{
			data->e = data->_inner_error_;
			data->_inner_error_ = NULL;
			{
				g_warning ("shared-query-test.vala:94: Couldn't perform query: %s", data->e->message);
				data->self->priv->res = -1;
				_g_error_free0 (data->e);
				_g_object_unref0 (data->cursor);
				{
					if (data->_state_ == 0) {
						g_simple_async_result_complete_in_idle (data->_async_result);
					} else {
						g_simple_async_result_complete (data->_async_result);
					}
					g_object_unref (data->_async_result);
					return FALSE;
				}
				_g_error_free0 (data->e);
			}
		}
		__finally2:
		if (data->_inner_error_ != NULL) {
			_g_object_unref0 (data->cursor);
			g_critical ("file %s: line %d: uncaught error: %s (%s, %d)", __FILE__, __LINE__, data->_inner_error_->message, g_quark_to_string (data->_inner_error_->domain), data->_inner_error_->code);
			g_clear_error (&data->_inner_error_);
			return FALSE;
		}
		data->self->priv->res = test_app_iter_cursor (data->self, data->cursor);
		if (data->self->priv->res == (-1)) {
			_g_object_unref0 (data->cursor);
			{
				if (data->_state_ == 0) {
					g_simple_async_result_complete_in_idle (data->_async_result);
				} else {
					g_simple_async_result_complete (data->_async_result);
				}
				g_object_unref (data->_async_result);
				return FALSE;
			}
		}
		g_print ("\nRewinding\n");
		tracker_sparql_cursor_rewind (data->cursor);
		g_print ("\nSecond run\n");
		data->self->priv->res = test_app_iter_cursor (data->self, data->cursor);
		_g_object_unref0 (data->cursor);
	}
	{
		if (data->_state_ == 0) {
			g_simple_async_result_complete_in_idle (data->_async_result);
		} else {
			g_simple_async_result_complete (data->_async_result);
		}
		g_object_unref (data->_async_result);
		return FALSE;
	}
}


static void test_app_do_sync_tests (TestApp* self) {
	g_return_if_fail (self != NULL);
	test_app_test_query (self);
}


static void test_app_do_async_tests_data_free (gpointer _data) {
	TestAppDoAsyncTestsData* data;
	data = _data;
	g_object_unref (data->self);
	g_slice_free (TestAppDoAsyncTestsData, data);
}


static void test_app_do_async_tests (TestApp* self, GAsyncReadyCallback _callback_, gpointer _user_data_) {
	TestAppDoAsyncTestsData* _data_;
	_data_ = g_slice_new0 (TestAppDoAsyncTestsData);
	_data_->_async_result = g_simple_async_result_new (G_OBJECT (self), _callback_, _user_data_, test_app_do_async_tests);
	g_simple_async_result_set_op_res_gpointer (_data_->_async_result, _data_, test_app_do_async_tests_data_free);
	_data_->self = g_object_ref (self);
	test_app_do_async_tests_co (_data_);
}


static void test_app_do_async_tests_finish (TestApp* self, GAsyncResult* _res_) {
	TestAppDoAsyncTestsData* _data_;
	_data_ = g_simple_async_result_get_op_res_gpointer (G_SIMPLE_ASYNC_RESULT (_res_));
}


static void test_app_do_async_tests_ready (GObject* source_object, GAsyncResult* _res_, gpointer _user_data_) {
	TestAppDoAsyncTestsData* data;
	data = _user_data_;
	data->_res_ = _res_;
	test_app_do_async_tests_co (data);
}


static gboolean test_app_do_async_tests_co (TestAppDoAsyncTestsData* data) {
	switch (data->_state_) {
		case 0:
		goto _state_0;
		case 2:
		goto _state_2;
		default:
		g_assert_not_reached ();
	}
	_state_0:
	{
		data->_state_ = 2;
		test_app_test_query_async (data->self, test_app_do_async_tests_ready, data);
		return FALSE;
		_state_2:
		test_app_test_query_finish (data->self, data->_res_);
		g_print ("Async tests done, now I can quit the mainloop\n");
		g_main_loop_quit (data->self->priv->loop);
	}
	{
		if (data->_state_ == 0) {
			g_simple_async_result_complete_in_idle (data->_async_result);
		} else {
			g_simple_async_result_complete (data->_async_result);
		}
		g_object_unref (data->_async_result);
		return FALSE;
	}
}


static gboolean test_app_in_mainloop (TestApp* self) {
	gboolean result = FALSE;
	g_return_val_if_fail (self != NULL, FALSE);
	test_app_do_sync_tests (self);
	test_app_do_async_tests (self, NULL, NULL);
	result = FALSE;
	return result;
}


static gboolean _test_app_in_mainloop_gsource_func (gpointer self) {
	gboolean result;
	result = test_app_in_mainloop (self);
	return result;
}


gint test_app_run (TestApp* self) {
	gint result = 0;
	GMainLoop* _tmp0_;
	g_return_val_if_fail (self != NULL, 0);
	self->priv->loop = (_tmp0_ = g_main_loop_new (NULL, FALSE), _g_main_loop_unref0 (self->priv->loop), _tmp0_);
	g_idle_add_full (G_PRIORITY_DEFAULT_IDLE, _test_app_in_mainloop_gsource_func, g_object_ref (self), g_object_unref);
	g_main_loop_run (self->priv->loop);
	result = self->priv->res;
	return result;
}


static void test_app_class_init (TestAppClass * klass) {
	test_app_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (TestAppPrivate));
	G_OBJECT_CLASS (klass)->finalize = test_app_finalize;
}


static void test_app_instance_init (TestApp * self) {
	self->priv = TEST_APP_GET_PRIVATE (self);
	self->priv->res = 0;
}


static void test_app_finalize (GObject* obj) {
	TestApp * self;
	self = TEST_APP (obj);
	_g_main_loop_unref0 (self->priv->loop);
	_g_object_unref0 (self->priv->con);
	G_OBJECT_CLASS (test_app_parent_class)->finalize (obj);
}


GType test_app_get_type (void) {
	static volatile gsize test_app_type_id__volatile = 0;
	if (g_once_init_enter (&test_app_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (TestAppClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) test_app_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (TestApp), 0, (GInstanceInitFunc) test_app_instance_init, NULL };
		GType test_app_type_id;
		test_app_type_id = g_type_register_static (G_TYPE_OBJECT, "TestApp", &g_define_type_info, 0);
		g_once_init_leave (&test_app_type_id__volatile, test_app_type_id);
	}
	return test_app_type_id__volatile;
}




