project-manager: Allow hidden node in the tree view (for object node)
[anjuta.git] / plugins / language-manager / plugin.c
blob4a0ca73e2dea36349d0e3784d299c73413458e00
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3 * plugin.c
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
5 *
6 * plugin.c is free software.
7 *
8 * You may redistribute it and/or modify it under the terms of the
9 * GNU General Public License, as published by the Free Software
10 * Foundation; either version 2 of the License, or (at your option)
11 * any later version.
13 * plugin.c is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
16 * See the GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with plugin.c. If not, write to:
20 * The Free Software Foundation, Inc.,
21 * 51 Franklin Street, Fifth Floor
22 * Boston, MA 02110-1301, USA.
25 #include <config.h>
26 #include <libanjuta/anjuta-shell.h>
27 #include <libanjuta/anjuta-debug.h>
28 #include <libanjuta/interfaces/ianjuta-language.h>
30 #include <libxml/parser.h>
31 #include <libxml/tree.h>
33 #include "plugin.h"
34 #define LANG_FILE PACKAGE_DATA_DIR"/languages.xml"
36 #define LANGUAGE_MANAGER(o) (LanguageManager*) (o)
38 typedef struct _Language Language;
40 struct _Language
42 IAnjutaLanguageId id;
43 gchar* name;
44 gchar* make_target;
45 GList* strings;
46 GList* mime_types;
50 static gpointer parent_class;
53 static void
54 language_destroy (gpointer data)
56 Language* lang = (Language*) data;
58 g_free(lang->name);
59 g_list_foreach(lang->strings, (GFunc) g_free, NULL);
60 g_list_foreach(lang->mime_types, (GFunc) g_free, NULL);
62 g_free(lang);
65 static void
66 load_languages (LanguageManager* language_manager)
68 xmlDocPtr doc;
69 xmlNodePtr root;
70 xmlNodePtr cur_node;
72 LIBXML_TEST_VERSION
74 doc = xmlReadFile (LANG_FILE, NULL, 0);
75 if (!doc)
77 DEBUG_PRINT("Could not read language xml file %s!", LANG_FILE);
78 return;
80 root = xmlDocGetRootElement (doc);
82 if (!g_str_equal (root->name, "languages"))
84 DEBUG_PRINT ("%s", "Invalid languages.xml file");
87 for (cur_node = root->children; cur_node != NULL; cur_node = cur_node->next)
89 Language* lang = g_new0(Language, 1);
90 gchar* id;
91 gchar* mime_types;
92 gchar* strings;
94 if (!g_str_equal (cur_node->name, (const xmlChar*) "language"))
95 continue;
97 id = (gchar*) xmlGetProp (cur_node, (const xmlChar*) "id");
98 lang->id = atoi(id);
99 lang->name = (gchar*) xmlGetProp(cur_node, (const xmlChar*) "name");
100 lang->make_target = (gchar*) xmlGetProp (cur_node, (const xmlChar*) "make-target");
101 mime_types = (gchar*) xmlGetProp (cur_node, (const xmlChar*) "mime-types");
102 strings = (gchar*) xmlGetProp (cur_node, (const xmlChar*) "strings");
103 if (lang->id != 0 && lang->name && mime_types && strings)
105 GStrv mime_typesv = g_strsplit(mime_types, ",", -1);
106 GStrv stringsv = g_strsplit (strings, ",", -1);
107 GStrv type;
108 GStrv string;
110 for (type = mime_typesv; *type != NULL; type++)
112 lang->mime_types = g_list_append (lang->mime_types, g_strdup(*type));
114 g_strfreev(mime_typesv);
116 for (string = stringsv; *string != NULL; string++)
118 lang->strings = g_list_append (lang->strings, g_strdup(*string));
120 g_strfreev(stringsv);
122 g_hash_table_insert (language_manager->languages, GINT_TO_POINTER(lang->id), lang);
124 else
126 g_free(lang);
128 g_free (id);
129 g_free (mime_types);
130 g_free (strings);
132 xmlFreeDoc(doc);
135 static gboolean
136 language_manager_activate (AnjutaPlugin *plugin)
138 LanguageManager *language_manager;
140 DEBUG_PRINT ("%s", "LanguageManager: Activating LanguageManager plugin ...");
141 language_manager = (LanguageManager*) plugin;
143 load_languages(language_manager);
145 return TRUE;
148 static gboolean
149 language_manager_deactivate (AnjutaPlugin *plugin)
151 DEBUG_PRINT ("%s", "LanguageManager: Dectivating LanguageManager plugin ...");
153 return TRUE;
156 static void
157 language_manager_finalize (GObject *obj)
159 /* Finalization codes here */
160 G_OBJECT_CLASS (parent_class)->finalize (obj);
163 static void
164 language_manager_dispose (GObject *obj)
166 /* Disposition codes */
167 LanguageManager* lang = LANGUAGE_MANAGER (obj);
169 g_hash_table_unref (lang->languages);
170 G_OBJECT_CLASS (parent_class)->dispose (obj);
173 static void
174 language_manager_instance_init (GObject *obj)
176 LanguageManager *plugin = (LanguageManager*)obj;
177 plugin->languages = g_hash_table_new_full (NULL, NULL,
178 NULL, language_destroy);
182 static void
183 language_manager_class_init (GObjectClass *klass)
185 AnjutaPluginClass *plugin_class = ANJUTA_PLUGIN_CLASS (klass);
187 parent_class = g_type_class_peek_parent (klass);
189 plugin_class->activate = language_manager_activate;
190 plugin_class->deactivate = language_manager_deactivate;
191 klass->finalize = language_manager_finalize;
192 klass->dispose = language_manager_dispose;
195 typedef struct
197 LanguageManager* lang;
198 const gchar* target;
199 IAnjutaLanguageId result_id;
200 gboolean found;
201 } LangData;
203 static void
204 language_manager_find_mime_type (gpointer key, Language* language, LangData* data)
206 if (data->found)
207 return;
208 if (g_list_find_custom (language->mime_types, data->target, (GCompareFunc) strcmp))
210 data->result_id = language->id;
211 data->found = TRUE;
215 static void
216 language_manager_find_string (gpointer key, Language* language, LangData* data)
218 if (data->found)
219 return;
220 if (g_list_find_custom (language->strings, data->target, (GCompareFunc) g_ascii_strcasecmp))
222 DEBUG_PRINT ("Found language %i: %s", language->id, language->name);
223 data->result_id = language->id;
224 data->found = TRUE;
228 static IAnjutaLanguageId
229 ilanguage_get_from_mime_type (IAnjutaLanguage* ilang, const gchar* mime_type, GError** e)
231 if (!mime_type)
232 return 0;
233 LanguageManager* lang = LANGUAGE_MANAGER(ilang);
234 LangData* data = g_new0(LangData, 1);
235 IAnjutaLanguageId ret_id;
236 data->target = mime_type;
237 g_hash_table_foreach (lang->languages, (GHFunc)language_manager_find_mime_type, data);
238 if (data->found)
240 ret_id = data->result_id;
242 else
244 DEBUG_PRINT ("Unknown mime-type = %s", mime_type);
245 ret_id = 0;
247 g_free(data);
248 return ret_id;
251 static IAnjutaLanguageId
252 ilanguage_get_from_string (IAnjutaLanguage* ilang, const gchar* string, GError** e)
254 if (!string)
255 return 0;
256 LanguageManager* lang = LANGUAGE_MANAGER(ilang);
257 LangData* data = g_new0(LangData, 1);
258 IAnjutaLanguageId ret_id;
259 data->target = string;
260 g_hash_table_foreach (lang->languages, (GHFunc)language_manager_find_string, data);
261 if (data->found)
263 ret_id = data->result_id;
265 else
267 DEBUG_PRINT ("Unknown language string = %s", string);
268 ret_id = 0;
270 g_free(data);
271 return ret_id;
274 static const gchar*
275 ilanguage_get_name (IAnjutaLanguage* ilang, IAnjutaLanguageId id, GError** e)
277 LanguageManager* lang = LANGUAGE_MANAGER(ilang);
278 Language* language = g_hash_table_lookup (lang->languages,
279 GINT_TO_POINTER(id));
281 if (language)
283 return language->name;
284 DEBUG_PRINT ("Found language: %s", language->name);
286 else
287 return NULL;
290 static GList*
291 ilanguage_get_strings (IAnjutaLanguage* ilang, IAnjutaLanguageId id, GError** e)
293 LanguageManager* lang = LANGUAGE_MANAGER(ilang);
294 Language* language = g_hash_table_lookup (lang->languages,
295 GINT_TO_POINTER(id));
296 if (language)
297 return language->strings;
298 else
299 return NULL;
302 static const char*
303 ilanguage_get_make_target (IAnjutaLanguage* ilang, IAnjutaLanguageId id, GError** e)
305 LanguageManager* lang = LANGUAGE_MANAGER(ilang);
306 Language* language = g_hash_table_lookup (lang->languages,
307 GINT_TO_POINTER(id));
308 if (language)
309 return language->make_target;
310 else
311 return NULL;
315 static IAnjutaLanguageId
316 ilanguage_get_from_editor (IAnjutaLanguage* ilang, IAnjutaEditorLanguage* editor, GError** e)
318 const gchar* language =
319 ianjuta_editor_language_get_language (editor, e);
321 IAnjutaLanguageId id =
322 ilanguage_get_from_string (ilang, language, e);
324 return id;
327 static const gchar*
328 ilanguage_get_name_from_editor (IAnjutaLanguage* ilang, IAnjutaEditorLanguage* editor, GError** e)
330 return ilanguage_get_name (ilang,
331 ilanguage_get_from_editor (ilang, editor, e), e);
334 static GList*
335 ilanguage_get_languages (IAnjutaLanguage* ilang, GError** e)
337 LanguageManager* lang = LANGUAGE_MANAGER(ilang);
338 return g_hash_table_get_keys (lang->languages);
341 static void
342 ilanguage_iface_init (IAnjutaLanguageIface* iface)
344 iface->get_from_mime_type = ilanguage_get_from_mime_type;
345 iface->get_from_string = ilanguage_get_from_string;
346 iface->get_name = ilanguage_get_name;
347 iface->get_strings = ilanguage_get_strings;
348 iface->get_make_target = ilanguage_get_make_target;
349 iface->get_from_editor = ilanguage_get_from_editor;
350 iface->get_name_from_editor = ilanguage_get_name_from_editor;
351 iface->get_languages = ilanguage_get_languages;
354 ANJUTA_PLUGIN_BEGIN (LanguageManager, language_manager);
355 ANJUTA_PLUGIN_ADD_INTERFACE (ilanguage, IANJUTA_TYPE_LANGUAGE);
356 ANJUTA_PLUGIN_END
358 ANJUTA_SIMPLE_PLUGIN (LanguageManager, language_manager);