/*
 * 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 <Python.h>

#include "gplugin-python-plugin.h"

#include <pygobject.h>

/******************************************************************************
 * Typedefs
 *****************************************************************************/
typedef struct {
	PyObject *module;
	PyObject *query;
	PyObject *load;
	PyObject *unload;

	/* overrides */
	gchar *filename;
	GPluginLoader *loader;
	GPluginPluginInfo *info;
	GPluginPluginState state;
} GPluginPythonPluginPrivate;

/******************************************************************************
 * 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,};

/* I hate forward declarations... */
static void gplugin_python_plugin_iface_init(GPluginPluginInterface *iface);

G_DEFINE_DYNAMIC_TYPE_EXTENDED(
	GPluginPythonPlugin,
	gplugin_python_plugin,
	G_TYPE_OBJECT,
	0,
	G_ADD_PRIVATE_DYNAMIC(GPluginPythonPlugin)
	G_IMPLEMENT_INTERFACE(GPLUGIN_TYPE_PLUGIN, gplugin_python_plugin_iface_init)
);

/******************************************************************************
 * GPluginPlugin Implementation
 *****************************************************************************/
static void
gplugin_python_plugin_iface_init(GPluginPluginInterface *iface) {
}

/******************************************************************************
 * Private Stuff
 *****************************************************************************/
static PyObject *
gplugin_python_plugin_get_module(GPluginPythonPlugin *plugin) {
	GPluginPythonPluginPrivate *priv = NULL;

	g_return_val_if_fail(GPLUGIN_IS_PYTHON_PLUGIN(plugin), NULL);

	priv = gplugin_python_plugin_get_instance_private(plugin);

	return priv->module;
}

static void
gplugin_python_plugin_set_module(GPluginPythonPlugin *plugin,
                                 PyObject *module)
{
	GPluginPythonPluginPrivate *priv = NULL;

	g_return_if_fail(GPLUGIN_IS_PLUGIN(plugin));
	g_return_if_fail(module);

	priv = gplugin_python_plugin_get_instance_private(plugin);

	if(priv->module)
		Py_DECREF(priv->module);

	priv->module = module;

	if(priv->module)
		Py_INCREF(priv->module);
}

static gpointer
gplugin_python_plugin_get_load_func(GPluginPythonPlugin *plugin) {
	GPluginPythonPluginPrivate *priv = NULL;

	g_return_val_if_fail(GPLUGIN_IS_PYTHON_PLUGIN(plugin), NULL);

	priv = gplugin_python_plugin_get_instance_private(plugin);

	return priv->load;
}

static void
gplugin_python_plugin_set_load_func(GPluginPythonPlugin *plugin,
                                     PyObject *func)
{
	GPluginPythonPluginPrivate *priv = NULL;

	g_return_if_fail(GPLUGIN_IS_PYTHON_PLUGIN(plugin));
	g_return_if_fail(func != NULL);

	priv = gplugin_python_plugin_get_instance_private(plugin);

	if(priv->load)
		Py_DECREF(priv->load);

	priv->load = func;
	if(priv->load)
		Py_INCREF(priv->load);
}

static gpointer
gplugin_python_plugin_get_unload_func(GPluginPythonPlugin *plugin) {
	GPluginPythonPluginPrivate *priv = NULL;

	g_return_val_if_fail(GPLUGIN_IS_PYTHON_PLUGIN(plugin), NULL);

	priv = gplugin_python_plugin_get_instance_private(plugin);

	return priv->unload;
}

static void
gplugin_python_plugin_set_unload_func(GPluginPythonPlugin *plugin,
                                     PyObject *func)
{
	GPluginPythonPluginPrivate *priv = NULL;

	g_return_if_fail(GPLUGIN_IS_PYTHON_PLUGIN(plugin));
	g_return_if_fail(func != NULL);

	priv = gplugin_python_plugin_get_instance_private(plugin);

	if(priv->unload)
		Py_DECREF(priv->unload);

	priv->unload = func;
	if(priv->unload)
		Py_INCREF(priv->unload);
}

/******************************************************************************
 * Object Stuff
 *****************************************************************************/
static void
gplugin_python_plugin_get_property(GObject *obj, guint param_id, GValue *value,
                                   GParamSpec *pspec)
{
	GPluginPythonPlugin *plugin = GPLUGIN_PYTHON_PLUGIN(obj);
	GPluginPythonPluginPrivate *priv = gplugin_python_plugin_get_instance_private(plugin);

	switch(param_id) {
		case PROP_MODULE:
			g_value_set_pointer(value,
			                    gplugin_python_plugin_get_module(plugin));
			break;
		case PROP_LOAD_FUNC:
			g_value_set_pointer(value,
			                    gplugin_python_plugin_get_load_func(plugin));
			break;
		case PROP_UNLOAD_FUNC:
			g_value_set_pointer(value,
			                    gplugin_python_plugin_get_unload_func(plugin));
			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_python_plugin_set_property(GObject *obj, guint param_id,
                                   const GValue *value, GParamSpec *pspec)
{
	GPluginPythonPlugin *plugin = GPLUGIN_PYTHON_PLUGIN(obj);
	GPluginPythonPluginPrivate *priv = gplugin_python_plugin_get_instance_private(plugin);

	switch(param_id) {
		case PROP_MODULE:
			gplugin_python_plugin_set_module(plugin,
			                                 g_value_get_pointer(value));
			break;
		case PROP_LOAD_FUNC:
			gplugin_python_plugin_set_load_func(plugin,
			                                    g_value_get_pointer(value));
			break;
		case PROP_UNLOAD_FUNC:
			gplugin_python_plugin_set_unload_func(plugin,
			                                      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_python_plugin_finalize(GObject *obj) {
	GPluginPythonPluginPrivate *priv = gplugin_python_plugin_get_instance_private(GPLUGIN_PYTHON_PLUGIN(obj));

	if(priv->module)
		Py_DECREF(priv->module);
	if(priv->load)
		Py_DECREF(priv->load);
	if(priv->unload)
		Py_DECREF(priv->unload);

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

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

static void
gplugin_python_plugin_init(GPluginPythonPlugin *plugin) {
}

static void
gplugin_python_plugin_class_finalize(GPluginPythonPluginClass *klass) {
}

static void
gplugin_python_plugin_class_init(GPluginPythonPluginClass *klass) {
	GObjectClass *obj_class = G_OBJECT_CLASS(klass);

	obj_class->get_property = gplugin_python_plugin_get_property;
	obj_class->set_property = gplugin_python_plugin_set_property;
	obj_class->finalize = gplugin_python_plugin_finalize;

	properties[PROP_MODULE] = g_param_spec_pointer(
		"module", "module",
		"The python module object",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	properties[PROP_LOAD_FUNC] = g_param_spec_pointer(
		"load-func", "load-func",
		"The python load function",
		G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY
	);

	properties[PROP_UNLOAD_FUNC] = g_param_spec_pointer(
		"unload-func", "unload-func",
		"The python 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
 *****************************************************************************/
void
gplugin_python_plugin_register(GPluginNativePlugin *native) {
	gplugin_python_plugin_register_type(G_TYPE_MODULE(native));
}
