Imported Upstream version 0.2.0
[debian/lxdm.git] / src / lang.c
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>
25 #include <stdlib.h>
26 #include <glib.h>
27 #include <glib/gi18n.h>
28 #include <string.h>
29
30 #ifdef HAVE_CONFIG_H
31 #include <config.h>
32 #endif
33
34 #ifndef LXDM_DATA_DIR
35 #define LXDM_DATA_DIR "/usr/share/lxdm"
36 #endif
37
38 #include "gdm/gdm-languages.h"
39
40 static int cmpstr(const void *p1, const void *p2)
41 {
42 return strcmp(* (char * const *) p1, * (char * const *) p2);
43 }
44
45 static 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
85 static char **lxdm_get_config_language_names(GKeyFile *config)
86 {
87 char **list;
88
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
97 void 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" */
103
104 lang = all?lxdm_get_all_language_names():lxdm_get_config_language_names(config);
105 if(!lang) return;
106
107 for(i=0;lang[i]!=NULL;i++)
108 {
109 char *readable=gdm_get_language_from_name(lang[i],lang[i]);
110 cb(arg,lang[i],readable);
111 g_free(readable);
112 }
113
114 g_strfreev(lang);
115
116 if(!all) cb(arg,"~",_("More ..."));
117 }
118