/* rygel-engine-loader.c generated by valac 0.25.1.16-16e7a, the Vala compiler
 * generated from rygel-engine-loader.vala, do not modify */

/*
 * Copyright (C) 2012 Intel Corporation.
 *
 * Author: Jens Georg <jensg@openismus.com>
 *
 * This file is part of Rygel.
 *
 * Rygel 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 of the License, or
 * (at your option) any later version.
 *
 * Rygel 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 program; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 */

#include <glib.h>
#include <glib-object.h>
#include <rygel-core.h>
#include <stdlib.h>
#include <string.h>
#include <gio/gio.h>
#include <gmodule.h>
#include <config.h>


#define RYGEL_TYPE_ENGINE_LOADER (rygel_engine_loader_get_type ())
#define RYGEL_ENGINE_LOADER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_ENGINE_LOADER, RygelEngineLoader))
#define RYGEL_ENGINE_LOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_ENGINE_LOADER, RygelEngineLoaderClass))
#define RYGEL_IS_ENGINE_LOADER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_ENGINE_LOADER))
#define RYGEL_IS_ENGINE_LOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_ENGINE_LOADER))
#define RYGEL_ENGINE_LOADER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_ENGINE_LOADER, RygelEngineLoaderClass))

typedef struct _RygelEngineLoader RygelEngineLoader;
typedef struct _RygelEngineLoaderClass RygelEngineLoaderClass;
typedef struct _RygelEngineLoaderPrivate RygelEngineLoaderPrivate;

#define RYGEL_TYPE_MEDIA_ENGINE (rygel_media_engine_get_type ())
#define RYGEL_MEDIA_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), RYGEL_TYPE_MEDIA_ENGINE, RygelMediaEngine))
#define RYGEL_MEDIA_ENGINE_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), RYGEL_TYPE_MEDIA_ENGINE, RygelMediaEngineClass))
#define RYGEL_IS_MEDIA_ENGINE(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), RYGEL_TYPE_MEDIA_ENGINE))
#define RYGEL_IS_MEDIA_ENGINE_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), RYGEL_TYPE_MEDIA_ENGINE))
#define RYGEL_MEDIA_ENGINE_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), RYGEL_TYPE_MEDIA_ENGINE, RygelMediaEngineClass))

typedef struct _RygelMediaEngine RygelMediaEngine;
typedef struct _RygelMediaEngineClass RygelMediaEngineClass;
#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)))
#define _g_module_close0(var) ((var == NULL) ? NULL : (var = (g_module_close (var), NULL)))

struct _RygelEngineLoader {
	RygelRecursiveModuleLoader parent_instance;
	RygelEngineLoaderPrivate * priv;
};

struct _RygelEngineLoaderClass {
	RygelRecursiveModuleLoaderClass parent_class;
};

struct _RygelEngineLoaderPrivate {
	RygelMediaEngine* instance;
	gchar* engine_name;
};

typedef RygelMediaEngine* (*RygelEngineLoaderModuleInstanceFunc) (void* user_data);

static gpointer rygel_engine_loader_parent_class = NULL;

GType rygel_engine_loader_get_type (void) G_GNUC_CONST;
GType rygel_media_engine_get_type (void) G_GNUC_CONST;
#define RYGEL_ENGINE_LOADER_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), RYGEL_TYPE_ENGINE_LOADER, RygelEngineLoaderPrivate))
enum  {
	RYGEL_ENGINE_LOADER_DUMMY_PROPERTY
};
RygelEngineLoader* rygel_engine_loader_new (void);
RygelEngineLoader* rygel_engine_loader_construct (GType object_type);
static gchar* rygel_engine_loader_get_config (void);
static void rygel_engine_loader_real_constructed (GObject* base);
RygelMediaEngine* rygel_engine_loader_load_engine (RygelEngineLoader* self);
static gboolean rygel_engine_loader_real_load_module_from_file (RygelRecursiveModuleLoader* base, GFile* file);
static gboolean rygel_engine_loader_real_load_module_from_info (RygelRecursiveModuleLoader* base, RygelPluginInformation* info);
static void rygel_engine_loader_finalize (GObject* obj);


RygelEngineLoader* rygel_engine_loader_construct (GType object_type) {
	RygelEngineLoader * self = NULL;
	gchar* _tmp0_ = NULL;
	gchar* _tmp1_ = NULL;
	_tmp0_ = rygel_engine_loader_get_config ();
	_tmp1_ = _tmp0_;
	self = (RygelEngineLoader*) g_object_new (object_type, "base-path", _tmp1_, NULL);
	_g_free0 (_tmp1_);
	return self;
}


RygelEngineLoader* rygel_engine_loader_new (void) {
	return rygel_engine_loader_construct (RYGEL_TYPE_ENGINE_LOADER);
}


static void rygel_engine_loader_real_constructed (GObject* base) {
	RygelEngineLoader * self;
	const gchar* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	GError * _inner_error_ = NULL;
	self = (RygelEngineLoader*) base;
	G_OBJECT_CLASS (rygel_engine_loader_parent_class)->constructed ((GObject*) G_TYPE_CHECK_INSTANCE_CAST (self, RYGEL_TYPE_RECURSIVE_MODULE_LOADER, RygelRecursiveModuleLoader));
	_tmp0_ = rygel_recursive_module_loader_get_base_path ((RygelRecursiveModuleLoader*) self);
	_tmp1_ = _tmp0_;
	if (_tmp1_ == NULL) {
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_ = NULL;
		_tmp2_ = rygel_engine_loader_get_config ();
		_tmp3_ = _tmp2_;
		rygel_recursive_module_loader_set_base_path ((RygelRecursiveModuleLoader*) self, _tmp3_);
		_g_free0 (_tmp3_);
	}
	{
		RygelMetaConfig* config = NULL;
		RygelMetaConfig* _tmp4_ = NULL;
		gchar* _tmp5_ = NULL;
		gchar* _tmp6_ = NULL;
		gchar* _tmp7_ = NULL;
		const gchar* _tmp8_ = NULL;
		_tmp4_ = rygel_meta_config_get_default ();
		config = _tmp4_;
		_tmp6_ = rygel_configuration_get_media_engine ((RygelConfiguration*) config, &_inner_error_);
		_tmp5_ = _tmp6_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			_g_object_unref0 (config);
			goto __catch27_g_error;
		}
		_tmp7_ = _tmp5_;
		_tmp5_ = NULL;
		_g_free0 (self->priv->engine_name);
		self->priv->engine_name = _tmp7_;
		_tmp8_ = self->priv->engine_name;
		g_debug ("rygel-engine-loader.vala:45: Looking for specific engine named '%s", _tmp8_);
		_g_free0 (_tmp5_);
		_g_object_unref0 (config);
	}
	goto __finally27;
	__catch27_g_error:
	{
		GError* _error_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (_error_);
	}
	__finally27:
	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;
	}
}


/**
     * Load a media engine.
     */
static gpointer _g_object_ref0 (gpointer self) {
	return self ? g_object_ref (self) : NULL;
}


RygelMediaEngine* rygel_engine_loader_load_engine (RygelEngineLoader* self) {
	RygelMediaEngine* result = NULL;
	RygelMediaEngine* _tmp0_ = NULL;
	RygelMediaEngine* _tmp1_ = NULL;
	g_return_val_if_fail (self != NULL, NULL);
	rygel_recursive_module_loader_load_modules_sync ((RygelRecursiveModuleLoader*) self, NULL);
	_tmp0_ = self->priv->instance;
	_tmp1_ = _g_object_ref0 (_tmp0_);
	result = _tmp1_;
	return result;
}


static gboolean rygel_engine_loader_real_load_module_from_file (RygelRecursiveModuleLoader* base, GFile* file) {
	RygelEngineLoader * self;
	gboolean result = FALSE;
	const gchar* _tmp0_ = NULL;
	GModule* module = NULL;
	GFile* _tmp6_ = NULL;
	gchar* _tmp7_ = NULL;
	gchar* _tmp8_ = NULL;
	GModule* _tmp9_ = NULL;
	GModule* _tmp10_ = NULL;
	GModule* _tmp11_ = NULL;
	void* function = NULL;
	GModule* _tmp17_ = NULL;
	void* _tmp18_ = NULL;
	gboolean _tmp19_ = FALSE;
	RygelEngineLoaderModuleInstanceFunc get_instance = NULL;
	void* _tmp21_ = NULL;
	void* get_instance_target = NULL;
	GModule* _tmp22_ = NULL;
	RygelEngineLoaderModuleInstanceFunc _tmp23_ = NULL;
	void* _tmp23__target = NULL;
	RygelMediaEngine* _tmp24_ = NULL;
	self = (RygelEngineLoader*) base;
	g_return_val_if_fail (file != NULL, FALSE);
	_tmp0_ = self->priv->engine_name;
	if (_tmp0_ != NULL) {
		GFile* _tmp1_ = NULL;
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_ = NULL;
		const gchar* _tmp4_ = NULL;
		gboolean _tmp5_ = FALSE;
		_tmp1_ = file;
		_tmp2_ = g_file_get_basename (_tmp1_);
		_tmp3_ = _tmp2_;
		_tmp4_ = self->priv->engine_name;
		_tmp5_ = g_strcmp0 (_tmp3_, _tmp4_) != 0;
		_g_free0 (_tmp3_);
		if (_tmp5_) {
			result = TRUE;
			return result;
		}
	}
	_tmp6_ = file;
	_tmp7_ = g_file_get_path (_tmp6_);
	_tmp8_ = _tmp7_;
	_tmp9_ = g_module_open (_tmp8_, G_MODULE_BIND_LOCAL);
	_tmp10_ = _tmp9_;
	_g_free0 (_tmp8_);
	module = _tmp10_;
	_tmp11_ = module;
	if (_tmp11_ == NULL) {
		GFile* _tmp12_ = NULL;
		gchar* _tmp13_ = NULL;
		gchar* _tmp14_ = NULL;
		const gchar* _tmp15_ = NULL;
		const gchar* _tmp16_ = NULL;
		_tmp12_ = file;
		_tmp13_ = g_file_get_path (_tmp12_);
		_tmp14_ = _tmp13_;
		_tmp15_ = g_module_error ();
		g_debug ("rygel-engine-loader.vala:68: Failed to load engine %s: %s", _tmp14_, _tmp15_);
		_g_free0 (_tmp14_);
		_tmp16_ = self->priv->engine_name;
		if (_tmp16_ != NULL) {
			result = FALSE;
			_g_module_close0 (module);
			return result;
		}
		result = TRUE;
		_g_module_close0 (module);
		return result;
	}
	_tmp17_ = module;
	_tmp19_ = g_module_symbol (_tmp17_, "module_get_instance", &_tmp18_);
	function = _tmp18_;
	if (!_tmp19_) {
		const gchar* _tmp20_ = NULL;
		_tmp20_ = self->priv->engine_name;
		if (_tmp20_ != NULL) {
			result = FALSE;
			_g_module_close0 (module);
			return result;
		}
		result = TRUE;
		_g_module_close0 (module);
		return result;
	}
	_tmp21_ = function;
	get_instance = (RygelEngineLoaderModuleInstanceFunc) _tmp21_;
	get_instance_target = NULL;
	_tmp22_ = module;
	g_module_make_resident (_tmp22_);
	_tmp23_ = get_instance;
	_tmp23__target = get_instance_target;
	_tmp24_ = _tmp23_ (_tmp23__target);
	_g_object_unref0 (self->priv->instance);
	self->priv->instance = _tmp24_;
	result = FALSE;
	_g_module_close0 (module);
	return result;
}


static gboolean rygel_engine_loader_real_load_module_from_info (RygelRecursiveModuleLoader* base, RygelPluginInformation* info) {
	RygelEngineLoader * self;
	gboolean result = FALSE;
	RygelPluginInformation* _tmp0_ = NULL;
	const gchar* _tmp1_ = NULL;
	const gchar* _tmp2_ = NULL;
	GFile* _tmp3_ = NULL;
	GFile* _tmp4_ = NULL;
	gboolean _tmp5_ = FALSE;
	gboolean _tmp6_ = FALSE;
	self = (RygelEngineLoader*) base;
	g_return_val_if_fail (info != NULL, FALSE);
	_tmp0_ = info;
	_tmp1_ = rygel_plugin_information_get_module_path (_tmp0_);
	_tmp2_ = _tmp1_;
	_tmp3_ = g_file_new_for_path (_tmp2_);
	_tmp4_ = _tmp3_;
	_tmp5_ = rygel_recursive_module_loader_load_module_from_file ((RygelRecursiveModuleLoader*) self, _tmp4_);
	_tmp6_ = _tmp5_;
	_g_object_unref0 (_tmp4_);
	result = _tmp6_;
	return result;
}


static gchar* rygel_engine_loader_get_config (void) {
	gchar* result = NULL;
	gchar* path = NULL;
	gchar* _tmp0_ = NULL;
	RygelMetaConfig* config = NULL;
	RygelMetaConfig* _tmp1_ = NULL;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_strdup (ENGINE_DIR);
	path = _tmp0_;
	_tmp1_ = rygel_meta_config_get_default ();
	config = _tmp1_;
	{
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_ = NULL;
		gchar* _tmp4_ = NULL;
		_tmp3_ = rygel_configuration_get_engine_path ((RygelConfiguration*) config, &_inner_error_);
		_tmp2_ = _tmp3_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch28_g_error;
		}
		_tmp4_ = _tmp2_;
		_tmp2_ = NULL;
		_g_free0 (path);
		path = _tmp4_;
		_g_free0 (_tmp2_);
	}
	goto __finally28;
	__catch28_g_error:
	{
		GError* _error_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (_error_);
	}
	__finally28:
	if (G_UNLIKELY (_inner_error_ != NULL)) {
		_g_object_unref0 (config);
		_g_free0 (path);
		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 NULL;
	}
	result = path;
	_g_object_unref0 (config);
	return result;
}


static void rygel_engine_loader_class_init (RygelEngineLoaderClass * klass) {
	rygel_engine_loader_parent_class = g_type_class_peek_parent (klass);
	g_type_class_add_private (klass, sizeof (RygelEngineLoaderPrivate));
	((GObjectClass *) klass)->constructed = rygel_engine_loader_real_constructed;
	((RygelRecursiveModuleLoaderClass *) klass)->load_module_from_file = rygel_engine_loader_real_load_module_from_file;
	((RygelRecursiveModuleLoaderClass *) klass)->load_module_from_info = rygel_engine_loader_real_load_module_from_info;
	G_OBJECT_CLASS (klass)->finalize = rygel_engine_loader_finalize;
}


static void rygel_engine_loader_instance_init (RygelEngineLoader * self) {
	self->priv = RYGEL_ENGINE_LOADER_GET_PRIVATE (self);
}


static void rygel_engine_loader_finalize (GObject* obj) {
	RygelEngineLoader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, RYGEL_TYPE_ENGINE_LOADER, RygelEngineLoader);
	_g_object_unref0 (self->priv->instance);
	_g_free0 (self->priv->engine_name);
	G_OBJECT_CLASS (rygel_engine_loader_parent_class)->finalize (obj);
}


/**
 * Load a media engine.
 */
GType rygel_engine_loader_get_type (void) {
	static volatile gsize rygel_engine_loader_type_id__volatile = 0;
	if (g_once_init_enter (&rygel_engine_loader_type_id__volatile)) {
		static const GTypeInfo g_define_type_info = { sizeof (RygelEngineLoaderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) rygel_engine_loader_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (RygelEngineLoader), 0, (GInstanceInitFunc) rygel_engine_loader_instance_init, NULL };
		GType rygel_engine_loader_type_id;
		rygel_engine_loader_type_id = g_type_register_static (RYGEL_TYPE_RECURSIVE_MODULE_LOADER, "RygelEngineLoader", &g_define_type_info, 0);
		g_once_init_leave (&rygel_engine_loader_type_id__volatile, rygel_engine_loader_type_id);
	}
	return rygel_engine_loader_type_id__volatile;
}



