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


#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <SQLHeavy.h>
#include <SQLHeavyGTK.h>

#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)))



gint _vala_main (gchar** args, int args_length1);
static void _gtk_main_quit_gtk_widget_destroy (GtkWidget* _sender, gpointer self);


static void _gtk_main_quit_gtk_widget_destroy (GtkWidget* _sender, gpointer self) {
	gtk_main_quit ();
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	gchar** _tmp0_;
	gint _tmp0__length1;
	GError * _inner_error_ = NULL;
	gtk_init (&args_length1, &args);
	_tmp0_ = args;
	_tmp0__length1 = args_length1;
	if (_tmp0__length1 != 3) {
		FILE* _tmp1_;
		gchar** _tmp2_;
		gint _tmp2__length1;
		const gchar* _tmp3_;
		_tmp1_ = stderr;
		_tmp2_ = args;
		_tmp2__length1 = args_length1;
		_tmp3_ = _tmp2_[0];
		fprintf (_tmp1_, "Usage: %s database table\n", _tmp3_);
		result = -1;
		return result;
	}
	{
		gchar** _tmp4_;
		gint _tmp4__length1;
		const gchar* _tmp5_;
		SQLHeavyDatabase* _tmp6_;
		SQLHeavyDatabase* db;
		GtkWindow* _tmp7_;
		GtkWindow* _tmp8_;
		GtkWindow* window;
		GtkWindow* _tmp9_;
		GtkWindow* _tmp10_;
		SQLHeavyDatabase* _tmp11_;
		gchar** _tmp12_;
		gint _tmp12__length1;
		const gchar* _tmp13_;
		SQLHeavyTable* _tmp14_ = NULL;
		SQLHeavyTable* table;
		SQLHeavyTable* _tmp15_;
		SQLHeavyGtkModel* _tmp16_;
		SQLHeavyGtkModel* model;
		GtkScrolledWindow* _tmp17_;
		GtkScrolledWindow* _tmp18_;
		GtkScrolledWindow* scroll;
		GtkWindow* _tmp19_;
		GtkScrolledWindow* _tmp20_;
		SQLHeavyGtkModel* _tmp21_;
		GtkTreeView* _tmp22_;
		GtkTreeView* _tmp23_;
		GtkTreeView* view;
		GtkScrolledWindow* _tmp24_;
		GtkTreeView* _tmp25_;
		GtkWindow* _tmp42_;
		_tmp4_ = args;
		_tmp4__length1 = args_length1;
		_tmp5_ = _tmp4_[1];
		_tmp6_ = sql_heavy_database_new (_tmp5_, (SQL_HEAVY_FILE_MODE_READ | SQL_HEAVY_FILE_MODE_WRITE) | SQL_HEAVY_FILE_MODE_CREATE, &_inner_error_);
		db = _tmp6_;
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_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 0;
		}
		_tmp7_ = (GtkWindow*) gtk_window_new (GTK_WINDOW_TOPLEVEL);
		_tmp8_ = g_object_ref_sink (_tmp7_);
		window = _tmp8_;
		_tmp9_ = window;
		gtk_window_set_title (_tmp9_, "SQLHeavy Database Browser");
		_tmp10_ = window;
		g_signal_connect ((GtkWidget*) _tmp10_, "destroy", (GCallback) _gtk_main_quit_gtk_widget_destroy, NULL);
		_tmp11_ = db;
		_tmp12_ = args;
		_tmp12__length1 = args_length1;
		_tmp13_ = _tmp12_[2];
		_tmp14_ = sql_heavy_database_get_table (_tmp11_, _tmp13_, &_inner_error_);
		table = _tmp14_;
		if (_inner_error_ != NULL) {
			_g_object_unref0 (window);
			_g_object_unref0 (db);
			if (_inner_error_->domain == SQL_HEAVY_ERROR) {
				goto __catch0_sql_heavy_error;
			}
			_g_object_unref0 (window);
			_g_object_unref0 (db);
			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 0;
		}
		_tmp15_ = table;
		_tmp16_ = sql_heavy_gtk_model_new (_tmp15_);
		model = _tmp16_;
		_tmp17_ = (GtkScrolledWindow*) gtk_scrolled_window_new (NULL, NULL);
		_tmp18_ = g_object_ref_sink (_tmp17_);
		scroll = _tmp18_;
		_tmp19_ = window;
		_tmp20_ = scroll;
		gtk_container_add ((GtkContainer*) _tmp19_, (GtkWidget*) _tmp20_);
		_tmp21_ = model;
		_tmp22_ = (GtkTreeView*) gtk_tree_view_new_with_model ((GtkTreeModel*) _tmp21_);
		_tmp23_ = g_object_ref_sink (_tmp22_);
		view = _tmp23_;
		_tmp24_ = scroll;
		_tmp25_ = view;
		gtk_container_add ((GtkContainer*) _tmp24_, (GtkWidget*) _tmp25_);
		{
			gint i;
			i = 0;
			{
				gboolean _tmp26_;
				_tmp26_ = TRUE;
				while (TRUE) {
					gboolean _tmp27_;
					gint _tmp29_;
					SQLHeavyTable* _tmp30_;
					gint _tmp31_;
					gint _tmp32_;
					SQLHeavyTable* _tmp33_;
					gint _tmp34_;
					gchar* _tmp35_ = NULL;
					gchar* _tmp36_;
					GtkTreeView* _tmp37_;
					gchar* _tmp38_;
					GtkCellRendererText* _tmp39_;
					GtkCellRendererText* _tmp40_;
					gint _tmp41_;
					_tmp27_ = _tmp26_;
					if (!_tmp27_) {
						gint _tmp28_;
						_tmp28_ = i;
						i = _tmp28_ + 1;
					}
					_tmp26_ = FALSE;
					_tmp29_ = i;
					_tmp30_ = table;
					_tmp31_ = sql_heavy_table_get_field_count (_tmp30_);
					_tmp32_ = _tmp31_;
					if (!(_tmp29_ < _tmp32_)) {
						break;
					}
					_tmp33_ = table;
					_tmp34_ = i;
					_tmp35_ = sql_heavy_table_field_name (_tmp33_, _tmp34_, &_inner_error_);
					_tmp36_ = _tmp35_;
					if (_inner_error_ != NULL) {
						_g_object_unref0 (view);
						_g_object_unref0 (scroll);
						_g_object_unref0 (model);
						_g_object_unref0 (table);
						_g_object_unref0 (window);
						_g_object_unref0 (db);
						if (_inner_error_->domain == SQL_HEAVY_ERROR) {
							goto __catch0_sql_heavy_error;
						}
						_g_object_unref0 (view);
						_g_object_unref0 (scroll);
						_g_object_unref0 (model);
						_g_object_unref0 (table);
						_g_object_unref0 (window);
						_g_object_unref0 (db);
						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 0;
					}
					_tmp37_ = view;
					_tmp38_ = _tmp36_;
					_tmp39_ = (GtkCellRendererText*) gtk_cell_renderer_text_new ();
					_tmp40_ = g_object_ref_sink (_tmp39_);
					_tmp41_ = i;
					gtk_tree_view_insert_column_with_attributes (_tmp37_, -1, _tmp38_, (GtkCellRenderer*) _tmp40_, "text", _tmp41_ + 1, NULL);
					_g_object_unref0 (_tmp40_);
					_g_free0 (_tmp38_);
				}
			}
		}
		_tmp42_ = window;
		gtk_widget_show_all ((GtkWidget*) _tmp42_);
		gtk_main ();
		_g_object_unref0 (view);
		_g_object_unref0 (scroll);
		_g_object_unref0 (model);
		_g_object_unref0 (table);
		_g_object_unref0 (window);
		_g_object_unref0 (db);
	}
	goto __finally0;
	__catch0_sql_heavy_error:
	{
		GError* e = NULL;
		const gchar* _tmp43_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp43_ = e->message;
		g_error ("browser.vala:32: %s", _tmp43_);
		_g_error_free0 (e);
	}
	__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;
}


int main (int argc, char ** argv) {
	g_type_init ();
	return _vala_main (argv, argc);
}



