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

/* moduleloader.vala
 *
 * Copyright (C) 2008-2009 Florian Brosch
 * Copyright (C) 2011      Florian Brosch
 *
 * 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:
 * 	Brosch Florian <flo.brosch@gmail.com>
 */

#include <glib.h>
#include <glib-object.h>
#include "valadoc.h"
#include <valagee.h>
#include <stdlib.h>
#include <string.h>
#include <gmodule.h>
#include <glib/gstdio.h>
#include <config.h>


#define VALADOC_MODULE_LOADER_TYPE_MODULE_DATA (valadoc_module_loader_module_data_get_type ())
#define VALADOC_MODULE_LOADER_MODULE_DATA(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), VALADOC_MODULE_LOADER_TYPE_MODULE_DATA, ValadocModuleLoaderModuleData))
#define VALADOC_MODULE_LOADER_MODULE_DATA_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), VALADOC_MODULE_LOADER_TYPE_MODULE_DATA, ValadocModuleLoaderModuleDataClass))
#define VALADOC_MODULE_LOADER_IS_MODULE_DATA(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), VALADOC_MODULE_LOADER_TYPE_MODULE_DATA))
#define VALADOC_MODULE_LOADER_IS_MODULE_DATA_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), VALADOC_MODULE_LOADER_TYPE_MODULE_DATA))
#define VALADOC_MODULE_LOADER_MODULE_DATA_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), VALADOC_MODULE_LOADER_TYPE_MODULE_DATA, ValadocModuleLoaderModuleDataClass))

typedef struct _ValadocModuleLoaderModuleData ValadocModuleLoaderModuleData;
typedef struct _ValadocModuleLoaderModuleDataClass ValadocModuleLoaderModuleDataClass;
enum  {
	VALADOC_MODULE_LOADER_0_PROPERTY,
	VALADOC_MODULE_LOADER_NUM_PROPERTIES
};
static GParamSpec* valadoc_module_loader_properties[VALADOC_MODULE_LOADER_NUM_PROPERTIES];
#define _vala_map_unref0(var) ((var == NULL) ? NULL : (var = (vala_map_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_module_close0(var) ((var == NULL) ? NULL : (var = (g_module_close (var), NULL)))
typedef struct _ValadocModuleLoaderModuleDataPrivate ValadocModuleLoaderModuleDataPrivate;
enum  {
	VALADOC_MODULE_LOADER_MODULE_DATA_0_PROPERTY,
	VALADOC_MODULE_LOADER_MODULE_DATA_NUM_PROPERTIES
};
static GParamSpec* valadoc_module_loader_module_data_properties[VALADOC_MODULE_LOADER_MODULE_DATA_NUM_PROPERTIES];

struct _ValadocModuleLoaderPrivate {
	ValaHashMap* doclets;
	ValaHashMap* taglets;
};

struct _ValadocModuleLoaderModuleData {
	GObject parent_instance;
	ValadocModuleLoaderModuleDataPrivate * priv;
	GModule* module;
	GType type;
};

struct _ValadocModuleLoaderModuleDataClass {
	GObjectClass parent_class;
};


static gpointer valadoc_module_loader_parent_class = NULL;
static ValadocModuleLoader* valadoc_module_loader_instance;
static ValadocModuleLoader* valadoc_module_loader_instance = NULL;
static gpointer valadoc_module_loader_module_data_parent_class = NULL;

static GType valadoc_module_loader_module_data_get_type (void) G_GNUC_CONST G_GNUC_UNUSED;
#define VALADOC_MODULE_LOADER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), VALADOC_TYPE_MODULE_LOADER, ValadocModuleLoaderPrivate))
static ValadocModuleLoader* valadoc_module_loader_new (void);
static ValadocModuleLoader* valadoc_module_loader_construct (GType object_type);
static gchar* valadoc_module_loader_get_plugin_path (const gchar* pluginpath, const gchar* pluginsubdir);
static ValadocModuleLoaderModuleData* valadoc_module_loader_module_data_new (void);
static ValadocModuleLoaderModuleData* valadoc_module_loader_module_data_construct (GType object_type);
static void valadoc_module_loader_module_data_finalize (GObject * obj);
static void valadoc_module_loader_finalize (GObject * obj);


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


ValadocModuleLoader* valadoc_module_loader_get_instance (void) {
	ValadocModuleLoader* result = NULL;
	ValadocModuleLoader* _tmp0_;
	ValadocModuleLoader* _tmp3_;
	ValadocModuleLoader* _tmp4_;
	_tmp0_ = valadoc_module_loader_instance;
	if (_tmp0_ == NULL) {
		ValadocModuleLoader* _tmp1_;
		ValadocModuleLoader* _tmp2_;
		_tmp1_ = valadoc_module_loader_new ();
		_g_object_unref0 (valadoc_module_loader_instance);
		valadoc_module_loader_instance = _tmp1_;
		_tmp2_ = valadoc_module_loader_instance;
		valadoc_taglets_init (_tmp2_);
	}
	_tmp3_ = valadoc_module_loader_instance;
	_tmp4_ = _g_object_ref0 (_tmp3_);
	result = _tmp4_;
	return result;
}


static ValadocModuleLoader* valadoc_module_loader_construct (GType object_type) {
	ValadocModuleLoader * self = NULL;
	self = (ValadocModuleLoader*) g_object_new (object_type, NULL);
	return self;
}


static ValadocModuleLoader* valadoc_module_loader_new (void) {
	return valadoc_module_loader_construct (VALADOC_TYPE_MODULE_LOADER);
}


gboolean valadoc_module_loader_is_doclet (const gchar* path) {
	gboolean result = FALSE;
	gchar* library_path = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	gboolean _tmp2_ = FALSE;
	const gchar* _tmp3_;
	gboolean _tmp4_;
	g_return_val_if_fail (path != NULL, FALSE);
	_tmp0_ = path;
	_tmp1_ = g_module_build_path (_tmp0_, "doclet");
	library_path = _tmp1_;
	_tmp3_ = path;
	_tmp4_ = g_file_test (_tmp3_, G_FILE_TEST_EXISTS);
	if (_tmp4_) {
		const gchar* _tmp5_;
		gboolean _tmp6_;
		_tmp5_ = library_path;
		_tmp6_ = g_file_test (_tmp5_, G_FILE_TEST_EXISTS);
		_tmp2_ = _tmp6_;
	} else {
		_tmp2_ = FALSE;
	}
	result = _tmp2_;
	_g_free0 (library_path);
	return result;
}


static gchar* valadoc_module_loader_get_plugin_path (const gchar* pluginpath, const gchar* pluginsubdir) {
	gchar* result = NULL;
	const gchar* _tmp0_;
	gboolean _tmp1_;
	const gchar* _tmp12_;
	gchar* _tmp13_;
	g_return_val_if_fail (pluginpath != NULL, NULL);
	g_return_val_if_fail (pluginsubdir != NULL, NULL);
	_tmp0_ = pluginpath;
	_tmp1_ = g_path_is_absolute (_tmp0_);
	if (_tmp1_ == FALSE) {
		gchar* local_path = NULL;
		gchar* _tmp2_;
		gchar* _tmp3_;
		const gchar* _tmp4_;
		gchar* _tmp5_;
		gchar* _tmp6_;
		const gchar* _tmp7_;
		gboolean _tmp8_;
		_tmp2_ = g_get_current_dir ();
		_tmp3_ = _tmp2_;
		_tmp4_ = pluginpath;
		_tmp5_ = g_build_filename (_tmp3_, _tmp4_, NULL);
		_tmp6_ = _tmp5_;
		_g_free0 (_tmp3_);
		local_path = _tmp6_;
		_tmp7_ = local_path;
		_tmp8_ = valadoc_module_loader_is_doclet (_tmp7_);
		if (_tmp8_) {
			result = local_path;
			return result;
		} else {
			const gchar* _tmp9_;
			const gchar* _tmp10_;
			gchar* _tmp11_;
			_tmp9_ = pluginsubdir;
			_tmp10_ = pluginpath;
			_tmp11_ = g_build_filename (PACKAGE_DATADIR, _tmp9_, _tmp10_, NULL);
			result = _tmp11_;
			_g_free0 (local_path);
			return result;
		}
		_g_free0 (local_path);
	}
	_tmp12_ = pluginpath;
	_tmp13_ = g_strdup (_tmp12_);
	result = _tmp13_;
	return result;
}


gchar* valadoc_module_loader_get_doclet_path (const gchar* docletpath, ValadocErrorReporter* reporter) {
	gchar* result = NULL;
	const gchar* _tmp0_;
	const gchar* _tmp2_;
	gchar* _tmp3_;
	g_return_val_if_fail (reporter != NULL, NULL);
	_tmp0_ = docletpath;
	if (_tmp0_ == NULL) {
		gchar* _tmp1_;
		_tmp1_ = g_build_filename (PACKAGE_DATADIR, "doclets", "html", NULL);
		result = _tmp1_;
		return result;
	}
	_tmp2_ = docletpath;
	_tmp3_ = valadoc_module_loader_get_plugin_path (_tmp2_, "doclets");
	result = _tmp3_;
	return result;
}


ValadocContentTaglet* valadoc_module_loader_create_taglet (ValadocModuleLoader* self, const gchar* keyword) {
	ValadocContentTaglet* result = NULL;
	ValadocContentTaglet* _tmp0_ = NULL;
	ValaHashMap* _tmp1_;
	const gchar* _tmp2_;
	gboolean _tmp3_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (keyword != NULL, NULL);
	_tmp1_ = self->priv->taglets;
	_tmp2_ = keyword;
	_tmp3_ = vala_map_contains ((ValaMap*) _tmp1_, _tmp2_);
	if (_tmp3_) {
		ValaHashMap* _tmp4_;
		const gchar* _tmp5_;
		gpointer _tmp6_;
		GObject* _tmp7_;
		GObject* _tmp8_;
		_tmp4_ = self->priv->taglets;
		_tmp5_ = keyword;
		_tmp6_ = vala_map_get ((ValaMap*) _tmp4_, _tmp5_);
		_tmp7_ = g_object_new ((GType) ((gintptr) _tmp6_), NULL);
		_tmp8_ = G_IS_INITIALLY_UNOWNED (_tmp7_) ? g_object_ref_sink (_tmp7_) : _tmp7_;
		_g_object_unref0 (_tmp0_);
		_tmp0_ = G_TYPE_CHECK_INSTANCE_CAST (_tmp8_, VALADOC_CONTENT_TYPE_TAGLET, ValadocContentTaglet);
	} else {
		_g_object_unref0 (_tmp0_);
		_tmp0_ = NULL;
	}
	result = _tmp0_;
	return result;
}


void valadoc_module_loader_register_taglet (ValadocModuleLoader* self, const gchar* keyword, GType type) {
	ValaHashMap* _tmp0_;
	const gchar* _tmp1_;
	GType _tmp2_;
	g_return_if_fail (self != NULL);
	g_return_if_fail (keyword != NULL);
	_tmp0_ = self->priv->taglets;
	_tmp1_ = keyword;
	_tmp2_ = type;
	vala_map_set ((ValaMap*) _tmp0_, _tmp1_, (gpointer) ((gintptr) _tmp2_));
}


ValadocDoclet* valadoc_module_loader_create_doclet (ValadocModuleLoader* self, const gchar* _path) {
	ValadocDoclet* result = NULL;
	gchar* path = NULL;
	const gchar* _tmp0_;
	gchar* _tmp1_;
	ValadocModuleLoaderModuleData* data = NULL;
	ValaHashMap* _tmp2_;
	const gchar* _tmp3_;
	gpointer _tmp4_;
	ValadocModuleLoaderModuleData* _tmp5_;
	ValadocModuleLoaderModuleData* _tmp25_;
	GType _tmp26_;
	GObject* _tmp27_;
	GObject* _tmp28_;
	g_return_val_if_fail (self != NULL, NULL);
	g_return_val_if_fail (_path != NULL, NULL);
	_tmp0_ = _path;
	_tmp1_ = valadoc_realpath (_tmp0_);
	path = _tmp1_;
	_tmp2_ = self->priv->doclets;
	_tmp3_ = path;
	_tmp4_ = vala_map_get ((ValaMap*) _tmp2_, _tmp3_);
	data = (ValadocModuleLoaderModuleData*) _tmp4_;
	_tmp5_ = data;
	if (_tmp5_ == NULL) {
		void* function = NULL;
		GModule* module = NULL;
		const gchar* _tmp6_;
		gchar* _tmp7_;
		gchar* _tmp8_;
		GModule* _tmp9_;
		GModule* _tmp10_;
		GModule* _tmp11_;
		GModule* _tmp12_;
		void* _tmp13_ = NULL;
		void* _tmp14_;
		ValadocDocletRegisterFunction register_func = NULL;
		void* _tmp15_;
		ValadocModuleLoaderModuleData* _tmp16_;
		ValaHashMap* _tmp17_;
		const gchar* _tmp18_;
		ValadocModuleLoaderModuleData* _tmp19_;
		ValadocModuleLoaderModuleData* _tmp20_;
		ValadocDocletRegisterFunction _tmp21_;
		GType _tmp22_;
		ValadocModuleLoaderModuleData* _tmp23_;
		GModule* _tmp24_;
		_tmp6_ = path;
		_tmp7_ = g_module_build_path (_tmp6_, "doclet");
		_tmp8_ = _tmp7_;
		_tmp9_ = g_module_open (_tmp8_, G_MODULE_BIND_LAZY | G_MODULE_BIND_LOCAL);
		_tmp10_ = _tmp9_;
		_g_free0 (_tmp8_);
		module = _tmp10_;
		_tmp11_ = module;
		if (_tmp11_ == NULL) {
			result = NULL;
			_g_module_close0 (module);
			_g_object_unref0 (data);
			_g_free0 (path);
			return result;
		}
		_tmp12_ = module;
		g_module_symbol (_tmp12_, "register_plugin", &_tmp13_);
		function = _tmp13_;
		_tmp14_ = function;
		if (_tmp14_ == NULL) {
			result = NULL;
			_g_module_close0 (module);
			_g_object_unref0 (data);
			_g_free0 (path);
			return result;
		}
		_tmp15_ = function;
		register_func = (ValadocDocletRegisterFunction) _tmp15_;
		_tmp16_ = valadoc_module_loader_module_data_new ();
		_g_object_unref0 (data);
		data = _tmp16_;
		_tmp17_ = self->priv->doclets;
		_tmp18_ = path;
		_tmp19_ = data;
		vala_map_set ((ValaMap*) _tmp17_, _tmp18_, _tmp19_);
		_tmp20_ = data;
		_tmp21_ = register_func;
		_tmp22_ = _tmp21_ (self);
		_tmp20_->type = _tmp22_;
		_tmp23_ = data;
		_tmp24_ = module;
		module = NULL;
		_g_module_close0 (_tmp23_->module);
		_tmp23_->module = _tmp24_;
		_g_module_close0 (module);
	}
	_tmp25_ = data;
	_tmp26_ = _tmp25_->type;
	_tmp27_ = g_object_new (_tmp26_, NULL);
	_tmp28_ = G_IS_INITIALLY_UNOWNED (_tmp27_) ? g_object_ref_sink (_tmp27_) : _tmp27_;
	result = G_TYPE_CHECK_INSTANCE_CAST (_tmp28_, VALADOC_TYPE_DOCLET, ValadocDoclet);
	_g_object_unref0 (data);
	_g_free0 (path);
	return result;
}


static ValadocModuleLoaderModuleData* valadoc_module_loader_module_data_construct (GType object_type) {
	ValadocModuleLoaderModuleData * self = NULL;
	self = (ValadocModuleLoaderModuleData*) g_object_new (object_type, NULL);
	return self;
}


static ValadocModuleLoaderModuleData* valadoc_module_loader_module_data_new (void) {
	return valadoc_module_loader_module_data_construct (VALADOC_MODULE_LOADER_TYPE_MODULE_DATA);
}


static void valadoc_module_loader_module_data_class_init (ValadocModuleLoaderModuleDataClass * klass) {
	valadoc_module_loader_module_data_parent_class = g_type_class_peek_parent (klass);
	G_OBJECT_CLASS (klass)->finalize = valadoc_module_loader_module_data_finalize;
}


static void valadoc_module_loader_module_data_instance_init (ValadocModuleLoaderModuleData * self) {
}


static void valadoc_module_loader_module_data_finalize (GObject * obj) {
	ValadocModuleLoaderModuleData * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, VALADOC_MODULE_LOADER_TYPE_MODULE_DATA, ValadocModuleLoaderModuleData);
	_g_module_close0 (self->module);
	G_OBJECT_CLASS (valadoc_module_loader_module_data_parent_class)->finalize (obj);
}


static GType valadoc_module_loader_module_data_get_type (void) {
	static volatile gsize valadoc_module_loader_module_data_type_id__volatile = 0;
	if (g_once_init_enter (&valadoc_module_loader_module_data_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ValadocModuleLoaderModuleDataClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) valadoc_module_loader_module_data_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValadocModuleLoaderModuleData), 0, (GInstanceInitFunc) valadoc_module_loader_module_data_instance_init, NULL };
		GType valadoc_module_loader_module_data_type_id;
		valadoc_module_loader_module_data_type_id = g_type_register_static (G_TYPE_OBJECT, "ValadocModuleLoaderModuleData", &g_define_type_info, 0);
		g_once_init_leave (&valadoc_module_loader_module_data_type_id__volatile, valadoc_module_loader_module_data_type_id);
	}
	return valadoc_module_loader_module_data_type_id__volatile;
}


static void valadoc_module_loader_class_init (ValadocModuleLoaderClass * klass) {
	valadoc_module_loader_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (ValadocModuleLoaderPrivate));
	G_OBJECT_CLASS (klass)->finalize = valadoc_module_loader_finalize;
}


static void valadoc_module_loader_instance_init (ValadocModuleLoader * self) {
	GHashFunc _tmp0_;
	GEqualFunc _tmp1_;
	GEqualFunc _tmp2_;
	ValaHashMap* _tmp3_;
	GHashFunc _tmp4_;
	GEqualFunc _tmp5_;
	GEqualFunc _tmp6_;
	ValaHashMap* _tmp7_;
	self->priv = VALADOC_MODULE_LOADER_GET_PRIVATE (self);
	_tmp0_ = g_str_hash;
	_tmp1_ = g_str_equal;
	_tmp2_ = g_direct_equal;
	_tmp3_ = vala_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, VALADOC_MODULE_LOADER_TYPE_MODULE_DATA, (GBoxedCopyFunc) g_object_ref, (GDestroyNotify) g_object_unref, _tmp0_, _tmp1_, _tmp2_);
	self->priv->doclets = _tmp3_;
	_tmp4_ = g_str_hash;
	_tmp5_ = g_str_equal;
	_tmp6_ = g_direct_equal;
	_tmp7_ = vala_hash_map_new (G_TYPE_STRING, (GBoxedCopyFunc) g_strdup, (GDestroyNotify) g_free, G_TYPE_GTYPE, NULL, NULL, _tmp4_, _tmp5_, _tmp6_);
	self->priv->taglets = _tmp7_;
}


static void valadoc_module_loader_finalize (GObject * obj) {
	ValadocModuleLoader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, VALADOC_TYPE_MODULE_LOADER, ValadocModuleLoader);
	_vala_map_unref0 (self->priv->doclets);
	_vala_map_unref0 (self->priv->taglets);
	G_OBJECT_CLASS (valadoc_module_loader_parent_class)->finalize (obj);
}


GType valadoc_module_loader_get_type (void) {
	static volatile gsize valadoc_module_loader_type_id__volatile = 0;
	if (g_once_init_enter (&valadoc_module_loader_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (ValadocModuleLoaderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) valadoc_module_loader_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (ValadocModuleLoader), 0, (GInstanceInitFunc) valadoc_module_loader_instance_init, NULL };
		GType valadoc_module_loader_type_id;
		valadoc_module_loader_type_id = g_type_register_static (G_TYPE_OBJECT, "ValadocModuleLoader", &g_define_type_info, 0);
		g_once_init_leave (&valadoc_module_loader_type_id__volatile, valadoc_module_loader_type_id);
	}
	return valadoc_module_loader_type_id__volatile;
}



