/*
 * Copyright (C) 2011-2014 Gary Kramlich <grim@reaperworld.com>
 *
 * 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 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, see <http://www.gnu.org/licenses/>.
 */

#include <gplugin/gplugin-native-plugin.h>

#include <gplugin/gplugin-native-private.h>

#include <gplugin/gplugin-loader.h>
#include <gplugin/gplugin-manager.h>

#include <gplugin/gplugin-core.h>

#include <glib/gi18n.h>

/**
 * SECTION:gplugin-native-plugin
 * @Title: Native Plugin API
 * @Short_description: API for native plugins
 *
 * API for use by native plugins.  That is plugins written in a compiled
 * language.
 */

/******************************************************************************
 * Structs
 *****************************************************************************/
typedef struct {
	GModule *module;

	gpointer load_func;
	gpointer unload_func;

	gchar *filename;
	GPluginLoader *loader;
	GPluginPluginInfo *info;
	GPluginPluginState state;
} GPluginNativePluginPrivate;

/******************************************************************************
 * Enums
 *****************************************************************************/
enum {
	PROP_ZERO,
	PROP_MODULE,
	PROP_LOAD_FUNC,
	PROP_UNLOAD_FUNC,
	N_PROPERTIES,
	/* overrides */
	PROP_FILENAME = N_PROPERTIES,
	PROP_LOADER,
	PROP_INFO,
	PROP_STATE
};
static GParamSpec *properties[N_PROPERTIES] = {NULL,};

/******************************************************************************
 * GPluginPlugin Implementation
 *****************************************************************************/
static void
gplugin_native_plugin_iface_init(GPluginPluginInterface *iface) {
	/* we just override properites from GPluginPlugin */
}

/******************************************************************************
 * GTypeModule Implementation
 *****************************************************************************/
static gboolean
gplugin_native_plugin_load(GTypeModule *module) {
	return TRUE;
}

static void
gplugin_native_plugin_unload(GTypeModule *module) {
}

/******************************************************************************
 * GObject Implementation
 *****************************************************************************/
G_DEFINE_TYPE_WITH_CODE(
	GPluginNativePlugin,
	gplugin_native_plugin,
	G_TYPE_TYPE_MODULE,
	G_ADD_PRIVATE(GPluginNativePlugin)
	G_IMPLEMENT_INTERFACE(GPLUGIN_TYPE_PLUGIN, gplugin_native_plugin_iface_init)
);

static void
gplugin_native_plugin_get_property(GObject *obj, guint param_id, GValue *value,
                                   GParamSpec *pspec)
{
	GPluginNativePlugin *plugin = GPLUGIN_NATIVE_PLUGIN(obj);
	GPluginNativePluginPrivate *priv = gplugin_native_plugin_get_instance_private(plugin);

	switch(param_id) {
		case PROP_MODULE:
			g_value_set_pointer(value,
			                    gplugin_native_plugin_get_module(plugin));
			break;
		case PROP_LOAD_FUNC:
			g_value_set_pointer(value, priv->load_func);
			break;
		case PROP_UNLOAD_FUNC:
			g_value_set_pointer(value, priv->unload_func);
			break;

		/* overrides */
		case PROP_FILENAME:
			g_value_set_string(value, priv->filename);
			break;
		case PROP_LOADER:
			g_value_set_object(value, priv->loader);
			break;
		case PROP_INFO:
			g_value_set_object(value, priv->info);
			break;
		case PROP_STATE:
			g_value_set_enum(value, priv->state);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
gplugin_native_plugin_set_property(GObject *obj, guint param_id,
                                   const GValue *value, GParamSpec *pspec)
{
	GPluginNativePluginPrivate *priv = gplugin_native_plugin_get_instance_private(GPLUGIN_NATIVE_PLUGIN(obj));

	switch(param_id) {
		case PROP_MODULE:
			priv->module = g_value_get_pointer(value);
			break;
		case PROP_LOAD_FUNC:
			priv->load_func = g_value_get_pointer(value);
			break;
		case PROP_UNLOAD_FUNC:
			priv->unload_func = g_value_get_pointer(value);
			break;

		/* overrides */
		case PROP_FILENAME:
			priv->filename = g_strdup(g_value_get_string(value));
			break;
		case PROP_LOADER:
			priv->loader = g_object_ref(g_value_get_object(value));
			break;
		case PROP_INFO:
			priv->info = g_object_ref(g_value_get_object(value));
			break;
		case PROP_STATE:
			priv->state = g_value_get_enum(value);
			break;

		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID(obj, param_id, pspec);
			break;
	}
}

static void
gplugin_native_plugin_finalize(GObject *obj) {
	GPluginNativePluginPrivate *priv = gplugin_native_plugin_get_instance_private(GPLUGIN_NATIVE_PLUGIN(obj));

	g_free(priv->filename);
	g_object_unref(G_OBJECT(priv->loader));
	g_object_unref(G_OBJECT(priv->info));

	G_OBJECT_CLASS(gplugin_native_plugin_parent_class)->finalize(obj);
}

static void
gplugin_native_plugin_init(GPluginNativePlugin *plugin) {
}

static void
gplugin_native_plugin_class_init(GPluginNativePluginClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);
	GTypeModuleClass *module_class = G_TYPE_MODULE_CLASS(klass);

	obj_class->finalize = gplugin_native_plugin_finalize;
	obj_class->get_property = gplugin_native_plugin_get_property;
	obj_class->set_property = gplugin_native_plugin_set_property;

	module_class->load = gplugin_native_plugin_load;
	module_class->unload = gplugin_native_plugin_unload;

	/**
	 * GPluginNativePlugin:module:
	 *
	 * The GModule instance for this plugin.
	 */
	properties[PROP_MODULE] = g_param_spec_pointer(
		"module", "module handle",
		"The GModule instance of the plugin",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	/**
	 * GPluginNativePlugin:load-func:
	 *
	 * A function pointer to the load method of the plugin.
	 */
	properties[PROP_LOAD_FUNC] = g_param_spec_pointer(
		"load-func", "load function pointer",
		"address pointer to load function",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	/**
	 * GPluginNativePlugin:unload-func:
	 *
	 * A function pointer to the unload method of the plugin.
	 */
	properties[PROP_UNLOAD_FUNC] = g_param_spec_pointer(
		"unload-func", "unload function pointer",
		"address pointer to the unload function",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	g_object_class_install_properties(obj_class, N_PROPERTIES, properties);

	/* add our overrides */
	g_object_class_override_property(obj_class, PROP_FILENAME, "filename");
	g_object_class_override_property(obj_class, PROP_LOADER, "loader");
	g_object_class_override_property(obj_class, PROP_INFO, "info");
	g_object_class_override_property(obj_class, PROP_STATE, "state");
}

/******************************************************************************
 * API
 *****************************************************************************/

/**
 * gplugin_native_plugin_get_module: (skip)
 * @plugin: #GPluginNativePlugin instance
 *
 * Returns the %GModule associated with this plugin.  This should really only
 * be used if you need to make your plugin resident.
 *
 * Returns: The %GModule associated with this plugin.
 */
GModule *
gplugin_native_plugin_get_module(GPluginNativePlugin *plugin) {
	GPluginNativePluginPrivate *priv = NULL;

	g_return_val_if_fail(GPLUGIN_IS_NATIVE_PLUGIN(plugin), NULL);

	priv = gplugin_native_plugin_get_instance_private(plugin);

	return priv->module;
}

