plugin-loader.c 12.5 KB
Newer Older
1 2 3 4 5 6
/*
 * plugin-loader.c: Base class for plugin loaders.
 *
 * Author: Zbigniew Chyla (cyba@gnome.pl)
 */

7
#include <gnumeric-config.h>
8
#include "gnumeric.h"
9 10
#include "plugin-loader.h"

11 12
#include "plugin.h"
#include "plugin-service.h"
13 14 15

#include <libgnome/libgnome.h>
#include <gal/util/e-util.h>
16 17

#define PARENT_TYPE (gtk_object_get_type ())
18
#define PL_GET_CLASS(loader)	GNUMERIC_PLUGIN_LOADER_CLASS (G_OBJECT_GET_CLASS (loader))
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44

void gnumeric_plugin_loader_unload_service_general_real (GnumericPluginLoader *loader,
                                                         PluginService *service,
                                                         ErrorInfo **ret_error);
void gnumeric_plugin_loader_unload_service_file_opener_real (GnumericPluginLoader *loader,
                                                             PluginService *service,
                                                             ErrorInfo **ret_error);
void gnumeric_plugin_loader_unload_service_file_saver_real (GnumericPluginLoader *loader,
                                                            PluginService *service,
                                                            ErrorInfo **ret_error);
void gnumeric_plugin_loader_unload_service_function_group_real (GnumericPluginLoader *loader,
                                                                PluginService *service,
                                                                ErrorInfo **ret_error);
void gnumeric_plugin_loader_unload_service_plugin_loader_real (GnumericPluginLoader *loader,
                                                               PluginService *service,
                                                               ErrorInfo **ret_error);

static GtkObjectClass *parent_class = NULL;

static void
gnumeric_plugin_loader_init (GnumericPluginLoader *loader)
{
	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));

	loader->plugin = NULL;
	loader->is_loaded = FALSE;
45
	loader->n_loaded_services = 0;
46 47 48 49 50 51 52 53 54 55 56 57 58 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 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174
}

static void
gnumeric_plugin_loader_destroy (GtkObject *obj)
{
	GnumericPluginLoader *loader;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (obj));

	loader = GNUMERIC_PLUGIN_LOADER (obj);

	GTK_OBJECT_CLASS (parent_class)->destroy (obj);
}

void
gnumeric_plugin_loader_unload_service_general_real (GnumericPluginLoader *loader,
                                                    PluginService *service,
                                                    ErrorInfo **ret_error)
{
	PluginServiceGeneral *service_general;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	service_general = &service->t.general;
	service_general->plugin_func_init = NULL;
	service_general->plugin_func_can_deactivate = NULL;
	service_general->plugin_func_cleanup = NULL;
}

void
gnumeric_plugin_loader_unload_service_file_opener_real (GnumericPluginLoader *loader,
                                                        PluginService *service,
                                                        ErrorInfo **ret_error)
{
	PluginServiceFileOpener *service_file_opener;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	service_file_opener = &service->t.file_opener;
	service_file_opener->plugin_func_file_probe = NULL;
	service_file_opener->plugin_func_file_open = NULL;
}

void
gnumeric_plugin_loader_unload_service_file_saver_real (GnumericPluginLoader *loader,
                                                       PluginService *service,
                                                       ErrorInfo **ret_error)
{
	PluginServiceFileSaver *service_file_saver;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	service_file_saver = &service->t.file_saver;
	service_file_saver->plugin_func_file_save = NULL;
}

void
gnumeric_plugin_loader_unload_service_function_group_real (GnumericPluginLoader *loader,
                                                           PluginService *service,
                                                           ErrorInfo **ret_error)
{
	PluginServiceFunctionGroup *service_function_group;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	service_function_group = &service->t.function_group;
	service_function_group->plugin_func_get_full_function_info = NULL;
}

void gnumeric_plugin_loader_unload_service_plugin_loader_real (GnumericPluginLoader *loader,
                                                               PluginService *service,
                                                               ErrorInfo **ret_error)
{
	PluginServicePluginLoader *service_plugin_loader;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	service_plugin_loader = &service->t.plugin_loader;
	service_plugin_loader->plugin_func_get_loader_type = NULL;
}

static void
gnumeric_plugin_loader_class_init (GnumericPluginLoaderClass *klass)
{
	klass->set_attributes = NULL;
	klass->load = NULL;
	klass->unload = NULL;
	klass->load_service_general = NULL;
	klass->unload_service_general = gnumeric_plugin_loader_unload_service_general_real;
	klass->load_service_file_opener = NULL;
	klass->unload_service_file_opener = gnumeric_plugin_loader_unload_service_file_opener_real;
	klass->load_service_file_saver = NULL;
	klass->unload_service_file_saver = gnumeric_plugin_loader_unload_service_file_saver_real;
	klass->load_service_function_group = NULL;
	klass->unload_service_function_group = gnumeric_plugin_loader_unload_service_function_group_real;
	klass->load_service_plugin_loader = NULL;
	klass->unload_service_plugin_loader = gnumeric_plugin_loader_unload_service_plugin_loader_real;
	klass->get_extra_info_list = NULL;

	GTK_OBJECT_CLASS (klass)->destroy = gnumeric_plugin_loader_destroy;
}

E_MAKE_TYPE (gnumeric_plugin_loader, "GnumericPluginLoader", GnumericPluginLoader, &gnumeric_plugin_loader_class_init, gnumeric_plugin_loader_init, PARENT_TYPE)

void
gnumeric_plugin_loader_set_attributes (GnumericPluginLoader *loader,
                                       GList *attr_names, GList *attr_values,
                                       ErrorInfo **ret_error)
{
	ErrorInfo *error;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (ret_error != NULL);

175
	PL_GET_CLASS(loader)->set_attributes (loader, attr_names, attr_values, &error);
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
	*ret_error = error;
}

void
gnumeric_plugin_loader_set_plugin (GnumericPluginLoader *loader, PluginInfo *plugin)
{
	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (plugin != NULL);

	loader->plugin = plugin;
}

void
gnumeric_plugin_loader_load (GnumericPluginLoader *loader, ErrorInfo **ret_error)
{
	GnumericPluginLoaderClass *gnumeric_plugin_loader_class;
	ErrorInfo *error;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	if (loader->is_loaded) {
		return;
	}
201
	plugin_load_dependencies (loader->plugin, &error);
202
	if (error == NULL) {
203
		gnumeric_plugin_loader_class = PL_GET_CLASS(loader);
204 205 206 207 208
		g_return_if_fail (gnumeric_plugin_loader_class->load != NULL);
		gnumeric_plugin_loader_class->load (loader, &error);
		if (error == NULL) {
			loader->is_loaded = TRUE;
			plugin_dependencies_inc_dependants (loader->plugin, DEPENDENCY_LOAD);
209
			plugin_message (0, "Loaded plugin \"%s\".\n", plugin_info_peek_id (loader->plugin));
210 211 212
		} else {
			*ret_error = error;
		}
213
	} else {
214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232
		*ret_error = error_info_new_str_with_details (
		             _("Error while loading plugin dependencies."),
		             error);
	}
}

void
gnumeric_plugin_loader_unload (GnumericPluginLoader *loader, ErrorInfo **ret_error)
{
	GnumericPluginLoaderClass *gnumeric_plugin_loader_class;
	ErrorInfo *error;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
	if (!loader->is_loaded) {
		return;
	}
233
	gnumeric_plugin_loader_class = PL_GET_CLASS(loader);
234 235 236 237 238
	if (gnumeric_plugin_loader_class->unload != NULL) {
		gnumeric_plugin_loader_class->unload (loader, &error);
		if (error == NULL) {
			loader->is_loaded = FALSE;
			plugin_dependencies_dec_dependants (loader->plugin, DEPENDENCY_LOAD);
239
			plugin_message (0, "Unloaded plugin \"%s\".\n", plugin_info_peek_id (loader->plugin));
240 241 242
		} else {
			*ret_error = error;
		}
243 244 245 246 247 248 249 250 251 252 253 254 255 256
	}
}

void
gnumeric_plugin_loader_load_service (GnumericPluginLoader *loader, PluginService *service, ErrorInfo **ret_error)
{
	GnumericPluginLoaderClass *gnumeric_plugin_loader_class;
	ErrorInfo *error;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
257
	gnumeric_plugin_loader_class = PL_GET_CLASS(loader);
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280
	gnumeric_plugin_loader_load (loader, &error);
	if (error == NULL) {
		void (*load_service_method) (GnumericPluginLoader *, PluginService *, ErrorInfo **) = NULL;

		switch (service->service_type) {
		case PLUGIN_SERVICE_GENERAL:
			load_service_method = gnumeric_plugin_loader_class->load_service_general;
			break;
		case PLUGIN_SERVICE_FILE_OPENER:
			load_service_method = gnumeric_plugin_loader_class->load_service_file_opener;
			break;
		case PLUGIN_SERVICE_FILE_SAVER:
			load_service_method = gnumeric_plugin_loader_class->load_service_file_saver;
			break;
		case PLUGIN_SERVICE_FUNCTION_GROUP:
			load_service_method = gnumeric_plugin_loader_class->load_service_function_group;
			break;
		case PLUGIN_SERVICE_PLUGIN_LOADER:
			load_service_method = gnumeric_plugin_loader_class->load_service_plugin_loader;
			break;
		default:
			g_assert_not_reached ();
		}
281 282 283 284 285 286 287
		if (load_service_method != NULL) {
			load_service_method (loader, service, &error);
			*ret_error = error;
		} else {
			*ret_error = error_info_new_str (
			             _("Service not supported by loader."));
		}
288 289 290 291 292
	} else {
		*ret_error = error_info_new_str_with_details (
		             _("Error while loading plugin."),
		             error);
	}
293 294 295 296

	if (*ret_error == NULL) {
		loader->n_loaded_services++; 
	}
297 298 299 300 301 302 303 304 305 306 307 308 309 310
}

void
gnumeric_plugin_loader_unload_service (GnumericPluginLoader *loader, PluginService *service, ErrorInfo **ret_error)
{
	GnumericPluginLoaderClass *gnumeric_plugin_loader_class;
	void (*unload_service_method) (GnumericPluginLoader *, PluginService *, ErrorInfo **) = NULL;
	ErrorInfo *error;

	g_return_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader));
	g_return_if_fail (service != NULL);
	g_return_if_fail (ret_error != NULL);

	*ret_error = NULL;
311
	gnumeric_plugin_loader_class = PL_GET_CLASS(loader);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331

	switch (service->service_type) {
	case PLUGIN_SERVICE_GENERAL:
		unload_service_method = gnumeric_plugin_loader_class->unload_service_general;
		break;
	case PLUGIN_SERVICE_FILE_OPENER:
		unload_service_method = gnumeric_plugin_loader_class->unload_service_file_opener;
		break;
	case PLUGIN_SERVICE_FILE_SAVER:
		unload_service_method = gnumeric_plugin_loader_class->unload_service_file_saver;
		break;
	case PLUGIN_SERVICE_FUNCTION_GROUP:
		unload_service_method = gnumeric_plugin_loader_class->unload_service_function_group;
		break;
	case PLUGIN_SERVICE_PLUGIN_LOADER:
		unload_service_method = gnumeric_plugin_loader_class->unload_service_plugin_loader;
		break;
	default:
		g_assert_not_reached ();
	}
332 333 334 335 336 337 338
	if (unload_service_method != NULL) {
		unload_service_method (loader, service, &error);
		g_free (plugin_service_get_loader_data (service));
	} else {
		*ret_error = error_info_new_str (
		             _("Service not supported by loader."));
	}
339 340 341 342 343 344 345 346 347 348 349

	if (error == NULL) {
		g_return_if_fail (loader->n_loaded_services > 0);
		loader->n_loaded_services--;
		if (loader->n_loaded_services == 0) {
			gnumeric_plugin_loader_unload (loader, &error);
			error_info_free (error);
		}
	} else {
		*ret_error = error;
	}
350 351 352 353 354 355 356 357 358 359
}

gint
gnumeric_plugin_loader_get_extra_info_list (GnumericPluginLoader *loader, GList **ret_keys_list, GList **ret_values_list)
{
	GnumericPluginLoaderClass *gnumeric_plugin_loader_class;

	g_return_val_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader), 0);
	g_return_val_if_fail (ret_keys_list != NULL && ret_values_list != NULL, 0);

360
	gnumeric_plugin_loader_class = PL_GET_CLASS(loader);
361 362 363 364 365 366
	if (gnumeric_plugin_loader_class->get_extra_info_list != NULL) {
		return gnumeric_plugin_loader_class->get_extra_info_list (loader, ret_keys_list, ret_values_list);
	} else {
		return 0;
	}
}
367 368 369 370 371 372 373 374

gboolean
gnumeric_plugin_loader_is_loaded (GnumericPluginLoader *loader)
{
	g_return_val_if_fail (IS_GNUMERIC_PLUGIN_LOADER (loader), FALSE);

	return loader->is_loaded;
}