anjuta-c-plugin-factory.c 7.32 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
/*
    anjuta-c-plugin-factory.c
    Copyright (C) 2007 Sebastien Granjoux  <seb.sfo@free.fr>

    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 2 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, write to the Free Software
    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
*/

/**
 * SECTION:anjuta-c-plugin-factory
23 24 25
 * @title: AnjutaCPluginFactory
 * @short_description: Anjuta C plugin factory
 * @see_also: #AnjutaCModule, #AnjutaPluginManager
26 27
 * @stability: Unstable
 * @include: libanjuta/anjuta-c-plugin-factory.h
28 29 30 31 32 33 34 35 36 37 38 39 40 41
 *
 * #AnjutaCPluginFactory implements the #IAnjutaPluginFactory interface. This
 * interface is used to create new plugin objects in Anjuta.
 *
 * This plugin factory creates new plugin objects which have a source code
 * written in C. This factory is always available in Anjuta. Other plugin
 * factories can be implemented as Anjuta plugins.
 *
 * This plugin factory uses the GLib dynamic type support implemented in 
 * #AnjutaCModule object to allow loading and unloading of plugins code. But
 * if the plugins itself can be unloaded, the #AnjutaCModule object must stay.
 * If the plugin is needed later, it must be registed with the same
 * module object. The factory take care of this and of creating the plugin
 * object itself.
42 43 44 45 46 47 48 49 50 51 52 53 54 55
 */

#include "config.h"

#include "anjuta-c-plugin-factory.h"

#include "anjuta-c-module.h"

#include <libanjuta/interfaces/ianjuta-plugin-factory.h>

#include <glib.h>

#include <string.h>

56 57 58
/* Types
 *---------------------------------------------------------------------------*/

59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103
struct _AnjutaCPluginFactoryClass
{
	GObjectClass parent;
};

struct _AnjutaCPluginFactory
{
	GObject parent;

	GHashTable* loaded_plugins;
};

static void ianjuta_c_plugin_factory_iface_init (IAnjutaPluginFactoryIface *iface);
G_DEFINE_TYPE_WITH_CODE (AnjutaCPluginFactory, anjuta_c_plugin_factory, G_TYPE_OBJECT,	\
G_IMPLEMENT_INTERFACE (IANJUTA_TYPE_PLUGIN_FACTORY, ianjuta_c_plugin_factory_iface_init))

/* Private functions
 *---------------------------------------------------------------------------*/

static AnjutaPlugin*
anjuta_c_plugin_factory_create_plugin (AnjutaCPluginFactory *factory,
					AnjutaPluginHandle *handle,
					AnjutaShell *shell,
					GError **error)
{
	const gchar *path;
	GHashTable *plugin_in_path;
	gchar **pieces;
	AnjutaCModule *module;
	GType type;
	AnjutaPlugin *plugin;
	
	g_return_val_if_fail (handle != NULL, NULL);
	g_return_val_if_fail (shell != NULL, NULL);
	
	path = anjuta_plugin_handle_get_path (handle);
	plugin_in_path = g_hash_table_lookup (factory->loaded_plugins, path);
	if (plugin_in_path == NULL)
	{
		/* No loaded plugin with this path, create sub hash */
		plugin_in_path = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_object_unref);
		g_hash_table_insert (plugin_in_path, g_strdup (path), plugin_in_path);
	}
	
	pieces = g_strsplit (anjuta_plugin_handle_get_id (handle), ":", -1);
104
	if ((pieces == NULL) || (pieces[0] == NULL))
105
	{
106 107 108
		g_set_error (error, IANJUTA_PLUGIN_FACTORY_ERROR,
				 	IANJUTA_PLUGIN_FACTORY_MISSING_LOCATION,
				 	_("Missing location of plugin %s"), anjuta_plugin_handle_get_name (handle));
109 110 111 112 113 114 115
		return NULL;
	}
	module = g_hash_table_lookup (plugin_in_path, pieces[0]);
	if (module == NULL)
	{
		/* Plugin is not loaded */
		module = anjuta_c_module_new (path, pieces[0]);
116 117 118
		g_return_val_if_fail (module != NULL, NULL);

		/* Load module */
119
		g_type_module_use (G_TYPE_MODULE (module));
120 121 122 123 124 125 126 127

		if (anjuta_c_module_get_last_error (module, error))
		{
			/* Avoid memory leak in case of error*/
			g_strfreev (pieces);
			
			return NULL;
		}
128 129 130 131 132 133 134 135 136 137
		g_hash_table_insert (plugin_in_path, g_strdup (pieces[0]), module);
	}
	else
	{
		module = NULL;
	}
	
	/* Find plugin type */
	if (pieces[1] == NULL)
	{
138 139 140 141
		g_strfreev (pieces);
		g_set_error (error, IANJUTA_PLUGIN_FACTORY_ERROR,
				 	IANJUTA_PLUGIN_FACTORY_MISSING_TYPE,
				 	_("Missing type defined by plugin %s"), anjuta_plugin_handle_get_name (handle));
142 143 144
		return NULL;
	}
	type = g_type_from_name (pieces[1]);
145 146 147 148 149 150 151 152 153
	if (type == G_TYPE_INVALID)
	{
		g_set_error (error, IANJUTA_PLUGIN_FACTORY_ERROR,
				 	IANJUTA_PLUGIN_FACTORY_INVALID_TYPE,
				 	_("plugin %s fails to register type %s"), anjuta_plugin_handle_get_name (handle), pieces[1]);
		g_strfreev (pieces);
		
		return NULL;
	}
154 155 156 157 158 159 160 161
	g_strfreev (pieces);
	
	/* Create plugin */
	plugin = (AnjutaPlugin *)g_object_new (type, "shell", shell, NULL);
	
	if ((module != NULL) && (anjuta_plugin_handle_get_resident(handle) == FALSE))
	{
		/* This module can be unloaded when not needed */
162
		g_type_module_unuse (G_TYPE_MODULE (module));
163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220
	}
	
	return plugin;
}

/* IAnjutaPluginFactory interface
 *---------------------------------------------------------------------------*/	

static AnjutaPlugin*
ianjuta_c_plugin_factory_new_plugin (IAnjutaPluginFactory *ifactory, AnjutaPluginHandle *handle, AnjutaShell *shell, GError **error)
{
	AnjutaCPluginFactory *factory = ANJUTA_C_PLUGIN_FACTORY (ifactory);

	return anjuta_c_plugin_factory_create_plugin (factory, handle, shell, error);
}

static void
ianjuta_c_plugin_factory_iface_init (IAnjutaPluginFactoryIface *iface)
{
	iface->new_plugin = ianjuta_c_plugin_factory_new_plugin;
}


/* GObject functions
 *---------------------------------------------------------------------------*/

/* dispose is the first destruction step. It is used to unref object created
 * with instance_init in order to break reference counting cycles. This
 * function could be called several times. All function should still work
 * after this call. It has to called its parents.*/

static void
anjuta_c_plugin_factory_dispose (GObject *object)
{
	AnjutaCPluginFactory *factory = ANJUTA_C_PLUGIN_FACTORY (object);
	
	g_hash_table_unref (factory->loaded_plugins);
	factory->loaded_plugins = NULL;

	G_OBJECT_CLASS (anjuta_c_plugin_factory_parent_class)->dispose (object);
}

static void
anjuta_c_plugin_factory_class_init (AnjutaCPluginFactoryClass *klass)
{
	GObjectClass *gobject_class = G_OBJECT_CLASS (klass);

	gobject_class->dispose = anjuta_c_plugin_factory_dispose;
}

static void
anjuta_c_plugin_factory_init (AnjutaCPluginFactory *factory)
{
	factory->loaded_plugins = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify)g_hash_table_unref);
}

/* Creation and Destruction
 *---------------------------------------------------------------------------*/
221 222 223 224 225 226 227
/**
 * anjuta_c_plugin_factory_new:
 *
 * Create a new #AnjutaCPluginFactory object.
 *
 * Return value: a new #AnjutaCPluginFactory object.
 */
228 229 230 231 232 233 234 235 236 237
AnjutaCPluginFactory*
anjuta_c_plugin_factory_new (void)
{
	AnjutaCPluginFactory *factory;

	factory = g_object_new (ANJUTA_TYPE_C_PLUGIN_FACTORY, NULL);
		
	return factory;
}

238 239 240 241 242 243
/**
 * anjuta_c_plugin_factory_free:
 * @factory: a #AnjutaCPluginFactory
 *
 * Delete a #AnjutaCPluginFactory object.
 */
244 245 246 247 248
void
anjuta_c_plugin_factory_free (AnjutaCPluginFactory *factory)
{
	g_object_unref (G_OBJECT(factory));
}