Blame libgnome-desktop/gnome-languages.c

rpm-build 18b009
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
rpm-build 18b009
 *
rpm-build 18b009
 * Copyright 2008  Red Hat, Inc,
rpm-build 18b009
 *           2007  William Jon McCann <mccann@jhu.edu>
rpm-build 18b009
 *
rpm-build 18b009
 * This program is free software; you can redistribute it and/or modify
rpm-build 18b009
 * it under the terms of the GNU General Public License as published by
rpm-build 18b009
 * the Free Software Foundation; either version 2 of the License, or
rpm-build 18b009
 * (at your option) any later version.
rpm-build 18b009
 *
rpm-build 18b009
 * This program is distributed in the hope that it will be useful,
rpm-build 18b009
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build 18b009
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
rpm-build 18b009
 * GNU General Public License for more details.
rpm-build 18b009
 *
rpm-build 18b009
 * You should have received a copy of the GNU General Public License
rpm-build 18b009
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
rpm-build 18b009
 *
rpm-build 18b009
 * Written by : William Jon McCann <mccann@jhu.edu>
rpm-build 18b009
 *              Ray Strode <rstrode@redhat.com>
rpm-build 18b009
 */
rpm-build 18b009
rpm-build 18b009
#include "config.h"
rpm-build 18b009
rpm-build 18b009
#include <stdlib.h>
rpm-build 18b009
#include <stdio.h>
rpm-build 18b009
#include <unistd.h>
rpm-build 18b009
#include <string.h>
rpm-build 18b009
#include <errno.h>
rpm-build 18b009
#include <dirent.h>
rpm-build 18b009
#include <locale.h>
rpm-build 18b009
#include <langinfo.h>
rpm-build 18b009
#include <sys/stat.h>
rpm-build 18b009
rpm-build 18b009
#include <glib.h>
rpm-build 18b009
#include <glib/gi18n.h>
rpm-build 18b009
#include <glib/gstdio.h>
rpm-build 18b009
rpm-build 18b009
#define GNOME_DESKTOP_USE_UNSTABLE_API
rpm-build 18b009
#include "gnome-languages.h"
rpm-build 18b009
rpm-build 18b009
#include <langinfo.h>
rpm-build 18b009
#ifndef __LC_LAST
rpm-build 18b009
#define __LC_LAST       13
rpm-build 18b009
#endif
rpm-build 18b009
rpm-build 18b009
#define ISO_CODES_DATADIR ISO_CODES_PREFIX "/share/xml/iso-codes"
rpm-build 18b009
#define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
rpm-build 18b009
rpm-build 18b009
#include "default-input-sources.h"
rpm-build 18b009
rpm-build 18b009
typedef struct _GnomeLocale {
rpm-build 18b009
        char *id;
rpm-build 18b009
        char *name;
rpm-build 18b009
        char *language_code;
rpm-build 18b009
        char *territory_code;
rpm-build 18b009
        char *codeset;
rpm-build 18b009
        char *modifier;
rpm-build 18b009
} GnomeLocale;
rpm-build 18b009
rpm-build 18b009
static GHashTable *gnome_languages_map;
rpm-build 18b009
static GHashTable *gnome_territories_map;
rpm-build 18b009
static GHashTable *gnome_available_locales_map;
rpm-build 18b009
static GHashTable *gnome_language_count_map;
rpm-build 18b009
static GHashTable *gnome_territory_count_map;
rpm-build 18b009
rpm-build 18b009
static char * construct_language_name (const char *language,
rpm-build 18b009
                                       const char *territory,
rpm-build 18b009
                                       const char *codeset,
rpm-build 18b009
                                       const char *modifier);
rpm-build 18b009
rpm-build 18b009
static gboolean language_name_is_valid (const char *language_name);
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
gnome_locale_free (GnomeLocale *locale)
rpm-build 18b009
{
rpm-build 18b009
        if (locale == NULL) {
rpm-build 18b009
                return;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        g_free (locale->id);
rpm-build 18b009
        g_free (locale->name);
rpm-build 18b009
        g_free (locale->codeset);
rpm-build 18b009
        g_free (locale->modifier);
rpm-build 18b009
        g_free (locale->language_code);
rpm-build 18b009
        g_free (locale->territory_code);
rpm-build 18b009
        g_free (locale);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static char *
rpm-build 18b009
normalize_codeset (const char *codeset)
rpm-build 18b009
{
rpm-build 18b009
        if (codeset == NULL)
rpm-build 18b009
                return NULL;
rpm-build 18b009
rpm-build 18b009
        if (g_str_equal (codeset, "UTF-8") ||
rpm-build 18b009
            g_str_equal (codeset, "utf8"))
rpm-build 18b009
                return g_strdup ("UTF-8");
rpm-build 18b009
rpm-build 18b009
        return g_strdup (codeset);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_parse_locale:
rpm-build 18b009
 * @locale: a locale string
rpm-build 18b009
 * @language_codep: (out) (allow-none) (transfer full): location to
rpm-build 18b009
 * store the language code, or %NULL
rpm-build 18b009
 * @country_codep: (out) (allow-none) (transfer full): location to
rpm-build 18b009
 * store the country code, or %NULL
rpm-build 18b009
 * @codesetp: (out) (allow-none) (transfer full): location to
rpm-build 18b009
 * store the codeset, or %NULL
rpm-build 18b009
 * @modifierp: (out) (allow-none) (transfer full): location to
rpm-build 18b009
 * store the modifier, or %NULL
rpm-build 18b009
 *
rpm-build 18b009
 * Extracts the various components of a locale string of the form
rpm-build 18b009
 * [language[_country][.codeset][@modifier]]. See
rpm-build 18b009
 * http://en.wikipedia.org/wiki/Locale.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: %TRUE if parsing was successful.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
gboolean
rpm-build 18b009
gnome_parse_locale (const char *locale,
rpm-build 18b009
                    char      **language_codep,
rpm-build 18b009
                    char      **country_codep,
rpm-build 18b009
                    char      **codesetp,
rpm-build 18b009
                    char      **modifierp)
rpm-build 18b009
{
rpm-build 18b009
        static GRegex *re = NULL;
rpm-build 18b009
        GMatchInfo *match_info;
rpm-build 18b009
        gboolean    res;
rpm-build 18b009
        gboolean    retval;
rpm-build 18b009
rpm-build 18b009
        match_info = NULL;
rpm-build 18b009
        retval = FALSE;
rpm-build 18b009
rpm-build 18b009
        if (re == NULL) {
rpm-build 18b009
                GError *error = NULL;
rpm-build 18b009
                re = g_regex_new ("^(?P<language>[^_.@[:space:]]+)"
rpm-build 18b009
                                  "(_(?P<territory>[[:upper:]]+))?"
rpm-build 18b009
                                  "(\\.(?P<codeset>[-_0-9a-zA-Z]+))?"
rpm-build 18b009
                                  "(@(?P<modifier>[[:ascii:]]+))?$",
rpm-build 18b009
                                  0, 0, &error);
rpm-build 18b009
                if (re == NULL) {
rpm-build 18b009
                        g_warning ("%s", error->message);
rpm-build 18b009
                        g_error_free (error);
rpm-build 18b009
                        goto out;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (!g_regex_match (re, locale, 0, &match_info) ||
rpm-build 18b009
            g_match_info_is_partial_match (match_info)) {
rpm-build 18b009
                g_warning ("locale '%s' isn't valid\n", locale);
rpm-build 18b009
                goto out;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        res = g_match_info_matches (match_info);
rpm-build 18b009
        if (! res) {
rpm-build 18b009
                g_warning ("Unable to parse locale: %s", locale);
rpm-build 18b009
                goto out;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        retval = TRUE;
rpm-build 18b009
rpm-build 18b009
        if (language_codep != NULL) {
rpm-build 18b009
                *language_codep = g_match_info_fetch_named (match_info, "language");
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (country_codep != NULL) {
rpm-build 18b009
                *country_codep = g_match_info_fetch_named (match_info, "territory");
rpm-build 18b009
rpm-build 18b009
                if (*country_codep != NULL &&
rpm-build 18b009
                    *country_codep[0] == '\0') {
rpm-build 18b009
                        g_free (*country_codep);
rpm-build 18b009
                        *country_codep = NULL;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (codesetp != NULL) {
rpm-build 18b009
                *codesetp = g_match_info_fetch_named (match_info, "codeset");
rpm-build 18b009
rpm-build 18b009
                if (*codesetp != NULL &&
rpm-build 18b009
                    *codesetp[0] == '\0') {
rpm-build 18b009
                        g_free (*codesetp);
rpm-build 18b009
                        *codesetp = NULL;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (modifierp != NULL) {
rpm-build 18b009
                *modifierp = g_match_info_fetch_named (match_info, "modifier");
rpm-build 18b009
rpm-build 18b009
                if (*modifierp != NULL &&
rpm-build 18b009
                    *modifierp[0] == '\0') {
rpm-build 18b009
                        g_free (*modifierp);
rpm-build 18b009
                        *modifierp = NULL;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (codesetp != NULL && *codesetp != NULL) {
rpm-build 18b009
                g_autofree gchar *normalized_codeset = NULL;
rpm-build 18b009
                g_autofree gchar *normalized_name = NULL;
rpm-build 18b009
rpm-build 18b009
                normalized_codeset = normalize_codeset (*codesetp);
rpm-build 18b009
                normalized_name = construct_language_name (language_codep ? *language_codep : NULL,
rpm-build 18b009
                                                           country_codep ? *country_codep : NULL,
rpm-build 18b009
                                                           normalized_codeset,
rpm-build 18b009
                                                           modifierp ? *modifierp : NULL);
rpm-build 18b009
rpm-build 18b009
                if (language_name_is_valid (normalized_name)) {
rpm-build 18b009
                        g_free (*codesetp);
rpm-build 18b009
                        *codesetp = g_steal_pointer (&normalized_codeset);
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
 out:
rpm-build 18b009
        g_match_info_free (match_info);
rpm-build 18b009
rpm-build 18b009
        return retval;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static char *
rpm-build 18b009
construct_language_name (const char *language,
rpm-build 18b009
                         const char *territory,
rpm-build 18b009
                         const char *codeset,
rpm-build 18b009
                         const char *modifier)
rpm-build 18b009
{
rpm-build 18b009
        char *name;
rpm-build 18b009
rpm-build 18b009
        g_assert (language != NULL && language[0] != 0);
rpm-build 18b009
        g_assert (territory == NULL || territory[0] != 0);
rpm-build 18b009
        g_assert (codeset == NULL || codeset[0] != 0);
rpm-build 18b009
        g_assert (modifier == NULL || modifier[0] != 0);
rpm-build 18b009
rpm-build 18b009
        name = g_strdup_printf ("%s%s%s%s%s%s%s",
rpm-build 18b009
                                language,
rpm-build 18b009
                                territory != NULL? "_" : "",
rpm-build 18b009
                                territory != NULL? territory : "",
rpm-build 18b009
                                codeset != NULL? "." : "",
rpm-build 18b009
                                codeset != NULL? codeset : "",
rpm-build 18b009
                                modifier != NULL? "@" : "",
rpm-build 18b009
                                modifier != NULL? modifier : "");
rpm-build 18b009
rpm-build 18b009
        return name;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_normalize_locale:
rpm-build 18b009
 * @locale: a locale string
rpm-build 18b009
 *
rpm-build 18b009
 * Gets the normalized locale string in the form
rpm-build 18b009
 * [language[_country][.codeset][@modifier]] for @name.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: (transfer full): normalized locale string. Caller
rpm-build 18b009
 * takes ownership.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
char *
rpm-build 18b009
gnome_normalize_locale (const char *locale)
rpm-build 18b009
{
rpm-build 18b009
        char *normalized_name;
rpm-build 18b009
        gboolean valid;
rpm-build 18b009
        g_autofree char *language_code = NULL;
rpm-build 18b009
        g_autofree char *territory_code = NULL;
rpm-build 18b009
        g_autofree char *codeset = NULL;
rpm-build 18b009
        g_autofree char *modifier = NULL;
rpm-build 18b009
rpm-build 18b009
        if (locale[0] == '\0') {
rpm-build 18b009
                return NULL;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        valid = gnome_parse_locale (locale,
rpm-build 18b009
                                    &language_code,
rpm-build 18b009
                                    &territory_code,
rpm-build 18b009
                                    &codeset, &modifier);
rpm-build 18b009
        if (!valid)
rpm-build 18b009
                return NULL;
rpm-build 18b009
rpm-build 18b009
        normalized_name = construct_language_name (language_code,
rpm-build 18b009
                                                   territory_code,
rpm-build 18b009
                                                   codeset, modifier);
rpm-build 18b009
        return normalized_name;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
language_name_is_valid (const char *language_name)
rpm-build 18b009
{
rpm-build 18b009
        gboolean  is_valid;
rpm-build 18b009
        int lc_type_id = LC_MESSAGES;
rpm-build 18b009
        g_autofree char *old_locale = NULL;
rpm-build 18b009
rpm-build 18b009
        old_locale = g_strdup (setlocale (lc_type_id, NULL));
rpm-build 18b009
        is_valid = setlocale (lc_type_id, language_name) != NULL;
rpm-build 18b009
        setlocale (lc_type_id, old_locale);
rpm-build 18b009
rpm-build 18b009
        return is_valid;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
language_name_get_codeset_details (const char  *language_name,
rpm-build 18b009
                                   char       **pcodeset,
rpm-build 18b009
                                   gboolean    *is_utf8)
rpm-build 18b009
{
rpm-build 18b009
        g_autofree char *old_locale = NULL;
rpm-build 18b009
        const char *codeset = NULL;
rpm-build 18b009
rpm-build 18b009
        old_locale = g_strdup (setlocale (LC_CTYPE, NULL));
rpm-build 18b009
rpm-build 18b009
        if (setlocale (LC_CTYPE, language_name) == NULL)
rpm-build 18b009
                return;
rpm-build 18b009
rpm-build 18b009
        codeset = nl_langinfo (CODESET);
rpm-build 18b009
rpm-build 18b009
        if (pcodeset != NULL) {
rpm-build 18b009
                *pcodeset = g_strdup (codeset);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (is_utf8 != NULL) {
rpm-build 18b009
                g_autofree char *normalized_codeset = normalize_codeset (codeset);
rpm-build 18b009
rpm-build 18b009
                *is_utf8 = strcmp (normalized_codeset, "UTF-8") == 0;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        setlocale (LC_CTYPE, old_locale);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_language_has_translations:
rpm-build 18b009
 * @code: an ISO 639 code string
rpm-build 18b009
 *
rpm-build 18b009
 * Returns %TRUE if there are translations for language @code.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: %TRUE if there are translations for language @code.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
gboolean
rpm-build 18b009
gnome_language_has_translations (const char *code)
rpm-build 18b009
{
rpm-build 18b009
        GDir        *dir;
rpm-build 18b009
        const char  *name;
rpm-build 18b009
        gboolean     has_translations;
rpm-build 18b009
        g_autofree char *path = NULL;
rpm-build 18b009
rpm-build 18b009
        path = g_build_filename (GNOMELOCALEDIR, code, "LC_MESSAGES", NULL);
rpm-build 18b009
rpm-build 18b009
        has_translations = FALSE;
rpm-build 18b009
        dir = g_dir_open (path, 0, NULL);
rpm-build 18b009
rpm-build 18b009
        if (dir == NULL) {
rpm-build 18b009
                goto out;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        do {
rpm-build 18b009
                name = g_dir_read_name (dir);
rpm-build 18b009
rpm-build 18b009
                if (name == NULL) {
rpm-build 18b009
                        break;
rpm-build 18b009
                }
rpm-build 18b009
rpm-build 18b009
                if (g_str_has_suffix (name, ".mo")) {
rpm-build 18b009
                        has_translations = TRUE;
rpm-build 18b009
                        break;
rpm-build 18b009
                }
rpm-build 18b009
        } while (name != NULL);
rpm-build 18b009
        g_dir_close (dir);
rpm-build 18b009
rpm-build 18b009
 out:
rpm-build 18b009
        return has_translations;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
add_locale (const char *language_name,
rpm-build 18b009
            gboolean    utf8_only)
rpm-build 18b009
{
rpm-build 18b009
        GnomeLocale *locale;
rpm-build 18b009
        GnomeLocale *old_locale;
rpm-build 18b009
        g_autofree char *name = NULL;
rpm-build 18b009
        gboolean   is_utf8 = FALSE;
rpm-build 18b009
        gboolean   valid = FALSE;
rpm-build 18b009
rpm-build 18b009
        g_return_val_if_fail (language_name != NULL, FALSE);
rpm-build 18b009
        g_return_val_if_fail (*language_name != '\0', FALSE);
rpm-build 18b009
rpm-build 18b009
        language_name_get_codeset_details (language_name, NULL, &is_utf8);
rpm-build 18b009
rpm-build 18b009
        if (is_utf8) {
rpm-build 18b009
                name = g_strdup (language_name);
rpm-build 18b009
        } else if (utf8_only) {
rpm-build 18b009
rpm-build 18b009
                if (strchr (language_name, '.'))
rpm-build 18b009
                        return FALSE;
rpm-build 18b009
rpm-build 18b009
                /* If the locale name has no dot, assume that its
rpm-build 18b009
                 * encoding part is missing and try again after adding
rpm-build 18b009
                 * ".UTF-8". This catches locale names like "de_DE".
rpm-build 18b009
                 */
rpm-build 18b009
                name = g_strdup_printf ("%s.UTF-8", language_name);
rpm-build 18b009
rpm-build 18b009
                language_name_get_codeset_details (name, NULL, &is_utf8);
rpm-build 18b009
                if (!is_utf8)
rpm-build 18b009
                        return FALSE;
rpm-build 18b009
        } else {
rpm-build 18b009
                name = g_strdup (language_name);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (!language_name_is_valid (name)) {
rpm-build 18b009
                g_debug ("Ignoring '%s' as a locale, since it's invalid", name);
rpm-build 18b009
                return FALSE;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        locale = g_new0 (GnomeLocale, 1);
rpm-build 18b009
        valid = gnome_parse_locale (name,
rpm-build 18b009
                                    &locale->language_code,
rpm-build 18b009
                                    &locale->territory_code,
rpm-build 18b009
                                    &locale->codeset,
rpm-build 18b009
                                    &locale->modifier);
rpm-build 18b009
        if (!valid) {
rpm-build 18b009
                gnome_locale_free (locale);
rpm-build 18b009
                return FALSE;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        locale->id = construct_language_name (locale->language_code, locale->territory_code,
rpm-build 18b009
                                              NULL, locale->modifier);
rpm-build 18b009
        locale->name = construct_language_name (locale->language_code, locale->territory_code,
rpm-build 18b009
                                                locale->codeset, locale->modifier);
rpm-build 18b009
rpm-build 18b009
        if (!gnome_language_has_translations (locale->name) &&
rpm-build 18b009
            !gnome_language_has_translations (locale->id) &&
rpm-build 18b009
            !gnome_language_has_translations (locale->language_code) &&
rpm-build 18b009
            utf8_only) {
rpm-build 18b009
                g_debug ("Ignoring '%s' as a locale, since it lacks translations", locale->name);
rpm-build 18b009
                gnome_locale_free (locale);
rpm-build 18b009
                return FALSE;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (!utf8_only) {
rpm-build 18b009
                g_free (locale->id);
rpm-build 18b009
                locale->id = g_strdup (locale->name);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        old_locale = g_hash_table_lookup (gnome_available_locales_map, locale->id);
rpm-build 18b009
        if (old_locale != NULL) {
rpm-build 18b009
                if (strlen (old_locale->name) > strlen (locale->name)) {
rpm-build 18b009
                        gnome_locale_free (locale);
rpm-build 18b009
                        return FALSE;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        g_hash_table_insert (gnome_available_locales_map, g_strdup (locale->id), locale);
rpm-build 18b009
rpm-build 18b009
        return TRUE;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static int
rpm-build 18b009
select_dirs (const struct dirent *dirent)
rpm-build 18b009
{
rpm-build 18b009
        int result = 0;
rpm-build 18b009
rpm-build 18b009
        if (strcmp (dirent->d_name, ".") != 0 && strcmp (dirent->d_name, "..") != 0) {
rpm-build 18b009
                mode_t mode = 0;
rpm-build 18b009
rpm-build 18b009
#ifdef _DIRENT_HAVE_D_TYPE
rpm-build 18b009
                if (dirent->d_type != DT_UNKNOWN && dirent->d_type != DT_LNK) {
rpm-build 18b009
                        mode = DTTOIF (dirent->d_type);
rpm-build 18b009
                } else
rpm-build 18b009
#endif
rpm-build 18b009
                        {
rpm-build 18b009
                                struct stat st;
rpm-build 18b009
                                g_autofree char *path = NULL;
rpm-build 18b009
rpm-build 18b009
                                path = g_build_filename (LIBLOCALEDIR, dirent->d_name, NULL);
rpm-build 18b009
                                if (g_stat (path, &st) == 0) {
rpm-build 18b009
                                        mode = st.st_mode;
rpm-build 18b009
                                }
rpm-build 18b009
                        }
rpm-build 18b009
rpm-build 18b009
                result = S_ISDIR (mode);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return result;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
collect_locales_from_directory (void)
rpm-build 18b009
{
rpm-build 18b009
        gboolean found_locales = FALSE;
rpm-build 18b009
        struct dirent **dirents;
rpm-build 18b009
        int             ndirents;
rpm-build 18b009
        int             cnt;
rpm-build 18b009
rpm-build 18b009
        ndirents = scandir (LIBLOCALEDIR, &dirents, select_dirs, alphasort);
rpm-build 18b009
rpm-build 18b009
        for (cnt = 0; cnt < ndirents; ++cnt) {
rpm-build 18b009
                if (add_locale (dirents[cnt]->d_name, TRUE))
rpm-build 18b009
                        found_locales = TRUE;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (ndirents > 0) {
rpm-build 18b009
                free (dirents);
rpm-build 18b009
        }
rpm-build 18b009
        return found_locales;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
collect_locales_from_localebin (void)
rpm-build 18b009
{
rpm-build 18b009
        gboolean found_locales = FALSE;
rpm-build 18b009
        const gchar *argv[] = { "locale", "-a", NULL };
rpm-build 18b009
        gchar **linep;
rpm-build 18b009
        g_auto (GStrv) lines = NULL;
rpm-build 18b009
        g_autofree gchar *output = NULL;
rpm-build 18b009
rpm-build 18b009
        if (g_spawn_sync (NULL, (gchar **) argv, NULL,
rpm-build 18b009
                          G_SPAWN_SEARCH_PATH|G_SPAWN_STDERR_TO_DEV_NULL,
rpm-build 18b009
                          NULL, NULL, &output, NULL, NULL, NULL) == FALSE)
rpm-build 18b009
                return FALSE;
rpm-build 18b009
rpm-build 18b009
        g_return_val_if_fail (output != NULL, FALSE);
rpm-build 18b009
rpm-build 18b009
        lines = g_strsplit (output, "\n", 0);
rpm-build 18b009
        if (lines) {
rpm-build 18b009
                linep = lines;
rpm-build 18b009
                while (*linep) {
rpm-build 18b009
                        if (*linep[0] && add_locale (*linep, TRUE))
rpm-build 18b009
                                found_locales = TRUE;
rpm-build 18b009
                        linep++;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return found_locales;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
count_languages_and_territories (void)
rpm-build 18b009
{
rpm-build 18b009
	gpointer value;
rpm-build 18b009
	GHashTableIter iter;
rpm-build 18b009
rpm-build 18b009
	gnome_language_count_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
rpm-build 18b009
	gnome_territory_count_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);
rpm-build 18b009
rpm-build 18b009
        g_hash_table_iter_init (&iter, gnome_available_locales_map);
rpm-build 18b009
        while (g_hash_table_iter_next (&iter, NULL, &value)) {
rpm-build 18b009
                GnomeLocale *locale;
rpm-build 18b009
rpm-build 18b009
                locale = (GnomeLocale *) value;
rpm-build 18b009
rpm-build 18b009
		if (locale->language_code != NULL) {
rpm-build 18b009
			int count;
rpm-build 18b009
rpm-build 18b009
			count = GPOINTER_TO_INT (g_hash_table_lookup (gnome_language_count_map, locale->language_code));
rpm-build 18b009
			count++;
rpm-build 18b009
			g_hash_table_insert (gnome_language_count_map, g_strdup (locale->language_code), GINT_TO_POINTER (count));
rpm-build 18b009
		}
rpm-build 18b009
rpm-build 18b009
		if (locale->territory_code != NULL) {
rpm-build 18b009
			int count;
rpm-build 18b009
rpm-build 18b009
			count = GPOINTER_TO_INT (g_hash_table_lookup (gnome_territory_count_map, locale->territory_code));
rpm-build 18b009
			count++;
rpm-build 18b009
			g_hash_table_insert (gnome_territory_count_map, g_strdup (locale->territory_code), GINT_TO_POINTER (count));
rpm-build 18b009
		}
rpm-build 18b009
        }
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
collect_locales (void)
rpm-build 18b009
{
rpm-build 18b009
        gboolean found_localebin_locales = FALSE;
rpm-build 18b009
        gboolean found_dir_locales = FALSE;
rpm-build 18b009
rpm-build 18b009
        if (gnome_available_locales_map == NULL) {
rpm-build 18b009
                gnome_available_locales_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) gnome_locale_free);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        found_localebin_locales = collect_locales_from_localebin ();
rpm-build 18b009
rpm-build 18b009
        found_dir_locales = collect_locales_from_directory ();
rpm-build 18b009
rpm-build 18b009
        if (!(found_localebin_locales || found_dir_locales)) {
rpm-build 18b009
                g_warning ("Could not read list of available locales from libc, "
rpm-build 18b009
                           "guessing possible locales from available translations, "
rpm-build 18b009
                           "but list may be incomplete!");
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
	count_languages_and_territories ();
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gint
rpm-build 18b009
get_language_count (const char *language)
rpm-build 18b009
{
rpm-build 18b009
        if (gnome_language_count_map == NULL) {
rpm-build 18b009
                collect_locales ();
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
	return GPOINTER_TO_INT (g_hash_table_lookup (gnome_language_count_map, language));
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
is_unique_language (const char *language)
rpm-build 18b009
{
rpm-build 18b009
        return get_language_count (language) == 1;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gint
rpm-build 18b009
get_territory_count (const char *territory)
rpm-build 18b009
{
rpm-build 18b009
        if (gnome_territory_count_map == NULL) {
rpm-build 18b009
                collect_locales ();
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
	return GPOINTER_TO_INT (g_hash_table_lookup (gnome_territory_count_map, territory));
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
is_unique_territory (const char *territory)
rpm-build 18b009
{
rpm-build 18b009
        return get_territory_count (territory) == 1;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static gboolean
rpm-build 18b009
is_fallback_language (const char *code)
rpm-build 18b009
{
rpm-build 18b009
        const char *fallback_language_names[] = { "C", "POSIX", NULL };
rpm-build 18b009
        int i;
rpm-build 18b009
rpm-build 18b009
        for (i = 0; fallback_language_names[i] != NULL; i++) {
rpm-build 18b009
                if (strcmp (code, fallback_language_names[i]) == 0) {
rpm-build 18b009
                        return TRUE;
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return FALSE;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static const char *
rpm-build 18b009
get_language (const char *code)
rpm-build 18b009
{
rpm-build 18b009
        const char *name;
rpm-build 18b009
        int         len;
rpm-build 18b009
rpm-build 18b009
        g_assert (code != NULL);
rpm-build 18b009
rpm-build 18b009
        if (is_fallback_language (code)) {
rpm-build 18b009
                return "Unspecified";
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        len = strlen (code);
rpm-build 18b009
        if (len != 2 && len != 3) {
rpm-build 18b009
                return NULL;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        name = (const char *) g_hash_table_lookup (gnome_languages_map, code);
rpm-build 18b009
rpm-build 18b009
        return name;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static char *
rpm-build 18b009
get_first_item_in_semicolon_list (const char *list)
rpm-build 18b009
{
rpm-build 18b009
        char **items;
rpm-build 18b009
        char  *item;
rpm-build 18b009
rpm-build 18b009
        /* Some entries in iso codes have multiple values, separated
rpm-build 18b009
         * by semicolons.  Not really sure which one to pick, so
rpm-build 18b009
         * we just arbitrarily pick the first one.
rpm-build 18b009
         */
rpm-build 18b009
        items = g_strsplit (list, "; ", 2);
rpm-build 18b009
rpm-build 18b009
        item = g_strdup (items[0]);
rpm-build 18b009
        g_strfreev (items);
rpm-build 18b009
rpm-build 18b009
        return item;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static char *
rpm-build 18b009
capitalize_utf8_string (const char *str)
rpm-build 18b009
{
rpm-build 18b009
        char first[8] = { 0 };
rpm-build 18b009
rpm-build 18b009
        if (!str)
rpm-build 18b009
                return NULL;
rpm-build 18b009
rpm-build 18b009
        g_unichar_to_utf8 (g_unichar_totitle (g_utf8_get_char (str)), first);
rpm-build 18b009
rpm-build 18b009
        return g_strconcat (first, g_utf8_offset_to_pointer (str, 1), NULL);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static char *
rpm-build 18b009
get_translated_language (const char *code,
rpm-build 18b009
                         const char *locale)
rpm-build 18b009
{
rpm-build 18b009
        const char *language;
rpm-build 18b009
        char *name;
rpm-build 18b009
rpm-build 18b009
        language = get_language (code);
rpm-build 18b009
rpm-build 18b009
        name = NULL;
rpm-build 18b009
        if (language != NULL) {
rpm-build 18b009
                const char  *translated_name;
rpm-build 18b009
                g_autofree char *old_locale = NULL;
rpm-build 18b009
rpm-build 18b009
                if (locale != NULL) {
rpm-build 18b009
                        old_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
rpm-build 18b009
                        setlocale (LC_MESSAGES, locale);
rpm-build 18b009
                }
rpm-build 18b009
rpm-build 18b009
                if (is_fallback_language (code)) {
rpm-build 18b009
                        name = g_strdup (_("Unspecified"));
rpm-build 18b009
                } else {
rpm-build 18b009
                        g_autofree char *tmp = NULL;
rpm-build 18b009
                        translated_name = dgettext ("iso_639", language);
rpm-build 18b009
                        tmp = get_first_item_in_semicolon_list (translated_name);
rpm-build 18b009
                        name = capitalize_utf8_string (tmp);
rpm-build 18b009
                }
rpm-build 18b009
rpm-build 18b009
                if (locale != NULL) {
rpm-build 18b009
                        setlocale (LC_MESSAGES, old_locale);
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return name;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static const char *
rpm-build 18b009
get_territory (const char *code)
rpm-build 18b009
{
rpm-build 18b009
        const char *name;
rpm-build 18b009
        int         len;
rpm-build 18b009
rpm-build 18b009
        g_assert (code != NULL);
rpm-build 18b009
rpm-build 18b009
        len = strlen (code);
rpm-build 18b009
        if (len != 2 && len != 3) {
rpm-build 18b009
                return NULL;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        name = (const char *) g_hash_table_lookup (gnome_territories_map, code);
rpm-build 18b009
rpm-build 18b009
        return name;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static char *
rpm-build 18b009
get_translated_territory (const char *code,
rpm-build 18b009
                          const char *locale)
rpm-build 18b009
{
rpm-build 18b009
        const char *territory;
rpm-build 18b009
        char       *name;
rpm-build 18b009
rpm-build 18b009
        territory = get_territory (code);
rpm-build 18b009
rpm-build 18b009
        name = NULL;
rpm-build 18b009
        if (territory != NULL) {
rpm-build 18b009
                const char *translated_territory;
rpm-build 18b009
                g_autofree char *old_locale = NULL;
rpm-build 18b009
                g_autofree char *tmp = NULL;
rpm-build 18b009
rpm-build 18b009
                if (locale != NULL) {
rpm-build 18b009
                        old_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
rpm-build 18b009
                        setlocale (LC_MESSAGES, locale);
rpm-build 18b009
                }
rpm-build 18b009
rpm-build 18b009
                translated_territory = dgettext ("iso_3166", territory);
rpm-build 18b009
                tmp = get_first_item_in_semicolon_list (translated_territory);
rpm-build 18b009
                name = capitalize_utf8_string (tmp);
rpm-build 18b009
rpm-build 18b009
                if (locale != NULL) {
rpm-build 18b009
                        setlocale (LC_MESSAGES, old_locale);
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return name;
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
languages_parse_start_tag (GMarkupParseContext      *ctx,
rpm-build 18b009
                           const char               *element_name,
rpm-build 18b009
                           const char              **attr_names,
rpm-build 18b009
                           const char              **attr_values,
rpm-build 18b009
                           gpointer                  user_data,
rpm-build 18b009
                           GError                  **error)
rpm-build 18b009
{
rpm-build 18b009
        const char *ccode_longB;
rpm-build 18b009
        const char *ccode_longT;
rpm-build 18b009
        const char *ccode;
rpm-build 18b009
        const char *ccode_id;
rpm-build 18b009
        const char *lang_name;
rpm-build 18b009
rpm-build 18b009
        if (! (g_str_equal (element_name, "iso_639_entry") || g_str_equal (element_name, "iso_639_3_entry"))
rpm-build 18b009
            || attr_names == NULL || attr_values == NULL) {
rpm-build 18b009
                return;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        ccode = NULL;
rpm-build 18b009
        ccode_longB = NULL;
rpm-build 18b009
        ccode_longT = NULL;
rpm-build 18b009
        ccode_id = NULL;
rpm-build 18b009
        lang_name = NULL;
rpm-build 18b009
rpm-build 18b009
        while (*attr_names && *attr_values) {
rpm-build 18b009
                if (g_str_equal (*attr_names, "iso_639_1_code")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 2) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                ccode = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "iso_639_2B_code")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 3) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                ccode_longB = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "iso_639_2T_code")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 3) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                ccode_longT = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "id")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 2 &&
rpm-build 18b009
                                    strlen (*attr_values) != 3) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                ccode_id = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "name")) {
rpm-build 18b009
                        lang_name = *attr_values;
rpm-build 18b009
                }
rpm-build 18b009
rpm-build 18b009
                ++attr_names;
rpm-build 18b009
                ++attr_values;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (lang_name == NULL) {
rpm-build 18b009
                return;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (ccode != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_languages_map,
rpm-build 18b009
                                     g_strdup (ccode),
rpm-build 18b009
                                     g_strdup (lang_name));
rpm-build 18b009
        }
rpm-build 18b009
        if (ccode_longB != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_languages_map,
rpm-build 18b009
                                     g_strdup (ccode_longB),
rpm-build 18b009
                                     g_strdup (lang_name));
rpm-build 18b009
        }
rpm-build 18b009
        if (ccode_longT != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_languages_map,
rpm-build 18b009
                                     g_strdup (ccode_longT),
rpm-build 18b009
                                     g_strdup (lang_name));
rpm-build 18b009
        }
rpm-build 18b009
        if (ccode_id != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_languages_map,
rpm-build 18b009
                                     g_strdup (ccode_id),
rpm-build 18b009
                                     g_strdup (lang_name));
rpm-build 18b009
        }
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
territories_parse_start_tag (GMarkupParseContext      *ctx,
rpm-build 18b009
                             const char               *element_name,
rpm-build 18b009
                             const char              **attr_names,
rpm-build 18b009
                             const char              **attr_values,
rpm-build 18b009
                             gpointer                  user_data,
rpm-build 18b009
                             GError                  **error)
rpm-build 18b009
{
rpm-build 18b009
        const char *acode_2;
rpm-build 18b009
        const char *acode_3;
rpm-build 18b009
        const char *ncode;
rpm-build 18b009
        const char *territory_common_name;
rpm-build 18b009
        const char *territory_name;
rpm-build 18b009
rpm-build 18b009
        if (! g_str_equal (element_name, "iso_3166_entry") || attr_names == NULL || attr_values == NULL) {
rpm-build 18b009
                return;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        acode_2 = NULL;
rpm-build 18b009
        acode_3 = NULL;
rpm-build 18b009
        ncode = NULL;
rpm-build 18b009
        territory_common_name = NULL;
rpm-build 18b009
        territory_name = NULL;
rpm-build 18b009
rpm-build 18b009
        while (*attr_names && *attr_values) {
rpm-build 18b009
                if (g_str_equal (*attr_names, "alpha_2_code")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 2) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                acode_2 = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "alpha_3_code")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 3) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                acode_3 = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "numeric_code")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                if (strlen (*attr_values) != 3) {
rpm-build 18b009
                                        return;
rpm-build 18b009
                                }
rpm-build 18b009
                                ncode = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "common_name")) {
rpm-build 18b009
                        /* skip if empty */
rpm-build 18b009
                        if (**attr_values) {
rpm-build 18b009
                                territory_common_name = *attr_values;
rpm-build 18b009
                        }
rpm-build 18b009
                } else if (g_str_equal (*attr_names, "name")) {
rpm-build 18b009
                        territory_name = *attr_values;
rpm-build 18b009
                }
rpm-build 18b009
rpm-build 18b009
                ++attr_names;
rpm-build 18b009
                ++attr_values;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (territory_common_name != NULL) {
rpm-build 18b009
                territory_name = territory_common_name;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (territory_name == NULL) {
rpm-build 18b009
                return;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (acode_2 != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_territories_map,
rpm-build 18b009
                                     g_strdup (acode_2),
rpm-build 18b009
                                     g_strdup (territory_name));
rpm-build 18b009
        }
rpm-build 18b009
        if (acode_3 != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_territories_map,
rpm-build 18b009
                                     g_strdup (acode_3),
rpm-build 18b009
                                     g_strdup (territory_name));
rpm-build 18b009
        }
rpm-build 18b009
        if (ncode != NULL) {
rpm-build 18b009
                g_hash_table_insert (gnome_territories_map,
rpm-build 18b009
                                     g_strdup (ncode),
rpm-build 18b009
                                     g_strdup (territory_name));
rpm-build 18b009
        }
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
languages_variant_init (const char *variant)
rpm-build 18b009
{
rpm-build 18b009
        gboolean res;
rpm-build 18b009
        gsize    buf_len;
rpm-build 18b009
        g_autofree char *buf = NULL;
rpm-build 18b009
        g_autofree char *filename = NULL;
rpm-build 18b009
        g_autoptr (GError) error = NULL;
rpm-build 18b009
rpm-build 18b009
        bindtextdomain (variant, ISO_CODES_LOCALESDIR);
rpm-build 18b009
        bind_textdomain_codeset (variant, "UTF-8");
rpm-build 18b009
rpm-build 18b009
        error = NULL;
rpm-build 18b009
        filename = g_strdup_printf (ISO_CODES_DATADIR "/%s.xml", variant);
rpm-build 18b009
        res = g_file_get_contents (filename,
rpm-build 18b009
                                   &buf,
rpm-build 18b009
                                   &buf_len,
rpm-build 18b009
                                   &error);
rpm-build 18b009
        if (res) {
rpm-build 18b009
                g_autoptr (GMarkupParseContext) ctx = NULL;
rpm-build 18b009
                GMarkupParser        parser = { languages_parse_start_tag, NULL, NULL, NULL, NULL };
rpm-build 18b009
rpm-build 18b009
                ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
rpm-build 18b009
rpm-build 18b009
                error = NULL;
rpm-build 18b009
                res = g_markup_parse_context_parse (ctx, buf, buf_len, &error);
rpm-build 18b009
rpm-build 18b009
                if (! res) {
rpm-build 18b009
                        g_warning ("Failed to parse '%s': %s\n",
rpm-build 18b009
                                   filename,
rpm-build 18b009
                                   error->message);
rpm-build 18b009
                }
rpm-build 18b009
        } else {
rpm-build 18b009
                g_warning ("Failed to load '%s': %s\n",
rpm-build 18b009
                           filename,
rpm-build 18b009
                           error->message);
rpm-build 18b009
        }
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
languages_init (void)
rpm-build 18b009
{
rpm-build 18b009
        if (gnome_languages_map)
rpm-build 18b009
                return;
rpm-build 18b009
rpm-build 18b009
        gnome_languages_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
rpm-build 18b009
rpm-build 18b009
        languages_variant_init ("iso_639");
rpm-build 18b009
        languages_variant_init ("iso_639_3");
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
static void
rpm-build 18b009
territories_init (void)
rpm-build 18b009
{
rpm-build 18b009
        gboolean res;
rpm-build 18b009
        gsize    buf_len;
rpm-build 18b009
        g_autofree char *buf = NULL;
rpm-build 18b009
        g_autoptr (GError) error = NULL;
rpm-build 18b009
rpm-build 18b009
        if (gnome_territories_map)
rpm-build 18b009
                return;
rpm-build 18b009
rpm-build 18b009
        bindtextdomain ("iso_3166", ISO_CODES_LOCALESDIR);
rpm-build 18b009
        bind_textdomain_codeset ("iso_3166", "UTF-8");
rpm-build 18b009
rpm-build 18b009
        gnome_territories_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
rpm-build 18b009
rpm-build 18b009
        error = NULL;
rpm-build 18b009
        res = g_file_get_contents (ISO_CODES_DATADIR "/iso_3166.xml",
rpm-build 18b009
                                   &buf,
rpm-build 18b009
                                   &buf_len,
rpm-build 18b009
                                   &error);
rpm-build 18b009
        if (res) {
rpm-build 18b009
                g_autoptr (GMarkupParseContext) ctx = NULL;
rpm-build 18b009
                GMarkupParser        parser = { territories_parse_start_tag, NULL, NULL, NULL, NULL };
rpm-build 18b009
rpm-build 18b009
                ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
rpm-build 18b009
rpm-build 18b009
                error = NULL;
rpm-build 18b009
                res = g_markup_parse_context_parse (ctx, buf, buf_len, &error);
rpm-build 18b009
rpm-build 18b009
                if (! res) {
rpm-build 18b009
                        g_warning ("Failed to parse '%s': %s\n",
rpm-build 18b009
                                   ISO_CODES_DATADIR "/iso_3166.xml",
rpm-build 18b009
                                   error->message);
rpm-build 18b009
                }
rpm-build 18b009
        } else {
rpm-build 18b009
                g_warning ("Failed to load '%s': %s\n",
rpm-build 18b009
                           ISO_CODES_DATADIR "/iso_3166.xml",
rpm-build 18b009
                           error->message);
rpm-build 18b009
        }
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_get_language_from_locale:
rpm-build 18b009
 * @locale: a locale string
rpm-build 18b009
 * @translation: (allow-none): a locale string
rpm-build 18b009
 *
rpm-build 18b009
 * Gets the language description for @locale. If @translation is
rpm-build 18b009
 * provided the returned string is translated accordingly.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: (transfer full): the language description. Caller
rpm-build 18b009
 * takes ownership.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
char *
rpm-build 18b009
gnome_get_language_from_locale (const char *locale,
rpm-build 18b009
                                const char *translation)
rpm-build 18b009
{
rpm-build 18b009
        GString *full_language;
rpm-build 18b009
        g_autofree char *language_code = NULL;
rpm-build 18b009
        g_autofree char *territory_code = NULL;
rpm-build 18b009
        g_autofree char *codeset_code = NULL;
rpm-build 18b009
        g_autofree char *langinfo_codeset = NULL;
rpm-build 18b009
        g_autofree char *translated_language = NULL;
rpm-build 18b009
        g_autofree char *translated_territory = NULL;
rpm-build 18b009
        gboolean is_utf8 = TRUE;
rpm-build 18b009
rpm-build 18b009
        g_return_val_if_fail (locale != NULL, NULL);
rpm-build 18b009
        g_return_val_if_fail (*locale != '\0', NULL);
rpm-build 18b009
rpm-build 18b009
        full_language = g_string_new (NULL);
rpm-build 18b009
rpm-build 18b009
        languages_init ();
rpm-build 18b009
        territories_init ();
rpm-build 18b009
rpm-build 18b009
        gnome_parse_locale (locale,
rpm-build 18b009
                            &language_code,
rpm-build 18b009
                            &territory_code,
rpm-build 18b009
                            &codeset_code,
rpm-build 18b009
                            NULL);
rpm-build 18b009
rpm-build 18b009
        if (language_code == NULL) {
rpm-build 18b009
                goto out;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        translated_language = get_translated_language (language_code, translation);
rpm-build 18b009
        if (translated_language == NULL) {
rpm-build 18b009
                goto out;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        full_language = g_string_append (full_language, translated_language);
rpm-build 18b009
rpm-build 18b009
	if (is_unique_language (language_code)) {
rpm-build 18b009
		goto out;
rpm-build 18b009
	}
rpm-build 18b009
rpm-build 18b009
        if (territory_code != NULL) {
rpm-build 18b009
                translated_territory = get_translated_territory (territory_code, translation);
rpm-build 18b009
        }
rpm-build 18b009
        if (translated_territory != NULL) {
rpm-build 18b009
                g_string_append_printf (full_language,
rpm-build 18b009
                                        " (%s)",
rpm-build 18b009
                                        translated_territory);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        language_name_get_codeset_details (locale, &langinfo_codeset, &is_utf8);
rpm-build 18b009
rpm-build 18b009
        if (codeset_code == NULL && langinfo_codeset != NULL) {
rpm-build 18b009
                codeset_code = g_strdup (langinfo_codeset);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (!is_utf8 && codeset_code) {
rpm-build 18b009
                g_string_append_printf (full_language,
rpm-build 18b009
                                        " [%s]",
rpm-build 18b009
                                        codeset_code);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
 out:
rpm-build 18b009
        if (full_language->len == 0) {
rpm-build 18b009
                g_string_free (full_language, TRUE);
rpm-build 18b009
                return NULL;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return g_string_free (full_language, FALSE);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_get_country_from_locale:
rpm-build 18b009
 * @locale: a locale string
rpm-build 18b009
 * @translation: (allow-none): a locale string
rpm-build 18b009
 *
rpm-build 18b009
 * Gets the country description for @locale. If @translation is
rpm-build 18b009
 * provided the returned string is translated accordingly.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: (transfer full): the country description. Caller
rpm-build 18b009
 * takes ownership.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
char *
rpm-build 18b009
gnome_get_country_from_locale (const char *locale,
rpm-build 18b009
                               const char *translation)
rpm-build 18b009
{
rpm-build 18b009
        GString *full_name;
rpm-build 18b009
        g_autofree char *language_code = NULL;
rpm-build 18b009
        g_autofree char *territory_code = NULL;
rpm-build 18b009
        g_autofree char *codeset_code = NULL;
rpm-build 18b009
        g_autofree char *langinfo_codeset = NULL;
rpm-build 18b009
        g_autofree char *translated_language = NULL;
rpm-build 18b009
        g_autofree char *translated_territory = NULL;
rpm-build 18b009
        gboolean is_utf8 = TRUE;
rpm-build 18b009
rpm-build 18b009
        g_return_val_if_fail (locale != NULL, NULL);
rpm-build 18b009
        g_return_val_if_fail (*locale != '\0', NULL);
rpm-build 18b009
rpm-build 18b009
        full_name = g_string_new (NULL);
rpm-build 18b009
rpm-build 18b009
        languages_init ();
rpm-build 18b009
        territories_init ();
rpm-build 18b009
rpm-build 18b009
        gnome_parse_locale (locale,
rpm-build 18b009
                            &language_code,
rpm-build 18b009
                            &territory_code,
rpm-build 18b009
                            &codeset_code,
rpm-build 18b009
                            NULL);
rpm-build 18b009
rpm-build 18b009
        if (territory_code == NULL) {
rpm-build 18b009
                goto out;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        translated_territory = get_translated_territory (territory_code, translation);
rpm-build 18b009
        g_string_append (full_name, translated_territory);
rpm-build 18b009
rpm-build 18b009
	if (is_unique_territory (territory_code)) {
rpm-build 18b009
		goto out;
rpm-build 18b009
	}
rpm-build 18b009
rpm-build 18b009
        if (language_code != NULL) {
rpm-build 18b009
                translated_language = get_translated_language (language_code, translation);
rpm-build 18b009
        }
rpm-build 18b009
        if (translated_language != NULL) {
rpm-build 18b009
                g_string_append_printf (full_name,
rpm-build 18b009
                                        " (%s)",
rpm-build 18b009
                                        translated_language);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        language_name_get_codeset_details (translation, &langinfo_codeset, &is_utf8);
rpm-build 18b009
rpm-build 18b009
        if (codeset_code == NULL && langinfo_codeset != NULL) {
rpm-build 18b009
                codeset_code = g_strdup (langinfo_codeset);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (!is_utf8 && codeset_code) {
rpm-build 18b009
                g_string_append_printf (full_name,
rpm-build 18b009
                                        " [%s]",
rpm-build 18b009
                                        codeset_code);
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
 out:
rpm-build 18b009
        if (full_name->len == 0) {
rpm-build 18b009
                g_string_free (full_name, TRUE);
rpm-build 18b009
                return NULL;
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        return g_string_free (full_name, FALSE);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_get_all_locales:
rpm-build 18b009
 *
rpm-build 18b009
 * Gets all locales.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: (array zero-terminated=1) (element-type utf8) (transfer full):
rpm-build 18b009
 *   a newly allocated %NULL-terminated string array containing the
rpm-build 18b009
 *   all locales. Free with g_strfreev().
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
char **
rpm-build 18b009
gnome_get_all_locales (void)
rpm-build 18b009
{
rpm-build 18b009
        GHashTableIter iter;
rpm-build 18b009
        gpointer key, value;
rpm-build 18b009
        GPtrArray *array;
rpm-build 18b009
rpm-build 18b009
        if (gnome_available_locales_map == NULL) {
rpm-build 18b009
                collect_locales ();
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        array = g_ptr_array_new ();
rpm-build 18b009
        g_hash_table_iter_init (&iter, gnome_available_locales_map);
rpm-build 18b009
        while (g_hash_table_iter_next (&iter, &key, &value)) {
rpm-build 18b009
                GnomeLocale *locale;
rpm-build 18b009
rpm-build 18b009
                locale = (GnomeLocale *) value;
rpm-build 18b009
rpm-build 18b009
                g_ptr_array_add (array, g_strdup (locale->name));
rpm-build 18b009
        }
rpm-build 18b009
        g_ptr_array_add (array, NULL);
rpm-build 18b009
rpm-build 18b009
        return (char **) g_ptr_array_free (array, FALSE);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_get_language_from_code:
rpm-build 18b009
 * @code: an ISO 639 code string
rpm-build 18b009
 * @translation: (allow-none): a locale string
rpm-build 18b009
 *
rpm-build 18b009
 * Gets the language name for @code. If @translation is provided the
rpm-build 18b009
 * returned string is translated accordingly.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: (transfer full): the language name. Caller takes
rpm-build 18b009
 * ownership.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
char *
rpm-build 18b009
gnome_get_language_from_code (const char *code,
rpm-build 18b009
                              const char *translation)
rpm-build 18b009
{
rpm-build 18b009
        g_return_val_if_fail (code != NULL, NULL);
rpm-build 18b009
rpm-build 18b009
        languages_init ();
rpm-build 18b009
rpm-build 18b009
        return get_translated_language (code, translation);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_get_country_from_code:
rpm-build 18b009
 * @code: an ISO 3166 code string
rpm-build 18b009
 * @translation: (allow-none): a locale string
rpm-build 18b009
 *
rpm-build 18b009
 * Gets the country name for @code. If @translation is provided the
rpm-build 18b009
 * returned string is translated accordingly.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: (transfer full): the country name. Caller takes
rpm-build 18b009
 * ownership.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
char *
rpm-build 18b009
gnome_get_country_from_code (const char *code,
rpm-build 18b009
                             const char *translation)
rpm-build 18b009
{
rpm-build 18b009
        g_return_val_if_fail (code != NULL, NULL);
rpm-build 18b009
rpm-build 18b009
        territories_init ();
rpm-build 18b009
rpm-build 18b009
        return get_translated_territory (code, translation);
rpm-build 18b009
}
rpm-build 18b009
rpm-build 18b009
/**
rpm-build 18b009
 * gnome_get_input_source_from_locale:
rpm-build 18b009
 * @locale: a locale string
rpm-build 18b009
 * @type: (out) (transfer none): location to store the input source
rpm-build 18b009
 * type
rpm-build 18b009
 * @id: (out) (transfer none): location to store the input source
rpm-build 18b009
 * identifier
rpm-build 18b009
 *
rpm-build 18b009
 * Gets the default input source's type and identifier for a given
rpm-build 18b009
 * locale.
rpm-build 18b009
 *
rpm-build 18b009
 * Return value: %TRUE if a input source exists or %FALSE otherwise.
rpm-build 18b009
 *
rpm-build 18b009
 * Since: 3.8
rpm-build 18b009
 */
rpm-build 18b009
gboolean
rpm-build 18b009
gnome_get_input_source_from_locale (const char  *locale,
rpm-build 18b009
                                    const char **type,
rpm-build 18b009
                                    const char **id)
rpm-build 18b009
{
rpm-build 18b009
        static GHashTable *table = NULL;
rpm-build 18b009
        DefaultInputSource *dis;
rpm-build 18b009
        g_autofree gchar *l_code = NULL;
rpm-build 18b009
        g_autofree gchar *c_code = NULL;
rpm-build 18b009
        g_autofree gchar *key = NULL;
rpm-build 18b009
        gint i;
rpm-build 18b009
rpm-build 18b009
        g_return_val_if_fail (locale != NULL, FALSE);
rpm-build 18b009
        g_return_val_if_fail (type != NULL, FALSE);
rpm-build 18b009
        g_return_val_if_fail (id != NULL, FALSE);
rpm-build 18b009
rpm-build 18b009
        if (!table) {
rpm-build 18b009
                table = g_hash_table_new (g_str_hash, g_str_equal);
rpm-build 18b009
                for (i = 0; default_input_sources[i].id; ++i) {
rpm-build 18b009
                        dis = &default_input_sources[i];
rpm-build 18b009
                        g_hash_table_insert (table, (gpointer) dis->locale, dis);
rpm-build 18b009
                }
rpm-build 18b009
        }
rpm-build 18b009
rpm-build 18b009
        if (!gnome_parse_locale (locale, &l_code, &c_code, NULL, NULL))
rpm-build 18b009
                return FALSE;
rpm-build 18b009
rpm-build 18b009
        key = g_strconcat (l_code, "_", c_code, NULL);
rpm-build 18b009
rpm-build 18b009
        dis = g_hash_table_lookup (table, key);
rpm-build 18b009
        if (dis) {
rpm-build 18b009
                *type = dis->type;
rpm-build 18b009
                *id = dis->id;
rpm-build 18b009
        }
rpm-build 18b009
        return dis != NULL;
rpm-build 18b009
}