* data/default.proflie, data/Makefile.am, src/anjuta.c,
[anjuta-git-plugin.git] / libanjuta / anjuta-plugin-handle.c
blobe3e5bef026ce4c784243d9d1f1f47c9becc2f927
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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <string.h>
22 #include "anjuta-plugin-handle.h"
23 #include "resources.h"
24 #include "anjuta-debug.h"
26 enum
28 PROP_0,
30 PROP_ID,
31 PROP_NAME,
32 PROP_ABOUT,
33 PROP_ICON_PATH,
34 PROP_USER_ACTIVATABLE,
35 PROP_DESCRIPTION,
36 PROP_DEPENDENCY_NAMES,
37 PROP_DEPENDENCIES,
38 PROP_DEPENDENTS,
39 PROP_INTERFACES,
40 PROP_CAN_LOAD,
41 PROP_CHECKED,
42 PROP_RESOLVE_PASS
45 struct _AnjutaPluginHandlePriv
47 char *id;
48 char *name;
49 char *about;
50 char *icon_path;
51 gboolean user_activatable;
53 AnjutaPluginDescription *description;
55 /* The dependencies listed in the oaf file */
56 GList *dependency_names;
58 /* Interfaces exported by this plugin */
59 GList *interfaces;
61 /* Attributes defined for this plugin */
62 /* GHashTable *attributes; */
64 /* The keys of these tables represent the dependencies and dependents
65 * of the module. The values point back at the plugins */
66 GHashTable *dependencies;
67 GHashTable *dependents;
69 gboolean can_load;
70 gboolean checked;
72 /* The pass on which the module was resolved, or -1 if
73 * unresolved */
74 int resolve_pass;
77 static GObjectClass* parent_class = NULL;
79 static void
80 anjuta_plugin_handle_init (AnjutaPluginHandle *object)
82 object->priv = g_new0 (AnjutaPluginHandlePriv, 1);
84 object->priv->resolve_pass = -1;
86 object->priv->dependencies = g_hash_table_new (g_direct_hash,
87 g_direct_equal);
88 object->priv->dependents = g_hash_table_new (g_direct_hash,
89 g_direct_equal);
92 static void
93 anjuta_plugin_handle_finalize (GObject *object)
95 AnjutaPluginHandlePriv *priv;
97 priv = ANJUTA_PLUGIN_HANDLE (object)->priv;
99 g_free (priv->id);
100 priv->id = NULL;
101 g_free (priv->name);
102 priv->name = NULL;
103 g_free (priv->icon_path);
104 priv->icon_path = NULL;
106 g_list_foreach (priv->dependency_names, (GFunc)g_free, NULL);
107 g_list_free (priv->dependency_names);
108 priv->dependency_names = NULL;
110 g_list_foreach (priv->interfaces, (GFunc)g_free, NULL);
111 g_list_free (priv->dependency_names);
112 priv->dependency_names = NULL;
114 g_hash_table_destroy (priv->dependencies);
115 priv->dependencies = NULL;
116 g_hash_table_destroy (priv->dependents);
117 priv->dependents = NULL;
119 g_free (priv);
121 G_OBJECT_CLASS (parent_class)->finalize (object);
124 static void
125 anjuta_plugin_handle_set_property (GObject *object, guint prop_id,
126 const GValue *value, GParamSpec *pspec)
128 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (object));
130 switch (prop_id)
132 #if 0
133 case PROP_ID:
134 /* TODO: Add setter for "id" property here */
135 break;
136 case PROP_NAME:
137 /* TODO: Add setter for "name" property here */
138 break;
139 case PROP_ABOUT:
140 /* TODO: Add setter for "about" property here */
141 break;
142 case PROP_ICON_PATH:
143 /* TODO: Add setter for "icon-path" property here */
144 break;
145 case PROP_USER_ACTIVATABLE:
146 /* TODO: Add setter for "user-activatable" property here */
147 break;
148 case PROP_DESCRIPTION:
149 /* TODO: Add setter for "description" property here */
150 break;
151 case PROP_DEPENDENCY_NAMES:
152 /* TODO: Add setter for "dependency-names" property here */
153 break;
154 case PROP_DEPENDENCIES:
155 /* TODO: Add setter for "dependencies" property here */
156 break;
157 case PROP_DEPENDENTS:
158 /* TODO: Add setter for "dependents" property here */
159 break;
160 case PROP_INTERFACES:
161 /* TODO: Add setter for "interfaces" property here */
162 break;
163 case PROP_CAN_LOAD:
164 /* TODO: Add setter for "can-load" property here */
165 break;
166 case PROP_CHECKED:
167 /* TODO: Add setter for "checked" property here */
168 break;
169 case PROP_RESOLVE_PASS:
170 /* TODO: Add setter for "resolve-pass" property here */
171 break;
172 #endif
173 default:
174 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
175 break;
179 static void
180 anjuta_plugin_handle_get_property (GObject *object, guint prop_id,
181 GValue *value, GParamSpec *pspec)
183 AnjutaPluginHandlePriv *priv;
185 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (object));
186 priv = ANJUTA_PLUGIN_HANDLE (object)->priv;
187 switch (prop_id)
189 case PROP_ID:
190 g_value_set_string (value, priv->id);
191 break;
192 case PROP_NAME:
193 g_value_set_string (value, priv->name);
194 break;
195 case PROP_ABOUT:
196 g_value_set_string (value, priv->about);
197 break;
198 case PROP_ICON_PATH:
199 g_value_set_string (value, priv->icon_path);
200 break;
201 case PROP_USER_ACTIVATABLE:
202 g_value_set_boolean (value, priv->user_activatable);
203 break;
204 case PROP_DESCRIPTION:
205 g_value_set_pointer (value, priv->description);
206 break;
207 case PROP_DEPENDENCY_NAMES:
208 g_value_set_pointer (value, priv->dependency_names);
209 break;
210 case PROP_DEPENDENCIES:
211 g_value_set_pointer (value, priv->dependencies);
212 break;
213 case PROP_DEPENDENTS:
214 g_value_set_pointer (value, priv->dependents);
215 break;
216 case PROP_INTERFACES:
217 g_value_set_pointer (value, priv->interfaces);
218 break;
219 case PROP_CAN_LOAD:
220 g_value_set_boolean (value, priv->can_load);
221 break;
222 case PROP_CHECKED:
223 g_value_set_boolean (value, priv->checked);
224 break;
225 case PROP_RESOLVE_PASS:
226 g_value_set_int (value, priv->resolve_pass);
227 break;
228 default:
229 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
230 break;
234 static void
235 anjuta_plugin_handle_class_init (AnjutaPluginHandleClass *klass)
237 GObjectClass* object_class = G_OBJECT_CLASS (klass);
238 parent_class = G_OBJECT_CLASS (g_type_class_peek_parent (klass));
240 object_class->finalize = anjuta_plugin_handle_finalize;
241 object_class->set_property = anjuta_plugin_handle_set_property;
242 object_class->get_property = anjuta_plugin_handle_get_property;
244 g_object_class_install_property (object_class,
245 PROP_ID,
246 g_param_spec_string ("id",
247 "ID",
248 "Unique plugin ID",
249 NULL,
250 G_PARAM_READABLE));
252 g_object_class_install_property (object_class,
253 PROP_NAME,
254 g_param_spec_string ("name",
255 "Name",
256 "Plugin name",
257 NULL,
258 G_PARAM_READABLE));
260 g_object_class_install_property (object_class,
261 PROP_ABOUT,
262 g_param_spec_string ("about",
263 "About Plugin",
264 "About description of the plugin",
265 NULL,
266 G_PARAM_READABLE));
268 g_object_class_install_property (object_class,
269 PROP_ICON_PATH,
270 g_param_spec_string ("icon-path",
271 "Icon Path",
272 "Icon path of the plugin",
273 NULL,
274 G_PARAM_READABLE));
276 g_object_class_install_property (object_class,
277 PROP_USER_ACTIVATABLE,
278 g_param_spec_boolean ("user-activatable",
279 "User Activatable",
280 "If the plugin is user activatable",
281 FALSE,
282 G_PARAM_READABLE));
284 g_object_class_install_property (object_class,
285 PROP_DESCRIPTION,
286 g_param_spec_pointer ("description",
287 "Description",
288 "Plugin description",
289 G_PARAM_READABLE));
291 g_object_class_install_property (object_class,
292 PROP_DEPENDENCY_NAMES,
293 g_param_spec_pointer ("dependency-names",
294 "Dependency names",
295 "Plugin dependency names listed in oaf file",
296 G_PARAM_READABLE));
298 g_object_class_install_property (object_class,
299 PROP_DEPENDENCIES,
300 g_param_spec_pointer ("dependencies",
301 "Dependencies",
302 "Plugin dependencies",
303 G_PARAM_READABLE));
305 g_object_class_install_property (object_class,
306 PROP_DEPENDENTS,
307 g_param_spec_pointer ("dependents",
308 "Dependents",
309 "Plugin dependents",
310 G_PARAM_READABLE));
312 g_object_class_install_property (object_class,
313 PROP_INTERFACES,
314 g_param_spec_pointer ("interfaces",
315 "Interfaces",
316 "Interfaces exported by the plugin",
317 G_PARAM_READABLE));
319 g_object_class_install_property (object_class,
320 PROP_CAN_LOAD,
321 g_param_spec_boolean ("can-load",
322 "Can Load",
323 "If the plugin can be loaded",
324 FALSE,
325 G_PARAM_READABLE));
327 g_object_class_install_property (object_class,
328 PROP_CHECKED,
329 g_param_spec_boolean ("checked",
330 "Checked",
331 "If the plugin is checked in UI",
332 FALSE,
333 G_PARAM_READABLE));
335 g_object_class_install_property (object_class,
336 PROP_RESOLVE_PASS,
337 g_param_spec_int ("resolve-pass",
338 "Resolve Pass",
339 "Dependency resolution pass",
340 G_MININT, /* TODO: Adjust minimum property value */
341 G_MAXINT, /* TODO: Adjust maximum property value */
343 G_PARAM_READABLE));
346 GType
347 anjuta_plugin_handle_get_type (void)
349 static GType our_type = 0;
351 if(our_type == 0)
353 static const GTypeInfo our_info =
355 sizeof (AnjutaPluginHandleClass), /* class_size */
356 (GBaseInitFunc) NULL, /* base_init */
357 (GBaseFinalizeFunc) NULL, /* base_finalize */
358 (GClassInitFunc) anjuta_plugin_handle_class_init, /* class_init */
359 (GClassFinalizeFunc) NULL, /* class_finalize */
360 NULL /* class_data */,
361 sizeof (AnjutaPluginHandle), /* instance_size */
362 0, /* n_preallocs */
363 (GInstanceInitFunc) anjuta_plugin_handle_init, /* instance_init */
364 NULL /* value_table */
367 our_type = g_type_register_static (G_TYPE_OBJECT, "AnjutaPluginHandle",
368 &our_info, 0);
371 return our_type;
375 static char *
376 get_icon_path (char *icon_name)
378 char *ret;
380 if (g_path_is_absolute (icon_name)) {
381 ret = g_strdup (icon_name);
382 } else {
383 ret = anjuta_res_get_pixmap_file (icon_name);
386 return ret;
389 static GList *
390 property_to_list (const char *value)
392 GList *l = NULL;
393 char **split_str;
394 char **p;
396 split_str = g_strsplit (value, ",", -1);
397 for (p = split_str; *p != NULL; p++) {
398 l = g_list_prepend (l, g_strdup (g_strstrip (*p)));
400 g_strfreev (split_str);
401 return l;
404 AnjutaPluginHandle*
405 anjuta_plugin_handle_new (const gchar *plugin_desc_path)
407 AnjutaPluginHandle *plugin_handle;
408 AnjutaPluginDescription *desc;
409 char *str;
410 gchar *contents;
411 gboolean success = TRUE;
413 /* Load file content */
414 if (g_file_get_contents (plugin_desc_path, &contents, NULL, NULL)) {
416 desc = anjuta_plugin_description_new_from_string (contents, NULL);
417 g_free (contents);
418 if (!desc) {
419 g_warning ("Bad plugin file: %s\n", plugin_desc_path);
420 return NULL;
423 else
425 return NULL;
428 plugin_handle = g_object_new (ANJUTA_TYPE_PLUGIN_HANDLE, NULL);
430 /* Initialize plugin handle */
431 plugin_handle->priv->description = desc;
432 plugin_handle->priv->user_activatable = TRUE;
434 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
435 "Location", &str)) {
436 plugin_handle->priv->id = str;
437 } else {
438 g_warning ("Couldn't find 'Location'");
439 success = FALSE;
442 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
443 "Name", &str)) {
444 plugin_handle->priv->name = str;
445 } else {
446 g_warning ("couldn't find 'Name' attribute.");
447 success = FALSE;
450 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
451 "Description", &str)) {
452 plugin_handle->priv->about = str;
453 } else {
454 g_warning ("Couldn't find 'Description' attribute.");
455 success = FALSE;
458 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
459 "Icon", &str)) {
460 plugin_handle->priv->icon_path = get_icon_path (str);
461 g_free (str);
464 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
465 "Dependencies",
466 &str)) {
467 plugin_handle->priv->dependency_names = property_to_list (str);
468 g_free (str);
471 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
472 "Interfaces",
473 &str)) {
474 plugin_handle->priv->interfaces = property_to_list (str);
475 g_free (str);
478 if (anjuta_plugin_description_get_string (desc, "Anjuta Plugin",
479 "UserActivatable", &str)) {
480 if (str && strcasecmp (str, "no") == 0)
482 plugin_handle->priv->user_activatable = FALSE;
484 DEBUG_PRINT ("Plugin '%s' is not user activatable",
485 plugin_handle->priv->name?
486 plugin_handle->priv->name : "Unknown");
489 g_free (str);
491 if (!success) {
492 g_object_unref (plugin_handle);
493 plugin_handle = NULL;
496 return plugin_handle;
499 const char*
500 anjuta_plugin_handle_get_id (AnjutaPluginHandle *plugin_handle)
502 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
503 return plugin_handle->priv->id;
506 const char*
507 anjuta_plugin_handle_get_name (AnjutaPluginHandle *plugin_handle)
509 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
510 return plugin_handle->priv->name;
513 const char*
514 anjuta_plugin_handle_get_about (AnjutaPluginHandle *plugin_handle)
516 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
517 return plugin_handle->priv->about;
520 const char*
521 anjuta_plugin_handle_get_icon_path (AnjutaPluginHandle *plugin_handle)
523 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
524 return plugin_handle->priv->icon_path;
527 gboolean
528 anjuta_plugin_handle_get_user_activatable (AnjutaPluginHandle *plugin_handle)
530 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
531 return plugin_handle->priv->user_activatable;
534 AnjutaPluginDescription*
535 anjuta_plugin_handle_get_description (AnjutaPluginHandle *plugin_handle)
537 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
538 return plugin_handle->priv->description;
541 GList*
542 anjuta_plugin_handle_get_dependency_names (AnjutaPluginHandle *plugin_handle)
544 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
545 return plugin_handle->priv->dependency_names;
548 GHashTable*
549 anjuta_plugin_handle_get_dependencies (AnjutaPluginHandle *plugin_handle)
551 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
552 return plugin_handle->priv->dependencies;
555 GHashTable*
556 anjuta_plugin_handle_get_dependents (AnjutaPluginHandle *plugin_handle)
558 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
559 return plugin_handle->priv->dependents;
562 GList*
563 anjuta_plugin_handle_get_interfaces (AnjutaPluginHandle *plugin_handle)
565 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), NULL);
566 return plugin_handle->priv->interfaces;
569 gboolean
570 anjuta_plugin_handle_get_can_load (AnjutaPluginHandle *plugin_handle)
572 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
573 return plugin_handle->priv->can_load;
576 gboolean
577 anjuta_plugin_handle_get_checked (AnjutaPluginHandle *plugin_handle)
579 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), FALSE);
580 return plugin_handle->priv->checked;
583 gint
584 anjuta_plugin_handle_get_resolve_pass (AnjutaPluginHandle *plugin_handle)
586 g_return_val_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle), 0);
587 return plugin_handle->priv->resolve_pass;
590 void
591 anjuta_plugin_handle_set_can_load (AnjutaPluginHandle *plugin_handle,
592 gboolean can_load)
594 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
595 plugin_handle->priv->can_load = can_load;
598 void
599 anjuta_plugin_handle_set_checked (AnjutaPluginHandle *plugin_handle,
600 gboolean checked)
602 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
603 plugin_handle->priv->checked = checked;
606 void
607 anjuta_plugin_handle_set_resolve_pass (AnjutaPluginHandle *plugin_handle,
608 gboolean resolve_pass)
610 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
611 plugin_handle->priv->resolve_pass = resolve_pass;
614 static gboolean
615 g_hashtable_foreach_true (gpointer key, gpointer value, gpointer user_data)
617 return TRUE;
620 void
621 anjuta_plugin_handle_unresolve_dependencies (AnjutaPluginHandle *plugin_handle)
623 AnjutaPluginHandlePriv *priv;
625 g_return_if_fail (ANJUTA_IS_PLUGIN_HANDLE (plugin_handle));
626 priv = plugin_handle->priv;
628 g_hash_table_foreach_remove (priv->dependencies, g_hashtable_foreach_true,
629 NULL);
630 g_hash_table_foreach_remove (priv->dependents, g_hashtable_foreach_true,
631 NULL);
632 priv->can_load = TRUE;
633 priv->resolve_pass = -1;