Add Ctrl+space customization.
[platform/upstream/ibus.git] / src / ibusutil.c
1 /* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
2 /* vim:set et sts=4: */
3 /* bus - The Input Bus
4  * Copyright (C) 2008-2011 Peng Huang <shawn.p.huang@gmail.com>
5  * Copyright (C) 2010-2011 Takao Fujiwara <takao.fujiwara1@gmail.com>
6  * Copyright (C) 2008-2011 Red Hat, Inc.
7  *
8  * This library is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU Lesser General Public
10  * License as published by the Free Software Foundation; either
11  * version 2 of the License, or (at your option) any later version.
12  *
13  * This library 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.  See the GNU
16  * Lesser General Public License for more details.
17  *
18  * You should have received a copy of the GNU Lesser General Public
19  * License along with this library; if not, write to the
20  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
21  * Boston, MA 02111-1307, USA.
22  */
23 #ifdef HAVE_CONFIG_H
24 #include <config.h>
25 #endif
26
27 #include <glib.h>
28 #include <glib/gstdio.h>
29 #include <gio/gio.h>
30 #include <string.h>
31 #include "ibusxml.h"
32
33 #ifdef ENABLE_NLS
34 #include <libintl.h>
35 #endif
36
37 /* gettext macro */
38 #define N_(t) t
39
40 static GHashTable *__languages_dict;
41
42 static gboolean
43 _iso_codes_parse_xml_node (XMLNode          *node)
44 {
45     GList *p;
46     g_assert (node);
47
48     if (G_UNLIKELY (g_strcmp0 (node->name, "iso_639_entries") != 0)) {
49         return FALSE;
50     }
51
52     for (p = node->sub_nodes; p != NULL; p = p->next) {
53         XMLNode *sub_node = (XMLNode *)p->data;
54         gchar **attributes = NULL;
55         int i, j;
56         struct {
57             const gchar *key;
58             gchar *value;
59         } entries[] = {
60             { "iso_639_2B_code", NULL },
61             { "iso_639_2T_code", NULL },
62             { "iso_639_1_code", NULL },
63         };
64
65         if (sub_node->attributes == NULL) {
66             continue;
67         }
68
69         attributes = sub_node->attributes;
70         for (i = 0; attributes[i]; i += 2) {
71             if (g_strcmp0 (attributes[i], "name") == 0) {
72                 for (j = 0; j < G_N_ELEMENTS (entries); j++) {
73                     if (entries[j].value == NULL)
74                         continue;
75                     g_hash_table_insert (__languages_dict,
76                                          (gpointer) g_strdup (entries[j].value),
77                                          (gpointer) g_strdup (attributes[i + 1]));
78                     entries[j].value = NULL;
79                 }
80             } else {
81                 for (j = 0; j < G_N_ELEMENTS (entries); j++) {
82                     if (g_strcmp0 (attributes[i], entries[j].key) == 0 &&
83                         attributes[i + 1] != NULL) {
84                         entries[j].value = attributes[i + 1];
85                     }
86                 }
87             }
88         }
89     }
90
91     return TRUE;
92 }
93
94 void
95 _load_lang()
96 {
97     gchar *filename;
98     XMLNode *node;
99     struct stat buf;
100
101     __languages_dict = g_hash_table_new_full (g_str_hash,
102             g_str_equal, g_free, g_free);
103     filename = g_build_filename (ISOCODES_PREFIX,
104                                  "share/xml/iso-codes/iso_639.xml",
105                                  NULL);
106     if (g_stat (filename, &buf) != 0) {
107         g_warning ("Can not get stat of file %s", filename);
108         g_free (filename);
109         return;
110     }
111
112     node = ibus_xml_parse_file (filename);
113     g_free (filename);
114
115     if (!node) {
116         return;
117     }
118
119     _iso_codes_parse_xml_node (node);
120     ibus_xml_free (node);
121 }
122
123 const gchar *
124 ibus_get_language_name(const gchar *_locale) {
125     const gchar *retval;
126     gchar *p = NULL;
127     gchar *lang = NULL;
128
129     if (__languages_dict == NULL ) {
130         _load_lang();
131     }
132     if ((p = strchr (_locale, '_')) !=  NULL) {
133         p = g_strndup (_locale, p - _locale);
134     } else {
135         p = g_strdup (_locale);
136     }
137     lang = g_ascii_strdown (p, -1);
138     g_free (p);
139     retval = (const gchar *) g_hash_table_lookup (__languages_dict, lang);
140     g_free (lang);
141     if (retval != NULL) {
142 #ifdef ENABLE_NLS
143         return dgettext("iso_639", retval);
144 #else
145         return retval;
146 #endif
147     }
148     else {
149 #ifdef ENABLE_NLS
150         return dgettext(GETTEXT_PACKAGE, N_("Other"));
151 #else
152         return N_("Other");
153 #endif
154     }
155 }