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

/*
 * This file is part of LaTeXila.
 *
 * Copyright © 2010-2011 Sébastien Wilmet
 *
 * LaTeXila 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.
 *
 * LaTeXila 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 LaTeXila.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include "config.h"
#include <glib/gi18n-lib.h>
#include <gtk/gtk.h>
#include <stdio.h>
#include <unique/unique.h>
#include <gio/gio.h>

#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_option_context_free0(var) ((var == NULL) ? NULL : (var = (g_option_context_free (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _unique_message_data_free0(var) ((var == NULL) ? NULL : (var = (unique_message_data_free (var), NULL)))

#define TYPE_LATEXILA (latexila_get_type ())
#define LATEXILA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_LATEXILA, Latexila))
#define LATEXILA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_LATEXILA, LatexilaClass))
#define IS_LATEXILA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_LATEXILA))
#define IS_LATEXILA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), TYPE_LATEXILA))
#define LATEXILA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), TYPE_LATEXILA, LatexilaClass))

typedef struct _Latexila Latexila;
typedef struct _LatexilaClass LatexilaClass;


extern gboolean option_version;
gboolean option_version = FALSE;
extern gboolean option_new_document;
gboolean option_new_document = FALSE;
extern gboolean option_new_window;
gboolean option_new_window = FALSE;
extern gchar** remaining_args;
gchar** remaining_args = NULL;
extern gint latexila_NEW_WINDOW;

gint _vala_main (gchar** args, int args_length1);
static gboolean _vala_string_array_contains (gchar** stack, int stack_length, gchar* needle);
static void _vala_array_add14 (gchar*** array, int* length, int* size, gchar* value);
GType latexila_get_type (void) G_GNUC_CONST;
Latexila* latexila_get_default (void);
void latexila_open_documents (Latexila* self, gchar** uris);
void latexila_create_document (Latexila* self);
UniqueResponse latexila_message (Latexila* self, UniqueApp* sender, gint command, UniqueMessageData* data, guint time);
static UniqueResponse _latexila_message_unique_app_message_received (UniqueApp* _sender, gint command, UniqueMessageData* message_data, guint time_, gpointer self);
static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func);
static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func);
static gint _vala_array_length (gpointer array);

const GOptionEntry options[5] = {{"version", 'V', 0, G_OPTION_ARG_NONE, &option_version, "Show the application's version", NULL}, {"new-document", 'n', 0, G_OPTION_ARG_NONE, &option_new_document, "Create new document", NULL}, {"new-window", '\0', 0, G_OPTION_ARG_NONE, &option_new_window, "Create a new top-level window in an existing instance of LaTeXila", NULL}, {"", '\0', 0, G_OPTION_ARG_FILENAME_ARRAY, &remaining_args, NULL, "[FILE...]"}, {NULL}};

static gboolean _vala_string_array_contains (gchar** stack, int stack_length, gchar* needle) {
	int i;
	for (i = 0; i < stack_length; i++) {
		if (g_strcmp0 (stack[i], needle) == 0) {
			return TRUE;
		}
	}
	return FALSE;
}


static void _vala_array_add14 (gchar*** array, int* length, int* size, gchar* value) {
	if ((*length) == (*size)) {
		*size = (*size) ? (2 * (*size)) : 4;
		*array = g_renew (gchar*, *array, (*size) + 1);
	}
	(*array)[(*length)++] = value;
	(*array)[*length] = NULL;
}


static UniqueResponse _latexila_message_unique_app_message_received (UniqueApp* _sender, gint command, UniqueMessageData* message_data, guint time_, gpointer self) {
	UniqueResponse result;
	result = latexila_message (self, _sender, command, message_data, time_);
	return result;
}


gint _vala_main (gchar** args, int args_length1) {
	gint result = 0;
	const gchar* _tmp0_ = NULL;
	gchar* _tmp1_;
	gchar* data_dirs_env;
	const gchar* _tmp2_;
	const gchar* _tmp4_;
	gchar** _tmp5_;
	gchar** _tmp6_ = NULL;
	gchar** data_dirs;
	gint data_dirs_length1;
	gint _data_dirs_size_;
	gchar** _tmp7_;
	gint _tmp7__length1;
	const gchar* _tmp11_ = NULL;
	GOptionContext* _tmp12_;
	GOptionContext* context;
	GOptionContext* _tmp13_;
	GOptionContext* _tmp14_;
	GOptionGroup* _tmp15_ = NULL;
	gboolean _tmp23_;
	gboolean command_open;
	UniqueMessageData* _tmp25_;
	UniqueMessageData* data;
	gchar** _tmp26_;
	gint _tmp26__length1;
	UniqueApp* _tmp37_;
	UniqueApp* app;
	UniqueApp* _tmp38_;
	gint _tmp39_;
	UniqueApp* _tmp40_;
	gboolean _tmp41_ = FALSE;
	gboolean _tmp42_;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_getenv ("XDG_DATA_DIRS");
	_tmp1_ = g_strdup (_tmp0_);
	data_dirs_env = _tmp1_;
	_tmp2_ = data_dirs_env;
	if (_tmp2_ == NULL) {
		gchar* _tmp3_;
		_tmp3_ = g_strdup ("/usr/local/share:/usr/share");
		_g_free0 (data_dirs_env);
		data_dirs_env = _tmp3_;
	}
	_tmp4_ = data_dirs_env;
	_tmp6_ = _tmp5_ = g_strsplit (_tmp4_, ":", 0);
	data_dirs = _tmp6_;
	data_dirs_length1 = _vala_array_length (_tmp5_);
	_data_dirs_size_ = data_dirs_length1;
	_tmp7_ = data_dirs;
	_tmp7__length1 = data_dirs_length1;
	if (!_vala_string_array_contains (_tmp7_, _tmp7__length1, SCHEMA_DIR)) {
		const gchar* _tmp8_;
		gchar* _tmp9_;
		gchar* _tmp10_;
		_tmp8_ = data_dirs_env;
		_tmp9_ = g_strconcat (SCHEMA_DIR ":", _tmp8_, NULL);
		_tmp10_ = _tmp9_;
		g_setenv ("XDG_DATA_DIRS", _tmp10_, TRUE);
		_g_free0 (_tmp10_);
	}
	bindtextdomain (GETTEXT_PACKAGE, LOCALE_DIR);
	bind_textdomain_codeset (GETTEXT_PACKAGE, "UTF-8");
	textdomain (GETTEXT_PACKAGE);
	gtk_init (&args_length1, &args);
	_tmp11_ = _ ("- Integrated LaTeX Environment for GNOME");
	_tmp12_ = g_option_context_new (_tmp11_);
	context = _tmp12_;
	_tmp13_ = context;
	g_option_context_add_main_entries (_tmp13_, options, GETTEXT_PACKAGE);
	_tmp14_ = context;
	_tmp15_ = gtk_get_option_group (FALSE);
	g_option_context_add_group (_tmp14_, _tmp15_);
	{
		GOptionContext* _tmp16_;
		_tmp16_ = context;
		g_option_context_parse (_tmp16_, &args_length1, &args, &_inner_error_);
		if (_inner_error_ != NULL) {
			if (_inner_error_->domain == G_OPTION_ERROR) {
				goto __catch57_g_option_error;
			}
			_g_option_context_free0 (context);
			data_dirs = (_vala_array_free (data_dirs, data_dirs_length1, (GDestroyNotify) g_free), NULL);
			_g_free0 (data_dirs_env);
			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;
		}
	}
	goto __finally57;
	__catch57_g_option_error:
	{
		GError* e = NULL;
		GError* _tmp17_;
		const gchar* _tmp18_;
		FILE* _tmp19_;
		const gchar* _tmp20_ = NULL;
		gchar** _tmp21_;
		gint _tmp21__length1;
		const gchar* _tmp22_;
		e = _inner_error_;
		_inner_error_ = NULL;
		_tmp17_ = e;
		_tmp18_ = _tmp17_->message;
		g_warning ("main.vala:83: %s", _tmp18_);
		_tmp19_ = stderr;
		_tmp20_ = _ ("Run '%s --help' to see a full list of available command line options.\n");
		_tmp21_ = args;
		_tmp21__length1 = args_length1;
		_tmp22_ = _tmp21_[0];
		fprintf (_tmp19_, _tmp20_, _tmp22_);
		result = 1;
		_g_error_free0 (e);
		_g_option_context_free0 (context);
		data_dirs = (_vala_array_free (data_dirs, data_dirs_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (data_dirs_env);
		return result;
	}
	__finally57:
	if (_inner_error_ != NULL) {
		_g_option_context_free0 (context);
		data_dirs = (_vala_array_free (data_dirs, data_dirs_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (data_dirs_env);
		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;
	}
	_tmp23_ = option_version;
	if (_tmp23_) {
		FILE* _tmp24_;
		_tmp24_ = stdout;
		fprintf (_tmp24_, "%s %s\n", APP_NAME, APP_VERSION);
		result = 0;
		_g_option_context_free0 (context);
		data_dirs = (_vala_array_free (data_dirs, data_dirs_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (data_dirs_env);
		return result;
	}
	command_open = FALSE;
	_tmp25_ = unique_message_data_new ();
	data = _tmp25_;
	_tmp26_ = remaining_args;
	_tmp26__length1 = _vala_array_length (remaining_args);
	if (_tmp26__length1 != 0) {
		gchar** _tmp27_ = NULL;
		gchar** uris;
		gint uris_length1;
		gint _uris_size_;
		gchar** _tmp28_;
		gint _tmp28__length1;
		UniqueMessageData* _tmp35_;
		gchar** _tmp36_;
		gint _tmp36__length1;
		command_open = TRUE;
		_tmp27_ = g_new0 (gchar*, 0 + 1);
		uris = _tmp27_;
		uris_length1 = 0;
		_uris_size_ = uris_length1;
		_tmp28_ = remaining_args;
		_tmp28__length1 = _vala_array_length (remaining_args);
		{
			gchar** arg_collection = NULL;
			gint arg_collection_length1 = 0;
			gint _arg_collection_size_ = 0;
			gint arg_it = 0;
			arg_collection = _tmp28_;
			arg_collection_length1 = _tmp28__length1;
			for (arg_it = 0; arg_it < _tmp28__length1; arg_it = arg_it + 1) {
				gchar* _tmp29_;
				gchar* arg = NULL;
				_tmp29_ = g_strdup (arg_collection[arg_it]);
				arg = _tmp29_;
				{
					gchar** _tmp30_;
					gint _tmp30__length1;
					const gchar* _tmp31_;
					GFile* _tmp32_ = NULL;
					GFile* _tmp33_;
					gchar* _tmp34_ = NULL;
					_tmp30_ = uris;
					_tmp30__length1 = uris_length1;
					_tmp31_ = arg;
					_tmp32_ = g_file_new_for_path (_tmp31_);
					_tmp33_ = _tmp32_;
					_tmp34_ = g_file_get_uri (_tmp33_);
					_vala_array_add14 (&uris, &uris_length1, &_uris_size_, _tmp34_);
					_g_object_unref0 (_tmp33_);
					_g_free0 (arg);
				}
			}
		}
		_tmp35_ = data;
		_tmp36_ = uris;
		_tmp36__length1 = uris_length1;
		unique_message_data_set_uris (_tmp35_, _tmp36_);
		uris = (_vala_array_free (uris, uris_length1, (GDestroyNotify) g_free), NULL);
	}
	_tmp37_ = unique_app_new ("org.gnome.latexila", NULL);
	app = _tmp37_;
	_tmp38_ = app;
	_tmp39_ = latexila_NEW_WINDOW;
	unique_app_add_command (_tmp38_, "new_window", _tmp39_);
	_tmp40_ = app;
	g_object_get (_tmp40_, "is-running", &_tmp41_, NULL);
	_tmp42_ = _tmp41_;
	if (_tmp42_) {
		gboolean ok;
		gboolean _tmp43_;
		gboolean _tmp48_ = FALSE;
		gboolean _tmp49_;
		gboolean _tmp51_;
		gboolean _tmp56_ = FALSE;
		gboolean _tmp57_;
		gboolean _tmp59_;
		gboolean _tmp63_ = FALSE;
		gboolean _tmp64_ = FALSE;
		gboolean _tmp65_;
		gboolean _tmp67_;
		gboolean _tmp69_;
		gboolean _tmp73_;
		ok = TRUE;
		_tmp43_ = option_new_window;
		if (_tmp43_) {
			UniqueApp* _tmp44_;
			gint _tmp45_;
			UniqueResponse _tmp46_ = 0;
			UniqueResponse resp;
			UniqueResponse _tmp47_;
			_tmp44_ = app;
			_tmp45_ = latexila_NEW_WINDOW;
			_tmp46_ = unique_app_send_message (_tmp44_, _tmp45_, NULL);
			resp = _tmp46_;
			_tmp47_ = resp;
			ok = _tmp47_ == UNIQUE_RESPONSE_OK;
		}
		_tmp49_ = ok;
		if (_tmp49_) {
			gboolean _tmp50_;
			_tmp50_ = command_open;
			_tmp48_ = _tmp50_;
		} else {
			_tmp48_ = FALSE;
		}
		_tmp51_ = _tmp48_;
		if (_tmp51_) {
			UniqueApp* _tmp52_;
			UniqueMessageData* _tmp53_;
			UniqueResponse _tmp54_ = 0;
			UniqueResponse resp;
			UniqueResponse _tmp55_;
			_tmp52_ = app;
			_tmp53_ = data;
			_tmp54_ = unique_app_send_message (_tmp52_, (gint) UNIQUE_OPEN, _tmp53_);
			resp = _tmp54_;
			_tmp55_ = resp;
			ok = _tmp55_ == UNIQUE_RESPONSE_OK;
		}
		_tmp57_ = ok;
		if (_tmp57_) {
			gboolean _tmp58_;
			_tmp58_ = option_new_document;
			_tmp56_ = _tmp58_;
		} else {
			_tmp56_ = FALSE;
		}
		_tmp59_ = _tmp56_;
		if (_tmp59_) {
			UniqueApp* _tmp60_;
			UniqueResponse _tmp61_ = 0;
			UniqueResponse resp;
			UniqueResponse _tmp62_;
			_tmp60_ = app;
			_tmp61_ = unique_app_send_message (_tmp60_, (gint) UNIQUE_NEW, NULL);
			resp = _tmp61_;
			_tmp62_ = resp;
			ok = _tmp62_ == UNIQUE_RESPONSE_OK;
		}
		_tmp65_ = option_new_window;
		if (!_tmp65_) {
			gboolean _tmp66_;
			_tmp66_ = command_open;
			_tmp64_ = !_tmp66_;
		} else {
			_tmp64_ = FALSE;
		}
		_tmp67_ = _tmp64_;
		if (_tmp67_) {
			gboolean _tmp68_;
			_tmp68_ = option_new_document;
			_tmp63_ = !_tmp68_;
		} else {
			_tmp63_ = FALSE;
		}
		_tmp69_ = _tmp63_;
		if (_tmp69_) {
			UniqueApp* _tmp70_;
			UniqueResponse _tmp71_ = 0;
			UniqueResponse resp;
			UniqueResponse _tmp72_;
			_tmp70_ = app;
			_tmp71_ = unique_app_send_message (_tmp70_, (gint) UNIQUE_ACTIVATE, NULL);
			resp = _tmp71_;
			_tmp72_ = resp;
			ok = _tmp72_ == UNIQUE_RESPONSE_OK;
		}
		_tmp73_ = ok;
		if (!_tmp73_) {
			g_error ("main.vala:142: Error: communication with first instance of LaTeXila fa" \
"iled\n");
		}
		result = 0;
		_g_object_unref0 (app);
		_unique_message_data_free0 (data);
		_g_option_context_free0 (context);
		data_dirs = (_vala_array_free (data_dirs, data_dirs_length1, (GDestroyNotify) g_free), NULL);
		_g_free0 (data_dirs_env);
		return result;
	} else {
		Latexila* _tmp74_ = NULL;
		Latexila* latexila;
		GSettings* _tmp75_;
		GSettings* editor_settings;
		GSettings* _tmp76_;
		gboolean _tmp77_ = FALSE;
		gboolean _tmp84_;
		gboolean _tmp89_;
		UniqueApp* _tmp91_;
		Latexila* _tmp92_;
		_tmp74_ = latexila_get_default ();
		latexila = _tmp74_;
		_tmp75_ = g_settings_new ("org.gnome.latexila.preferences.editor");
		editor_settings = _tmp75_;
		_tmp76_ = editor_settings;
		_tmp77_ = g_settings_get_boolean (_tmp76_, "reopen-files");
		if (_tmp77_) {
			GSettings* _tmp78_;
			GSettings* window_settings;
			GSettings* _tmp79_;
			gchar** _tmp80_;
			gchar** _tmp81_ = NULL;
			gchar** uris;
			gint uris_length1;
			gint _uris_size_;
			Latexila* _tmp82_;
			gchar** _tmp83_;
			gint _tmp83__length1;
			_tmp78_ = g_settings_new ("org.gnome.latexila.state.window");
			window_settings = _tmp78_;
			_tmp79_ = window_settings;
			_tmp81_ = _tmp80_ = g_settings_get_strv (_tmp79_, "documents");
			uris = _tmp81_;
			uris_length1 = _vala_array_length (_tmp80_);
			_uris_size_ = uris_length1;
			_tmp82_ = latexila;
			_tmp83_ = uris;
			_tmp83__length1 = uris_length1;
			latexila_open_documents (_tmp82_, _tmp83_);
			uris = (_vala_array_free (uris, uris_length1, (GDestroyNotify) g_free), NULL);
			_g_object_unref0 (window_settings);
		}
		_tmp84_ = command_open;
		if (_tmp84_) {
			Latexila* _tmp85_;
			UniqueMessageData* _tmp86_;
			gchar** _tmp87_ = NULL;
			gchar** _tmp88_;
			gint _tmp88__length1;
			_tmp85_ = latexila;
			_tmp86_ = data;
			_tmp87_ = unique_message_data_get_uris (_tmp86_);
			_tmp88_ = _tmp87_;
			_tmp88__length1 = -1;
			latexila_open_documents (_tmp85_, _tmp88_);
			_tmp88_ = (_vala_array_free (_tmp88_, _tmp88__length1, (GDestroyNotify) g_free), NULL);
		}
		_tmp89_ = option_new_document;
		if (_tmp89_) {
			Latexila* _tmp90_;
			_tmp90_ = latexila;
			latexila_create_document (_tmp90_);
		}
		_tmp91_ = app;
		_tmp92_ = latexila;
		g_signal_connect_object (_tmp91_, "message-received", (GCallback) _latexila_message_unique_app_message_received, _tmp92_, 0);
		gtk_main ();
		_g_object_unref0 (editor_settings);
		_g_object_unref0 (latexila);
	}
	result = 0;
	_g_object_unref0 (app);
	_unique_message_data_free0 (data);
	_g_option_context_free0 (context);
	data_dirs = (_vala_array_free (data_dirs, data_dirs_length1, (GDestroyNotify) g_free), NULL);
	_g_free0 (data_dirs_env);
	return result;
}


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


static void _vala_array_destroy (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	if ((array != NULL) && (destroy_func != NULL)) {
		int i;
		for (i = 0; i < array_length; i = i + 1) {
			if (((gpointer*) array)[i] != NULL) {
				destroy_func (((gpointer*) array)[i]);
			}
		}
	}
}


static void _vala_array_free (gpointer array, gint array_length, GDestroyNotify destroy_func) {
	_vala_array_destroy (array, array_length, destroy_func);
	g_free (array);
}


static gint _vala_array_length (gpointer array) {
	int length;
	length = 0;
	if (array) {
		while (((gpointer*) array)[length]) {
			length++;
		}
	}
	return length;
}



