plugin-service.c 48.1 KB
Newer Older
1
/* vim: set sw=8: -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
2 3 4 5 6 7 8
/*
 * plugin-service.c: Plugin services - reading XML info, activating, etc.
 *                   (everything independent of plugin loading method)
 *
 * Author: Zbigniew Chyla (cyba@gnome.pl)
 */

9
#include <gnumeric-config.h>
Zbigniew Chyla's avatar
Zbigniew Chyla committed
10
#include <gnumeric-i18n.h>
11
#include "gnumeric.h"
12 13
#include "plugin-service.h"

14
#include "gutils.h"
15 16 17 18 19
#include "workbook.h"
#include "workbook-view.h"
#include "func.h"
#include "io-context.h"
#include "error-info.h"
20
#include "file.h"
21 22 23
#ifdef WITH_BONOBO
#include <bonobo/bonobo-stream.h>
#endif
24
#include "file-priv.h"
25
#include "plugin.h"
26 27

#include <fnmatch.h>
Jody Goldberg's avatar
Jody Goldberg committed
28
#include <gsf/gsf-input.h>
Jody Goldberg's avatar
Jody Goldberg committed
29
#include <libxml/tree.h>
30
#include <libxml/globals.h>
Jody Goldberg's avatar
Jody Goldberg committed
31 32
#include <gsf/gsf-impl-utils.h>

Zbigniew Chyla's avatar
Zbigniew Chyla committed
33
#include <bonobo/bonobo-ui-node.h>
Jody Goldberg's avatar
Jody Goldberg committed
34
#include <gal/util/e-xml-utils.h>
35

Jody Goldberg's avatar
Jody Goldberg committed
36 37
#include <string.h>

38 39 40 41 42 43 44 45 46 47 48

static void plugin_service_load (PluginService *service, ErrorInfo **ret_error);
static void plugin_service_unload (PluginService *service, ErrorInfo **ret_error);

static FileFormatLevel
parse_format_level_str (const gchar *format_level_str, FileFormatLevel def)
{
	FileFormatLevel	format_level;

	if (format_level_str == NULL) {
		format_level = def;
49
	} else if (g_ascii_strcasecmp (format_level_str, "none") == 0) {
50
		format_level = FILE_FL_NONE;
51
	} else if (g_ascii_strcasecmp (format_level_str, "write_only") == 0) {
52
		format_level = FILE_FL_WRITE_ONLY;
53
	} else if (g_ascii_strcasecmp (format_level_str, "new") == 0) {
54
		format_level = FILE_FL_NEW;
55
	} else if (g_ascii_strcasecmp (format_level_str, "manual") == 0) {
56
		format_level = FILE_FL_MANUAL;
57
	} else if (g_ascii_strcasecmp (format_level_str, "manual_remember") == 0) {
58
		format_level = FILE_FL_MANUAL_REMEMBER;
59
	} else if (g_ascii_strcasecmp (format_level_str, "auto") == 0) {
Chyla Zbigniew's avatar
Chyla Zbigniew committed
60
		format_level = FILE_FL_AUTO;
61 62 63 64 65 66 67
	} else {
		format_level = def;
	}

	return format_level;
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static GHashTable *
get_plugin_file_savers_hash (GnmPlugin *plugin)
{
	GHashTable *hash;

	hash = g_object_get_data (G_OBJECT (plugin), "file_savers_hash");
	if (hash == NULL) {
		hash = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
		g_object_set_data_full (
			G_OBJECT (plugin), "file_savers_hash",
			hash, (GDestroyNotify) g_hash_table_destroy);
	}

	return hash;
}

84
/*
85
 * PluginService
86 87
 */

88 89 90 91 92 93 94 95 96
#define GPS_CLASS(klass) (G_TYPE_CHECK_CLASS_CAST ((klass), GNM_PLUGIN_SERVICE_TYPE, PluginServiceClass))
#define GPS_GET_CLASS(o) GPS_CLASS (G_OBJECT_GET_CLASS (o))

typedef struct{
	GObjectClass g_object_class;

	void (*read_xml) (PluginService *service, xmlNode *tree, ErrorInfo **ret_error);
	void (*activate) (PluginService *service, ErrorInfo **ret_error);
	void (*deactivate) (PluginService *service, ErrorInfo **ret_error);
97
	char *(*get_description) (PluginService *service);
98 99 100 101 102 103 104 105 106 107 108 109
} PluginServiceClass;

struct _PluginService {
	GObject   g_object;

	char   *id;
	GnmPlugin *plugin;
	gboolean is_loaded;

	/* protected */
	gpointer cbs_ptr;
	gboolean is_active;
110 111 112

	/* private */
	char *saved_description;
113 114 115 116 117
};


static void
plugin_service_init (GObject *obj)
118
{
119
	PluginService *service = GNM_PLUGIN_SERVICE (obj);
120

121 122 123 124 125
	service->id = NULL;
	service->is_active = FALSE;
	service->is_loaded = FALSE;
	service->plugin = NULL;
	service->cbs_ptr = NULL;
126
	service->saved_description = NULL;
127 128 129 130 131 132 133
}

static void
plugin_service_finalize (GObject *obj)
{
	PluginService *service = GNM_PLUGIN_SERVICE (obj);
	GObjectClass *parent_class;
134

135 136
	g_free (service->id);
	service->id = NULL;
137 138
	g_free (service->saved_description);
	service->saved_description = NULL;
139

140 141
	parent_class = g_type_class_peek (G_TYPE_OBJECT);
	parent_class->finalize (obj);
142 143 144
}

static void
145
plugin_service_class_init (GObjectClass *gobject_class)
146
{
147 148 149 150 151 152
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);

	gobject_class->finalize = plugin_service_finalize;
	plugin_service_class->read_xml = NULL;
	plugin_service_class->activate = NULL;
	plugin_service_class->deactivate = NULL;
153
	plugin_service_class->get_description = NULL;
154 155
}

156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173
GSF_CLASS (PluginService, plugin_service, plugin_service_class_init, plugin_service_init,
           G_TYPE_OBJECT)


/*
 * PluginServiceGeneral
 */

typedef struct{
	PluginServiceClass plugin_service_class;
} PluginServiceGeneralClass;

struct _PluginServiceGeneral {
	PluginService plugin_service;
	PluginServiceGeneralCallbacks cbs;
};


174
static void
175
plugin_service_general_init (GObject *obj)
176
{
177
	PluginServiceGeneral *service_general = GNM_PLUGIN_SERVICE_GENERAL (obj);
178

179 180 181 182
	GNM_PLUGIN_SERVICE (obj)->cbs_ptr = &service_general->cbs;
	service_general->cbs.plugin_func_init = NULL;
	service_general->cbs.plugin_func_cleanup = NULL;
}
183

184 185 186 187
static void
plugin_service_general_activate (PluginService *service, ErrorInfo **ret_error)
{
	PluginServiceGeneral *service_general = GNM_PLUGIN_SERVICE_GENERAL (service);
188
	ErrorInfo *error = NULL;
189

190 191 192
	GNM_INIT_RET_ERROR_INFO (ret_error);
	plugin_service_load (service, &error);
	if (error != NULL) {
193 194 195
		*ret_error = error_info_new_str_with_details (
		             _("Error while loading plugin service."),
		             error);
196 197 198 199 200 201 202 203 204
		return;
	}
	g_return_if_fail (service_general->cbs.plugin_func_init != NULL);
	service_general->cbs.plugin_func_init (service, &error);
	if (error != NULL) {
		*ret_error = error_info_new_str_with_details (
		             _("Initializing function inside plugin returned error."),
		             error);
		return;
205
	}
206
	service->is_active = TRUE;
207 208 209
}

static void
210
plugin_service_general_deactivate (PluginService *service, ErrorInfo **ret_error)
211
{
212
	PluginServiceGeneral *service_general = GNM_PLUGIN_SERVICE_GENERAL (service);
213
	ErrorInfo *error = NULL;
214

215 216 217
	GNM_INIT_RET_ERROR_INFO (ret_error);
	g_return_if_fail (service_general->cbs.plugin_func_cleanup != NULL);
	service_general->cbs.plugin_func_cleanup (service, &error);
218 219 220 221
	if (error != NULL) {
		*ret_error = error_info_new_str_with_details (
		             _("Cleanup function inside plugin returned error."),
		             error);
222
		return;
223
	}
224 225 226
	service->is_active = FALSE;
}

227 228 229 230 231 232
static char *
plugin_service_general_get_description (PluginService *service)
{
	return g_strdup (_("General"));
}

233 234 235 236 237 238 239
static void
plugin_service_general_class_init (GObjectClass *gobject_class)
{
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);

	plugin_service_class->activate = plugin_service_general_activate;
	plugin_service_class->deactivate = plugin_service_general_deactivate;
240
	plugin_service_class->get_description = plugin_service_general_get_description;
241 242
}

243 244 245 246 247
GSF_CLASS (PluginServiceGeneral, plugin_service_general,
           plugin_service_general_class_init, plugin_service_general_init,
           GNM_PLUGIN_SERVICE_TYPE)


248
/*
249
 * PluginServiceFileOpener
250 251
 */

252
typedef struct _GnumPluginFileOpener GnumPluginFileOpener;
253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
static GnumPluginFileOpener *gnum_plugin_file_opener_new (PluginService *service);

typedef enum {FILE_PATTERN_SHELL, FILE_PATTERN_LAST} InputFilePatternType;

typedef struct {
	InputFilePatternType pattern_type;
	gboolean case_sensitive;
	gchar *value;
} InputFilePattern;

struct _InputFileSaveInfo {
	gchar *saver_id_str;
	FileFormatLevel format_level;
};

typedef struct _InputFileSaveInfo InputFileSaveInfo;


typedef struct{
	PluginServiceClass plugin_service_class;
} PluginServiceFileOpenerClass;

struct _PluginServiceFileOpener {
	PluginService plugin_service;

	gint priority;
	gboolean has_probe;
	gboolean can_open, can_import;
	gint   default_importer_priority;
	gchar *description;
	GSList *file_patterns;      /* list of InputFilePattern */
	InputFileSaveInfo *save_info;

	GnumFileOpener *opener;
	PluginServiceFileOpenerCallbacks cbs;
};


static void
plugin_service_file_opener_init (GObject *obj)
{
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (obj);

	GNM_PLUGIN_SERVICE (obj)->cbs_ptr = &service_file_opener->cbs;
	service_file_opener->description = NULL;
	service_file_opener->file_patterns = NULL;
	service_file_opener->save_info = NULL;
	service_file_opener->opener = NULL;
	service_file_opener->cbs.plugin_func_file_probe = NULL;
	service_file_opener->cbs.plugin_func_file_open = NULL;
}

static void
input_file_pattern_free (gpointer data)
{
	InputFilePattern *pattern = data;

	g_free (pattern->value);
	g_free (pattern);
}

static void
input_file_saver_info_free (InputFileSaveInfo *saver_info)
{
	g_free (saver_info->saver_id_str);
	g_free (saver_info);
}

static void
plugin_service_file_opener_finalize (GObject *obj)
{
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (obj);
	GObjectClass *parent_class;

	g_free (service_file_opener->description);
	service_file_opener->description = NULL;
	g_slist_free_custom (service_file_opener->file_patterns, input_file_pattern_free);
	service_file_opener->file_patterns = NULL;
	if (service_file_opener->save_info != NULL) {
		input_file_saver_info_free (service_file_opener->save_info);
		service_file_opener->save_info = NULL;
	}
	if (service_file_opener->opener != NULL) {
		g_object_unref (service_file_opener->opener);
		service_file_opener->opener = NULL;
	}

	parent_class = g_type_class_peek (GNM_PLUGIN_SERVICE_TYPE);
	parent_class->finalize (obj);
}

static InputFileSaveInfo *
input_file_save_info_read (xmlNode *tree)
{
	InputFileSaveInfo *save_info = NULL;
	gchar *saver_id_str, *format_level_str;

	saver_id_str = e_xml_get_string_prop_by_name (tree, (xmlChar *)"saver_id");
	format_level_str = e_xml_get_string_prop_by_name (tree, (xmlChar *)"format_level");

	save_info = g_new (InputFileSaveInfo, 1);
	save_info->saver_id_str = saver_id_str != NULL ? saver_id_str : g_strdup ("");
	save_info->format_level = parse_format_level_str (format_level_str, FILE_FL_MANUAL);

	g_free (format_level_str);

	return save_info;
}

static void
plugin_service_file_opener_read_xml (PluginService *service, xmlNode *tree, ErrorInfo **ret_error)
{
	guint priority;
	gboolean has_probe, can_open, can_import;
	xmlNode *information_node;
	gchar *description;

	GNM_INIT_RET_ERROR_INFO (ret_error);
	priority = e_xml_get_uint_prop_by_name_with_default (tree, (xmlChar *)"priority", 50);
	priority = MIN (priority, (guint)100);
	has_probe = e_xml_get_bool_prop_by_name_with_default (tree, (xmlChar *)"probe", TRUE);
	can_open = e_xml_get_bool_prop_by_name_with_default (tree, (xmlChar *)"open", TRUE);
	can_import = e_xml_get_bool_prop_by_name_with_default (tree, (xmlChar *)"import", FALSE);
	information_node = e_xml_get_child_by_name (tree, (xmlChar *)"information");
	if (information_node != NULL) {
		xmlNode *node;
		xmlChar *val;

		node = e_xml_get_child_by_name_by_lang_list (
		       information_node, "description", NULL);
		if (node != NULL) {
			val = xmlNodeGetContent (node);
			description = g_strdup ((gchar *)val);
			xmlFree (val);
		} else {
			description = NULL;
		}
	} else {
		description = NULL;
	}
	if (description != NULL) {
		GSList *file_patterns = NULL;
		xmlNode *file_patterns_node, *save_info_node, *node;
		PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (service);
		InputFileSaveInfo *save_info;

		file_patterns_node = e_xml_get_child_by_name (tree, (xmlChar *)"file_patterns");
		if (file_patterns_node != NULL) {
			for (node = file_patterns_node->xmlChildrenNode; node != NULL; node = node->next) {
				InputFilePattern *file_pattern;
				gchar *value, *type_str;

				if (strcmp (node->name, "file_pattern") != 0 ||
				    (value = e_xml_get_string_prop_by_name (node, (xmlChar *)"value")) == NULL) {
					continue;
				}
				type_str = e_xml_get_string_prop_by_name (node, (xmlChar *)"type");
				file_pattern = g_new (InputFilePattern, 1);
				file_pattern->value = value;
				if (type_str == NULL) {
					file_pattern->pattern_type = FILE_PATTERN_SHELL;
414
				} else if (g_ascii_strcasecmp (type_str, "shell_pattern") == 0) {
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486
					file_pattern->pattern_type = FILE_PATTERN_SHELL;
					file_pattern->case_sensitive = e_xml_get_bool_prop_by_name_with_default (
					                               node, (xmlChar *)"case_sensitive", FALSE);
				} else {
					file_pattern->pattern_type = FILE_PATTERN_SHELL;
				}
				g_free (type_str);
				GNM_SLIST_PREPEND (file_patterns, file_pattern);
			}
		}
		GNM_SLIST_REVERSE (file_patterns);

		save_info_node = e_xml_get_child_by_name (tree, (xmlChar *)"save_info");
		if (save_info_node != NULL) {
			save_info = input_file_save_info_read (save_info_node);
		} else {
			save_info = NULL;
		}

		service_file_opener->priority = priority;
		service_file_opener->has_probe = has_probe;
		service_file_opener->can_open = can_open;
		service_file_opener->can_import = can_import;
		if (can_import) {
			service_file_opener->default_importer_priority =
			e_xml_get_integer_prop_by_name_with_default (tree, (xmlChar *)"default_importer_priority", -1);
		}
		service_file_opener->description = description;
		service_file_opener->file_patterns = file_patterns;
		service_file_opener->save_info = save_info;
	} else {
		*ret_error = error_info_new_str (_("File opener has no description"));
	}
}

static void
plugin_service_file_opener_activate (PluginService *service, ErrorInfo **ret_error)
{
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (service);

	GNM_INIT_RET_ERROR_INFO (ret_error);
	service_file_opener->opener = GNUM_FILE_OPENER (gnum_plugin_file_opener_new (service));
	if (service_file_opener->can_open) {
		register_file_opener (service_file_opener->opener,
		                      service_file_opener->priority);
	}
	if (service_file_opener->can_import) {
		if (service_file_opener->default_importer_priority < 0) {
			register_file_opener_as_importer (service_file_opener->opener);
		} else {
			register_file_opener_as_importer_as_default (service_file_opener->opener,
			                                             service_file_opener->default_importer_priority);
		}
	}
	service->is_active = TRUE;
}

static void
plugin_service_file_opener_deactivate (PluginService *service, ErrorInfo **ret_error)
{
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (service);

	GNM_INIT_RET_ERROR_INFO (ret_error);
	if (service_file_opener->can_open) {
		unregister_file_opener (service_file_opener->opener);
	}
	if (service_file_opener->can_import) {
		unregister_file_opener_as_importer (service_file_opener->opener);
	}
	service->is_active = FALSE;
}

487 488 489 490 491 492 493 494 495
static char *
plugin_service_file_opener_get_description (PluginService *service)
{
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (service);

	return g_strdup_printf (
		_("File opener - %s"), service_file_opener->description);
}

496 497 498 499 500 501 502 503 504
static void
plugin_service_file_opener_class_init (GObjectClass *gobject_class)
{
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);

	gobject_class->finalize = plugin_service_file_opener_finalize;
	plugin_service_class->read_xml = plugin_service_file_opener_read_xml;
	plugin_service_class->activate = plugin_service_file_opener_activate;
	plugin_service_class->deactivate = plugin_service_file_opener_deactivate;
505
	plugin_service_class->get_description = plugin_service_file_opener_get_description;
506 507 508 509 510 511 512 513
}

GSF_CLASS (PluginServiceFileOpener, plugin_service_file_opener,
           plugin_service_file_opener_class_init, plugin_service_file_opener_init,
           GNM_PLUGIN_SERVICE_TYPE)


/** GnumPluginFileOpener class **/
514 515

#define TYPE_GNUM_PLUGIN_FILE_OPENER             (gnum_plugin_file_opener_get_type ())
516 517 518
#define GNUM_PLUGIN_FILE_OPENER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GNUM_PLUGIN_FILE_OPENER, GnumPluginFileOpener))
#define GNUM_PLUGIN_FILE_OPENER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GNUM_PLUGIN_FILE_OPENER, GnumPluginFileOpenerClass))
#define IS_GNUM_PLUGIN_FILE_OPENER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GNUM_PLUGIN_FILE_OPENER))
519

520
GType gnum_plugin_file_opener_get_type (void);
521

522
typedef struct {
523
	GnumFileOpenerClass parent_class;
524
} GnumPluginFileOpenerClass;
525 526 527 528 529 530 531 532 533 534 535 536 537 538

struct _GnumPluginFileOpener {
	GnumFileOpener parent;

	PluginService *service;
};

static void
gnum_plugin_file_opener_init (GnumPluginFileOpener *fo)
{
	fo->service = NULL;
}

static gboolean
Jody Goldberg's avatar
Jody Goldberg committed
539
gnum_plugin_file_opener_probe (GnumFileOpener const *fo, GsfInput *input,
540
                               FileProbeLevel pl)
541
{
542 543
	GnumPluginFileOpener *pfo = GNUM_PLUGIN_FILE_OPENER (fo);
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (pfo->service);
544

Rodrigo Moya's avatar
Rodrigo Moya committed
545
	g_return_val_if_fail (GSF_IS_INPUT (input), FALSE);
546

547 548
	if (pl == FILE_PROBE_FILE_NAME && service_file_opener->file_patterns != NULL) {
		gboolean match = FALSE;
549
		GSList *l;
550
		gchar *base_file_name = (gchar *)gsf_input_name (input);
Jody Goldberg's avatar
Jody Goldberg committed
551 552

		if (base_file_name == NULL)
553 554
			return FALSE;
		base_file_name = g_path_get_basename (base_file_name);
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569

		for (l = service_file_opener->file_patterns; l != NULL && !match; l = l->next) {
			InputFilePattern *pattern = l->data;

			if (pattern->pattern_type == FILE_PATTERN_SHELL) {
				if (pattern->case_sensitive) {
					match = fnmatch (pattern->value, base_file_name, FNM_PATHNAME) == 0;
				} else {
					gchar *pattern_str, *name_str;

					pattern_str = g_alloca (strlen (pattern->value) + 1);
					name_str = g_alloca (strlen (base_file_name) + 1);
					g_strdown (strcpy (pattern_str, pattern->value));
					g_strdown (strcpy (name_str, base_file_name));
					match = fnmatch (pattern_str, name_str, FNM_PATHNAME) == 0;
570 571
				}
			} else {
572
				g_assert_not_reached ();
573
			}
574
		}
575

576
		g_free (base_file_name);
577
		return match;
578 579
	}

580
	if (service_file_opener->has_probe) {
581
		ErrorInfo *ignored_error = NULL;
582 583

		plugin_service_load (pfo->service, &ignored_error);
584
		if (ignored_error != NULL) {
585 586 587
			error_info_print (ignored_error);
			error_info_free (ignored_error);
			return FALSE;
588 589 590 591
		} else if (service_file_opener->cbs.plugin_func_file_probe == NULL) {
			return FALSE;
		} else {
			return service_file_opener->cbs.plugin_func_file_probe (fo, pfo->service, input, pl);
592 593
		}
	} else {
594
		return FALSE;
595 596 597 598 599
	}
}

static void
gnum_plugin_file_opener_open (GnumFileOpener const *fo, IOContext *io_context,
Jody Goldberg's avatar
Jody Goldberg committed
600
                              WorkbookView *wbv, GsfInput *input)
601 602

{
603 604
	GnumPluginFileOpener *pfo = GNUM_PLUGIN_FILE_OPENER (fo);
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (pfo->service);
605
	ErrorInfo *error = NULL;
606

Rodrigo Moya's avatar
Rodrigo Moya committed
607
	g_return_if_fail (GSF_IS_INPUT (input));
608 609 610

	plugin_service_load (pfo->service, &error);
	if (error == NULL) {
611 612
		g_return_if_fail (service_file_opener->cbs.plugin_func_file_open != NULL);
		service_file_opener->cbs.plugin_func_file_open (fo, pfo->service, io_context, wbv, input);
613 614 615 616 617 618
		if (!gnumeric_io_error_occurred (io_context)) {
			if (service_file_opener->save_info != NULL) {
				InputFileSaveInfo *save_info;

				save_info = service_file_opener->save_info;
				if (save_info->saver_id_str[0] == '\0') {
Jody Goldberg's avatar
Jody Goldberg committed
619
					workbook_set_saveinfo (wb_view_workbook (wbv), gsf_input_name (input),
620 621 622 623 624
					                       save_info->format_level, NULL);
				} else {
					GHashTable *file_savers_hash;
					GnumFileSaver *saver;

625
					file_savers_hash = get_plugin_file_savers_hash (pfo->service->plugin);
626 627 628
					saver = (GnumFileSaver *) g_hash_table_lookup (file_savers_hash,
					                                           save_info->saver_id_str);
					if (saver != NULL) {
Jody Goldberg's avatar
Jody Goldberg committed
629
						workbook_set_saveinfo (wb_view_workbook (wbv), gsf_input_name (input),
630 631 632 633 634 635 636
						                       save_info->format_level, saver);
					}
				}
			}
		}
	} else {
		gnumeric_io_error_info_set (io_context, error);
637 638
		gnumeric_io_error_push (io_context, error_info_new_str (
		                        _("Error while reading file.")));
639 640 641 642 643 644 645 646 647 648 649 650
	}
}

static void
gnum_plugin_file_opener_class_init (GnumPluginFileOpenerClass *klass)
{
	GnumFileOpenerClass *gnum_file_opener_klass = GNUM_FILE_OPENER_CLASS (klass);

	gnum_file_opener_klass->probe = gnum_plugin_file_opener_probe;
	gnum_file_opener_klass->open = gnum_plugin_file_opener_open;
}

Jody Goldberg's avatar
Jody Goldberg committed
651 652 653
GSF_CLASS (GnumPluginFileOpener, gnum_plugin_file_opener,
	   gnum_plugin_file_opener_class_init, gnum_plugin_file_opener_init,
	   TYPE_GNUM_FILE_OPENER)
654 655 656 657

static GnumPluginFileOpener *
gnum_plugin_file_opener_new (PluginService *service)
{
658
	PluginServiceFileOpener *service_file_opener = GNM_PLUGIN_SERVICE_FILE_OPENER (service);
659 660 661
	GnumPluginFileOpener *fo;
	gchar *opener_id;

662 663
	opener_id = g_strconcat (
		gnm_plugin_get_id (service->plugin), ":", service->id, NULL);
664
	fo = GNUM_PLUGIN_FILE_OPENER (g_object_new (TYPE_GNUM_PLUGIN_FILE_OPENER, NULL));
665 666 667 668 669 670 671 672 673
	gnum_file_opener_setup (GNUM_FILE_OPENER (fo), opener_id,
	                        service_file_opener->description,
	                        NULL, NULL);
	fo->service = service;
	g_free (opener_id);

	return fo;
}

674
/** -- **/
675 676


677 678 679
/*
 * PluginServiceFileSaver
 */
680

681 682
typedef struct _GnumPluginFileSaver GnumPluginFileSaver;
static GnumPluginFileSaver *gnum_plugin_file_saver_new (PluginService *service);
683 684


685 686 687
typedef struct{
	PluginServiceClass plugin_service_class;
} PluginServiceFileSaverClass;
688

689 690
struct _PluginServiceFileSaver {
	PluginService plugin_service;
691

692 693 694 695 696 697
	gchar *file_extension;
	FileFormatLevel format_level;
	gchar *description;
	gint   default_saver_priority;
	FileSaveScope save_scope;
	gboolean overwrite_files;
698

699 700 701
	GnumFileSaver *saver;
	PluginServiceFileSaverCallbacks cbs;
};
702

703 704 705 706 707 708 709 710 711 712 713

static void
plugin_service_file_saver_init (GObject *obj)
{
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (obj);

	GNM_PLUGIN_SERVICE (obj)->cbs_ptr = &service_file_saver->cbs;
	service_file_saver->file_extension = NULL;
	service_file_saver->description = NULL;
	service_file_saver->cbs.plugin_func_file_save = NULL;
	service_file_saver->saver = NULL;
714 715 716
}

static void
717
plugin_service_file_saver_finalize (GObject *obj)
718
{
719 720
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (obj);
	GObjectClass *parent_class;
721

722 723 724 725 726 727 728 729
	g_free (service_file_saver->file_extension);
	service_file_saver->file_extension = NULL;
	g_free (service_file_saver->description);
	service_file_saver->description = NULL;
	if (service_file_saver->saver != NULL) {
		g_object_unref (service_file_saver->saver);
		service_file_saver->saver = NULL;
	}
730

731 732
	parent_class = g_type_class_peek (GNM_PLUGIN_SERVICE_TYPE);
	parent_class->finalize (obj);
733 734 735
}

static void
736
plugin_service_file_saver_read_xml (PluginService *service, xmlNode *tree, ErrorInfo **ret_error)
737
{
738 739 740 741
	gchar *file_extension;
	xmlNode *information_node;
	gchar *description;
	gchar *format_level_str, *save_scope_str;
742

743 744 745 746 747 748 749 750
	GNM_INIT_RET_ERROR_INFO (ret_error);
	file_extension = e_xml_get_string_prop_by_name (tree, (xmlChar *)"file_extension");
	format_level_str = e_xml_get_string_prop_by_name (tree, (xmlChar *)"format_level");
	save_scope_str = e_xml_get_string_prop_by_name (tree, (xmlChar *)"save_scope");
	information_node = e_xml_get_child_by_name (tree, (xmlChar *)"information");
	if (information_node != NULL) {
		xmlNode *node;
		xmlChar *val;
751

752 753 754 755 756 757
		node = e_xml_get_child_by_name_by_lang_list (
		       information_node, "description", NULL);
		if (node != NULL) {
			val = xmlNodeGetContent (node);
			description = g_strdup ((gchar *)val);
			xmlFree (val);
758
		} else {
759
			description = NULL;
760
		}
761 762 763 764 765 766 767 768 769 770 771 772 773
	} else {
		description = NULL;
	}
	if (description != NULL) {
		PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (service);

		service_file_saver->file_extension = file_extension;
		service_file_saver->description = description;
		service_file_saver->format_level = parse_format_level_str (format_level_str,
		                                                           FILE_FL_WRITE_ONLY);
		service_file_saver->default_saver_priority = e_xml_get_integer_prop_by_name_with_default (
		                                             tree, (xmlChar *)"default_saver_priority", -1);
		service_file_saver->save_scope = (save_scope_str != NULL &&
774
		                                 g_ascii_strcasecmp (save_scope_str, "sheet") == 0) ?
775 776 777 778 779 780
		                                 FILE_SAVE_SHEET : FILE_SAVE_WORKBOOK;
		service_file_saver->overwrite_files = e_xml_get_bool_prop_by_name_with_default (
		                                      tree, (xmlChar *)"overwrite_files", TRUE);
	} else {
		*ret_error = error_info_new_str (_("File saver has no description"));
		g_free (file_extension);
781
	}
782 783
	g_free (format_level_str);
	g_free (save_scope_str);
784 785 786
}

static void
787
plugin_service_file_saver_activate (PluginService *service, ErrorInfo **ret_error)
788
{
789 790
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (service);
	GHashTable *file_savers_hash;
791

792 793 794 795 796 797 798
	GNM_INIT_RET_ERROR_INFO (ret_error);
	service_file_saver->saver = GNUM_FILE_SAVER (gnum_plugin_file_saver_new (service));
	if (service_file_saver->default_saver_priority < 0) {
		register_file_saver (service_file_saver->saver);
	} else {
		register_file_saver_as_default (service_file_saver->saver,
		                                service_file_saver->default_saver_priority);
799
	}
800 801 802 803
	file_savers_hash = get_plugin_file_savers_hash (service->plugin);
	g_assert (g_hash_table_lookup (file_savers_hash, service->id) == NULL);
	g_hash_table_insert (file_savers_hash, g_strdup (service->id), service_file_saver->saver);
	service->is_active = TRUE;
804 805
}

806 807 808 809 810
static void
plugin_service_file_saver_deactivate (PluginService *service, ErrorInfo **ret_error)
{
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (service);
	GHashTable *file_savers_hash;
811

812 813 814 815 816 817 818
	GNM_INIT_RET_ERROR_INFO (ret_error);
	file_savers_hash = get_plugin_file_savers_hash (service->plugin);
	g_hash_table_remove (file_savers_hash, service->id);
	unregister_file_saver (service_file_saver->saver);
	service->is_active = FALSE;
}

819 820 821 822 823 824 825 826 827
static char *
plugin_service_file_saver_get_description (PluginService *service)
{
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (service);

	return g_strdup_printf (
		_("File saver - %s"), service_file_saver->description);
}

828 829 830 831 832 833 834 835 836
static void
plugin_service_file_saver_class_init (GObjectClass *gobject_class)
{
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);

	gobject_class->finalize = plugin_service_file_saver_finalize;
	plugin_service_class->read_xml = plugin_service_file_saver_read_xml;
	plugin_service_class->activate = plugin_service_file_saver_activate;
	plugin_service_class->deactivate = plugin_service_file_saver_deactivate;
837
	plugin_service_class->get_description = plugin_service_file_saver_get_description;
838 839 840 841 842 843 844 845
}

GSF_CLASS (PluginServiceFileSaver, plugin_service_file_saver,
           plugin_service_file_saver_class_init, plugin_service_file_saver_init,
           GNM_PLUGIN_SERVICE_TYPE)


/** GnumPluginFileSaver class **/
846 847

#define TYPE_GNUM_PLUGIN_FILE_SAVER             (gnum_plugin_file_saver_get_type ())
848 849 850
#define GNUM_PLUGIN_FILE_SAVER(obj)             (G_TYPE_CHECK_INSTANCE_CAST ((obj), TYPE_GNUM_PLUGIN_FILE_SAVER, GnumPluginFileSaver))
#define GNUM_PLUGIN_FILE_SAVER_CLASS(klass)     (G_TYPE_CHECK_CLASS_CAST ((klass), TYPE_GNUM_PLUGIN_FILE_SAVER, GnumPluginFileSaverClass))
#define IS_GNUM_PLUGIN_FILE_SAVER(obj)          (G_TYPE_CHECK_INSTANCE_TYPE ((obj), TYPE_GNUM_PLUGIN_FILE_SAVER))
851

852
GType gnum_plugin_file_saver_get_type (void);
853

854
typedef struct {
855
	GnumFileSaverClass parent_class;
856
} GnumPluginFileSaverClass;
857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873

struct _GnumPluginFileSaver {
	GnumFileSaver parent;

	PluginService *service;
};

static void
gnum_plugin_file_saver_init (GnumPluginFileSaver *fs)
{
	fs->service = NULL;
}

static void
gnum_plugin_file_saver_save (GnumFileSaver const *fs, IOContext *io_context,
                             WorkbookView *wbv, const gchar *file_name)
{
874 875
	GnumPluginFileSaver *pfs = GNUM_PLUGIN_FILE_SAVER (fs);
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (pfs->service);
876
	ErrorInfo *error = NULL;
877 878 879 880 881

	g_return_if_fail (file_name != NULL);

	plugin_service_load (pfs->service, &error);
	if (error == NULL) {
882 883
		g_return_if_fail (service_file_saver->cbs.plugin_func_file_save != NULL);
		service_file_saver->cbs.plugin_func_file_save (fs, pfs->service, io_context, wbv, file_name);
884 885
	} else {
		gnumeric_io_error_info_set (io_context, error);
886 887
		gnumeric_io_error_push (io_context, error_info_new_str (
		                        _("Error while saving file.")));
888 889 890 891 892 893 894 895 896 897 898
	}
}

static void
gnum_plugin_file_saver_class_init (GnumPluginFileSaverClass *klass)
{
	GnumFileSaverClass *gnum_file_saver_klass = GNUM_FILE_SAVER_CLASS (klass);

	gnum_file_saver_klass->save = gnum_plugin_file_saver_save;
}

Jody Goldberg's avatar
Jody Goldberg committed
899 900 901
GSF_CLASS (GnumPluginFileSaver, gnum_plugin_file_saver,
	   gnum_plugin_file_saver_class_init, gnum_plugin_file_saver_init,
	   TYPE_GNUM_FILE_SAVER)
902 903 904 905 906

static GnumPluginFileSaver *
gnum_plugin_file_saver_new (PluginService *service)
{
	GnumPluginFileSaver *fs;
907
	PluginServiceFileSaver *service_file_saver = GNM_PLUGIN_SERVICE_FILE_SAVER (service);
908 909
	gchar *saver_id;

910 911
	saver_id = g_strconcat (
		gnm_plugin_get_id (service->plugin), ":", service->id, NULL);
912
	fs = GNUM_PLUGIN_FILE_SAVER (g_object_new (TYPE_GNUM_PLUGIN_FILE_SAVER, NULL));
913 914 915 916
	gnum_file_saver_setup (GNUM_FILE_SAVER (fs), saver_id,
	                       service_file_saver->file_extension,
	                       service_file_saver->description,
	                       service_file_saver->format_level,
917
	                       NULL);
918 919
	gnum_file_saver_set_save_scope (GNUM_FILE_SAVER (fs),
	                                service_file_saver->save_scope);
920 921
	gnum_file_saver_set_overwrite_files (GNUM_FILE_SAVER (fs),
	                                     service_file_saver->overwrite_files);
922 923 924 925 926 927
	fs->service = service;
	g_free (saver_id);

	return fs;
}

928
/** -- **/
929

930 931 932
/*
 * PluginServiceFunctionGroup
 */
933

934 935 936
typedef struct{
	PluginServiceClass plugin_service_class;
} PluginServiceFunctionGroupClass;
937

938 939
struct _PluginServiceFunctionGroup {
	PluginService plugin_service;
940

941 942
	gchar *category_name, *translated_category_name;
	GSList *function_name_list;
943

944
	GnmFuncGroup	*func_group;
945 946
	PluginServiceFunctionGroupCallbacks cbs;
};
947 948 949


static void
950
plugin_service_function_group_init (GObject *obj)
951
{
952
	PluginServiceFunctionGroup *service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (obj);
953

954 955 956 957
	GNM_PLUGIN_SERVICE (obj)->cbs_ptr = &service_function_group->cbs;
	service_function_group->category_name = NULL;
	service_function_group->translated_category_name = NULL;
	service_function_group->function_name_list = NULL;
958
	service_function_group->func_group = NULL;
959 960 961
}

static void
962
plugin_service_function_group_finalize (GObject *obj)
963
{
964 965
	PluginServiceFunctionGroup *service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (obj);
	GObjectClass *parent_class;
966

967 968 969 970 971 972
	g_free (service_function_group->category_name);
	service_function_group->category_name = NULL;
	g_free (service_function_group->translated_category_name);
	service_function_group->translated_category_name = NULL;
	g_slist_free_custom (service_function_group->function_name_list, g_free);
	service_function_group->function_name_list = NULL;
973

974 975
	parent_class = g_type_class_peek (GNM_PLUGIN_SERVICE_TYPE);
	parent_class->finalize (obj);
976 977
}

978 979
static void
plugin_service_function_group_read_xml (PluginService *service, xmlNode *tree, ErrorInfo **ret_error)
980 981
{
	xmlNode *category_node, *translated_category_node, *functions_node;
982 983
	gchar *category_name, *translated_category_name;
	GSList *function_name_list = NULL;
984

985
	GNM_INIT_RET_ERROR_INFO (ret_error);
986 987
	category_node = e_xml_get_child_by_name_no_lang (tree, "category");
	if (category_node != NULL) {
988 989 990
		xmlChar *val;

		val = xmlNodeGetContent (category_node);
Jody Goldberg's avatar
Jody Goldberg committed
991
		category_name = g_strdup ((gchar *)val);
992
		xmlFree (val);
993 994 995 996 997 998 999
	} else {
		category_name = NULL;
	}
	translated_category_node = e_xml_get_child_by_name_by_lang_list (tree, "category", NULL);
	if (translated_category_node != NULL) {
		gchar *lang;

Jody Goldberg's avatar
Jody Goldberg committed
1000
		lang = e_xml_get_string_prop_by_name (translated_category_node, (xmlChar *)"xml:lang");
1001
		if (lang != NULL) {
1002 1003 1004
			xmlChar *val;

			val = xmlNodeGetContent (translated_category_node);
Jody Goldberg's avatar
Jody Goldberg committed
1005
			translated_category_name = g_strdup ((gchar *)val);
1006
			xmlFree (val);
1007 1008 1009 1010 1011 1012 1013
			g_free (lang);
		} else {
			translated_category_name = NULL;
		}
	} else {
		translated_category_name = NULL;
	}
Jody Goldberg's avatar
Jody Goldberg committed
1014
	functions_node = e_xml_get_child_by_name (tree, (xmlChar *)"functions");
1015 1016 1017 1018 1019 1020 1021
	if (functions_node != NULL) {
		xmlNode *node;

		for (node = functions_node->xmlChildrenNode; node != NULL; node = node->next) {
			gchar *func_name;

			if (strcmp (node->name, "function") != 0 ||
Jody Goldberg's avatar
Jody Goldberg committed
1022
			    (func_name = e_xml_get_string_prop_by_name (node, (xmlChar *)"name")) == NULL) {
1023 1024
				continue;
			}
1025
			GNM_SLIST_PREPEND (function_name_list, func_name);
1026
		}
1027
		GNM_SLIST_REVERSE (function_name_list);
1028
	}
1029 1030
	if (category_name != NULL && function_name_list != NULL) {
		PluginServiceFunctionGroup *service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (service);
1031 1032 1033 1034 1035

		service_function_group->category_name = category_name;
		service_function_group->translated_category_name = translated_category_name;
		service_function_group->function_name_list = function_name_list;
	} else {
1036
		GSList *error_list = NULL;
1037 1038

		if (category_name == NULL) {
1039 1040
			GNM_SLIST_PREPEND (error_list, error_info_new_str (
				_("Missing function category name.")));
1041
		}
1042
		if (function_name_list == NULL) {
1043
			GNM_SLIST_PREPEND (error_list, error_info_new_str (
1044
				_("Function group is empty.")));
1045
		}
1046
		GNM_SLIST_REVERSE (error_list);
1047 1048 1049 1050
		*ret_error = error_info_new_from_error_list (error_list);

		g_free (category_name);
		g_free (translated_category_name);
1051
		g_slist_free_custom (function_name_list, g_free);
1052 1053 1054 1055
	}
}

static gboolean
1056 1057
plugin_service_function_group_func_desc_load (GnmFunc const *fn_def,
					      GnmFuncDescriptor *res)
1058 1059 1060
{
	PluginService *service;
	PluginServiceFunctionGroup *service_function_group;
1061
	ErrorInfo *error = NULL;
1062

1063
	g_return_val_if_fail (fn_def != NULL, FALSE);
1064

1065
	service = gnm_func_get_user_data (fn_def);
1066
	service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (service);
1067
	plugin_service_load (service, &error);
1068
	if (error != NULL) {
1069 1070 1071 1072
		error_info_print (error);
		error_info_free (error);
		return FALSE;
	}
1073 1074
	return service_function_group->cbs.func_desc_load (service,
		gnm_func_get_name (fn_def), res);
1075 1076
}

1077
static void
1078
plugin_service_function_group_func_ref_notify  (GnmFunc *fn_def, int refcount)
1079 1080 1081
{
	PluginService *service;

1082
	service = gnm_func_get_user_data (fn_def);
1083 1084 1085 1086 1087 1088 1089 1090
	g_return_if_fail (GNM_IS_PLUGIN_SERVICE_FUNCTION_GROUP (service));
	if (refcount == 0) {
		gnm_plugin_use_unref (service->plugin);
	} else {
		gnm_plugin_use_ref (service->plugin);
	}
}

1091
static void
1092
plugin_service_function_group_activate (PluginService *service, ErrorInfo **ret_error)
1093
{
1094
	PluginServiceFunctionGroup *service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (service);
1095

1096
	GNM_INIT_RET_ERROR_INFO (ret_error);
1097
	service_function_group->func_group = gnm_func_group_fetch_with_translation (
1098 1099 1100
		service_function_group->category_name,
		service_function_group->translated_category_name);
	GNM_SLIST_FOREACH (service_function_group->function_name_list, char, fname,
1101
		GnmFunc *fn_def;
1102

1103
		fn_def = gnm_func_add_stub (
1104
			service_function_group->func_group, fname,
1105
			plugin_service_function_group_func_desc_load,
1106
			plugin_service_function_group_func_ref_notify);
1107
		gnm_func_set_user_data (fn_def, service);
1108 1109
	);
	service->is_active = TRUE;
1110 1111 1112
}

static void
1113
plugin_service_function_group_deactivate (PluginService *service, ErrorInfo **ret_error)
1114
{
1115 1116 1117 1118
	PluginServiceFunctionGroup *service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (service);

	GNM_INIT_RET_ERROR_INFO (ret_error);
	GNM_SLIST_FOREACH (service_function_group->function_name_list, char, fname,
1119
		function_remove (service_function_group->func_group, fname);
1120 1121 1122
	);
	service->is_active = FALSE;
}
1123

1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143
static char *
plugin_service_function_group_get_description (PluginService *service)
{
	PluginServiceFunctionGroup *service_function_group = GNM_PLUGIN_SERVICE_FUNCTION_GROUP (service);
	int n_functions;
	const char *category_name;

	n_functions = g_slist_length (service_function_group->function_name_list);
	category_name = service_function_group->translated_category_name != NULL
		? service_function_group->translated_category_name
		: service_function_group->category_name;

	return g_strdup_printf (
		ngettext (
			N_("%d function in category \"%s\""),
			N_("Group of %d functions in category \"%s\""),
			n_functions),
		n_functions, category_name);
}

1144 1145 1146 1147
static void
plugin_service_function_group_class_init (GObjectClass *gobject_class)
{
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);
1148

1149 1150 1151 1152
	gobject_class->finalize = plugin_service_function_group_finalize;
	plugin_service_class->read_xml = plugin_service_function_group_read_xml;
	plugin_service_class->activate = plugin_service_function_group_activate;
	plugin_service_class->deactivate = plugin_service_function_group_deactivate;
1153
	plugin_service_class->get_description = plugin_service_function_group_get_description;
1154 1155
}

1156 1157 1158
GSF_CLASS (PluginServiceFunctionGroup, plugin_service_function_group,
           plugin_service_function_group_class_init, plugin_service_function_group_init,
           GNM_PLUGIN_SERVICE_TYPE)
1159 1160


1161 1162 1163
/*
 * PluginServicePluginLoader
 */
1164

1165 1166 1167
typedef struct{
	PluginServiceClass plugin_service_class;
} PluginServicePluginLoaderClass;
1168

1169 1170 1171 1172
struct _PluginServicePluginLoader {
	PluginService plugin_service;
	PluginServicePluginLoaderCallbacks cbs;
};
1173 1174 1175


static void
1176
plugin_service_plugin_loader_init (GObject *obj)
1177
{
1178
	PluginServicePluginLoader *service_plugin_loader = GNM_PLUGIN_SERVICE_PLUGIN_LOADER (obj);
1179

1180
	GNM_PLUGIN_SERVICE (obj)->cbs_ptr = &service_plugin_loader->cbs;
1181 1182
}

1183
GType
1184
plugin_service_plugin_loader_generate_type (PluginService *service, ErrorInfo **ret_error)
1185
{
1186
	PluginServicePluginLoader *service_plugin_loader = GNM_PLUGIN_SERVICE_PLUGIN_LOADER (service);
1187
	ErrorInfo *error = NULL;
1188
	GType loader_type;
1189

1190
	GNM_INIT_RET_ERROR_INFO (ret_error);
1191 1192
	plugin_service_load (service, &error);
	if (error == NULL) {
1193 1194
		loader_type = service_plugin_loader->cbs.plugin_func_get_loader_type (
			service, &error);
1195
		if (error == NULL)
1196
			return loader_type;
1197
		*ret_error = error;
1198 1199 1200 1201 1202
	} else {
		*ret_error = error_info_new_str_with_details (
		             _("Error while loading plugin service."),
		             error);
	}
1203
	return G_TYPE_NONE;
1204 1205 1206
}

static void
1207
plugin_service_plugin_loader_activate (PluginService *service, ErrorInfo **ret_error)
1208
{
1209
	gchar *full_id;
1210

1211
	GNM_INIT_RET_ERROR_INFO (ret_error);
1212
	full_id = g_strconcat (
1213
		gnm_plugin_get_id (service->plugin), ":", service->id, NULL);
1214 1215
	plugins_register_loader (full_id, service);
	g_free (full_id);
1216
	service->is_active = TRUE;
1217 1218 1219
}

static void
1220
plugin_service_plugin_loader_deactivate (PluginService *service, ErrorInfo **ret_error)
1221
{
1222 1223
	gchar *full_id;

1224
	GNM_INIT_RET_ERROR_INFO (ret_error);
1225
	full_id = g_strconcat (
1226
		gnm_plugin_get_id (service->plugin), ":", service->id, NULL);
1227 1228
	plugins_register_loader (full_id, service);
	g_free (full_id);
1229
	service->is_active = FALSE;
1230 1231
}

1232 1233 1234 1235 1236 1237
static char *
plugin_service_plugin_loader_get_description (PluginService *service)
{
	return g_strdup (_("Plugin loader"));
}

1238
static void
1239
plugin_service_plugin_loader_class_init (GObjectClass *gobject_class)
1240
{
1241
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);
1242

1243 1244
	plugin_service_class->activate = plugin_service_plugin_loader_activate;
	plugin_service_class->deactivate = plugin_service_plugin_loader_deactivate;
1245
	plugin_service_class->get_description = plugin_service_plugin_loader_get_description;
1246 1247
}

1248 1249 1250 1251
GSF_CLASS (PluginServicePluginLoader, plugin_service_plugin_loader,
           plugin_service_plugin_loader_class_init, plugin_service_plugin_loader_init,
           GNM_PLUGIN_SERVICE_TYPE)

Zbigniew Chyla's avatar
Zbigniew Chyla committed
1252

1253
#ifdef WITH_BONOBO
Zbigniew Chyla's avatar
Zbigniew Chyla committed
1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336
/*
 * PluginServiceUI
 */

typedef struct{
	PluginServiceClass plugin_service_class;
} PluginServiceUIClass;

struct _PluginServiceUI {
	PluginService plugin_service;

	char *file_name;
	GSList *verbs;

	CustomXmlUI *ui;
	PluginServiceUICallbacks cbs;
};

static void
plugin_service_ui_init (GObject *obj)
{
	PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (obj);

	GNM_PLUGIN_SERVICE (obj)->cbs_ptr = &service_ui->cbs;
	service_ui->file_name = NULL;
	service_ui->verbs = NULL;
	service_ui->ui = NULL;
	service_ui->cbs.plugin_func_exec_verb = NULL;
}

static void
plugin_service_ui_finalize (GObject *obj)
{
	PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (obj);
	GObjectClass *parent_class;

	g_free (service_ui->file_name);
	service_ui->file_name = NULL;
	g_slist_free_custom (service_ui->verbs, g_free);
	service_ui->verbs = NULL;

	parent_class = g_type_class_peek (GNM_PLUGIN_SERVICE_TYPE);
	parent_class->finalize (obj);
}

static void
plugin_service_ui_read_xml (PluginService *service, xmlNode *tree, ErrorInfo **ret_error)
{
	PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (service);
	char *file_name;
	xmlNode *verbs_node;
	GSList *verbs = NULL;

	GNM_INIT_RET_ERROR_INFO (ret_error);
	file_name = e_xml_get_string_prop_by_name (tree, "file");
	if (file_name == NULL) {
		*ret_error = error_info_new_str (
		             _("Missing file name."));
		return;
	}		
	verbs_node = e_xml_get_child_by_name (tree, "verbs");
	if (verbs_node != NULL) {
		xmlNode *node;

		for (node = verbs_node->xmlChildrenNode; node != NULL; node = node->next) {
			char *name;

			if (strcmp (node->name, "verb") == 0 &&
			    (name = e_xml_get_string_prop_by_name (node, "name")) != NULL) {
				GNM_SLIST_PREPEND (verbs, name);
			}
		}
	}
	GNM_SLIST_REVERSE (verbs);

	service_ui->file_name = file_name;
	service_ui->verbs = verbs;
}

static void
ui_verb_fn (BonoboUIComponent *uic, gpointer user_data, const gchar *cname)
{
	PluginService *service = GNM_PLUGIN_SERVICE (user_data);
1337
	ErrorInfo *load_error = NULL;
Zbigniew Chyla's avatar
Zbigniew Chyla committed
1338 1339 1340 1341 1342
	
	plugin_service_load (service, &load_error);
	if (load_error == NULL) {
		PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (service);
		WorkbookControlGUI *wbcg;
1343
		ErrorInfo *ignored_error = NULL;
Zbigniew Chyla's avatar
Zbigniew Chyla committed
1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384

		g_return_if_fail (service_ui->cbs.plugin_func_exec_verb != NULL);
		wbcg = g_object_get_data (
			G_OBJECT (uic), "gnumeric-workbook-control-gui");
		g_return_if_fail (IS_WORKBOOK_CONTROL_GUI (wbcg));
		service_ui->cbs.plugin_func_exec_verb (
			service, wbcg, uic, cname, &ignored_error);
		if (ignored_error != NULL) {
			error_info_print (ignored_error);
			error_info_free (ignored_error);
		}
	} else {
		error_info_print (load_error);
		error_info_free (load_error);
	}
}

static void
plugin_service_ui_activate (PluginService *service, ErrorInfo **ret_error)
{
	PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (service);
	char *full_file_name;
	BonoboUINode *uinode;
	char *xml_ui;
	const char *textdomain;

	GNM_INIT_RET_ERROR_INFO (ret_error);
	full_file_name = g_build_path (
		G_DIR_SEPARATOR_S, gnm_plugin_get_dir_name (service->plugin),
		service_ui->file_name, NULL);
	uinode = bonobo_ui_node_from_file (full_file_name);
	if (uinode == NULL) {
		*ret_error = error_info_new_printf (
		             _("Cannot read UI description from XML file %s."),
		             full_file_name);
		g_free (full_file_name);
		return;
	}		             
	g_free (full_file_name);

	xml_ui = bonobo_ui_node_to_string (uinode, TRUE);
Morten Welinder's avatar
Morten Welinder committed
1385 1386
	bonobo_ui_node_free (uinode);

Zbigniew Chyla's avatar
Zbigniew Chyla committed
1387 1388 1389 1390
	textdomain = gnm_plugin_get_textdomain (service->plugin);
	service_ui->ui = register_xml_ui (
		xml_ui, textdomain, service_ui->verbs, ui_verb_fn, service);
	service->is_active = TRUE;
Morten Welinder's avatar
Morten Welinder committed
1391
	g_free (xml_ui);
Zbigniew Chyla's avatar
Zbigniew Chyla committed
1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403
}

static void
plugin_service_ui_deactivate (PluginService *service, ErrorInfo **ret_error)
{
	PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (service);

	GNM_INIT_RET_ERROR_INFO (ret_error);
	unregister_xml_ui (service_ui->ui);
	service->is_active = FALSE;
}

1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418
static char *
plugin_service_ui_get_description (PluginService *service)
{
	PluginServiceUI *service_ui = GNM_PLUGIN_SERVICE_UI (service);
	int n_verbs;

	n_verbs = g_slist_length (service_ui->verbs);
	return g_strdup_printf (
		ngettext (
			N_("User interface with %d action"),
			N_("User interface with %d actions"),
			n_verbs),
		n_verbs);
}

Zbigniew Chyla's avatar
Zbigniew Chyla committed
1419 1420 1421 1422 1423 1424 1425 1426 1427
static void
plugin_service_ui_class_init (GObjectClass *gobject_class)
{
	PluginServiceClass *plugin_service_class = GPS_CLASS (gobject_class);

	gobject_class->finalize = plugin_service_ui_finalize;
	plugin_service_class->read_xml = plugin_service_ui_read_xml;
	plugin_service_class->activate = plugin_service_ui_activate;
	plugin_service_class->deactivate = plugin_service_ui_deactivate;
1428
	plugin_service_class->get_description = plugin_service_ui_get_description;
Zbigniew Chyla's avatar
Zbigniew Chyla committed
1429 1430 1431 1432 1433 1434
}

GSF_CLASS (PluginServiceUI, plugin_service_ui,
           plugin_service_ui_class_init, plugin_service_ui_init,
           GNM_PLUGIN_SERVICE_TYPE)

1435
#endif
Zbigniew Chyla's avatar
Zbigniew Chyla committed
1436

1437 1438
/* ---------------------------------------------------------------------- */

1439 1440 1441
static void
plugin_service_load (PluginService *service, ErrorInfo **ret_error)
{
1442
	g_return_if_fail (GNM_IS_PLUGIN_SERVICE (service));
1443

1444
	GNM_INIT_RET_ERROR_INFO (ret_error);
1445 1446 1447
	if (service->is_loaded) {
		return;
	}
1448 1449
	gnm_plugin_load_service (service->plugin, service, ret_error);
	if (*ret_error == NULL) {
1450 1451 1452 1453 1454 1455 1456
		service->is_loaded = TRUE;
	}
}

static void
plugin_service_unload (PluginService *service, ErrorInfo **ret_error)
{
1457
	ErrorInfo *error = NULL;
1458

1459
	g_return_if_fail (GNM_IS_PLUGIN_SERVICE (service));
1460

1461
	GNM_INIT_RET_ERROR_INFO (ret_error);
1462 1463 1464
	if (!service->is_loaded) {
		return;
	}
1465
	gnm_plugin_unload_service (service->plugin, service, &error);
1466 1467 1468 1469 1470 1471 1472
	if (error == NULL) {
		service->is_loaded = FALSE;
	} else {
		*ret_error = error;
	}
}

1473 1474 1475 1476 1477 1478 1479 1480
static struct {
	const char *type_str;
	GType (*get_type_func) (void);
} service_types[] = {
	{"general", plugin_service_general_get_type},
	{"file_opener", plugin_service_file_opener_get_type},
	{"file_saver", plugin_service_file_saver_get_type},
	{"function_group", plugin_service_function_group_get_type},
1481 1482 1483 1484
	{"plugin_loader", plugin_service_plugin_loader_get_type}
#ifdef WITH_BONOBO
	,{"ui", plugin_service_ui_get_type}
#endif
1485 1486
};

1487
PluginService *
1488
plugin_service_new (xmlNode *tree, ErrorInfo **ret_error)
1489 1490
{
	PluginService *service = NULL;
1491
	char *id, *type_str;
1492
	ErrorInfo *service_error = NULL;
1493
	int ti;
1494 1495 1496

	g_return_val_if_fail (tree != NULL, NULL);
	g_return_val_if_fail (strcmp (tree->name, "service") == 0, NULL);
Zbigniew Chyla's avatar