Updated Spanish translation
[anjuta-git-plugin.git] / libanjuta / anjuta-plugin-handle.c
blob41f2dea84397fd6cced6202535ff0d3973ead4ad
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * anjuta-plugin-handle.c
4 * Copyright (C) Naba Kumar <naba@gnome.org>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 /**
22 * SECTION:anjuta-plugin-handle
23 * @short_description: Handle used by plugin manager to keep track of plugins.
24 * @see_also: #AnjutaPluginManager, #AnjutaPluginDescription, #AnjutaPlugin
25 * @stability: Unstable
26 * @include: libanjuta/anjuta-plugin-handle.h
28 * Plugin handle is wrapper for installed plugins. For each installed plugin
29 * there will be one corresponding plugin handle. It is mainly used by
30 * plugin manager to keep track of loading and unloading of plugins.
33 #include <string.h>
34 #include "anjuta-plugin-handle.h"
35 #include "resources.h"
36 #include "anjuta-debug.h"
38 enum
40 PROP_0,
42 PROP_ID,
43 PROP_NAME,
44 PROP_ABOUT,
45 PROP_ICON_PATH,
46 PROP_USER_ACTIVATABLE,
47 PROP_RESIDENT,
48 PROP_LANGUAGE,
49 PROP_DESCRIPTION,
50 PROP_DEPENDENCY_NAMES,
51 PROP_DEPENDENCIES,
52 PROP_DEPENDENTS,
53 PROP_INTERFACES,
54 PROP_CAN_LOAD,
55 PROP_CHECKED,
56 PROP_RESOLVE_PASS,
57 PROP_PATH
60 struct _AnjutaPluginHandlePriv
62 char *id;
63 char *name;
64 char *about;
65 char *icon_path;
66 char *path;
67 gboolean user_activatable;
68 gboolean resident;
69 char *language;
71 AnjutaPluginDescription *description;
73 /* The dependencies listed in the oaf file */
74 GList *dependency_names;
76 /* Interfaces exported by this plugin */
77 GList *interfaces;
79 /* Attributes defined for this plugin */
80 /* GHashTable *attributes; */
82 /* The keys of these tables represent the dependencies and dependents
83 * of the module. The values point back at the plugins */
84 GHashTable *dependencies;
85 GHashTable *dependents;
87 gboolean can_load;
88 gboolean checked;
90 /* The pass on which the module was resolved, or -1 if
91 * unresolved */
92 int resolve_pass;
95 static GObjectClass* parent_class = NULL;
97 static void
98 anjuta_plugin_handle_init (AnjutaPluginHandle *object)
100 object->priv = g_new0 (AnjutaPluginHandlePriv, 1);
102 object->priv->resolve_pass = -1;
104 object->priv->dependencies = g_hash_table_new (g_direct_hash,
105 g_direct_equal);
106 object->priv->dependents = g_hash_table_new (g_direct_hash,
107 g_direct_equal);
110 static void
111 anjuta_plugin_handle_finalize (GObject *object)
113 AnjutaPluginHandlePriv *priv;
115 priv = ANJUTA_PLUGIN_HANDLE (object)->priv;
117 g_free (priv->id);
118 priv->id = NULL;
119 g_free (priv->name);
120 priv->name = NULL;
121 g_free (priv->icon_path);
122 priv->icon_path = NULL;
123 g_free (priv->path);
124 priv->path = NULL;
125 g_free (priv->language);
126 priv->language = NULL;
128 g_list_foreach (priv->dependency_names, (GFunc)g_free, NULL);
129 g_list_free (priv->dependency_names);
130 priv->dependency_names = NULL;
132 g_list_foreach (priv->interfaces, (GFunc)g_free, NULL);
133 g_list_free (priv->dependency_names);
134 priv->dependency_names = NULL;
136 g_hash_table_destroy (priv->dependencies);
137 priv->dependencies = NULL;
138 g_hash_table_destroy (priv->dependents);
139 priv->dependents = NULL;
141 g_free (priv);
143 G_OBJECT_CLASS (parent_class)->finalize (object);
146 static void
147 anjuta_plugin_handle_set_property (GObject *object, guint prop_id,
148 const GValue *value, GParamSpec *pspec)
150 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (object));
152 switch (prop_id)
154 #if 0
155 case PROP_ID:
156 /* TODO: Add setter for "id" property here */
157 break;
158 case PROP_NAME:
159 /* TODO: Add setter for "name" property here */
160 break;
161 case PROP_ABOUT:
162 /* TODO: Add setter for "about" property here */
163 break;
164 case PROP_ICON_PATH:
165 /* TODO: Add setter for "icon-path" property here */
166 break;
167 case PROP_PATH:
168 /* TODO: Add setter for "path" property here */
169 break;
170 case PROP_USER_ACTIVATABLE:
171 /* TODO: Add setter for "user-activatable" property here */
172 break;
173 case PROP_RESIDENT:
174 /* TODO: Add setter for "resident" property here */
175 break;
176 case PROP_LANGUAGE:
177 /* TODO: Add setter for "language" property here */
178 break;
179 case PROP_DESCRIPTION:
180 /* TODO: Add setter for "description" property here */
181 break;
182 case PROP_DEPENDENCY_NAMES:
183 /* TODO: Add setter for "dependency-names" property here */
184 break;
185 case PROP_DEPENDENCIES:
186 /* TODO: Add setter for "dependencies" property here */
187 break;
188 case PROP_DEPENDENTS:
189 /* TODO: Add setter for "dependents" property here */
190 break;
191 case PROP_INTERFACES:
192 /* TODO: Add setter for "interfaces" property here */
193 break;
194 case PROP_CAN_LOAD:
195 /* TODO: Add setter for "can-load" property here */
196 break;
197 case PROP_CHECKED:
198 /* TODO: Add setter for "checked" property here */
199 break;
200 case PROP_RESOLVE_PASS:
201 /* TODO: Add setter for "resolve-pass" property here */
202 break;
203 #endif
204 default:
205 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
206 break;
210 static void
211 anjuta_plugin_handle_get_property (GObject *object, guint prop_id,
212 GValue *value, GParamSpec *pspec)
214 AnjutaPluginHandlePriv *priv;
216 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (object));
217 priv = ANJUTA_PLUGIN_HANDLE (object)->priv;
218 switch (prop_id)
220 case PROP_ID:
221 g_value_set_string (value, priv->id);
222 break;
223 case PROP_NAME:
224 g_value_set_string (value, priv->name);
225 break;
226 case PROP_ABOUT:
227 g_value_set_string (value, priv->about);
228 break;
229 case PROP_ICON_PATH:
230 g_value_set_string (value, priv->icon_path);
231 break;
232 case PROP_PATH:
233 g_value_set_string (value, priv->path);
234 break;
235 case PROP_USER_ACTIVATABLE:
236 g_value_set_boolean (value, priv->user_activatable);
237 break;
238 case PROP_RESIDENT:
239 g_value_set_boolean (value, priv->resident);
240 break;
241 case PROP_LANGUAGE:
242 g_value_set_string (value, priv->language);
243 break;
244 case PROP_DESCRIPTION:
245 g_value_set_pointer (value, priv->description);
246 break;
247 case PROP_DEPENDENCY_NAMES:
248 g_value_set_pointer (value, priv->dependency_names);
249 break;
250 case PROP_DEPENDENCIES:
251 g_value_set_pointer (value, priv->dependencies);
252 break;
253 case PROP_DEPENDENTS:
254 g_value_set_pointer (value, priv->dependents);
255 break;
256 case PROP_INTERFACES:
257 g_value_set_pointer (value, priv->interfaces);
258 break;
259 case PROP_CAN_LOAD:
260 g_value_set_boolean (value, priv->can_load);
261 break;
262 case PROP_CHECKED:
263 g_value_set_boolean (value, priv->checked);
264 break;
265 case PROP_RESOLVE_PASS:
266 g_value_set_int (value, priv->resolve_pass);
267 break;
268 default:
269 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
270 break;
274 static void
275 anjuta_plugin_handle_class_init (AnjutaPluginHandleClass *klass)
277 GObjectClass* object_class = G_OBJECT_CLASS (klass);
278 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
280 object_class->finalize = anjuta_plugin_handle_finalize;
281 object_class->set_property = anjuta_plugin_handle_set_property;
282 object_class->get_property = anjuta_plugin_handle_get_property;
284 g_object_class_install_property (object_class,
285 PROP_ID,
286 g_param_spec_string ("id",
287 "ID",
288 "Unique plugin ID",
289 NULL,
290 G_PARAM_READABLE));
292 g_object_class_install_property (object_class,
293 PROP_NAME,
294 g_param_spec_string ("name",
295 "Name",
296 "Plugin name",
297 NULL,
298 G_PARAM_READABLE));
300 g_object_class_install_property (object_class,
301 PROP_ABOUT,
302 g_param_spec_string ("about",
303 "About Plugin",
304 "About description of the plugin",
305 NULL,
306 G_PARAM_READABLE));
308 g_object_class_install_property (object_class,
309 PROP_ICON_PATH,
310 g_param_spec_string ("icon-path",
311 "Icon Path",
312 "Icon path of the plugin",
313 NULL,
314 G_PARAM_READABLE));
316 g_object_class_install_property (object_class,
317 PROP_PATH,
318 g_param_spec_string ("path",
319 "Path",
320 "Path of the plugin",
321 NULL,
322 G_PARAM_READABLE));
324 g_object_class_install_property (object_class,
325 PROP_USER_ACTIVATABLE,
326 g_param_spec_boolean ("user-activatable",
327 "User Activatable",
328 "If the plugin is user activatable",
329 FALSE,
330 G_PARAM_READABLE));
332 g_object_class_install_property (object_class,
333 PROP_RESIDENT,
334 g_param_spec_boolean ("resident",
335 "Resident",
336 "If the plugin cannot be unloaded",
337 FALSE,
338 G_PARAM_READABLE));
340 g_object_class_install_property (object_class,
341 PROP_LANGUAGE,
342 g_param_spec_string ("language",
343 "Language",
344 "Language used to write the plugin",
345 NULL,
346 G_PARAM_READABLE));
348 g_object_class_install_property (object_class,
349 PROP_DESCRIPTION,
350 g_param_spec_pointer ("description",
351 "Description",
352 "Plugin description",
353 G_PARAM_READABLE));
355 g_object_class_install_property (object_class,
356 PROP_DEPENDENCY_NAMES,
357 g_param_spec_pointer ("dependency-names",
358 "Dependency names",
359 "Plugin dependency names listed in oaf file",
360 G_PARAM_READABLE));
362 g_object_class_install_property (object_class,
363 PROP_DEPENDENCIES,
364 g_param_spec_pointer ("dependencies",
365 "Dependencies",
366 "Plugin dependencies",
367 G_PARAM_READABLE));
369 g_object_class_install_property (object_class,
370 PROP_DEPENDENTS,
371 g_param_spec_pointer ("dependents",
372 "Dependents",
373 "Plugin dependents",
374 G_PARAM_READABLE));
376 g_object_class_install_property (object_class,
377 PROP_INTERFACES,
378 g_param_spec_pointer ("interfaces",
379 "Interfaces",
380 "Interfaces exported by the plugin",
381 G_PARAM_READABLE));
383 g_object_class_install_property (object_class,
384 PROP_CAN_LOAD,
385 g_param_spec_boolean ("can-load",
386 "Can Load",
387 "If the plugin can be loaded",
388 FALSE,
389 G_PARAM_READABLE));
391 g_object_class_install_property (object_class,
392 PROP_CHECKED,
393 g_param_spec_boolean ("checked",
394 "Checked",
395 "If the plugin is checked in UI",
396 FALSE,
397 G_PARAM_READABLE));
399 g_object_class_install_property (object_class,
400 PROP_RESOLVE_PASS,
401 g_param_spec_int ("resolve-pass",
402 "Resolve Pass",
403 "Dependency resolution pass",
404 G_MININT, /* TODO: Adjust minimum property value */
405 G_MAXINT, /* TODO: Adjust maximum property value */
407 G_PARAM_READABLE));
410 GType
411 anjuta_plugin_handle_get_type (void)
413 static GType our_type = 0;
415 if(our_type == 0)
417 static const GTypeInfo our_info =
419 sizeof (AnjutaPluginHandleClass), /* class_size */
420 (GBaseInitFunc) NULL, /* base_init */
421 (GBaseFinalizeFunc) NULL, /* base_finalize */
422 (GClassInitFunc) anjuta_plugin_handle_class_init, /* class_init */
423 (GClassFinalizeFunc) NULL, /* class_finalize */
424 NULL /* class_data */,
425 sizeof (AnjutaPluginHandle), /* instance_size */
426 0, /* n_preallocs */
427 (GInstanceInitFunc) anjuta_plugin_handle_init, /* instance_init */
428 NULL /* value_table */
431 our_type = g_type_register_static (G_TYPE_OBJECT, "AnjutaPluginHandle",
432 &our_info, 0);
435 return our_type;
439 static char *
440 get_icon_path (char *icon_name)
442 char *ret;
444 if (g_path_is_absolute (icon_name)) {
445 ret = g_strdup (icon_name);
446 } else {
447 ret = anjuta_res_get_pixmap_file (icon_name);
450 return ret;
453 static GList *
454 property_to_list (const char *value)
456 GList *l = NULL;
457 char **split_str;
458 char **p;
460 split_str = g_strsplit (value, ",", -1);
461 for (p = split_str; *p != NULL; p++) {
462 l = g_list_prepend (l, g_strdup (g_strstrip (*p)));
464 g_strfreev (split_str);
465 return l;
468 AnjutaPluginHandle*
469 anjuta_plugin_handle_new (const gchar *plugin_desc_path)
471 AnjutaPluginHandle *plugin_handle;
472 AnjutaPluginDescription *desc;
473 char *str;
474 gchar *contents;
475 gboolean success = TRUE;
477 /* Load file content */
478 if (g_file_get_contents (plugin_desc_path, &contents, NULL, NULL)) {
480 desc = anjuta_plugin_description_new_from_string (contents, NULL);
481 g_free (contents);
482 if (!desc) {
483 g_warning ("Bad plugin file: %s\n", plugin_desc_path);
484 return NULL;
487 else
489 return NULL;
492 plugin_handle = g_object_new (ANJUTA_TYPE_PLUGIN_HANDLE, NULL);
494 /* Initialize plugin handle */
495 plugin_handle->priv->description = desc;
496 plugin_handle->priv->user_activatable = TRUE;
497 plugin_handle->priv->resident = TRUE;
498 plugin_handle->priv->path = g_path_get_dirname (plugin_desc_path);
500 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
501 "Location", &str)) {
502 plugin_handle->priv->id = str;
503 } else {
504 g_warning ("Couldn't find 'Location'");
505 success = FALSE;
508 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
509 "Name", &str)) {
510 plugin_handle->priv->name = str;
511 } else {
512 g_warning ("couldn't find 'Name' attribute.");
513 success = FALSE;
516 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
517 "Description", &str)) {
518 plugin_handle->priv->about = str;
519 } else {
520 g_warning ("Couldn't find 'Description' attribute.");
521 success = FALSE;
524 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
525 "Icon", &str)) {
526 plugin_handle->priv->icon_path = get_icon_path (str);
527 g_free (str);
530 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
531 "Dependencies",
532 &str)) {
533 plugin_handle->priv->dependency_names = property_to_list (str);
534 g_free (str);
537 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
538 "Interfaces",
539 &str)) {
540 plugin_handle->priv->interfaces = property_to_list (str);
541 g_free (str);
544 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
545 "UserActivatable", &str)) {
546 if (str && strcasecmp (str, "no") == 0)
548 plugin_handle->priv->user_activatable = FALSE;
550 DEBUG_PRINT ("Plugin '%s' is not user activatable",
551 plugin_handle->priv->name?
552 plugin_handle->priv->name : "Unknown");
555 g_free (str);
558 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
559 "Resident", &str)) {
560 if (str && strcasecmp (str, "no") == 0)
562 plugin_handle->priv->resident = FALSE;
564 g_free (str);
567 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
568 "Language", &str)) {
569 plugin_handle->priv->language = str;
572 if (!success) {
573 g_object_unref (plugin_handle);
574 plugin_handle = NULL;
577 return plugin_handle;
580 const char*
581 anjuta_plugin_handle_get_id (AnjutaPluginHandle *plugin_handle)
583 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
584 return plugin_handle->priv->id;
587 const char*
588 anjuta_plugin_handle_get_name (AnjutaPluginHandle *plugin_handle)
590 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
591 return plugin_handle->priv->name;
594 const char*
595 anjuta_plugin_handle_get_about (AnjutaPluginHandle *plugin_handle)
597 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
598 return plugin_handle->priv->about;
601 const char*
602 anjuta_plugin_handle_get_icon_path (AnjutaPluginHandle *plugin_handle)
604 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
605 return plugin_handle->priv->icon_path;
608 const char*
609 anjuta_plugin_handle_get_path (AnjutaPluginHandle *plugin_handle)
611 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
612 return plugin_handle->priv->path;
615 gboolean
616 anjuta_plugin_handle_get_user_activatable (AnjutaPluginHandle *plugin_handle)
618 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
619 return plugin_handle->priv->user_activatable;
622 gboolean
623 anjuta_plugin_handle_get_resident (AnjutaPluginHandle *plugin_handle)
625 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
626 return plugin_handle->priv->resident;
629 const char*
630 anjuta_plugin_handle_get_language (AnjutaPluginHandle *plugin_handle)
632 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
633 return plugin_handle->priv->language;
636 AnjutaPluginDescription*
637 anjuta_plugin_handle_get_description (AnjutaPluginHandle *plugin_handle)
639 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
640 return plugin_handle->priv->description;
643 GList*
644 anjuta_plugin_handle_get_dependency_names (AnjutaPluginHandle *plugin_handle)
646 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
647 return plugin_handle->priv->dependency_names;
650 GHashTable*
651 anjuta_plugin_handle_get_dependencies (AnjutaPluginHandle *plugin_handle)
653 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
654 return plugin_handle->priv->dependencies;
657 GHashTable*
658 anjuta_plugin_handle_get_dependents (AnjutaPluginHandle *plugin_handle)
660 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
661 return plugin_handle->priv->dependents;
664 GList*
665 anjuta_plugin_handle_get_interfaces (AnjutaPluginHandle *plugin_handle)
667 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
668 return plugin_handle->priv->interfaces;
671 gboolean
672 anjuta_plugin_handle_get_can_load (AnjutaPluginHandle *plugin_handle)
674 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
675 return plugin_handle->priv->can_load;
678 gboolean
679 anjuta_plugin_handle_get_checked (AnjutaPluginHandle *plugin_handle)
681 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
682 return plugin_handle->priv->checked;
685 gint
686 anjuta_plugin_handle_get_resolve_pass (AnjutaPluginHandle *plugin_handle)
688 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), 0);
689 return plugin_handle->priv->resolve_pass;
692 void
693 anjuta_plugin_handle_set_can_load (AnjutaPluginHandle *plugin_handle,
694 gboolean can_load)
696 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
697 plugin_handle->priv->can_load = can_load;
700 void
701 anjuta_plugin_handle_set_checked (AnjutaPluginHandle *plugin_handle,
702 gboolean checked)
704 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
705 plugin_handle->priv->checked = checked;
708 void
709 anjuta_plugin_handle_set_resolve_pass (AnjutaPluginHandle *plugin_handle,
710 gboolean resolve_pass)
712 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
713 plugin_handle->priv->resolve_pass = resolve_pass;
716 static gboolean
717 g_hashtable_foreach_true (gpointer key, gpointer value, gpointer user_data)
719 return TRUE;
722 void
723 anjuta_plugin_handle_unresolve_dependencies (AnjutaPluginHandle *plugin_handle)
725 AnjutaPluginHandlePriv *priv;
727 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
728 priv = plugin_handle->priv;
730 g_hash_table_foreach_remove (priv->dependencies, g_hashtable_foreach_true,
731 NULL);
732 g_hash_table_foreach_remove (priv->dependents, g_hashtable_foreach_true,
733 NULL);
734 priv->can_load = TRUE;
735 priv->resolve_pass = -1;