/* ide-vala-service.c generated by valac 0.30.0, the Vala compiler
 * generated from ide-vala-service.vala, do not modify */

/* ide-vala-service.vala
 *
 * Copyright (C) 2015 Christian Hergert <christian@hergert.me>
 *
 * This program 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.
 *
 * This program 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 this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib.h>
#include <glib-object.h>
#include <ide.h>
#include <stdlib.h>
#include <string.h>
#include <valagee.h>
#include <gio/gio.h>


#define IDE_TYPE_VALA_SERVICE (ide_vala_service_get_type ())
#define IDE_VALA_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), IDE_TYPE_VALA_SERVICE, IdeValaService))
#define IDE_VALA_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), IDE_TYPE_VALA_SERVICE, IdeValaServiceClass))
#define IDE_IS_VALA_SERVICE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), IDE_TYPE_VALA_SERVICE))
#define IDE_IS_VALA_SERVICE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), IDE_TYPE_VALA_SERVICE))
#define IDE_VALA_SERVICE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), IDE_TYPE_VALA_SERVICE, IdeValaServiceClass))

typedef struct _IdeValaService IdeValaService;
typedef struct _IdeValaServiceClass IdeValaServiceClass;
typedef struct _IdeValaServicePrivate IdeValaServicePrivate;

#define IDE_TYPE_VALA_INDEX (ide_vala_index_get_type ())
#define IDE_VALA_INDEX(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), IDE_TYPE_VALA_INDEX, IdeValaIndex))
#define IDE_VALA_INDEX_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), IDE_TYPE_VALA_INDEX, IdeValaIndexClass))
#define IDE_IS_VALA_INDEX(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), IDE_TYPE_VALA_INDEX))
#define IDE_IS_VALA_INDEX_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), IDE_TYPE_VALA_INDEX))
#define IDE_VALA_INDEX_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), IDE_TYPE_VALA_INDEX, IdeValaIndexClass))

typedef struct _IdeValaIndex IdeValaIndex;
typedef struct _IdeValaIndexClass IdeValaIndexClass;
#define _g_object_unref0(var) ((var == NULL) ? NULL : (var = (g_object_unref (var), NULL)))
#define _vala_iterable_unref0(var) ((var == NULL) ? NULL : (var = (vala_iterable_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)))

struct _IdeValaService {
	IdeObject parent_instance;
	IdeValaServicePrivate * priv;
};

struct _IdeValaServiceClass {
	IdeObjectClass parent_class;
};

struct _IdeValaServicePrivate {
	IdeValaIndex* _index;
};


static gpointer ide_vala_service_parent_class = NULL;
static IdeServiceInterface* ide_vala_service_ide_service_parent_iface = NULL;
static GType ide_vala_service_type_id = 0;

GType ide_vala_service_get_type (void) G_GNUC_CONST;
GType ide_vala_service_register_type (GTypeModule * module);
GType ide_vala_index_get_type (void) G_GNUC_CONST;
GType ide_vala_index_register_type (GTypeModule * module);
#define IDE_VALA_SERVICE_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), IDE_TYPE_VALA_SERVICE, IdeValaServicePrivate))
enum  {
	IDE_VALA_SERVICE_DUMMY_PROPERTY,
	IDE_VALA_SERVICE_INDEX
};
static const gchar* ide_vala_service_real_get_name (IdeService* base);
static void ide_vala_service_real_start (IdeService* base);
static void __lambda13_ (IdeValaService* self);
void ide_vala_service_load_directory (IdeValaService* self, GFile* directory, GCancellable* cancellable, ValaArrayList* files);
void ide_vala_index_add_files (IdeValaIndex* self, ValaArrayList* files, GCancellable* cancellable, GAsyncReadyCallback _callback_, gpointer _user_data_);
void ide_vala_index_add_files_finish (IdeValaIndex* self, GAsyncResult* _res_);
static void ___lambda14_ (IdeValaService* self);
static void ____lambda14__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self);
static void ___lambda13__ide_thread_func (gpointer self);
static void ide_vala_service_real_stop (IdeService* base);
IdeValaService* ide_vala_service_new (void);
IdeValaService* ide_vala_service_construct (GType object_type);
IdeValaIndex* ide_vala_service_get_index (IdeValaService* self);
static GObject * ide_vala_service_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties);
IdeValaIndex* ide_vala_index_new (void);
IdeValaIndex* ide_vala_index_construct (GType object_type);
static void ide_vala_service_finalize (GObject* obj);
static void _vala_ide_vala_service_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec);


static const gchar* ide_vala_service_real_get_name (IdeService* base) {
	IdeValaService * self;
	const gchar* result = NULL;
	const gchar* _tmp0_ = NULL;
	self = (IdeValaService*) base;
	_tmp0_ = g_type_name (IDE_TYPE_VALA_SERVICE);
	result = _tmp0_;
	return result;
}


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


static void ___lambda14_ (IdeValaService* self) {
	g_debug ("ide-vala-service.vala:50: Vala files registered");
}


static void ____lambda14__gasync_ready_callback (GObject* source_object, GAsyncResult* res, gpointer self) {
	___lambda14_ ((IdeValaService*) self);
	g_object_unref (self);
}


static void __lambda13_ (IdeValaService* self) {
	IdeVcs* vcs = NULL;
	IdeContext* _tmp0_ = NULL;
	IdeVcs* _tmp1_ = NULL;
	IdeVcs* _tmp2_ = NULL;
	ValaArrayList* files = NULL;
	GEqualFunc _tmp3_ = NULL;
	ValaArrayList* _tmp4_ = NULL;
	IdeVcs* _tmp5_ = NULL;
	GFile* _tmp6_ = NULL;
	ValaArrayList* _tmp7_ = NULL;
	ValaArrayList* _tmp8_ = NULL;
	gint _tmp9_ = 0;
	gint _tmp10_ = 0;
	_tmp0_ = ide_object_get_context ((IdeObject*) self);
	_tmp1_ = ide_context_get_vcs (_tmp0_);
	_tmp2_ = _g_object_ref0 (_tmp1_);
	vcs = _tmp2_;
	_tmp3_ = g_direct_equal;
	_tmp4_ = vala_array_list_new (G_TYPE_FILE, (GBoxedCopyFunc) g_object_ref, g_object_unref, _tmp3_);
	files = _tmp4_;
	_tmp5_ = vcs;
	_tmp6_ = ide_vcs_get_working_directory (_tmp5_);
	_tmp7_ = files;
	ide_vala_service_load_directory (self, _tmp6_, NULL, _tmp7_);
	_tmp8_ = files;
	_tmp9_ = vala_collection_get_size ((ValaCollection*) _tmp8_);
	_tmp10_ = _tmp9_;
	if (_tmp10_ > 0) {
		IdeValaIndex* _tmp11_ = NULL;
		ValaArrayList* _tmp12_ = NULL;
		_tmp11_ = self->priv->_index;
		_tmp12_ = files;
		ide_vala_index_add_files (_tmp11_, _tmp12_, NULL, ____lambda14__gasync_ready_callback, g_object_ref (self));
	}
	_vala_iterable_unref0 (files);
	_g_object_unref0 (vcs);
}


static void ___lambda13__ide_thread_func (gpointer self) {
	__lambda13_ ((IdeValaService*) self);
	g_object_unref (self);
}


static void ide_vala_service_real_start (IdeService* base) {
	IdeValaService * self;
	self = (IdeValaService*) base;
	ide_thread_pool_push (IDE_THREAD_POOL_INDEXER, ___lambda13__ide_thread_func, g_object_ref (self));
}


static void ide_vala_service_real_stop (IdeService* base) {
	IdeValaService * self;
	self = (IdeValaService*) base;
}


static gpointer _vala_iterable_ref0 (gpointer self) {
	return self ? vala_iterable_ref (self) : NULL;
}


void ide_vala_service_load_directory (IdeValaService* self, GFile* directory, GCancellable* cancellable, ValaArrayList* files) {
	GError * _inner_error_ = NULL;
	g_return_if_fail (self != NULL);
	g_return_if_fail (directory != NULL);
	g_return_if_fail (files != NULL);
	{
		GFileEnumerator* enumerator = NULL;
		GFile* _tmp0_ = NULL;
		GCancellable* _tmp1_ = NULL;
		GFileEnumerator* _tmp2_ = NULL;
		ValaArrayList* directories = NULL;
		GEqualFunc _tmp3_ = NULL;
		ValaArrayList* _tmp4_ = NULL;
		GFileInfo* file_info = NULL;
		GFileEnumerator* _tmp32_ = NULL;
		_tmp0_ = directory;
		_tmp1_ = cancellable;
		_tmp2_ = g_file_enumerate_children (_tmp0_, G_FILE_ATTRIBUTE_STANDARD_NAME "," G_FILE_ATTRIBUTE_STANDARD_TYPE, 0, _tmp1_, &_inner_error_);
		enumerator = _tmp2_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch0_g_error;
		}
		_tmp3_ = g_direct_equal;
		_tmp4_ = vala_array_list_new (G_TYPE_FILE, (GBoxedCopyFunc) g_object_ref, g_object_unref, _tmp3_);
		directories = _tmp4_;
		while (TRUE) {
			GFileInfo* _tmp5_ = NULL;
			GFileEnumerator* _tmp6_ = NULL;
			GFileInfo* _tmp7_ = NULL;
			GFileInfo* _tmp8_ = NULL;
			GFileInfo* _tmp9_ = NULL;
			gchar* name = NULL;
			GFileInfo* _tmp10_ = NULL;
			const gchar* _tmp11_ = NULL;
			gchar* _tmp12_ = NULL;
			GFileInfo* _tmp13_ = NULL;
			GFileType _tmp14_ = 0;
			_tmp6_ = enumerator;
			_tmp7_ = g_file_enumerator_next_file (_tmp6_, NULL, &_inner_error_);
			_tmp5_ = _tmp7_;
			if (G_UNLIKELY (_inner_error_ != NULL)) {
				_g_object_unref0 (file_info);
				_vala_iterable_unref0 (directories);
				_g_object_unref0 (enumerator);
				goto __catch0_g_error;
			}
			_tmp8_ = _tmp5_;
			_tmp5_ = NULL;
			_g_object_unref0 (file_info);
			file_info = _tmp8_;
			_tmp9_ = file_info;
			if (!(_tmp9_ != NULL)) {
				_g_object_unref0 (_tmp5_);
				break;
			}
			_tmp10_ = file_info;
			_tmp11_ = g_file_info_get_name (_tmp10_);
			_tmp12_ = g_strdup (_tmp11_);
			name = _tmp12_;
			_tmp13_ = file_info;
			_tmp14_ = g_file_info_get_file_type (_tmp13_);
			if (_tmp14_ == G_FILE_TYPE_DIRECTORY) {
				ValaArrayList* _tmp15_ = NULL;
				GFile* _tmp16_ = NULL;
				GFileInfo* _tmp17_ = NULL;
				const gchar* _tmp18_ = NULL;
				GFile* _tmp19_ = NULL;
				GFile* _tmp20_ = NULL;
				_tmp15_ = directories;
				_tmp16_ = directory;
				_tmp17_ = file_info;
				_tmp18_ = g_file_info_get_name (_tmp17_);
				_tmp19_ = g_file_get_child (_tmp16_, _tmp18_);
				_tmp20_ = _tmp19_;
				vala_collection_add ((ValaCollection*) _tmp15_, _tmp20_);
				_g_object_unref0 (_tmp20_);
			} else {
				gboolean _tmp21_ = FALSE;
				const gchar* _tmp22_ = NULL;
				gboolean _tmp23_ = FALSE;
				_tmp22_ = name;
				_tmp23_ = g_str_has_suffix (_tmp22_, ".vala");
				if (_tmp23_) {
					_tmp21_ = TRUE;
				} else {
					const gchar* _tmp24_ = NULL;
					gboolean _tmp25_ = FALSE;
					_tmp24_ = name;
					_tmp25_ = g_str_has_suffix (_tmp24_, ".vapi");
					_tmp21_ = _tmp25_;
				}
				if (_tmp21_) {
					ValaArrayList* _tmp26_ = NULL;
					GFile* _tmp27_ = NULL;
					GFileInfo* _tmp28_ = NULL;
					const gchar* _tmp29_ = NULL;
					GFile* _tmp30_ = NULL;
					GFile* _tmp31_ = NULL;
					_tmp26_ = files;
					_tmp27_ = directory;
					_tmp28_ = file_info;
					_tmp29_ = g_file_info_get_name (_tmp28_);
					_tmp30_ = g_file_get_child (_tmp27_, _tmp29_);
					_tmp31_ = _tmp30_;
					vala_collection_add ((ValaCollection*) _tmp26_, _tmp31_);
					_g_object_unref0 (_tmp31_);
				}
			}
			_g_free0 (name);
			_g_object_unref0 (_tmp5_);
		}
		_tmp32_ = enumerator;
		g_file_enumerator_close (_tmp32_, NULL, &_inner_error_);
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_object_unref0 (file_info);
			_vala_iterable_unref0 (directories);
			_g_object_unref0 (enumerator);
			goto __catch0_g_error;
		}
		{
			ValaArrayList* _child_list = NULL;
			ValaArrayList* _tmp33_ = NULL;
			ValaArrayList* _tmp34_ = NULL;
			gint _child_size = 0;
			ValaArrayList* _tmp35_ = NULL;
			gint _tmp36_ = 0;
			gint _tmp37_ = 0;
			gint _child_index = 0;
			_tmp33_ = directories;
			_tmp34_ = _vala_iterable_ref0 (_tmp33_);
			_child_list = _tmp34_;
			_tmp35_ = _child_list;
			_tmp36_ = vala_collection_get_size ((ValaCollection*) _tmp35_);
			_tmp37_ = _tmp36_;
			_child_size = _tmp37_;
			_child_index = -1;
			while (TRUE) {
				gint _tmp38_ = 0;
				gint _tmp39_ = 0;
				gint _tmp40_ = 0;
				GFile* child = NULL;
				ValaArrayList* _tmp41_ = NULL;
				gint _tmp42_ = 0;
				gpointer _tmp43_ = NULL;
				GFile* _tmp44_ = NULL;
				GCancellable* _tmp45_ = NULL;
				ValaArrayList* _tmp46_ = NULL;
				_tmp38_ = _child_index;
				_child_index = _tmp38_ + 1;
				_tmp39_ = _child_index;
				_tmp40_ = _child_size;
				if (!(_tmp39_ < _tmp40_)) {
					break;
				}
				_tmp41_ = _child_list;
				_tmp42_ = _child_index;
				_tmp43_ = vala_list_get ((ValaList*) _tmp41_, _tmp42_);
				child = (GFile*) _tmp43_;
				_tmp44_ = child;
				_tmp45_ = cancellable;
				_tmp46_ = files;
				ide_vala_service_load_directory (self, _tmp44_, _tmp45_, _tmp46_);
				_g_object_unref0 (child);
			}
			_vala_iterable_unref0 (_child_list);
		}
		_g_object_unref0 (file_info);
		_vala_iterable_unref0 (directories);
		_g_object_unref0 (enumerator);
	}
	goto __finally0;
	__catch0_g_error:
	{
		GError* err = NULL;
		GError* _tmp47_ = NULL;
		const gchar* _tmp48_ = NULL;
		gchar* _tmp49_ = NULL;
		gchar* _tmp50_ = NULL;
		err = _inner_error_;
		_inner_error_ = NULL;
		_tmp47_ = err;
		_tmp48_ = _tmp47_->message;
		_tmp49_ = g_strdup_printf ("%s", _tmp48_);
		_tmp50_ = _tmp49_;
		g_warning ("ide-vala-service.vala:83: %s", _tmp50_);
		_g_free0 (_tmp50_);
		_g_error_free0 (err);
	}
	__finally0:
	if (G_UNLIKELY (_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;
	}
}


IdeValaService* ide_vala_service_construct (GType object_type) {
	IdeValaService * self = NULL;
	self = (IdeValaService*) g_object_new (object_type, NULL);
	return self;
}


IdeValaService* ide_vala_service_new (void) {
	return ide_vala_service_construct (IDE_TYPE_VALA_SERVICE);
}


IdeValaIndex* ide_vala_service_get_index (IdeValaService* self) {
	IdeValaIndex* result;
	IdeValaIndex* _tmp0_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_index;
	result = _tmp0_;
	return result;
}


static GObject * ide_vala_service_constructor (GType type, guint n_construct_properties, GObjectConstructParam * construct_properties) {
	GObject * obj;
	GObjectClass * parent_class;
	IdeValaService * self;
	IdeValaIndex* _tmp0_ = NULL;
	parent_class = G_OBJECT_CLASS (ide_vala_service_parent_class);
	obj = parent_class->constructor (type, n_construct_properties, construct_properties);
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, IDE_TYPE_VALA_SERVICE, IdeValaService);
	_tmp0_ = ide_vala_index_new ();
	_g_object_unref0 (self->priv->_index);
	self->priv->_index = _tmp0_;
	return obj;
}


static void ide_vala_service_class_init (IdeValaServiceClass * klass) {
	ide_vala_service_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (IdeValaServicePrivate));
	G_OBJECT_CLASS (klass)->get_property = _vala_ide_vala_service_get_property;
	G_OBJECT_CLASS (klass)->constructor = ide_vala_service_constructor;
	G_OBJECT_CLASS (klass)->finalize = ide_vala_service_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), IDE_VALA_SERVICE_INDEX, g_param_spec_object ("index", "index", "index", IDE_TYPE_VALA_INDEX, G_PARAM_STATIC_NAME | G_PARAM_STATIC_NICK | G_PARAM_STATIC_BLURB | G_PARAM_READABLE));
}


static void ide_vala_service_ide_service_interface_init (IdeServiceInterface * iface) {
	ide_vala_service_ide_service_parent_iface = g_type_interface_peek_parent (iface);
	iface->get_name = (const gchar* (*)(IdeService*)) ide_vala_service_real_get_name;
	iface->start = (void (*)(IdeService*)) ide_vala_service_real_start;
	iface->stop = (void (*)(IdeService*)) ide_vala_service_real_stop;
	iface->set_context = (void (*)(IdeService*, IdeContext*)) ide_object_set_context;
	iface->set_context = (void (*) (IdeService *, IdeContext*)) ide_object_set_context;
}


static void ide_vala_service_instance_init (IdeValaService * self) {
	self->priv = IDE_VALA_SERVICE_GET_PRIVATE (self);
}


static void ide_vala_service_finalize (GObject* obj) {
	IdeValaService * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, IDE_TYPE_VALA_SERVICE, IdeValaService);
	_g_object_unref0 (self->priv->_index);
	G_OBJECT_CLASS (ide_vala_service_parent_class)->finalize (obj);
}


GType ide_vala_service_get_type (void) {
	return ide_vala_service_type_id;
}


GType ide_vala_service_register_type (GTypeModule * module) {
	static const GTypeInfo g_define_type_info = { sizeof (IdeValaServiceClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) ide_vala_service_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (IdeValaService), 0, (GInstanceInitFunc) ide_vala_service_instance_init, NULL };
	static const GInterfaceInfo ide_service_info = { (GInterfaceInitFunc) ide_vala_service_ide_service_interface_init, (GInterfaceFinalizeFunc) NULL, NULL};
	ide_vala_service_type_id = g_type_module_register_type (module, ide_object_get_type (), "IdeValaService", &g_define_type_info, 0);
	g_type_module_add_interface (module, ide_vala_service_type_id, ide_service_get_type (), &ide_service_info);
	return ide_vala_service_type_id;
}


static void _vala_ide_vala_service_get_property (GObject * object, guint property_id, GValue * value, GParamSpec * pspec) {
	IdeValaService * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, IDE_TYPE_VALA_SERVICE, IdeValaService);
	switch (property_id) {
		case IDE_VALA_SERVICE_INDEX:
		g_value_set_object (value, ide_vala_service_get_index (self));
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}



