/* templates.c generated by valac 0.12.1.1-323029-dirty, the Vala compiler
 * generated from templates.vala, do not modify */

/* templates.vala
 *
 * Copyright (C) 2010  Andrea Del Signore
 *
 * 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
 *
 * Author:
 * 	Andrea Del Signore <sejerpz@tin.it>
 */

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


#define GEN_PROJECT_TYPE_TEMPLATES (gen_project_templates_get_type ())
#define GEN_PROJECT_TEMPLATES(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEN_PROJECT_TYPE_TEMPLATES, GenProjectTemplates))
#define GEN_PROJECT_TEMPLATES_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEN_PROJECT_TYPE_TEMPLATES, GenProjectTemplatesClass))
#define GEN_PROJECT_IS_TEMPLATES(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEN_PROJECT_TYPE_TEMPLATES))
#define GEN_PROJECT_IS_TEMPLATES_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEN_PROJECT_TYPE_TEMPLATES))
#define GEN_PROJECT_TEMPLATES_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEN_PROJECT_TYPE_TEMPLATES, GenProjectTemplatesClass))

typedef struct _GenProjectTemplates GenProjectTemplates;
typedef struct _GenProjectTemplatesClass GenProjectTemplatesClass;
typedef struct _GenProjectTemplatesPrivate GenProjectTemplatesPrivate;

#define GEN_PROJECT_TYPE_TEMPLATE_DEFINITION (gen_project_template_definition_get_type ())
#define GEN_PROJECT_TEMPLATE_DEFINITION(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), GEN_PROJECT_TYPE_TEMPLATE_DEFINITION, GenProjectTemplateDefinition))
#define GEN_PROJECT_TEMPLATE_DEFINITION_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GEN_PROJECT_TYPE_TEMPLATE_DEFINITION, GenProjectTemplateDefinitionClass))
#define GEN_PROJECT_IS_TEMPLATE_DEFINITION(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), GEN_PROJECT_TYPE_TEMPLATE_DEFINITION))
#define GEN_PROJECT_IS_TEMPLATE_DEFINITION_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), GEN_PROJECT_TYPE_TEMPLATE_DEFINITION))
#define GEN_PROJECT_TEMPLATE_DEFINITION_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), GEN_PROJECT_TYPE_TEMPLATE_DEFINITION, GenProjectTemplateDefinitionClass))

typedef struct _GenProjectTemplateDefinition GenProjectTemplateDefinition;
typedef struct _GenProjectTemplateDefinitionClass GenProjectTemplateDefinitionClass;
#define __g_list_free__g_object_unref0_0(var) ((var == NULL) ? NULL : (var = (_g_list_free__g_object_unref0_ (var), NULL)))
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _g_dir_close0(var) ((var == NULL) ? NULL : (var = (g_dir_close (var), NULL)))
#define _g_error_free0(var) ((var == NULL) ? NULL : (var = (g_error_free (var), NULL)))

struct _GenProjectTemplates {
	GObject parent_instance;
	GenProjectTemplatesPrivate * priv;
};

struct _GenProjectTemplatesClass {
	GObjectClass parent_class;
};

struct _GenProjectTemplatesPrivate {
	GList* _definitions;
	gint _next_id;
};


static gpointer gen_project_templates_parent_class = NULL;

GType gen_project_templates_get_type (void) G_GNUC_CONST;
GType gen_project_template_definition_get_type (void) G_GNUC_CONST;
#define GEN_PROJECT_TEMPLATES_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), GEN_PROJECT_TYPE_TEMPLATES, GenProjectTemplatesPrivate))
enum  {
	GEN_PROJECT_TEMPLATES_DUMMY_PROPERTY,
	GEN_PROJECT_TEMPLATES_DEFINITIONS
};
static void _g_object_unref0_ (gpointer var);
static void _g_list_free__g_object_unref0_ (GList* self);
static GenProjectTemplates* gen_project_templates_new (void);
static GenProjectTemplates* gen_project_templates_construct (GType object_type);
GenProjectTemplates* gen_project_templates_load (gchar** user_path, int user_path_length1);
static void gen_project_templates_scan_path (GenProjectTemplates* self, const gchar* path);
static gint _lambda0_ (GenProjectTemplateDefinition* a, GenProjectTemplateDefinition* b);
const gchar* gen_project_template_definition_get_name (GenProjectTemplateDefinition* self);
static gint __lambda0__gcompare_func (gconstpointer a, gconstpointer b);
GenProjectTemplateDefinition* gen_project_template_definition_load (const gchar* filename);
void gen_project_template_definition_set_id (GenProjectTemplateDefinition* self, gint value);
GList* gen_project_templates_get_definitions (GenProjectTemplates* self);
static void gen_project_templates_finalize (GObject* obj);
static void _vala_gen_project_templates_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);
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 void _g_object_unref0_ (gpointer var) {
	(var == NULL) ? NULL : (var = (g_object_unref (var), NULL));
}


static void _g_list_free__g_object_unref0_ (GList* self) {
	g_list_foreach (self, (GFunc) _g_object_unref0_, NULL);
	g_list_free (self);
}


static GenProjectTemplates* gen_project_templates_construct (GType object_type) {
	GenProjectTemplates * self = NULL;
	self = (GenProjectTemplates*) g_object_new (object_type, NULL);
	return self;
}


static GenProjectTemplates* gen_project_templates_new (void) {
	return gen_project_templates_construct (GEN_PROJECT_TYPE_TEMPLATES);
}


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


static gint _lambda0_ (GenProjectTemplateDefinition* a, GenProjectTemplateDefinition* b) {
	gint result = 0;
	GenProjectTemplateDefinition* _tmp0_;
	GenProjectTemplateDefinition* t1;
	GenProjectTemplateDefinition* _tmp1_;
	GenProjectTemplateDefinition* t2;
	const gchar* _tmp2_ = NULL;
	const gchar* _tmp3_ = NULL;
	gint _tmp4_;
	g_return_val_if_fail (a != NULL, 0);
	g_return_val_if_fail (b != NULL, 0);
	_tmp0_ = _g_object_ref0 (GEN_PROJECT_TEMPLATE_DEFINITION (a));
	t1 = _tmp0_;
	_tmp1_ = _g_object_ref0 (GEN_PROJECT_TEMPLATE_DEFINITION (b));
	t2 = _tmp1_;
	_tmp2_ = gen_project_template_definition_get_name (t1);
	_tmp3_ = gen_project_template_definition_get_name (t2);
	_tmp4_ = g_strcmp0 (_tmp2_, _tmp3_);
	result = _tmp4_;
	_g_object_unref0 (t2);
	_g_object_unref0 (t1);
	return result;
}


static gint __lambda0__gcompare_func (gconstpointer a, gconstpointer b) {
	gint result;
	result = _lambda0_ (a, b);
	return result;
}


GenProjectTemplates* gen_project_templates_load (gchar** user_path, int user_path_length1) {
	GenProjectTemplates* result = NULL;
	GenProjectTemplates* _tmp0_ = NULL;
	GenProjectTemplates* _result_;
	gchar* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	gchar* _tmp3_ = NULL;
	gchar** _tmp4_ = NULL;
	gchar** standard_path;
	gint standard_path_length1;
	gint _standard_path_size_;
	_tmp0_ = gen_project_templates_new ();
	_result_ = _tmp0_;
	_tmp1_ = g_build_filename (PACKAGE_DATADIR, "templates", NULL);
	_tmp2_ = g_get_user_data_dir ();
	_tmp3_ = g_build_filename (_tmp2_, "gen-project", "templates", NULL);
	_tmp4_ = g_new0 (gchar*, 2 + 1);
	_tmp4_[0] = _tmp1_;
	_tmp4_[1] = _tmp3_;
	standard_path = _tmp4_;
	standard_path_length1 = 2;
	_standard_path_size_ = 2;
	{
		gchar** path_collection;
		int path_collection_length1;
		int path_it;
		path_collection = standard_path;
		path_collection_length1 = standard_path_length1;
		for (path_it = 0; path_it < standard_path_length1; path_it = path_it + 1) {
			gchar* _tmp5_;
			gchar* path;
			_tmp5_ = g_strdup (path_collection[path_it]);
			path = _tmp5_;
			{
				g_message ("templates.vala:49: scanning directory %s for templates", path);
				gen_project_templates_scan_path (_result_, path);
				_g_free0 (path);
			}
		}
	}
	if (user_path != NULL) {
		{
			gchar** path_collection;
			int path_collection_length1;
			int path_it;
			path_collection = user_path;
			path_collection_length1 = user_path_length1;
			for (path_it = 0; path_it < user_path_length1; path_it = path_it + 1) {
				gchar* _tmp6_;
				gchar* path;
				_tmp6_ = g_strdup (path_collection[path_it]);
				path = _tmp6_;
				{
					g_message ("templates.vala:56: scanning user directory %s for templates", path);
					gen_project_templates_scan_path (_result_, path);
					_g_free0 (path);
				}
			}
		}
	}
	_result_->priv->_definitions = g_list_sort (_result_->priv->_definitions, __lambda0__gcompare_func);
	result = _result_;
	standard_path = (_vala_array_free (standard_path, standard_path_length1, (GDestroyNotify) g_free), NULL);
	return result;
}


static glong string_strnlen (gchar* str, glong maxlen) {
	glong result = 0L;
	gchar* _tmp0_ = NULL;
	gchar* end;
	_tmp0_ = memchr (str, 0, (gsize) maxlen);
	end = _tmp0_;
	if (end == NULL) {
		result = maxlen;
		return result;
	} else {
		result = (glong) (end - str);
		return result;
	}
}


static gchar* string_substring (const gchar* self, glong offset, glong len) {
	gchar* result = NULL;
	glong string_length = 0L;
	gboolean _tmp0_ = FALSE;
	gchar* _tmp3_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	if (offset >= 0) {
		_tmp0_ = len >= 0;
	} else {
		_tmp0_ = FALSE;
	}
	if (_tmp0_) {
		glong _tmp1_;
		_tmp1_ = string_strnlen ((gchar*) self, offset + len);
		string_length = _tmp1_;
	} else {
		gint _tmp2_;
		_tmp2_ = strlen (self);
		string_length = (glong) _tmp2_;
	}
	if (offset < 0) {
		offset = string_length + offset;
		g_return_val_if_fail (offset >= 0, NULL);
	} else {
		g_return_val_if_fail (offset <= string_length, NULL);
	}
	if (len < 0) {
		len = string_length - offset;
	}
	g_return_val_if_fail ((offset + len) <= string_length, NULL);
	_tmp3_ = g_strndup (((gchar*) self) + offset, (gsize) len);
	result = _tmp3_;
	return result;
}


static void gen_project_templates_scan_path (GenProjectTemplates* self, const gchar* path) {
	gboolean _tmp0_;
	GDir* _tmp1_ = NULL;
	GDir* dir;
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (path != NULL);
	_tmp0_ = g_file_test (path, G_FILE_TEST_IS_DIR);
	if (!_tmp0_) {
		return;
	}
	_tmp1_ = g_dir_open (path, (guint) 0, &_inner_error_);
	dir = _tmp1_;
	if (_inner_error_ != NULL) {
		goto __catch5_g_error;
	}
	if (dir != NULL) {
		gchar* file = NULL;
		while (TRUE) {
			const gchar* _tmp2_ = NULL;
			gchar* _tmp3_;
			gboolean _tmp4_ = FALSE;
			_tmp2_ = g_dir_read_name (dir);
			_tmp3_ = g_strdup (_tmp2_);
			_g_free0 (file);
			file = _tmp3_;
			if (!(file != NULL)) {
				break;
			}
			if (file != NULL) {
				gboolean _tmp5_;
				_tmp5_ = g_str_has_suffix (file, ".ini");
				_tmp4_ = _tmp5_;
			} else {
				_tmp4_ = FALSE;
			}
			if (_tmp4_) {
				gchar* _tmp6_ = NULL;
				gchar* _tmp7_;
				gint _tmp8_;
				gchar* _tmp9_ = NULL;
				gchar* _tmp10_;
				gchar* template_name;
				gchar* _tmp11_;
				gchar* _tmp12_;
				gchar* _tmp13_ = NULL;
				gchar* _tmp14_;
				gchar* tar_filename;
				gboolean _tmp15_;
				_tmp6_ = g_path_get_basename (file);
				_tmp7_ = _tmp6_;
				_tmp8_ = strlen (file);
				_tmp9_ = string_substring (_tmp7_, (glong) 0, (glong) (_tmp8_ - 4));
				_tmp10_ = _tmp9_;
				_g_free0 (_tmp7_);
				template_name = _tmp10_;
				_tmp11_ = g_strconcat (template_name, ".tar.gz", NULL);
				_tmp12_ = _tmp11_;
				_tmp13_ = g_build_filename (path, _tmp12_, NULL);
				_tmp14_ = _tmp13_;
				_g_free0 (_tmp12_);
				tar_filename = _tmp14_;
				_tmp15_ = g_file_test (tar_filename, G_FILE_TEST_IS_REGULAR | G_FILE_TEST_IS_SYMLINK);
				if (_tmp15_) {
					gchar* _tmp16_ = NULL;
					gchar* _tmp17_;
					GenProjectTemplateDefinition* _tmp18_ = NULL;
					GenProjectTemplateDefinition* _tmp19_;
					GenProjectTemplateDefinition* template_definition;
					_tmp16_ = g_build_filename (path, file, NULL);
					_tmp17_ = _tmp16_;
					_tmp18_ = gen_project_template_definition_load (_tmp17_);
					_tmp19_ = _tmp18_;
					_g_free0 (_tmp17_);
					template_definition = _tmp19_;
					if (template_definition != NULL) {
						GenProjectTemplateDefinition* _tmp20_;
						gen_project_template_definition_set_id (template_definition, self->priv->_next_id);
						_tmp20_ = _g_object_ref0 (template_definition);
						self->priv->_definitions = g_list_append (self->priv->_definitions, _tmp20_);
						self->priv->_next_id++;
					}
					_g_object_unref0 (template_definition);
				}
				_g_free0 (tar_filename);
				_g_free0 (template_name);
			}
		}
		_g_free0 (file);
	}
	_g_dir_close0 (dir);
	goto __finally5;
	__catch5_g_error:
	{
		GError * err;
		err = _inner_error_;
		_inner_error_ = NULL;
		g_critical ("templates.vala:97: error %s", err->message);
		_g_error_free0 (err);
	}
	__finally5:
	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;
	}
}


GList* gen_project_templates_get_definitions (GenProjectTemplates* self) {
	GList* result;
	g_return_val_if_fail (self != NULL, NULL);
	result = self->priv->_definitions;
	return result;
}


static void gen_project_templates_class_init (GenProjectTemplatesClass * klass) {
	gen_project_templates_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (GenProjectTemplatesPrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_gen_project_templates_get_property;
	G_OBJECT_CLASS (klass)->finalize = gen_project_templates_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), GEN_PROJECT_TEMPLATES_DEFINITIONS, g_param_spec_pointer ("definitions", "definitions", "definitions", G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
}


static void gen_project_templates_instance_init (GenProjectTemplates * self) {
	self->priv = GEN_PROJECT_TEMPLATES_GET_PRIVATE (self);
	self->priv->_definitions = NULL;
	self->priv->_next_id = 0;
}


static void gen_project_templates_finalize (GObject* obj) {
	GenProjectTemplates * self;
	self = GEN_PROJECT_TEMPLATES (obj);
	__g_list_free__g_object_unref0_0 (self->priv->_definitions);
	G_OBJECT_CLASS (gen_project_templates_parent_class)->finalize (obj);
}


GType gen_project_templates_get_type (void) {
	static volatile gsize gen_project_templates_type_id__volatile = 0;
	if (g_once_init_enter (&gen_project_templates_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (GenProjectTemplatesClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) gen_project_templates_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (GenProjectTemplates), 0, (GInstanceInitFunc) gen_project_templates_instance_init, NULL };
		GType gen_project_templates_type_id;
		gen_project_templates_type_id = g_type_register_static (G_TYPE_OBJECT, "GenProjectTemplates", &g_define_type_info, 0);
		g_once_init_leave (&gen_project_templates_type_id__volatile, gen_project_templates_type_id);
	}
	return gen_project_templates_type_id__volatile;
}


static void _vala_gen_project_templates_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	GenProjectTemplates * self;
	self = GEN_PROJECT_TEMPLATES (object);
	switch (property_id) {
		case GEN_PROJECT_TEMPLATES_DEFINITIONS:
		g_value_set_pointer (value, gen_project_templates_get_definitions (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}


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);
}



