/* rygel-engine-loader.c generated by valac 0.40.8, 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.1 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 library; 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;
enum  {
	RYGEL_ENGINE_LOADER_0_PROPERTY,
	RYGEL_ENGINE_LOADER_NUM_PROPERTIES
};
static GParamSpec* rygel_engine_loader_properties[RYGEL_ENGINE_LOADER_NUM_PROPERTIES];
#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) (gpointer 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))
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_;
	gchar* _tmp1_;
	_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_;
	const gchar* _tmp1_;
	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_;
		gchar* _tmp3_;
		_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_;
		gchar* _tmp5_ = NULL;
		gchar* _tmp6_;
		gchar* _tmp7_;
		const gchar* _tmp8_;
		_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 __catch36_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 __finally36;
	__catch36_g_error:
	{
		GError* _error_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (_error_);
	}
	__finally36:
	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_;
	RygelMediaEngine* _tmp1_;
	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_;
	GModule* module = NULL;
	gchar* _tmp5_;
	gchar* _tmp6_;
	GModule* _tmp7_;
	GModule* _tmp8_;
	GModule* _tmp9_;
	void* function = NULL;
	GModule* _tmp14_;
	void* _tmp15_ = NULL;
	gboolean _tmp16_;
	RygelEngineLoaderModuleInstanceFunc get_instance = NULL;
	void* _tmp18_;
	void* get_instance_target;
	GModule* _tmp19_;
	RygelEngineLoaderModuleInstanceFunc _tmp20_;
	void* _tmp20__target;
	RygelMediaEngine* _tmp21_;
	self = (RygelEngineLoader*) base;
	g_return_val_if_fail (file != NULL, FALSE);
	_tmp0_ = self->priv->engine_name;
	if (_tmp0_ != NULL) {
		gchar* _tmp1_;
		gchar* _tmp2_;
		const gchar* _tmp3_;
		gboolean _tmp4_;
		_tmp1_ = g_file_get_basename (file);
		_tmp2_ = _tmp1_;
		_tmp3_ = self->priv->engine_name;
		_tmp4_ = g_strcmp0 (_tmp2_, _tmp3_) != 0;
		_g_free0 (_tmp2_);
		if (_tmp4_) {
			result = TRUE;
			return result;
		}
	}
	_tmp5_ = g_file_get_path (file);
	_tmp6_ = _tmp5_;
	_tmp7_ = g_module_open (_tmp6_, G_MODULE_BIND_LOCAL);
	_tmp8_ = _tmp7_;
	_g_free0 (_tmp6_);
	module = _tmp8_;
	_tmp9_ = module;
	if (_tmp9_ == NULL) {
		gchar* _tmp10_;
		gchar* _tmp11_;
		const gchar* _tmp12_;
		const gchar* _tmp13_;
		_tmp10_ = g_file_get_path (file);
		_tmp11_ = _tmp10_;
		_tmp12_ = g_module_error ();
		g_debug ("rygel-engine-loader.vala:68: Failed to load engine %s: %s", _tmp11_, _tmp12_);
		_g_free0 (_tmp11_);
		_tmp13_ = self->priv->engine_name;
		if (_tmp13_ != NULL) {
			result = FALSE;
			_g_module_close0 (module);
			return result;
		}
		result = TRUE;
		_g_module_close0 (module);
		return result;
	}
	_tmp14_ = module;
	_tmp16_ = g_module_symbol (_tmp14_, "module_get_instance", &_tmp15_);
	function = _tmp15_;
	if (!_tmp16_) {
		const gchar* _tmp17_;
		_tmp17_ = self->priv->engine_name;
		if (_tmp17_ != NULL) {
			result = FALSE;
			_g_module_close0 (module);
			return result;
		}
		result = TRUE;
		_g_module_close0 (module);
		return result;
	}
	_tmp18_ = function;
	get_instance = (RygelEngineLoaderModuleInstanceFunc) _tmp18_;
	get_instance_target = NULL;
	_tmp19_ = module;
	g_module_make_resident (_tmp19_);
	_tmp20_ = get_instance;
	_tmp20__target = get_instance_target;
	_tmp21_ = _tmp20_ (_tmp20__target);
	_g_object_unref0 (self->priv->instance);
	self->priv->instance = _tmp21_;
	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;
	const gchar* _tmp0_;
	const gchar* _tmp1_;
	GFile* _tmp2_;
	GFile* _tmp3_;
	gboolean _tmp4_;
	self = (RygelEngineLoader*) base;
	g_return_val_if_fail (info != NULL, FALSE);
	_tmp0_ = rygel_plugin_information_get_module_path (info);
	_tmp1_ = _tmp0_;
	_tmp2_ = g_file_new_for_path (_tmp1_);
	_tmp3_ = _tmp2_;
	_tmp4_ = rygel_recursive_module_loader_load_module_from_file ((RygelRecursiveModuleLoader*) self, _tmp3_);
	_g_object_unref0 (_tmp3_);
	result = _tmp4_;
	return result;
}


static gchar*
rygel_engine_loader_get_config (void)
{
	gchar* result = NULL;
	gchar* path = NULL;
	gchar* _tmp0_;
	RygelMetaConfig* config = NULL;
	RygelMetaConfig* _tmp1_;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_strdup (ENGINE_DIR);
	path = _tmp0_;
	_tmp1_ = rygel_meta_config_get_default ();
	config = _tmp1_;
	{
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_;
		gchar* _tmp4_;
		_tmp3_ = rygel_configuration_get_engine_path ((RygelConfiguration*) config, &_inner_error_);
		_tmp2_ = _tmp3_;
		if (G_UNLIKELY (_inner_error_ != NULL)) {
			goto __catch37_g_error;
		}
		_tmp4_ = _tmp2_;
		_tmp2_ = NULL;
		_g_free0 (path);
		path = _tmp4_;
		_g_free0 (_tmp2_);
	}
	goto __finally37;
	__catch37_g_error:
	{
		GError* _error_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (_error_);
	}
	__finally37:
	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 = (void (*) (GObject *)) rygel_engine_loader_real_constructed;
	((RygelRecursiveModuleLoaderClass *) klass)->load_module_from_file = (gboolean (*) (RygelRecursiveModuleLoader *, GFile*)) rygel_engine_loader_real_load_module_from_file;
	((RygelRecursiveModuleLoaderClass *) klass)->load_module_from_info = (gboolean (*) (RygelRecursiveModuleLoader *, RygelPluginInformation*)) 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;
}



