Imported Upstream version 0.2.0
[debian/lxdm.git] / src / lang.c
CommitLineData
eea1c851
AL
1/*
2 * lang.c - load language list
3 *
4 * Copyright 2009 dgod <dgod.osa@gmail.com>
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 3 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., 51 Franklin Street, Fifth Floor, Boston,
19 * MA 02110-1301, USA.
20 */
21
22#include "lang.h"
23
24#include <stdio.h>
faee902e 25#include <stdlib.h>
eea1c851 26#include <glib.h>
faee902e 27#include <glib/gi18n.h>
eea1c851
AL
28#include <string.h>
29
30#ifdef HAVE_CONFIG_H
31#include <config.h>
32#endif
33
34#ifndef LXDM_DATA_DIR
faee902e 35#define LXDM_DATA_DIR "/usr/share/lxdm"
eea1c851
AL
36#endif
37
38#include "gdm/gdm-languages.h"
39
faee902e 40static int cmpstr(const void *p1, const void *p2)
eea1c851 41{
faee902e
AL
42 return strcmp(* (char * const *) p1, * (char * const *) p2);
43}
44
45static char **lxdm_get_all_language_names(void)
46{
47#if 0
48 GPtrArray *array;
49 FILE *fp;
50 array=g_ptr_array_new();
51 fp=fopen(LXDM_DATA_DIR "/lang.txt","r");
52 if(fp)
53 {
54 char line[128];
55 while(fgets(line,128,fp)!=NULL)
56 {
57 char *p=strchr(line,'\n');
58 if(*p) *p=0;
59 g_ptr_array_add(array,g_strdup(line));
60 }
61 fclose(fp);
62 }
63 g_ptr_array_add (array, NULL);
64 return (char **) g_ptr_array_free (array, FALSE);
65#else
66 char **list,**lang;
67 int len;
68 list=gdm_get_all_language_names();
69 if(!list) return NULL;
70 for(lang=list;*lang!=NULL;lang++)
71 {
72 char *normal=gdm_normalize_language_name(*lang);
73 if(normal)
74 {
75 g_free(*lang);
76 *lang=normal;
77 }
78 }
79 len=g_strv_length(list);
80 qsort(list,len,sizeof(char*),(void*)cmpstr);
81 return list;
82#endif
83}
84
85static char **lxdm_get_config_language_names(GKeyFile *config)
86{
87 char **list;
eea1c851 88
faee902e
AL
89 list=g_key_file_get_string_list(config,"base","last_langs",NULL,NULL);
90 if(!list)
91 {
92 list=g_malloc0(sizeof(char*));
93 }
94 return list;
95}
96
97void lxdm_load_langs(GKeyFile *config, gboolean all, void *arg, void (*cb)(void *arg, char *lang, char *desc))
98{
99 char **lang;
100 int i;
101
102 cb(arg, "", _("Default")); /* default is to use the system wide settings ,not use the "C" */
eea1c851 103
faee902e
AL
104 lang = all?lxdm_get_all_language_names():lxdm_get_config_language_names(config);
105 if(!lang) return;
eea1c851 106
faee902e 107 for(i=0;lang[i]!=NULL;i++)
eea1c851 108 {
faee902e
AL
109 char *readable=gdm_get_language_from_name(lang[i],lang[i]);
110 cb(arg,lang[i],readable);
eea1c851 111 g_free(readable);
eea1c851 112 }
faee902e
AL
113
114 g_strfreev(lang);
115
116 if(!all) cb(arg,"~",_("More ..."));
eea1c851 117}
faee902e 118