Blame src/ibusutil.c

Packit 3ff832
/* -*- mode: C; c-basic-offset: 4; indent-tabs-mode: nil; -*- */
Packit 3ff832
/* vim:set et sts=4: */
Packit 3ff832
/* bus - The Input Bus
Packit 3ff832
 * Copyright (C) 2008-2015 Peng Huang <shawn.p.huang@gmail.com>
Packit 3ff832
 * Copyright (C) 2010-2017 Takao Fujiwara <takao.fujiwara1@gmail.com>
Packit 3ff832
 * Copyright (C) 2008-2016 Red Hat, Inc.
Packit 3ff832
 *
Packit 3ff832
 * This library is free software; you can redistribute it and/or
Packit 3ff832
 * modify it under the terms of the GNU Lesser General Public
Packit 3ff832
 * License as published by the Free Software Foundation; either
Packit 3ff832
 * version 2.1 of the License, or (at your option) any later version.
Packit 3ff832
 *
Packit 3ff832
 * This library is distributed in the hope that it will be useful,
Packit 3ff832
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 3ff832
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 3ff832
 * Lesser General Public License for more details.
Packit 3ff832
 *
Packit 3ff832
 * You should have received a copy of the GNU Lesser General Public
Packit 3ff832
 * License along with this library; if not, write to the Free Software
Packit 3ff832
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301
Packit 3ff832
 * USA
Packit 3ff832
 */
Packit 3ff832
#ifdef HAVE_CONFIG_H
Packit 3ff832
#include <config.h>
Packit 3ff832
#endif
Packit 3ff832
Packit 3ff832
#include <glib.h>
Packit 3ff832
#include <glib/gstdio.h>
Packit 3ff832
#include <gio/gio.h>
Packit 3ff832
#include <string.h>
Packit 3ff832
#include "ibusxml.h"
Packit 3ff832
Packit 3ff832
#ifdef ENABLE_NLS
Packit 3ff832
#include <libintl.h>
Packit 3ff832
#endif
Packit 3ff832
Packit 3ff832
/* gettext macro */
Packit 3ff832
#define N_(t) t
Packit 3ff832
Packit 3ff832
static GHashTable *__languages_dict;
Packit 3ff832
Packit 3ff832
static gboolean
Packit 3ff832
_iso_codes_parse_xml_node (XMLNode          *node)
Packit 3ff832
{
Packit 3ff832
    GList *p;
Packit 3ff832
    g_assert (node);
Packit 3ff832
Packit 3ff832
    if (G_UNLIKELY (g_strcmp0 (node->name, "iso_639_entries") != 0)) {
Packit 3ff832
        return FALSE;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    for (p = node->sub_nodes; p != NULL; p = p->next) {
Packit 3ff832
        XMLNode *sub_node = (XMLNode *)p->data;
Packit 3ff832
        gchar **attributes = NULL;
Packit 3ff832
        int i, j;
Packit 3ff832
        struct {
Packit 3ff832
            const gchar *key;
Packit 3ff832
            gchar *value;
Packit 3ff832
        } entries[] = {
Packit 3ff832
            { "iso_639_2B_code", NULL },
Packit 3ff832
            { "iso_639_2T_code", NULL },
Packit 3ff832
            { "iso_639_1_code", NULL },
Packit 3ff832
        };
Packit 3ff832
Packit 3ff832
        if (sub_node->attributes == NULL) {
Packit 3ff832
            continue;
Packit 3ff832
        }
Packit 3ff832
Packit 3ff832
        attributes = sub_node->attributes;
Packit 3ff832
        for (i = 0; attributes[i]; i += 2) {
Packit 3ff832
            if (g_strcmp0 (attributes[i], "name") == 0) {
Packit 3ff832
                for (j = 0; j < G_N_ELEMENTS (entries); j++) {
Packit 3ff832
                    if (entries[j].value == NULL)
Packit 3ff832
                        continue;
Packit 3ff832
                    g_hash_table_insert (__languages_dict,
Packit 3ff832
                                         (gpointer) g_strdup (entries[j].value),
Packit 3ff832
                                         (gpointer) g_strdup (attributes[i + 1]));
Packit 3ff832
                    entries[j].value = NULL;
Packit 3ff832
                }
Packit 3ff832
            } else {
Packit 3ff832
                for (j = 0; j < G_N_ELEMENTS (entries); j++) {
Packit 3ff832
                    if (g_strcmp0 (attributes[i], entries[j].key) == 0 &&
Packit 3ff832
                        attributes[i + 1] != NULL) {
Packit 3ff832
                        entries[j].value = attributes[i + 1];
Packit 3ff832
                    }
Packit 3ff832
                }
Packit 3ff832
            }
Packit 3ff832
        }
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    return TRUE;
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void
Packit 3ff832
_load_lang()
Packit 3ff832
{
Packit 3ff832
    gchar *filename;
Packit 3ff832
    XMLNode *node;
Packit 3ff832
    struct stat buf;
Packit 3ff832
Packit 3ff832
#ifdef ENABLE_NLS
Packit 3ff832
    bindtextdomain ("iso_639", GLIB_LOCALE_DIR);
Packit 3ff832
    bind_textdomain_codeset ("iso_639", "UTF-8");
Packit 3ff832
#endif
Packit 3ff832
Packit 3ff832
    __languages_dict = g_hash_table_new_full (g_str_hash,
Packit 3ff832
            g_str_equal, g_free, g_free);
Packit 3ff832
    filename = g_build_filename (ISOCODES_PREFIX,
Packit 3ff832
                                 "share/xml/iso-codes/iso_639.xml",
Packit 3ff832
                                 NULL);
Packit 3ff832
    if (g_stat (filename, &buf) != 0) {
Packit 3ff832
        g_warning ("Can not get stat of file %s", filename);
Packit 3ff832
        g_free (filename);
Packit 3ff832
        return;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    node = ibus_xml_parse_file (filename);
Packit 3ff832
    g_free (filename);
Packit 3ff832
Packit 3ff832
    if (!node) {
Packit 3ff832
        return;
Packit 3ff832
    }
Packit 3ff832
Packit 3ff832
    _iso_codes_parse_xml_node (node);
Packit 3ff832
    ibus_xml_free (node);
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
const gchar *
Packit 3ff832
ibus_get_untranslated_language_name (const gchar *_locale)
Packit 3ff832
{
Packit 3ff832
    const gchar *retval;
Packit 3ff832
    gchar *p = NULL;
Packit 3ff832
    gchar *lang = NULL;
Packit 3ff832
Packit 3ff832
    if (__languages_dict == NULL )
Packit 3ff832
        _load_lang();
Packit 3ff832
    if ((p = strchr (_locale, '_')) !=  NULL)
Packit 3ff832
        p = g_strndup (_locale, p - _locale);
Packit 3ff832
    else
Packit 3ff832
        p = g_strdup (_locale);
Packit 3ff832
    lang = g_ascii_strdown (p, -1);
Packit 3ff832
    g_free (p);
Packit 3ff832
    retval = (const gchar *) g_hash_table_lookup (__languages_dict, lang);
Packit 3ff832
    g_free (lang);
Packit 3ff832
    if (retval != NULL)
Packit 3ff832
        return retval;
Packit 3ff832
    else
Packit 3ff832
        return "Other";
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
const gchar *
Packit 3ff832
ibus_get_language_name (const gchar *_locale)
Packit 3ff832
{
Packit 3ff832
    const gchar *retval = ibus_get_untranslated_language_name (_locale);
Packit 3ff832
Packit 3ff832
#ifdef ENABLE_NLS
Packit 3ff832
    if (g_strcmp0 (retval, "Other") == 0)
Packit 3ff832
        return dgettext (GETTEXT_PACKAGE, N_("Other"));
Packit 3ff832
    else
Packit 3ff832
        return dgettext ("iso_639", retval);
Packit 3ff832
#else
Packit 3ff832
    return retval;
Packit 3ff832
#endif
Packit 3ff832
}
Packit 3ff832
Packit 3ff832
void
Packit 3ff832
ibus_g_variant_get_child_string (GVariant *variant, gsize index, char **str)
Packit 3ff832
{
Packit 3ff832
    g_return_if_fail (str != NULL);
Packit 3ff832
Packit 3ff832
    g_free (*str);
Packit 3ff832
    g_variant_get_child (variant, index, "s", str);
Packit 3ff832
}