~ubuntu-branches/ubuntu/oneiric/gdm3/oneiric

« back to all changes in this revision

Viewing changes to gui/simple-greeter/gdm-languages.c

  • Committer: Bazaar Package Importer
  • Author(s): Josselin Mouette
  • Date: 2010-03-25 20:02:20 UTC
  • Revision ID: james.westby@ubuntu.com-20100325200220-12cap62s6p304nuh
Tags: upstream-2.29.92
ImportĀ upstreamĀ versionĀ 2.29.92

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 
2
 *
 
3
 * Copyright 2008  Red Hat, Inc,
 
4
 *           2007  William Jon McCann <mccann@jhu.edu>
 
5
 *
 
6
 * This program is free software; you can redistribute it and/or modify
 
7
 * it under the terms of the GNU General Public License as published by
 
8
 * the Free Software Foundation; either version 2 of the License, or
 
9
 * (at your option) any later version.
 
10
 *
 
11
 * This program is distributed in the hope that it will be useful,
 
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
14
 * GNU General Public License for more details.
 
15
 *
 
16
 * You should have received a copy of the GNU General Public License
 
17
 * along with this program; if not, write to the Free Software
 
18
 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
 
19
 *
 
20
 * Written by : William Jon McCann <mccann@jhu.edu>
 
21
 *              Ray Strode <rstrode@redhat.com>
 
22
 */
 
23
 
 
24
#include "config.h"
 
25
 
 
26
#include <stdlib.h>
 
27
#include <stdio.h>
 
28
#include <unistd.h>
 
29
#include <string.h>
 
30
#include <errno.h>
 
31
#include <dirent.h>
 
32
#include <locale.h>
 
33
#include <langinfo.h>
 
34
#include <sys/stat.h>
 
35
 
 
36
#include <glib.h>
 
37
#include <glib/gi18n.h>
 
38
#include <glib/gstdio.h>
 
39
 
 
40
#include "gdm-languages.h"
 
41
 
 
42
#include <langinfo.h>
 
43
#ifndef __LC_LAST
 
44
#define __LC_LAST       13
 
45
#endif
 
46
#include "locarchive.h"
 
47
 
 
48
#define ALIASES_FILE DATADIR "/gdm/locale.alias"
 
49
#define ARCHIVE_FILE LIBLOCALEDIR "/locale-archive"
 
50
#define ISO_CODES_DATADIR ISO_CODES_PREFIX "/share/xml/iso-codes"
 
51
#define ISO_CODES_LOCALESDIR ISO_CODES_PREFIX "/share/locale"
 
52
 
 
53
typedef struct _GdmLocale {
 
54
        char *id;
 
55
        char *name;
 
56
        char *language_code;
 
57
        char *territory_code;
 
58
        char *codeset;
 
59
        char *modifier;
 
60
} GdmLocale;
 
61
 
 
62
static GHashTable *gdm_languages_map;
 
63
static GHashTable *gdm_territories_map;
 
64
static GHashTable *gdm_available_locales_map;
 
65
 
 
66
static char * construct_language_name (const char *language,
 
67
                                       const char *territory,
 
68
                                       const char *codeset,
 
69
                                       const char *modifier);
 
70
 
 
71
static gboolean language_name_is_valid (const char *language_name);
 
72
 
 
73
static void
 
74
gdm_locale_free (GdmLocale *locale)
 
75
{
 
76
        if (locale == NULL) {
 
77
                return;
 
78
        }
 
79
 
 
80
        g_free (locale->id);
 
81
        g_free (locale->name);
 
82
        g_free (locale->codeset);
 
83
        g_free (locale->modifier);
 
84
        g_free (locale);
 
85
}
 
86
 
 
87
static char *
 
88
normalize_codeset (const char *codeset)
 
89
{
 
90
        char *normalized_codeset;
 
91
        const char *p;
 
92
        char *q;
 
93
 
 
94
        normalized_codeset = g_strdup (codeset);
 
95
 
 
96
        if (codeset != NULL) {
 
97
                for (p = codeset, q = normalized_codeset;
 
98
                     *p != '\0'; p++) {
 
99
 
 
100
                        if (*p == '-' || *p == '_') {
 
101
                                continue;
 
102
                        }
 
103
 
 
104
                        *q = g_ascii_tolower (*p);
 
105
                        q++;
 
106
                }
 
107
                *q = '\0';
 
108
        }
 
109
 
 
110
        return normalized_codeset;
 
111
}
 
112
 
 
113
/*
 
114
 * According to http://en.wikipedia.org/wiki/Locale
 
115
 * locale names are of the form:
 
116
 * [language[_territory][.codeset][@modifier]]
 
117
 */
 
118
void
 
119
gdm_parse_language_name (const char *name,
 
120
                         char      **language_codep,
 
121
                         char      **territory_codep,
 
122
                         char      **codesetp,
 
123
                         char      **modifierp)
 
124
{
 
125
        GRegex     *re;
 
126
        GMatchInfo *match_info;
 
127
        gboolean    res;
 
128
        GError     *error;
 
129
        gchar      *normalized_codeset = NULL;
 
130
        gchar      *normalized_name = NULL;
 
131
 
 
132
        error = NULL;
 
133
        re = g_regex_new ("^(?P<language>[^_.@[:space:]]+)"
 
134
                          "(_(?P<territory>[[:upper:]]+))?"
 
135
                          "(\\.(?P<codeset>[-_0-9a-zA-Z]+))?"
 
136
                          "(@(?P<modifier>[[:ascii:]]+))?$",
 
137
                          0, 0, &error);
 
138
        if (re == NULL) {
 
139
                g_critical ("%s", error->message);
 
140
                return;
 
141
        }
 
142
 
 
143
        if (!g_regex_match (re, name, 0, &match_info) ||
 
144
            g_match_info_is_partial_match (match_info)) {
 
145
                g_match_info_free (match_info);
 
146
                g_regex_unref (re);
 
147
                g_warning ("locale %s isn't valid\n", name);
 
148
                return;
 
149
        }
 
150
 
 
151
        res = g_match_info_matches (match_info);
 
152
        if (! res) {
 
153
                g_warning ("Unable to parse locale: %s", name);
 
154
                return;
 
155
        }
 
156
 
 
157
        if (language_codep != NULL) {
 
158
                *language_codep = g_match_info_fetch_named (match_info, "language");
 
159
        }
 
160
 
 
161
        if (territory_codep != NULL) {
 
162
                *territory_codep = g_match_info_fetch_named (match_info, "territory");
 
163
 
 
164
                if (*territory_codep != NULL &&
 
165
                    *territory_codep[0] == '\0') {
 
166
                        g_free (*territory_codep);
 
167
                        *territory_codep = NULL;
 
168
                }
 
169
        }
 
170
 
 
171
        if (codesetp != NULL) {
 
172
                *codesetp = g_match_info_fetch_named (match_info, "codeset");
 
173
 
 
174
                if (*codesetp != NULL &&
 
175
                    *codesetp[0] == '\0') {
 
176
                        g_free (*codesetp);
 
177
                        *codesetp = NULL;
 
178
                }
 
179
        }
 
180
 
 
181
        if (modifierp != NULL) {
 
182
                *modifierp = g_match_info_fetch_named (match_info, "modifier");
 
183
 
 
184
                if (*modifierp != NULL &&
 
185
                    *modifierp[0] == '\0') {
 
186
                        g_free (*modifierp);
 
187
                        *modifierp = NULL;
 
188
                }
 
189
        }
 
190
 
 
191
        if (codesetp != NULL && *codesetp != NULL) {
 
192
                normalized_codeset = normalize_codeset (*codesetp);
 
193
                normalized_name = construct_language_name (language_codep ? *language_codep : NULL,
 
194
                                                           territory_codep ? *territory_codep : NULL,
 
195
                                                           normalized_codeset,
 
196
                                                           modifierp ? *modifierp : NULL);
 
197
 
 
198
                if (language_name_is_valid (normalized_name)) {
 
199
                        g_free (*codesetp);
 
200
                        *codesetp = normalized_codeset;
 
201
                } else {
 
202
                        g_free (normalized_codeset);
 
203
                }
 
204
                g_free (normalized_name);
 
205
        }
 
206
 
 
207
        g_match_info_free (match_info);
 
208
        g_regex_unref (re);
 
209
}
 
210
 
 
211
static char *
 
212
construct_language_name (const char *language,
 
213
                         const char *territory,
 
214
                         const char *codeset,
 
215
                         const char *modifier)
 
216
{
 
217
        char *name;
 
218
 
 
219
        g_assert (language[0] != 0);
 
220
        g_assert (territory == NULL || territory[0] != 0);
 
221
        g_assert (codeset == NULL || codeset[0] != 0);
 
222
        g_assert (modifier == NULL || modifier[0] != 0);
 
223
 
 
224
        name = g_strdup_printf ("%s%s%s%s%s%s%s",
 
225
                                language,
 
226
                                territory != NULL? "_" : "",
 
227
                                territory != NULL? territory : "",
 
228
                                codeset != NULL? "." : "",
 
229
                                codeset != NULL? codeset : "",
 
230
                                modifier != NULL? "@" : "",
 
231
                                modifier != NULL? modifier : "");
 
232
 
 
233
        return name;
 
234
}
 
235
 
 
236
char *
 
237
gdm_normalize_language_name (const char *name)
 
238
{
 
239
        char *normalized_name;
 
240
        char *language_code;
 
241
        char *territory_code;
 
242
        char *codeset;
 
243
        char *modifier;
 
244
 
 
245
        if (name[0] == '\0') {
 
246
                return NULL;
 
247
        }
 
248
 
 
249
        gdm_parse_language_name (name,
 
250
                                 &language_code,
 
251
                                 &territory_code,
 
252
                                 &codeset, &modifier);
 
253
 
 
254
        normalized_name = construct_language_name (language_code,
 
255
                                                   territory_code,
 
256
                                                   codeset, modifier);
 
257
        g_free (language_code);
 
258
        g_free (territory_code);
 
259
        g_free (codeset);
 
260
        g_free (modifier);
 
261
 
 
262
        return normalized_name;
 
263
}
 
264
 
 
265
static gboolean
 
266
language_name_is_valid (const char *language_name)
 
267
{
 
268
        char     *old_locale;
 
269
        gboolean  is_valid;
 
270
#ifdef WITH_INCOMPLETE_LOCALES
 
271
        int lc_type_id = LC_CTYPE;
 
272
#else
 
273
        int lc_type_id = LC_MESSAGES;
 
274
#endif
 
275
 
 
276
        old_locale = g_strdup (setlocale (lc_type_id, NULL));
 
277
        is_valid = setlocale (lc_type_id, language_name) != NULL;
 
278
        setlocale (lc_type_id, old_locale);
 
279
        g_free (old_locale);
 
280
 
 
281
        return is_valid;
 
282
}
 
283
 
 
284
static void
 
285
language_name_get_codeset_details (const char  *language_name,
 
286
                                   char       **pcodeset,
 
287
                                   gboolean    *is_utf8)
 
288
{
 
289
        char     *old_locale;
 
290
        char     *codeset;
 
291
 
 
292
        old_locale = g_strdup (setlocale (LC_CTYPE, NULL));
 
293
 
 
294
        if (setlocale (LC_CTYPE, language_name) == NULL) {
 
295
                g_free (old_locale);
 
296
                return;
 
297
        }
 
298
 
 
299
        codeset = nl_langinfo (CODESET);
 
300
 
 
301
        if (pcodeset != NULL) {
 
302
                *pcodeset = codeset;
 
303
        }
 
304
 
 
305
        if (is_utf8 != NULL) {
 
306
                codeset = normalize_codeset (codeset);
 
307
 
 
308
                *is_utf8 = strcmp (codeset, "utf8") == 0;
 
309
                g_free (codeset);
 
310
        }
 
311
 
 
312
        setlocale (LC_CTYPE, old_locale);
 
313
        g_free (old_locale);
 
314
}
 
315
 
 
316
static gboolean
 
317
language_name_has_translations (const char *language_name)
 
318
{
 
319
        GDir        *dir;
 
320
        char        *path;
 
321
        const char  *name;
 
322
        gboolean     has_translations;
 
323
 
 
324
        path = g_build_filename (GNOMELOCALEDIR, language_name, "LC_MESSAGES", NULL);
 
325
 
 
326
        has_translations = FALSE;
 
327
        dir = g_dir_open (path, 0, NULL);
 
328
        g_free (path);
 
329
 
 
330
        if (dir == NULL) {
 
331
                goto out;
 
332
        }
 
333
 
 
334
        do {
 
335
                name = g_dir_read_name (dir);
 
336
 
 
337
                if (name == NULL) {
 
338
                        break;
 
339
                }
 
340
 
 
341
                if (g_str_has_suffix (name, ".mo")) {
 
342
                        has_translations = TRUE;
 
343
                        break;
 
344
                }
 
345
        } while (name != NULL);
 
346
        g_dir_close (dir);
 
347
 
 
348
out:
 
349
        return has_translations;
 
350
}
 
351
 
 
352
static gboolean
 
353
add_locale (const char *language_name,
 
354
            gboolean    utf8_only)
 
355
{
 
356
        GdmLocale *locale;
 
357
        GdmLocale *old_locale;
 
358
        char      *name;
 
359
        gboolean   is_utf8;
 
360
 
 
361
        g_return_val_if_fail (language_name != NULL, FALSE);
 
362
 
 
363
        language_name_get_codeset_details (language_name, NULL, &is_utf8);
 
364
 
 
365
        if (is_utf8) {
 
366
                name = g_strdup (language_name);
 
367
        } else if (utf8_only) {
 
368
                name = g_strdup_printf ("%s.utf8", language_name);
 
369
 
 
370
                language_name_get_codeset_details (name, NULL, &is_utf8);
 
371
                if (is_utf8) {
 
372
                        g_free (name);
 
373
                        return FALSE;
 
374
                }
 
375
        } else {
 
376
                name = g_strdup (language_name);
 
377
        }
 
378
 
 
379
        if (!language_name_is_valid (name)) {
 
380
                g_warning ("Your locale '%s' was failed by setlocale()", name);
 
381
                g_free (name);
 
382
                return FALSE;
 
383
        }
 
384
 
 
385
        locale = g_new0 (GdmLocale, 1);
 
386
        gdm_parse_language_name (name,
 
387
                                 &locale->language_code,
 
388
                                 &locale->territory_code,
 
389
                                 &locale->codeset,
 
390
                                 &locale->modifier);
 
391
        g_free (name);
 
392
        name = NULL;
 
393
 
 
394
#ifdef WITH_INCOMPLETE_LOCALES
 
395
        if (utf8_only) {
 
396
                if (locale->territory_code == NULL || locale->modifier) {
 
397
                        gdm_locale_free (locale);
 
398
                        return FALSE;
 
399
                }
 
400
        }
 
401
#endif
 
402
 
 
403
        locale->id = construct_language_name (locale->language_code, locale->territory_code,
 
404
                                              NULL, locale->modifier);
 
405
        locale->name = construct_language_name (locale->language_code, locale->territory_code,
 
406
                                                locale->codeset, locale->modifier);
 
407
 
 
408
#ifndef WITH_INCOMPLETE_LOCALES
 
409
        if (!language_name_has_translations (locale->name) &&
 
410
            !language_name_has_translations (locale->id) &&
 
411
            !language_name_has_translations (locale->language_code) &&
 
412
            utf8_only) {
 
413
                g_warning ("Your locale '%s' doesn't have message catalog files",
 
414
                           language_name);
 
415
                gdm_locale_free (locale);
 
416
                return FALSE;
 
417
        }
 
418
#endif
 
419
 
 
420
        if (!utf8_only) {
 
421
                g_free (locale->id);
 
422
                locale->id = g_strdup (locale->name);
 
423
        }
 
424
 
 
425
        old_locale = g_hash_table_lookup (gdm_available_locales_map, locale->id);
 
426
        if (old_locale != NULL) {
 
427
                if (strlen (old_locale->name) > strlen (locale->name)) {
 
428
                        gdm_locale_free (locale);
 
429
                        return FALSE;
 
430
                }
 
431
        }
 
432
 
 
433
        g_hash_table_insert (gdm_available_locales_map, g_strdup (locale->id), locale);
 
434
 
 
435
        return TRUE;
 
436
}
 
437
 
 
438
struct nameent
 
439
{
 
440
        char    *name;
 
441
        uint32_t locrec_offset;
 
442
};
 
443
 
 
444
static gboolean
 
445
collect_locales_from_archive (void)
 
446
{
 
447
        GMappedFile        *mapped;
 
448
        GError             *error;
 
449
        char               *addr;
 
450
        struct locarhead   *head;
 
451
        struct namehashent *namehashtab;
 
452
        struct nameent     *names;
 
453
        uint32_t            used;
 
454
        uint32_t            cnt;
 
455
        gsize               len;
 
456
        gboolean            locales_collected;
 
457
 
 
458
        error = NULL;
 
459
        mapped = g_mapped_file_new (ARCHIVE_FILE, FALSE, &error);
 
460
        if (mapped == NULL) {
 
461
                g_warning ("Mapping failed for %s: %s", ARCHIVE_FILE, error->message);
 
462
                g_error_free (error);
 
463
                return FALSE;
 
464
        }
 
465
 
 
466
        locales_collected = FALSE;
 
467
 
 
468
        addr = g_mapped_file_get_contents (mapped);
 
469
        len = g_mapped_file_get_length (mapped);
 
470
 
 
471
        head = (struct locarhead *) addr;
 
472
        if (head->namehash_offset + head->namehash_size > len
 
473
            || head->string_offset + head->string_size > len
 
474
            || head->locrectab_offset + head->locrectab_size > len
 
475
            || head->sumhash_offset + head->sumhash_size > len) {
 
476
                goto out;
 
477
        }
 
478
 
 
479
        namehashtab = (struct namehashent *) (addr + head->namehash_offset);
 
480
 
 
481
        names = (struct nameent *) g_new0 (struct nameent, head->namehash_used);
 
482
        for (cnt = used = 0; cnt < head->namehash_size; ++cnt) {
 
483
                if (namehashtab[cnt].locrec_offset != 0) {
 
484
                        names[used].name = addr + namehashtab[cnt].name_offset;
 
485
                        names[used++].locrec_offset = namehashtab[cnt].locrec_offset;
 
486
                }
 
487
        }
 
488
 
 
489
        for (cnt = 0; cnt < used; ++cnt) {
 
490
                add_locale (names[cnt].name, TRUE);
 
491
        }
 
492
 
 
493
        g_free (names);
 
494
 
 
495
        locales_collected = TRUE;
 
496
 out:
 
497
 
 
498
        g_mapped_file_unref (mapped);
 
499
        return locales_collected;
 
500
}
 
501
 
 
502
static int
 
503
select_dirs (const struct dirent *dirent)
 
504
{
 
505
        int result = 0;
 
506
 
 
507
        if (strcmp (dirent->d_name, ".") != 0 && strcmp (dirent->d_name, "..") != 0) {
 
508
                mode_t mode = 0;
 
509
 
 
510
#ifdef _DIRENT_HAVE_D_TYPE
 
511
                if (dirent->d_type != DT_UNKNOWN && dirent->d_type != DT_LNK) {
 
512
                        mode = DTTOIF (dirent->d_type);
 
513
                } else
 
514
#endif
 
515
                        {
 
516
                                struct stat st;
 
517
                                char       *path;
 
518
 
 
519
                                path = g_build_filename (LIBLOCALEDIR, dirent->d_name, NULL);
 
520
                                if (g_stat (path, &st) == 0) {
 
521
                                        mode = st.st_mode;
 
522
                                }
 
523
                                g_free (path);
 
524
                        }
 
525
 
 
526
                result = S_ISDIR (mode);
 
527
        }
 
528
 
 
529
        return result;
 
530
}
 
531
 
 
532
static void
 
533
collect_locales_from_directory (void)
 
534
{
 
535
        struct dirent **dirents;
 
536
        int             ndirents;
 
537
        int             cnt;
 
538
 
 
539
        ndirents = scandir (LIBLOCALEDIR, &dirents, select_dirs, alphasort);
 
540
 
 
541
        for (cnt = 0; cnt < ndirents; ++cnt) {
 
542
                add_locale (dirents[cnt]->d_name, TRUE);
 
543
        }
 
544
 
 
545
        if (ndirents > 0) {
 
546
                free (dirents);
 
547
        }
 
548
}
 
549
 
 
550
static void
 
551
collect_locales_from_locale_file (const char *locale_file)
 
552
{
 
553
        FILE *langlist;
 
554
        char curline[256];
 
555
        char *getsret;
 
556
 
 
557
        if (locale_file == NULL)
 
558
                return;
 
559
 
 
560
        langlist = fopen (locale_file, "r");
 
561
 
 
562
        if (langlist == NULL)
 
563
                return;
 
564
 
 
565
        for (;;) {
 
566
                char *name;
 
567
                char *lang;
 
568
                char **lang_list;
 
569
                int i;
 
570
 
 
571
                getsret = fgets (curline, sizeof (curline), langlist);
 
572
                if (getsret == NULL)
 
573
                        break;
 
574
 
 
575
                if (curline[0] <= ' ' ||
 
576
                    curline[0] == '#')
 
577
                        continue;
 
578
 
 
579
                name = strtok (curline, " \t\r\n");
 
580
                if (name == NULL)
 
581
                        continue;
 
582
 
 
583
                lang = strtok (NULL, " \t\r\n");
 
584
                if (lang == NULL)
 
585
                        continue;
 
586
 
 
587
                lang_list = g_strsplit (lang, ",", -1);
 
588
                if (lang_list == NULL)
 
589
                        continue;
 
590
 
 
591
                lang = NULL;
 
592
                for (i = 0; lang_list[i] != NULL; i++) {
 
593
                        if (add_locale (lang_list[i], FALSE)) {
 
594
                                break;
 
595
                        }
 
596
                }
 
597
                g_strfreev (lang_list);
 
598
        }
 
599
 
 
600
        fclose (langlist);
 
601
}
 
602
 
 
603
static void
 
604
collect_locales (void)
 
605
{
 
606
 
 
607
        if (gdm_available_locales_map == NULL) {
 
608
                gdm_available_locales_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, (GDestroyNotify) gdm_locale_free);
 
609
        }
 
610
 
 
611
        if (!collect_locales_from_archive ()) {
 
612
#ifndef WITH_INCOMPLETE_LOCALES
 
613
                g_warning ("Could not read list of available locales from libc, "
 
614
                           "guessing possible locales from available translations, "
 
615
                           "but list may be incomplete!");
 
616
#endif
 
617
 
 
618
                collect_locales_from_directory ();
 
619
        }
 
620
        collect_locales_from_locale_file (ALIASES_FILE);
 
621
}
 
622
 
 
623
static gboolean
 
624
is_fallback_language (const char *code)
 
625
{
 
626
        const char *fallback_language_names[] = { "C", "POSIX", NULL };
 
627
        int i;
 
628
 
 
629
        for (i = 0; fallback_language_names[i] != NULL; i++) {
 
630
                if (strcmp (code, fallback_language_names[i]) == 0) {
 
631
                        return TRUE;
 
632
                }
 
633
        }
 
634
 
 
635
        return FALSE;
 
636
}
 
637
 
 
638
static const char *
 
639
get_language (const char *code)
 
640
{
 
641
        const char *name;
 
642
        int         len;
 
643
 
 
644
        g_assert (code != NULL);
 
645
 
 
646
        if (is_fallback_language (code)) {
 
647
                return "Unspecified";
 
648
        }
 
649
 
 
650
        len = strlen (code);
 
651
        if (len != 2 && len != 3) {
 
652
                return NULL;
 
653
        }
 
654
 
 
655
        name = (const char *) g_hash_table_lookup (gdm_languages_map, code);
 
656
 
 
657
        return name;
 
658
}
 
659
 
 
660
static char *
 
661
get_first_item_in_semicolon_list (const char *list)
 
662
{
 
663
    char **items;
 
664
    char  *item;
 
665
 
 
666
    /* Some entries in iso codes have multiple values, separated
 
667
     * by semicolons.  Not really sure which one to pick, so
 
668
     * we just arbitrarily pick the first one.
 
669
     */
 
670
    items = g_strsplit (list, "; ", 2);
 
671
 
 
672
    item = g_strdup (items[0]);
 
673
    g_strfreev (items);
 
674
 
 
675
    return item;
 
676
}
 
677
 
 
678
static char *
 
679
get_translated_language (const char *code,
 
680
                         const char *locale)
 
681
{
 
682
        const char *language;
 
683
        char *name;
 
684
 
 
685
        language = get_language (code);
 
686
 
 
687
        name = NULL;
 
688
        if (language != NULL) {
 
689
                const char  *translated_name;
 
690
                char        *old_locale;
 
691
 
 
692
                if (locale != NULL) {
 
693
                        old_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
 
694
                        setlocale (LC_MESSAGES, locale);
 
695
                }
 
696
 
 
697
                if (is_fallback_language (code)) {
 
698
                        name = _("Unspecified");
 
699
                } else {
 
700
                        translated_name = dgettext ("iso_639", language);
 
701
                        name = get_first_item_in_semicolon_list (translated_name);
 
702
                }
 
703
 
 
704
                if (locale != NULL) {
 
705
                        setlocale (LC_MESSAGES, old_locale);
 
706
                        g_free (old_locale);
 
707
                }
 
708
        }
 
709
 
 
710
        return name;
 
711
}
 
712
 
 
713
static const char *
 
714
get_territory (const char *code)
 
715
{
 
716
        const char *name;
 
717
        int         len;
 
718
 
 
719
        g_assert (code != NULL);
 
720
 
 
721
        len = strlen (code);
 
722
        if (len != 2 && len != 3) {
 
723
                return NULL;
 
724
        }
 
725
 
 
726
        name = (const char *) g_hash_table_lookup (gdm_territories_map, code);
 
727
 
 
728
        return name;
 
729
}
 
730
 
 
731
static const char *
 
732
get_translated_territory (const char *code,
 
733
                          const char *locale)
 
734
{
 
735
        const char *territory;
 
736
        char *name;
 
737
 
 
738
        territory = get_territory (code);
 
739
 
 
740
        name = NULL;
 
741
        if (territory != NULL) {
 
742
                const char *translated_territory;
 
743
                char       *old_locale;
 
744
 
 
745
                if (locale != NULL) {
 
746
                        old_locale = g_strdup (setlocale (LC_MESSAGES, NULL));
 
747
                        setlocale (LC_MESSAGES, locale);
 
748
                }
 
749
 
 
750
                translated_territory = dgettext ("iso_3166", territory);
 
751
                name = get_first_item_in_semicolon_list (translated_territory);
 
752
 
 
753
                if (locale != NULL) {
 
754
                        setlocale (LC_MESSAGES, old_locale);
 
755
                        g_free (old_locale);
 
756
                }
 
757
        }
 
758
 
 
759
        return name;
 
760
}
 
761
 
 
762
static void
 
763
languages_parse_start_tag (GMarkupParseContext      *ctx,
 
764
                           const char               *element_name,
 
765
                           const char              **attr_names,
 
766
                           const char              **attr_values,
 
767
                           gpointer                  user_data,
 
768
                           GError                  **error)
 
769
{
 
770
        const char *ccode_longB;
 
771
        const char *ccode_longT;
 
772
        const char *ccode;
 
773
        const char *lang_name;
 
774
 
 
775
        if (! g_str_equal (element_name, "iso_639_entry") || attr_names == NULL || attr_values == NULL) {
 
776
                return;
 
777
        }
 
778
 
 
779
        ccode = NULL;
 
780
        ccode_longB = NULL;
 
781
        ccode_longT = NULL;
 
782
        lang_name = NULL;
 
783
 
 
784
        while (*attr_names && *attr_values) {
 
785
                if (g_str_equal (*attr_names, "iso_639_1_code")) {
 
786
                        /* skip if empty */
 
787
                        if (**attr_values) {
 
788
                                if (strlen (*attr_values) != 2) {
 
789
                                        return;
 
790
                                }
 
791
                                ccode = *attr_values;
 
792
                        }
 
793
                } else if (g_str_equal (*attr_names, "iso_639_2B_code")) {
 
794
                        /* skip if empty */
 
795
                        if (**attr_values) {
 
796
                                if (strlen (*attr_values) != 3) {
 
797
                                        return;
 
798
                                }
 
799
                                ccode_longB = *attr_values;
 
800
                        }
 
801
                } else if (g_str_equal (*attr_names, "iso_639_2T_code")) {
 
802
                        /* skip if empty */
 
803
                        if (**attr_values) {
 
804
                                if (strlen (*attr_values) != 3) {
 
805
                                        return;
 
806
                                }
 
807
                                ccode_longT = *attr_values;
 
808
                        }
 
809
                } else if (g_str_equal (*attr_names, "name")) {
 
810
                        lang_name = *attr_values;
 
811
                }
 
812
 
 
813
                ++attr_names;
 
814
                ++attr_values;
 
815
        }
 
816
 
 
817
        if (lang_name == NULL) {
 
818
                return;
 
819
        }
 
820
 
 
821
        if (ccode != NULL) {
 
822
                g_hash_table_insert (gdm_languages_map,
 
823
                                     g_strdup (ccode),
 
824
                                     g_strdup (lang_name));
 
825
        }
 
826
        if (ccode_longB != NULL) {
 
827
                g_hash_table_insert (gdm_languages_map,
 
828
                                     g_strdup (ccode_longB),
 
829
                                     g_strdup (lang_name));
 
830
        }
 
831
        if (ccode_longT != NULL) {
 
832
                g_hash_table_insert (gdm_languages_map,
 
833
                                     g_strdup (ccode_longT),
 
834
                                     g_strdup (lang_name));
 
835
        }
 
836
}
 
837
 
 
838
static void
 
839
territories_parse_start_tag (GMarkupParseContext      *ctx,
 
840
                             const char               *element_name,
 
841
                             const char              **attr_names,
 
842
                             const char              **attr_values,
 
843
                             gpointer                  user_data,
 
844
                             GError                  **error)
 
845
{
 
846
        const char *acode_2;
 
847
        const char *acode_3;
 
848
        const char *ncode;
 
849
        const char *territory_common_name;
 
850
        const char *territory_name;
 
851
 
 
852
        if (! g_str_equal (element_name, "iso_3166_entry") || attr_names == NULL || attr_values == NULL) {
 
853
                return;
 
854
        }
 
855
 
 
856
        acode_2 = NULL;
 
857
        acode_3 = NULL;
 
858
        ncode = NULL;
 
859
        territory_common_name = NULL;
 
860
        territory_name = NULL;
 
861
 
 
862
        while (*attr_names && *attr_values) {
 
863
                if (g_str_equal (*attr_names, "alpha_2_code")) {
 
864
                        /* skip if empty */
 
865
                        if (**attr_values) {
 
866
                                if (strlen (*attr_values) != 2) {
 
867
                                        return;
 
868
                                }
 
869
                                acode_2 = *attr_values;
 
870
                        }
 
871
                } else if (g_str_equal (*attr_names, "alpha_3_code")) {
 
872
                        /* skip if empty */
 
873
                        if (**attr_values) {
 
874
                                if (strlen (*attr_values) != 3) {
 
875
                                        return;
 
876
                                }
 
877
                                acode_3 = *attr_values;
 
878
                        }
 
879
                } else if (g_str_equal (*attr_names, "numeric_code")) {
 
880
                        /* skip if empty */
 
881
                        if (**attr_values) {
 
882
                                if (strlen (*attr_values) != 3) {
 
883
                                        return;
 
884
                                }
 
885
                                ncode = *attr_values;
 
886
                        }
 
887
                } else if (g_str_equal (*attr_names, "common_name")) {
 
888
                        /* skip if empty */
 
889
                        if (**attr_values) {
 
890
                                territory_common_name = *attr_values;
 
891
                        }
 
892
                } else if (g_str_equal (*attr_names, "name")) {
 
893
                        territory_name = *attr_values;
 
894
                }
 
895
 
 
896
                ++attr_names;
 
897
                ++attr_values;
 
898
        }
 
899
 
 
900
        if (territory_common_name != NULL) {
 
901
                territory_name = territory_common_name;
 
902
        }
 
903
 
 
904
        if (territory_name == NULL) {
 
905
                return;
 
906
        }
 
907
 
 
908
        if (acode_2 != NULL) {
 
909
                g_hash_table_insert (gdm_territories_map,
 
910
                                     g_strdup (acode_2),
 
911
                                     g_strdup (territory_name));
 
912
        }
 
913
        if (acode_3 != NULL) {
 
914
                g_hash_table_insert (gdm_territories_map,
 
915
                                     g_strdup (acode_3),
 
916
                                     g_strdup (territory_name));
 
917
        }
 
918
        if (ncode != NULL) {
 
919
                g_hash_table_insert (gdm_territories_map,
 
920
                                     g_strdup (ncode),
 
921
                                     g_strdup (territory_name));
 
922
        }
 
923
}
 
924
 
 
925
static void
 
926
languages_init (void)
 
927
{
 
928
        GError  *error;
 
929
        gboolean res;
 
930
        char    *buf;
 
931
        gsize    buf_len;
 
932
 
 
933
        bindtextdomain ("iso_639", ISO_CODES_LOCALESDIR);
 
934
        bind_textdomain_codeset ("iso_639", "UTF-8");
 
935
 
 
936
        gdm_languages_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
937
 
 
938
        error = NULL;
 
939
        res = g_file_get_contents (ISO_CODES_DATADIR "/iso_639.xml",
 
940
                                   &buf,
 
941
                                   &buf_len,
 
942
                                   &error);
 
943
        if (res) {
 
944
                GMarkupParseContext *ctx;
 
945
                GMarkupParser        parser = { languages_parse_start_tag, NULL, NULL, NULL, NULL };
 
946
 
 
947
                ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
 
948
 
 
949
                error = NULL;
 
950
                res = g_markup_parse_context_parse (ctx, buf, buf_len, &error);
 
951
 
 
952
                if (! res) {
 
953
                        g_warning ("Failed to parse '%s': %s\n",
 
954
                                   ISO_CODES_DATADIR "/iso_639.xml",
 
955
                                   error->message);
 
956
                        g_error_free (error);
 
957
                }
 
958
 
 
959
                g_markup_parse_context_free (ctx);
 
960
                g_free (buf);
 
961
        } else {
 
962
                g_warning ("Failed to load '%s': %s\n",
 
963
                           ISO_CODES_DATADIR "/iso_639.xml",
 
964
                           error->message);
 
965
                g_error_free (error);
 
966
        }
 
967
}
 
968
 
 
969
static void
 
970
territories_init (void)
 
971
{
 
972
        GError  *error;
 
973
        gboolean res;
 
974
        char    *buf;
 
975
        gsize    buf_len;
 
976
 
 
977
        bindtextdomain ("iso_3166", ISO_CODES_LOCALESDIR);
 
978
        bind_textdomain_codeset ("iso_3166", "UTF-8");
 
979
 
 
980
        gdm_territories_map = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 
981
 
 
982
        error = NULL;
 
983
        res = g_file_get_contents (ISO_CODES_DATADIR "/iso_3166.xml",
 
984
                                   &buf,
 
985
                                   &buf_len,
 
986
                                   &error);
 
987
        if (res) {
 
988
                GMarkupParseContext *ctx;
 
989
                GMarkupParser        parser = { territories_parse_start_tag, NULL, NULL, NULL, NULL };
 
990
 
 
991
                ctx = g_markup_parse_context_new (&parser, 0, NULL, NULL);
 
992
 
 
993
                error = NULL;
 
994
                res = g_markup_parse_context_parse (ctx, buf, buf_len, &error);
 
995
 
 
996
                if (! res) {
 
997
                        g_warning ("Failed to parse '%s': %s\n",
 
998
                                   ISO_CODES_DATADIR "/iso_3166.xml",
 
999
                                   error->message);
 
1000
                        g_error_free (error);
 
1001
                }
 
1002
 
 
1003
                g_markup_parse_context_free (ctx);
 
1004
                g_free (buf);
 
1005
        } else {
 
1006
                g_warning ("Failed to load '%s': %s\n",
 
1007
                           ISO_CODES_DATADIR "/iso_3166.xml",
 
1008
                           error->message);
 
1009
                g_error_free (error);
 
1010
        }
 
1011
}
 
1012
 
 
1013
char *
 
1014
gdm_get_language_from_name (const char *name,
 
1015
                            const char *locale)
 
1016
{
 
1017
        char *full_language;
 
1018
        char *language_code;
 
1019
        char *territory_code;
 
1020
        char *codeset_code;
 
1021
        char *langinfo_codeset;
 
1022
        char *language;
 
1023
        const char *territory;
 
1024
        gboolean is_utf8 = TRUE;
 
1025
 
 
1026
        if (gdm_languages_map == NULL) {
 
1027
                languages_init ();
 
1028
        }
 
1029
 
 
1030
        if (gdm_territories_map == NULL) {
 
1031
                territories_init ();
 
1032
        }
 
1033
 
 
1034
        language_code = NULL;
 
1035
        territory_code = NULL;
 
1036
        codeset_code = NULL;
 
1037
 
 
1038
        gdm_parse_language_name (name, &language_code, &territory_code,
 
1039
                                 &codeset_code, NULL);
 
1040
 
 
1041
        if (language_code == NULL) {
 
1042
                goto out;
 
1043
        }
 
1044
 
 
1045
        language = get_translated_language (language_code, locale);
 
1046
 
 
1047
        if (territory_code != NULL) {
 
1048
                territory = get_translated_territory (territory_code, locale);
 
1049
        } else {
 
1050
                territory = NULL;
 
1051
        }
 
1052
 
 
1053
        language_name_get_codeset_details (name, &langinfo_codeset, &is_utf8);
 
1054
 
 
1055
        if (codeset_code == NULL && langinfo_codeset != NULL) {
 
1056
            codeset_code = g_strdup (langinfo_codeset);
 
1057
        }
 
1058
 
 
1059
        full_language  = NULL;
 
1060
 
 
1061
        if (language) {
 
1062
                full_language  = g_strdup (language);
 
1063
        } else {
 
1064
                goto out;
 
1065
        }
 
1066
 
 
1067
        if (territory) {
 
1068
                language = full_language;
 
1069
                full_language  = g_strdup_printf ("%s (%s)",
 
1070
                                                  language, territory);
 
1071
                g_free (language);
 
1072
        }
 
1073
 
 
1074
        if (!is_utf8 && codeset_code) {
 
1075
                language = full_language;
 
1076
                full_language  = g_strdup_printf ("%s [%s]",
 
1077
                                                  language, codeset_code);
 
1078
                g_free (language);
 
1079
        }
 
1080
 
 
1081
out:
 
1082
       g_free (language_code);
 
1083
       g_free (territory_code);
 
1084
       g_free (codeset_code);
 
1085
 
 
1086
       return full_language;
 
1087
}
 
1088
 
 
1089
char **
 
1090
gdm_get_all_language_names (void)
 
1091
{
 
1092
        GHashTableIter iter;
 
1093
        gpointer key, value;
 
1094
        GPtrArray *array;
 
1095
 
 
1096
        if (gdm_available_locales_map == NULL) {
 
1097
                collect_locales ();
 
1098
        }
 
1099
 
 
1100
        array = g_ptr_array_new ();
 
1101
        g_hash_table_iter_init (&iter, gdm_available_locales_map);
 
1102
        while (g_hash_table_iter_next (&iter, &key, &value)) {
 
1103
                GdmLocale *locale;
 
1104
 
 
1105
                locale = (GdmLocale *) value;
 
1106
 
 
1107
                g_ptr_array_add (array, g_strdup (locale->name));
 
1108
        }
 
1109
        g_ptr_array_add (array, NULL);
 
1110
 
 
1111
        return (char **) g_ptr_array_free (array, FALSE);
 
1112
}