1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
4 * Copyright (C) Johannes Schmid 2007 <jhs@gnome.org>
6 * plugin.c is free software.
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)
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.
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>
34 #define LANG_FILE PACKAGE_DATA_DIR"/languages.xml"
36 #define LANGUAGE_MANAGER(o) (LanguageManager*) (o)
38 typedef struct _Language Language
;
50 static gpointer parent_class
;
54 language_destroy (gpointer data
)
56 Language
* lang
= (Language
*) data
;
59 g_list_foreach(lang
->strings
, (GFunc
) g_free
, NULL
);
60 g_list_foreach(lang
->mime_types
, (GFunc
) g_free
, NULL
);
66 load_languages (LanguageManager
* language_manager
)
74 doc
= xmlReadFile (LANG_FILE
, NULL
, 0);
77 DEBUG_PRINT("Could not read language xml file %s!", LANG_FILE
);
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);
94 if (!g_str_equal (cur_node
->name
, (const xmlChar
*) "language"))
97 id
= (gchar
*) xmlGetProp (cur_node
, (const xmlChar
*) "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);
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
);
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
);
149 language_manager_deactivate (AnjutaPlugin
*plugin
)
151 DEBUG_PRINT ("%s", "LanguageManager: Dectivating LanguageManager plugin ...");
157 language_manager_finalize (GObject
*obj
)
159 /* Finalization codes here */
160 G_OBJECT_CLASS (parent_class
)->finalize (obj
);
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
);
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
);
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
;
197 LanguageManager
* lang
;
199 IAnjutaLanguageId result_id
;
204 language_manager_find_mime_type (gpointer key
, Language
* language
, LangData
* data
)
208 if (g_list_find_custom (language
->mime_types
, data
->target
, (GCompareFunc
) strcmp
))
210 data
->result_id
= language
->id
;
216 language_manager_find_string (gpointer key
, Language
* language
, LangData
* data
)
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
;
228 static IAnjutaLanguageId
229 ilanguage_get_from_mime_type (IAnjutaLanguage
* ilang
, const gchar
* mime_type
, GError
** e
)
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
);
240 ret_id
= data
->result_id
;
244 DEBUG_PRINT ("Unknown mime-type = %s", mime_type
);
251 static IAnjutaLanguageId
252 ilanguage_get_from_string (IAnjutaLanguage
* ilang
, const gchar
* string
, GError
** e
)
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
);
263 ret_id
= data
->result_id
;
267 DEBUG_PRINT ("Unknown language string = %s", string
);
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
));
283 return language
->name
;
284 DEBUG_PRINT ("Found language: %s", language
->name
);
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
));
297 return language
->strings
;
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
));
309 return language
->make_target
;
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
);
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
);
335 ilanguage_get_languages (IAnjutaLanguage
* ilang
, GError
** e
)
337 LanguageManager
* lang
= LANGUAGE_MANAGER(ilang
);
338 return g_hash_table_get_keys (lang
->languages
);
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
);
358 ANJUTA_SIMPLE_PLUGIN (LanguageManager
, language_manager
);