/* rygel-engine-loader.c generated by valac 0.18.0.2-2c22, 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 <config.h>
#include <gio/gio.h>
#include <gmodule.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);
RygelMediaEngine* rygel_engine_loader_load_engine (RygelEngineLoader* self);
static gboolean rygel_engine_loader_real_load_module_from_file (RygelRecursiveModuleLoader* base, GFile* file);
static void rygel_engine_loader_finalize (GObject* obj);


RygelEngineLoader* rygel_engine_loader_construct (GType object_type) {
	RygelEngineLoader * self = NULL;
	gchar* _tmp0_;
	gchar* path;
	RygelMetaConfig* _tmp1_ = NULL;
	RygelMetaConfig* config;
	const gchar* _tmp4_;
	GError * _inner_error_ = NULL;
	_tmp0_ = g_strdup (ENGINE_DIR);
	path = _tmp0_;
	_tmp1_ = rygel_meta_config_get_default ();
	config = _tmp1_;
	{
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_;
		_tmp2_ = rygel_configuration_get_engine_path ((RygelConfiguration*) config, &_inner_error_);
		_tmp3_ = _tmp2_;
		if (_inner_error_ != NULL) {
			goto __catch22_g_error;
		}
		_g_free0 (path);
		path = _tmp3_;
	}
	goto __finally22;
	__catch22_g_error:
	{
		GError* _error_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (_error_);
	}
	__finally22:
	if (_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;
	}
	_tmp4_ = path;
	self = (RygelEngineLoader*) rygel_recursive_module_loader_construct (object_type, _tmp4_);
	{
		gchar* _tmp5_ = NULL;
		gchar* _tmp6_;
		const gchar* _tmp7_;
		_tmp5_ = rygel_configuration_get_media_engine ((RygelConfiguration*) config, &_inner_error_);
		_tmp6_ = _tmp5_;
		if (_inner_error_ != NULL) {
			goto __catch23_g_error;
		}
		_g_free0 (self->priv->engine_name);
		self->priv->engine_name = _tmp6_;
		_tmp7_ = self->priv->engine_name;
		g_debug ("rygel-engine-loader.vala:42: Looking for specific engine named '%s", _tmp7_);
	}
	goto __finally23;
	__catch23_g_error:
	{
		GError* _error_ = NULL;
		_error_ = _inner_error_;
		_inner_error_ = NULL;
		_g_error_free0 (_error_);
	}
	__finally23:
	if (_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;
	}
	_g_object_unref0 (config);
	_g_free0 (path);
	return self;
}


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


/**
     * 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_;
	GFile* _tmp6_;
	gchar* _tmp7_ = NULL;
	gchar* _tmp8_;
	GModule* _tmp9_ = NULL;
	GModule* _tmp10_;
	GModule* module;
	GModule* _tmp11_;
	void* function = NULL;
	GModule* _tmp17_;
	void* _tmp18_ = NULL;
	gboolean _tmp19_ = FALSE;
	void* _tmp21_;
	RygelEngineLoaderModuleInstanceFunc get_instance;
	void* get_instance_target;
	GModule* _tmp22_;
	RygelEngineLoaderModuleInstanceFunc _tmp23_;
	void* _tmp23__target;
	RygelMediaEngine* _tmp24_ = NULL;
	self = (RygelEngineLoader*) base;
	g_return_val_if_fail (file != NULL, FALSE);
	_tmp0_ = self->priv->engine_name;
	if (_tmp0_ != NULL) {
		GFile* _tmp1_;
		gchar* _tmp2_ = NULL;
		gchar* _tmp3_;
		const gchar* _tmp4_;
		gboolean _tmp5_;
		_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_, 0);
	_tmp10_ = _tmp9_;
	_g_free0 (_tmp8_);
	module = _tmp10_;
	_tmp11_ = module;
	if (_tmp11_ == NULL) {
		GFile* _tmp12_;
		gchar* _tmp13_ = NULL;
		gchar* _tmp14_;
		const gchar* _tmp15_ = NULL;
		const gchar* _tmp16_;
		_tmp12_ = file;
		_tmp13_ = g_file_get_path (_tmp12_);
		_tmp14_ = _tmp13_;
		_tmp15_ = g_module_error ();
		g_debug ("rygel-engine-loader.vala:65: 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_;
		_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 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));
	RYGEL_RECURSIVE_MODULE_LOADER_CLASS (klass)->load_module_from_file = rygel_engine_loader_real_load_module_from_file;
	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;
}



