1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
3 * SECTION:anjuta-glue-factory
4 * @short_description: Underlying plugin factory
7 * @include: libanjuta/anjuta-glue-factory.h
11 #include "anjuta-glue-factory.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
27 struct _AnjutaGlueFactoryClass
29 GObjectClass parent_class
;
33 anjuta_glue_factory_get_type (void)
35 static GType type
= 0;
39 static const GTypeInfo type_info
=
41 sizeof (AnjutaGlueFactoryClass
),
43 (GBaseFinalizeFunc
) NULL
,
44 (GClassInitFunc
) anjuta_glue_factory_class_init
,
45 (GClassFinalizeFunc
) NULL
,
48 sizeof (AnjutaGlueFactory
),
50 (GInstanceInitFunc
) anjuta_glue_factory_init
,
53 type
= g_type_register_static (G_TYPE_OBJECT
,
61 anjuta_glue_factory_class_init (AnjutaGlueFactoryClass
*class)
66 anjuta_glue_factory_init (AnjutaGlueFactory
*factory
)
71 anjuta_glue_factory_new (void)
73 AnjutaGlueFactory
*factory
;
75 factory
= g_object_new (anjuta_glue_factory_get_type (), NULL
);
81 anjuta_glue_factory_add_path (AnjutaGlueFactory
*factory
, const gchar
*path
)
86 if (!g_file_test (path
, G_FILE_TEST_EXISTS
| G_FILE_TEST_IS_DIR
))
89 /* Check if the path has been added */
94 PathEntry
*entry
= p
->data
;
96 /* If it's already added we return TRUE */
97 if (strcmp (path
, entry
->path
) == 0)
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
);
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
)
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
)
139 load_plugin (AnjutaGlueFactory
*factory
, const gchar
*component_name
, const gchar
*type_name
, AnjutaGluePlugin
*glue
)
142 const gchar
*plugin_name
;
144 AnjutaPluginDescription
*plugin
;
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
;
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)
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
),
185 /* Plugin file not found, free glue object */
186 g_object_unref (G_OBJECT (glue
));
188 return G_TYPE_INVALID
;
192 anjuta_glue_factory_get_object_type (AnjutaGlueFactory
*factory
,
193 const gchar
*component_name
,
194 const gchar
*type_name
,
198 AnjutaGluePlugin
*glue
;
200 glue
= get_already_loaded_component (factory
, component_name
, type_name
);
204 AnjutaGluePlugin
*glue
;
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
);
213 return anjuta_glue_plugin_get_component_type (glue
, ANJUTA_TYPE_PLUGIN
, type_name
);