/*
 * Copyright (C) 2012 - Juan Ferrer Toribio
 *
 * 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 copy of the GNU General Public License
 * along with this program. If not, see <http://www.gnu.org/licenses/>.
 */

#include "vn-mod.h"

struct _VnModPrivate
{
	gchar * name;
	gchar * data_dir;
	gchar * text_domain;
	gchar * title;
	GModule * module;
	GActionEntry * actions;
};

G_DEFINE_TYPE (VnMod, vn_mod, G_TYPE_OBJECT);

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Public

/**
 * vn_mod_activate: (virtual activate)
 * @obj: the #VnMod
 * 
 * Activates the module.
 **/
void vn_mod_activate (VnMod * obj)
{
	g_return_if_fail (VN_IS_MOD (obj));

	VN_MOD_GET_CLASS (obj)->activate (obj);
}

const gchar * vn_mod_get_name (VnMod * obj)
{
	g_return_val_if_fail (VN_IS_MOD (obj), NULL);
	
	return obj->priv->name;
}

const gchar * vn_mod_get_text_domain (VnMod * obj)
{
	g_return_val_if_fail (VN_IS_MOD (obj), NULL);
	
	return obj->priv->text_domain;
}

const gchar * vn_mod_get_data_dir (VnMod * obj)
{
	g_return_val_if_fail (VN_IS_MOD (obj), NULL);
	
	return obj->priv->data_dir;
}

const gchar * vn_mod_get_title (VnMod * obj)
{
	g_return_val_if_fail (VN_IS_MOD (obj), NULL);
	
	return obj->priv->title;
}

/**
 * vn_mod_get_menu_model:
 * @obj: a #VnMod
 * 
 * Gets a menu to open the forms of the module.
 * 
 * Returns: (transfer full): a #GMenuModel
 **/
GMenuModel * vn_mod_get_menu_model (VnMod * obj)
{
	gchar * menu_file;
	GMenuModel * menu = NULL;
	GtkBuilder * builder;
	GError * err = NULL;

	g_return_val_if_fail (VN_IS_MOD (obj), NULL);

	menu_file = g_strdup_printf ("%s/%s-menu.glade",
		obj->priv->data_dir, obj->priv->name);
	builder = gtk_builder_new ();

	if (gtk_builder_add_from_file (builder, menu_file, &err))
		menu = G_MENU_MODEL (gtk_builder_get_object (builder, "menu"));
	else if (err)
	{
		if (err->code != G_FILE_ERROR_NOENT)
			g_warning ("%s", err->message);

		g_clear_error (&err);
	}

	g_free (menu_file);
	g_object_unref (builder);
	return menu;
}

/**
 * vn_mod_set_action_entries:
 * @obj: a #VnMod
 * @actions: a #GActionEntry array
 * 
 * Sets the actions for the menu displayed for @obj.
 **/
void vn_mod_set_action_entries (VnMod * obj, GActionEntry * actions)
{
	g_return_if_fail (VN_IS_MOD (obj));

	obj->priv->actions = actions;
}

/**
 * vn_mod_get_action_entries:
 * @obj: a #VnMod
 * 
 * Returns the actions for the menu displayed for @obj.
 * 
 * Return value:(transfer none): a #GActionEntry
 **/
const GActionEntry * vn_mod_get_action_entries (VnMod * obj)
{
	g_return_if_fail (VN_IS_MOD (obj));

	return obj->priv->actions;
}

static void vn_mod_free_action_entry (VnMod * obj)
{
	gint i;
	GActionEntry * actions = obj->priv->actions;

	if (actions)
	for (i = 0; actions[i].name != NULL; i++)
	{
		g_free ((gchar *) actions[i].name);
		g_free ((gchar *) actions[i].state);
	}

	g_free (actions);
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Properties

enum
{
	 PROP_NAME = 1
	,PROP_DATA_DIR
	,PROP_MODULE
	,PROP_TEXT_DOMAIN
	,PROP_TITLE
};

static void vn_mod_set_property (VnMod * obj, guint id,
	const GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_NAME:
			obj->priv->name = g_value_dup_string (value);
			
			//XXX Remove when the "example" mod gets removed from the project
			if (g_strcmp0 (obj->priv->name, "example"))
				obj->priv->text_domain = g_strdup_printf ("%s-%s", GETTEXT_PACKAGE, obj->priv->name);
			else
				obj->priv->text_domain = g_strdup (GETTEXT_PACKAGE);

			bindtextdomain (obj->priv->text_domain, _HEDERA_LOCALE_DIR);
			break;
		case PROP_DATA_DIR:
			obj->priv->data_dir = g_value_dup_string (value);
			break;
		case PROP_MODULE:
			obj->priv->module = g_value_get_pointer (value);
			break;
		case PROP_TITLE:
			obj->priv->title = g_value_dup_string (value);
			break;
		default:		
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);		
	}
}

static void vn_mod_get_property (VnMod * obj, guint id,
	GValue * value, GParamSpec * pspec)
{
	switch (id)
	{
		case PROP_NAME:
			g_value_set_string (value, obj->priv->name);
			break;
		case PROP_DATA_DIR:
			g_value_set_string (value, obj->priv->data_dir);
			break;
		case PROP_MODULE:
			g_value_set_pointer (value, obj->priv->module);
			break;
		case PROP_TEXT_DOMAIN:
			g_value_set_string (value, obj->priv->text_domain);
			break;
		case PROP_TITLE:
			g_value_set_string (value, obj->priv->title);
			break;
		default:
			G_OBJECT_WARN_INVALID_PROPERTY_ID (obj, id, pspec);		
	}
}

//+++++++++++++++++++++++++++++++++++++++++++++++++++ Class

static void vn_mod_init (VnMod * obj)
{
	obj->priv = G_TYPE_INSTANCE_GET_PRIVATE (obj, VN_TYPE_MOD, VnModPrivate);
	obj->priv->name = NULL;
	obj->priv->data_dir = NULL;
	obj->priv->module = NULL;
	obj->priv->text_domain = NULL;
	obj->priv->actions = NULL;
}

static void vn_mod_finalize (VnMod * obj)
{
	g_free (obj->priv->name);
	g_free (obj->priv->data_dir);
	g_free (obj->priv->text_domain);
	vn_mod_free_action_entry (obj);
	G_OBJECT_CLASS (vn_mod_parent_class)->finalize (G_OBJECT (obj));
}

static void vn_mod_class_init (VnModClass * k)
{
	GObjectClass * klass = G_OBJECT_CLASS (k);
	klass->finalize = (GObjectFinalizeFunc) vn_mod_finalize;
	klass->set_property = (GObjectSetPropertyFunc) vn_mod_set_property;
	klass->get_property = (GObjectGetPropertyFunc) vn_mod_get_property;
	g_type_class_add_private (klass, sizeof (VnModPrivate));

	g_object_class_install_property (klass, PROP_NAME,
		g_param_spec_string ("name"
			,"Name"
			,"The module name"
			,NULL
			,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
	));
	g_object_class_install_property (klass, PROP_DATA_DIR,
		g_param_spec_string ("data-dir"
			,"Data directory"
			,"The directory in which the data of the module is allocated"
			,NULL
			,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
	));
	g_object_class_install_property (klass, PROP_MODULE,
		g_param_spec_pointer ("module"
			,"Module"
			,"The GModule"
			,G_PARAM_CONSTRUCT_ONLY | G_PARAM_WRITABLE
	));
	g_object_class_install_property (klass, PROP_TEXT_DOMAIN,
		g_param_spec_string ("text-domain"
			,"Text domain"
			,"The domain in wich the module is"
			,NULL
			,G_PARAM_READABLE
	));
	g_object_class_install_property (klass, PROP_TITLE,
		g_param_spec_string ("title"
			,"Title"
			,"The module title"
			,NULL
			,G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE
	));
}