~kroq-gar78/ubuntu/precise/gnome-control-center/fix-885947

« back to all changes in this revision

Viewing changes to panels/common/gdm-languages.c

  • Committer: Bazaar Package Importer
  • Author(s): Rodrigo Moya
  • Date: 2011-05-17 10:47:27 UTC
  • mfrom: (0.1.11 experimental) (1.1.45 upstream)
  • Revision ID: james.westby@ubuntu.com-20110517104727-lqel6m8vhfw5jby1
Tags: 1:3.0.1.1-1ubuntu1
* Rebase on Debian, remaining Ubuntu changes:
* debian/control:
  - Build-Depend on hardening-wrapper, dpkg-dev and dh-autoreconf
  - Add dependency on ubuntu-system-service
  - Remove dependency on gnome-icon-theme-symbolic
  - Move dependency on apg, gnome-icon-theme-symbolic and accountsservice to
    be a Recommends: until we get them in main
* debian/rules:
  - Use autoreconf
  - Add binary-post-install rule for gnome-control-center-data
  - Run dh-autoreconf
* debian/gnome-control-center.dirs:
* debian/gnome-control-center.links:
  - Add a link to the control center shell for indicators
* debian/patches/00_disable-nm.patch:
  - Temporary patch to disable building with NetworkManager until we get
    the new one in the archive
* debian/patches/01_git_remove_gettext_calls.patch:
  - Remove calls to AM_GNU_GETTEXT, IT_PROG_INTLTOOL should be enough
* debian/patches/01_git_kill_warning.patch:
  - Kill warning
* debian/patches/50_ubuntu_systemwide_prefs.patch:
  - Ubuntu specific proxy preferences
* debian/patches/51_ubuntu_system_keyboard.patch:
  - Implement the global keyboard spec at https://wiki.ubuntu.com/DefaultKeyboardSettings

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