Back to index

lightdm  1.3.2
Classes | Defines | Enumerations | Functions | Variables
language.c File Reference
#include <string.h>
#include <locale.h>
#include <langinfo.h>
#include <stdio.h>
#include <glib/gi18n.h>
#include "lightdm/language.h"

Go to the source code of this file.

Classes

struct  LightDMLanguagePrivate

Defines

#define GET_PRIVATE(obj)   G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LANGUAGE, LightDMLanguagePrivate)

Enumerations

enum  { PROP_0, PROP_CODE, PROP_NAME, PROP_TERRITORY }

Functions

 G_DEFINE_TYPE (LightDMLanguage, lightdm_language, G_TYPE_OBJECT)
static void update_languages (void)
static gboolean is_utf8 (const gchar *code)
static gchar * get_locale_name (const gchar *code)
LightDMLanguagelightdm_get_language (void)
 lightdm_get_language:
GList * lightdm_get_languages (void)
 lightdm_get_languages:
const gchar * lightdm_language_get_code (LightDMLanguage *language)
 lightdm_language_get_code: : A LightDMLanguage
const gchar * lightdm_language_get_name (LightDMLanguage *language)
 lightdm_language_get_name: : A LightDMLanguage
const gchar * lightdm_language_get_territory (LightDMLanguage *language)
 lightdm_language_get_territory: : A LightDMLanguage

Variables

static gboolean have_languages = FALSE
static GList * languages = NULL

Class Documentation

struct LightDMLanguagePrivate

Definition at line 27 of file language.c.

Class Members
gchar * code
gchar * name
gchar * territory

Define Documentation

#define GET_PRIVATE (   obj)    G_TYPE_INSTANCE_GET_PRIVATE ((obj), LIGHTDM_TYPE_LANGUAGE, LightDMLanguagePrivate)

Definition at line 36 of file language.c.


Enumeration Type Documentation

anonymous enum
Enumerator:
PROP_0 
PROP_CODE 
PROP_NAME 
PROP_TERRITORY 

Definition at line 20 of file language.c.


Function Documentation

G_DEFINE_TYPE ( LightDMLanguage  ,
lightdm_language  ,
G_TYPE_OBJECT   
)
static gchar* get_locale_name ( const gchar *  code) [static]

Definition at line 101 of file language.c.

{
    gchar *locale = NULL, *language;
    char *at;
    static gchar **avail_locales;
    gint i;

    if (is_utf8 (code))
        return (gchar *) code;

    if ((at = strchr (code, '@')))
        language = g_strndup (code, at - code);
    else
        language = g_strdup (code);

    if (!avail_locales)
    {
        gchar *locales;
        GError *error = NULL;

        if (g_spawn_command_line_sync ("locale -a", &locales, NULL, NULL, &error))
        {
            avail_locales = g_strsplit (g_strchomp (locales), "\n", -1);
            g_free (locales);
        }
        else
        {
            g_warning ("Failed to run 'locale -a': %s", error->message);
            g_clear_error (&error);
        }
    }

    if (avail_locales)
    {
        for (i = 0; avail_locales[i]; i++)
        {
            gchar *loc = avail_locales[i];
            if (!g_strrstr (loc, ".utf8"))
                continue;
            if (g_str_has_prefix (loc, language))
            {
                locale = g_strdup (loc);
                break;
            }
        }
    }

    g_free (language);

    return locale;
}

Here is the call graph for this function:

Here is the caller graph for this function:

static gboolean is_utf8 ( const gchar *  code) [static]

Definition at line 94 of file language.c.

{
    return g_strrstr (code, ".utf8") || g_strrstr (code, ".UTF-8");
}

Here is the caller graph for this function:

lightdm_get_language:

Get the current language.

Return value: (transfer none): The current language or #NULL if no language.

Definition at line 161 of file language.c.

{
    const gchar *lang;
    GList *link;

    lang = g_getenv ("LANG");
    if (!lang)
        return NULL;

    for (link = lightdm_get_languages (); link; link = link->next)
    {
        LightDMLanguage *language = link->data;
        if (lightdm_language_matches (language, lang))
            return language;
    }

    return NULL;
}

Here is the call graph for this function:

GList* lightdm_get_languages ( void  )

lightdm_get_languages:

Get a list of languages to present to the user.

Return value: (element-type LightDMLanguage) (transfer none): A list of LightDMLanguage that should be presented to the user.

Definition at line 188 of file language.c.

{
    update_languages ();
    return languages;
}

Here is the call graph for this function:

Here is the caller graph for this function:

const gchar* lightdm_language_get_code ( LightDMLanguage language)

lightdm_language_get_code: : A LightDMLanguage

Get the code of a language.

Return value: The code of the language

Definition at line 203 of file language.c.

{
    g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);
    return GET_PRIVATE (language)->code;
}
const gchar* lightdm_language_get_name ( LightDMLanguage language)

lightdm_language_get_name: : A LightDMLanguage

Get the name of a language.

Return value: The name of the language

Definition at line 218 of file language.c.

{
    LightDMLanguagePrivate *priv;

    g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);

    priv = GET_PRIVATE (language);

    if (!priv->name)
    {
        gchar *locale = get_locale_name (priv->code);
        if (locale)
        {
            gchar *current = setlocale (LC_ALL, NULL);
            setlocale (LC_IDENTIFICATION, locale);
            setlocale (LC_MESSAGES, "");

            gchar *language_en = nl_langinfo (_NL_IDENTIFICATION_LANGUAGE);
            if (language_en && strlen (language_en) > 0)
                priv->name = g_strdup (dgettext ("iso_639_3", language_en));

            setlocale (LC_ALL, current);
        }
        if (!priv->name)
        {
            gchar **tokens = g_strsplit_set (priv->code, "_.@", 2);
            priv->name = g_strdup (tokens[0]);
            g_strfreev (tokens);
        }
    }

    return priv->name;
}

Here is the call graph for this function:

const gchar* lightdm_language_get_territory ( LightDMLanguage language)

lightdm_language_get_territory: : A LightDMLanguage

Get the territory the language is used in.

Return value: The territory the language is used in.

Definition at line 261 of file language.c.

{
    LightDMLanguagePrivate *priv;

    g_return_val_if_fail (LIGHTDM_IS_LANGUAGE (language), NULL);

    priv = GET_PRIVATE (language);

    if (!priv->territory && strchr (priv->code, '_'))
    {
        gchar *locale = get_locale_name (priv->code);
        if (locale)
        {
            gchar *current = setlocale (LC_ALL, NULL);
            setlocale (LC_IDENTIFICATION, locale);
            setlocale (LC_MESSAGES, "");

            gchar *country_en = nl_langinfo (_NL_IDENTIFICATION_TERRITORY);
            if (country_en && strlen (country_en) > 0 && g_strcmp0 (country_en, "ISO") != 0)
                priv->territory = g_strdup (dgettext ("iso_3166", country_en));

            setlocale (LC_ALL, current);
        }
        if (!priv->territory)
        {
            gchar **tokens = g_strsplit_set (priv->code, "_.@", 3);
            priv->territory = g_strdup (tokens[1]);
            g_strfreev (tokens);
        }
    }

    return priv->territory;
}

Here is the call graph for this function:

static void update_languages ( void  ) [static]

Definition at line 42 of file language.c.

{
    gchar *command = "locale -a";
    gchar *stdout_text = NULL, *stderr_text = NULL;
    gint exit_status;
    gboolean result;
    GError *error = NULL;

    if (have_languages)
        return;

    result = g_spawn_command_line_sync (command, &stdout_text, &stderr_text, &exit_status, &error);
    if (error)
    {
        g_warning ("Failed to run '%s': %s", command, error->message);
        g_clear_error (&error);
    }
    else if (exit_status != 0)
        g_warning ("Failed to get languages, '%s' returned %d", command, exit_status);
    else if (result)
    {
        gchar **tokens;
        int i;

        tokens = g_strsplit_set (stdout_text, "\n\r", -1);
        for (i = 0; tokens[i]; i++)
        {
            LightDMLanguage *language;
            gchar *code;

            code = g_strchug (tokens[i]);
            if (code[0] == '\0')
                continue;

            /* Ignore the non-interesting languages */
            if (strcmp (command, "locale -a") == 0 && !g_strrstr (code, ".utf8"))
                continue;

            language = g_object_new (LIGHTDM_TYPE_LANGUAGE, "code", code, NULL);
            languages = g_list_append (languages, language);
        }

        g_strfreev (tokens);
    }

    g_free (stdout_text);
    g_free (stderr_text);

    have_languages = TRUE;
}

Here is the caller graph for this function:


Variable Documentation

gboolean have_languages = FALSE [static]

Definition at line 38 of file language.c.

GList* languages = NULL [static]

Definition at line 39 of file language.c.