Updated Spanish translation
[anjuta-git-plugin.git] / libanjuta / anjuta-glue-factory.c
blobfe3979e534f8c8a854c5d8cceee6f5c986847f4b
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /**
3 * SECTION:anjuta-glue-factory
4 * @short_description: Underlying plugin factory
5 * @see_also:
6 * @stability: Unstable
7 * @include: libanjuta/anjuta-glue-factory.h
8 *
9 */
11 #include "anjuta-glue-factory.h"
13 #include <string.h>
15 #include "anjuta-plugin.h"
17 static void anjuta_glue_factory_init (AnjutaGlueFactory *factory);
18 static void anjuta_glue_factory_class_init (AnjutaGlueFactoryClass *class);
20 struct _AnjutaGlueFactory
22 GObject parent;
24 GList *paths;
27 struct _AnjutaGlueFactoryClass
29 GObjectClass parent_class;
32 GType
33 anjuta_glue_factory_get_type (void)
35 static GType type = 0;
37 if (!type)
39 static const GTypeInfo type_info =
41 sizeof (AnjutaGlueFactoryClass),
42 (GBaseInitFunc) NULL,
43 (GBaseFinalizeFunc) NULL,
44 (GClassInitFunc) anjuta_glue_factory_class_init,
45 (GClassFinalizeFunc) NULL,
46 NULL,
48 sizeof (AnjutaGlueFactory),
49 0, /* n_preallocs */
50 (GInstanceInitFunc) anjuta_glue_factory_init,
53 type = g_type_register_static (G_TYPE_OBJECT,
54 "AnjutaGlueFactory",
55 &type_info, 0);
57 return type;
60 static void
61 anjuta_glue_factory_class_init (AnjutaGlueFactoryClass *class)
65 static void
66 anjuta_glue_factory_init (AnjutaGlueFactory *factory)
70 AnjutaGlueFactory *
71 anjuta_glue_factory_new (void)
73 AnjutaGlueFactory *factory;
75 factory = g_object_new (anjuta_glue_factory_get_type (), NULL);
77 return factory;
80 gboolean
81 anjuta_glue_factory_add_path (AnjutaGlueFactory *factory, const gchar *path)
83 GList *p;
84 PathEntry *entry;
86 if (!g_file_test (path, G_FILE_TEST_EXISTS | G_FILE_TEST_IS_DIR))
87 return FALSE;
89 /* Check if the path has been added */
90 p = factory->paths;
92 while (p)
94 PathEntry *entry = p->data;
96 /* If it's already added we return TRUE */
97 if (strcmp (path, entry->path) == 0)
98 return TRUE;
100 p = p->next;
103 entry = g_new (PathEntry, 1);
104 entry->path = g_strdup (path);
105 entry->loaded_plugins = g_hash_table_new_full (NULL, NULL, g_free, g_object_unref);
107 factory->paths = g_list_prepend (factory->paths, entry);
109 return TRUE;
112 GList* anjuta_glue_factory_get_path(AnjutaGlueFactory *factory)
114 return factory->paths;
117 static AnjutaGluePlugin *
118 get_already_loaded_component (AnjutaGlueFactory *factory,
119 const gchar *component_name,
120 const gchar *type_name)
122 GList *p;
124 for (p = factory->paths; p != NULL; p = p->next)
126 PathEntry *entry = p->data;
127 AnjutaGluePlugin *plugin;
129 plugin = g_hash_table_lookup (entry->loaded_plugins, component_name);
131 if (plugin && anjuta_glue_plugin_get_component_type (plugin, ANJUTA_TYPE_PLUGIN, type_name) != G_TYPE_INVALID)
132 return plugin;
135 return NULL;
138 static GType
139 load_plugin (AnjutaGlueFactory *factory, const gchar *component_name, const gchar *type_name, AnjutaGluePlugin *glue)
141 GList *p;
142 const gchar *plugin_name;
143 GList *descs = NULL;
144 AnjutaPluginDescription *plugin;
145 gchar *value;
147 plugin_name = anjuta_glue_plugin_build_component_path (glue, NULL, component_name);
148 for (p = factory->paths; p != NULL; p = p->next)
150 const gchar *file_name;
151 PathEntry *entry = p->data;
152 GDir *dir;
154 dir = g_dir_open (entry->path, 0, NULL);
155 if (dir == NULL) continue;
157 /* Search for corresponding file */
158 for(file_name = g_dir_read_name (dir); file_name != NULL; file_name = g_dir_read_name (dir))
160 if (file_name && strcmp (file_name, plugin_name) == 0)
162 GType type;
164 /* We have found a matching component */
165 anjuta_glue_plugin_build_component_path (glue, entry->path, component_name);
167 /* Load module to check if type really exist */
168 g_type_module_use (G_TYPE_MODULE (glue));
169 type = anjuta_glue_plugin_get_component_type (glue, ANJUTA_TYPE_PLUGIN, type_name);
170 g_type_module_unuse (G_TYPE_MODULE (glue));
171 if (type != G_TYPE_INVALID)
173 g_hash_table_insert (entry->loaded_plugins,
174 (gpointer)strdup (component_name),
175 glue);
177 return type;
179 break;
182 g_dir_close (dir);
185 /* Plugin file not found, free glue object */
186 g_object_unref (G_OBJECT (glue));
188 return G_TYPE_INVALID;
191 GType
192 anjuta_glue_factory_get_object_type (AnjutaGlueFactory *factory,
193 const gchar *component_name,
194 const gchar *type_name,
195 gboolean resident,
196 GType language)
198 AnjutaGluePlugin *glue;
200 glue = get_already_loaded_component (factory, component_name, type_name);
202 if (!glue)
204 AnjutaGluePlugin *glue;
205 GType type;
207 glue = (AnjutaGluePlugin *)g_object_new (language, NULL);
208 anjuta_glue_plugin_set_resident (glue, resident);
209 return load_plugin (factory, component_name, type_name, glue);
211 else
213 return anjuta_glue_plugin_get_component_type (glue, ANJUTA_TYPE_PLUGIN, type_name);