~lightdm-team/lightdm/1.4

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
/*
 * Copyright (C) 2010 Robert Ancell.
 * Author: Robert Ancell <robert.ancell@canonical.com>
 * 
 * This library is free software; you can redistribute it and/or modify it under
 * the terms of the GNU Lesser General Public License as published by the Free
 * Software Foundation; either version 3 of the License, or (at your option) any
 * later version. See http://www.gnu.org/copyleft/lgpl.html the full text of the
 * license.
 */

#include <locale.h>
#include <langinfo.h>

#include "lightdm/language.h"

enum {
    PROP_0,
    PROP_CODE,
    PROP_NAME,
    PROP_TERRITORY
};

struct _LdmLanguagePrivate
{
    gchar *code;
    gchar *name;
    gchar *territory;
};

G_DEFINE_TYPE (LdmLanguage, ldm_language, G_TYPE_OBJECT);

/**
 * ldm_language_new:
 * 
 * Create a new language.
 * @code: The language code
 * 
 * Return value: the new #LdmLanguage
 **/
LdmLanguage *
ldm_language_new (const gchar *code)
{
    return g_object_new (LDM_TYPE_LANGUAGE, "code", code, NULL);
}

/**
 * ldm_language_get_code:
 * @language: A #LdmLanguage
 * 
 * Get the code of a language.
 * 
 * Return value: The code of the language
 **/
const gchar *
ldm_language_get_code (LdmLanguage *language)
{
    g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL);
    return language->priv->code;
}

/**
 * ldm_language_get_name:
 * @language: A #LdmLanguage
 * 
 * Get the name of a language.
 *
 * Return value: The name of the language
 **/
const gchar *
ldm_language_get_name (LdmLanguage *language)
{
    g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL);

    if (!language->priv->name)
    {
        char *current = setlocale(LC_ALL, NULL);
        setlocale(LC_ALL, language->priv->code);
#ifdef _NL_IDENTIFICATION_LANGUAGE
        language->priv->name = g_strdup (nl_langinfo (_NL_IDENTIFICATION_LANGUAGE));
#else
        language->priv->name = g_strdup ("Unknown");
#endif
        setlocale(LC_ALL, current);
    }

    return language->priv->name;
}

/**
 * ldm_language_get_territory:
 * @language: A #LdmLanguage
 * 
 * Get the territory the language is used in.
 * 
 * Return value: The territory the language is used in.
 **/
const gchar *
ldm_language_get_territory (LdmLanguage *language)
{
    g_return_val_if_fail (LDM_IS_LANGUAGE (language), NULL);

    if (!language->priv->territory)
    {
        char *current = setlocale(LC_ALL, NULL);
        setlocale(LC_ALL, language->priv->code);
#ifdef _NL_IDENTIFICATION_TERRITORY
        language->priv->territory = g_strdup (nl_langinfo (_NL_IDENTIFICATION_TERRITORY));
#else
        language->priv->territory = g_strdup ("Unknown");
#endif
        setlocale(LC_ALL, current);
    }

    return language->priv->territory;
}

static gboolean
is_utf8 (const gchar *code)
{
    return g_str_has_suffix (code, ".utf8") || g_str_has_suffix (code, ".UTF-8");
}

/**
 * ldm_language_matches:
 * @language: A #LdmLanguage
 * @code: A language code
 * 
 * Check if a language code matches this language.
 * 
 * Return value: TRUE if the code matches this language.
 **/
gboolean
ldm_language_matches (LdmLanguage *language, const gchar *code)
{
    g_return_val_if_fail (LDM_IS_LANGUAGE (language), FALSE);
    g_return_val_if_fail (code != NULL, FALSE);

    /* Handle the fact the UTF-8 is specified both as '.utf8' and '.UTF-8' */
    if (is_utf8 (language->priv->code) && is_utf8 (code))
    {
        /* Match the characters before the '.' */
        int i;
        for (i = 0; language->priv->code[i] && code[i] && language->priv->code[i] == code[i] && code[i] != '.' ; i++);
        return language->priv->code[i] == '.' && code[i] == '.';
    }

    return g_str_equal (language->priv->code, code);
}

static void
ldm_language_init (LdmLanguage *language)
{
    language->priv = G_TYPE_INSTANCE_GET_PRIVATE (language, LDM_TYPE_LANGUAGE, LdmLanguagePrivate);
}

static void
ldm_language_set_property (GObject      *object,
                           guint         prop_id,
                           const GValue *value,
                           GParamSpec   *pspec)
{
    LdmLanguage *self;

    self = LDM_LANGUAGE (object);

    switch (prop_id) {
    case PROP_CODE:
        g_free (self->priv->name);
        self->priv->code = g_strdup (g_value_get_string (value));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
ldm_language_get_property (GObject    *object,
                           guint       prop_id,
                           GValue     *value,
                           GParamSpec *pspec)
{
    LdmLanguage *self;

    self = LDM_LANGUAGE (object);

    switch (prop_id) {
    case PROP_CODE:
        g_value_set_string (value, ldm_language_get_code (self));
        break;
    case PROP_NAME:
        g_value_set_string (value, ldm_language_get_name (self));
        break;
    case PROP_TERRITORY:
        g_value_set_string (value, ldm_language_get_territory (self));
        break;
    default:
        G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
        break;
    }
}

static void
ldm_language_class_init (LdmLanguageClass *klass)
{
    GObjectClass *object_class = G_OBJECT_CLASS (klass);
  
    g_type_class_add_private (klass, sizeof (LdmLanguagePrivate));

    object_class->set_property = ldm_language_set_property;
    object_class->get_property = ldm_language_get_property;

    g_object_class_install_property(object_class,
                                    PROP_CODE,
                                    g_param_spec_string("code",
                                                        "code",
                                                        "Language code",
                                                        NULL,
                                                        G_PARAM_READWRITE | G_PARAM_CONSTRUCT_ONLY));
    g_object_class_install_property(object_class,
                                    PROP_NAME,
                                    g_param_spec_string("name",
                                                        "name",
                                                        "Name of the language",
                                                        NULL,
                                                        G_PARAM_READABLE));
    g_object_class_install_property(object_class,
                                    PROP_TERRITORY,
                                    g_param_spec_string("territory",
                                                        "territory",
                                                        "Territory the language is from",
                                                        NULL,
                                                        G_PARAM_READABLE));
}