/* plugin_loader.c generated by valac 0.56.18-dirty, the Vala compiler
 * generated from plugin_loader.vala, do not modify */

/*
 *  pamac-vala
 *
 *  Copyright (C) 2019-2023 Guillaume Benoit <guillaume@manjaro.org>
 *
 *  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 get of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <glib-object.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>
#include <gmodule.h>

#if !defined(VALA_STRICT_C)
#if !defined(__clang__) && defined(__GNUC__) && (__GNUC__ >= 14)
#pragma GCC diagnostic warning "-Wincompatible-pointer-types"
#elif defined(__clang__) && (__clang_major__ >= 16)
#pragma clang diagnostic ignored "-Wincompatible-function-pointer-types"
#pragma clang diagnostic ignored "-Wincompatible-pointer-types"
#endif
#endif
#if !defined(VALA_EXTERN)
#if defined(_MSC_VER)
#define VALA_EXTERN __declspec(dllexport) extern
#elif __GNUC__ >= 4
#define VALA_EXTERN __attribute__((visibility("default"))) extern
#else
#define VALA_EXTERN extern
#endif
#endif

#define PAMAC_TYPE_PLUGIN_LOADER (pamac_plugin_loader_get_type ())
#define PAMAC_PLUGIN_LOADER(obj) (G_TYPE_CHECK_INSTANCE_CAST ((obj), PAMAC_TYPE_PLUGIN_LOADER, PamacPluginLoader))
#define PAMAC_PLUGIN_LOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), PAMAC_TYPE_PLUGIN_LOADER, PamacPluginLoaderClass))
#define PAMAC_IS_PLUGIN_LOADER(obj) (G_TYPE_CHECK_INSTANCE_TYPE ((obj), PAMAC_TYPE_PLUGIN_LOADER))
#define PAMAC_IS_PLUGIN_LOADER_CLASS(klass) (G_TYPE_CHECK_CLASS_TYPE ((klass), PAMAC_TYPE_PLUGIN_LOADER))
#define PAMAC_PLUGIN_LOADER_GET_CLASS(obj) (G_TYPE_INSTANCE_GET_CLASS ((obj), PAMAC_TYPE_PLUGIN_LOADER, PamacPluginLoaderClass))

typedef struct _PamacPluginLoader PamacPluginLoader;
typedef struct _PamacPluginLoaderClass PamacPluginLoaderClass;
typedef struct _PamacPluginLoaderPrivate PamacPluginLoaderPrivate;
enum  {
	PAMAC_PLUGIN_LOADER_0_PROPERTY,
	PAMAC_PLUGIN_LOADER_T_TYPE,
	PAMAC_PLUGIN_LOADER_T_DUP_FUNC,
	PAMAC_PLUGIN_LOADER_T_DESTROY_FUNC,
	PAMAC_PLUGIN_LOADER_PATH_PROPERTY,
	PAMAC_PLUGIN_LOADER_NUM_PROPERTIES
};
static GParamSpec* pamac_plugin_loader_properties[PAMAC_PLUGIN_LOADER_NUM_PROPERTIES];
#define _g_free0(var) (var = (g_free (var), NULL))
#define _g_module_close0(var) ((var == NULL) ? NULL : (var = (g_module_close (var), NULL)))
typedef GType (*PamacPluginLoaderRegisterPluginFunction) (GModule* module, gpointer user_data);
#define _vala_assert(expr, msg) if G_LIKELY (expr) ; else g_assertion_message_expr (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);
#define _vala_return_if_fail(expr, msg) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return; }
#define _vala_return_val_if_fail(expr, msg, val) if G_LIKELY (expr) ; else { g_return_if_fail_warning (G_LOG_DOMAIN, G_STRFUNC, msg); return val; }
#define _vala_warn_if_fail(expr, msg) if G_LIKELY (expr) ; else g_warn_message (G_LOG_DOMAIN, __FILE__, __LINE__, G_STRFUNC, msg);

struct _PamacPluginLoader {
	GObject parent_instance;
	PamacPluginLoaderPrivate * priv;
};

struct _PamacPluginLoaderClass {
	GObjectClass parent_class;
};

struct _PamacPluginLoaderPrivate {
	GType t_type;
	GBoxedCopyFunc t_dup_func;
	GDestroyNotify t_destroy_func;
	gchar* _path;
	GType type;
	GModule* module;
};

static gint PamacPluginLoader_private_offset;
static gpointer pamac_plugin_loader_parent_class = NULL;

VALA_EXTERN GType pamac_plugin_loader_get_type (void) G_GNUC_CONST ;
G_DEFINE_AUTOPTR_CLEANUP_FUNC (PamacPluginLoader, g_object_unref)
VALA_EXTERN PamacPluginLoader* pamac_plugin_loader_new (GType t_type,
                                            GBoxedCopyFunc t_dup_func,
                                            GDestroyNotify t_destroy_func,
                                            const gchar* name);
VALA_EXTERN PamacPluginLoader* pamac_plugin_loader_construct (GType object_type,
                                                  GType t_type,
                                                  GBoxedCopyFunc t_dup_func,
                                                  GDestroyNotify t_destroy_func,
                                                  const gchar* name);
static void pamac_plugin_loader_set_path (PamacPluginLoader* self,
                                   const gchar* value);
VALA_EXTERN gboolean pamac_plugin_loader_load (PamacPluginLoader* self);
VALA_EXTERN const gchar* pamac_plugin_loader_get_path (PamacPluginLoader* self);
VALA_EXTERN gpointer pamac_plugin_loader_get_plugin (PamacPluginLoader* self);
static void pamac_plugin_loader_finalize (GObject * obj);
static GType pamac_plugin_loader_get_type_once (void);
static void _vala_pamac_plugin_loader_get_property (GObject * object,
                                             guint property_id,
                                             GValue * value,
                                             GParamSpec * pspec);
static void _vala_pamac_plugin_loader_set_property (GObject * object,
                                             guint property_id,
                                             const GValue * value,
                                             GParamSpec * pspec);

static inline gpointer
pamac_plugin_loader_get_instance_private (PamacPluginLoader* self)
{
	return G_STRUCT_MEMBER_P (self, PamacPluginLoader_private_offset);
}

PamacPluginLoader*
pamac_plugin_loader_construct (GType object_type,
                               GType t_type,
                               GBoxedCopyFunc t_dup_func,
                               GDestroyNotify t_destroy_func,
                               const gchar* name)
{
	PamacPluginLoader * self = NULL;
	gchar* _tmp0_;
	gchar* _tmp1_;
	g_return_val_if_fail (name != NULL, NULL);
	self = (PamacPluginLoader*) g_object_new (object_type, "t-type", t_type, "t-dup-func", t_dup_func, "t-destroy-func", t_destroy_func, NULL);
	self->priv->t_type = t_type;
	self->priv->t_dup_func = t_dup_func;
	self->priv->t_destroy_func = t_destroy_func;
	_vala_assert (g_module_supported (), "Module.supported ()");
	_tmp0_ = g_module_build_path (NULL, name);
	_tmp1_ = _tmp0_;
	pamac_plugin_loader_set_path (self, _tmp1_);
	_g_free0 (_tmp1_);
	return self;
}

PamacPluginLoader*
pamac_plugin_loader_new (GType t_type,
                         GBoxedCopyFunc t_dup_func,
                         GDestroyNotify t_destroy_func,
                         const gchar* name)
{
	return pamac_plugin_loader_construct (PAMAC_TYPE_PLUGIN_LOADER, t_type, t_dup_func, t_destroy_func, name);
}

gboolean
pamac_plugin_loader_load (PamacPluginLoader* self)
{
	const gchar* _tmp0_;
	GModule* _tmp1_;
	GModule* _tmp2_;
	void* function = NULL;
	GModule* _tmp3_;
	void* _tmp4_ = NULL;
	PamacPluginLoaderRegisterPluginFunction register_plugin = NULL;
	void* _tmp5_;
	gpointer register_plugin_target;
	PamacPluginLoaderRegisterPluginFunction _tmp6_;
	gpointer _tmp6__target;
	GModule* _tmp7_;
	gboolean result;
	g_return_val_if_fail (self != NULL, FALSE);
	_tmp0_ = self->priv->_path;
	_tmp1_ = g_module_open (_tmp0_, G_MODULE_BIND_LAZY);
	_g_module_close0 (self->priv->module);
	self->priv->module = _tmp1_;
	_tmp2_ = self->priv->module;
	if (_tmp2_ == NULL) {
		result = FALSE;
		return result;
	}
	_tmp3_ = self->priv->module;
	g_module_symbol (_tmp3_, "register_plugin", &_tmp4_);
	function = _tmp4_;
	_tmp5_ = function;
	register_plugin = (PamacPluginLoaderRegisterPluginFunction) _tmp5_;
	register_plugin_target = NULL;
	_tmp6_ = register_plugin;
	_tmp6__target = register_plugin_target;
	_tmp7_ = self->priv->module;
	self->priv->type = _tmp6_ (_tmp7_, _tmp6__target);
	result = TRUE;
	return result;
}

gpointer
pamac_plugin_loader_get_plugin (PamacPluginLoader* self)
{
	GObject* plugin = NULL;
	GObject* _tmp0_;
	GObject* _tmp1_;
	gpointer result;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = g_object_new (self->priv->type, NULL);
	_tmp1_ = G_IS_INITIALLY_UNOWNED (_tmp0_) ? g_object_ref_sink (_tmp0_) : _tmp0_;
	plugin = _tmp1_;
	result = plugin;
	return result;
}

const gchar*
pamac_plugin_loader_get_path (PamacPluginLoader* self)
{
	const gchar* result;
	const gchar* _tmp0_;
	g_return_val_if_fail (self != NULL, NULL);
	_tmp0_ = self->priv->_path;
	result = _tmp0_;
	return result;
}

static void
pamac_plugin_loader_set_path (PamacPluginLoader* self,
                              const gchar* value)
{
	gchar* old_value;
	g_return_if_fail (self != NULL);
	old_value = pamac_plugin_loader_get_path (self);
	if (g_strcmp0 (value, old_value) != 0) {
		gchar* _tmp0_;
		_tmp0_ = g_strdup (value);
		_g_free0 (self->priv->_path);
		self->priv->_path = _tmp0_;
		g_object_notify_by_pspec ((GObject *) self, pamac_plugin_loader_properties[PAMAC_PLUGIN_LOADER_PATH_PROPERTY]);
	}
}

static void
pamac_plugin_loader_class_init (PamacPluginLoaderClass * klass,
                                gpointer klass_data)
{
	pamac_plugin_loader_parent_class = g_type_class_peek_parent (klass);
	g_type_class_adjust_private_offset (klass, &PamacPluginLoader_private_offset);
	G_OBJECT_CLASS (klass)->get_property = _vala_pamac_plugin_loader_get_property;
	G_OBJECT_CLASS (klass)->set_property = _vala_pamac_plugin_loader_set_property;
	G_OBJECT_CLASS (klass)->finalize = pamac_plugin_loader_finalize;
	g_object_class_install_property (G_OBJECT_CLASS (klass), PAMAC_PLUGIN_LOADER_T_TYPE, g_param_spec_gtype ("t-type", "type", "type", G_TYPE_NONE, G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), PAMAC_PLUGIN_LOADER_T_DUP_FUNC, g_param_spec_pointer ("t-dup-func", "dup func", "dup func", G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), PAMAC_PLUGIN_LOADER_T_DESTROY_FUNC, g_param_spec_pointer ("t-destroy-func", "destroy func", "destroy func", G_PARAM_STATIC_STRINGS | G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
	g_object_class_install_property (G_OBJECT_CLASS (klass), PAMAC_PLUGIN_LOADER_PATH_PROPERTY, pamac_plugin_loader_properties[PAMAC_PLUGIN_LOADER_PATH_PROPERTY] = g_param_spec_string ("path", "path", "path", NULL, G_PARAM_STATIC_STRINGS | G_PARAM_READABLE));
}

static void
pamac_plugin_loader_instance_init (PamacPluginLoader * self,
                                   gpointer klass)
{
	self->priv = pamac_plugin_loader_get_instance_private (self);
}

static void
pamac_plugin_loader_finalize (GObject * obj)
{
	PamacPluginLoader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (obj, PAMAC_TYPE_PLUGIN_LOADER, PamacPluginLoader);
	_g_free0 (self->priv->_path);
	_g_module_close0 (self->priv->module);
	G_OBJECT_CLASS (pamac_plugin_loader_parent_class)->finalize (obj);
}

static GType
pamac_plugin_loader_get_type_once (void)
{
	static const GTypeInfo g_define_type_info = { sizeof (PamacPluginLoaderClass), (GBaseInitFunc) NULL, (GBaseFinalizeFunc) NULL, (GClassInitFunc) pamac_plugin_loader_class_init, (GClassFinalizeFunc) NULL, NULL, sizeof (PamacPluginLoader), 0, (GInstanceInitFunc) pamac_plugin_loader_instance_init, NULL };
	GType pamac_plugin_loader_type_id;
	pamac_plugin_loader_type_id = g_type_register_static (G_TYPE_OBJECT, "PamacPluginLoader", &g_define_type_info, 0);
	PamacPluginLoader_private_offset = g_type_add_instance_private (pamac_plugin_loader_type_id, sizeof (PamacPluginLoaderPrivate));
	return pamac_plugin_loader_type_id;
}

GType
pamac_plugin_loader_get_type (void)
{
	static volatile gsize pamac_plugin_loader_type_id__once = 0;
	if (g_once_init_enter (&pamac_plugin_loader_type_id__once)) {
		GType pamac_plugin_loader_type_id;
		pamac_plugin_loader_type_id = pamac_plugin_loader_get_type_once ();
		g_once_init_leave (&pamac_plugin_loader_type_id__once, pamac_plugin_loader_type_id);
	}
	return pamac_plugin_loader_type_id__once;
}

static void
_vala_pamac_plugin_loader_get_property (GObject * object,
                                        guint property_id,
                                        GValue * value,
                                        GParamSpec * pspec)
{
	PamacPluginLoader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, PAMAC_TYPE_PLUGIN_LOADER, PamacPluginLoader);
	switch (property_id) {
		case PAMAC_PLUGIN_LOADER_PATH_PROPERTY:
		g_value_set_string (value, pamac_plugin_loader_get_path (self));
		break;
		case PAMAC_PLUGIN_LOADER_T_TYPE:
		g_value_set_gtype (value, self->priv->t_type);
		break;
		case PAMAC_PLUGIN_LOADER_T_DUP_FUNC:
		g_value_set_pointer (value, self->priv->t_dup_func);
		break;
		case PAMAC_PLUGIN_LOADER_T_DESTROY_FUNC:
		g_value_set_pointer (value, self->priv->t_destroy_func);
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

static void
_vala_pamac_plugin_loader_set_property (GObject * object,
                                        guint property_id,
                                        const GValue * value,
                                        GParamSpec * pspec)
{
	PamacPluginLoader * self;
	self = G_TYPE_CHECK_INSTANCE_CAST (object, PAMAC_TYPE_PLUGIN_LOADER, PamacPluginLoader);
	switch (property_id) {
		case PAMAC_PLUGIN_LOADER_PATH_PROPERTY:
		pamac_plugin_loader_set_path (self, g_value_get_string (value));
		break;
		case PAMAC_PLUGIN_LOADER_T_TYPE:
		self->priv->t_type = g_value_get_gtype (value);
		break;
		case PAMAC_PLUGIN_LOADER_T_DUP_FUNC:
		self->priv->t_dup_func = g_value_get_pointer (value);
		break;
		case PAMAC_PLUGIN_LOADER_T_DESTROY_FUNC:
		self->priv->t_destroy_func = g_value_get_pointer (value);
		break;
		default:
		G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
		break;
	}
}

